-
Notifications
You must be signed in to change notification settings - Fork 1
/
quectel_gps.c
317 lines (177 loc) · 5.31 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
#include "quectel_gps.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "quectel_define.h"
#include "nrf_gpio.h"
#include "app_uart.h"
#include "nrf_delay.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#define NEMA_DATA_RX_BUFFER_SIZE 255
#define NMEA_WRITE_READ_DATA_SIZE 255
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 };
static uint32_t POWER_GPIO_PIN = 0;
void set_quectel_gps_callback(quectel_gps_callback callback) {
quectel_callback = callback;
}
void set_quectel_gps_gpio_instance(uint32_t POWER_PIN) {
POWER_GPIO_PIN = POWER_PIN;
}
static void wait_100_ms() {
quectel_gps_timer = 0x00;
while (!quectel_gps_timer) {
if (NRF_LOG_PROCESS() == false) {
nrf_pwr_mgmt_run();
}
}
}
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;
for (uint16_t i = 0; i < size; i++) {
while (app_uart_put(data[i]) != NRF_SUCCESS) {
}
}
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));
nrf_gpio_pin_set(POWER_GPIO_PIN);
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;
nrf_gpio_pin_clear(POWER_GPIO_PIN);
wait_100_ms_timer(10);
}
uint8_t set_quectel_gps_sbas_disable(uint16_t wait) {
return quectel_uart_data_send(PMTK_SBAS_DISABLE, sizeof(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,
sizeof(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 == 2) {
float lat = atof(nemaDataRxBuffer);
quectel_gps_data.latitude = lat * 100000;
} else if (step == 3) {
if (nemaDataRxBuffer[0] == 'S') {
quectel_gps_data.latitude *= -1;
}
NRF_LOG_INFO("Quectel_GPS LAT %d",
quectel_gps_data.latitude);
} else if (step == 4) {
float lon = atof(nemaDataRxBuffer);
quectel_gps_data.longitude = lon * 100000;
} else if (step == 5) {
if (nemaDataRxBuffer[0] == 'W') {
quectel_gps_data.longitude *= -1;
}
NRF_LOG_INFO("Quectel_GPS LON %d",
quectel_gps_data.longitude);
} else if (step == 6) {
quectel_gps_data.navigation_statue = atoi(
nemaDataRxBuffer);
NRF_LOG_INFO("Quectel_GPS Navigaton state %d",
quectel_gps_data.navigation_statue);
} else if (step == 7) {
quectel_gps_data.satellites_number = atoi(
nemaDataRxBuffer);
NRF_LOG_INFO("Quectel_GPS Navigaton state %d",
quectel_gps_data.navigation_statue);
} else if (step == 8) {
float HDOP = atof(nemaDataRxBuffer);
HDOP *= 10;
quectel_gps_data.HDOP = (uint8_t) HDOP;
NRF_LOG_INFO("Quectel_GPS HDOP %d",
quectel_gps_data.HDOP);
} else if (step == 9) {
quectel_gps_data.height = atof(
nemaDataRxBuffer);
NRF_LOG_INFO("Quectel_GPS Height %d",
quectel_gps_data.height);
} else if (step >= 10) {
if (quectel_callback) {
NRF_LOG_INFO("Quectel_GPS Get %s", rxBuffer);
NRF_LOG_FLUSH();
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;
}
}