922
|
1 /**
|
|
2 ******************************************************************************
|
|
3 * @file uart_Internal.c
|
|
4 * @author heinrichs weikamp gmbh
|
|
5 * @version V0.0.1
|
|
6 * @date 03-November-2044
|
|
7 * @brief Control functions for devices connected to the internal UART
|
|
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"
|
|
23 #include "uart_Internal.h"
|
|
24 #include "uartProtocol_GNSS.h"
|
|
25 #include "GNSS.h"
|
|
26 #include "externalInterface.h"
|
|
27 #include "data_exchange.h"
|
|
28 #include <string.h> /* memset */
|
|
29
|
|
30
|
|
31 static uint8_t isEndIndication6(uint8_t index);
|
931
|
32 static uartGnssStatus_t gnssState = UART_GNSS_INIT;
|
|
33 static gnssRequest_s activeRequest = {0,0};
|
922
|
34
|
|
35 /* Private variables ---------------------------------------------------------*/
|
|
36
|
|
37
|
|
38 #define TX_BUF_SIZE (40u) /* max length for commands */
|
931
|
39 #define CHUNK_SIZE (50u) /* the DMA will handle chunk size transfers */
|
|
40 #define CHUNKS_PER_BUFFER (3u)
|
922
|
41
|
|
42 #define REQUEST_INT_SENSOR_MS (1500) /* Minimum time interval for cyclic sensor data requests per sensor (UART mux) */
|
|
43 #define COMMAND_TX_DELAY (30u) /* The time the sensor needs to recover from a invalid command request */
|
|
44 #define TIMEOUT_SENSOR_ANSWER (300) /* Time till a request is repeated if no answer was received */
|
|
45
|
|
46
|
|
47 static receiveStateGnss_t rxState = GNSSRX_READY;
|
|
48 static uint8_t GnssConnected = 0; /* Binary indicator if a sensor is connected or not */
|
|
49
|
|
50 static uint8_t writeIndex = 0;
|
|
51
|
931
|
52 static uint16_t dataToRead = 0;
|
922
|
53
|
|
54 DMA_HandleTypeDef hdma_usart6_rx, hdma_usart6_tx;
|
|
55
|
|
56 uint8_t tx6Buffer[CHUNK_SIZE]; /* tx uses less bytes */
|
|
57 uint8_t tx6BufferQue[TX_BUF_SIZE]; /* In MUX mode command may be send shortly after each other => allow q 1 entry que */
|
|
58 uint8_t tx6BufferQueLen;
|
|
59
|
|
60 uint8_t rxBufferUart6[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow variations in buffer read time */
|
|
61 uint8_t txBufferUart6[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow variations in buffer read time */
|
|
62
|
|
63 static uint8_t rx6WriteIndex; /* Index of the data item which is analysed */
|
|
64 static uint8_t rx6ReadIndex; /* Index at which new data is stared */
|
|
65
|
|
66 static uint8_t dmaRx6Active; /* Indicator if DMA reception needs to be started */
|
|
67 static uint8_t dmaTx6Active; /* Indicator if DMA reception needs to be started */
|
|
68
|
|
69
|
|
70 /* Exported functions --------------------------------------------------------*/
|
|
71
|
|
72 void UART_clearRx6Buffer(void)
|
|
73 {
|
|
74 uint16_t index = 0;
|
|
75 do
|
|
76 {
|
|
77 rxBufferUart6[index++] = BUFFER_NODATA_LOW;
|
|
78 rxBufferUart6[index++] = BUFFER_NODATA_HIGH;
|
|
79 } while (index < sizeof(rxBufferUart6));
|
|
80
|
|
81 rx6ReadIndex = 0;
|
|
82 rx6WriteIndex = 0;
|
|
83 }
|
|
84
|
|
85 void GNSS_IO_init() {
|
|
86
|
|
87 GPIO_InitTypeDef GPIO_InitStruct = { 0 };
|
|
88 /* Peripheral clock enable */
|
|
89 __HAL_RCC_USART6_CLK_ENABLE()
|
|
90 ;
|
|
91
|
|
92 __HAL_RCC_GPIOA_CLK_ENABLE()
|
|
93 ;
|
|
94 /**USART6 GPIO Configuration
|
|
95 PA11 ------> USART6_TX
|
|
96 PA12 ------> USART6_RX
|
|
97 */
|
|
98 GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12;
|
|
99 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
100 GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
101 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
|
|
102 GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
|
|
103 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
104
|
|
105 /* USART6 DMA Init */
|
|
106 /* USART6_RX Init */
|
923
|
107 hdma_usart6_rx.Instance = DMA2_Stream2;
|
922
|
108 hdma_usart6_rx.Init.Channel = DMA_CHANNEL_5;
|
|
109 hdma_usart6_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
|
110 hdma_usart6_rx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
111 hdma_usart6_rx.Init.MemInc = DMA_MINC_ENABLE;
|
|
112 hdma_usart6_rx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
113 hdma_usart6_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
114 hdma_usart6_rx.Init.Mode = DMA_NORMAL;
|
|
115 hdma_usart6_rx.Init.Priority = DMA_PRIORITY_LOW;
|
|
116 hdma_usart6_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
117 HAL_DMA_Init(&hdma_usart6_rx);
|
|
118
|
|
119 __HAL_LINKDMA(&huart6, hdmarx, hdma_usart6_rx);
|
|
120
|
|
121 /* USART6_TX Init */
|
|
122 hdma_usart6_tx.Instance = DMA2_Stream6;
|
|
123 hdma_usart6_tx.Init.Channel = DMA_CHANNEL_5;
|
|
124 hdma_usart6_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
|
125 hdma_usart6_tx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
126 hdma_usart6_tx.Init.MemInc = DMA_MINC_ENABLE;
|
|
127 hdma_usart6_tx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
128 hdma_usart6_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
129 hdma_usart6_tx.Init.Mode = DMA_NORMAL;
|
|
130 hdma_usart6_tx.Init.Priority = DMA_PRIORITY_LOW;
|
|
131 hdma_usart6_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
132 HAL_DMA_Init(&hdma_usart6_tx);
|
|
133
|
|
134 __HAL_LINKDMA(&huart6, hdmatx, hdma_usart6_tx);
|
|
135
|
|
136 /* USART6 interrupt Init */
|
|
137 HAL_NVIC_SetPriority(USART6_IRQn, 0, 0);
|
|
138 HAL_NVIC_EnableIRQ(USART6_IRQn);
|
|
139
|
|
140 MX_USART6_DMA_Init();
|
|
141
|
|
142 }
|
|
143
|
|
144 void MX_USART6_DMA_Init() {
|
|
145 /* DMA controller clock enable */
|
|
146 __HAL_RCC_DMA2_CLK_ENABLE();
|
|
147
|
|
148 /* DMA interrupt init */
|
|
149 /* DMA2_Stream2_IRQn interrupt configuration */
|
|
150 HAL_NVIC_SetPriority(DMA2_Stream2_IRQn, 0, 0);
|
|
151 HAL_NVIC_EnableIRQ(DMA2_Stream2_IRQn);
|
|
152 /* DMA2_Stream6_IRQn interrupt configuration */
|
|
153 HAL_NVIC_SetPriority(DMA2_Stream6_IRQn, 0, 0);
|
|
154 HAL_NVIC_EnableIRQ(DMA2_Stream6_IRQn);
|
|
155 }
|
|
156
|
|
157
|
|
158 void MX_USART6_UART_DeInit(void)
|
|
159 {
|
|
160 HAL_DMA_Abort(&hdma_usart6_rx);
|
|
161 HAL_DMA_DeInit(&hdma_usart6_rx);
|
|
162 HAL_DMA_Abort(&hdma_usart6_tx);
|
|
163 HAL_DMA_DeInit(&hdma_usart6_tx);
|
|
164 HAL_UART_DeInit(&huart6);
|
|
165 HAL_UART_DeInit(&huart6);
|
|
166 }
|
|
167
|
|
168 void MX_USART6_UART_Init(void) {
|
|
169 huart6.Instance = USART6;
|
|
170 huart6.Init.BaudRate = 9600;
|
|
171 huart6.Init.WordLength = UART_WORDLENGTH_8B;
|
|
172 huart6.Init.StopBits = UART_STOPBITS_1;
|
|
173 huart6.Init.Parity = UART_PARITY_NONE;
|
|
174 huart6.Init.Mode = UART_MODE_TX_RX;
|
|
175 huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
176 huart6.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
177 HAL_UART_Init(&huart6);
|
923
|
178
|
|
179 UART_clearRx6Buffer();
|
|
180 dmaRx6Active = 0;
|
|
181 dmaTx6Active = 0;
|
|
182 tx6BufferQueLen = 0;
|
922
|
183 }
|
|
184
|
|
185
|
|
186
|
|
187 void UART6_SendCmdUbx(const uint8_t *cmd, uint8_t len)
|
|
188 {
|
|
189 if(len < TX_BUF_SIZE) /* A longer string is an indication for a missing 0 termination */
|
|
190 {
|
|
191 if(dmaRx6Active == 0)
|
|
192 {
|
927
|
193 UART6_StartDMA_Receiption();
|
922
|
194 }
|
|
195 memcpy(tx6Buffer, cmd, len);
|
|
196 if(HAL_OK == HAL_UART_Transmit_DMA(&huart6,tx6Buffer,len))
|
|
197 {
|
|
198 dmaTx6Active = 1;
|
|
199 }
|
|
200 }
|
|
201 }
|
|
202
|
|
203 uint8_t isEndIndication6(uint8_t index)
|
|
204 {
|
|
205 uint8_t ret = 0;
|
|
206 if(index % 2)
|
|
207 {
|
|
208 if(rxBufferUart6[index] == BUFFER_NODATA_HIGH)
|
|
209 {
|
|
210 ret = 1;
|
|
211 }
|
|
212 }
|
|
213 else
|
|
214 {
|
|
215 if(rxBufferUart6[index] == BUFFER_NODATA_LOW)
|
|
216 {
|
|
217 ret = 1;
|
|
218 }
|
|
219 }
|
|
220
|
|
221 return ret;
|
|
222 }
|
|
223
|
|
224 void UART6_StartDMA_Receiption()
|
|
225 {
|
|
226 if(dmaRx6Active == 0)
|
|
227 {
|
|
228 if(((rx6WriteIndex / CHUNK_SIZE) != (rx6ReadIndex / CHUNK_SIZE)) || ((isEndIndication6(rx6WriteIndex)) && (isEndIndication6(rx6WriteIndex + 1)))) /* start next transfer if we did not catch up with read index */
|
|
229 {
|
|
230 if(HAL_OK == HAL_UART_Receive_DMA (&huart6, &rxBufferUart6[rx6WriteIndex], CHUNK_SIZE))
|
|
231 {
|
|
232 dmaRx6Active = 1;
|
|
233 }
|
|
234 }
|
|
235 }
|
|
236 }
|
|
237
|
|
238
|
|
239
|
|
240 void UART6_RxCpltCallback(UART_HandleTypeDef *huart)
|
|
241 {
|
|
242 if(huart == &huart6)
|
|
243 {
|
|
244 dmaRx6Active = 0;
|
|
245 rx6WriteIndex+=CHUNK_SIZE;
|
|
246 if(rx6WriteIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
|
|
247 {
|
|
248 rx6WriteIndex = 0;
|
|
249 }
|
|
250 UART6_StartDMA_Receiption();
|
|
251 }
|
|
252 }
|
|
253 void UART6_TxCpltCallback(UART_HandleTypeDef *huart)
|
|
254 {
|
|
255 if(huart == &huart6)
|
|
256 {
|
|
257 dmaTx6Active = 0;
|
|
258 UART6_WriteData();
|
|
259 if(tx6BufferQueLen)
|
|
260 {
|
|
261 memcpy(tx6Buffer, tx6BufferQue, tx6BufferQueLen);
|
|
262 HAL_UART_Transmit_DMA(&huart6,tx6Buffer,tx6BufferQueLen);
|
|
263 dmaTx6Active = 1;
|
|
264 tx6BufferQueLen = 0;
|
|
265 }
|
|
266 }
|
|
267 }
|
|
268
|
|
269 void UART6_ReadData()
|
|
270 {
|
|
271 uint8_t localRX = rx6ReadIndex;
|
|
272 uint8_t futureIndex = rx6ReadIndex + 1;
|
|
273 uint8_t moreData = 0;
|
|
274
|
|
275 if(futureIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
|
|
276 {
|
|
277 futureIndex = 0;
|
|
278 }
|
|
279
|
931
|
280 if(!isEndIndication6(futureIndex))
|
|
281 {
|
|
282 moreData = 1;
|
|
283 }
|
|
284
|
|
285 if((!isEndIndication6(localRX)) || (moreData))
|
|
286 do
|
922
|
287 {
|
|
288 while((!isEndIndication6(localRX)) || (moreData))
|
|
289 {
|
|
290 moreData = 0;
|
|
291 UART6_Gnss_ProcessData(rxBufferUart6[localRX]);
|
|
292
|
|
293 if(localRX % 2)
|
|
294 {
|
|
295 rxBufferUart6[localRX] = BUFFER_NODATA_HIGH;
|
|
296 }
|
|
297 else
|
|
298 {
|
|
299 rxBufferUart6[localRX] = BUFFER_NODATA_LOW;
|
|
300 }
|
|
301
|
|
302 localRX++;
|
|
303 rx6ReadIndex++;
|
|
304 if(rx6ReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
|
|
305 {
|
|
306 localRX = 0;
|
|
307 rx6ReadIndex = 0;
|
|
308 }
|
|
309 futureIndex++;
|
|
310 if(futureIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
|
|
311 {
|
|
312 futureIndex = 0;
|
|
313 }
|
|
314 }
|
|
315 if(!isEndIndication6(futureIndex))
|
|
316 {
|
|
317 moreData = 1;
|
|
318 }
|
|
319 } while(moreData);
|
|
320 }
|
|
321
|
|
322 void UART6_WriteData(void)
|
|
323 {
|
|
324 if(huart6.hdmatx->State == HAL_DMA_STATE_READY)
|
|
325 {
|
|
326 huart6.gState = HAL_UART_STATE_READY;
|
|
327 dmaTx6Active = 0;
|
|
328 }
|
|
329 if(huart6.hdmarx->State == HAL_DMA_STATE_READY)
|
|
330 {
|
|
331 huart6.RxState = HAL_UART_STATE_READY;
|
|
332 dmaRx6Active = 0;
|
|
333 }
|
|
334 }
|
|
335
|
|
336 void UART6_Gnss_SendCmd(uint8_t GnssCmd)
|
|
337 {
|
|
338 const uint8_t* pData;
|
|
339 uint8_t txLength = 0;
|
|
340
|
|
341 switch (GnssCmd)
|
|
342 {
|
|
343 case GNSSCMD_LOADCONF_0: pData = configUBX;
|
|
344 txLength = sizeof(configUBX) / sizeof(uint8_t);
|
|
345 break;
|
|
346 case GNSSCMD_LOADCONF_1: pData = setNMEA410;
|
|
347 txLength = sizeof(setNMEA410) / sizeof(uint8_t);
|
|
348 break;
|
|
349 case GNSSCMD_LOADCONF_2: pData = setGNSS;
|
|
350 txLength = sizeof(setGNSS) / sizeof(uint8_t);
|
|
351 break;
|
|
352 case GNSSCMD_GET_PVT_DATA: pData = getPVTData;
|
|
353 txLength = sizeof(getPVTData) / sizeof(uint8_t);
|
|
354 break;
|
|
355 case GNSSCMD_GET_NAV_DATA: pData = getNavigatorData;
|
|
356 txLength = sizeof(getNavigatorData) / sizeof(uint8_t);
|
|
357 break;
|
931
|
358 case GNSSCMD_GET_NAVSAT_DATA: pData = getNavSat;
|
|
359 txLength = sizeof(getNavSat) / sizeof(uint8_t);
|
|
360 break;
|
922
|
361 default:
|
|
362 break;
|
|
363 }
|
|
364 if(txLength != 0)
|
|
365 {
|
931
|
366 activeRequest.class = pData[2];
|
|
367 activeRequest.id = pData[3];
|
922
|
368 UART6_SendCmdUbx(pData, txLength);
|
|
369 }
|
|
370 }
|
|
371
|
|
372 void UART6_Gnss_Control(void)
|
|
373 {
|
|
374 static uint32_t warmupTick = 0;
|
931
|
375 static uint8_t dataToggle = 0;
|
922
|
376
|
|
377 switch (gnssState)
|
|
378 {
|
|
379 case UART_GNSS_INIT: gnssState = UART_GNSS_WARMUP;
|
|
380 warmupTick = HAL_GetTick();
|
|
381 UART_clearRxBuffer();
|
|
382 break;
|
|
383 case UART_GNSS_WARMUP: if(time_elapsed_ms(warmupTick,HAL_GetTick()) > 1000)
|
|
384 {
|
|
385 gnssState = UART_GNSS_LOADCONF_0;
|
|
386 }
|
|
387 break;
|
|
388 case UART_GNSS_LOADCONF_0: UART6_Gnss_SendCmd(GNSSCMD_LOADCONF_0);
|
|
389 gnssState = UART_GNSS_LOADCONF_1;
|
|
390 rxState = GNSSRX_DETECT_ACK_0;
|
|
391 break;
|
|
392 case UART_GNSS_LOADCONF_1: UART6_Gnss_SendCmd(GNSSCMD_LOADCONF_1);
|
|
393 gnssState = UART_GNSS_LOADCONF_2;
|
|
394 rxState = GNSSRX_DETECT_ACK_0;
|
|
395 break;
|
|
396 case UART_GNSS_LOADCONF_2: UART6_Gnss_SendCmd(GNSSCMD_LOADCONF_2);
|
|
397 gnssState = UART_GNSS_IDLE;
|
|
398 rxState = GNSSRX_DETECT_ACK_0;
|
|
399 break;
|
931
|
400 case UART_GNSS_IDLE: if(dataToggle)
|
|
401 {
|
|
402 UART6_Gnss_SendCmd(GNSSCMD_GET_PVT_DATA);
|
|
403 gnssState = UART_GNSS_GET_PVT;
|
|
404 rxState = GNSSRX_DETECT_HEADER_0;
|
|
405 dataToggle = 0;
|
|
406 }
|
|
407 else
|
|
408 {
|
|
409 UART6_Gnss_SendCmd(GNSSCMD_GET_NAVSAT_DATA);
|
|
410 gnssState = UART_GNSS_GET_SAT;
|
|
411 rxState = GNSSRX_DETECT_HEADER_0;
|
|
412 dataToggle = 1;
|
|
413 }
|
922
|
414 break;
|
|
415 default:
|
|
416 break;
|
|
417 }
|
|
418 }
|
|
419
|
|
420 void UART6_Gnss_ProcessData(uint8_t data)
|
|
421 {
|
931
|
422 static uint16_t rxLength = 0;
|
|
423 static uint8_t ck_A = 0;
|
|
424 static uint8_t ck_B = 0;
|
|
425 static uint8_t ck_A_Ref = 0;
|
|
426 static uint8_t ck_B_Ref = 0;
|
|
427
|
922
|
428 GNSS_Handle.uartWorkingBuffer[writeIndex++] = data;
|
931
|
429 if((rxState >= GNSSRX_DETECT_HEADER_2) && (rxState < GNSSRX_READ_CK_A))
|
|
430 {
|
|
431 ck_A += data;
|
|
432 ck_B += ck_A;
|
|
433 }
|
|
434
|
922
|
435 switch(rxState)
|
|
436 {
|
|
437 case GNSSRX_DETECT_ACK_0:
|
|
438 case GNSSRX_DETECT_HEADER_0: if(data == 0xB5)
|
|
439 {
|
|
440 writeIndex = 0;
|
931
|
441 memset(GNSS_Handle.uartWorkingBuffer,0xff, sizeof(GNSS_Handle.uartWorkingBuffer));
|
922
|
442 GNSS_Handle.uartWorkingBuffer[writeIndex++] = data;
|
|
443 rxState++;
|
931
|
444 ck_A = 0;
|
|
445 ck_B = 0;
|
922
|
446 }
|
|
447 break;
|
|
448 case GNSSRX_DETECT_ACK_1:
|
|
449 case GNSSRX_DETECT_HEADER_1: if(data == 0x62)
|
|
450 {
|
|
451 rxState++;
|
|
452 }
|
|
453 else
|
|
454 {
|
|
455 rxState = GNSSRX_DETECT_HEADER_0;
|
|
456 }
|
|
457 break;
|
|
458 case GNSSRX_DETECT_ACK_2: if(data == 0x05)
|
|
459 {
|
|
460 rxState++;
|
|
461 }
|
|
462 else
|
|
463 {
|
|
464 rxState = GNSSRX_DETECT_HEADER_0;
|
|
465 }
|
|
466 break;
|
|
467 case GNSSRX_DETECT_ACK_3: if((data == 0x01) || (data == 0x00))
|
|
468 {
|
|
469 GnssConnected = 1;
|
|
470 rxState = GNSSRX_READY;
|
|
471 }
|
|
472 else
|
|
473 {
|
|
474 rxState = GNSSRX_DETECT_HEADER_0;
|
|
475 }
|
|
476 break;
|
931
|
477 case GNSSRX_DETECT_HEADER_2: if(data == activeRequest.class)
|
922
|
478 {
|
|
479 rxState++;
|
|
480 }
|
|
481 else
|
|
482 {
|
|
483 rxState = GNSSRX_DETECT_HEADER_0;
|
|
484 }
|
|
485 break;
|
931
|
486 case GNSSRX_DETECT_HEADER_3: if(data == activeRequest.id)
|
|
487 {
|
|
488 rxState = GNSSRX_DETECT_LENGTH_0;
|
|
489 }
|
|
490 else
|
922
|
491 {
|
931
|
492 rxState = GNSSRX_DETECT_HEADER_0;
|
922
|
493 }
|
931
|
494 break;
|
|
495 case GNSSRX_DETECT_LENGTH_0: rxLength = GNSS_Handle.uartWorkingBuffer[4];
|
|
496 rxState = GNSSRX_DETECT_LENGTH_1;
|
|
497 break;
|
|
498 case GNSSRX_DETECT_LENGTH_1: rxLength += (GNSS_Handle.uartWorkingBuffer[5] << 8);
|
|
499 rxState = GNSSRX_READ_DATA;
|
|
500 dataToRead = rxLength;
|
|
501 break;
|
|
502 case GNSSRX_READ_DATA: if(dataToRead > 0)
|
922
|
503 {
|
|
504 dataToRead--;
|
|
505 }
|
931
|
506 if(dataToRead == 0)
|
922
|
507 {
|
931
|
508 rxState = GNSSRX_READ_CK_A;
|
922
|
509 }
|
|
510 break;
|
931
|
511 case GNSSRX_READ_CK_A: ck_A_Ref = data;
|
|
512 rxState++;
|
|
513 break;
|
|
514 case GNSSRX_READ_CK_B: ck_B_Ref = data;
|
|
515 if((ck_A_Ref == ck_A) && (ck_B_Ref == ck_B))
|
|
516 {
|
|
517 switch(gnssState)
|
|
518 {
|
|
519 case UART_GNSS_GET_PVT:GNSS_ParsePVTData(&GNSS_Handle);
|
|
520 break;
|
|
521 case UART_GNSS_GET_SAT: GNSS_ParseNavSatData(&GNSS_Handle);
|
|
522 break;
|
|
523 default:
|
|
524 break;
|
|
525 }
|
|
526 }
|
|
527 rxState = GNSSRX_DETECT_HEADER_0;
|
|
528 gnssState = UART_GNSS_IDLE;
|
|
529 break;
|
|
530
|
922
|
531 default: rxState = GNSSRX_READY;
|
|
532 break;
|
|
533 }
|
|
534 }
|
|
535
|
|
536
|
|
537 void UART6_HandleUART()
|
|
538 {
|
|
539 static uint8_t retryRequest = 0;
|
|
540 static uint32_t lastRequestTick = 0;
|
|
541 static uint32_t TriggerTick = 0;
|
|
542 static uint8_t timeToTrigger = 0;
|
|
543 uint32_t tick = HAL_GetTick();
|
|
544
|
|
545 if(gnssState != UART_GNSS_INIT)
|
|
546 {
|
|
547 UART6_ReadData();
|
|
548 UART6_WriteData();
|
|
549 }
|
|
550 if(gnssState == UART_GNSS_INIT)
|
|
551 {
|
|
552 lastRequestTick = tick;
|
|
553 TriggerTick = tick - 10; /* just to make sure control is triggered */
|
|
554 timeToTrigger = 1;
|
|
555 retryRequest = 0;
|
|
556 }
|
|
557 else if(((retryRequest == 0) /* timeout or error */
|
|
558 && (((time_elapsed_ms(lastRequestTick,tick) > (TIMEOUT_SENSOR_ANSWER)) && (gnssState != UART_GNSS_IDLE)) /* retry if no answer after half request interval */
|
|
559 || (gnssState == UART_GNSS_ERROR))))
|
|
560 {
|
|
561 /* The channel switch will cause the sensor to respond with an error message. */
|
|
562 /* The sensor needs ~30ms to recover before he is ready to receive the next command => transmission delay needed */
|
|
563
|
|
564 TriggerTick = tick;
|
|
565 timeToTrigger = COMMAND_TX_DELAY;
|
|
566 retryRequest = 1;
|
|
567 }
|
|
568
|
|
569 else if(time_elapsed_ms(lastRequestTick,tick) > 1000) /* switch sensor and / or trigger next request */
|
|
570 {
|
|
571 lastRequestTick = tick;
|
|
572 TriggerTick = tick;
|
|
573 retryRequest = 0;
|
|
574 timeToTrigger = 1;
|
|
575
|
931
|
576 if((gnssState == UART_GNSS_GET_SAT) || (gnssState == UART_GNSS_GET_PVT)) /* timeout */
|
922
|
577 {
|
|
578 gnssState = UART_GNSS_IDLE;
|
|
579 }
|
|
580 timeToTrigger = 1;
|
|
581 }
|
|
582 if((timeToTrigger != 0) && (time_elapsed_ms(TriggerTick,tick) > timeToTrigger))
|
|
583 {
|
|
584 timeToTrigger = 0;
|
|
585 UART6_Gnss_Control();
|
|
586 }
|
|
587
|
|
588 }
|
|
589
|
|
590
|
|
591 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/
|