Mercurial > public > ostc4
comparison Small_CPU/Src/externalInterface.c @ 779:0b5f45448eb6
Added UART multiplexer support for DiveO2:
The existing autodetect function now includes a UART multiplexer. The datastructures and protocol handling has been adapted to support several DiveO2 sensors connected to the UART.
author | Ideenmodellierer |
---|---|
date | Tue, 23 May 2023 21:45:34 +0200 |
parents | 4a28402e4aca |
children | 01b3eb9d55c3 |
comparison
equal
deleted
inserted
replaced
778:74253a41cf80 | 779:0b5f45448eb6 |
---|---|
65 static uint8_t externalUART_Protocol = 0; | 65 static uint8_t externalUART_Protocol = 0; |
66 static uint16_t externalCO2Value; | 66 static uint16_t externalCO2Value; |
67 static uint16_t externalCO2SignalStrength; | 67 static uint16_t externalCO2SignalStrength; |
68 static uint16_t externalCO2Status = 0; | 68 static uint16_t externalCO2Status = 0; |
69 | 69 |
70 static uint8_t sensorDataId = 0; | 70 static uint8_t lastSensorDataId = 0; |
71 static SSensorDataDiveO2 sensorDataDiveO2; | 71 static SSensorDataDiveO2 sensorDataDiveO2[MAX_ADC_CHANNEL]; |
72 static externalInterfaceAutoDetect_t externalAutoDetect = DETECTION_OFF; | 72 static externalInterfaceAutoDetect_t externalAutoDetect = DETECTION_OFF; |
73 static externalInterfaceSensorType SensorMap[EXT_INTERFACE_SENSOR_CNT] ={ SENSOR_OPTIC, SENSOR_OPTIC, SENSOR_OPTIC, SENSOR_NONE, SENSOR_NONE}; | 73 static externalInterfaceSensorType SensorMap[EXT_INTERFACE_SENSOR_CNT] ={ SENSOR_OPTIC, SENSOR_OPTIC, SENSOR_OPTIC, SENSOR_NONE, SENSOR_NONE}; |
74 static externalInterfaceSensorType tmpSensorMap[EXT_INTERFACE_SENSOR_CNT]; | 74 static externalInterfaceSensorType tmpSensorMap[EXT_INTERFACE_SENSOR_CNT]; |
75 static externalInterfaceSensorType MasterSensorMap[EXT_INTERFACE_SENSOR_CNT]; | 75 static externalInterfaceSensorType MasterSensorMap[EXT_INTERFACE_SENSOR_CNT]; |
76 | 76 |
329 { | 329 { |
330 case 0: | 330 case 0: |
331 case (EXT_INTERFACE_UART_CO2 >> 8): | 331 case (EXT_INTERFACE_UART_CO2 >> 8): |
332 case (EXT_INTERFACE_UART_O2 >> 8): | 332 case (EXT_INTERFACE_UART_O2 >> 8): |
333 case (EXT_INTERFACE_UART_SENTINEL >> 8): | 333 case (EXT_INTERFACE_UART_SENTINEL >> 8): |
334 if((externalAutoDetect <= DETECTION_START) || ((protocol == EXT_INTERFACE_UART_O2 >> 8) && (externalAutoDetect == DETECTION_DIGO2)) | 334 if((externalAutoDetect <= DETECTION_START) |
335 || ((protocol == EXT_INTERFACE_UART_O2 >> 8) && (externalAutoDetect == DETECTION_DIGO2_0)) | |
336 || ((protocol == EXT_INTERFACE_UART_O2 >> 8) && (externalAutoDetect == DETECTION_DIGO2_1)) | |
337 || ((protocol == EXT_INTERFACE_UART_O2 >> 8) && (externalAutoDetect == DETECTION_DIGO2_2)) | |
338 || ((protocol == EXT_INTERFACE_UART_O2 >> 8) && (externalAutoDetect == DETECTION_UARTMUX)) | |
335 #ifdef ENABLE_CO2_SUPPORT | 339 #ifdef ENABLE_CO2_SUPPORT |
336 || ((protocol == EXT_INTERFACE_UART_CO2 >> 8) && (externalAutoDetect == DETECTION_CO2)) | 340 || ((protocol == EXT_INTERFACE_UART_CO2 >> 8) && (externalAutoDetect == DETECTION_CO2)) |
337 #endif | 341 #endif |
338 #ifdef ENABLE_SENTINEL_MODE | 342 #ifdef ENABLE_SENTINEL_MODE |
339 || ((protocol == EXT_INTERFACE_UART_SENTINEL >> 8) && (externalAutoDetect == DETECTION_SENTINEL)) | 343 || ((protocol == EXT_INTERFACE_UART_SENTINEL >> 8) && (externalAutoDetect == DETECTION_SENTINEL)) |
340 #endif | 344 #endif |
341 ) | 345 ) |
342 { | 346 { |
343 sensorDataId = 0; | 347 lastSensorDataId = 0; |
344 externalUART_Protocol = protocol; | 348 externalUART_Protocol = protocol; |
345 MX_USART1_UART_DeInit(); | 349 MX_USART1_UART_DeInit(); |
346 if( protocol != 0) | 350 if( protocol != 0) |
347 { | 351 { |
348 MX_USART1_UART_Init(); | 352 MX_USART1_UART_Init(); |
402 { | 406 { |
403 return externalCO2Status; | 407 return externalCO2Status; |
404 } | 408 } |
405 | 409 |
406 | 410 |
407 uint8_t externalInterface_GetSensorData(uint8_t* pDataStruct) | 411 uint8_t externalInterface_GetSensorData(uint8_t sensorId, uint8_t* pDataStruct) |
408 { | 412 { |
409 | 413 uint8_t localId = sensorId; |
410 if((pDataStruct != NULL) && sensorDataId != 0) | 414 if(localId == 0) |
411 { | 415 { |
412 memcpy(pDataStruct, &sensorDataDiveO2, sizeof(sensorDataDiveO2)); | 416 localId = lastSensorDataId; |
413 } | 417 } |
414 return sensorDataId; | 418 |
419 if((pDataStruct != NULL) && (localId > 0) && (localId <= MAX_ADC_CHANNEL)) | |
420 { | |
421 memcpy(pDataStruct, &sensorDataDiveO2[localId-1], sizeof(SSensorDataDiveO2)); | |
422 } | |
423 return localId; | |
415 } | 424 } |
416 | 425 |
417 void externalInterface_SetSensorData(uint8_t dataId, uint8_t* pDataStruct) | 426 void externalInterface_SetSensorData(uint8_t dataId, uint8_t* pDataStruct) |
418 { | 427 { |
419 if(pDataStruct != NULL) | 428 if(pDataStruct != NULL) |
420 { | 429 { |
421 if(dataId != 0) | 430 if((dataId != 0) && (dataId <= MAX_ADC_CHANNEL)) |
422 { | 431 { |
423 memcpy(&sensorDataDiveO2, pDataStruct, sizeof(sensorDataDiveO2)); | 432 memcpy(&sensorDataDiveO2[dataId-1], pDataStruct, sizeof(SSensorDataDiveO2)); |
424 } | 433 } |
425 else | 434 else |
426 { | 435 { |
427 memset(&sensorDataDiveO2,0,sizeof(sensorDataDiveO2)); | 436 memset(&sensorDataDiveO2,0,sizeof(sensorDataDiveO2)); |
428 } | 437 } |
429 sensorDataId = dataId; | 438 lastSensorDataId = dataId; |
430 } | 439 } |
431 } | 440 } |
432 | 441 |
433 void externalInface_SetSensorMap(uint8_t* pMap) | 442 void externalInface_SetSensorMap(uint8_t* pMap) |
434 { | 443 { |
453 return pret; | 462 return pret; |
454 } | 463 } |
455 | 464 |
456 void externalInterface_AutodetectSensor() | 465 void externalInterface_AutodetectSensor() |
457 { | 466 { |
458 static uint8_t sensorIndex = 0; | 467 static uint8_t tmpMuxMapping[MAX_ADC_CHANNEL]; |
468 static uint8_t sensorIndex = 0; | |
469 static uint8_t uartMuxChannel = 0; | |
459 uint8_t index = 0; | 470 uint8_t index = 0; |
460 | 471 |
461 if(externalAutoDetect != DETECTION_OFF) | 472 if(externalAutoDetect != DETECTION_OFF) |
462 { | 473 { |
463 switch(externalAutoDetect) | 474 switch(externalAutoDetect) |
464 { | 475 { |
465 case DETECTION_INIT: sensorIndex = 0; | 476 case DETECTION_INIT: sensorIndex = 0; |
477 uartMuxChannel = 0; | |
466 tmpSensorMap[0] = SENSOR_OPTIC; | 478 tmpSensorMap[0] = SENSOR_OPTIC; |
467 tmpSensorMap[1] = SENSOR_OPTIC; | 479 tmpSensorMap[1] = SENSOR_OPTIC; |
468 tmpSensorMap[2] = SENSOR_OPTIC; | 480 tmpSensorMap[2] = SENSOR_OPTIC; |
469 tmpSensorMap[3] = SENSOR_NONE; | 481 tmpSensorMap[3] = SENSOR_NONE; |
470 tmpSensorMap[4] = SENSOR_NONE; | 482 tmpSensorMap[4] = SENSOR_NONE; |
471 | 483 |
484 for(index = 0; index < MAX_ADC_CHANNEL; index++) | |
485 { | |
486 UART_MapDigO2_Channel(index,index); /* request all addresses */ | |
487 tmpMuxMapping[index] = 0xff; | |
488 } | |
472 if(externalInterfacePresent) | 489 if(externalInterfacePresent) |
473 { | 490 { |
474 externalInterface_SwitchPower33(0); | 491 externalInterface_SwitchPower33(0); |
475 externalInterface_SwitchUART(0); | 492 externalInterface_SwitchUART(0); |
476 for(index = 0; index < MAX_ADC_CHANNEL; index++) | 493 for(index = 0; index < MAX_ADC_CHANNEL; index++) |
502 else | 519 else |
503 { | 520 { |
504 tmpSensorMap[sensorIndex++] = SENSOR_NONE; | 521 tmpSensorMap[sensorIndex++] = SENSOR_NONE; |
505 } | 522 } |
506 } | 523 } |
507 externalAutoDetect = DETECTION_DIGO2; | 524 externalAutoDetect = DETECTION_UARTMUX; |
508 externalInterface_SwitchUART(EXT_INTERFACE_UART_O2 >> 8); | 525 externalInterface_SwitchUART(EXT_INTERFACE_UART_O2 >> 8); |
509 break; | 526 UART_SetDigO2_Channel(3); |
510 case DETECTION_DIGO2: if(UART_isDigO2Connected()) | 527 break; |
528 case DETECTION_UARTMUX: if(UART_isDigO2Connected()) | |
529 { | |
530 uartMuxChannel = 1; | |
531 } | |
532 externalAutoDetect = DETECTION_DIGO2_0; | |
533 externalInterface_SwitchUART(EXT_INTERFACE_UART_O2 >> 8); | |
534 UART_SetDigO2_Channel(0); | |
535 | |
536 break; | |
537 case DETECTION_DIGO2_0: | |
538 case DETECTION_DIGO2_1: | |
539 case DETECTION_DIGO2_2: if(UART_isDigO2Connected()) | |
511 { | 540 { |
512 for(index = 0; index < 3; index++) /* lookup a channel which may be used by digO2 */ | 541 for(index = 0; index < 3; index++) /* lookup a channel which may be used by digO2 */ |
513 { | 542 { |
514 if(tmpSensorMap[index] == SENSOR_NONE) | 543 if(tmpSensorMap[index] == SENSOR_NONE) |
515 { | 544 { |
521 tmpSensorMap[2] = SENSOR_DIGO2; /* digital sensor overwrites ADC */ | 550 tmpSensorMap[2] = SENSOR_DIGO2; /* digital sensor overwrites ADC */ |
522 } | 551 } |
523 else | 552 else |
524 { | 553 { |
525 tmpSensorMap[index] = SENSOR_DIGO2; | 554 tmpSensorMap[index] = SENSOR_DIGO2; |
526 } | 555 tmpMuxMapping[index] = externalAutoDetect - DETECTION_DIGO2_0; |
527 | 556 } |
528 UART_setTargetChannel(index); | 557 UART_setTargetChannel(index); |
558 | |
529 /* tmpSensorMap[sensorIndex++] = SENSOR_DIGO2; */ | 559 /* tmpSensorMap[sensorIndex++] = SENSOR_DIGO2; */ |
560 } | |
561 if(uartMuxChannel) | |
562 { | |
563 externalInterface_SwitchUART(EXT_INTERFACE_UART_O2 >> 8); | |
564 UART_SetDigO2_Channel(uartMuxChannel); | |
565 uartMuxChannel++; | |
566 } | |
567 else | |
568 { | |
569 externalAutoDetect = DETECTION_DIGO2_2; /* skip detection of other serial sensors */ | |
530 } | 570 } |
531 externalAutoDetect++; | 571 externalAutoDetect++; |
532 #ifdef ENABLE_CO2_SUPPORT | 572 #ifdef ENABLE_CO2_SUPPORT |
533 externalInterface_SwitchUART(EXT_INTERFACE_UART_CO2 >> 8); | 573 if(externalAutoDetect == DETECTION_CO2) |
574 { | |
575 externalInterface_SwitchUART(EXT_INTERFACE_UART_CO2 >> 8); | |
576 } | |
534 break; | 577 break; |
535 case DETECTION_CO2: if(UART_isCO2Connected()) | 578 case DETECTION_CO2: if(UART_isCO2Connected()) |
536 { | 579 { |
537 for(index = 0; index < 3; index++) /* lookup a channel which may be used by CO2*/ | 580 for(index = 0; index < 3; index++) /* lookup a channel which may be used by CO2*/ |
538 { | 581 { |
552 | 595 |
553 } | 596 } |
554 externalAutoDetect++; | 597 externalAutoDetect++; |
555 #endif | 598 #endif |
556 #ifdef ENABLE_SENTINEL_MODE | 599 #ifdef ENABLE_SENTINEL_MODE |
557 externalInterface_SwitchUART(EXT_INTERFACE_UART_SENTINEL >> 8); | 600 if(externalAutoDetect == DETECTION_SENTINEL) |
558 UART_StartDMA_Receiption(); | 601 { |
602 externalInterface_SwitchUART(EXT_INTERFACE_UART_SENTINEL >> 8); | |
603 UART_StartDMA_Receiption(); | |
604 } | |
559 break; | 605 break; |
560 | 606 |
561 case DETECTION_SENTINEL: | 607 case DETECTION_SENTINEL: |
562 case DETECTION_SENTINEL2: | 608 case DETECTION_SENTINEL2: |
563 if(UART_isSentinelConnected()) | 609 if(UART_isSentinelConnected()) |
569 sensorIndex = 3; | 615 sensorIndex = 3; |
570 } | 616 } |
571 externalAutoDetect++; | 617 externalAutoDetect++; |
572 #endif | 618 #endif |
573 break; | 619 break; |
574 case DETECTION_DONE: for(index = 0; index < EXT_INTERFACE_SENSOR_CNT; index++) | 620 case DETECTION_DONE: if(uartMuxChannel) |
575 { | 621 { |
576 if(tmpSensorMap[index] != SENSOR_NONE) | 622 tmpSensorMap[EXT_INTERFACE_SENSOR_CNT-1] = SENSOR_MUX; |
577 { | 623 } |
578 break; | 624 for(index = 0; index < MAX_ADC_CHANNEL; index++) |
579 } | 625 { |
580 } | 626 UART_MapDigO2_Channel(index,tmpMuxMapping[index]); |
581 | 627 } |
582 if(index != EXT_INTERFACE_SENSOR_CNT) /* return default sensor map if no sensor at all has been detected */ | 628 externalAutoDetect = DETECTION_OFF; |
583 { | 629 externalInterface_SwitchUART(0); |
584 while(sensorIndex < EXT_INTERFACE_SENSOR_CNT) | 630 UART_SetDigO2_Channel(0); |
585 { | |
586 tmpSensorMap[sensorIndex++] = SENSOR_NONE; | |
587 } | |
588 } | |
589 else | |
590 { | |
591 tmpSensorMap[0] = SENSOR_OPTIC; | |
592 tmpSensorMap[1] = SENSOR_OPTIC; | |
593 tmpSensorMap[2] = SENSOR_OPTIC; | |
594 } | |
595 memcpy(SensorMap, tmpSensorMap, sizeof(tmpSensorMap)); | 631 memcpy(SensorMap, tmpSensorMap, sizeof(tmpSensorMap)); |
596 externalAutoDetect = DETECTION_OFF; | 632 |
597 break; | 633 break; |
598 default: | 634 default: |
599 break; | 635 break; |
600 } | 636 } |
601 } | 637 } |