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 3
24 static const char ao_gps_header[] = "GPG";
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 struct ao_gps_data ao_gps_data;
32 __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
34 static __xdata struct ao_gps_data ao_gps_next;
35 static __xdata struct ao_gps_tracking_data ao_gps_tracking_next;
37 static const char ao_gps_config[] = {
38 0xa0, 0xa1, 0x00, 0x09, /* length 9 bytes */
39 0x08, /* configure nmea */
47 0, /* attributes (0 = update to sram, 1 = update flash too) */
50 0xa0, 0xa1, 0x00, 0x03, /* length: 3 bytes */
51 0x3c, /* configure navigation mode */
52 0x00, /* 0 = car, 1 = pedestrian */
53 0x00, /* 0 = update to sram, 1 = update sram + flash */
63 ao_gps_char = ao_serial_getchar();
64 ao_gps_cksum ^= ao_gps_char;
70 while (ao_gps_char >= '0')
75 ao_gps_skip_field(void)
77 while (ao_gps_char != ',' && ao_gps_char != '*' && ao_gps_char != '\n')
84 if (ao_gps_char == ',' || ao_gps_char == '.' || ao_gps_char == '*')
88 __xdata static uint8_t ao_gps_num_width;
91 ao_gps_decimal(uint8_t max_width)
94 __xdata uint8_t neg = 0;
97 if (ao_gps_char == '-') {
102 ao_gps_num_width = 0;
103 while (ao_gps_num_width < max_width) {
104 if (ao_gps_char < '0' || '9' < ao_gps_char)
106 v = v * (int16_t) 10 + ao_gps_char - '0';
116 ao_gps_hex(uint8_t max_width)
122 ao_gps_num_width = 0;
123 while (ao_gps_num_width < max_width) {
124 if ('0' <= ao_gps_char && ao_gps_char <= '9')
125 d = ao_gps_char - '0';
126 else if ('A' <= ao_gps_char && ao_gps_char <= 'F')
127 d = ao_gps_char - 'A' + 10;
128 else if ('a' <= ao_gps_char && ao_gps_char <= 'f')
129 d = ao_gps_char - 'a' + 10;
140 ao_gps_parse_pos(uint8_t deg_width) __reentrant
146 d = ao_gps_decimal(deg_width);
147 m = ao_gps_decimal(2);
148 if (ao_gps_char == '.') {
149 f = ao_gps_decimal(4);
150 while (ao_gps_num_width < 4) {
156 if (ao_gps_char != ',')
166 ao_gps_parse_flag(char no_c, char yes_c) __reentrant
170 if (ao_gps_char == yes_c)
172 else if (ao_gps_char == no_c)
182 ao_gps(void) __reentrant
187 ao_serial_set_speed(AO_SERIAL_SPEED_9600);
188 for (i = 0; i < sizeof (ao_gps_config); i++)
189 ao_serial_putchar(ao_gps_config[i]);
191 /* Locate the begining of the next record */
193 c = ao_serial_getchar();
201 /* Skip anything other than GPG */
202 for (i = 0; i < AO_GPS_LEADER; i++) {
204 if (ao_gps_char != ao_gps_header[i])
207 if (i != AO_GPS_LEADER)
210 /* pull the record identifier characters off the link */
216 if (ao_gps_char != ',')
219 if (c == (uint8_t) 'G' && i == (uint8_t) 'A') {
220 /* Now read the data into the gps data record
222 * $GPGGA,025149.000,4528.1723,N,12244.2480,W,1,05,2.0,103.5,M,-19.5,M,,0000*66
226 * 025149.000 time (02:51:49.000 GMT)
227 * 4528.1723,N Latitude 45°28.1723' N
228 * 12244.2480,W Longitude 122°44.2480' W
234 * 4 = Real Time Kinematic
236 * 6 = estimated (dead reckoning)
237 * 7 = Manual input mode
238 * 8 = Simulation mode
239 * 05 Number of satellites (5)
240 * 2.0 Horizontal dilution
241 * 103.5,M Altitude, 103.5M above msl
242 * -19.5,M Height of geoid above WGS84 ellipsoid
243 * ? time in seconds since last DGPS update
244 * 0000 DGPS station ID
248 ao_gps_next.flags = AO_GPS_RUNNING;
249 ao_gps_next.hour = ao_gps_decimal(2);
250 ao_gps_next.minute = ao_gps_decimal(2);
251 ao_gps_next.second = ao_gps_decimal(2);
252 ao_gps_skip_field(); /* skip seconds fraction */
254 ao_gps_next.latitude = ao_gps_parse_pos(2);
255 if (ao_gps_parse_flag('N', 'S'))
256 ao_gps_next.latitude = -ao_gps_next.latitude;
257 ao_gps_next.longitude = ao_gps_parse_pos(3);
258 if (ao_gps_parse_flag('E', 'W'))
259 ao_gps_next.longitude = -ao_gps_next.longitude;
261 i = ao_gps_decimal(0xff);
263 ao_gps_next.flags |= AO_GPS_VALID;
265 i = ao_gps_decimal(0xff) << AO_GPS_NUM_SAT_SHIFT;
266 if (i > AO_GPS_NUM_SAT_MASK)
267 i = AO_GPS_NUM_SAT_MASK;
268 ao_gps_next.flags |= i;
271 ao_gps_skip_field(); /* Horizontal dilution */
273 ao_gps_next.altitude = ao_gps_decimal(0xff);
274 ao_gps_skip_field(); /* skip any fractional portion */
276 /* Skip remaining fields */
277 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
281 if (ao_gps_char == '*') {
282 uint8_t cksum = ao_gps_cksum ^ '*';
283 if (cksum != ao_gps_hex(2))
288 ao_mutex_get(&ao_gps_mutex);
289 memcpy(&ao_gps_data, &ao_gps_next, sizeof (struct ao_gps_data));
290 ao_mutex_put(&ao_gps_mutex);
291 ao_wakeup(&ao_gps_data);
293 } else if (c == (uint8_t) 'S' && i == (uint8_t) 'V') {
295 /* Now read the data into the GPS tracking data record
297 * $GPGSV,3,1,12,05,54,069,45,12,44,061,44,21,07,184,46,22,78,289,47*72<CR><LF>
299 * Satellites in view data
301 * 3 Total number of GSV messages
302 * 1 Sequence number of current GSV message
303 * 12 Total sats in view (0-12)
311 c = ao_gps_decimal(1); /* total messages */
312 i = ao_gps_decimal(1); /* message sequence */
314 ao_gps_tracking_next.channels = 0;
316 done = (uint8_t) c == i;
318 ao_gps_skip_field(); /* sats in view */
319 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
320 i = ao_gps_tracking_next.channels;
321 ao_gps_tracking_next.sats[i].svid = ao_gps_decimal(2); /* SVID */
323 ao_gps_skip_field(); /* elevation */
325 ao_gps_skip_field(); /* azimuth */
326 if (ao_gps_tracking_next.sats[i].c_n_1 = ao_gps_decimal(2)) /* C/N0 */
327 ao_gps_tracking_next.sats[i].state = 0xbf;
329 ao_gps_tracking_next.sats[i].state = 0;
330 ao_gps_tracking_next.channels = i + 1;
332 if (ao_gps_char == '*') {
333 uint8_t cksum = ao_gps_cksum ^ '*';
334 if (cksum != ao_gps_hex(2))
340 ao_gps_tracking_next.channels = 0;
342 ao_mutex_get(&ao_gps_mutex);
343 memcpy(&ao_gps_tracking_data, &ao_gps_tracking_next,
344 sizeof(ao_gps_tracking_data));
345 ao_mutex_put(&ao_gps_mutex);
346 ao_wakeup(&ao_gps_tracking_data);
352 __xdata struct ao_task ao_gps_task;
355 gps_dump(void) __reentrant
357 ao_mutex_get(&ao_gps_mutex);
358 ao_gps_print(&ao_gps_data);
360 ao_gps_tracking_print(&ao_gps_tracking_data);
362 ao_mutex_put(&ao_gps_mutex);
365 __code struct ao_cmds ao_gps_cmds[] = {
366 { 'g', gps_dump, "g Display current GPS values" },
367 { 0, gps_dump, NULL },
373 ao_add_task(&ao_gps_task, ao_gps, "gps");
374 ao_cmd_register(&ao_gps_cmds[0]);