comparison Small_CPU/Src/uart.c @ 784:95af969fe0ae

Bugfixloop UART multiplexer integration: Fixed problems showing up in cases like no Mux detected, only one sensor connected to mux and mixed analog / digital sensor operation
author Ideenmodellierer
date Tue, 30 May 2023 18:44:20 +0200
parents 01b3eb9d55c3
children 3c0b16473af4
comparison
equal deleted inserted replaced
783:c31237d20491 784:95af969fe0ae
156 break; 156 break;
157 case UART_O2_REQ_INFO: *cmdLength = snprintf((char*)cmdString, 10, "#VERS"); 157 case UART_O2_REQ_INFO: *cmdLength = snprintf((char*)cmdString, 10, "#VERS");
158 break; 158 break;
159 case UART_O2_REQ_ID: *cmdLength = snprintf((char*)cmdString, 10, "#IDNR"); 159 case UART_O2_REQ_ID: *cmdLength = snprintf((char*)cmdString, 10, "#IDNR");
160 break; 160 break;
161 case UART_O2_REQ_O2: *cmdLength = snprintf((char*)cmdString, 10, "#DOXY"); 161 case UART_O2_REQ_O2: *cmdLength = snprintf((char*)cmdString, 10, "#MOXY");
162 break; 162 break;
163 case UART_O2_REQ_RAW: *cmdLength = snprintf((char*)cmdString, 10, "#DRAW"); 163 case UART_O2_REQ_RAW: *cmdLength = snprintf((char*)cmdString, 10, "#MRAW");
164 break; 164 break;
165 default: *cmdLength = 0; 165 default: *cmdLength = 0;
166 break; 166 break;
167 } 167 }
168 if(*cmdLength != 0) 168 if(*cmdLength != 0)
476 476
477 477
478 void UART_HandleDigitalO2(void) 478 void UART_HandleDigitalO2(void)
479 { 479 {
480 static uint32_t lastO2ReqTick = 0; 480 static uint32_t lastO2ReqTick = 0;
481 481 static uint8_t errorStr[] = "#ERRO";
482 static uartO2RxState_t rxState = O2RX_IDLE; 482 static uartO2RxState_t rxState = O2RX_IDLE;
483 static uint32_t lastReceiveTick = 0; 483 static uint32_t lastReceiveTick = 0;
484 static uint8_t lastAlive = 0; 484 static uint8_t lastAlive = 0;
485 static uint8_t curAlive = 0; 485 static uint8_t curAlive = 0;
486 486
487 static uint8_t cmdLength = 0; 487 static uint8_t cmdLength = 0;
488 static uint8_t cmdString[10]; 488 static uint8_t cmdString[10];
489 static uint8_t cmdReadIndex = 0; 489 static uint8_t cmdReadIndex = 0;
490 static uint8_t errorReadIndex = 0;
490 static uint32_t tickToTX = 0; 491 static uint32_t tickToTX = 0;
491 static uint32_t delayStartTick = 0; 492 static uint32_t delayStartTick = 0;
492 static uint16_t requestIntervall = 0; 493 static uint16_t requestIntervall = 0;
493 static uint8_t retryRequest = 1; 494 static uint8_t retryRequest = 1;
494 static uint8_t lastComState = 0; 495 static uint8_t lastComState = 0;
495 496 static uint8_t respondErrorDetected = 0;
496 uint8_t switchChannel = 0; 497 static uint8_t switchChannel = 0;
498
497 uint8_t index = 0; 499 uint8_t index = 0;
498 uint32_t tmpO2 = 0; 500 uint32_t tmpO2 = 0;
499 uint32_t tmpData = 0; 501 uint32_t tmpData = 0;
500 uint8_t localRX = rxReadIndex; 502 uint8_t localRX = rxReadIndex;
501 uint32_t tick = HAL_GetTick(); 503 uint32_t tick = HAL_GetTick();
530 delayStartTick = tick; 532 delayStartTick = tick;
531 tickToTX = COMMAND_TX_DELAY; 533 tickToTX = COMMAND_TX_DELAY;
532 534
533 rxState = O2RX_CONFIRM; 535 rxState = O2RX_CONFIRM;
534 cmdReadIndex = 0; 536 cmdReadIndex = 0;
537 errorReadIndex = 0;
538 respondErrorDetected = 0;
539 digO2Connected = 0;
535 lastO2ReqTick = tick; 540 lastO2ReqTick = tick;
541 switchChannel = 1;
536 542
537 requestIntervall = 0; 543 requestIntervall = 0;
538 for(index = 0; index < MAX_MUX_CHANNEL; index++) 544 for(index = 0; index < MAX_MUX_CHANNEL; index++)
539 { 545 {
540 if(pmap[index] == SENSOR_DIGO2) 546 if(pmap[index] == SENSOR_DIGO2)
554 } 560 }
555 if(time_elapsed_ms(lastO2ReqTick,tick) > requestIntervall) /* repeat request or iterate to next sensor */ 561 if(time_elapsed_ms(lastO2ReqTick,tick) > requestIntervall) /* repeat request or iterate to next sensor */
556 { 562 {
557 lastO2ReqTick = tick; 563 lastO2ReqTick = tick;
558 index = activeSensor; 564 index = activeSensor;
559 if(lastComState == Comstatus_O2) 565 if((lastComState == Comstatus_O2) && (Comstatus_O2 != UART_O2_IDLE))
560 { 566 {
561 if(retryRequest) 567 if(retryRequest)
562 { 568 {
563 retryRequest = 0; 569 retryRequest = 0;
564 } 570 }
575 if(Comstatus_O2 == UART_O2_IDLE) /* cyclic request of o2 value */ 581 if(Comstatus_O2 == UART_O2_IDLE) /* cyclic request of o2 value */
576 { 582 {
577 retryRequest = 1; 583 retryRequest = 1;
578 if(pmap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX) /* select next sensor if mux is connected */ 584 if(pmap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX) /* select next sensor if mux is connected */
579 { 585 {
586 switchChannel = 0;
580 if(activeSensor < MAX_MUX_CHANNEL) 587 if(activeSensor < MAX_MUX_CHANNEL)
581 { 588 {
582 do 589 do
583 { 590 {
584 index++; 591 index++;
585 if(index == MAX_MUX_CHANNEL) 592 if(index == MAX_MUX_CHANNEL)
586 { 593 {
587 index = 0; 594 index = 0;
588 } 595 }
589 if(pmap[index] == SENSOR_DIGO2) 596 if((pmap[index] == SENSOR_DIGO2) && (index != activeSensor))
590 { 597 {
591 activeSensor = index; 598 activeSensor = index;
592 switchChannel = 1; 599 switchChannel = 1;
593 break; 600 break;
594 } 601 }
598 Comstatus_O2 = UART_O2_REQ_RAW; 605 Comstatus_O2 = UART_O2_REQ_RAW;
599 rxState = O2RX_CONFIRM; 606 rxState = O2RX_CONFIRM;
600 } 607 }
601 if(switchChannel) 608 if(switchChannel)
602 { 609 {
610 switchChannel = 0;
603 delayStartTick = tick; 611 delayStartTick = tick;
604 DigitalO2_SelectSensor(activeSensor); 612 DigitalO2_SelectSensor(activeSensor);
605 externalInterface_GetSensorData(activeSensor + 1, (uint8_t*)&tmpSensorDataDiveO2); 613 externalInterface_GetSensorData(activeSensor + 1, (uint8_t*)&tmpSensorDataDiveO2);
606 tickToTX = COMMAND_TX_DELAY; 614 tickToTX = COMMAND_TX_DELAY;
607 if(tmpSensorDataDiveO2.sensorId == 0) 615 if(tmpSensorDataDiveO2.sensorId == 0)
622 switch(rxState) 630 switch(rxState)
623 { 631 {
624 case O2RX_CONFIRM: if(rxBuffer[localRX] == '#') 632 case O2RX_CONFIRM: if(rxBuffer[localRX] == '#')
625 { 633 {
626 cmdReadIndex = 0; 634 cmdReadIndex = 0;
635 errorReadIndex = 0;
636 }
637 if(errorReadIndex < sizeof(errorStr)-1)
638 {
639 if(rxBuffer[localRX] == errorStr[errorReadIndex])
640 {
641 errorReadIndex++;
642 }
643 else
644 {
645 errorReadIndex = 0;
646 }
647 }
648 else
649 {
650 respondErrorDetected = 1;
627 } 651 }
628 if(rxBuffer[localRX] == cmdString[cmdReadIndex]) 652 if(rxBuffer[localRX] == cmdString[cmdReadIndex])
629 { 653 {
630 cmdReadIndex++; 654 cmdReadIndex++;
631 if(cmdReadIndex == cmdLength - 1) 655 if(cmdReadIndex == cmdLength - 1)
632 { 656 {
633 digO2Connected = 1; 657 if((activeSensor == MAX_MUX_CHANNEL))
658 {
659 if(respondErrorDetected)
660 {
661 digO2Connected = 0; /* the multiplexer mirrors the incoming message and does not generate an error information => no mux connected */
662 }
663 else
664 {
665 digO2Connected = 1;
666 }
667 }
668 else /* handle sensors which should respond with an error message after channel switch */
669 {
670 if(respondErrorDetected)
671 {
672 digO2Connected = 1;
673 }
674 }
634 tmpRxIdx = 0; 675 tmpRxIdx = 0;
635 memset((char*) tmpRxBuf, 0, sizeof(tmpRxBuf)); 676 memset((char*) tmpRxBuf, 0, sizeof(tmpRxBuf));
636 switch (Comstatus_O2) 677 switch (Comstatus_O2)
637 { 678 {
638 case UART_O2_CHECK: Comstatus_O2 = UART_O2_IDLE; 679 case UART_O2_CHECK: Comstatus_O2 = UART_O2_IDLE;
736 rxState = O2RX_IDLE; 777 rxState = O2RX_IDLE;
737 break; 778 break;
738 case O2RX_GETNR: StringToUInt64((char*)tmpRxBuf,&tmpSensorDataDiveO2.sensorId); 779 case O2RX_GETNR: StringToUInt64((char*)tmpRxBuf,&tmpSensorDataDiveO2.sensorId);
739 externalInterface_SetSensorData(activeSensor+1,(uint8_t*)&tmpSensorDataDiveO2); 780 externalInterface_SetSensorData(activeSensor+1,(uint8_t*)&tmpSensorDataDiveO2);
740 index = activeSensor; 781 index = activeSensor;
741 782 Comstatus_O2 = UART_O2_IDLE;
742 if(switchChannel == 0) 783 rxState = O2RX_IDLE;
743 {
744 Comstatus_O2 = UART_O2_IDLE;
745 rxState = O2RX_IDLE;
746 }
747 break; 784 break;
748 default: Comstatus_O2 = UART_O2_IDLE; 785 default: Comstatus_O2 = UART_O2_IDLE;
749 rxState = O2RX_IDLE; 786 rxState = O2RX_IDLE;
750 break; 787 break;
751 } 788 }