Mercurial > public > ostc4
annotate Small_CPU/Src/uart.c @ 795:d4083ac09b5d
Moved DiveO2 sensor specific funtionality into separated file:
Having the protocol functions of all sensors within one file made the code hard to ready and created aa risk of interference between the protocols. In furture every (new) sensor should be maintained in its own source file. The ols UART.c does only contain the function which are used by all UART protocols (common functions).
author | Ideenmodellierer |
---|---|
date | Mon, 31 Jul 2023 19:50:36 +0200 |
parents | bb37d4f3e50e |
children | e9eba334b942 |
rev | line source |
---|---|
38 | 1 /** |
2 ****************************************************************************** | |
3 * @file uart.c | |
4 * @author heinrichs weikamp gmbh | |
5 * @version V0.0.1 | |
6 * @date 27-March-2014 | |
7 * @brief button control | |
8 * | |
9 @verbatim | |
10 ============================================================================== | |
11 ##### How to use ##### | |
12 ============================================================================== | |
13 @endverbatim | |
14 ****************************************************************************** | |
15 * @attention | |
16 * | |
17 * <h2><center>© COPYRIGHT(c) 2015 heinrichs weikamp</center></h2> | |
18 * | |
19 ****************************************************************************** | |
20 */ | |
21 /* Includes ------------------------------------------------------------------*/ | |
22 #include "uart.h" | |
794 | 23 #include "uartProtocol_O2.h" |
662 | 24 #include "externalInterface.h" |
25 #include "data_exchange.h" | |
704
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
26 #include <string.h> /* memset */ |
38 | 27 |
28 /* Private variables ---------------------------------------------------------*/ | |
29 | |
794 | 30 |
787
aeb72882f30a
Dev Bugfx Empty buffer indication and stability improvments:
Ideenmodellierer
parents:
785
diff
changeset
|
31 |
781 | 32 #define CHUNK_SIZE (25u) /* the DMA will handle chunk size transfers */ |
33 #define CHUNKS_PER_BUFFER (5u) | |
794 | 34 |
662 | 35 UART_HandleTypeDef huart1; |
36 | |
37 DMA_HandleTypeDef hdma_usart1_rx; | |
38 | 38 |
662 | 39 uint8_t rxBuffer[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow fariations in buffer read time */ |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
40 static uint8_t rxWriteIndex; /* Index of the data item which is analysed */ |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
41 static uint8_t rxReadIndex; /* Index at which new data is stared */ |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
42 static uint8_t lastCmdIndex; /* Index of last command which has not been completly received */ |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
43 static uint8_t dmaActive; /* Indicator if DMA reception needs to be started */ |
794 | 44 |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
45 static uint8_t CO2Connected = 0; /* Binary indicator if a sensor is connected or not */ |
742 | 46 static uint8_t SentinelConnected = 0; /* Binary indicator if a sensor is connected or not */ |
38 | 47 |
794 | 48 |
714
045ff7800501
Added customizable data area for specific sensor data:
Ideenmodellierer
parents:
704
diff
changeset
|
49 |
794 | 50 static uartCO2Status_t ComStatus_CO2 = UART_CO2_INIT; |
704
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
51 |
662 | 52 float LED_Level = 0.0; /* Normalized LED value which may be used as indication for the health status of the sensor */ |
53 float LED_ZeroOffset = 0.0; | |
54 float pCO2 = 0.0; | |
38 | 55 /* Exported functions --------------------------------------------------------*/ |
56 | |
794 | 57 |
58 | |
59 //huart.Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), new_baudrate); | |
60 | |
662 | 61 void MX_USART1_UART_Init(void) |
38 | 62 { |
662 | 63 /* regular init */ |
64 | |
65 huart1.Instance = USART1; | |
704
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
66 |
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
67 if(externalInterface_GetUARTProtocol() == 0x04) |
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
68 { |
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
69 huart1.Init.BaudRate = 19200; |
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
70 } |
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
71 else |
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
72 { |
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
73 huart1.Init.BaudRate = 9600; |
794 | 74 ComStatus_CO2 = UART_CO2_INIT; |
704
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
75 } |
662 | 76 huart1.Init.WordLength = UART_WORDLENGTH_8B; |
77 huart1.Init.StopBits = UART_STOPBITS_1; | |
78 huart1.Init.Parity = UART_PARITY_NONE; | |
79 huart1.Init.Mode = UART_MODE_TX_RX; | |
80 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; | |
81 huart1.Init.OverSampling = UART_OVERSAMPLING_16; | |
82 | |
83 HAL_UART_Init(&huart1); | |
84 | |
704
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
85 MX_USART1_DMA_Init(); |
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
86 |
787
aeb72882f30a
Dev Bugfx Empty buffer indication and stability improvments:
Ideenmodellierer
parents:
785
diff
changeset
|
87 memset(rxBuffer,BUFFER_NODATA,sizeof(rxBuffer)); |
662 | 88 rxReadIndex = 0; |
89 lastCmdIndex = 0; | |
90 rxWriteIndex = 0; | |
91 dmaActive = 0; | |
794 | 92 |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
93 CO2Connected = 0; |
742 | 94 SentinelConnected = 0; |
794 | 95 |
662 | 96 } |
38 | 97 |
662 | 98 void MX_USART1_UART_DeInit(void) |
99 { | |
704
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
100 HAL_DMA_Abort(&hdma_usart1_rx); |
662 | 101 HAL_DMA_DeInit(&hdma_usart1_rx); |
102 HAL_UART_DeInit(&huart1); | |
103 } | |
104 | |
105 void MX_USART1_DMA_Init() | |
106 { | |
107 /* DMA controller clock enable */ | |
108 __DMA2_CLK_ENABLE(); | |
109 | |
110 /* Peripheral DMA init*/ | |
111 hdma_usart1_rx.Instance = DMA2_Stream5; | |
112 hdma_usart1_rx.Init.Channel = DMA_CHANNEL_4; | |
113 hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; //DMA_MEMORY_TO_PERIPH; | |
114 hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE; | |
115 hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE; | |
116 hdma_usart1_rx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE; | |
117 hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; | |
118 hdma_usart1_rx.Init.Mode = DMA_NORMAL; | |
119 hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW; | |
120 hdma_usart1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; | |
121 HAL_DMA_Init(&hdma_usart1_rx); | |
122 | |
123 __HAL_LINKDMA(&huart1,hdmarx,hdma_usart1_rx); | |
124 | |
125 /* DMA interrupt init */ | |
126 HAL_NVIC_SetPriority(DMA2_Stream5_IRQn, 0, 0); | |
127 HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn); | |
38 | 128 } |
129 | |
794 | 130 void UART_MUX_SelectAddress(uint8_t muxAddress) |
779
0b5f45448eb6
Added UART multiplexer support for DiveO2:
Ideenmodellierer
parents:
747
diff
changeset
|
131 { |
0b5f45448eb6
Added UART multiplexer support for DiveO2:
Ideenmodellierer
parents:
747
diff
changeset
|
132 uint8_t indexstr[4]; |
0b5f45448eb6
Added UART multiplexer support for DiveO2:
Ideenmodellierer
parents:
747
diff
changeset
|
133 |
794 | 134 if(muxAddress <= MAX_MUX_CHANNEL) |
779
0b5f45448eb6
Added UART multiplexer support for DiveO2:
Ideenmodellierer
parents:
747
diff
changeset
|
135 { |
794 | 136 indexstr[0] = '~'; |
137 indexstr[1] = muxAddress; | |
138 indexstr[2] = 0x0D; | |
139 indexstr[3] = 0x0A; | |
140 | |
141 HAL_UART_Transmit(&huart1,indexstr,4,10); | |
779
0b5f45448eb6
Added UART multiplexer support for DiveO2:
Ideenmodellierer
parents:
747
diff
changeset
|
142 } |
0b5f45448eb6
Added UART multiplexer support for DiveO2:
Ideenmodellierer
parents:
747
diff
changeset
|
143 } |
0b5f45448eb6
Added UART multiplexer support for DiveO2:
Ideenmodellierer
parents:
747
diff
changeset
|
144 |
794 | 145 |
146 void UART_SendCmdString(uint8_t *cmdString) | |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
147 { |
794 | 148 uint8_t cmdLength = strlen((char*)cmdString); |
149 | |
150 if(cmdLength < 20) /* A longer string is an indication for a missing 0 termination */ | |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
151 { |
794 | 152 if(dmaActive == 0) |
153 { | |
154 UART_StartDMA_Receiption(); | |
155 } | |
156 HAL_UART_Transmit(&huart1,cmdString,cmdLength,10); | |
157 } | |
158 } | |
159 | |
160 void DigitalCO2_SendCmd(uint8_t CO2Cmd, uint8_t *cmdString, uint16_t *cmdLength) | |
161 { | |
162 switch (CO2Cmd) | |
163 { | |
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; | |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
174 default: *cmdLength = 0; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
175 break; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
176 } |
794 | 177 if(cmdLength != 0) |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
178 { |
794 | 179 HAL_UART_Transmit(&huart1,cmdString,*cmdLength,10); |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
180 } |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
181 } |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
182 |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
183 void StringToInt(char *pstr, uint32_t *puInt32) |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
184 { |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
185 uint8_t index = 0; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
186 uint32_t result = 0; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
187 while((pstr[index] >= '0') && (pstr[index] <= '9')) |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
188 { |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
189 result *=10; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
190 result += pstr[index] - '0'; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
191 index++; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
192 } |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
193 *puInt32 = result; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
194 } |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
195 |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
196 void StringToUInt64(char *pstr, uint64_t *puint64) |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
197 { |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
198 uint8_t index = 0; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
199 uint64_t result = 0; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
200 while((pstr[index] >= '0') && (pstr[index] <= '9')) |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
201 { |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
202 result *=10; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
203 result += pstr[index] - '0'; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
204 index++; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
205 } |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
206 *puint64 = result; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
207 } |
690 | 208 void ConvertByteToHexString(uint8_t byte, char* str) |
209 { | |
210 uint8_t worker = 0; | |
211 uint8_t digit = 0; | |
212 uint8_t digitCnt = 1; | |
38 | 213 |
690 | 214 worker = byte; |
215 while((worker!=0) && (digitCnt != 255)) | |
216 { | |
217 digit = worker % 16; | |
218 if( digit < 10) | |
219 { | |
220 digit += '0'; | |
221 } | |
222 else | |
223 { | |
224 digit += 'A' - 10; | |
225 } | |
226 str[digitCnt--]= digit; | |
227 worker = worker / 16; | |
228 } | |
229 } | |
662 | 230 |
742 | 231 void UART_StartDMA_Receiption() |
232 { | |
233 if(HAL_OK == HAL_UART_Receive_DMA (&huart1, &rxBuffer[rxWriteIndex], CHUNK_SIZE)) | |
234 { | |
235 dmaActive = 1; | |
236 } | |
237 } | |
690 | 238 |
239 #ifdef ENABLE_CO2_SUPPORT | |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
240 void UART_HandleCO2Data(void) |
38 | 241 { |
662 | 242 uint8_t localRX = rxReadIndex; |
747
df0d43da1614
Added pressure compensation to CO2 detection:
Ideenmodellierer
parents:
742
diff
changeset
|
243 static uint8_t dataType = 0; |
df0d43da1614
Added pressure compensation to CO2 detection:
Ideenmodellierer
parents:
742
diff
changeset
|
244 static uint32_t dataValue = 0; |
662 | 245 static receiveState_t rxState = RX_Ready; |
246 static uint32_t lastReceiveTick = 0; | |
794 | 247 static uint32_t lastTransmitTick = 0; |
248 static uint8_t cmdString[10]; | |
249 static uint16_t cmdLength = 0; | |
662 | 250 |
794 | 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 } | |
725 | 260 |
794 | 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 | |
662 | 277 { |
794 | 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; | |
662 | 298 if(rxState == RX_Ready) /* identify data content */ |
299 { | |
300 switch(rxBuffer[localRX]) | |
301 { | |
302 case 'l': | |
303 case 'D': | |
304 case 'Z': | |
794 | 305 case '.': |
662 | 306 dataType = rxBuffer[localRX]; |
307 rxState = RX_Data0; | |
308 dataValue = 0; | |
309 break; | |
310 | |
311 default: /* unknown or corrupted => ignore */ | |
312 break; | |
313 } | |
314 } | |
725 | 315 else if((rxBuffer[localRX] >= '0') && (rxBuffer[localRX] <= '9')) |
662 | 316 { |
725 | 317 if((rxState >= RX_Data0) && (rxState <= RX_Data4)) |
662 | 318 { |
319 dataValue = dataValue * 10 + (rxBuffer[localRX] - '0'); | |
320 rxState++; | |
725 | 321 if(rxState == RX_Data5) |
322 { | |
323 rxState = RX_DataComplete; | |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
324 CO2Connected = 1; |
725 | 325 } |
326 } | |
327 else /* protocol error data has max 5 digits */ | |
328 { | |
329 rxState = RX_Ready; | |
662 | 330 } |
331 } | |
332 if((rxBuffer[localRX] == ' ') || (rxBuffer[localRX] == '\n')) /* Abort data detection */ | |
333 { | |
334 if(rxState == RX_DataComplete) | |
335 { | |
336 if(externalInterface_GetCO2State() == 0) | |
337 { | |
338 externalInterface_SetCO2State(EXT_INTERFACE_33V_ON); | |
339 } | |
340 switch(dataType) | |
341 { | |
342 case 'D': externalInterface_SetCO2SignalStrength(dataValue); | |
343 break; | |
344 case 'l': LED_ZeroOffset = dataValue; | |
345 break; | |
346 case 'Z': externalInterface_SetCO2Value(dataValue); | |
347 break; | |
794 | 348 case '.': externalInterface_SetCO2Scale(dataValue); |
349 break; | |
747
df0d43da1614
Added pressure compensation to CO2 detection:
Ideenmodellierer
parents:
742
diff
changeset
|
350 default: rxState = RX_Ready; |
df0d43da1614
Added pressure compensation to CO2 detection:
Ideenmodellierer
parents:
742
diff
changeset
|
351 break; |
662 | 352 } |
353 } | |
354 if(rxState != RX_Data0) /* reset state machine because message in wrong format */ | |
355 { | |
356 rxState = RX_Ready; | |
357 } | |
358 } | |
794 | 359 rxBuffer[localRX] = BUFFER_NODATA; |
662 | 360 localRX++; |
361 rxReadIndex++; | |
362 if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER) | |
363 { | |
364 localRX = 0; | |
365 rxReadIndex = 0; | |
366 } | |
367 } | |
368 | |
369 if(time_elapsed_ms(lastReceiveTick,HAL_GetTick()) > 2000) /* check for communication timeout */ | |
370 { | |
371 externalInterface_SetCO2State(0); | |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
372 CO2Connected = 0; |
662 | 373 } |
374 | |
375 if((dmaActive == 0) && (externalInterface_isEnabledPower33())) /* Should never happen in normal operation => restart in case of communication error */ | |
376 { | |
742 | 377 UART_StartDMA_Receiption(); |
662 | 378 } |
38 | 379 } |
690 | 380 #endif |
381 | |
382 #ifdef ENABLE_SENTINEL_MODE | |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
383 void UART_HandleSentinelData(void) |
690 | 384 { |
385 uint8_t localRX = rxReadIndex; | |
386 static uint8_t dataType = 0; | |
387 static uint32_t dataValue[3]; | |
388 static uint8_t dataValueIdx = 0; | |
389 static receiveState_t rxState = RX_Ready; | |
390 static uint32_t lastReceiveTick = 0; | |
391 static uint8_t lastAlive = 0; | |
392 static uint8_t curAlive = 0; | |
393 static uint8_t checksum = 0; | |
742 | 394 static char checksum_str[]="00"; |
690 | 395 |
742 | 396 while((rxBuffer[localRX]!=0)) |
690 | 397 { |
398 lastReceiveTick = HAL_GetTick(); | |
399 | |
400 switch(rxState) | |
401 { | |
402 case RX_Ready: if((rxBuffer[localRX] >= 'a') && (rxBuffer[localRX] <= 'z')) | |
403 { | |
404 rxState = RX_DetectStart; | |
405 curAlive = rxBuffer[localRX]; | |
406 checksum = 0; | |
407 } | |
408 break; | |
409 | |
410 case RX_DetectStart: checksum += rxBuffer[localRX]; | |
411 if(rxBuffer[localRX] == '1') | |
412 { | |
413 rxState = RX_SelectData; | |
414 dataType = 0xFF; | |
415 | |
416 } | |
417 else | |
418 { | |
419 rxState = RX_Ready; | |
420 } | |
421 break; | |
422 | |
423 case RX_SelectData: checksum += rxBuffer[localRX]; | |
424 switch(rxBuffer[localRX]) | |
425 { | |
426 case 'T': dataType = rxBuffer[localRX]; | |
427 break; | |
428 case '0': if(dataType != 0xff) | |
429 { | |
430 rxState = RX_Data0; | |
431 dataValueIdx = 0; | |
432 dataValue[0] = 0; | |
433 | |
434 } | |
435 else | |
436 { | |
437 rxState = RX_Ready; | |
438 } | |
439 break; | |
440 default: rxState = RX_Ready; | |
441 } | |
442 break; | |
443 | |
444 case RX_Data0: | |
445 case RX_Data1: | |
446 case RX_Data2: | |
447 case RX_Data4: | |
448 case RX_Data5: | |
449 case RX_Data6: | |
450 case RX_Data8: | |
451 case RX_Data9: | |
452 case RX_Data10: checksum += rxBuffer[localRX]; | |
453 if((rxBuffer[localRX] >= '0') && (rxBuffer[localRX] <= '9')) | |
454 { | |
455 dataValue[dataValueIdx] = dataValue[dataValueIdx] * 10 + (rxBuffer[localRX] - '0'); | |
456 rxState++; | |
457 } | |
458 else | |
459 { | |
460 rxState = RX_Ready; | |
461 } | |
462 break; | |
463 | |
464 case RX_Data3: | |
465 case RX_Data7: checksum += rxBuffer[localRX]; | |
466 if(rxBuffer[localRX] == '0') | |
467 { | |
468 rxState++; | |
469 dataValueIdx++; | |
470 dataValue[dataValueIdx] = 0; | |
471 } | |
472 else | |
473 { | |
474 rxState = RX_Ready; | |
475 } | |
476 break; | |
477 case RX_Data11: rxState = RX_DataComplete; | |
478 ConvertByteToHexString(checksum,checksum_str); | |
479 if(rxBuffer[localRX] == checksum_str[0]) | |
480 { | |
481 rxState = RX_DataComplete; | |
482 } | |
483 else | |
484 { | |
485 rxState = RX_Ready; | |
486 } | |
487 | |
488 break; | |
489 | |
490 case RX_DataComplete: if(rxBuffer[localRX] == checksum_str[1]) | |
491 { | |
492 setExternalInterfaceChannel(0,(float)(dataValue[0] / 10.0)); | |
493 setExternalInterfaceChannel(1,(float)(dataValue[1] / 10.0)); | |
494 setExternalInterfaceChannel(2,(float)(dataValue[2] / 10.0)); | |
742 | 495 SentinelConnected = 1; |
690 | 496 } |
497 rxState = RX_Ready; | |
498 break; | |
499 | |
500 | |
501 default: rxState = RX_Ready; | |
502 break; | |
503 | |
504 } | |
505 localRX++; | |
506 rxReadIndex++; | |
507 if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER) | |
508 { | |
509 localRX = 0; | |
510 rxReadIndex = 0; | |
511 } | |
512 } | |
513 | |
514 if(time_elapsed_ms(lastReceiveTick,HAL_GetTick()) > 4000) /* check for communication timeout */ | |
515 { | |
516 if(curAlive == lastAlive) | |
517 { | |
518 setExternalInterfaceChannel(0,0.0); | |
519 setExternalInterfaceChannel(1,0.0); | |
520 setExternalInterfaceChannel(2,0.0); | |
742 | 521 SentinelConnected = 0; |
690 | 522 } |
523 lastAlive = curAlive; | |
524 } | |
525 | |
526 if((dmaActive == 0) && (externalInterface_isEnabledPower33())) /* Should never happen in normal operation => restart in case of communication error */ | |
527 { | |
742 | 528 UART_StartDMA_Receiption(); |
690 | 529 } |
530 } | |
531 #endif | |
38 | 532 |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
533 |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
534 uint8_t UART_isCO2Connected() |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
535 { |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
536 return CO2Connected; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
537 } |
742 | 538 uint8_t UART_isSentinelConnected() |
539 { | |
540 return SentinelConnected; | |
541 } | |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
542 |
662 | 543 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) |
38 | 544 { |
662 | 545 if(huart == &huart1) |
546 { | |
547 dmaActive = 0; | |
548 rxWriteIndex+=CHUNK_SIZE; | |
549 if(rxWriteIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER) | |
550 { | |
551 rxWriteIndex = 0; | |
552 } | |
787
aeb72882f30a
Dev Bugfx Empty buffer indication and stability improvments:
Ideenmodellierer
parents:
785
diff
changeset
|
553 if((rxWriteIndex / CHUNK_SIZE) != (rxReadIndex / CHUNK_SIZE) || (rxWriteIndex == rxReadIndex)) /* start next transfer if we did not catch up with read index */ |
662 | 554 { |
787
aeb72882f30a
Dev Bugfx Empty buffer indication and stability improvments:
Ideenmodellierer
parents:
785
diff
changeset
|
555 if(externalInterface_GetUARTProtocol() != 0) |
662 | 556 { |
742 | 557 UART_StartDMA_Receiption(); |
662 | 558 } |
559 } | |
560 } | |
38 | 561 } |
562 | |
794 | 563 void UART_ReadData(uint8_t sensorType) |
564 { | |
565 uint8_t localRX = rxReadIndex; | |
38 | 566 |
794 | 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 } | |
662 | 604 |
38 | 605 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ |