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) */
57 ao_gps_char = ao_serial_getchar();
58 ao_gps_cksum ^= ao_gps_char;
64 while (ao_gps_char >= '0')
69 ao_gps_skip_field(void)
71 while (ao_gps_char != ',' && ao_gps_char != '*' && ao_gps_char != '\n')
78 if (ao_gps_char == ',' || ao_gps_char == '.' || ao_gps_char == '*')
82 __xdata static uint8_t ao_gps_num_width;
85 ao_gps_decimal(uint8_t max_width)
88 __xdata uint8_t neg = 0;
91 if (ao_gps_char == '-') {
97 while (ao_gps_num_width < max_width) {
98 if (ao_gps_char < '0' || '9' < ao_gps_char)
100 v = v * (int16_t) 10 + ao_gps_char - '0';
110 ao_gps_hex(uint8_t max_width)
116 ao_gps_num_width = 0;
117 while (ao_gps_num_width < max_width) {
118 if ('0' <= ao_gps_char && ao_gps_char <= '9')
119 d = ao_gps_char - '0';
120 else if ('A' <= ao_gps_char && ao_gps_char <= 'F')
121 d = ao_gps_char - 'A' + 10;
122 else if ('a' <= ao_gps_char && ao_gps_char <= 'f')
123 d = ao_gps_char - 'a' + 10;
134 ao_gps_parse_pos(uint8_t deg_width) __reentrant
140 d = ao_gps_decimal(deg_width);
141 m = ao_gps_decimal(2);
142 if (ao_gps_char == '.') {
143 f = ao_gps_decimal(4);
144 while (ao_gps_num_width < 4) {
150 if (ao_gps_char != ',')
160 ao_gps_parse_flag(char no_c, char yes_c) __reentrant
164 if (ao_gps_char == yes_c)
166 else if (ao_gps_char == no_c)
176 ao_gps(void) __reentrant
181 ao_serial_set_speed(AO_SERIAL_SPEED_9600);
182 for (i = 0; i < sizeof (ao_gps_config); i++)
183 ao_serial_putchar(ao_gps_config[i]);
185 /* Locate the begining of the next record */
187 c = ao_serial_getchar();
195 /* Skip anything other than GPG */
196 for (i = 0; i < AO_GPS_LEADER; i++) {
198 if (ao_gps_char != ao_gps_header[i])
201 if (i != AO_GPS_LEADER)
204 /* pull the record identifier characters off the link */
210 if (ao_gps_char != ',')
213 if (c == (uint8_t) 'G' && i == (uint8_t) 'A') {
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.flags = AO_GPS_RUNNING;
243 ao_gps_next.hour = ao_gps_decimal(2);
244 ao_gps_next.minute = ao_gps_decimal(2);
245 ao_gps_next.second = ao_gps_decimal(2);
246 ao_gps_skip_field(); /* skip seconds fraction */
248 ao_gps_next.latitude = ao_gps_parse_pos(2);
249 if (ao_gps_parse_flag('N', 'S'))
250 ao_gps_next.latitude = -ao_gps_next.latitude;
251 ao_gps_next.longitude = ao_gps_parse_pos(3);
252 if (ao_gps_parse_flag('E', 'W'))
253 ao_gps_next.longitude = -ao_gps_next.longitude;
255 i = ao_gps_decimal(0xff);
257 ao_gps_next.flags |= AO_GPS_VALID;
259 i = ao_gps_decimal(0xff) << AO_GPS_NUM_SAT_SHIFT;
260 if (i > AO_GPS_NUM_SAT_MASK)
261 i = AO_GPS_NUM_SAT_MASK;
262 ao_gps_next.flags |= i;
265 ao_gps_skip_field(); /* Horizontal dilution */
267 ao_gps_next.altitude = ao_gps_decimal(0xff);
268 ao_gps_skip_field(); /* skip any fractional portion */
270 /* Skip remaining fields */
271 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
275 if (ao_gps_char == '*') {
276 uint8_t cksum = ao_gps_cksum ^ '*';
277 if (cksum != ao_gps_hex(2))
282 ao_mutex_get(&ao_gps_mutex);
283 memcpy(&ao_gps_data, &ao_gps_next, sizeof (struct ao_gps_data));
284 ao_mutex_put(&ao_gps_mutex);
285 ao_wakeup(&ao_gps_data);
287 } else if (c == (uint8_t) 'S' && i == (uint8_t) 'V') {
289 /* Now read the data into the GPS tracking data record
291 * $GPGSV,3,1,12,05,54,069,45,12,44,061,44,21,07,184,46,22,78,289,47*72<CR><LF>
293 * Satellites in view data
295 * 3 Total number of GSV messages
296 * 1 Sequence number of current GSV message
297 * 12 Total sats in view (0-12)
305 c = ao_gps_decimal(1); /* total messages */
306 i = ao_gps_decimal(1); /* message sequence */
308 ao_gps_tracking_next.channels = 0;
310 done = (uint8_t) c == i;
312 ao_gps_skip_field(); /* sats in view */
313 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
314 i = ao_gps_tracking_next.channels;
315 ao_gps_tracking_next.sats[i].svid = ao_gps_decimal(2); /* SVID */
317 ao_gps_skip_field(); /* elevation */
319 ao_gps_skip_field(); /* azimuth */
320 if (ao_gps_tracking_next.sats[i].c_n_1 = ao_gps_decimal(2)) /* C/N0 */
321 ao_gps_tracking_next.sats[i].state = 0xbf;
323 ao_gps_tracking_next.sats[i].state = 0;
324 ao_gps_tracking_next.channels = i + 1;
326 if (ao_gps_char == '*') {
327 uint8_t cksum = ao_gps_cksum ^ '*';
328 if (cksum != ao_gps_hex(2))
334 ao_gps_tracking_next.channels = 0;
336 ao_mutex_get(&ao_gps_mutex);
337 memcpy(&ao_gps_tracking_data, &ao_gps_tracking_next,
338 sizeof(ao_gps_tracking_data));
339 ao_mutex_put(&ao_gps_mutex);
340 ao_wakeup(&ao_gps_tracking_data);
346 __xdata struct ao_task ao_gps_task;
349 gps_dump(void) __reentrant
351 ao_mutex_get(&ao_gps_mutex);
352 ao_gps_print(&ao_gps_data);
354 ao_gps_tracking_print(&ao_gps_tracking_data);
356 ao_mutex_put(&ao_gps_mutex);
359 __code struct ao_cmds ao_gps_cmds[] = {
360 { 'g', gps_dump, "g Display current GPS values" },
361 { 0, gps_dump, NULL },
367 ao_add_task(&ao_gps_task, ao_gps, "gps");
368 ao_cmd_register(&ao_gps_cmds[0]);