comparison Small_CPU/Src/uart.c @ 690:fca2bd25e6e2 Betatest

Added Sentinel protocoll support: Added function for evaluation of the Sentinel protocoll. At the moment only O2 sensor values are extracted.
author Ideenmodellierer
date Fri, 05 Aug 2022 15:22:26 +0200
parents 1b995079c045
children f1b40364b0af
comparison
equal deleted inserted replaced
689:4dd487b407f7 690:fca2bd25e6e2
92 /* DMA interrupt init */ 92 /* DMA interrupt init */
93 HAL_NVIC_SetPriority(DMA2_Stream5_IRQn, 0, 0); 93 HAL_NVIC_SetPriority(DMA2_Stream5_IRQn, 0, 0);
94 HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn); 94 HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn);
95 } 95 }
96 96
97 97 void ConvertByteToHexString(uint8_t byte, char* str)
98 uint32_t dataValue = 0; 98 {
99 99 uint8_t worker = 0;
100 void HandleUARTData(void) 100 uint8_t digit = 0;
101 uint8_t digitCnt = 1;
102
103 worker = byte;
104 while((worker!=0) && (digitCnt != 255))
105 {
106 digit = worker % 16;
107 if( digit < 10)
108 {
109 digit += '0';
110 }
111 else
112 {
113 digit += 'A' - 10;
114 }
115 str[digitCnt--]= digit;
116 worker = worker / 16;
117 }
118 }
119
120
121 #ifdef ENABLE_CO2_SUPPORT
122 void HandleUARTCO2Data(void)
101 { 123 {
102 uint8_t localRX = rxReadIndex; 124 uint8_t localRX = rxReadIndex;
103 uint8_t dataType = 0; 125 uint8_t dataType = 0;
126 uint32_t dataValue = 0;
104 static receiveState_t rxState = RX_Ready; 127 static receiveState_t rxState = RX_Ready;
105 static uint32_t lastReceiveTick = 0; 128 static uint32_t lastReceiveTick = 0;
106 129
107 while(localRX != rxWriteIndex) 130 while(localRX != rxWriteIndex)
108 { 131 {
176 { 199 {
177 dmaActive = 1; 200 dmaActive = 1;
178 } 201 }
179 } 202 }
180 } 203 }
204 #endif
205
206 #ifdef ENABLE_SENTINEL_MODE
207 void HandleUARTSentinelData(void)
208 {
209 uint8_t localRX = rxReadIndex;
210 static uint8_t dataType = 0;
211 static uint32_t dataValue[3];
212 static uint8_t dataValueIdx = 0;
213 static receiveState_t rxState = RX_Ready;
214 static uint32_t lastReceiveTick = 0;
215 static uint8_t lastAlive = 0;
216 static uint8_t curAlive = 0;
217 static uint8_t checksum = 0;
218 char checksum_str[]="00";
219
220 while(localRX != rxWriteIndex)
221 {
222 lastReceiveTick = HAL_GetTick();
223
224 switch(rxState)
225 {
226 case RX_Ready: if((rxBuffer[localRX] >= 'a') && (rxBuffer[localRX] <= 'z'))
227 {
228 rxState = RX_DetectStart;
229 curAlive = rxBuffer[localRX];
230 checksum = 0;
231 }
232 break;
233
234 case RX_DetectStart: checksum += rxBuffer[localRX];
235 if(rxBuffer[localRX] == '1')
236 {
237 rxState = RX_SelectData;
238 dataType = 0xFF;
239
240 }
241 else
242 {
243 rxState = RX_Ready;
244 }
245 break;
246
247 case RX_SelectData: checksum += rxBuffer[localRX];
248 switch(rxBuffer[localRX])
249 {
250 case 'T': dataType = rxBuffer[localRX];
251 break;
252 case '0': if(dataType != 0xff)
253 {
254 rxState = RX_Data0;
255 dataValueIdx = 0;
256 dataValue[0] = 0;
257
258 }
259 else
260 {
261 rxState = RX_Ready;
262 }
263 break;
264 default: rxState = RX_Ready;
265 }
266 break;
267
268 case RX_Data0:
269 case RX_Data1:
270 case RX_Data2:
271 case RX_Data4:
272 case RX_Data5:
273 case RX_Data6:
274 case RX_Data8:
275 case RX_Data9:
276 case RX_Data10: checksum += rxBuffer[localRX];
277 if((rxBuffer[localRX] >= '0') && (rxBuffer[localRX] <= '9'))
278 {
279 dataValue[dataValueIdx] = dataValue[dataValueIdx] * 10 + (rxBuffer[localRX] - '0');
280 rxState++;
281 }
282 else
283 {
284 rxState = RX_Ready;
285 }
286 break;
287
288 case RX_Data3:
289 case RX_Data7: checksum += rxBuffer[localRX];
290 if(rxBuffer[localRX] == '0')
291 {
292 rxState++;
293 dataValueIdx++;
294 dataValue[dataValueIdx] = 0;
295 }
296 else
297 {
298 rxState = RX_Ready;
299 }
300 break;
301 case RX_Data11: rxState = RX_DataComplete;
302 ConvertByteToHexString(checksum,checksum_str);
303 if(rxBuffer[localRX] == checksum_str[0])
304 {
305 rxState = RX_DataComplete;
306 }
307 else
308 {
309 rxState = RX_Ready;
310 }
311
312 break;
313
314 case RX_DataComplete: if(rxBuffer[localRX] == checksum_str[1])
315 {
316 setExternalInterfaceChannel(0,(float)(dataValue[0] / 10.0));
317 setExternalInterfaceChannel(1,(float)(dataValue[1] / 10.0));
318 setExternalInterfaceChannel(2,(float)(dataValue[2] / 10.0));
319 }
320 rxState = RX_Ready;
321 break;
322
323
324 default: rxState = RX_Ready;
325 break;
326
327 }
328
329 localRX++;
330 rxReadIndex++;
331 if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
332 {
333 localRX = 0;
334 rxReadIndex = 0;
335 }
336 }
337
338 if(time_elapsed_ms(lastReceiveTick,HAL_GetTick()) > 4000) /* check for communication timeout */
339 {
340 if(curAlive == lastAlive)
341 {
342 setExternalInterfaceChannel(0,0.0);
343 setExternalInterfaceChannel(1,0.0);
344 setExternalInterfaceChannel(2,0.0);
345 }
346 lastAlive = curAlive;
347 }
348
349 if((dmaActive == 0) && (externalInterface_isEnabledPower33())) /* Should never happen in normal operation => restart in case of communication error */
350 {
351 if(HAL_OK == HAL_UART_Receive_DMA (&huart1, &rxBuffer[rxWriteIndex], CHUNK_SIZE))
352 {
353 dmaActive = 1;
354 }
355 }
356 }
357 #endif
181 358
182 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) 359 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
183 { 360 {
184 if(huart == &huart1) 361 if(huart == &huart1)
185 { 362 {