-
Notifications
You must be signed in to change notification settings - Fork 0
/
quectel_gps.c
348 lines (224 loc) · 7.61 KB
/
quectel_gps.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "quectel_gps.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Clock.h>
#include "Board.h"
#include "quectel_define.h"
#define NEMA_DATA_RX_BUFFER_SIZE 255
#define NMEA_WRITE_READ_DATA_SIZE 255
static UART_Handle uart;
static PIN_Handle pinHandle;
static quectel_gps_data_t quectel_gps_data;
static quectel_gps_callback quectel_callback;
static uint8_t rxSize = 0;
static char rxBuffer[NMEA_WRITE_READ_DATA_SIZE] = { 0x00 };
static uint8_t nmeaDataRxSize = 0;
static char nemaDataRxBuffer[NEMA_DATA_RX_BUFFER_SIZE] = { 0x00 };
void set_quectel_gps_callback(quectel_gps_callback callback)
{
quectel_callback = callback;
}
void set_quectel_gps_gpio_instance(PIN_Handle pinHandleInstance)
{
pinHandle = pinHandleInstance;
PIN_setOutputValue(pinHandle, GPS_POWER_EN_GPIO, 0);
}
void set_quectel_gps_uart_instance(UART_Handle uartInstatnce)
{
uart = uartInstatnce;
}
static void wait_100_ms()
{
Task_sleep(100 * 1000 / Clock_tickPeriod);
}
static void wait_100_ms_timer(uint16_t wait)
{
for (uint16_t i = 0; i < wait; i++)
{
wait_100_ms();
}
}
static uint8_t quectel_uart_data_send(char *data, uint16_t size, uint16_t wait)
{
quectel_gps_data.ack = 0x00;
UART_write(uart, data, size);
for (uint16_t i = 0; i < wait; i++)
{
if (quectel_gps_data.ack)
{
return 0x01;
}
wait_100_ms();
}
return 0x00;
}
uint8_t set_quectel_gps_power_on(uint16_t wait)
{
memset(&quectel_gps_data, 0x00, sizeof(quectel_gps_data_t));
quectel_gps_data.HDOP = 100;
PIN_setOutputValue(pinHandle, GPS_POWER_EN_GPIO, 1);
for (uint16_t i = 0; i < wait; i++)
{
if (quectel_gps_data.gps_start_state)
{
return 0x01;
}
wait_100_ms();
}
return 0x00;
}
void set_quectel_gps_power_off()
{
quectel_gps_data.gps_start_state = 0x00;
PIN_setOutputValue(pinHandle, GPS_POWER_EN_GPIO, 0);
wait_100_ms_timer(10);
}
uint8_t set_quectel_gps_sbas_disable(uint16_t wait)
{
return quectel_uart_data_send(PMTK_SBAS_DISABLE, strlen(PMTK_SBAS_DISABLE),
wait);
}
uint8_t set_quectel_gps_nmea_off_gga_only(uint16_t wait)
{
return quectel_uart_data_send(PMTK_NEMA_OUTPUT_DISABLE_GGA_ONLY,
strlen(PMTK_NEMA_OUTPUT_DISABLE_GGA_ONLY),
wait);
}
static uint8_t nmea_crc_update(char *data, uint16_t size)
{
uint8_t crc = 0;
char convert_crc[4];
for (uint16_t i = 1; i < size - 3; i++)
{
crc ^= data[i];
}
sprintf(convert_crc, "%02X", crc);
if (strncmp(convert_crc, data + (size - 2), 2) == 0)
{
return 0x01;
}
return 0x00;
}
void quectel_gps_nmea_input(char data)
{
rxBuffer[rxSize++] = data;
if (rxSize > 2)
{
if ((rxBuffer[rxSize - 1] == '\n') && (rxBuffer[rxSize - 2] == '\r'))
{
if (nmea_crc_update(rxBuffer, rxSize - 2))
{
if (strncmp("$GPGGA", (char*) rxBuffer, 5) == 0)
{
uint8_t step = 0;
nmeaDataRxSize = 0;
memset(nemaDataRxBuffer, 0x00, sizeof(nemaDataRxBuffer));
for (uint16_t i = 0; i < rxSize; i++)
{
if (rxBuffer[i] == ',')
{
if (step == 1)
{
}
else if (step == 2)
{
//Lat
float lat = atof(nemaDataRxBuffer);
quectel_gps_data.latitude = lat * 100000;
}
else if (step == 3)
{
//NS
if (nemaDataRxBuffer[0] == 'S')
{
quectel_gps_data.latitude *= -1;
}
}
else if (step == 4)
{
//Lon
float lon = atof(nemaDataRxBuffer);
quectel_gps_data.longitude = lon * 100000;
}
else if (step == 5)
{
//EW
if (nemaDataRxBuffer[0] == 'W')
{
quectel_gps_data.longitude *= -1;
}
}
else if (step == 6)
{
//Fix State
quectel_gps_data.navigation_statue = atoi(
nemaDataRxBuffer);
}
else if (step == 7)
{
//Satellites count
quectel_gps_data.satellites_number = atoi(
nemaDataRxBuffer);
}
else if (step == 8)
{
//HDOP
//HDOP <= 5 Normal Fix Value
//HDOP > 5 Not Fix
float HDOP = atof(nemaDataRxBuffer);
HDOP *= 10;
quectel_gps_data.HDOP = (uint8_t) HDOP;
}
else if (step == 9)
{
//Altitue
quectel_gps_data.height = atof(
nemaDataRxBuffer);
}
else if (step >= 10)
{
if (quectel_callback)
{
quectel_gps_data.gps_start_state = 0x01;
quectel_callback(quectel_gps_data);
}
break;
}
nmeaDataRxSize = 0;
memset(nemaDataRxBuffer, 0x00,
sizeof(nemaDataRxBuffer));
step++;
}
else
{
nemaDataRxBuffer[nmeaDataRxSize++] = rxBuffer[i];
if (nmeaDataRxSize >= sizeof(nemaDataRxBuffer))
{
nmeaDataRxSize = 0;
}
}
}
}
else if (strncmp("$PMTK001,314,3", (char*) rxBuffer, 14) == 0)
{
quectel_gps_data.ack = 0x01;
if (quectel_callback)
{
quectel_callback(quectel_gps_data);
}
}
}
rxSize = 0;
}
}
if (rxSize >= sizeof(rxBuffer))
{
rxSize = 0;
}
}