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.
20 #define AO_GPS_LEADER 6
22 static const char ao_gps_header[] = "GPGGA,";
23 __xdata uint8_t ao_gps_mutex;
24 static __xdata char ao_gps_char;
25 static __xdata uint8_t ao_gps_cksum;
26 static __xdata uint8_t ao_gps_error;
28 __xdata struct ao_gps_data ao_gps_data;
29 static __xdata struct ao_gps_data ao_gps_next;
31 const char ao_gps_config[] =
32 "$PSRF103,00,00,01,01*25\r\n" /* GGA 1 per sec */
33 "$PSRF103,01,00,00,01*25\r\n" /* GLL disable */
34 "$PSRF103,02,00,00,01*26\r\n" /* GSA disable */
35 "$PSRF103,03,00,00,01*27\r\n" /* GSV disable */
36 "$PSRF103,04,00,00,01*20\r\n" /* RMC disable */
37 "$PSRF103,05,00,00,01*21\r\n"; /* VTG disable */
45 ao_gps_char = ao_serial_getchar();
46 ao_gps_cksum ^= ao_gps_char;
52 while (ao_gps_char >= '0')
57 ao_gps_skip_field(void)
59 while (ao_gps_char != ',' && ao_gps_char != '*' && ao_gps_char != '\n')
66 while (ao_gps_char == ',' || ao_gps_char == '.' || ao_gps_char == '*')
70 __xdata static uint8_t ao_gps_num_width;
73 ao_gps_decimal(uint8_t max_width)
76 __xdata uint8_t neg = 0;
79 if (ao_gps_char == '-') {
85 while (ao_gps_num_width < max_width) {
86 if (ao_gps_char < '0' || '9' < ao_gps_char)
88 v = v * (int16_t) 10 + ao_gps_char - '0';
98 ao_gps_hex(uint8_t max_width)
104 ao_gps_num_width = 0;
105 while (ao_gps_num_width < max_width) {
106 if ('0' <= ao_gps_char && ao_gps_char <= '9')
107 d = ao_gps_char - '0';
108 else if ('A' <= ao_gps_char && ao_gps_char <= 'F')
109 d = ao_gps_char - 'A' + 10;
110 else if ('a' <= ao_gps_char && ao_gps_char <= 'f')
111 d = ao_gps_char - 'a' + 10;
122 ao_gps_parse_pos(__xdata struct ao_gps_pos * pos, uint8_t deg_width) __reentrant
124 pos->degrees = ao_gps_decimal(deg_width);
125 pos->minutes = ao_gps_decimal(2);
126 if (ao_gps_char == '.') {
127 pos->minutes_fraction = ao_gps_decimal(4);
128 while (ao_gps_num_width < 4) {
129 pos->minutes_fraction *= 10;
133 pos->minutes_fraction = 0;
134 if (ao_gps_char != ',')
140 ao_gps_parse_flag(char yes_c, uint8_t yes, char no_c, uint8_t no) __reentrant
143 if (ao_gps_char == yes_c)
144 ao_gps_next.flags |= yes;
145 else if (ao_gps_char == no_c)
146 ao_gps_next.flags |= no;
154 ao_gps(void) __reentrant
159 for (i = 0; (c = ao_gps_config[i]); i++)
160 ao_serial_putchar(c);
162 /* Locate the begining of the next record */
164 c = ao_serial_getchar();
172 /* Skip anything other than GGA */
173 for (i = 0; i < AO_GPS_LEADER; i++) {
175 if (ao_gps_char != ao_gps_header[i])
178 if (i != AO_GPS_LEADER)
181 /* Now read the data into the gps data record
183 * $GPGGA,025149.000,4528.1723,N,12244.2480,W,1,05,2.0,103.5,M,-19.5,M,,0000*66
187 * 025149.000 time (02:51:49.000 GMT)
188 * 4528.1723,N Latitude 45°28.1723' N
189 * 12244.2480,W Longitude 122°44.2480' W
195 * 4 = Real Time Kinematic
197 * 6 = estimated (dead reckoning)
198 * 7 = Manual input mode
199 * 8 = Simulation mode
200 * 05 Number of satellites (5)
201 * 2.0 Horizontal dilution
202 * 103.5,M Altitude, 103.5M above msl
203 * -19.5,M Height of geoid above WGS84 ellipsoid
204 * ? time in seconds since last DGPS update
205 * 0000 DGPS station ID
209 ao_gps_next.flags = 0;
210 ao_gps_next.hour = ao_gps_decimal(2);
211 ao_gps_next.minute = ao_gps_decimal(2);
212 ao_gps_next.second = ao_gps_decimal(2);
213 ao_gps_skip_field(); /* skip seconds fraction */
215 ao_gps_parse_pos(&ao_gps_next.latitude, 2);
216 ao_gps_parse_flag('N', AO_GPS_LATITUDE_NORTH, 'S', AO_GPS_LATITUDE_SOUTH);
217 ao_gps_parse_pos(&ao_gps_next.longitude, 3);
218 ao_gps_parse_flag('W', AO_GPS_LONGITUDE_WEST, 'E', AO_GPS_LONGITUDE_EAST);
220 i = ao_gps_decimal(0xff);
222 ao_gps_next.flags |= AO_GPS_VALID;
224 i = ao_gps_decimal(0xff) << AO_GPS_NUM_SAT_SHIFT;
225 if (i > AO_GPS_NUM_SAT_MASK)
226 i = AO_GPS_NUM_SAT_MASK;
227 ao_gps_next.flags |= i;
230 ao_gps_skip_field(); /* Horizontal dilution */
232 ao_gps_next.altitude = ao_gps_decimal(0xff);
233 ao_gps_skip_field(); /* skip any fractional portion */
235 /* Skip remaining fields */
236 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
240 if (ao_gps_char == '*') {
241 uint8_t cksum = ao_gps_cksum ^ '*';
242 if (cksum != ao_gps_hex(2))
247 ao_mutex_get(&ao_gps_mutex);
248 memcpy(&ao_gps_data, &ao_gps_next, sizeof (struct ao_gps_data));
249 ao_mutex_put(&ao_gps_mutex);
250 ao_wakeup(&ao_gps_data);
255 __xdata struct ao_task ao_gps_task;
258 gps_dump(void) __reentrant
260 ao_mutex_get(&ao_gps_mutex);
261 ao_gps_print(&ao_gps_data);
262 ao_mutex_put(&ao_gps_mutex);
265 __code struct ao_cmds ao_gps_cmds[] = {
266 { 'g', gps_dump, "g Display current GPS values" },
267 { 0, gps_dump, NULL },
273 ao_add_task(&ao_gps_task, ao_gps, "gps");
274 ao_cmd_register(&ao_gps_cmds[0]);