2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 #define AO_GPS_LEADER 2
24 static const char ao_gps_header[] = "GP";
26 __xdata uint8_t ao_gps_mutex;
27 static __xdata char ao_gps_char;
28 static __xdata uint8_t ao_gps_cksum;
29 static __xdata uint8_t ao_gps_error;
31 __xdata uint16_t ao_gps_tick;
32 __xdata struct ao_gps_data ao_gps_data;
33 __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
35 static __xdata uint16_t ao_gps_next_tick;
36 static __xdata struct ao_gps_data ao_gps_next;
37 static __xdata uint8_t ao_gps_date_flags;
38 static __xdata struct ao_gps_tracking_data ao_gps_tracking_next;
40 static const char ao_gps_config[] = {
41 0xa0, 0xa1, 0x00, 0x09, /* length 9 bytes */
42 0x08, /* configure nmea */
50 0, /* attributes (0 = update to sram, 1 = update flash too) */
53 0xa0, 0xa1, 0x00, 0x03, /* length: 3 bytes */
54 0x3c, /* configure navigation mode */
55 0x00, /* 0 = car, 1 = pedestrian */
56 0x00, /* 0 = update to sram, 1 = update sram + flash */
66 ao_gps_char = ao_serial_getchar();
67 ao_gps_cksum ^= ao_gps_char;
73 while (ao_gps_char >= '0')
78 ao_gps_skip_field(void)
80 while (ao_gps_char != ',' && ao_gps_char != '*' && ao_gps_char != '\n')
87 if (ao_gps_char == ',' || ao_gps_char == '.' || ao_gps_char == '*')
91 __xdata static uint8_t ao_gps_num_width;
94 ao_gps_decimal(uint8_t max_width)
97 __xdata uint8_t neg = 0;
100 if (ao_gps_char == '-') {
105 ao_gps_num_width = 0;
106 while (ao_gps_num_width < max_width) {
107 if (ao_gps_char < '0' || '9' < ao_gps_char)
109 v = v * (int16_t) 10 + ao_gps_char - '0';
119 ao_gps_hex(uint8_t max_width)
125 ao_gps_num_width = 0;
126 while (ao_gps_num_width < max_width) {
127 if ('0' <= ao_gps_char && ao_gps_char <= '9')
128 d = ao_gps_char - '0';
129 else if ('A' <= ao_gps_char && ao_gps_char <= 'F')
130 d = ao_gps_char - 'A' + 10;
131 else if ('a' <= ao_gps_char && ao_gps_char <= 'f')
132 d = ao_gps_char - 'a' + 10;
143 ao_gps_parse_pos(uint8_t deg_width) __reentrant
149 d = ao_gps_decimal(deg_width);
150 m = ao_gps_decimal(2);
151 if (ao_gps_char == '.') {
152 f = ao_gps_decimal(4);
153 while (ao_gps_num_width < 4) {
159 if (ao_gps_char != ',')
169 ao_gps_parse_flag(char no_c, char yes_c) __reentrant
173 if (ao_gps_char == yes_c)
175 else if (ao_gps_char == no_c)
188 /* Now read the data into the gps data record
190 * $GPGGA,025149.000,4528.1723,N,12244.2480,W,1,05,2.0,103.5,M,-19.5,M,,0000*66
194 * 025149.000 time (02:51:49.000 GMT)
195 * 4528.1723,N Latitude 45°28.1723' N
196 * 12244.2480,W Longitude 122°44.2480' W
202 * 4 = Real Time Kinematic
204 * 6 = estimated (dead reckoning)
205 * 7 = Manual input mode
206 * 8 = Simulation mode
207 * 05 Number of satellites (5)
208 * 2.0 Horizontal dilution
209 * 103.5,M Altitude, 103.5M above msl
210 * -19.5,M Height of geoid above WGS84 ellipsoid
211 * ? time in seconds since last DGPS update
212 * 0000 DGPS station ID
216 ao_gps_next_tick = ao_time();
217 ao_gps_next.flags = AO_GPS_RUNNING | ao_gps_date_flags;
218 ao_gps_next.hour = ao_gps_decimal(2);
219 ao_gps_next.minute = ao_gps_decimal(2);
220 ao_gps_next.second = ao_gps_decimal(2);
221 ao_gps_skip_field(); /* skip seconds fraction */
223 ao_gps_next.latitude = ao_gps_parse_pos(2);
224 if (ao_gps_parse_flag('N', 'S'))
225 ao_gps_next.latitude = -ao_gps_next.latitude;
226 ao_gps_next.longitude = ao_gps_parse_pos(3);
227 if (ao_gps_parse_flag('E', 'W'))
228 ao_gps_next.longitude = -ao_gps_next.longitude;
230 i = ao_gps_decimal(0xff);
232 ao_gps_next.flags |= AO_GPS_VALID;
234 i = ao_gps_decimal(0xff) << AO_GPS_NUM_SAT_SHIFT;
235 if (i > AO_GPS_NUM_SAT_MASK)
236 i = AO_GPS_NUM_SAT_MASK;
237 ao_gps_next.flags |= i;
240 ao_gps_next.hdop = ao_gps_decimal(0xff);
241 if (ao_gps_next.hdop <= 50) {
242 ao_gps_next.hdop = (uint8_t) 5 * ao_gps_next.hdop;
243 if (ao_gps_char == '.')
244 ao_gps_next.hdop = (ao_gps_next.hdop +
245 ((uint8_t) ao_gps_decimal(1) >> 1));
247 ao_gps_next.hdop = 255;
250 ao_gps_next.altitude = ao_gps_decimal(0xff);
251 ao_gps_skip_field(); /* skip any fractional portion */
253 /* Skip remaining fields */
254 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
258 if (ao_gps_char == '*') {
259 uint8_t cksum = ao_gps_cksum ^ '*';
260 if (cksum != ao_gps_hex(2))
265 ao_mutex_get(&ao_gps_mutex);
266 ao_gps_tick = ao_gps_next_tick;
267 memcpy(&ao_gps_data, &ao_gps_next, sizeof (struct ao_gps_data));
268 ao_mutex_put(&ao_gps_mutex);
269 ao_wakeup(&ao_gps_data);
279 /* Now read the data into the GPS tracking data record
281 * $GPGSV,3,1,12,05,54,069,45,12,44,061,44,21,07,184,46,22,78,289,47*72<CR><LF>
283 * Satellites in view data
285 * 3 Total number of GSV messages
286 * 1 Sequence number of current GSV message
287 * 12 Total sats in view (0-12)
295 c = ao_gps_decimal(1); /* total messages */
296 i = ao_gps_decimal(1); /* message sequence */
298 ao_gps_tracking_next.channels = 0;
300 done = (uint8_t) c == i;
302 ao_gps_skip_field(); /* sats in view */
303 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
304 i = ao_gps_tracking_next.channels;
305 c = ao_gps_decimal(2); /* SVID */
306 if (i < AO_MAX_GPS_TRACKING)
307 ao_gps_tracking_next.sats[i].svid = c;
309 ao_gps_skip_field(); /* elevation */
311 ao_gps_skip_field(); /* azimuth */
312 c = ao_gps_decimal(2); /* C/N0 */
313 if (i < AO_MAX_GPS_TRACKING) {
314 if (!(ao_gps_tracking_next.sats[i].c_n_1 = c))
315 ao_gps_tracking_next.sats[i].svid = 0;
316 ao_gps_tracking_next.channels = i + 1;
319 if (ao_gps_char == '*') {
320 uint8_t cksum = ao_gps_cksum ^ '*';
321 if (cksum != ao_gps_hex(2))
327 ao_gps_tracking_next.channels = 0;
329 ao_mutex_get(&ao_gps_mutex);
330 memcpy(&ao_gps_tracking_data, &ao_gps_tracking_next,
331 sizeof(ao_gps_tracking_data));
332 ao_mutex_put(&ao_gps_mutex);
333 ao_wakeup(&ao_gps_tracking_data);
342 /* Parse the RMC record to read out the current date */
344 /* $GPRMC,111636.932,A,2447.0949,N,12100.5223,E,000.0,000.0,030407,,,A*61
346 * Recommended Minimum Specific GNSS Data
348 * 111636.932 UTC time 11:16:36.932
349 * A Data Valid (V = receiver warning)
351 * N North/south indicator
352 * 12100.5223 Longitude
353 * E East/west indicator
354 * 000.0 Speed over ground
355 * 000.0 Course over ground
356 * 030407 UTC date (ddmmyy format)
359 * A = autonomous mode
360 * D = differential mode
361 * E = estimated (dead reckoning) mode
362 * M = manual input mode
367 for (i = 0; i < 8; i++) {
371 a = ao_gps_decimal(2);
372 c = ao_gps_decimal(2);
373 i = ao_gps_decimal(2);
374 /* Skip remaining fields */
375 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
379 if (ao_gps_char == '*') {
380 uint8_t cksum = ao_gps_cksum ^ '*';
381 if (cksum != ao_gps_hex(2))
386 ao_gps_next.year = i;
387 ao_gps_next.month = c;
389 ao_gps_date_flags = AO_GPS_DATE_VALID;
394 ao_gps(void) __reentrant
399 ao_serial_set_speed(AO_SERIAL_SPEED_9600);
400 for (i = 0; i < sizeof (ao_gps_config); i++)
401 ao_serial_putchar(ao_gps_config[i]);
403 /* Locate the begining of the next record */
405 c = ao_serial_getchar();
413 /* Skip anything other than GP */
414 for (i = 0; i < AO_GPS_LEADER; i++) {
416 if (ao_gps_char != ao_gps_header[i])
419 if (i != AO_GPS_LEADER)
422 /* pull the record identifier characters off the link */
430 if (ao_gps_char != ',')
433 if (a == (uint8_t) 'G' && c == (uint8_t) 'G' && i == (uint8_t) 'A') {
435 } else if (a == (uint8_t) 'G' && c == (uint8_t) 'S' && i == (uint8_t) 'V') {
437 } else if (a == (uint8_t) 'R' && c == (uint8_t) 'M' && i == (uint8_t) 'C') {
443 __xdata struct ao_task ao_gps_task;
446 gps_dump(void) __reentrant
448 ao_mutex_get(&ao_gps_mutex);
449 printf ("Date: %02d/%02d/%02d\n", ao_gps_data.year, ao_gps_data.month, ao_gps_data.day);
450 printf ("Time: %02d:%02d:%02d\n", ao_gps_data.hour, ao_gps_data.minute, ao_gps_data.second);
451 printf ("Lat/Lon: %ld %ld\n", ao_gps_data.latitude, ao_gps_data.longitude);
452 printf ("Alt: %d\n", ao_gps_data.altitude);
453 printf ("Flags: 0x%x\n", ao_gps_data.flags);
454 ao_mutex_put(&ao_gps_mutex);
457 __code struct ao_cmds ao_gps_cmds[] = {
458 { 'g', gps_dump, "g Display current GPS values" },
459 { 0, gps_dump, NULL },
465 ao_add_task(&ao_gps_task, ao_gps, "gps");
466 ao_cmd_register(&ao_gps_cmds[0]);