comparison Small_CPU/Src/uart.c @ 794:bb37d4f3e50e

Restructure UART based sensor handling: In the previous version every UART sensor instance had its own protocol handling instance (requests, timeout, errors). With the introduction of the multiplexer these functionalities had to be harmonized. E.g. only one errorhandling which is applied to all sensors. In the new structure the sensor communication is split into one function which takes care for the control needs of a sensor and one function which handles the incoming data. The functions behalf the same independend if the sensor are connected to multiplexer or directly to the OSTC. Second big change in the external sensor concepts is that the data processing is no longer focussed at the three existing ADC channels. Every external sensor (up to 3 ADC and 4 UART) sensor has its own instance. If the ADC slots are not in use then they may be used for visiualization of UART sensors by creating a mirror instance but this is no longer a must.
author Ideenmodellierer
date Mon, 31 Jul 2023 19:46:29 +0200
parents aeb72882f30a
children e9eba334b942
comparison
equal deleted inserted replaced
793:9da81033ad44 794:bb37d4f3e50e
18 * 18 *
19 ****************************************************************************** 19 ******************************************************************************
20 */ 20 */
21 /* Includes ------------------------------------------------------------------*/ 21 /* Includes ------------------------------------------------------------------*/
22 #include "uart.h" 22 #include "uart.h"
23 #include "uartProtocol_O2.h"
23 #include "externalInterface.h" 24 #include "externalInterface.h"
24 #include "data_exchange.h" 25 #include "data_exchange.h"
25 #include <string.h> /* memset */ 26 #include <string.h> /* memset */
26 27
27 /* Private variables ---------------------------------------------------------*/ 28 /* Private variables ---------------------------------------------------------*/
28 29
29 #define BUFFER_NODATA (7u) /* The read function needs a byte which indecated that no data for processing is available.*/ 30
30 /* This byte shall never appear in a normal data steam */
31 31
32 #define CHUNK_SIZE (25u) /* the DMA will handle chunk size transfers */ 32 #define CHUNK_SIZE (25u) /* the DMA will handle chunk size transfers */
33 #define CHUNKS_PER_BUFFER (5u) 33 #define CHUNKS_PER_BUFFER (5u)
34 #define COMMAND_TX_DELAY (30u) /* The time the sensor needs to recover from a invalid command request */ 34
35 #define REQUEST_INT_SENSOR_MS (1500) /* Minimum time interval for cyclic sensor data requests per sensor */
36 UART_HandleTypeDef huart1; 35 UART_HandleTypeDef huart1;
37 36
38 DMA_HandleTypeDef hdma_usart1_rx; 37 DMA_HandleTypeDef hdma_usart1_rx;
39 38
40 uint8_t rxBuffer[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow fariations in buffer read time */ 39 uint8_t rxBuffer[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow fariations in buffer read time */
41 static uint8_t rxWriteIndex; /* Index of the data item which is analysed */ 40 static uint8_t rxWriteIndex; /* Index of the data item which is analysed */
42 static uint8_t rxReadIndex; /* Index at which new data is stared */ 41 static uint8_t rxReadIndex; /* Index at which new data is stared */
43 static uint8_t lastCmdIndex; /* Index of last command which has not been completly received */ 42 static uint8_t lastCmdIndex; /* Index of last command which has not been completly received */
44 static uint8_t dmaActive; /* Indicator if DMA reception needs to be started */ 43 static uint8_t dmaActive; /* Indicator if DMA reception needs to be started */
45 static uint8_t digO2Connected = 0; /* Binary indicator if a sensor is connected or not */ 44
46 static uint8_t CO2Connected = 0; /* Binary indicator if a sensor is connected or not */ 45 static uint8_t CO2Connected = 0; /* Binary indicator if a sensor is connected or not */
47 static uint8_t SentinelConnected = 0; /* Binary indicator if a sensor is connected or not */ 46 static uint8_t SentinelConnected = 0; /* Binary indicator if a sensor is connected or not */
48 47
49 static SSensorDataDiveO2 tmpSensorDataDiveO2; /* intermediate storage for additional sensor data */ 48
50 49
51 char tmpRxBuf[30]; 50 static uartCO2Status_t ComStatus_CO2 = UART_CO2_INIT;
52 uint8_t tmpRxIdx = 0;
53
54 static uartO2Status_t Comstatus_O2 = UART_O2_INIT;
55 static uint8_t activeSensor = 0;
56 static uint8_t sensorMapping[MAX_MUX_CHANNEL]; /* The mapping is used to assign the visible sensor channel to the mux address (DiveO2) */
57 51
58 float LED_Level = 0.0; /* Normalized LED value which may be used as indication for the health status of the sensor */ 52 float LED_Level = 0.0; /* Normalized LED value which may be used as indication for the health status of the sensor */
59 float LED_ZeroOffset = 0.0; 53 float LED_ZeroOffset = 0.0;
60 float pCO2 = 0.0; 54 float pCO2 = 0.0;
61 /* Exported functions --------------------------------------------------------*/ 55 /* Exported functions --------------------------------------------------------*/
62 56
57
58
59 //huart.Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), new_baudrate);
60
63 void MX_USART1_UART_Init(void) 61 void MX_USART1_UART_Init(void)
64 { 62 {
65 /* regular init */ 63 /* regular init */
66 64
67 huart1.Instance = USART1; 65 huart1.Instance = USART1;
68 66
69 if(externalInterface_GetUARTProtocol() == 0x04) 67 if(externalInterface_GetUARTProtocol() == 0x04)
70 { 68 {
71 huart1.Init.BaudRate = 19200; 69 huart1.Init.BaudRate = 19200;
72 Comstatus_O2 = UART_O2_INIT;
73 } 70 }
74 else 71 else
75 { 72 {
76 huart1.Init.BaudRate = 9600; 73 huart1.Init.BaudRate = 9600;
74 ComStatus_CO2 = UART_CO2_INIT;
77 } 75 }
78 huart1.Init.WordLength = UART_WORDLENGTH_8B; 76 huart1.Init.WordLength = UART_WORDLENGTH_8B;
79 huart1.Init.StopBits = UART_STOPBITS_1; 77 huart1.Init.StopBits = UART_STOPBITS_1;
80 huart1.Init.Parity = UART_PARITY_NONE; 78 huart1.Init.Parity = UART_PARITY_NONE;
81 huart1.Init.Mode = UART_MODE_TX_RX; 79 huart1.Init.Mode = UART_MODE_TX_RX;
89 memset(rxBuffer,BUFFER_NODATA,sizeof(rxBuffer)); 87 memset(rxBuffer,BUFFER_NODATA,sizeof(rxBuffer));
90 rxReadIndex = 0; 88 rxReadIndex = 0;
91 lastCmdIndex = 0; 89 lastCmdIndex = 0;
92 rxWriteIndex = 0; 90 rxWriteIndex = 0;
93 dmaActive = 0; 91 dmaActive = 0;
94 digO2Connected = 0; 92
95 CO2Connected = 0; 93 CO2Connected = 0;
96 SentinelConnected = 0; 94 SentinelConnected = 0;
97 Comstatus_O2 = UART_O2_INIT; 95
98 } 96 }
99 97
100 void MX_USART1_UART_DeInit(void) 98 void MX_USART1_UART_DeInit(void)
101 { 99 {
102 HAL_DMA_Abort(&hdma_usart1_rx); 100 HAL_DMA_Abort(&hdma_usart1_rx);
127 /* DMA interrupt init */ 125 /* DMA interrupt init */
128 HAL_NVIC_SetPriority(DMA2_Stream5_IRQn, 0, 0); 126 HAL_NVIC_SetPriority(DMA2_Stream5_IRQn, 0, 0);
129 HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn); 127 HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn);
130 } 128 }
131 129
132 130 void UART_MUX_SelectAddress(uint8_t muxAddress)
133 void DigitalO2_SelectSensor(uint8_t channel)
134 { 131 {
135 uint8_t indexstr[4]; 132 uint8_t indexstr[4];
136 uint8_t muxAddress = 0; 133
137 indexstr[0] = '~'; 134 if(muxAddress <= MAX_MUX_CHANNEL)
138 indexstr[1] = '1'; 135 {
139 indexstr[2] = 0x0D; 136 indexstr[0] = '~';
140 indexstr[3] = 0x0A; 137 indexstr[1] = muxAddress;
141 138 indexstr[2] = 0x0D;
142 /* Lookup mux address mapped to the provided channel. If no mapping is found the the MUX itself will be selected */ 139 indexstr[3] = 0x0A;
143 for(muxAddress = 0; muxAddress < MAX_MUX_CHANNEL; muxAddress++) 140
144 { 141 HAL_UART_Transmit(&huart1,indexstr,4,10);
145 if(sensorMapping[muxAddress] == channel) 142 }
146 { 143 }
147 break; 144
148 } 145
149 } 146 void UART_SendCmdString(uint8_t *cmdString)
150 indexstr[1] = muxAddress; 147 {
151 HAL_UART_Transmit(&huart1,indexstr,4,10); 148 uint8_t cmdLength = strlen((char*)cmdString);
152 } 149
153 150 if(cmdLength < 20) /* A longer string is an indication for a missing 0 termination */
154 void DigitalO2_SetupCmd(uint8_t O2State, uint8_t *cmdString, uint8_t *cmdLength) 151 {
155 { 152 if(dmaActive == 0)
156 switch (O2State) 153 {
157 { 154 UART_StartDMA_Receiption();
158 case UART_O2_CHECK: *cmdLength = snprintf((char*)cmdString, 10, "#LOGO"); 155 }
159 break; 156 HAL_UART_Transmit(&huart1,cmdString,cmdLength,10);
160 case UART_O2_REQ_INFO: *cmdLength = snprintf((char*)cmdString, 10, "#VERS"); 157 }
161 break; 158 }
162 case UART_O2_REQ_ID: *cmdLength = snprintf((char*)cmdString, 10, "#IDNR"); 159
163 break; 160 void DigitalCO2_SendCmd(uint8_t CO2Cmd, uint8_t *cmdString, uint16_t *cmdLength)
164 case UART_O2_REQ_O2: *cmdLength = snprintf((char*)cmdString, 10, "#DOXY"); 161 {
165 break; 162 switch (CO2Cmd)
166 case UART_O2_REQ_RAW: *cmdLength = snprintf((char*)cmdString, 10, "#DRAW"); 163 {
167 break; 164 case CO2CMD_MODE_POLL: *cmdLength = snprintf((char*)cmdString, 10, "K 2\r\n");
165 break;
166 case CO2CMD_MODE_STREAM: *cmdLength = snprintf((char*)cmdString, 10, "K 1\r\n");
167 break;
168 case CO2CMD_CALIBRATE: *cmdLength = snprintf((char*)cmdString, 10, "G\r\n");
169 break;
170 case CO2CMD_GETDATA: *cmdLength = snprintf((char*)cmdString, 10, "Q\r\n");
171 break;
172 case CO2CMD_GETSCALE: *cmdLength = snprintf((char*)cmdString, 10, ".\r\n");
173 break;
168 default: *cmdLength = 0; 174 default: *cmdLength = 0;
169 break; 175 break;
170 } 176 }
171 if(*cmdLength != 0) 177 if(cmdLength != 0)
172 { 178 {
173 cmdString[*cmdLength] = 0x0D; 179 HAL_UART_Transmit(&huart1,cmdString,*cmdLength,10);
174 *cmdLength = *cmdLength + 1;
175 } 180 }
176 } 181 }
177 182
178 void StringToInt(char *pstr, uint32_t *puInt32) 183 void StringToInt(char *pstr, uint32_t *puInt32)
179 { 184 {
237 uint8_t localRX = rxReadIndex; 242 uint8_t localRX = rxReadIndex;
238 static uint8_t dataType = 0; 243 static uint8_t dataType = 0;
239 static uint32_t dataValue = 0; 244 static uint32_t dataValue = 0;
240 static receiveState_t rxState = RX_Ready; 245 static receiveState_t rxState = RX_Ready;
241 static uint32_t lastReceiveTick = 0; 246 static uint32_t lastReceiveTick = 0;
242 247 static uint32_t lastTransmitTick = 0;
243 248 static uint8_t cmdString[10];
244 while((rxBuffer[localRX]!=0)) 249 static uint16_t cmdLength = 0;
245 { 250
246 lastReceiveTick = HAL_GetTick(); 251 uint32_t Tick = HAL_GetTick();
252
253 uint8_t *pmap = externalInterface_GetSensorMapPointer(0);
254
255 if(ComStatus_CO2 == UART_CO2_INIT)
256 {
257 UART_StartDMA_Receiption();
258 ComStatus_CO2 = UART_CO2_SETUP;
259 }
260
261 if(ComStatus_CO2 == UART_CO2_SETUP)
262 {
263 if(time_elapsed_ms(lastTransmitTick,Tick) > 200)
264 {
265 if(externalInterface_GetCO2Scale() == 0.0)
266 {
267 DigitalCO2_SendCmd(CO2CMD_GETDATA, cmdString, &cmdLength);
268 lastTransmitTick = Tick;
269 }
270 else
271 {
272 ComStatus_CO2 = UART_CO2_OPERATING;
273 }
274 }
275 }
276 else
277 {
278 if(pmap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX) /* sensor is working in polling mode if mux is connected to avoid interference with other sensors */
279 {
280 if(time_elapsed_ms(lastTransmitTick,Tick) > 2000) /* poll every two seconds */
281 {
282 lastTransmitTick = Tick;
283 if(cmdLength == 0) /* poll data */
284 {
285 DigitalCO2_SendCmd(CO2CMD_GETDATA, cmdString, &cmdLength);
286 }
287 else /* resend last command */
288 {
289 HAL_UART_Transmit(&huart1,cmdString,strlen((char*)cmdString),10);
290 cmdLength = 0;
291 }
292 }
293 }
294 }
295 while((rxBuffer[localRX]!=BUFFER_NODATA))
296 {
297 lastReceiveTick = Tick;
247 if(rxState == RX_Ready) /* identify data content */ 298 if(rxState == RX_Ready) /* identify data content */
248 { 299 {
249 switch(rxBuffer[localRX]) 300 switch(rxBuffer[localRX])
250 { 301 {
251 case 'l': 302 case 'l':
252 case 'D': 303 case 'D':
253 case 'Z': 304 case 'Z':
305 case '.':
254 dataType = rxBuffer[localRX]; 306 dataType = rxBuffer[localRX];
255 rxState = RX_Data0; 307 rxState = RX_Data0;
256 dataValue = 0; 308 dataValue = 0;
257 break; 309 break;
258 310
291 break; 343 break;
292 case 'l': LED_ZeroOffset = dataValue; 344 case 'l': LED_ZeroOffset = dataValue;
293 break; 345 break;
294 case 'Z': externalInterface_SetCO2Value(dataValue); 346 case 'Z': externalInterface_SetCO2Value(dataValue);
295 break; 347 break;
348 case '.': externalInterface_SetCO2Scale(dataValue);
349 break;
296 default: rxState = RX_Ready; 350 default: rxState = RX_Ready;
297 break; 351 break;
298 } 352 }
299 } 353 }
300 if(rxState != RX_Data0) /* reset state machine because message in wrong format */ 354 if(rxState != RX_Data0) /* reset state machine because message in wrong format */
301 { 355 {
302 rxState = RX_Ready; 356 rxState = RX_Ready;
303 } 357 }
304 } 358 }
305 rxBuffer[localRX] = 0; 359 rxBuffer[localRX] = BUFFER_NODATA;
306 localRX++; 360 localRX++;
307 rxReadIndex++; 361 rxReadIndex++;
308 if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER) 362 if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
309 { 363 {
310 localRX = 0; 364 localRX = 0;
475 } 529 }
476 } 530 }
477 #endif 531 #endif
478 532
479 533
480
481 void UART_HandleDigitalO2(void)
482 {
483 static uint32_t lastO2ReqTick = 0;
484 static uint8_t errorStr[] = "#ERRO";
485 static uartO2RxState_t rxState = O2RX_IDLE;
486 static uint32_t lastReceiveTick = 0;
487 static uint8_t lastAlive = 0;
488 static uint8_t curAlive = 0;
489
490 static uint8_t cmdLength = 0;
491 static uint8_t cmdString[10];
492 static uint8_t cmdReadIndex = 0;
493 static uint8_t errorReadIndex = 0;
494 static uint32_t tickToTX = 0;
495 static uint32_t delayStartTick = 0;
496 static uint16_t requestIntervall = 0;
497 static uint8_t retryRequest = 1;
498 static uint8_t lastComState = 0;
499 static uint8_t respondErrorDetected = 0;
500 static uint8_t switchChannel = 0;
501
502 uint8_t index = 0;
503 uint32_t tmpO2 = 0;
504 uint32_t tmpData = 0;
505 uint8_t localRX = rxReadIndex;
506 uint32_t tick = HAL_GetTick();
507
508 uint8_t *pmap = externalInterface_GetSensorMapPointer(0);
509
510 /* The channel switch will cause the sensor to respond with an error message. */
511 /* The sensor needs ~30ms to recover before he is ready to receive the next command => transmission delay needed */
512 if((tickToTX) && (time_elapsed_ms(delayStartTick,tick) >= tickToTX ))
513 {
514 HAL_UART_Transmit(&huart1,cmdString,cmdLength,10);
515 tickToTX = 0;
516 }
517 else
518 {
519 if(Comstatus_O2 == UART_O2_INIT)
520 {
521 memset((char*)&rxBuffer[rxWriteIndex],(int)BUFFER_NODATA, sizeof(rxBuffer));
522 memset((char*) &tmpSensorDataDiveO2, 0, sizeof(tmpSensorDataDiveO2));
523 externalInterface_SetSensorData(0xFF,(uint8_t*)&tmpSensorDataDiveO2);
524
525 lastAlive = 0;
526 curAlive = 0;
527
528 Comstatus_O2 = UART_O2_CHECK;
529 lastComState = UART_O2_IDLE;
530 DigitalO2_SetupCmd(Comstatus_O2,cmdString,&cmdLength);
531
532 if(activeSensor < MAX_MUX_CHANNEL)
533 {
534 externalInterface_GetSensorData(activeSensor, (uint8_t*)&tmpSensorDataDiveO2);
535 }
536
537 tickToTX = COMMAND_TX_DELAY;
538
539 rxState = O2RX_CONFIRM;
540 cmdReadIndex = 0;
541 errorReadIndex = 0;
542 respondErrorDetected = 0;
543 digO2Connected = 0;
544 switchChannel = 1;
545
546 requestIntervall = 0;
547 for(index = 0; index < MAX_MUX_CHANNEL; index++)
548 {
549 if(pmap[index] == SENSOR_DIGO2)
550 {
551 requestIntervall++;
552 }
553 }
554 if(requestIntervall != 0)
555 {
556 requestIntervall = REQUEST_INT_SENSOR_MS / requestIntervall;
557 }
558 else
559 {
560 requestIntervall = REQUEST_INT_SENSOR_MS;
561 }
562 UART_StartDMA_Receiption();
563 lastO2ReqTick = tick + requestIntervall + 1;
564 }
565 if(time_elapsed_ms(lastO2ReqTick,tick) > requestIntervall) /* repeat request or iterate to next sensor */
566 {
567 respondErrorDetected = 0;
568 lastO2ReqTick = tick;
569 index = activeSensor;
570 if((lastComState == Comstatus_O2) && (Comstatus_O2 != UART_O2_IDLE))
571 {
572 if(retryRequest)
573 {
574 retryRequest = 0;
575 }
576 else /* no answer even repeating the request => abort request */
577 {
578 if(Comstatus_O2 == UART_O2_REQ_RAW)
579 {
580 setExternalInterfaceChannel(activeSensor,0.0);
581 }
582 Comstatus_O2 = UART_O2_IDLE;
583 }
584 }
585 lastComState = Comstatus_O2;
586 if(Comstatus_O2 == UART_O2_IDLE) /* cyclic request of o2 value */
587 {
588 retryRequest = 1;
589 if(pmap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX) /* select next sensor if mux is connected */
590 {
591 if(activeSensor < MAX_MUX_CHANNEL)
592 {
593 do
594 {
595 index++;
596 if(index == MAX_MUX_CHANNEL)
597 {
598 index = 0;
599 }
600 if((pmap[index] == SENSOR_DIGO2) && (index != activeSensor))
601 {
602 activeSensor = index;
603 switchChannel = 1;
604 break;
605 }
606 } while(index != activeSensor);
607 externalInterface_GetSensorData(activeSensor, (uint8_t*)&tmpSensorDataDiveO2);
608 }
609 }
610 if((activeSensor != MAX_MUX_CHANNEL) && (tmpSensorDataDiveO2.sensorId == 0))
611 {
612 Comstatus_O2 = UART_O2_REQ_ID;
613 }
614 else
615 {
616 Comstatus_O2 = UART_O2_REQ_RAW;
617 }
618 }
619 rxState = O2RX_CONFIRM;
620 if(switchChannel)
621 {
622 switchChannel = 0;
623 delayStartTick = tick;
624 DigitalO2_SelectSensor(activeSensor);
625 tickToTX = COMMAND_TX_DELAY;
626 }
627 else
628 {
629 tickToTX = 0;
630 HAL_UART_Transmit(&huart1,cmdString,cmdLength,10);
631 }
632 DigitalO2_SetupCmd(Comstatus_O2,cmdString,&cmdLength);
633 }
634 }
635 while((rxBuffer[localRX] != BUFFER_NODATA))
636 {
637 lastReceiveTick = tick;
638 switch(rxState)
639 {
640 case O2RX_CONFIRM: if(rxBuffer[localRX] == '#')
641 {
642 cmdReadIndex = 0;
643 errorReadIndex = 0;
644 }
645 if(errorReadIndex < sizeof(errorStr)-1)
646 {
647 if(rxBuffer[localRX] == errorStr[errorReadIndex])
648 {
649 errorReadIndex++;
650 }
651 else
652 {
653 errorReadIndex = 0;
654 }
655 }
656 else
657 {
658 respondErrorDetected = 1;
659 errorReadIndex = 0;
660 }
661 if(rxBuffer[localRX] == cmdString[cmdReadIndex])
662 {
663 cmdReadIndex++;
664 if(cmdReadIndex == cmdLength - 1)
665 {
666 if((activeSensor == MAX_MUX_CHANNEL))
667 {
668 if(respondErrorDetected)
669 {
670 digO2Connected = 0; /* the multiplexer mirrors the incoming message and does not generate an error information => no mux connected */
671 }
672 else
673 {
674 digO2Connected = 1;
675 }
676 }
677 else /* handle sensors which should respond with an error message after channel switch */
678 {
679 if(respondErrorDetected)
680 {
681 digO2Connected = 1;
682 }
683 }
684 tmpRxIdx = 0;
685 memset((char*) tmpRxBuf, 0, sizeof(tmpRxBuf));
686 cmdReadIndex = 0;
687 switch (Comstatus_O2)
688 {
689 case UART_O2_CHECK: Comstatus_O2 = UART_O2_IDLE;
690 rxState = O2RX_IDLE;
691 break;
692 case UART_O2_REQ_ID: rxState = O2RX_GETNR;
693 break;
694 case UART_O2_REQ_INFO: rxState = O2RX_GETTYPE;
695 break;
696 case UART_O2_REQ_RAW:
697 case UART_O2_REQ_O2: rxState = O2RX_GETO2;
698 break;
699 default: Comstatus_O2 = UART_O2_IDLE;
700 rxState = O2RX_IDLE;
701 break;
702 }
703 }
704 }
705 else
706 {
707 cmdReadIndex = 0;
708 }
709 break;
710
711 case O2RX_GETSTATUS:
712 case O2RX_GETTEMP:
713 case O2RX_GETTYPE:
714 case O2RX_GETVERSION:
715 case O2RX_GETCHANNEL:
716 case O2RX_GETSUBSENSORS:
717 case O2RX_GETO2:
718 case O2RX_GETNR:
719 case O2RX_GETDPHI:
720 case O2RX_INTENSITY:
721 case O2RX_AMBIENTLIGHT:
722 case O2RX_PRESSURE:
723 case O2RX_HUMIDITY:
724 if(rxBuffer[localRX] != 0x0D)
725 {
726 if(rxBuffer[localRX] != ' ') /* the following data entities are placed within the data stream => no need to store data at the end */
727 {
728 tmpRxBuf[tmpRxIdx++] = rxBuffer[localRX];
729 }
730 else
731 {
732 if(tmpRxIdx != 0)
733 {
734 switch(rxState)
735 {
736 case O2RX_GETCHANNEL: StringToInt(tmpRxBuf,&tmpData);
737 rxState = O2RX_GETVERSION;
738 break;
739 case O2RX_GETVERSION: StringToInt(tmpRxBuf,&tmpData);
740 rxState = O2RX_GETSUBSENSORS;
741 break;
742 case O2RX_GETTYPE: StringToInt(tmpRxBuf,&tmpData);
743 rxState = O2RX_GETCHANNEL;
744 break;
745
746 case O2RX_GETO2: StringToInt(tmpRxBuf,&tmpO2);
747 setExternalInterfaceChannel(activeSensor,(float)(tmpO2 / 10000.0));
748 rxState = O2RX_GETTEMP;
749 break;
750 case O2RX_GETTEMP: StringToInt(tmpRxBuf,(uint32_t*)&tmpSensorDataDiveO2.temperature);
751 rxState = O2RX_GETSTATUS;
752 break;
753 case O2RX_GETSTATUS: StringToInt(tmpRxBuf,&tmpSensorDataDiveO2.status); /* raw data cycle */
754 rxState = O2RX_GETDPHI;
755 break;
756 case O2RX_GETDPHI: /* ignored to save memory and most likly irrelevant for diver */
757 rxState = O2RX_INTENSITY;
758 break;
759 case O2RX_INTENSITY: StringToInt(tmpRxBuf,(uint32_t*)&tmpSensorDataDiveO2.intensity); /* raw data cycle */
760 rxState = O2RX_AMBIENTLIGHT;
761 break;
762 case O2RX_AMBIENTLIGHT: StringToInt(tmpRxBuf,(uint32_t*)&tmpSensorDataDiveO2.ambient); /* raw data cycle */
763 rxState = O2RX_PRESSURE;
764 break;
765 case O2RX_PRESSURE: StringToInt(tmpRxBuf,(uint32_t*)&tmpSensorDataDiveO2.pressure); /* raw data cycle */
766 rxState = O2RX_HUMIDITY;
767 break;
768 default:
769 break;
770 }
771 memset((char*) tmpRxBuf, 0, tmpRxIdx);
772 tmpRxIdx = 0;
773 }
774 }
775 }
776 else
777 { /* the following data items are the last of a sensor respond => store temporal data */
778 switch (rxState)
779 {
780 case O2RX_GETSTATUS: StringToInt(tmpRxBuf,&tmpSensorDataDiveO2.status);
781 externalInterface_SetSensorData(activeSensor,(uint8_t*)&tmpSensorDataDiveO2);
782 Comstatus_O2 = UART_O2_IDLE;
783 rxState = O2RX_IDLE;
784 break;
785 case O2RX_GETSUBSENSORS: StringToInt(tmpRxBuf,&tmpData);
786 Comstatus_O2 = UART_O2_IDLE;
787 rxState = O2RX_IDLE;
788 break;
789 case O2RX_HUMIDITY: StringToInt(tmpRxBuf,(uint32_t*)&tmpSensorDataDiveO2.humidity); /* raw data cycle */
790 externalInterface_SetSensorData(activeSensor,(uint8_t*)&tmpSensorDataDiveO2);
791 Comstatus_O2 = UART_O2_IDLE;
792 rxState = O2RX_IDLE;
793 break;
794 case O2RX_GETNR: StringToUInt64((char*)tmpRxBuf,&tmpSensorDataDiveO2.sensorId);
795 externalInterface_SetSensorData(activeSensor,(uint8_t*)&tmpSensorDataDiveO2);
796 index = activeSensor;
797 Comstatus_O2 = UART_O2_IDLE;
798 rxState = O2RX_IDLE;
799 break;
800 default: Comstatus_O2 = UART_O2_IDLE;
801 rxState = O2RX_IDLE;
802 break;
803 }
804 }
805 break;
806 default: rxState = O2RX_IDLE;
807 break;
808
809 }
810 rxBuffer[localRX] = BUFFER_NODATA;
811 localRX++;
812 rxReadIndex++;
813 if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
814 {
815 localRX = 0;
816 rxReadIndex = 0;
817 }
818 }
819
820 if((digO2Connected) && time_elapsed_ms(lastReceiveTick,HAL_GetTick()) > 4000) /* check for communication timeout */
821 {
822 digO2Connected = 0;
823 if(curAlive == lastAlive)
824 {
825 for(index = 0; index < MAX_ADC_CHANNEL; index++)
826 {
827 if(pmap[index] == SENSOR_DIGO2)
828 {
829 setExternalInterfaceChannel(index,0.0);
830 }
831 }
832 }
833 lastAlive = curAlive;
834 }
835 if((dmaActive == 0) && (externalInterface_isEnabledPower33())) /* Should never happen in normal operation => restart in case of communication error */
836 {
837 UART_StartDMA_Receiption();
838 }
839 }
840
841 void UART_SetDigO2_Channel(uint8_t channel)
842 {
843 if(channel <= MAX_MUX_CHANNEL)
844 {
845 activeSensor = channel;
846 }
847 }
848 void UART_MapDigO2_Channel(uint8_t channel, uint8_t muxAddress)
849 {
850 if(((channel < MAX_ADC_CHANNEL) || (channel == 0xff)) && (muxAddress < MAX_MUX_CHANNEL))
851 {
852 sensorMapping[muxAddress] = channel;
853 }
854 }
855
856 uint8_t UART_isDigO2Connected()
857 {
858 return digO2Connected;
859 }
860 uint8_t UART_isCO2Connected() 534 uint8_t UART_isCO2Connected()
861 { 535 {
862 return CO2Connected; 536 return CO2Connected;
863 } 537 }
864 uint8_t UART_isSentinelConnected() 538 uint8_t UART_isSentinelConnected()
884 } 558 }
885 } 559 }
886 } 560 }
887 } 561 }
888 562
889 563 void UART_ReadData(uint8_t sensorType)
564 {
565 uint8_t localRX = rxReadIndex;
566
567 while((rxBuffer[localRX]!=BUFFER_NODATA))
568 {
569 switch (sensorType)
570 {
571 case SENSOR_MUX:
572 case SENSOR_DIGO2: uartO2_ProcessData(rxBuffer[localRX]);
573 break;
574 // case SENSOR_CO2: uartCO2_Control();
575 break;
576 default:
577 break;
578 }
579
580 rxBuffer[localRX] = BUFFER_NODATA;
581 localRX++;
582 rxReadIndex++;
583 if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
584 {
585 localRX = 0;
586 rxReadIndex = 0;
587 }
588 }
589 }
590
591 void UART_FlushRxBuffer(void)
592 {
593 while(rxBuffer[rxReadIndex] != BUFFER_NODATA)
594 {
595 rxBuffer[rxReadIndex] = BUFFER_NODATA;
596 rxReadIndex++;
597 if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
598 {
599 rxReadIndex = 0;
600 }
601 }
602
603 }
890 604
891 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ 605 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/