comparison Small_CPU/Src/uart.c @ 696:cc542448fb28

Merge
author heinrichsweikamp
date Fri, 19 Aug 2022 11:30:24 +0200
parents fca2bd25e6e2
children f1b40364b0af
comparison
equal deleted inserted replaced
661:87bee7cc77b3 696:cc542448fb28
18 * 18 *
19 ****************************************************************************** 19 ******************************************************************************
20 */ 20 */
21 /* Includes ------------------------------------------------------------------*/ 21 /* Includes ------------------------------------------------------------------*/
22 #include "uart.h" 22 #include "uart.h"
23 #include "externalInterface.h"
24 #include "data_exchange.h"
23 25
24 /* Private variables ---------------------------------------------------------*/ 26 /* Private variables ---------------------------------------------------------*/
25 27
26 UART_HandleTypeDef huart2; 28 #define CHUNK_SIZE (20u) /* the DMA will handle chunk size transfers */
27 29 #define CHUNKS_PER_BUFFER (3u)
28 30 UART_HandleTypeDef huart1;
31
32 DMA_HandleTypeDef hdma_usart1_rx;
33
34 uint8_t rxBuffer[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow fariations in buffer read time */
35 static uint8_t rxWriteIndex; /* Index of the data item which is analysed */
36 static uint8_t rxReadIndex; /* Index at which new data is stared */
37 static uint8_t lastCmdIndex; /* Index of last command which has not been completly received */
38 static uint8_t dmaActive; /* Indicator if DMA receiption needs to be started */
39
40 float LED_Level = 0.0; /* Normalized LED value which may be used as indication for the health status of the sensor */
41 float LED_ZeroOffset = 0.0;
42 float pCO2 = 0.0;
29 /* Exported functions --------------------------------------------------------*/ 43 /* Exported functions --------------------------------------------------------*/
30 44
31 void MX_USART2_UART_Init(void) 45 void MX_USART1_UART_Init(void)
32 { 46 {
33 /* pullup special */
34 GPIO_InitTypeDef GPIO_InitStructure;
35 __GPIOA_CLK_ENABLE();
36 GPIO_InitStructure.Pin = GPIO_PIN_2;
37 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
38 GPIO_InitStructure.Pull = GPIO_PULLUP;
39 GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
40 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
41
42 /* regular init */ 47 /* regular init */
43 huart2.Instance = USART2; 48
44 huart2.Init.BaudRate = 1200; 49 huart1.Instance = USART1;
45 huart2.Init.WordLength = UART_WORDLENGTH_8B; 50 huart1.Init.BaudRate = 9600;
46 huart2.Init.StopBits = UART_STOPBITS_1; 51 huart1.Init.WordLength = UART_WORDLENGTH_8B;
47 huart2.Init.Parity = UART_PARITY_NONE; 52 huart1.Init.StopBits = UART_STOPBITS_1;
48 huart2.Init.Mode = UART_MODE_TX_RX; 53 huart1.Init.Parity = UART_PARITY_NONE;
49 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; 54 huart1.Init.Mode = UART_MODE_TX_RX;
50 huart2.Init.OverSampling = UART_OVERSAMPLING_16; 55 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
51 HAL_UART_Init(&huart2); 56 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
52 } 57
53 58 HAL_UART_Init(&huart1);
54 59
55 uint8_t UART_ButtonAdjust(uint8_t *array) 60 rxReadIndex = 0;
56 { 61 lastCmdIndex = 0;
57 uint8_t answer[4]; 62 rxWriteIndex = 0;
58 63 dmaActive = 0;
59 HAL_UART_Transmit(&huart2,array,4,1000); 64 }
60 HAL_UART_Receive(&huart2,answer,4,2000); 65
61 if( (answer[0] == array[0]) 66 void MX_USART1_UART_DeInit(void)
62 &&(answer[1] == array[1]) 67 {
63 &&(answer[2] == array[2]) 68 HAL_DMA_DeInit(&hdma_usart1_rx);
64 &&(answer[3] == array[3])) 69 HAL_UART_DeInit(&huart1);
65 return 1; 70 }
66 else 71
67 return 0; 72 void MX_USART1_DMA_Init()
68 } 73 {
69 74 /* DMA controller clock enable */
70 void MX_USART2_UART_DeInit(void) 75 __DMA2_CLK_ENABLE();
71 { 76
72 HAL_UART_DeInit(&huart2); 77 /* Peripheral DMA init*/
73 } 78 hdma_usart1_rx.Instance = DMA2_Stream5;
79 hdma_usart1_rx.Init.Channel = DMA_CHANNEL_4;
80 hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; //DMA_MEMORY_TO_PERIPH;
81 hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
82 hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
83 hdma_usart1_rx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE;
84 hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
85 hdma_usart1_rx.Init.Mode = DMA_NORMAL;
86 hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW;
87 hdma_usart1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
88 HAL_DMA_Init(&hdma_usart1_rx);
89
90 __HAL_LINKDMA(&huart1,hdmarx,hdma_usart1_rx);
91
92 /* DMA interrupt init */
93 HAL_NVIC_SetPriority(DMA2_Stream5_IRQn, 0, 0);
94 HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn);
95 }
96
97 void ConvertByteToHexString(uint8_t byte, char* str)
98 {
99 uint8_t worker = 0;
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)
123 {
124 uint8_t localRX = rxReadIndex;
125 uint8_t dataType = 0;
126 uint32_t dataValue = 0;
127 static receiveState_t rxState = RX_Ready;
128 static uint32_t lastReceiveTick = 0;
129
130 while(localRX != rxWriteIndex)
131 {
132 lastReceiveTick = HAL_GetTick();
133 if(rxState == RX_Ready) /* identify data content */
134 {
135 switch(rxBuffer[localRX])
136 {
137 case 'l':
138 case 'D':
139 case 'Z':
140 dataType = rxBuffer[localRX];
141 rxState = RX_Data0;
142 dataValue = 0;
143 break;
144
145 default: /* unknown or corrupted => ignore */
146 break;
147 }
148 }
149 else if((rxState >= RX_Data0) && (rxState <= RX_Data4))
150 {
151 if((rxBuffer[localRX] >= '0') && (rxBuffer[localRX] <= '9'))
152 {
153 dataValue = dataValue * 10 + (rxBuffer[localRX] - '0');
154 rxState++;
155 }
156 }
157 if((rxBuffer[localRX] == ' ') || (rxBuffer[localRX] == '\n')) /* Abort data detection */
158 {
159 if(rxState == RX_DataComplete)
160 {
161 if(externalInterface_GetCO2State() == 0)
162 {
163 externalInterface_SetCO2State(EXT_INTERFACE_33V_ON);
164 }
165 switch(dataType)
166 {
167 case 'D': externalInterface_SetCO2SignalStrength(dataValue);
168 break;
169 case 'l': LED_ZeroOffset = dataValue;
170 break;
171 case 'Z': externalInterface_SetCO2Value(dataValue);
172 break;
173 default: break;
174 }
175 }
176 if(rxState != RX_Data0) /* reset state machine because message in wrong format */
177 {
178 rxState = RX_Ready;
179 }
180 }
181
182 localRX++;
183 rxReadIndex++;
184 if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
185 {
186 localRX = 0;
187 rxReadIndex = 0;
188 }
189 }
190
191 if(time_elapsed_ms(lastReceiveTick,HAL_GetTick()) > 2000) /* check for communication timeout */
192 {
193 externalInterface_SetCO2State(0);
194 }
195
196 if((dmaActive == 0) && (externalInterface_isEnabledPower33())) /* Should never happen in normal operation => restart in case of communication error */
197 {
198 if(HAL_OK == HAL_UART_Receive_DMA (&huart1, &rxBuffer[rxWriteIndex], CHUNK_SIZE))
199 {
200 dmaActive = 1;
201 }
202 }
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
358
359 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
360 {
361 if(huart == &huart1)
362 {
363 dmaActive = 0;
364 rxWriteIndex+=CHUNK_SIZE;
365 if(rxWriteIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
366 {
367 rxWriteIndex = 0;
368 }
369 if((rxWriteIndex / CHUNK_SIZE) != (rxReadIndex / CHUNK_SIZE)) /* start next transfer if we did not catch up with read index */
370 {
371 if(externalInterface_isEnabledPower33())
372 {
373 if(HAL_OK == HAL_UART_Receive_DMA (&huart1, &rxBuffer[rxWriteIndex], CHUNK_SIZE))
374 {
375 dmaActive = 1;
376 }
377 }
378 }
379 }
380 }
381
382
383
384
385
386
74 387
75 388
76 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ 389 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/