diff Small_CPU/Src/externalInterface.c @ 731:2a801cfe23ab

Improvment sensor Autodetection: The sensor type optical has been introduce to enable the OSTC to completly switch of the external interface (voltage + ADC operation) The optical interface is handled via firmware. In addition some states have been added to reset the external power supply and to provide the ADC more time to do its conversation.
author Ideenmodellierer
date Sun, 15 Jan 2023 21:43:33 +0100
parents d646a0f724a7
children 30717de00f3a
line wrap: on
line diff
--- a/Small_CPU/Src/externalInterface.c	Sat Jan 14 20:49:40 2023 +0100
+++ b/Small_CPU/Src/externalInterface.c	Sun Jan 15 21:43:33 2023 +0100
@@ -111,7 +111,7 @@
 {
 	uint8_t retval = EXTERNAL_ADC_NO_DATA;
 	uint8_t nextChannel;
-	uint8_t* psensorMap = externalInterface_GetSensorMapPointer();
+	uint8_t* psensorMap = externalInterface_GetSensorMapPointer(0);
 
 	if(externalADC_On)
 	{
@@ -260,10 +260,13 @@
 		}
 		else
 		{
-			HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_SET);
-			externalV33_On = 0;
-			externalInterface_SetCO2Value(0);
-			externalInterface_SetCO2SignalStrength(0);
+			if(externalAutoDetect == DETECTION_OFF)
+			{
+				HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_SET);
+				externalV33_On = 0;
+				externalInterface_SetCO2Value(0);
+				externalInterface_SetCO2SignalStrength(0);
+			}
 		}
 	}
 }
@@ -299,8 +302,8 @@
 		case 0:
 		case (EXT_INTERFACE_UART_CO2 >> 8):
 		case (EXT_INTERFACE_UART_O2 >> 8):
-				if((externalAutoDetect == DETECTION_OFF) || ((protocol == EXT_INTERFACE_UART_CO2 >> 8) && (externalAutoDetect == DETECTION_CO2))
-														 || ((protocol == EXT_INTERFACE_UART_O2 >> 8) && (externalAutoDetect == DETECTION_DIGO2)))
+				if((externalAutoDetect <= DETECTION_START) || ((protocol == EXT_INTERFACE_UART_CO2 >> 8) && (externalAutoDetect == DETECTION_CO2))
+														   || ((protocol == EXT_INTERFACE_UART_O2 >> 8) && (externalAutoDetect == DETECTION_DIGO2)))
 				{
 					sensorDataId = 0;
 					externalUART_Protocol = protocol;
@@ -382,11 +385,11 @@
 	}
 
 }
-uint8_t* externalInterface_GetSensorMapPointer()
+uint8_t* externalInterface_GetSensorMapPointer(uint8_t finalMap)
 {
 	uint8_t* pret;
 
-	if(externalAutoDetect != DETECTION_OFF)
+	if((externalAutoDetect != DETECTION_OFF) && (!finalMap))
 	{
 		pret = tmpSensorMap;
 	}
@@ -407,16 +410,37 @@
 		switch(externalAutoDetect)
 		{
 			case DETECTION_INIT:	sensorIndex = 0;
-									tmpSensorMap[0] = SENSOR_ANALOG;
-									tmpSensorMap[1] = SENSOR_ANALOG;
-									tmpSensorMap[2] = SENSOR_ANALOG;
+									tmpSensorMap[0] = SENSOR_OPTIC;
+									tmpSensorMap[1] = SENSOR_OPTIC;
+									tmpSensorMap[2] = SENSOR_OPTIC;
 									tmpSensorMap[3] = SENSOR_NONE;
 									tmpSensorMap[4] = SENSOR_NONE;
 
-									externalInterface_SwitchADC(1);
-									externalAutoDetect = DETECTION_ANALOG;
+									if(externalInterfacePresent)
+									{
+										externalInterface_SwitchPower33(0);
+										externalInterface_SwitchUART(0);
+										for(index = 0; index < MAX_ADC_CHANNEL; index++)
+										{
+											externalChannel_mV[index] = 0;
+										}
+										externalAutoDetect = DETECTION_START;
+									}
+									else
+									{
+										externalAutoDetect = DETECTION_DONE;	/* without external interface O2 values may only be received via optical port => return default sensor map */
+									}
 				break;
-			case DETECTION_ANALOG:	for(index = 0; index < MAX_ADC_CHANNEL; index++)
+			case DETECTION_START:		tmpSensorMap[0] = SENSOR_ANALOG;
+										tmpSensorMap[1] = SENSOR_ANALOG;
+										tmpSensorMap[2] = SENSOR_ANALOG;
+										externalInterface_SwitchPower33(1);
+										externalInterface_SwitchADC(1);
+										externalAutoDetect = DETECTION_ANALOG1;
+				break;
+			case DETECTION_ANALOG1:	externalAutoDetect = DETECTION_ANALOG2;		/* do a second loop to make sure all adc channels could be processed */
+				break;
+			case DETECTION_ANALOG2:	for(index = 0; index < MAX_ADC_CHANNEL; index++)
 									{
 										if(externalChannel_mV[index] > MIN_ADC_VOLTAGE_MV)
 										{
@@ -475,9 +499,26 @@
 									}
 									externalAutoDetect = DETECTION_DONE;
 				break;
-			case DETECTION_DONE:	while(sensorIndex < 5)
+			case DETECTION_DONE:	for(index = 0; index < EXT_INTERFACE_SENSOR_CNT; index++)
+									{
+										if(tmpSensorMap[index] != SENSOR_NONE)
+										{
+											break;
+										}
+									}
+
+									if(index != EXT_INTERFACE_SENSOR_CNT)		/* return default sensor map if no sensor at all has been detected */
 									{
-										tmpSensorMap[sensorIndex++] = SENSOR_NONE;
+										while(sensorIndex < EXT_INTERFACE_SENSOR_CNT)
+										{
+											tmpSensorMap[sensorIndex++] = SENSOR_NONE;
+										}
+									}
+									else
+									{
+										tmpSensorMap[0] = SENSOR_OPTIC;
+										tmpSensorMap[1] = SENSOR_OPTIC;
+										tmpSensorMap[2] = SENSOR_OPTIC;
 									}
 									memcpy(SensorMap, tmpSensorMap, sizeof(tmpSensorMap));
 									externalAutoDetect = DETECTION_OFF;