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 __xdata uint8_t ao_gps_mutex;
23 static __data char ao_gps_char;
24 static __data uint8_t ao_gps_cksum;
25 static __data uint8_t ao_gps_error;
27 __pdata uint16_t ao_gps_tick;
28 __xdata struct ao_telemetry_location ao_gps_data;
29 __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
31 static __pdata uint16_t ao_gps_next_tick;
32 static __pdata struct ao_telemetry_location ao_gps_next;
33 static __pdata uint8_t ao_gps_date_flags;
34 static __pdata struct ao_telemetry_satellite ao_gps_tracking_next;
36 #define STQ_S 0xa0, 0xa1
37 #define STQ_E 0x0d, 0x0a
38 #define SKYTRAQ_MSG_2(id,a,b) \
39 STQ_S, 0, 3, id, a,b, (id^a^b), STQ_E
40 #define SKYTRAQ_MSG_3(id,a,b,c) \
41 STQ_S, 0, 4, id, a,b,c, (id^a^b^c), STQ_E
42 #define SKYTRAQ_MSG_8(id,a,b,c,d,e,f,g,h) \
43 STQ_S, 0, 9, id, a,b,c,d,e,f,g,h, (id^a^b^c^d^e^f^g^h), STQ_E
44 #define SKYTRAQ_MSG_14(id,a,b,c,d,e,f,g,h,i,j,k,l,m,n) \
45 STQ_S, 0,15, id, a,b,c,d,e,f,g,h,i,j,k,l,m,n, \
46 (id^a^b^c^d^e^f^g^h^i^j^k^l^m^n), STQ_E
48 static __code uint8_t ao_gps_config[] = {
49 SKYTRAQ_MSG_8(0x08, 1, 0, 1, 0, 1, 0, 0, 0), /* configure nmea */
57 /* attributes (0 = update to sram, 1 = update flash too) */
59 SKYTRAQ_MSG_2(0x3c, 0x00, 0x00), /* configure navigation mode */
60 /* 0 = car, 1 = pedestrian */
61 /* 0 = update to sram, 1 = update sram + flash */
71 c = ao_serial_getchar();
77 ao_gps_skip_field(void)
81 if (c == ',' || c == '*' || c == '\n')
91 if (c == ',' || c == '.' || c == '*')
95 __data static uint8_t ao_gps_num_width;
98 ao_gps_decimal(uint8_t max_width)
104 if (ao_gps_char == '-') {
109 ao_gps_num_width = 0;
110 while (ao_gps_num_width < max_width) {
111 uint8_t c = ao_gps_char;
112 if (c < (uint8_t) '0' || (uint8_t) '9' < c)
114 v = v * 10 + (uint8_t) (c - (uint8_t) '0');
130 ao_gps_num_width = 0;
131 while (ao_gps_num_width < 2) {
132 uint8_t c = ao_gps_char;
134 if ((uint8_t) '0' <= c && c <= (uint8_t) '9')
136 else if ((uint8_t) 'A' <= c && c <= (uint8_t) 'F')
138 else if ((uint8_t) 'a' <= c && c <= (uint8_t) 'f')
142 v = (v << 4) | (c + d);
150 ao_gps_parse_pos(uint8_t deg_width) __reentrant
152 static __pdata uint16_t d;
153 static __pdata uint8_t m;
154 static __pdata uint16_t f;
157 d = ao_gps_decimal(deg_width);
158 m = ao_gps_decimal(2);
161 f = ao_gps_decimal(4);
162 while (ao_gps_num_width < 4) {
171 return d * 10000000l + (m * 10000l + f) * 50 / 3;
175 ao_gps_parse_flag(char no_c, char yes_c)
179 if (ao_gps_char == yes_c)
181 else if (ao_gps_char == no_c)
193 /* Skip remaining fields */
196 if (c == '*' || c == '\n' || c == '\r')
202 uint8_t cksum = ao_gps_cksum ^ '*';
203 if (cksum != ao_gps_hex())
214 /* Now read the data into the gps data record
216 * $GPGGA,025149.000,4528.1723,N,12244.2480,W,1,05,2.0,103.5,M,-19.5,M,,0000*66
220 * 025149.000 time (02:51:49.000 GMT)
221 * 4528.1723,N Latitude 45°28.1723' N
222 * 12244.2480,W Longitude 122°44.2480' W
228 * 4 = Real Time Kinematic
230 * 6 = estimated (dead reckoning)
231 * 7 = Manual input mode
232 * 8 = Simulation mode
233 * 05 Number of satellites (5)
234 * 2.0 Horizontal dilution
235 * 103.5,M Altitude, 103.5M above msl
236 * -19.5,M Height of geoid above WGS84 ellipsoid
237 * ? time in seconds since last DGPS update
238 * 0000 DGPS station ID
242 ao_gps_next_tick = ao_time();
243 ao_gps_next.flags = AO_GPS_RUNNING | ao_gps_date_flags;
244 ao_gps_next.hour = ao_gps_decimal(2);
245 ao_gps_next.minute = ao_gps_decimal(2);
246 ao_gps_next.second = ao_gps_decimal(2);
247 ao_gps_skip_field(); /* skip seconds fraction */
249 ao_gps_next.latitude = ao_gps_parse_pos(2);
250 if (ao_gps_parse_flag('N', 'S'))
251 ao_gps_next.latitude = -ao_gps_next.latitude;
252 ao_gps_next.longitude = ao_gps_parse_pos(3);
253 if (ao_gps_parse_flag('E', 'W'))
254 ao_gps_next.longitude = -ao_gps_next.longitude;
256 i = ao_gps_decimal(0xff);
258 ao_gps_next.flags |= AO_GPS_VALID;
260 i = ao_gps_decimal(0xff) << AO_GPS_NUM_SAT_SHIFT;
261 if (i > AO_GPS_NUM_SAT_MASK)
262 i = AO_GPS_NUM_SAT_MASK;
263 ao_gps_next.flags |= i;
266 i = ao_gps_decimal(0xff);
269 if (ao_gps_char == '.')
270 i = (i + ((uint8_t) ao_gps_decimal(1) >> 1));
273 ao_gps_next.hdop = i;
276 ao_gps_next.altitude = ao_gps_decimal(0xff);
277 ao_gps_skip_field(); /* skip any fractional portion */
282 ao_mutex_get(&ao_gps_mutex);
283 ao_gps_tick = ao_gps_next_tick;
284 ao_xmemcpy(&ao_gps_data, PDATA_TO_XDATA(&ao_gps_next), sizeof (ao_gps_data));
285 ao_mutex_put(&ao_gps_mutex);
286 ao_wakeup(&ao_gps_data);
296 /* Now read the data into the GPS tracking data record
298 * $GPGSV,3,1,12,05,54,069,45,12,44,061,44,21,07,184,46,22,78,289,47*72<CR><LF>
300 * Satellites in view data
302 * 3 Total number of GSV messages
303 * 1 Sequence number of current GSV message
304 * 12 Total sats in view (0-12)
312 c = ao_gps_decimal(1); /* total messages */
313 i = ao_gps_decimal(1); /* message sequence */
315 ao_gps_tracking_next.channels = 0;
317 done = (uint8_t) c == i;
319 ao_gps_skip_field(); /* sats in view */
320 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
321 i = ao_gps_tracking_next.channels;
322 c = ao_gps_decimal(2); /* SVID */
323 if (i < AO_MAX_GPS_TRACKING)
324 ao_gps_tracking_next.sats[i].svid = c;
326 ao_gps_skip_field(); /* elevation */
328 ao_gps_skip_field(); /* azimuth */
329 c = ao_gps_decimal(2); /* C/N0 */
330 if (i < AO_MAX_GPS_TRACKING) {
331 if ((ao_gps_tracking_next.sats[i].c_n_1 = c) != 0)
332 ao_gps_tracking_next.channels = i + 1;
339 ao_gps_tracking_next.channels = 0;
341 ao_mutex_get(&ao_gps_mutex);
342 ao_xmemcpy(&ao_gps_tracking_data, PDATA_TO_XDATA(&ao_gps_tracking_next), sizeof(ao_gps_tracking_data));
343 ao_mutex_put(&ao_gps_mutex);
344 ao_wakeup(&ao_gps_tracking_data);
353 /* Parse the RMC record to read out the current date */
355 /* $GPRMC,111636.932,A,2447.0949,N,12100.5223,E,000.0,000.0,030407,,,A*61
357 * Recommended Minimum Specific GNSS Data
359 * 111636.932 UTC time 11:16:36.932
360 * A Data Valid (V = receiver warning)
362 * N North/south indicator
363 * 12100.5223 Longitude
364 * E East/west indicator
365 * 000.0 Speed over ground
366 * 000.0 Course over ground
367 * 030407 UTC date (ddmmyy format)
370 * A = autonomous mode
371 * D = differential mode
372 * E = estimated (dead reckoning) mode
373 * M = manual input mode
378 for (i = 0; i < 8; i++) {
382 a = ao_gps_decimal(2);
383 c = ao_gps_decimal(2);
384 i = ao_gps_decimal(2);
389 ao_gps_next.year = i;
390 ao_gps_next.month = c;
392 ao_gps_date_flags = AO_GPS_DATE_VALID;
396 #define ao_skytraq_sendstruct(s) ao_skytraq_sendbytes((s), sizeof(s))
399 ao_skytraq_sendbytes(__code uint8_t *b, uint8_t l)
404 ao_delay(AO_MS_TO_TICKS(500));
405 ao_serial_putchar(c);
410 ao_gps_nmea_parse(void)
418 if (ao_gps_char != 'G')
421 if (ao_gps_char != 'P')
432 if (ao_gps_char != ',')
435 if (a == (uint8_t) 'G' && b == (uint8_t) 'G' && c == (uint8_t) 'A') {
437 } else if (a == (uint8_t) 'G' && b == (uint8_t) 'S' && c == (uint8_t) 'V') {
439 } else if (a == (uint8_t) 'R' && b == (uint8_t) 'M' && c == (uint8_t) 'C') {
445 ao_gps(void) __reentrant
447 ao_serial_set_speed(AO_SERIAL_SPEED_9600);
449 /* give skytraq time to boot in case of cold start */
450 ao_delay(AO_MS_TO_TICKS(2000));
452 ao_skytraq_sendstruct(ao_gps_config);
455 /* Locate the begining of the next record */
456 if (ao_serial_getchar() == '$') {
462 __xdata struct ao_task ao_gps_task;
465 gps_dump(void) __reentrant
468 ao_mutex_get(&ao_gps_mutex);
469 printf ("Date: %02d/%02d/%02d\n", ao_gps_data.year, ao_gps_data.month, ao_gps_data.day);
470 printf ("Time: %02d:%02d:%02d\n", ao_gps_data.hour, ao_gps_data.minute, ao_gps_data.second);
471 printf ("Lat/Lon: %ld %ld\n", ao_gps_data.latitude, ao_gps_data.longitude);
472 printf ("Alt: %d\n", ao_gps_data.altitude);
473 printf ("Flags: 0x%x\n", ao_gps_data.flags);
474 printf ("Sats: %d", ao_gps_tracking_data.channels);
475 for (i = 0; i < ao_gps_tracking_data.channels; i++)
477 ao_gps_tracking_data.sats[i].svid,
478 ao_gps_tracking_data.sats[i].c_n_1);
480 ao_mutex_put(&ao_gps_mutex);
483 __code struct ao_cmds ao_gps_cmds[] = {
484 { gps_dump, "g\0Display GPS" },
491 ao_add_task(&ao_gps_task, ao_gps, "gps");
492 ao_cmd_register(&ao_gps_cmds[0]);