comparison 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
comparison
equal deleted inserted replaced
730:9c65d226f4f6 731:2a801cfe23ab
109 /* Check if conversion is done and trigger measurement of next channel */ 109 /* Check if conversion is done and trigger measurement of next channel */
110 uint8_t externalInterface_ReadAndSwitch() 110 uint8_t externalInterface_ReadAndSwitch()
111 { 111 {
112 uint8_t retval = EXTERNAL_ADC_NO_DATA; 112 uint8_t retval = EXTERNAL_ADC_NO_DATA;
113 uint8_t nextChannel; 113 uint8_t nextChannel;
114 uint8_t* psensorMap = externalInterface_GetSensorMapPointer(); 114 uint8_t* psensorMap = externalInterface_GetSensorMapPointer(0);
115 115
116 if(externalADC_On) 116 if(externalADC_On)
117 { 117 {
118 if(I2C_Master_Receive(DEVICE_EXTERNAL_ADC, recBuf, ADC_ANSWER_LENGTH) == HAL_OK) 118 if(I2C_Master_Receive(DEVICE_EXTERNAL_ADC, recBuf, ADC_ANSWER_LENGTH) == HAL_OK)
119 { 119 {
258 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_RESET); 258 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_RESET);
259 externalV33_On = 1; 259 externalV33_On = 1;
260 } 260 }
261 else 261 else
262 { 262 {
263 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_SET); 263 if(externalAutoDetect == DETECTION_OFF)
264 externalV33_On = 0; 264 {
265 externalInterface_SetCO2Value(0); 265 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_SET);
266 externalInterface_SetCO2SignalStrength(0); 266 externalV33_On = 0;
267 externalInterface_SetCO2Value(0);
268 externalInterface_SetCO2SignalStrength(0);
269 }
267 } 270 }
268 } 271 }
269 } 272 }
270 void externalInterface_SwitchADC(uint8_t state) 273 void externalInterface_SwitchADC(uint8_t state)
271 { 274 {
297 switch(protocol) 300 switch(protocol)
298 { 301 {
299 case 0: 302 case 0:
300 case (EXT_INTERFACE_UART_CO2 >> 8): 303 case (EXT_INTERFACE_UART_CO2 >> 8):
301 case (EXT_INTERFACE_UART_O2 >> 8): 304 case (EXT_INTERFACE_UART_O2 >> 8):
302 if((externalAutoDetect == DETECTION_OFF) || ((protocol == EXT_INTERFACE_UART_CO2 >> 8) && (externalAutoDetect == DETECTION_CO2)) 305 if((externalAutoDetect <= DETECTION_START) || ((protocol == EXT_INTERFACE_UART_CO2 >> 8) && (externalAutoDetect == DETECTION_CO2))
303 || ((protocol == EXT_INTERFACE_UART_O2 >> 8) && (externalAutoDetect == DETECTION_DIGO2))) 306 || ((protocol == EXT_INTERFACE_UART_O2 >> 8) && (externalAutoDetect == DETECTION_DIGO2)))
304 { 307 {
305 sensorDataId = 0; 308 sensorDataId = 0;
306 externalUART_Protocol = protocol; 309 externalUART_Protocol = protocol;
307 MX_USART1_UART_DeInit(); 310 MX_USART1_UART_DeInit();
308 if( protocol != 0) 311 if( protocol != 0)
380 { 383 {
381 memcpy(MasterSensorMap, pMap, 5); /* the map is not directly copied. Copy is done via cmd request */ 384 memcpy(MasterSensorMap, pMap, 5); /* the map is not directly copied. Copy is done via cmd request */
382 } 385 }
383 386
384 } 387 }
385 uint8_t* externalInterface_GetSensorMapPointer() 388 uint8_t* externalInterface_GetSensorMapPointer(uint8_t finalMap)
386 { 389 {
387 uint8_t* pret; 390 uint8_t* pret;
388 391
389 if(externalAutoDetect != DETECTION_OFF) 392 if((externalAutoDetect != DETECTION_OFF) && (!finalMap))
390 { 393 {
391 pret = tmpSensorMap; 394 pret = tmpSensorMap;
392 } 395 }
393 else 396 else
394 { 397 {
405 if(externalAutoDetect != DETECTION_OFF) 408 if(externalAutoDetect != DETECTION_OFF)
406 { 409 {
407 switch(externalAutoDetect) 410 switch(externalAutoDetect)
408 { 411 {
409 case DETECTION_INIT: sensorIndex = 0; 412 case DETECTION_INIT: sensorIndex = 0;
410 tmpSensorMap[0] = SENSOR_ANALOG; 413 tmpSensorMap[0] = SENSOR_OPTIC;
411 tmpSensorMap[1] = SENSOR_ANALOG; 414 tmpSensorMap[1] = SENSOR_OPTIC;
412 tmpSensorMap[2] = SENSOR_ANALOG; 415 tmpSensorMap[2] = SENSOR_OPTIC;
413 tmpSensorMap[3] = SENSOR_NONE; 416 tmpSensorMap[3] = SENSOR_NONE;
414 tmpSensorMap[4] = SENSOR_NONE; 417 tmpSensorMap[4] = SENSOR_NONE;
415 418
416 externalInterface_SwitchADC(1); 419 if(externalInterfacePresent)
417 externalAutoDetect = DETECTION_ANALOG; 420 {
418 break; 421 externalInterface_SwitchPower33(0);
419 case DETECTION_ANALOG: for(index = 0; index < MAX_ADC_CHANNEL; index++) 422 externalInterface_SwitchUART(0);
423 for(index = 0; index < MAX_ADC_CHANNEL; index++)
424 {
425 externalChannel_mV[index] = 0;
426 }
427 externalAutoDetect = DETECTION_START;
428 }
429 else
430 {
431 externalAutoDetect = DETECTION_DONE; /* without external interface O2 values may only be received via optical port => return default sensor map */
432 }
433 break;
434 case DETECTION_START: tmpSensorMap[0] = SENSOR_ANALOG;
435 tmpSensorMap[1] = SENSOR_ANALOG;
436 tmpSensorMap[2] = SENSOR_ANALOG;
437 externalInterface_SwitchPower33(1);
438 externalInterface_SwitchADC(1);
439 externalAutoDetect = DETECTION_ANALOG1;
440 break;
441 case DETECTION_ANALOG1: externalAutoDetect = DETECTION_ANALOG2; /* do a second loop to make sure all adc channels could be processed */
442 break;
443 case DETECTION_ANALOG2: for(index = 0; index < MAX_ADC_CHANNEL; index++)
420 { 444 {
421 if(externalChannel_mV[index] > MIN_ADC_VOLTAGE_MV) 445 if(externalChannel_mV[index] > MIN_ADC_VOLTAGE_MV)
422 { 446 {
423 tmpSensorMap[sensorIndex++] = SENSOR_ANALOG; 447 tmpSensorMap[sensorIndex++] = SENSOR_ANALOG;
424 } 448 }
473 } 497 }
474 498
475 } 499 }
476 externalAutoDetect = DETECTION_DONE; 500 externalAutoDetect = DETECTION_DONE;
477 break; 501 break;
478 case DETECTION_DONE: while(sensorIndex < 5) 502 case DETECTION_DONE: for(index = 0; index < EXT_INTERFACE_SENSOR_CNT; index++)
479 { 503 {
480 tmpSensorMap[sensorIndex++] = SENSOR_NONE; 504 if(tmpSensorMap[index] != SENSOR_NONE)
505 {
506 break;
507 }
508 }
509
510 if(index != EXT_INTERFACE_SENSOR_CNT) /* return default sensor map if no sensor at all has been detected */
511 {
512 while(sensorIndex < EXT_INTERFACE_SENSOR_CNT)
513 {
514 tmpSensorMap[sensorIndex++] = SENSOR_NONE;
515 }
516 }
517 else
518 {
519 tmpSensorMap[0] = SENSOR_OPTIC;
520 tmpSensorMap[1] = SENSOR_OPTIC;
521 tmpSensorMap[2] = SENSOR_OPTIC;
481 } 522 }
482 memcpy(SensorMap, tmpSensorMap, sizeof(tmpSensorMap)); 523 memcpy(SensorMap, tmpSensorMap, sizeof(tmpSensorMap));
483 externalAutoDetect = DETECTION_OFF; 524 externalAutoDetect = DETECTION_OFF;
484 break; 525 break;
485 default: 526 default: