2 * Copyright © 2013 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 #include "ao_gps_ublox.h"
24 #define AO_UBLOX_DEBUG 0
28 __xdata uint8_t ao_gps_new;
29 __xdata uint8_t ao_gps_mutex;
30 __pdata uint16_t ao_gps_tick;
31 __xdata struct ao_telemetry_location ao_gps_data;
32 __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
34 #undef AO_SERIAL_SPEED_UBLOX
36 #ifndef AO_SERIAL_SPEED_UBLOX
37 #define AO_SERIAL_SPEED_UBLOX AO_SERIAL_SPEED_9600
40 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_57600
41 #define SERIAL_SPEED_STRING "57600"
42 #define SERIAL_SPEED_CHECKSUM "2d"
44 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_19200
45 #define SERIAL_SPEED_STRING "19200"
46 #define SERIAL_SPEED_CHECKSUM "23"
48 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_9600
49 #define SERIAL_SPEED_STRING "9600"
50 #define SERIAL_SPEED_CHECKSUM "16"
53 static const char ao_gps_set_nmea[] =
54 "\r\n$PUBX,41,1,3,1," SERIAL_SPEED_STRING ",0*" SERIAL_SPEED_CHECKSUM "\r\n";
56 struct ao_ublox_cksum {
60 static __pdata struct ao_ublox_cksum ao_ublox_cksum;
61 static __pdata uint16_t ao_ublox_len;
65 static uint8_t ao_gps_dbg_enable;
71 static void ao_gps_dbg(int level, char *format, ...) {
74 if (level & ao_gps_dbg_enable) {
83 #define ao_gps_dbg(fmt, ...)
86 static inline uint8_t ao_ublox_byte(void) {
87 uint8_t c = (uint8_t) ao_gps_getchar();
88 ao_gps_dbg(DBG_CHAR, " %02x", c);
92 static inline void add_cksum(struct ao_ublox_cksum *cksum, uint8_t c)
98 static void ao_ublox_init_cksum(void)
100 ao_ublox_cksum.a = ao_ublox_cksum.b = 0;
103 static void ao_ublox_put_u8(uint8_t c)
105 add_cksum(&ao_ublox_cksum, c);
106 ao_gps_dbg(DBG_CHAR, " (%02x)", c);
110 static void ao_ublox_put_i8(int8_t c)
112 ao_ublox_put_u8((uint8_t) c);
115 static void ao_ublox_put_u16(uint16_t c)
118 ao_ublox_put_u8(c>>8);
122 static void ao_ublox_put_i16(int16_t c)
124 ao_ublox_put_u16((uint16_t) c);
128 static void ao_ublox_put_u32(uint32_t c)
131 ao_ublox_put_u8(c>>8);
132 ao_ublox_put_u8(c>>16);
133 ao_ublox_put_u8(c>>24);
136 static void ao_ublox_put_i32(int32_t c)
138 ao_ublox_put_u32((uint32_t) c);
141 static uint8_t header_byte(void)
143 uint8_t c = ao_ublox_byte();
144 add_cksum(&ao_ublox_cksum, c);
148 static uint8_t data_byte(void)
151 return header_byte();
154 static char __xdata *ublox_target;
156 static void ublox_u16(uint8_t offset)
158 uint16_t __xdata *ptr = (uint16_t __xdata *) (ublox_target + offset);
162 val |= data_byte () << 8;
166 static void ublox_u8(uint8_t offset)
168 uint8_t __xdata *ptr = (uint8_t __xdata *) (ublox_target + offset);
175 static void ublox_u32(uint8_t offset) __reentrant
177 uint32_t __xdata *ptr = (uint32_t __xdata *) (ublox_target + offset);
180 val = ((uint32_t) data_byte ());
181 val |= ((uint32_t) data_byte ()) << 8;
182 val |= ((uint32_t) data_byte ()) << 16;
183 val |= ((uint32_t) data_byte ()) << 24;
187 static void ublox_discard(uint8_t len)
194 #define UBLOX_DISCARD 1
199 struct ublox_packet_parse {
205 ao_ublox_parse(void __xdata *target, const struct ublox_packet_parse *parse) __reentrant
209 ublox_target = target;
211 offset = parse[i].offset;
212 switch (parse[i].type) {
216 ublox_discard(offset);
229 ao_gps_dbg(DBG_PROTO, "\n");
233 * NAV-DOP message parsing
236 static struct nav_dop {
242 static const struct ublox_packet_parse nav_dop_packet[] = {
243 { UBLOX_DISCARD, 6 }, /* 0 GPS Millisecond Time of Week, gDOP */
244 { UBLOX_U16, offsetof(struct nav_dop, pdop) }, /* 6 pDOP */
245 { UBLOX_DISCARD, 2 }, /* 8 tDOP */
246 { UBLOX_U16, offsetof(struct nav_dop, vdop) }, /* 10 vDOP */
247 { UBLOX_U16, offsetof(struct nav_dop, hdop) }, /* 12 hDOP */
248 { UBLOX_DISCARD, 4 }, /* 14 nDOP, eDOP */
253 ao_ublox_parse_nav_dop(void)
255 ao_ublox_parse(&nav_dop, nav_dop_packet);
259 * NAV-POSLLH message parsing
261 static struct nav_posllh {
267 static const struct ublox_packet_parse nav_posllh_packet[] = {
268 { UBLOX_DISCARD, 4 }, /* 0 GPS Millisecond Time of Week */
269 { UBLOX_U32, offsetof(struct nav_posllh, lon) }, /* 4 Longitude */
270 { UBLOX_U32, offsetof(struct nav_posllh, lat) }, /* 8 Latitude */
271 { UBLOX_DISCARD, 4 }, /* 12 Height above Ellipsoid */
272 { UBLOX_U32, offsetof(struct nav_posllh, alt_msl) }, /* 16 Height above mean sea level */
273 { UBLOX_DISCARD, 8 }, /* 20 hAcc, vAcc */
274 { UBLOX_END, 0 }, /* 28 */
278 ao_ublox_parse_nav_posllh(void)
280 ao_ublox_parse(&nav_posllh, nav_posllh_packet);
284 * NAV-SOL message parsing
286 static struct nav_sol {
292 static const struct ublox_packet_parse nav_sol_packet[] = {
293 { UBLOX_DISCARD, 10 }, /* 0 iTOW, fTOW, week */
294 { UBLOX_U8, offsetof(struct nav_sol, gps_fix) }, /* 10 gpsFix */
295 { UBLOX_U8, offsetof(struct nav_sol, flags) }, /* 11 flags */
296 { UBLOX_DISCARD, 35 }, /* 12 ecefX, ecefY, ecefZ, pAcc, ecefVX, ecefVY, ecefVZ, sAcc, pDOP, reserved1 */
297 { UBLOX_U8, offsetof(struct nav_sol, nsat) }, /* 47 numSV */
298 { UBLOX_DISCARD, 4 }, /* 48 reserved2 */
302 #define NAV_SOL_FLAGS_GPSFIXOK 0
303 #define NAV_SOL_FLAGS_DIFFSOLN 1
304 #define NAV_SOL_FLAGS_WKNSET 2
305 #define NAV_SOL_FLAGS_TOWSET 3
308 ao_ublox_parse_nav_sol(void)
310 ao_ublox_parse(&nav_sol, nav_sol_packet);
314 * NAV-SVINFO message parsing
317 static struct nav_svinfo {
322 static const struct ublox_packet_parse nav_svinfo_packet[] = {
323 { UBLOX_DISCARD, 4 }, /* 0 iTOW */
324 { UBLOX_U8, offsetof(struct nav_svinfo, num_ch) }, /* 4 numCh */
325 { UBLOX_U8, offsetof(struct nav_svinfo, flags) }, /* 5 globalFlags */
326 { UBLOX_DISCARD, 2 }, /* 6 reserved2 */
330 #define NAV_SVINFO_MAX_SAT 16
332 static struct nav_svinfo_sat {
338 } nav_svinfo_sat[NAV_SVINFO_MAX_SAT];
340 static uint8_t nav_svinfo_nsat;
342 static const struct ublox_packet_parse nav_svinfo_sat_packet[] = {
343 { UBLOX_U8, offsetof(struct nav_svinfo_sat, chn) }, /* 8 + 12*N chn */
344 { UBLOX_U8, offsetof(struct nav_svinfo_sat, svid) }, /* 9 + 12*N svid */
345 { UBLOX_U8, offsetof(struct nav_svinfo_sat, flags) }, /* 10 + 12*N flags */
346 { UBLOX_U8, offsetof(struct nav_svinfo_sat, quality) }, /* 11 + 12*N quality */
347 { UBLOX_U8, offsetof(struct nav_svinfo_sat, cno) }, /* 12 + 12*N cno */
348 { UBLOX_DISCARD, 7 }, /* 13 + 12*N elev, azim, prRes */
352 #define NAV_SVINFO_SAT_FLAGS_SVUSED 0
353 #define NAV_SVINFO_SAT_FLAGS_DIFFCORR 1
354 #define NAV_SVINFO_SAT_FLAGS_ORBITAVAIL 2
355 #define NAV_SVINFO_SAT_FLAGS_ORBITEPH 3
356 #define NAV_SVINFO_SAT_FLAGS_UNHEALTHY 4
357 #define NAV_SVINFO_SAT_FLAGS_ORBITALM 5
358 #define NAV_SVINFO_SAT_FLAGS_ORBITAOP 6
359 #define NAV_SVINFO_SAT_FLAGS_SMOOTHED 7
361 #define NAV_SVINFO_SAT_QUALITY_IDLE 0
362 #define NAV_SVINFO_SAT_QUALITY_SEARCHING 1
363 #define NAV_SVINFO_SAT_QUALITY_ACQUIRED 2
364 #define NAV_SVINFO_SAT_QUALITY_UNUSABLE 3
365 #define NAV_SVINFO_SAT_QUALITY_LOCKED 4
366 #define NAV_SVINFO_SAT_QUALITY_RUNNING 5
369 ao_ublox_parse_nav_svinfo(void)
374 ao_ublox_parse(&nav_svinfo, nav_svinfo_packet);
375 for (nsat = 0; nsat < nav_svinfo.num_ch && ao_ublox_len >= 12; nsat++) {
376 if (nsat < NAV_SVINFO_MAX_SAT) {
377 ao_ublox_parse(&nav_svinfo_sat[nav_svinfo_nsat++], nav_svinfo_sat_packet);
383 ao_gps_dbg(DBG_PROTO, "svinfo num_ch %d flags %02x\n", nav_svinfo.num_ch, nav_svinfo.flags);
384 for (nsat = 0; nsat < nav_svinfo.num_ch; nsat++)
385 ao_gps_dbg(DBG_PROTO, "\t%d: chn %d svid %d flags %02x quality %d cno %d\n",
387 nav_svinfo_sat[nsat].chn,
388 nav_svinfo_sat[nsat].svid,
389 nav_svinfo_sat[nsat].flags,
390 nav_svinfo_sat[nsat].quality,
391 nav_svinfo_sat[nsat].cno);
396 * NAV-TIMEUTC message parsing
398 static struct nav_timeutc {
408 #define NAV_TIMEUTC_VALID_TOW 0
409 #define NAV_TIMEUTC_VALID_WKN 1
410 #define NAV_TIMEUTC_VALID_UTC 2
412 static const struct ublox_packet_parse nav_timeutc_packet[] = {
413 { UBLOX_DISCARD, 12 }, /* 0 iTOW, tAcc, nano */
414 { UBLOX_U16, offsetof(struct nav_timeutc, year) }, /* 12 year */
415 { UBLOX_U8, offsetof(struct nav_timeutc, month) }, /* 14 month */
416 { UBLOX_U8, offsetof(struct nav_timeutc, day) }, /* 15 day */
417 { UBLOX_U8, offsetof(struct nav_timeutc, hour) }, /* 16 hour */
418 { UBLOX_U8, offsetof(struct nav_timeutc, min) }, /* 17 min */
419 { UBLOX_U8, offsetof(struct nav_timeutc, sec) }, /* 18 sec */
420 { UBLOX_U8, offsetof(struct nav_timeutc, valid) }, /* 19 valid */
425 ao_ublox_parse_nav_timeutc(void)
427 ao_ublox_parse(&nav_timeutc, nav_timeutc_packet);
431 * NAV-VELNED message parsing
434 static struct nav_velned {
440 static const struct ublox_packet_parse nav_velned_packet[] = {
441 { UBLOX_DISCARD, 12 }, /* 0 iTOW, velN, velE */
442 { UBLOX_U32, offsetof(struct nav_velned, vel_d) }, /* 12 velD */
443 { UBLOX_DISCARD, 4 }, /* 16 speed */
444 { UBLOX_U32, offsetof(struct nav_velned, g_speed) }, /* 20 gSpeed */
445 { UBLOX_U32, offsetof(struct nav_velned, heading) }, /* 24 heading */
446 { UBLOX_DISCARD, 8 }, /* 28 sAcc, cAcc */
451 ao_ublox_parse_nav_velned(void)
453 ao_ublox_parse(&nav_velned, nav_velned_packet);
457 * Set the protocol mode and baud rate
466 * A bunch of nulls so the start bit
470 for (i = 0; i < 64; i++)
471 ao_gps_putchar(0x00);
479 ao_delay(AO_SEC_TO_TICKS(3));
481 ao_gps_dbg(DBG_INIT, "Set speed 9600\n");
482 ao_gps_set_speed(AO_SERIAL_SPEED_9600);
485 * Send the baud-rate setting and protocol-setting
486 * command three times
488 for (k = 0; k < 3; k++) {
491 ao_gps_dbg(DBG_INIT, "Send initial setting\n");
492 for (i = 0; i < sizeof (ao_gps_set_nmea); i++)
493 ao_gps_putchar(ao_gps_set_nmea[i]);
498 #if AO_SERIAL_SPEED_UBLOX != AO_SERIAL_SPEED_9600
499 ao_gps_dbg(DBG_INIT, "Set speed high\n");
501 * Increase the baud rate
503 ao_gps_set_speed(AO_SERIAL_SPEED_UBLOX);
510 ao_ublox_putstart(uint8_t class, uint8_t id, uint16_t len)
512 ao_ublox_init_cksum();
513 ao_gps_putchar(0xb5);
514 ao_gps_putchar(0x62);
515 ao_ublox_put_u8(class);
517 ao_ublox_put_u8(len);
518 ao_ublox_put_u8(len >> 8);
522 ao_ublox_putend(void)
524 ao_gps_putchar(ao_ublox_cksum.a);
525 ao_gps_putchar(ao_ublox_cksum.b);
529 ao_ublox_set_message_rate(uint8_t class, uint8_t msgid, uint8_t rate)
531 ao_ublox_putstart(0x06, 0x01, 3);
532 ao_ublox_put_u8(class);
533 ao_ublox_put_u8(msgid);
534 ao_ublox_put_u8(rate);
539 ao_ublox_set_navigation_settings(uint16_t mask,
543 uint32_t fixed_alt_var,
550 uint8_t static_hold_thresh,
551 uint8_t dgps_time_out)
553 ao_ublox_putstart(UBLOX_CFG, UBLOX_CFG_NAV5, 36);
554 ao_ublox_put_u16(mask);
555 ao_ublox_put_u8(dyn_model);
556 ao_ublox_put_u8(fix_mode);
557 ao_ublox_put_i32(fixed_alt);
558 ao_ublox_put_u32(fixed_alt_var);
559 ao_ublox_put_i8(min_elev);
560 ao_ublox_put_u8(dr_limit);
561 ao_ublox_put_u16(pdop);
562 ao_ublox_put_u16(tdop);
563 ao_ublox_put_u16(pacc);
564 ao_ublox_put_u16(tacc);
565 ao_ublox_put_u8(static_hold_thresh);
566 ao_ublox_put_u8(dgps_time_out);
575 * Disable all MON message
577 static const uint8_t ublox_disable_mon[] = {
578 0x0b, 0x09, 0x02, 0x06, 0x07, 0x21, 0x08, 0x04
582 * Disable all NAV messages. The desired
583 * ones will be explicitly re-enabled
586 static const uint8_t ublox_disable_nav[] = {
587 0x60, 0x22, 0x31, 0x04, 0x40, 0x01, 0x02, 0x32,
588 0x06, 0x03, 0x30, 0x20, 0x21, 0x11, 0x12
592 * Enable enough messages to get all of the data we want
594 static const uint8_t ublox_enable_nav[] = {
604 ao_gps_set_rate(uint8_t rate)
607 for (i = 0; i < sizeof (ublox_enable_nav); i++)
608 ao_ublox_set_message_rate(UBLOX_NAV, ublox_enable_nav[i], rate);
612 ao_gps(void) __reentrant
615 struct ao_ublox_cksum cksum;
620 /* Disable all messages */
621 for (i = 0; i < sizeof (ublox_disable_mon); i++)
622 ao_ublox_set_message_rate(0x0a, ublox_disable_mon[i], 0);
623 for (i = 0; i < sizeof (ublox_disable_nav); i++)
624 ao_ublox_set_message_rate(UBLOX_NAV, ublox_disable_nav[i], 0);
626 /* Enable all of the messages we want */
629 ao_ublox_set_navigation_settings((1 << UBLOX_CFG_NAV5_MASK_DYN) | (1 << UBLOX_CFG_NAV5_MASK_FIXMODE),
630 UBLOX_CFG_NAV5_DYNMODEL_AIRBORNE_4G,
631 UBLOX_CFG_NAV5_FIXMODE_3D,
644 /* Locate the begining of the next record */
645 while (ao_ublox_byte() != (uint8_t) 0xb5)
647 if (ao_ublox_byte() != (uint8_t) 0x62)
650 ao_ublox_init_cksum();
652 class = header_byte();
656 ao_ublox_len = header_byte();
657 ao_ublox_len |= header_byte() << 8;
659 ao_gps_dbg(DBG_PROTO, "class %02x id %02x len %d\n", class, id, ao_ublox_len);
661 if (ao_ublox_len > 1023)
668 if (ao_ublox_len != 18)
670 ao_ublox_parse_nav_dop();
672 case UBLOX_NAV_POSLLH:
673 if (ao_ublox_len != 28)
675 ao_ublox_parse_nav_posllh();
678 if (ao_ublox_len != 52)
680 ao_ublox_parse_nav_sol();
682 case UBLOX_NAV_SVINFO:
683 if (ao_ublox_len < 8)
685 ao_ublox_parse_nav_svinfo();
687 case UBLOX_NAV_VELNED:
688 if (ao_ublox_len != 36)
690 ao_ublox_parse_nav_velned();
692 case UBLOX_NAV_TIMEUTC:
693 if (ao_ublox_len != 20)
695 ao_ublox_parse_nav_timeutc();
701 if (ao_ublox_len != 0) {
702 ao_gps_dbg(DBG_PROTO, "len left %d\n", ao_ublox_len);
706 /* verify checksum and end sequence */
707 cksum.a = ao_ublox_byte();
708 cksum.b = ao_ublox_byte();
709 if (ao_ublox_cksum.a != cksum.a || ao_ublox_cksum.b != cksum.b)
715 case UBLOX_NAV_TIMEUTC:
716 ao_mutex_get(&ao_gps_mutex);
717 ao_gps_tick = ao_time();
719 ao_gps_data.flags = 0;
720 ao_gps_data.flags |= AO_GPS_RUNNING;
721 if (nav_sol.gps_fix & (1 << NAV_SOL_FLAGS_GPSFIXOK)) {
722 uint8_t nsat = nav_sol.nsat;
723 ao_gps_data.flags |= AO_GPS_VALID | AO_GPS_COURSE_VALID;
726 ao_gps_data.flags |= nsat;
728 if (nav_timeutc.valid & (1 << NAV_TIMEUTC_VALID_UTC))
729 ao_gps_data.flags |= AO_GPS_DATE_VALID;
731 ao_gps_data.altitude = nav_posllh.alt_msl / 1000;
732 ao_gps_data.latitude = nav_posllh.lat;
733 ao_gps_data.longitude = nav_posllh.lon;
735 ao_gps_data.year = nav_timeutc.year - 2000;
736 ao_gps_data.month = nav_timeutc.month;
737 ao_gps_data.day = nav_timeutc.day;
739 ao_gps_data.hour = nav_timeutc.hour;
740 ao_gps_data.minute = nav_timeutc.min;
741 ao_gps_data.second = nav_timeutc.sec;
743 ao_gps_data.pdop = nav_dop.pdop;
744 ao_gps_data.hdop = nav_dop.hdop;
745 ao_gps_data.vdop = nav_dop.vdop;
747 /* mode is not set */
749 ao_gps_data.ground_speed = nav_velned.g_speed;
750 ao_gps_data.climb_rate = -nav_velned.vel_d;
751 ao_gps_data.course = nav_velned.heading / 200000;
753 ao_gps_tracking_data.channels = 0;
755 struct ao_telemetry_satellite_info *dst = &ao_gps_tracking_data.sats[0];
756 struct nav_svinfo_sat *src = &nav_svinfo_sat[0];
758 for (i = 0; i < nav_svinfo_nsat; i++) {
759 if (!(src->flags & (1 << NAV_SVINFO_SAT_FLAGS_UNHEALTHY)) &&
760 src->quality >= NAV_SVINFO_SAT_QUALITY_ACQUIRED)
762 if (ao_gps_tracking_data.channels < AO_TELEMETRY_SATELLITE_MAX_SAT) {
763 dst->svid = src->svid;
764 dst->c_n_1 = src->cno;
766 ao_gps_tracking_data.channels++;
772 ao_mutex_put(&ao_gps_mutex);
773 ao_gps_new = AO_GPS_NEW_DATA | AO_GPS_NEW_TRACKING;
774 ao_wakeup(&ao_gps_new);
783 static void ao_gps_option(void)
786 if (ao_cmd_status != ao_cmd_success) {
787 ao_cmd_status = ao_cmd_success;
790 ao_gps_dbg_enable = ao_cmd_lex_i;
791 printf ("gps debug set to %d\n", ao_gps_dbg_enable);
795 #define ao_gps_option ao_gps_show
798 __code struct ao_cmds ao_gps_cmds[] = {
799 { ao_gps_option, "g\0Display GPS" },
803 __xdata struct ao_task ao_gps_task;
808 ao_cmd_register(&ao_gps_cmds[0]);
809 ao_add_task(&ao_gps_task, ao_gps, "gps");