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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 __xdata uint8_t ao_gps_new;
24 __xdata uint8_t ao_gps_mutex;
25 __pdata uint16_t ao_gps_tick;
26 __xdata struct ao_telemetry_location ao_gps_data;
27 __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
29 static const char ao_gps_set_nmea[] = "\r\n$PSRF100,0,57600,8,1,0*37\r\n";
31 const char ao_gps_config[] = {
33 0xa0, 0xa2, 0x00, 0x0e, /* length: 14 bytes */
34 136, /* mode control */
36 0, /* degraded mode (allow 1-SV navigation) */
38 0, 0, /* user specified altitude */
39 2, /* alt hold mode (disabled, require 3d fixes) */
40 0, /* alt hold source (use last computed altitude) */
42 10, /* Degraded time out (10 sec) */
43 10, /* Dead Reckoning time out (10 sec) */
44 0, /* Track smoothing (disabled) */
45 0x00, 0x8e, 0xb0, 0xb3,
47 0xa0, 0xa2, 0x00, 0x08, /* length: 8 bytes */
48 166, /* Set message rate */
49 2, /* enable/disable all messages */
50 0, /* message id (ignored) */
51 0, /* update rate (0 = disable) */
52 0, 0, 0, 0, /* reserved */
53 0x00, 0xa8, 0xb0, 0xb3,
55 0xa0, 0xa2, 0x00, 0x02, /* length: 2 bytes */
56 143, /* static navigation */
58 0x00, 0x8f, 0xb0, 0xb3,
61 #define NAV_TYPE_GPS_FIX_TYPE_MASK (7 << 0)
62 #define NAV_TYPE_NO_FIX (0 << 0)
63 #define NAV_TYPE_SV_KF (1 << 0)
64 #define NAV_TYPE_2_SV_KF (2 << 0)
65 #define NAV_TYPE_3_SV_KF (3 << 0)
66 #define NAV_TYPE_4_SV_KF (4 << 0)
67 #define NAV_TYPE_2D_LEAST_SQUARES (5 << 0)
68 #define NAV_TYPE_3D_LEAST_SQUARES (6 << 0)
69 #define NAV_TYPE_DR (7 << 0)
70 #define NAV_TYPE_TRICKLE_POWER (1 << 3)
71 #define NAV_TYPE_ALTITUDE_HOLD_MASK (3 << 4)
72 #define NAV_TYPE_ALTITUDE_HOLD_NONE (0 << 4)
73 #define NAV_TYPE_ALTITUDE_HOLD_KF (1 << 4)
74 #define NAV_TYPE_ALTITUDE_HOLD_USER (2 << 4)
75 #define NAV_TYPE_ALTITUDE_HOLD_ALWAYS (3 << 4)
76 #define NAV_TYPE_DOP_LIMIT_EXCEEDED (1 << 6)
77 #define NAV_TYPE_DGPS_APPLIED (1 << 7)
78 #define NAV_TYPE_SENSOR_DR (1 << 8)
79 #define NAV_TYPE_OVERDETERMINED (1 << 9)
80 #define NAV_TYPE_DR_TIMEOUT_EXCEEDED (1 << 10)
81 #define NAV_TYPE_FIX_MI_EDIT (1 << 11)
82 #define NAV_TYPE_INVALID_VELOCITY (1 << 12)
83 #define NAV_TYPE_ALTITUDE_HOLD_DISABLED (1 << 13)
84 #define NAV_TYPE_DR_ERROR_STATUS_MASK (3 << 14)
85 #define NAV_TYPE_DR_ERROR_STATUS_GPS_ONLY (0 << 14)
86 #define NAV_TYPE_DR_ERROR_STATUS_DR_FROM_GPS (1 << 14)
87 #define NAV_TYPE_DR_ERROR_STATUS_DR_SENSOR_ERROR (2 << 14)
88 #define NAV_TYPE_DR_ERROR_STATUS_DR_IN_TEST (3 << 14)
90 struct sirf_geodetic_nav_data {
101 uint16_t ground_speed;
110 static __xdata struct sirf_geodetic_nav_data ao_sirf_data;
112 struct sirf_measured_sat_data {
117 struct sirf_measured_tracker_data {
121 struct sirf_measured_sat_data sats[12];
124 static __xdata struct sirf_measured_tracker_data ao_sirf_tracker_data;
126 static __pdata uint16_t ao_sirf_cksum;
127 static __pdata uint16_t ao_sirf_len;
129 #ifndef ao_sirf_getchar
130 #define ao_sirf_getchar ao_serial1_getchar
131 #define ao_sirf_putchar ao_serial1_putchar
132 #define ao_sirf_set_speed ao_serial1_set_speed
135 #define ao_sirf_byte() ((uint8_t) ao_sirf_getchar())
137 static uint8_t data_byte(void)
139 uint8_t c = ao_sirf_byte();
145 static char __xdata *sirf_target;
147 static void sirf_u16(uint8_t offset)
149 uint16_t __xdata *ptr = (uint16_t __xdata *) (sirf_target + offset);
152 val = data_byte() << 8;
157 static void sirf_u8(uint8_t offset)
159 uint8_t __xdata *ptr = (uint8_t __xdata *) (sirf_target + offset);
166 static void sirf_u32(uint8_t offset) __reentrant
168 uint32_t __xdata *ptr = (uint32_t __xdata *) (sirf_target + offset);
171 val = ((uint32_t) data_byte ()) << 24;
172 val |= ((uint32_t) data_byte ()) << 16;
173 val |= ((uint32_t) data_byte ()) << 8;
174 val |= ((uint32_t) data_byte ());
178 static void sirf_discard(uint8_t len)
185 #define SIRF_DISCARD 1
191 struct sirf_packet_parse {
197 ao_sirf_parse(void __xdata *target, const struct sirf_packet_parse *parse) __reentrant
199 uint8_t i, offset, j;
201 sirf_target = target;
203 offset = parse[i].offset;
204 switch (parse[i].type) {
208 sirf_discard(offset);
227 static const struct sirf_packet_parse geodetic_nav_data_packet[] = {
228 { SIRF_DISCARD, 2 }, /* 1 nav valid */
229 { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, nav_type) }, /* 3 */
230 { SIRF_DISCARD, 6 }, /* 5 week number, time of week */
231 { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, utc_year) }, /* 11 */
232 { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_month) }, /* 13 */
233 { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_day) }, /* 14 */
234 { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_hour) }, /* 15 */
235 { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_minute) }, /* 16 */
236 { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, utc_second) }, /* 17 */
237 { SIRF_DISCARD, 4 }, /* satellite id list */ /* 19 */
238 { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, lat) }, /* 23 */
239 { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, lon) }, /* 27 */
240 { SIRF_DISCARD, 4 }, /* altitude from ellipsoid */ /* 31 */
241 { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, alt_msl) }, /* 35 */
242 { SIRF_DISCARD, 1 }, /* map datum */ /* 39 */
243 { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, ground_speed) }, /* 40 */
244 { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, course) }, /* 42 */
245 { SIRF_DISCARD, 2 }, /* magnetic variation */ /* 44 */
246 { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, climb_rate) }, /* 46 */
247 { SIRF_DISCARD, 2 }, /* turn rate */ /* 48 */
248 { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, h_error) }, /* 50 */
249 { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, v_error) }, /* 54 */
250 { SIRF_DISCARD, 30 }, /* time error, h_vel error, clock_bias,
251 clock bias error, clock drift,
252 clock drift error, distance,
253 distance error, heading error */ /* 58 */
254 { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, num_sv) }, /* 88 */
255 { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, hdop) }, /* 89 */
256 { SIRF_DISCARD, 1 }, /* additional mode info */ /* 90 */
257 { SIRF_END, 0 }, /* 91 */
261 ao_sirf_parse_41(void) __reentrant
263 ao_sirf_parse(&ao_sirf_data, geodetic_nav_data_packet);
266 static const struct sirf_packet_parse measured_tracker_data_packet[] = {
267 { SIRF_U16, offsetof (struct sirf_measured_tracker_data, gps_week) }, /* 1 week */
268 { SIRF_U32, offsetof (struct sirf_measured_tracker_data, gps_tow) }, /* 3 time of week */
269 { SIRF_U8, offsetof (struct sirf_measured_tracker_data, channels) }, /* 7 channels */
273 static const struct sirf_packet_parse measured_sat_data_packet[] = {
274 { SIRF_U8, offsetof (struct sirf_measured_sat_data, svid) }, /* 0 SV id */
275 { SIRF_DISCARD, 4 }, /* 1 azimuth, 2 elevation, 3 state */
276 { SIRF_U8, offsetof (struct sirf_measured_sat_data, c_n_1) }, /* C/N0 1 */
277 { SIRF_DISCARD, 9 }, /* C/N0 2-10 */
282 ao_sirf_parse_4(void) __reentrant
285 ao_sirf_parse(&ao_sirf_tracker_data, measured_tracker_data_packet);
286 for (i = 0; i < 12; i++)
287 ao_sirf_parse(&ao_sirf_tracker_data.sats[i], measured_sat_data_packet);
291 ao_gps_setup(void) __reentrant
294 ao_sirf_set_speed(AO_SERIAL_SPEED_4800);
295 for (i = 0; i < 64; i++)
296 ao_sirf_putchar(0x00);
297 for (k = 0; k < 3; k++)
298 for (i = 0; i < sizeof (ao_gps_set_nmea); i++)
299 ao_sirf_putchar(ao_gps_set_nmea[i]);
300 ao_sirf_set_speed(AO_SERIAL_SPEED_57600);
301 for (i = 0; i < 64; i++)
302 ao_sirf_putchar(0x00);
305 static const char ao_gps_set_message_rate[] = {
306 0xa0, 0xa2, 0x00, 0x08,
312 ao_sirf_set_message_rate(uint8_t msg, uint8_t rate) __reentrant
314 uint16_t cksum = 0x00a6;
317 for (i = 0; i < sizeof (ao_gps_set_message_rate); i++)
318 ao_sirf_putchar(ao_gps_set_message_rate[i]);
319 ao_sirf_putchar(msg);
320 ao_sirf_putchar(rate);
321 cksum = 0xa6 + msg + rate;
322 for (i = 0; i < 4; i++)
324 ao_sirf_putchar((cksum >> 8) & 0x7f);
325 ao_sirf_putchar(cksum & 0xff);
326 ao_sirf_putchar(0xb0);
327 ao_sirf_putchar(0xb3);
330 static const uint8_t sirf_disable[] = {
340 ao_gps(void) __reentrant
346 for (k = 0; k < 5; k++)
348 for (i = 0; i < sizeof (ao_gps_config); i++)
349 ao_sirf_putchar(ao_gps_config[i]);
350 for (i = 0; i < sizeof (sirf_disable); i++)
351 ao_sirf_set_message_rate(sirf_disable[i], 0);
352 ao_sirf_set_message_rate(41, 1);
353 ao_sirf_set_message_rate(4, 1);
356 /* Locate the begining of the next record */
357 while (ao_sirf_byte() != (uint8_t) 0xa0)
359 if (ao_sirf_byte() != (uint8_t) 0xa2)
363 ao_sirf_len = ao_sirf_byte() << 8;
364 ao_sirf_len |= ao_sirf_byte();
365 if (ao_sirf_len > 1023)
371 i = data_byte (); /* 0 */
375 if (ao_sirf_len < 90)
380 if (ao_sirf_len < 187)
385 if (ao_sirf_len != 0)
388 /* verify checksum and end sequence */
389 ao_sirf_cksum &= 0x7fff;
390 cksum = ao_sirf_byte() << 8;
391 cksum |= ao_sirf_byte();
392 if (ao_sirf_cksum != cksum)
394 if (ao_sirf_byte() != (uint8_t) 0xb0)
396 if (ao_sirf_byte() != (uint8_t) 0xb3)
401 ao_mutex_get(&ao_gps_mutex);
402 ao_gps_tick = ao_time();
403 ao_gps_data.hour = ao_sirf_data.utc_hour;
404 ao_gps_data.minute = ao_sirf_data.utc_minute;
405 ao_gps_data.second = ao_sirf_data.utc_second / 1000;
406 ao_gps_data.flags = ((ao_sirf_data.num_sv << AO_GPS_NUM_SAT_SHIFT) & AO_GPS_NUM_SAT_MASK) | AO_GPS_RUNNING;
407 if ((ao_sirf_data.nav_type & NAV_TYPE_GPS_FIX_TYPE_MASK) >= NAV_TYPE_4_SV_KF)
408 ao_gps_data.flags |= AO_GPS_VALID;
409 ao_gps_data.latitude = ao_sirf_data.lat;
410 ao_gps_data.longitude = ao_sirf_data.lon;
411 ao_gps_data.altitude = ao_sirf_data.alt_msl / 100;
412 ao_gps_data.ground_speed = ao_sirf_data.ground_speed;
413 ao_gps_data.course = ao_sirf_data.course / 200;
414 ao_gps_data.hdop = ao_sirf_data.hdop;
415 ao_gps_data.climb_rate = ao_sirf_data.climb_rate;
416 ao_gps_data.flags |= AO_GPS_COURSE_VALID;
418 if (ao_sirf_data.h_error > 6553500)
419 ao_gps_data.h_error = 65535;
421 ao_gps_data.h_error = ao_sirf_data.h_error / 100;
422 if (ao_sirf_data.v_error > 6553500)
423 ao_gps_data.v_error = 65535;
425 ao_gps_data.v_error = ao_sirf_data.v_error / 100;
427 ao_gps_new |= AO_GPS_NEW_DATA;
428 ao_mutex_put(&ao_gps_mutex);
429 ao_wakeup(&ao_gps_new);
432 ao_mutex_get(&ao_gps_mutex);
433 ao_gps_tracking_data.channels = ao_sirf_tracker_data.channels;
434 for (i = 0; i < 12; i++) {
435 ao_gps_tracking_data.sats[i].svid = ao_sirf_tracker_data.sats[i].svid;
436 ao_gps_tracking_data.sats[i].c_n_1 = ao_sirf_tracker_data.sats[i].c_n_1;
438 ao_gps_new |= AO_GPS_NEW_TRACKING;
439 ao_mutex_put(&ao_gps_mutex);
440 ao_wakeup(&ao_gps_new);
446 __xdata struct ao_task ao_gps_task;
451 ao_add_task(&ao_gps_task, ao_gps, "gps");