comparison Small_CPU/Src/uartProtocol_GNSS.c @ 932:effadaa3a1f7 Evo_2_23

Cleanup Gnss UART implementation: The first draft of the internal UART implementation was just a copy of the external UART handling. To avoid duplicated code and maintainance issue both UARTs (external/internal 6/1) share the same functions. To enable this a control structure has to be used as function input which defines the none shared resources like DMA control and rx/tx buffers
author Ideenmodellierer
date Sat, 07 Dec 2024 21:28:08 +0100
parents 7c996354b8ac
children 3029f0332f4f
comparison
equal deleted inserted replaced
931:5a9bc2e6112d 932:effadaa3a1f7
25 #include "uart.h" 25 #include "uart.h"
26 #include "GNSS.h" 26 #include "GNSS.h"
27 #include "configuration.h" 27 #include "configuration.h"
28 #include "externalInterface.h" 28 #include "externalInterface.h"
29 29
30 #if defined ENABLE_GNSS || defined ENABLE_GNSS_SUPPORT 30
31 #if defined ENABLE_GNSS || defined ENABLE_GNSS_SUPPORT || defined ENABLE_GPIO_V2
32
33 static uartGnssStatus_t gnssState = UART_GNSS_INIT;
34 static gnssRequest_s activeRequest = {0,0};
31 35
32 static receiveStateGnss_t rxState = GNSSRX_READY; 36 static receiveStateGnss_t rxState = GNSSRX_READY;
33 static uint8_t GnssConnected = 0; /* Binary indicator if a sensor is connected or not */ 37 static uint8_t GnssConnected = 0; /* Binary indicator if a sensor is connected or not */
34 38
35 static uint8_t writeIndex = 0; 39 static uint8_t writeIndex = 0;
58 worker = worker / 16; 62 worker = worker / 16;
59 } 63 }
60 } 64 }
61 65
62 66
63 void uartGnss_SendCmd(uint8_t GnssCmd) 67 uartGnssStatus_t uartGnss_GetState()
68 {
69 return gnssState;
70 }
71 void uartGnss_SetState(uartGnssStatus_t newState)
72 {
73 gnssState = newState;
74 }
75
76 void UART_Gnss_SendCmd(uint8_t GnssCmd)
64 { 77 {
65 const uint8_t* pData; 78 const uint8_t* pData;
66 uint8_t txLength = 0; 79 uint8_t txLength = 0;
67 80
68 switch (GnssCmd) 81 switch (GnssCmd)
80 txLength = sizeof(getPVTData) / sizeof(uint8_t); 93 txLength = sizeof(getPVTData) / sizeof(uint8_t);
81 break; 94 break;
82 case GNSSCMD_GET_NAV_DATA: pData = getNavigatorData; 95 case GNSSCMD_GET_NAV_DATA: pData = getNavigatorData;
83 txLength = sizeof(getNavigatorData) / sizeof(uint8_t); 96 txLength = sizeof(getNavigatorData) / sizeof(uint8_t);
84 break; 97 break;
85 98 case GNSSCMD_GET_NAVSAT_DATA: pData = getNavSat;
99 txLength = sizeof(getNavSat) / sizeof(uint8_t);
100 break;
86 default: 101 default:
87 break; 102 break;
88 } 103 }
89 if(txLength != 0) 104 if(txLength != 0)
90 { 105 {
106 activeRequest.class = pData[2];
107 activeRequest.id = pData[3];
91 UART_SendCmdUbx(pData, txLength); 108 UART_SendCmdUbx(pData, txLength);
92 } 109 }
93 } 110 }
94 111
95 void uartGnss_Control(void) 112 void uartGnss_Control(void)
96 { 113 {
97 static uint32_t warmupTick = 0; 114 static uint32_t warmupTick = 0;
98 115 static uint8_t dataToggle = 0;
99 uint8_t activeSensor = externalInterface_GetActiveUartSensor(); 116 uint8_t activeSensor = 0;
100 uartGnssStatus_t localComState = externalInterface_GetSensorState(activeSensor + EXT_INTERFACE_MUX_OFFSET); 117 sUartComCtrl* pUartCtrl = UART_GetGnssCtrl();
101 118 // uartGnssStatus_t localComState;
102 switch (localComState) 119
103 { 120 if(pUartCtrl == &Uart1Ctrl)
104 case UART_GNSS_INIT: localComState = UART_GNSS_WARMUP; 121 {
122 activeSensor = externalInterface_GetActiveUartSensor();
123 gnssState = externalInterface_GetSensorState(activeSensor + EXT_INTERFACE_MUX_OFFSET);
124 }
125
126 switch (gnssState)
127 {
128 case UART_GNSS_INIT: gnssState = UART_GNSS_WARMUP;
105 warmupTick = HAL_GetTick(); 129 warmupTick = HAL_GetTick();
106 UART_clearRxBuffer(); 130 UART_clearRxBuffer(pUartCtrl);
107 break; 131 break;
108 case UART_GNSS_WARMUP: if(time_elapsed_ms(warmupTick,HAL_GetTick()) > 1000) 132 case UART_GNSS_WARMUP: if(time_elapsed_ms(warmupTick,HAL_GetTick()) > 1000)
109 { 133 {
110 localComState = UART_GNSS_LOADCONF_0; 134 gnssState = UART_GNSS_LOADCONF_0;
111 } 135 }
112 break; 136 break;
113 case UART_GNSS_LOADCONF_0: uartGnss_SendCmd(GNSSCMD_LOADCONF_0); 137 case UART_GNSS_LOADCONF_0: UART_Gnss_SendCmd(GNSSCMD_LOADCONF_0);
114 localComState = UART_GNSS_LOADCONF_1;
115 rxState = GNSSRX_DETECT_ACK_0; 138 rxState = GNSSRX_DETECT_ACK_0;
116 break; 139 break;
117 case UART_GNSS_LOADCONF_1: uartGnss_SendCmd(GNSSCMD_LOADCONF_1); 140 case UART_GNSS_LOADCONF_1: UART_Gnss_SendCmd(GNSSCMD_LOADCONF_1);
118 localComState = UART_GNSS_LOADCONF_2;
119 rxState = GNSSRX_DETECT_ACK_0; 141 rxState = GNSSRX_DETECT_ACK_0;
120 break; 142 break;
121 case UART_GNSS_LOADCONF_2: uartGnss_SendCmd(GNSSCMD_LOADCONF_2); 143 case UART_GNSS_LOADCONF_2: UART_Gnss_SendCmd(GNSSCMD_LOADCONF_2);
122 localComState = UART_GNSS_IDLE;
123 rxState = GNSSRX_DETECT_ACK_0; 144 rxState = GNSSRX_DETECT_ACK_0;
124 break; 145 break;
125 case UART_GNSS_IDLE: uartGnss_SendCmd(GNSSCMD_GET_PVT_DATA); 146 case UART_GNSS_IDLE: if(dataToggle)
126 localComState = UART_GNSS_GET_PVT; 147 {
127 rxState = GNSSRX_DETECT_HEADER_0; 148 UART_Gnss_SendCmd(GNSSCMD_GET_PVT_DATA);
149 gnssState = UART_GNSS_GET_PVT;
150 rxState = GNSSRX_DETECT_HEADER_0;
151 dataToggle = 0;
152 }
153 else
154 {
155 UART_Gnss_SendCmd(GNSSCMD_GET_NAVSAT_DATA);
156 gnssState = UART_GNSS_GET_SAT;
157 rxState = GNSSRX_DETECT_HEADER_0;
158 dataToggle = 1;
159 }
128 break; 160 break;
129 default: 161 default:
130 break; 162 break;
131 } 163 }
132 164 if(pUartCtrl == &Uart1Ctrl)
133 externalInterface_SetSensorState(activeSensor + EXT_INTERFACE_MUX_OFFSET,localComState); 165 {
166 externalInterface_SetSensorState(activeSensor + EXT_INTERFACE_MUX_OFFSET,gnssState);
167 }
134 168
135 } 169 }
136 170
137 void uartGnss_ProcessData(uint8_t data) 171 void uartGnss_ProcessData(uint8_t data)
138 { 172 {
139 uint8_t activeSensor = externalInterface_GetActiveUartSensor(); 173 static uint16_t rxLength = 0;
174 static uint8_t ck_A = 0;
175 static uint8_t ck_B = 0;
176 static uint8_t ck_A_Ref = 0;
177 static uint8_t ck_B_Ref = 0;
178
140 GNSS_Handle.uartWorkingBuffer[writeIndex++] = data; 179 GNSS_Handle.uartWorkingBuffer[writeIndex++] = data;
180 if((rxState >= GNSSRX_DETECT_HEADER_2) && (rxState < GNSSRX_READ_CK_A))
181 {
182 ck_A += data;
183 ck_B += ck_A;
184 }
185
141 switch(rxState) 186 switch(rxState)
142 { 187 {
143 case GNSSRX_DETECT_ACK_0: 188 case GNSSRX_DETECT_ACK_0:
144 case GNSSRX_DETECT_HEADER_0: if(data == 0xB5) 189 case GNSSRX_DETECT_HEADER_0: if(data == 0xB5)
145 { 190 {
146 writeIndex = 0; 191 writeIndex = 0;
147 memset(GNSS_Handle.uartWorkingBuffer,0, sizeof(GNSS_Handle.uartWorkingBuffer)); 192 memset(GNSS_Handle.uartWorkingBuffer,0xff, sizeof(GNSS_Handle.uartWorkingBuffer));
148 GNSS_Handle.uartWorkingBuffer[writeIndex++] = data; 193 GNSS_Handle.uartWorkingBuffer[writeIndex++] = data;
149 rxState++; 194 rxState++;
195 ck_A = 0;
196 ck_B = 0;
150 } 197 }
151 break; 198 break;
152 case GNSSRX_DETECT_ACK_1: 199 case GNSSRX_DETECT_ACK_1:
153 case GNSSRX_DETECT_HEADER_1: if(data == 0x62) 200 case GNSSRX_DETECT_HEADER_1: if(data == 0x62)
154 { 201 {
166 else 213 else
167 { 214 {
168 rxState = GNSSRX_DETECT_HEADER_0; 215 rxState = GNSSRX_DETECT_HEADER_0;
169 } 216 }
170 break; 217 break;
171 case GNSSRX_DETECT_ACK_3: if((data == 0x01) || (data == 0x00)) 218 case GNSSRX_DETECT_ACK_3: if((data == 0x01))
172 { 219 {
220 if((gnssState >= UART_GNSS_LOADCONF_0) && (gnssState <= UART_GNSS_LOADCONF_2))
221 {
222 if(gnssState == UART_GNSS_LOADCONF_2)
223 {
224 gnssState = UART_GNSS_IDLE;
225 }
226 else
227 {
228 gnssState++;
229 }
230 }
173 GnssConnected = 1; 231 GnssConnected = 1;
174 rxState = GNSSRX_READY; 232 rxState = GNSSRX_READY;
175 } 233 }
176 else 234 else
177 { 235 {
178 rxState = GNSSRX_DETECT_HEADER_0; 236 rxState = GNSSRX_DETECT_HEADER_0;
179 } 237 }
180 break; 238 break;
181 case GNSSRX_DETECT_HEADER_2: if(data == 0x01) 239 case GNSSRX_DETECT_HEADER_2: if(data == activeRequest.class)
182 { 240 {
183 rxState++; 241 rxState++;
184 } 242 }
185 else 243 else
186 { 244 {
187 rxState = GNSSRX_DETECT_HEADER_0; 245 rxState = GNSSRX_DETECT_HEADER_0;
188 } 246 }
189 break; 247 break;
190 case GNSSRX_DETECT_HEADER_3: 248 case GNSSRX_DETECT_HEADER_3: if(data == activeRequest.id)
191 switch(data) 249 {
192 { 250 rxState = GNSSRX_DETECT_LENGTH_0;
193 case 0x21: rxState = GNSSRX_READ_NAV_DATA; 251 }
194 dataToRead = 20; 252 else
195 break; 253 {
196 case 0x07: rxState = GNSSRX_READ_PVT_DATA; 254 rxState = GNSSRX_DETECT_HEADER_0;
197 dataToRead = 92; 255 }
198 break; 256 break;
199 case 0x02: rxState = GNSSRX_READ_POSLLH_DATA; 257 case GNSSRX_DETECT_LENGTH_0: rxLength = GNSS_Handle.uartWorkingBuffer[4];
200 break; 258 rxState = GNSSRX_DETECT_LENGTH_1;
201 default: rxState = GNSSRX_DETECT_HEADER_0; 259 break;
202 break; 260 case GNSSRX_DETECT_LENGTH_1: rxLength += (GNSS_Handle.uartWorkingBuffer[5] << 8);
203 } 261 rxState = GNSSRX_READ_DATA;
204 break; 262 dataToRead = rxLength;
205 case GNSSRX_READ_NAV_DATA: 263 break;
206 case GNSSRX_READ_PVT_DATA: 264 case GNSSRX_READ_DATA: if(dataToRead > 0)
207 case GNSSRX_READ_POSLLH_DATA: if(dataToRead > 0)
208 { 265 {
209 dataToRead--; 266 dataToRead--;
210 } 267 }
211 else 268 if(dataToRead == 0)
212 { 269 {
213 switch(rxState) 270 rxState = GNSSRX_READ_CK_A;
214 { 271 }
215 case GNSSRX_READ_NAV_DATA: GNSS_ParseNavigatorData(&GNSS_Handle); 272 break;
216 break; 273 case GNSSRX_READ_CK_A: ck_A_Ref = data;
217 case GNSSRX_READ_PVT_DATA: GNSS_ParsePVTData(&GNSS_Handle); 274 rxState++;
218 break; 275 break;
219 case GNSSRX_READ_POSLLH_DATA: GNSS_ParsePOSLLHData(&GNSS_Handle); 276 case GNSSRX_READ_CK_B: ck_B_Ref = data;
220 break; 277 if((ck_A_Ref == ck_A) && (ck_B_Ref == ck_B))
221 default: rxState = GNSSRX_DETECT_HEADER_0; 278 {
222 break; 279 switch(gnssState)
223 } 280 {
224 rxState = GNSSRX_DETECT_HEADER_0; 281 case UART_GNSS_GET_PVT:GNSS_ParsePVTData(&GNSS_Handle);
225 externalInterface_SetSensorState(activeSensor + EXT_INTERFACE_MUX_OFFSET,UART_GNSS_IDLE); 282 break;
226 } 283 case UART_GNSS_GET_SAT: GNSS_ParseNavSatData(&GNSS_Handle);
227 break; 284 break;
285 default:
286 break;
287 }
288 }
289 rxState = GNSSRX_DETECT_HEADER_0;
290 gnssState = UART_GNSS_IDLE;
291 break;
292
228 default: rxState = GNSSRX_READY; 293 default: rxState = GNSSRX_READY;
229 break; 294 break;
230 } 295 }
231 } 296 }
232 297