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 __xdata struct ao_gps_data ao_gps_data;
28 static const char ao_gps_set_nmea[] = "\r\n$PSRF100,0,57600,8,1,0*37\r\n\r\n";
30 static const char ao_gps_set_nmea[] = "\r\n$PSRF100,0,4800,8,1,0*0F\r\n\r\n";
33 const char ao_gps_config[] = {
34 0xa0, 0xa2, 0x00, 0x0e, /* length: 14 bytes */
35 136, /* mode control */
37 4, /* degraded mode (disabled) */
39 0, 0, /* user specified altitude */
40 2, /* alt hold mode (disabled, require 3d fixes) */
41 0, /* alt hold source (use last computed altitude) */
43 0, /* Degraded time out (disabled) */
44 0, /* Dead Reckoning time out (disabled) */
45 0, /* Track smoothing (disabled) */
46 0x00, 0x8e, 0xb0, 0xb3,
48 0xa0, 0xa2, 0x00, 0x08, /* length: 8 bytes */
49 166, /* Set message rate */
50 2, /* enable/disable all messages */
51 0, /* message id (ignored) */
52 0, /* update rate (0 = disable) */
53 0, 0, 0, 0, /* reserved */
54 0x00, 0xa8, 0xb0, 0xb3,
56 0xa0, 0xa2, 0x00, 0x02, /* length: 2 bytes */
57 143, /* static navigation */
59 0x00, 0x8f, 0xb0, 0xb3,
62 #define NAV_TYPE_GPS_FIX_TYPE_MASK (7 << 0)
63 #define NAV_TYPE_NO_FIX (0 << 0)
64 #define NAV_TYPE_SV_KF (1 << 0)
65 #define NAV_TYPE_2_SV_KF (2 << 0)
66 #define NAV_TYPE_3_SV_KF (3 << 0)
67 #define NAV_TYPE_4_SV_KF (4 << 0)
68 #define NAV_TYPE_2D_LEAST_SQUARES (5 << 0)
69 #define NAV_TYPE_3D_LEAST_SQUARES (6 << 0)
70 #define NAV_TYPE_DR (7 << 0)
71 #define NAV_TYPE_TRICKLE_POWER (1 << 3)
72 #define NAV_TYPE_ALTITUDE_HOLD_MASK (3 << 4)
73 #define NAV_TYPE_ALTITUDE_HOLD_NONE (0 << 4)
74 #define NAV_TYPE_ALTITUDE_HOLD_KF (1 << 4)
75 #define NAV_TYPE_ALTITUDE_HOLD_USER (2 << 4)
76 #define NAV_TYPE_ALTITUDE_HOLD_ALWAYS (3 << 4)
77 #define NAV_TYPE_DOP_LIMIT_EXCEEDED (1 << 6)
78 #define NAV_TYPE_DGPS_APPLIED (1 << 7)
79 #define NAV_TYPE_SENSOR_DR (1 << 8)
80 #define NAV_TYPE_OVERDETERMINED (1 << 9)
81 #define NAV_TYPE_DR_TIMEOUT_EXCEEDED (1 << 10)
82 #define NAV_TYPE_FIX_MI_EDIT (1 << 11)
83 #define NAV_TYPE_INVALID_VELOCITY (1 << 12)
84 #define NAV_TYPE_ALTITUDE_HOLD_DISABLED (1 << 13)
85 #define NAV_TYPE_DR_ERROR_STATUS_MASK (3 << 14)
86 #define NAV_TYPE_DR_ERROR_STATUS_GPS_ONLY (0 << 14)
87 #define NAV_TYPE_DR_ERROR_STATUS_DR_FROM_GPS (1 << 14)
88 #define NAV_TYPE_DR_ERROR_STATUS_DR_SENSOR_ERROR (2 << 14)
89 #define NAV_TYPE_DR_ERROR_STATUS_DR_IN_TEST (3 << 14)
91 struct sirf_geodetic_nav_data {
102 uint16_t ground_speed;
111 static __xdata struct sirf_geodetic_nav_data ao_sirf_data;
113 static __pdata uint16_t ao_sirf_cksum;
114 static __pdata uint16_t ao_sirf_len;
116 #define ao_sirf_byte() ((uint8_t) ao_serial_getchar())
118 static uint8_t data_byte(void)
120 uint8_t c = ao_sirf_byte();
126 static void sirf_u16(uint8_t offset)
128 uint16_t __xdata *ptr = (uint16_t __xdata *) (((char __xdata *) &ao_sirf_data) + offset);
131 val = data_byte() << 8;
136 static void sirf_u8(uint8_t offset)
138 uint8_t __xdata *ptr = (uint8_t __xdata *) (((char __xdata *) &ao_sirf_data) + offset);
145 static void sirf_u32(uint8_t offset)
147 uint32_t __xdata *ptr = (uint32_t __xdata *) (((char __xdata *) &ao_sirf_data) + offset);
150 val = ((uint32_t) data_byte ()) << 24;
151 val |= ((uint32_t) data_byte ()) << 16;
152 val |= ((uint32_t) data_byte ()) << 8;
153 val |= ((uint32_t) data_byte ());
157 static void sirf_discard(uint8_t len)
164 #define SIRF_DISCARD 1
169 struct sirf_packet_parse {
174 static const struct sirf_packet_parse geodetic_nav_data_packet[] = {
175 { SIRF_DISCARD, 2 }, /* 1 nav valid */
176 { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, nav_type) }, /* 3 */
177 { SIRF_DISCARD, 6 }, /* 5 week number, time of week */
178 { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, utc_year) }, /* 11 */
179 { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_month) }, /* 13 */
180 { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_day) }, /* 14 */
181 { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_hour) }, /* 15 */
182 { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_minute) }, /* 16 */
183 { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, utc_second) }, /* 17 */
184 { SIRF_DISCARD, 4 }, /* satellite id list */ /* 19 */
185 { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, lat) }, /* 23 */
186 { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, lon) }, /* 27 */
187 { SIRF_DISCARD, 4 }, /* altitude from ellipsoid */ /* 31 */
188 { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, alt_msl) }, /* 35 */
189 { SIRF_DISCARD, 1 }, /* map datum */ /* 39 */
190 { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, ground_speed) }, /* 40 */
191 { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, course) }, /* 42 */
192 { SIRF_DISCARD, 2 }, /* magnetic variation */ /* 44 */
193 { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, climb_rate) }, /* 46 */
194 { SIRF_DISCARD, 2 }, /* turn rate */ /* 48 */
195 { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, h_error) }, /* 50 */
196 { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, v_error) }, /* 54 */
197 { SIRF_DISCARD, 30 }, /* time error, h_vel error, clock_bias,
198 clock bias error, clock drift,
199 clock drift error, distance,
200 distance error, heading error */ /* 58 */
201 { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, num_sv) }, /* 88 */
202 { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, hdop) }, /* 89 */
203 { SIRF_DISCARD, 1 }, /* additional mode info */ /* 90 */
204 { SIRF_END, 0 }, /* 91 */
208 ao_sirf_parse_41(void)
213 offset = geodetic_nav_data_packet[i].offset;
214 switch (geodetic_nav_data_packet[i].type) {
218 sirf_discard(offset);
234 ao_gps_setup(void) __reentrant
238 ao_serial_set_speed(AO_SERIAL_SPEED_4800);
240 for (i = 0; i < 64; i++)
241 ao_serial_putchar(0x00);
242 for (k = 0; k < 3; k++)
243 for (i = 0; i < sizeof (ao_gps_set_nmea); i++)
244 ao_serial_putchar(ao_gps_set_nmea[i]);
246 ao_serial_set_speed(AO_SERIAL_SPEED_57600);
248 for (i = 0; i < 64; i++)
249 ao_serial_putchar(0x00);
252 static const char ao_gps_set_message_rate[] = {
253 0xa0, 0xa2, 0x00, 0x08,
259 ao_sirf_set_message_rate(uint8_t msg, uint8_t rate)
261 uint16_t cksum = 0x00a6;
264 for (i = 0; i < sizeof (ao_gps_set_message_rate); i++)
265 ao_serial_putchar(ao_gps_set_message_rate[i]);
266 ao_serial_putchar(msg);
267 ao_serial_putchar(rate);
268 cksum = 0xa6 + msg + rate;
269 for (i = 0; i < 4; i++)
270 ao_serial_putchar(0);
271 ao_serial_putchar((cksum >> 8) & 0x7f);
272 ao_serial_putchar(cksum & 0xff);
273 ao_serial_putchar(0xb0);
274 ao_serial_putchar(0xb3);
277 static const uint8_t sirf_disable[] = {
288 ao_gps(void) __reentrant
294 for (k = 0; k < 5; k++)
296 for (i = 0; i < sizeof (ao_gps_config); i++)
297 ao_serial_putchar(ao_gps_config[i]);
298 for (i = 0; i < sizeof (sirf_disable); i++)
299 ao_sirf_set_message_rate(sirf_disable[i], 0);
300 ao_sirf_set_message_rate(41, 1);
303 /* Locate the begining of the next record */
304 while (ao_sirf_byte() != (uint8_t) 0xa0)
306 if (ao_sirf_byte() != (uint8_t) 0xa2)
310 ao_sirf_len = ao_sirf_byte() << 8;
311 ao_sirf_len |= ao_sirf_byte();
312 if (ao_sirf_len > 1023)
318 i = data_byte (); /* 0 */
322 if (ao_sirf_len < 90)
327 if (ao_sirf_len != 0)
330 /* verify checksum and end sequence */
331 ao_sirf_cksum &= 0x7fff;
332 cksum = ao_sirf_byte() << 8;
333 cksum |= ao_sirf_byte();
334 if (ao_sirf_cksum != cksum)
336 if (ao_sirf_byte() != (uint8_t) 0xb0)
338 if (ao_sirf_byte() != (uint8_t) 0xb3)
343 ao_mutex_get(&ao_gps_mutex);
344 ao_gps_data.hour = ao_sirf_data.utc_hour;
345 ao_gps_data.minute = ao_sirf_data.utc_minute;
346 ao_gps_data.second = ao_sirf_data.utc_second / 1000;
347 ao_gps_data.flags = ((ao_sirf_data.num_sv << AO_GPS_NUM_SAT_SHIFT) & AO_GPS_NUM_SAT_MASK) | AO_GPS_RUNNING;
348 if ((ao_sirf_data.nav_type & NAV_TYPE_GPS_FIX_TYPE_MASK) >= NAV_TYPE_4_SV_KF)
349 ao_gps_data.flags |= AO_GPS_VALID;
350 ao_gps_data.latitude = ao_sirf_data.lat;
351 ao_gps_data.longitude = ao_sirf_data.lon;
352 ao_gps_data.altitude = ao_sirf_data.alt_msl / 100;
353 ao_gps_data.ground_speed = ao_sirf_data.ground_speed;
354 ao_gps_data.course = ao_sirf_data.course / 200;
355 ao_gps_data.hdop = ao_sirf_data.hdop;
356 ao_gps_data.climb_rate = ao_sirf_data.climb_rate;
357 if (ao_sirf_data.h_error > 6553500)
358 ao_gps_data.h_error = 65535;
360 ao_gps_data.h_error = ao_sirf_data.h_error / 100;
361 if (ao_sirf_data.v_error > 6553500)
362 ao_gps_data.v_error = 65535;
364 ao_gps_data.v_error = ao_sirf_data.v_error / 100;
365 ao_mutex_put(&ao_gps_mutex);
366 ao_wakeup(&ao_gps_data);
372 __xdata struct ao_task ao_gps_task;
375 gps_dump(void) __reentrant
377 ao_mutex_get(&ao_gps_mutex);
378 ao_gps_print(&ao_gps_data);
379 ao_mutex_put(&ao_gps_mutex);
382 __code struct ao_cmds ao_gps_cmds[] = {
383 { 'g', gps_dump, "g Display current GPS values" },
384 { 0, gps_dump, NULL },
390 ao_add_task(&ao_gps_task, ao_gps, "gps");
391 ao_cmd_register(&ao_gps_cmds[0]);