comparison Small_CPU/Src/externalInterface.c @ 804:391b3d420a39

Cleanup initialisation code: Some variables were not initialised during awake transition and the mapping of to the legacy adc slots were done at several location. This has been cleaned up not. Do not run UART during ADC sensor detection: A operation UART sensor caused the false detection of a not connected ADC sensor. To avoid this the UART is now deactivated during ADC detection phase.
author Ideenmodellierer
date Thu, 10 Aug 2023 21:35:34 +0200
parents e9eba334b942
children 9602a7338f28
comparison
equal deleted inserted replaced
803:96ffad0a4e57 804:391b3d420a39
87 static uint8_t Mux2ADCMap[MAX_ADC_CHANNEL]; 87 static uint8_t Mux2ADCMap[MAX_ADC_CHANNEL];
88 static uint8_t externalInterface_SensorState[EXT_INTERFACE_SENSOR_CNT]; 88 static uint8_t externalInterface_SensorState[EXT_INTERFACE_SENSOR_CNT];
89 89
90 static float LookupCO2PressureCorrection[LOOKUP_CO2_CORR_TABLE_MAX / LOOKUP_CO2_CORR_TABLE_SCALE]; /* lookup table for pressure compensation values */ 90 static float LookupCO2PressureCorrection[LOOKUP_CO2_CORR_TABLE_MAX / LOOKUP_CO2_CORR_TABLE_SCALE]; /* lookup table for pressure compensation values */
91 91
92 static uint16_t externalInterfaceMuxReqIntervall = 0; /* delay between switching from one MUX channel to the next */ 92 static uint16_t externalInterfaceMuxReqIntervall = 0xffff; /* delay between switching from one MUX channel to the next */
93 static uint8_t activeUartChannel = 0; /* Index of the sensor port which is selected by the mux or 0 if no mux is connected */ 93 static uint8_t activeUartChannel = 0; /* Index of the sensor port which is selected by the mux or 0 if no mux is connected */
94
95
96 static void externalInface_MapUartToLegacyADC(uint8_t* pMap);
94 97
95 void externalInterface_Init(void) 98 void externalInterface_Init(void)
96 { 99 {
97 uint16_t index; 100 uint16_t index;
98 uint16_t coeff; 101 uint16_t coeff;
104 externalInterfacePresent = 1; 107 externalInterfacePresent = 1;
105 global.deviceDataSendToMaster.hw_Info.extADC = 1; 108 global.deviceDataSendToMaster.hw_Info.extADC = 1;
106 } 109 }
107 global.deviceDataSendToMaster.hw_Info.checkADC = 1; 110 global.deviceDataSendToMaster.hw_Info.checkADC = 1;
108 111
109 memset(Mux2ADCMap,0xFF, sizeof(Mux2ADCMap)); 112
110 113
111 /* Create a lookup table based on GSS application note AN001: PRESSURE COMPENSATION OF A CO2 SENSOR */ 114 /* Create a lookup table based on GSS application note AN001: PRESSURE COMPENSATION OF A CO2 SENSOR */
112 /* The main purpose of the sensor in the dive application is to be a warning indicator */ 115 /* The main purpose of the sensor in the dive application is to be a warning indicator */
113 /* => no exact values necessary => a lookup table with 1000ppm scaling should be sufficient */ 116 /* => no exact values necessary => a lookup table with 1000ppm scaling should be sufficient */
114 LookupCO2PressureCorrection [0] = -0.0014; 117 LookupCO2PressureCorrection [0] = -0.0014;
123 void externalInterface_InitDatastruct(void) 126 void externalInterface_InitDatastruct(void)
124 { 127 {
125 uint8_t index = 0; 128 uint8_t index = 0;
126 /* init data values */ 129 /* init data values */
127 externalV33_On = 0; 130 externalV33_On = 0;
131 externalADC_On = 0;
132 externalUART_Protocol = 0;
128 externalCO2Value = 0; 133 externalCO2Value = 0;
129 externalCO2SignalStrength = 0; 134 externalCO2SignalStrength = 0;
130 externalCO2Status = 0; 135 externalCO2Status = 0;
136 externalCO2Scale = 0.0;
131 externalAutoDetect = DETECTION_OFF; 137 externalAutoDetect = DETECTION_OFF;
132 138
133 for(index = 0; index < MAX_ADC_CHANNEL; index++) 139 for(index = 0; index < MAX_ADC_CHANNEL; index++)
134 { 140 {
135 externalChannel_mV[index] = 0.0; 141 externalChannel_mV[index] = 0.0;
136 } 142 }
137 memset(externalInterface_SensorState,UART_COMMON_INIT,sizeof(externalInterface_SensorState)); 143 memset(externalInterface_SensorState,UART_COMMON_INIT,sizeof(externalInterface_SensorState));
144 externalInface_MapUartToLegacyADC(SensorMap);
145 activeUartChannel = 0xFF;
138 } 146 }
139 147
140 148
141 uint8_t externalInterface_StartConversion(uint8_t channel) 149 uint8_t externalInterface_StartConversion(uint8_t channel)
142 { 150 {
513 { 521 {
514 if((sensorId != 0xFF) && (sensorId < EXT_INTERFACE_SENSOR_CNT)) 522 if((sensorId != 0xFF) && (sensorId < EXT_INTERFACE_SENSOR_CNT))
515 { 523 {
516 memcpy(&sensorDataDiveO2[sensorId], pDataStruct, sizeof(SSensorDataDiveO2)); 524 memcpy(&sensorDataDiveO2[sensorId], pDataStruct, sizeof(SSensorDataDiveO2));
517 lastSensorDataId = sensorId; 525 lastSensorDataId = sensorId;
518 for(index = 0; index < MAX_MUX_CHANNEL; index++) 526 if(sensorId >= MAX_ADC_CHANNEL)
519 { 527 {
520 if(Mux2ADCMap[index] == sensorId) 528 for(index = 0; index < MAX_ADC_CHANNEL; index++)
521 { 529 {
522 memcpy(&sensorDataDiveO2[index], pDataStruct, sizeof(SSensorDataDiveO2)); 530 if(Mux2ADCMap[index] == sensorId)
523 lastSensorDataId = index; 531 {
532 memcpy(&sensorDataDiveO2[index], pDataStruct, sizeof(SSensorDataDiveO2));
533 lastSensorDataId = index;
534 break;
535 }
536 }
537 }
538 }
539 else
540 {
541 memset(&sensorDataDiveO2,0,sizeof(sensorDataDiveO2));
542 lastSensorDataId = 0xFF;
543 }
544 }
545 }
546
547 void externalInface_SetSensorMap(uint8_t* pMap)
548 {
549 if(pMap != NULL)
550 {
551 memcpy(MasterSensorMap, pMap, EXT_INTERFACE_SENSOR_CNT); /* the map is not directly copied. Copy is done via cmd request */
552 }
553
554 }
555
556 void externalInface_MapUartToLegacyADC(uint8_t* pMap)
557 {
558 uint8_t index, index2;
559
560 memset(Mux2ADCMap,0xFF, sizeof(Mux2ADCMap));
561
562 for(index2 = 0; index2 < MAX_ADC_CHANNEL; index2++) /* Unmap old mirror instances */
563 {
564 if((pMap[index2] == SENSOR_DIGO2M) || (pMap[index2] == SENSOR_CO2M))
565 {
566 pMap[index2] = SENSOR_NONE;
567 }
568 }
569
570 /* Map Mux O2 sensors to ADC Slot if ADC slot is not in use */
571 for(index = 0; index < EXT_INTERFACE_SENSOR_CNT-1; index++)
572 {
573 if(pMap[index] == SENSOR_DIGO2)
574 {
575 for(index2 = 0; index2 < MAX_ADC_CHANNEL; index2++)
576 {
577 if(pMap[index2] == SENSOR_NONE)
578 {
579 pMap[index2] = SENSOR_DIGO2M; /* store a mirror instance needed for visualization */
580 Mux2ADCMap[index2] = index;
524 break; 581 break;
525 } 582 }
526 } 583 }
527 } 584 }
528 else 585 }
529 { 586 for(index = 0; index < EXT_INTERFACE_SENSOR_CNT-1; index++)
530 memset(&sensorDataDiveO2,0,sizeof(sensorDataDiveO2)); 587 {
531 lastSensorDataId = 0xFF; 588 if(pMap[index] == SENSOR_CO2)
532 } 589 {
533 } 590 for(index2 = 0; index2 < MAX_ADC_CHANNEL; index2++)
534 } 591 {
535 592 if(pMap[index2] == SENSOR_NONE)
536 void externalInface_SetSensorMap(uint8_t* pMap) 593 {
537 { 594 pMap[index2] = SENSOR_CO2M; /* store a mirror instance needed for visualization */
538 if(pMap != NULL) 595 Mux2ADCMap[index2] = index;
539 { 596 break;
540 memcpy(MasterSensorMap, pMap, EXT_INTERFACE_SENSOR_CNT); /* the map is not directly copied. Copy is done via cmd request */ 597 }
541 } 598 }
542 599 }
543 } 600 }
601 }
602
544 uint8_t* externalInterface_GetSensorMapPointer(uint8_t finalMap) 603 uint8_t* externalInterface_GetSensorMapPointer(uint8_t finalMap)
545 { 604 {
546 uint8_t* pret; 605 uint8_t* pret;
547 606
548 if((externalAutoDetect != DETECTION_OFF) && (!finalMap)) 607 if((externalAutoDetect != DETECTION_OFF) && (!finalMap))
559 void externalInterface_AutodetectSensor() 618 void externalInterface_AutodetectSensor()
560 { 619 {
561 static uint8_t sensorIndex = 0; 620 static uint8_t sensorIndex = 0;
562 static uint8_t uartMuxChannel = 0; 621 static uint8_t uartMuxChannel = 0;
563 uint8_t index = 0; 622 uint8_t index = 0;
564 uint8_t index2 = 0;
565 uint8_t cntSensor = 0; 623 uint8_t cntSensor = 0;
566 uint8_t cntUARTSensor = 0; 624 uint8_t cntUARTSensor = 0;
567 #ifdef ENABLE_CO2_SUPPORT 625 #ifdef ENABLE_CO2_SUPPORT
568 uint8_t cmdString[10]; 626 uint8_t cmdString[10];
569 uint8_t cmdLength = 0; 627 uint8_t cmdLength = 0;
571 629
572 if(externalAutoDetect != DETECTION_OFF) 630 if(externalAutoDetect != DETECTION_OFF)
573 { 631 {
574 switch(externalAutoDetect) 632 switch(externalAutoDetect)
575 { 633 {
576 case DETECTION_INIT: externalInterfaceMuxReqIntervall = 1100; 634 case DETECTION_INIT: externalInterfaceMuxReqIntervall = 0;
577 sensorIndex = 0; 635 sensorIndex = 0;
578 uartMuxChannel = 0; 636 uartMuxChannel = 0;
579 tmpSensorMap[0] = SENSOR_OPTIC; 637 tmpSensorMap[0] = SENSOR_OPTIC;
580 tmpSensorMap[1] = SENSOR_OPTIC; 638 tmpSensorMap[1] = SENSOR_OPTIC;
581 tmpSensorMap[2] = SENSOR_OPTIC; 639 tmpSensorMap[2] = SENSOR_OPTIC;
582 tmpSensorMap[3] = SENSOR_NONE; 640 tmpSensorMap[3] = SENSOR_NONE;
583 tmpSensorMap[4] = SENSOR_NONE; 641 tmpSensorMap[4] = SENSOR_NONE;
642 tmpSensorMap[5] = SENSOR_NONE;
643 tmpSensorMap[6] = SENSOR_NONE;
644 tmpSensorMap[7] = SENSOR_NONE;
584 645
585 memset(foundSensorMap, SENSOR_NONE, sizeof(foundSensorMap)); 646 memset(foundSensorMap, SENSOR_NONE, sizeof(foundSensorMap));
586 memset(externalInterface_SensorState,UART_COMMON_INIT,sizeof(externalInterface_SensorState)); 647 memset(externalInterface_SensorState,UART_COMMON_INIT,sizeof(externalInterface_SensorState));
587 memset(Mux2ADCMap,0, sizeof(Mux2ADCMap)); 648 memset(Mux2ADCMap,0, sizeof(Mux2ADCMap));
588 649
620 else 681 else
621 { 682 {
622 tmpSensorMap[sensorIndex++] = SENSOR_NONE; 683 tmpSensorMap[sensorIndex++] = SENSOR_NONE;
623 } 684 }
624 } 685 }
686 externalInterfaceMuxReqIntervall = 1100;
625 externalAutoDetect = DETECTION_UARTMUX; 687 externalAutoDetect = DETECTION_UARTMUX;
626 externalInterface_SwitchUART(EXT_INTERFACE_UART_O2 >> 8); 688 externalInterface_SwitchUART(EXT_INTERFACE_UART_O2 >> 8);
627 UART_MUX_SelectAddress(MAX_MUX_CHANNEL); 689 UART_MUX_SelectAddress(MAX_MUX_CHANNEL);
628 uartO2_SetChannel(MAX_MUX_CHANNEL); 690 uartO2_SetChannel(MAX_MUX_CHANNEL);
629 activeUartChannel = MAX_MUX_CHANNEL; 691 activeUartChannel = MAX_MUX_CHANNEL;
638 else 700 else
639 { 701 {
640 tmpSensorMap[EXT_INTERFACE_SENSOR_CNT-1] = SENSOR_NONE; 702 tmpSensorMap[EXT_INTERFACE_SENSOR_CNT-1] = SENSOR_NONE;
641 } 703 }
642 externalAutoDetect = DETECTION_DIGO2_0; 704 externalAutoDetect = DETECTION_DIGO2_0;
643 UART_MUX_SelectAddress(0);
644 uartO2_SetChannel(0); 705 uartO2_SetChannel(0);
645 activeUartChannel = 0; 706 activeUartChannel = 0;
646 tmpSensorMap[EXT_INTERFACE_MUX_OFFSET] = SENSOR_DIGO2; 707 tmpSensorMap[EXT_INTERFACE_MUX_OFFSET] = SENSOR_DIGO2;
647 externalInterface_SensorState[EXT_INTERFACE_MUX_OFFSET] = UART_COMMON_INIT; 708 externalInterface_SensorState[EXT_INTERFACE_MUX_OFFSET] = UART_COMMON_INIT;
648 externalInterface_SwitchUART(EXT_INTERFACE_UART_O2 >> 8); 709 externalInterface_SwitchUART(EXT_INTERFACE_UART_O2 >> 8);
710 if(foundSensorMap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX)
711 {
712 UART_MUX_SelectAddress(0);
713 }
649 break; 714 break;
650 case DETECTION_DIGO2_0: 715 case DETECTION_DIGO2_0:
651 case DETECTION_DIGO2_1: 716 case DETECTION_DIGO2_1:
652 case DETECTION_DIGO2_2: 717 case DETECTION_DIGO2_2:
653 case DETECTION_DIGO2_3: 718 case DETECTION_DIGO2_3:
654 if(uartO2_isSensorConnected()) 719 if(uartO2_isSensorConnected())
655 { 720 {
656 foundSensorMap[externalAutoDetect - DETECTION_DIGO2_0 + EXT_INTERFACE_MUX_OFFSET] = SENSOR_DIGO2; 721 foundSensorMap[externalAutoDetect - DETECTION_DIGO2_0 + EXT_INTERFACE_MUX_OFFSET] = SENSOR_DIGO2;
657 } 722 }
658 723 tmpSensorMap[EXT_INTERFACE_MUX_OFFSET] = SENSOR_NONE;
659 if(uartMuxChannel) 724 if(uartMuxChannel)
660 { 725 {
661 externalInterface_SwitchUART(EXT_INTERFACE_UART_O2 >> 8); 726 externalInterface_SwitchUART(EXT_INTERFACE_UART_O2 >> 8);
662 UART_MUX_SelectAddress(uartMuxChannel); 727 UART_MUX_SelectAddress(uartMuxChannel);
663 externalInterface_SensorState[uartMuxChannel + EXT_INTERFACE_MUX_OFFSET] = UART_COMMON_INIT; 728 externalInterface_SensorState[uartMuxChannel + EXT_INTERFACE_MUX_OFFSET] = UART_COMMON_INIT;
757 822
758 if((foundSensorMap[index] == SENSOR_DIGO2) || (foundSensorMap[index] == SENSOR_CO2)) 823 if((foundSensorMap[index] == SENSOR_DIGO2) || (foundSensorMap[index] == SENSOR_CO2))
759 { 824 {
760 cntUARTSensor++; 825 cntUARTSensor++;
761 } 826 }
762 827 }
763 /* Map Mux O2 sensors to ADC Slot if ADC slot is not in use */ 828 externalInface_MapUartToLegacyADC(foundSensorMap);
764 if(foundSensorMap[index] == SENSOR_DIGO2)
765 {
766 for(index2 = 0; index2 < MAX_ADC_CHANNEL; index2++)
767 {
768 if(foundSensorMap[index2] == SENSOR_NONE)
769 {
770 foundSensorMap[index2] = SENSOR_DIGO2M; /* store a mirror instance needed for visualization */
771 Mux2ADCMap[index2] = index;
772 break;
773 }
774 }
775 }
776 }
777 for(index = 0; index < EXT_INTERFACE_SENSOR_CNT-1; index++)
778 {
779 if(foundSensorMap[index] == SENSOR_CO2)
780 {
781 for(index2 = 0; index2 < MAX_ADC_CHANNEL; index2++)
782 {
783 if(foundSensorMap[index2] == SENSOR_NONE)
784 {
785 foundSensorMap[index2] = SENSOR_CO2M; /* store a mirror instance needed for visualization */
786 Mux2ADCMap[index2] = index;
787 break;
788 }
789 }
790 }
791 }
792 externalInterfaceMuxReqIntervall = 0xFFFF; 829 externalInterfaceMuxReqIntervall = 0xFFFF;
793 if(cntSensor == 0) /* return default sensor map if no sensor at all has been detected */ 830 if(cntSensor == 0) /* return default sensor map if no sensor at all has been detected */
794 { 831 {
795 foundSensorMap[0] = SENSOR_OPTIC; 832 foundSensorMap[0] = SENSOR_OPTIC;
796 foundSensorMap[1] = SENSOR_OPTIC; 833 foundSensorMap[1] = SENSOR_OPTIC;
815 852
816 void externalInterface_ExecuteCmd(uint16_t Cmd) 853 void externalInterface_ExecuteCmd(uint16_t Cmd)
817 { 854 {
818 char cmdString[10]; 855 char cmdString[10];
819 uint8_t cmdLength = 0; 856 uint8_t cmdLength = 0;
820 uint8_t index, index2; 857 uint8_t index;
821 uint8_t cntUARTSensor = 0; 858 uint8_t cntUARTSensor = 0;
822 uint8_t lastMappedID = 0;
823
824 859
825 switch(Cmd & 0x00FF) /* lower byte is reserved for commands */ 860 switch(Cmd & 0x00FF) /* lower byte is reserved for commands */
826 { 861 {
827 case EXT_INTERFACE_AUTODETECT: externalAutoDetect = DETECTION_INIT; 862 case EXT_INTERFACE_AUTODETECT: externalAutoDetect = DETECTION_INIT;
828 for(index = 0; index < 3; index++) 863 for(index = 0; index < 3; index++)
846 { 881 {
847 if((SensorMap[index] == SENSOR_DIGO2) || (SensorMap[index] == SENSOR_CO2)) 882 if((SensorMap[index] == SENSOR_DIGO2) || (SensorMap[index] == SENSOR_CO2))
848 { 883 {
849 cntUARTSensor++; 884 cntUARTSensor++;
850 } 885 }
851 if(SensorMap[index] == SENSOR_DIGO2M) /* find matching sensor for mirror */
852 {
853 for(index2 = EXT_INTERFACE_MUX_OFFSET; index2 < EXT_INTERFACE_SENSOR_CNT; index2++)
854 {
855 if(SensorMap[index2] == SENSOR_DIGO2)
856 {
857 if(lastMappedID < index2)
858 {
859 lastMappedID = index2;
860 Mux2ADCMap[index] = index2;
861 break;
862 }
863 }
864 }
865 }
866 } 886 }
887 externalInface_MapUartToLegacyADC(SensorMap);
867 if(cntUARTSensor > 0) 888 if(cntUARTSensor > 0)
868 { 889 {
869 externalInterfaceMuxReqIntervall = REQUEST_INT_SENSOR_MS / cntUARTSensor; 890 externalInterfaceMuxReqIntervall = REQUEST_INT_SENSOR_MS / cntUARTSensor;
870 activeUartChannel = 0xFF; 891 activeUartChannel = 0xFF;
871 } 892 }
938 static uint8_t timeToTrigger = 0; 959 static uint8_t timeToTrigger = 0;
939 uint32_t tick = HAL_GetTick(); 960 uint32_t tick = HAL_GetTick();
940 uint8_t *pmap = externalInterface_GetSensorMapPointer(0); 961 uint8_t *pmap = externalInterface_GetSensorMapPointer(0);
941 962
942 963
943 UART_ReadData(pmap[activeSensorId]);
944
945 if(activeUartChannel == 0xFF)
946 {
947 activeUartChannel = ExternalInterface_SelectUsedMuxChannel(0);
948 uartO2_SetChannel(activeUartChannel);
949 if(pmap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX)
950 {
951 UART_MUX_SelectAddress(activeUartChannel);
952 }
953 externalInterface_CheckBaudrate(pmap[activeUartChannel + EXT_INTERFACE_MUX_OFFSET]);
954 UART_FlushRxBuffer();
955 }
956
957 if(externalInterfaceMuxReqIntervall != 0xFFFF) 964 if(externalInterfaceMuxReqIntervall != 0xFFFF)
958 { 965 {
966 UART_ReadData(pmap[activeSensorId]);
967
968 if(activeUartChannel == 0xFF)
969 {
970 activeUartChannel = ExternalInterface_SelectUsedMuxChannel(0);
971 uartO2_SetChannel(activeUartChannel);
972
973 switch(pmap[activeUartChannel + EXT_INTERFACE_MUX_OFFSET])
974 {
975 case SENSOR_CO2: externalInterface_SwitchUART(EXT_INTERFACE_UART_CO2 >> 8);
976 break;
977 default:
978 case SENSOR_DIGO2: externalInterface_SwitchUART(EXT_INTERFACE_UART_O2 >> 8);
979 break;
980 }
981 if(pmap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX)
982 {
983 UART_MUX_SelectAddress(activeUartChannel);
984 }
985 }
986
959 if(externalInterface_SensorState[activeSensorId] == UART_COMMON_INIT) 987 if(externalInterface_SensorState[activeSensorId] == UART_COMMON_INIT)
960 { 988 {
961 lastRequestTick = tick; 989 lastRequestTick = tick;
962 TriggerTick = tick - 10; /* just to make sure control is triggered */ 990 TriggerTick = tick - 10; /* just to make sure control is triggered */
963 timeToTrigger = 1; 991 timeToTrigger = 1;