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"
|
662
|
23 #include "externalInterface.h"
|
|
24 #include "data_exchange.h"
|
38
|
25
|
|
26 /* Private variables ---------------------------------------------------------*/
|
|
27
|
662
|
28 #define CHUNK_SIZE (20u) /* the DMA will handle chunk size transfers */
|
|
29 #define CHUNKS_PER_BUFFER (3u)
|
|
30 UART_HandleTypeDef huart1;
|
|
31
|
|
32 DMA_HandleTypeDef hdma_usart1_rx;
|
38
|
33
|
662
|
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 */
|
38
|
39
|
662
|
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;
|
38
|
43 /* Exported functions --------------------------------------------------------*/
|
|
44
|
662
|
45 void MX_USART1_UART_Init(void)
|
38
|
46 {
|
662
|
47 /* regular init */
|
|
48
|
|
49 huart1.Instance = USART1;
|
|
50 huart1.Init.BaudRate = 9600;
|
|
51 huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
|
52 huart1.Init.StopBits = UART_STOPBITS_1;
|
|
53 huart1.Init.Parity = UART_PARITY_NONE;
|
|
54 huart1.Init.Mode = UART_MODE_TX_RX;
|
|
55 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
56 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
57
|
|
58 HAL_UART_Init(&huart1);
|
|
59
|
|
60 rxReadIndex = 0;
|
|
61 lastCmdIndex = 0;
|
|
62 rxWriteIndex = 0;
|
|
63 dmaActive = 0;
|
|
64 }
|
38
|
65
|
662
|
66 void MX_USART1_UART_DeInit(void)
|
|
67 {
|
|
68 HAL_DMA_DeInit(&hdma_usart1_rx);
|
|
69 HAL_UART_DeInit(&huart1);
|
|
70 }
|
|
71
|
|
72 void MX_USART1_DMA_Init()
|
|
73 {
|
|
74 /* DMA controller clock enable */
|
|
75 __DMA2_CLK_ENABLE();
|
|
76
|
|
77 /* Peripheral DMA init*/
|
|
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);
|
38
|
95 }
|
|
96
|
690
|
97 void ConvertByteToHexString(uint8_t byte, char* str)
|
|
98 {
|
|
99 uint8_t worker = 0;
|
|
100 uint8_t digit = 0;
|
|
101 uint8_t digitCnt = 1;
|
38
|
102
|
690
|
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 }
|
662
|
119
|
690
|
120
|
|
121 #ifdef ENABLE_CO2_SUPPORT
|
|
122 void HandleUARTCO2Data(void)
|
38
|
123 {
|
662
|
124 uint8_t localRX = rxReadIndex;
|
|
125 uint8_t dataType = 0;
|
690
|
126 uint32_t dataValue = 0;
|
662
|
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 }
|
38
|
203 }
|
690
|
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
|
38
|
358
|
662
|
359 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
38
|
360 {
|
662
|
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 }
|
38
|
380 }
|
|
381
|
|
382
|
662
|
383
|
|
384
|
|
385
|
|
386
|
|
387
|
|
388
|
38
|
389 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/
|