comparison Small_CPU/Src/uart.c @ 842:c3dd461ca3f9 Evo_2_23

Migrated Sentinel protocol to new UART structure: The Sentinel protocol had not been migrated to the new UART structure which was introduced with the introduction of the UART MUX. The Sentinel is now supported by autodetection again (development version only)
author Ideenmodellierer
date Mon, 15 Jan 2024 21:44:18 +0100
parents 9602a7338f28
children ad96f99ebc78
comparison
equal deleted inserted replaced
841:70092f552f5a 842:c3dd461ca3f9
20 */ 20 */
21 /* Includes ------------------------------------------------------------------*/ 21 /* Includes ------------------------------------------------------------------*/
22 #include "uart.h" 22 #include "uart.h"
23 #include "uartProtocol_O2.h" 23 #include "uartProtocol_O2.h"
24 #include "uartProtocol_Co2.h" 24 #include "uartProtocol_Co2.h"
25 #include "uartProtocol_Sentinel.h"
25 #include "externalInterface.h" 26 #include "externalInterface.h"
26 #include "data_exchange.h" 27 #include "data_exchange.h"
27 #include <string.h> /* memset */ 28 #include <string.h> /* memset */
28 29
29 /* Private variables ---------------------------------------------------------*/ 30 /* Private variables ---------------------------------------------------------*/
40 uint8_t rxBuffer[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow fariations in buffer read time */ 41 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 */ 42 static uint8_t rxWriteIndex; /* Index of the data item which is analysed */
42 static uint8_t rxReadIndex; /* Index at which new data is stared */ 43 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 */ 44 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 */ 45 static uint8_t dmaActive; /* Indicator if DMA reception needs to be started */
45
46
47 static uint8_t SentinelConnected = 0; /* Binary indicator if a sensor is connected or not */
48 46
49 47
50 /* Exported functions --------------------------------------------------------*/ 48 /* Exported functions --------------------------------------------------------*/
51 49
52 50
70 memset(rxBuffer,BUFFER_NODATA,sizeof(rxBuffer)); 68 memset(rxBuffer,BUFFER_NODATA,sizeof(rxBuffer));
71 rxReadIndex = 0; 69 rxReadIndex = 0;
72 lastCmdIndex = 0; 70 lastCmdIndex = 0;
73 rxWriteIndex = 0; 71 rxWriteIndex = 0;
74 dmaActive = 0; 72 dmaActive = 0;
75
76 SentinelConnected = 0;
77
78 } 73 }
79 74
80 void MX_USART1_UART_DeInit(void) 75 void MX_USART1_UART_DeInit(void)
81 { 76 {
82 HAL_DMA_Abort(&hdma_usart1_rx); 77 HAL_DMA_Abort(&hdma_usart1_rx);
164 result += pstr[index] - '0'; 159 result += pstr[index] - '0';
165 index++; 160 index++;
166 } 161 }
167 *puint64 = result; 162 *puint64 = result;
168 } 163 }
169 void ConvertByteToHexString(uint8_t byte, char* str)
170 {
171 uint8_t worker = 0;
172 uint8_t digit = 0;
173 uint8_t digitCnt = 1;
174
175 worker = byte;
176 while((worker!=0) && (digitCnt != 255))
177 {
178 digit = worker % 16;
179 if( digit < 10)
180 {
181 digit += '0';
182 }
183 else
184 {
185 digit += 'A' - 10;
186 }
187 str[digitCnt--]= digit;
188 worker = worker / 16;
189 }
190 }
191 164
192 void UART_StartDMA_Receiption() 165 void UART_StartDMA_Receiption()
193 { 166 {
194 if(dmaActive == 0) 167 if(dmaActive == 0)
195 { 168 {
219 rxReadIndex = 0; 192 rxReadIndex = 0;
220 rxWriteIndex = 0; 193 rxWriteIndex = 0;
221 dmaActive = 0; 194 dmaActive = 0;
222 UART_StartDMA_Receiption(); 195 UART_StartDMA_Receiption();
223 } 196 }
224 }
225
226 #ifdef ENABLE_SENTINEL_MODE
227 void UART_HandleSentinelData(void)
228 {
229 uint8_t localRX = rxReadIndex;
230 static uint8_t dataType = 0;
231 static uint32_t dataValue[3];
232 static uint8_t dataValueIdx = 0;
233 static receiveState_t rxState = RX_Ready;
234 static uint32_t lastReceiveTick = 0;
235 static uint8_t lastAlive = 0;
236 static uint8_t curAlive = 0;
237 static uint8_t checksum = 0;
238 static char checksum_str[]="00";
239
240 while((rxBuffer[localRX]!=0))
241 {
242 lastReceiveTick = HAL_GetTick();
243
244 switch(rxState)
245 {
246 case RX_Ready: if((rxBuffer[localRX] >= 'a') && (rxBuffer[localRX] <= 'z'))
247 {
248 rxState = RX_DetectStart;
249 curAlive = rxBuffer[localRX];
250 checksum = 0;
251 }
252 break;
253
254 case RX_DetectStart: checksum += rxBuffer[localRX];
255 if(rxBuffer[localRX] == '1')
256 {
257 rxState = RX_SelectData;
258 dataType = 0xFF;
259
260 }
261 else
262 {
263 rxState = RX_Ready;
264 }
265 break;
266
267 case RX_SelectData: checksum += rxBuffer[localRX];
268 switch(rxBuffer[localRX])
269 {
270 case 'T': dataType = rxBuffer[localRX];
271 break;
272 case '0': if(dataType != 0xff)
273 {
274 rxState = RX_Data0;
275 dataValueIdx = 0;
276 dataValue[0] = 0;
277
278 }
279 else
280 {
281 rxState = RX_Ready;
282 }
283 break;
284 default: rxState = RX_Ready;
285 }
286 break;
287
288 case RX_Data0:
289 case RX_Data1:
290 case RX_Data2:
291 case RX_Data4:
292 case RX_Data5:
293 case RX_Data6:
294 case RX_Data8:
295 case RX_Data9:
296 case RX_Data10: checksum += rxBuffer[localRX];
297 if((rxBuffer[localRX] >= '0') && (rxBuffer[localRX] <= '9'))
298 {
299 dataValue[dataValueIdx] = dataValue[dataValueIdx] * 10 + (rxBuffer[localRX] - '0');
300 rxState++;
301 }
302 else
303 {
304 rxState = RX_Ready;
305 }
306 break;
307
308 case RX_Data3:
309 case RX_Data7: checksum += rxBuffer[localRX];
310 if(rxBuffer[localRX] == '0')
311 {
312 rxState++;
313 dataValueIdx++;
314 dataValue[dataValueIdx] = 0;
315 }
316 else
317 {
318 rxState = RX_Ready;
319 }
320 break;
321 case RX_Data11: rxState = RX_DataComplete;
322 ConvertByteToHexString(checksum,checksum_str);
323 if(rxBuffer[localRX] == checksum_str[0])
324 {
325 rxState = RX_DataComplete;
326 }
327 else
328 {
329 rxState = RX_Ready;
330 }
331
332 break;
333
334 case RX_DataComplete: if(rxBuffer[localRX] == checksum_str[1])
335 {
336 setExternalInterfaceChannel(0,(float)(dataValue[0] / 10.0));
337 setExternalInterfaceChannel(1,(float)(dataValue[1] / 10.0));
338 setExternalInterfaceChannel(2,(float)(dataValue[2] / 10.0));
339 SentinelConnected = 1;
340 }
341 rxState = RX_Ready;
342 break;
343
344
345 default: rxState = RX_Ready;
346 break;
347
348 }
349 localRX++;
350 rxReadIndex++;
351 if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
352 {
353 localRX = 0;
354 rxReadIndex = 0;
355 }
356 }
357
358 if(time_elapsed_ms(lastReceiveTick,HAL_GetTick()) > 4000) /* check for communication timeout */
359 {
360 if(curAlive == lastAlive)
361 {
362 setExternalInterfaceChannel(0,0.0);
363 setExternalInterfaceChannel(1,0.0);
364 setExternalInterfaceChannel(2,0.0);
365 SentinelConnected = 0;
366 }
367 lastAlive = curAlive;
368 }
369
370 if((dmaActive == 0) && (externalInterface_isEnabledPower33())) /* Should never happen in normal operation => restart in case of communication error */
371 {
372 UART_StartDMA_Receiption();
373 }
374 }
375 #endif
376
377
378
379 uint8_t UART_isSentinelConnected()
380 {
381 return SentinelConnected;
382 } 197 }
383 198
384 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) 199 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
385 { 200 {
386 if(huart == &huart1) 201 if(huart == &huart1)
411 break; 226 break;
412 #ifdef ENABLE_CO2_SUPPORT 227 #ifdef ENABLE_CO2_SUPPORT
413 case SENSOR_CO2: uartCo2_ProcessData(rxBuffer[localRX]); 228 case SENSOR_CO2: uartCo2_ProcessData(rxBuffer[localRX]);
414 break; 229 break;
415 #endif 230 #endif
231 #ifdef ENABLE_SENTINEL_MODE
232 case SENSOR_SENTINEL: uartSentinel_ProcessData(rxBuffer[localRX]);
233 break;
234 #endif
416 default: 235 default:
417 break; 236 break;
418 } 237 }
419 238
420 rxBuffer[localRX] = BUFFER_NODATA; 239 rxBuffer[localRX] = BUFFER_NODATA;