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 __code char ao_gps_header[] = "GP";
26 __xdata uint8_t ao_gps_mutex;
27 static __data char ao_gps_char;
28 static __data uint8_t ao_gps_cksum;
29 static __data uint8_t ao_gps_error;
31 __pdata uint16_t ao_gps_tick;
32 __xdata struct ao_telemetry_location ao_gps_data;
33 __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
35 static __pdata uint16_t ao_gps_next_tick;
36 static __pdata struct ao_telemetry_location ao_gps_next;
37 static __pdata uint8_t ao_gps_date_flags;
38 static __pdata struct ao_telemetry_satellite ao_gps_tracking_next;
40 #define STQ_S 0xa0, 0xa1
41 #define STQ_E 0x0d, 0x0a
42 #define SKYTRAQ_MSG_2(id,a,b) \
43 STQ_S, 0, 3, id, a,b, (id^a^b), STQ_E
44 #define SKYTRAQ_MSG_3(id,a,b,c) \
45 STQ_S, 0, 4, id, a,b,c, (id^a^b^c), STQ_E
46 #define SKYTRAQ_MSG_8(id,a,b,c,d,e,f,g,h) \
47 STQ_S, 0, 9, id, a,b,c,d,e,f,g,h, (id^a^b^c^d^e^f^g^h), STQ_E
48 #define SKYTRAQ_MSG_14(id,a,b,c,d,e,f,g,h,i,j,k,l,m,n) \
49 STQ_S, 0,15, id, a,b,c,d,e,f,g,h,i,j,k,l,m,n, \
50 (id^a^b^c^d^e^f^g^h^i^j^k^l^m^n), STQ_E
52 static __code uint8_t ao_gps_config[] = {
53 SKYTRAQ_MSG_8(0x08, 1, 1, 1, 1, 1, 1, 1, 0), /* configure nmea */
61 /* attributes (0 = update to sram, 1 = update flash too) */
63 SKYTRAQ_MSG_2(0x3c, 0x00, 0x00), /* configure navigation mode */
64 /* 0 = car, 1 = pedestrian */
65 /* 0 = update to sram, 1 = update sram + flash */
75 c = ao_serial_getchar();
81 ao_gps_skip_field(void)
85 if (c == ',' || c == '*' || c == '\n')
95 if (c == ',' || c == '.' || c == '*')
99 __data static uint8_t ao_gps_num_width;
102 ao_gps_decimal(uint8_t max_width)
108 if (ao_gps_char == '-') {
113 ao_gps_num_width = 0;
114 while (ao_gps_num_width < max_width) {
115 uint8_t c = ao_gps_char;
116 if (c < (uint8_t) '0' || (uint8_t) '9' < c)
118 v = v * 10 + (uint8_t) (c - (uint8_t) '0');
134 ao_gps_num_width = 0;
135 while (ao_gps_num_width < 2) {
136 uint8_t c = ao_gps_char;
138 if ((uint8_t) '0' <= c && c <= (uint8_t) '9')
140 else if ((uint8_t) 'A' <= c && c <= (uint8_t) 'F')
142 else if ((uint8_t) 'a' <= c && c <= (uint8_t) 'f')
146 v = (v << 4) | (c + d);
154 ao_gps_parse_pos(uint8_t deg_width) __reentrant
156 static __pdata uint16_t d;
157 static __pdata uint8_t m;
158 static __pdata uint16_t f;
161 d = ao_gps_decimal(deg_width);
162 m = ao_gps_decimal(2);
165 f = ao_gps_decimal(4);
166 while (ao_gps_num_width < 4) {
175 return d * 10000000l + (m * 10000l + f) * 50 / 3;
179 ao_gps_parse_flag(char no_c, char yes_c)
183 if (ao_gps_char == yes_c)
185 else if (ao_gps_char == no_c)
197 /* Skip remaining fields */
200 if (c == '*' || c == '\n' || c == '\r')
206 uint8_t cksum = ao_gps_cksum ^ '*';
207 if (cksum != ao_gps_hex())
218 /* Now read the data into the gps data record
220 * $GPGGA,025149.000,4528.1723,N,12244.2480,W,1,05,2.0,103.5,M,-19.5,M,,0000*66
224 * 025149.000 time (02:51:49.000 GMT)
225 * 4528.1723,N Latitude 45°28.1723' N
226 * 12244.2480,W Longitude 122°44.2480' W
232 * 4 = Real Time Kinematic
234 * 6 = estimated (dead reckoning)
235 * 7 = Manual input mode
236 * 8 = Simulation mode
237 * 05 Number of satellites (5)
238 * 2.0 Horizontal dilution
239 * 103.5,M Altitude, 103.5M above msl
240 * -19.5,M Height of geoid above WGS84 ellipsoid
241 * ? time in seconds since last DGPS update
242 * 0000 DGPS station ID
246 ao_gps_next_tick = ao_time();
247 ao_gps_next.flags = AO_GPS_RUNNING | ao_gps_date_flags;
248 ao_gps_next.hour = ao_gps_decimal(2);
249 ao_gps_next.minute = ao_gps_decimal(2);
250 ao_gps_next.second = ao_gps_decimal(2);
251 ao_gps_skip_field(); /* skip seconds fraction */
253 ao_gps_next.latitude = ao_gps_parse_pos(2);
254 if (ao_gps_parse_flag('N', 'S'))
255 ao_gps_next.latitude = -ao_gps_next.latitude;
256 ao_gps_next.longitude = ao_gps_parse_pos(3);
257 if (ao_gps_parse_flag('E', 'W'))
258 ao_gps_next.longitude = -ao_gps_next.longitude;
260 i = ao_gps_decimal(0xff);
262 ao_gps_next.flags |= AO_GPS_VALID;
264 i = ao_gps_decimal(0xff) << AO_GPS_NUM_SAT_SHIFT;
265 if (i > AO_GPS_NUM_SAT_MASK)
266 i = AO_GPS_NUM_SAT_MASK;
267 ao_gps_next.flags |= i;
270 i = ao_gps_decimal(0xff);
273 if (ao_gps_char == '.')
274 i = (i + ((uint8_t) ao_gps_decimal(1) >> 1));
277 ao_gps_next.hdop = i;
280 ao_gps_next.altitude = ao_gps_decimal(0xff);
281 ao_gps_skip_field(); /* skip any fractional portion */
286 ao_mutex_get(&ao_gps_mutex);
287 ao_gps_tick = ao_gps_next_tick;
288 ao_xmemcpy(&ao_gps_data, &ao_gps_next, sizeof (ao_gps_data));
289 ao_mutex_put(&ao_gps_mutex);
290 ao_wakeup(&ao_gps_data);
300 /* Now read the data into the GPS tracking data record
302 * $GPGSV,3,1,12,05,54,069,45,12,44,061,44,21,07,184,46,22,78,289,47*72<CR><LF>
304 * Satellites in view data
306 * 3 Total number of GSV messages
307 * 1 Sequence number of current GSV message
308 * 12 Total sats in view (0-12)
316 c = ao_gps_decimal(1); /* total messages */
317 i = ao_gps_decimal(1); /* message sequence */
319 ao_gps_tracking_next.channels = 0;
321 done = (uint8_t) c == i;
323 ao_gps_skip_field(); /* sats in view */
324 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
325 i = ao_gps_tracking_next.channels;
326 c = ao_gps_decimal(2); /* SVID */
327 if (i < AO_MAX_GPS_TRACKING)
328 ao_gps_tracking_next.sats[i].svid = c;
330 ao_gps_skip_field(); /* elevation */
332 ao_gps_skip_field(); /* azimuth */
333 c = ao_gps_decimal(2); /* C/N0 */
334 if (i < AO_MAX_GPS_TRACKING) {
335 if ((ao_gps_tracking_next.sats[i].c_n_1 = c) != 0)
336 ao_gps_tracking_next.channels = i + 1;
343 ao_gps_tracking_next.channels = 0;
345 ao_mutex_get(&ao_gps_mutex);
346 ao_xmemcpy(&ao_gps_tracking_data, &ao_gps_tracking_next,
347 sizeof(ao_gps_tracking_data));
348 ao_mutex_put(&ao_gps_mutex);
349 ao_wakeup(&ao_gps_tracking_data);
358 /* Parse the RMC record to read out the current date */
360 /* $GPRMC,111636.932,A,2447.0949,N,12100.5223,E,000.0,000.0,030407,,,A*61
362 * Recommended Minimum Specific GNSS Data
364 * 111636.932 UTC time 11:16:36.932
365 * A Data Valid (V = receiver warning)
367 * N North/south indicator
368 * 12100.5223 Longitude
369 * E East/west indicator
370 * 000.0 Speed over ground
371 * 000.0 Course over ground
372 * 030407 UTC date (ddmmyy format)
375 * A = autonomous mode
376 * D = differential mode
377 * E = estimated (dead reckoning) mode
378 * M = manual input mode
383 for (i = 0; i < 8; i++) {
387 a = ao_gps_decimal(2);
388 c = ao_gps_decimal(2);
389 i = ao_gps_decimal(2);
394 ao_gps_next.year = i;
395 ao_gps_next.month = c;
397 ao_gps_date_flags = AO_GPS_DATE_VALID;
401 #define ao_skytraq_sendstruct(s) ao_skytraq_sendbytes((s), sizeof(s))
404 ao_skytraq_sendbytes(__code uint8_t *b, uint8_t l)
409 ao_delay(AO_MS_TO_TICKS(500));
410 ao_serial_putchar(c);
415 ao_gps_nmea_parse(void)
422 for (a = 0; a < AO_GPS_LEADER; a++) {
424 if (ao_gps_char != ao_gps_header[a])
436 if (ao_gps_char != ',')
439 if (a == (uint8_t) 'G' && b == (uint8_t) 'G' && c == (uint8_t) 'A') {
441 } else if (a == (uint8_t) 'G' && b == (uint8_t) 'S' && c == (uint8_t) 'V') {
443 } else if (a == (uint8_t) 'R' && b == (uint8_t) 'M' && c == (uint8_t) 'C') {
449 ao_gps(void) __reentrant
451 ao_serial_set_speed(AO_SERIAL_SPEED_9600);
453 /* give skytraq time to boot in case of cold start */
454 ao_delay(AO_MS_TO_TICKS(2000));
456 ao_skytraq_sendstruct(ao_gps_config);
459 /* Locate the begining of the next record */
460 if (ao_serial_getchar() == '$') {
467 __xdata struct ao_task ao_gps_task;
470 gps_dump(void) __reentrant
473 ao_mutex_get(&ao_gps_mutex);
474 printf ("Date: %02d/%02d/%02d\n", ao_gps_data.year, ao_gps_data.month, ao_gps_data.day);
475 printf ("Time: %02d:%02d:%02d\n", ao_gps_data.hour, ao_gps_data.minute, ao_gps_data.second);
476 printf ("Lat/Lon: %ld %ld\n", ao_gps_data.latitude, ao_gps_data.longitude);
477 printf ("Alt: %d\n", ao_gps_data.altitude);
478 printf ("Flags: 0x%x\n", ao_gps_data.flags);
479 printf ("Sats: %d", ao_gps_tracking_data.channels);
480 for (i = 0; i < ao_gps_tracking_data.channels; i++)
482 ao_gps_tracking_data.sats[i].svid,
483 ao_gps_tracking_data.sats[i].c_n_1);
485 ao_mutex_put(&ao_gps_mutex);
488 __code struct ao_cmds ao_gps_cmds[] = {
489 { gps_dump, "g\0Display GPS" },
496 ao_add_task(&ao_gps_task, ao_gps, "gps");
497 ao_cmd_register(&ao_gps_cmds[0]);