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; 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 #include "ao_gps_ublox.h"
25 #define AO_UBLOX_DEBUG 0
31 AO_TICK_TYPE ao_gps_tick;
32 struct ao_telemetry_location ao_gps_data;
33 struct ao_telemetry_satellite ao_gps_tracking_data;
35 #undef AO_SERIAL_SPEED_UBLOX
37 #ifndef AO_SERIAL_SPEED_UBLOX
38 #define AO_SERIAL_SPEED_UBLOX AO_SERIAL_SPEED_9600
41 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_57600
42 #define SERIAL_SPEED_STRING "57600"
43 #define SERIAL_SPEED_CHECKSUM "2d"
45 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_19200
46 #define SERIAL_SPEED_STRING "19200"
47 #define SERIAL_SPEED_CHECKSUM "23"
49 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_9600
50 #define SERIAL_SPEED_STRING "9600"
51 #define SERIAL_SPEED_CHECKSUM "16"
54 static const char ao_gps_set_nmea[] =
55 "\r\n$PUBX,41,1,3,1," SERIAL_SPEED_STRING ",0*" SERIAL_SPEED_CHECKSUM "\r\n";
57 struct ao_ublox_cksum {
61 static struct ao_ublox_cksum ao_ublox_cksum;
62 static uint16_t ao_ublox_len;
71 static uint8_t ao_gps_dbg_enable = DBG_PROTO|DBG_CHAR|DBG_INIT;
73 static void ao_gps_dbg(int level, char *format, ...) {
76 if (level & ao_gps_dbg_enable) {
85 #define ao_gps_dbg(fmt, ...)
88 static inline uint8_t ao_ublox_byte(void) {
89 uint8_t c = (uint8_t) ao_gps_getchar();
90 ao_gps_dbg(DBG_CHAR, " %02x", c);
94 static inline void add_cksum(struct ao_ublox_cksum *cksum, uint8_t c)
100 static void ao_ublox_init_cksum(void)
102 ao_ublox_cksum.a = ao_ublox_cksum.b = 0;
105 static void ao_ublox_put_u8(uint8_t c)
107 add_cksum(&ao_ublox_cksum, c);
108 ao_gps_dbg(DBG_CHAR, " (%02x)", c);
112 static void ao_ublox_put_i8(int8_t c)
114 ao_ublox_put_u8((uint8_t) c);
117 static void ao_ublox_put_u16(uint16_t c)
119 ao_ublox_put_u8((uint8_t) c);
120 ao_ublox_put_u8((uint8_t) (c>>8));
124 static void ao_ublox_put_i16(int16_t c)
126 ao_ublox_put_u16((uint16_t) c);
130 static void ao_ublox_put_u32(uint32_t c)
132 ao_ublox_put_u8((uint8_t) c);
133 ao_ublox_put_u8((uint8_t) (c>>8));
134 ao_ublox_put_u8((uint8_t) (c>>16));
135 ao_ublox_put_u8((uint8_t) (c>>24));
138 static void ao_ublox_put_i32(int32_t c)
140 ao_ublox_put_u32((uint32_t) c);
143 static uint8_t header_byte(void)
145 uint8_t c = ao_ublox_byte();
146 add_cksum(&ao_ublox_cksum, c);
150 static uint8_t data_byte(void)
153 return header_byte();
156 static char *ublox_target;
158 static void ublox_u16(uint8_t offset)
160 uint16_t *ptr = (uint16_t *) (void *) (ublox_target + offset);
164 val |= (uint16_t) ((uint16_t) data_byte () << 8);
168 static void ublox_u8(uint8_t offset)
170 uint8_t *ptr = (uint8_t *) (ublox_target + offset);
177 static void ublox_u32(uint8_t offset)
179 uint32_t *ptr = (uint32_t *) (void *) (ublox_target + offset);
182 val = ((uint32_t) data_byte ());
183 val |= ((uint32_t) data_byte ()) << 8;
184 val |= ((uint32_t) data_byte ()) << 16;
185 val |= ((uint32_t) data_byte ()) << 24;
189 static void ublox_discard(uint8_t len)
196 #define UBLOX_DISCARD 1
201 struct ublox_packet_parse {
207 ao_ublox_parse(void *target, const struct ublox_packet_parse *parse)
211 ublox_target = target;
213 offset = parse[i].offset;
214 switch (parse[i].type) {
218 ublox_discard(offset);
231 ao_gps_dbg(DBG_PROTO, "\n");
235 * NAV-DOP message parsing
238 static struct nav_dop {
244 static const struct ublox_packet_parse nav_dop_packet[] = {
245 { UBLOX_DISCARD, 6 }, /* 0 GPS Millisecond Time of Week, gDOP */
246 { UBLOX_U16, offsetof(struct nav_dop, pdop) }, /* 6 pDOP */
247 { UBLOX_DISCARD, 2 }, /* 8 tDOP */
248 { UBLOX_U16, offsetof(struct nav_dop, vdop) }, /* 10 vDOP */
249 { UBLOX_U16, offsetof(struct nav_dop, hdop) }, /* 12 hDOP */
250 { UBLOX_DISCARD, 4 }, /* 14 nDOP, eDOP */
255 ao_ublox_parse_nav_dop(void)
257 ao_ublox_parse(&nav_dop, nav_dop_packet);
261 * NAV-POSLLH message parsing
263 static struct nav_posllh {
269 static const struct ublox_packet_parse nav_posllh_packet[] = {
270 { UBLOX_DISCARD, 4 }, /* 0 GPS Millisecond Time of Week */
271 { UBLOX_U32, offsetof(struct nav_posllh, lon) }, /* 4 Longitude */
272 { UBLOX_U32, offsetof(struct nav_posllh, lat) }, /* 8 Latitude */
273 { UBLOX_DISCARD, 4 }, /* 12 Height above Ellipsoid */
274 { UBLOX_U32, offsetof(struct nav_posllh, alt_msl) }, /* 16 Height above mean sea level */
275 { UBLOX_DISCARD, 8 }, /* 20 hAcc, vAcc */
276 { UBLOX_END, 0 }, /* 28 */
280 ao_ublox_parse_nav_posllh(void)
282 ao_ublox_parse(&nav_posllh, nav_posllh_packet);
286 * NAV-SOL message parsing
288 static struct nav_sol {
294 static const struct ublox_packet_parse nav_sol_packet[] = {
295 { UBLOX_DISCARD, 10 }, /* 0 iTOW, fTOW, week */
296 { UBLOX_U8, offsetof(struct nav_sol, gps_fix) }, /* 10 gpsFix */
297 { UBLOX_U8, offsetof(struct nav_sol, flags) }, /* 11 flags */
298 { UBLOX_DISCARD, 35 }, /* 12 ecefX, ecefY, ecefZ, pAcc, ecefVX, ecefVY, ecefVZ, sAcc, pDOP, reserved1 */
299 { UBLOX_U8, offsetof(struct nav_sol, nsat) }, /* 47 numSV */
300 { UBLOX_DISCARD, 4 }, /* 48 reserved2 */
304 #define NAV_SOL_FLAGS_GPSFIXOK 0
305 #define NAV_SOL_FLAGS_DIFFSOLN 1
306 #define NAV_SOL_FLAGS_WKNSET 2
307 #define NAV_SOL_FLAGS_TOWSET 3
310 ao_ublox_parse_nav_sol(void)
312 ao_ublox_parse(&nav_sol, nav_sol_packet);
316 * NAV-SVINFO message parsing
319 static struct nav_svinfo {
324 static const struct ublox_packet_parse nav_svinfo_packet[] = {
325 { UBLOX_DISCARD, 4 }, /* 0 iTOW */
326 { UBLOX_U8, offsetof(struct nav_svinfo, num_ch) }, /* 4 numCh */
327 { UBLOX_U8, offsetof(struct nav_svinfo, flags) }, /* 5 globalFlags */
328 { UBLOX_DISCARD, 2 }, /* 6 reserved2 */
332 #define NAV_SVINFO_MAX_SAT 16
334 static struct nav_svinfo_sat {
340 } nav_svinfo_sat[NAV_SVINFO_MAX_SAT];
342 static uint8_t nav_svinfo_nsat;
344 static const struct ublox_packet_parse nav_svinfo_sat_packet[] = {
345 { UBLOX_U8, offsetof(struct nav_svinfo_sat, chn) }, /* 8 + 12*N chn */
346 { UBLOX_U8, offsetof(struct nav_svinfo_sat, svid) }, /* 9 + 12*N svid */
347 { UBLOX_U8, offsetof(struct nav_svinfo_sat, flags) }, /* 10 + 12*N flags */
348 { UBLOX_U8, offsetof(struct nav_svinfo_sat, quality) }, /* 11 + 12*N quality */
349 { UBLOX_U8, offsetof(struct nav_svinfo_sat, cno) }, /* 12 + 12*N cno */
350 { UBLOX_DISCARD, 7 }, /* 13 + 12*N elev, azim, prRes */
354 #define NAV_SVINFO_SAT_FLAGS_SVUSED 0
355 #define NAV_SVINFO_SAT_FLAGS_DIFFCORR 1
356 #define NAV_SVINFO_SAT_FLAGS_ORBITAVAIL 2
357 #define NAV_SVINFO_SAT_FLAGS_ORBITEPH 3
358 #define NAV_SVINFO_SAT_FLAGS_UNHEALTHY 4
359 #define NAV_SVINFO_SAT_FLAGS_ORBITALM 5
360 #define NAV_SVINFO_SAT_FLAGS_ORBITAOP 6
361 #define NAV_SVINFO_SAT_FLAGS_SMOOTHED 7
363 #define NAV_SVINFO_SAT_QUALITY_IDLE 0
364 #define NAV_SVINFO_SAT_QUALITY_SEARCHING 1
365 #define NAV_SVINFO_SAT_QUALITY_ACQUIRED 2
366 #define NAV_SVINFO_SAT_QUALITY_UNUSABLE 3
367 #define NAV_SVINFO_SAT_QUALITY_LOCKED 4
368 #define NAV_SVINFO_SAT_QUALITY_RUNNING 5
371 ao_ublox_parse_nav_svinfo(void)
376 ao_ublox_parse(&nav_svinfo, nav_svinfo_packet);
377 for (nsat = 0; nsat < nav_svinfo.num_ch && ao_ublox_len >= 12; nsat++) {
378 if (nsat < NAV_SVINFO_MAX_SAT) {
379 ao_ublox_parse(&nav_svinfo_sat[nav_svinfo_nsat++], nav_svinfo_sat_packet);
385 ao_gps_dbg(DBG_PROTO, "svinfo num_ch %d flags %02x\n", nav_svinfo.num_ch, nav_svinfo.flags);
386 for (nsat = 0; nsat < nav_svinfo.num_ch; nsat++)
387 ao_gps_dbg(DBG_PROTO, "\t%d: chn %d svid %d flags %02x quality %d cno %d\n",
389 nav_svinfo_sat[nsat].chn,
390 nav_svinfo_sat[nsat].svid,
391 nav_svinfo_sat[nsat].flags,
392 nav_svinfo_sat[nsat].quality,
393 nav_svinfo_sat[nsat].cno);
398 * NAV-TIMEUTC message parsing
400 static struct nav_timeutc {
410 #define NAV_TIMEUTC_VALID_TOW 0
411 #define NAV_TIMEUTC_VALID_WKN 1
412 #define NAV_TIMEUTC_VALID_UTC 2
414 static const struct ublox_packet_parse nav_timeutc_packet[] = {
415 { UBLOX_DISCARD, 12 }, /* 0 iTOW, tAcc, nano */
416 { UBLOX_U16, offsetof(struct nav_timeutc, year) }, /* 12 year */
417 { UBLOX_U8, offsetof(struct nav_timeutc, month) }, /* 14 month */
418 { UBLOX_U8, offsetof(struct nav_timeutc, day) }, /* 15 day */
419 { UBLOX_U8, offsetof(struct nav_timeutc, hour) }, /* 16 hour */
420 { UBLOX_U8, offsetof(struct nav_timeutc, min) }, /* 17 min */
421 { UBLOX_U8, offsetof(struct nav_timeutc, sec) }, /* 18 sec */
422 { UBLOX_U8, offsetof(struct nav_timeutc, valid) }, /* 19 valid */
427 ao_ublox_parse_nav_timeutc(void)
429 ao_ublox_parse(&nav_timeutc, nav_timeutc_packet);
433 * NAV-VELNED message parsing
436 static struct nav_velned {
442 static const struct ublox_packet_parse nav_velned_packet[] = {
443 { UBLOX_DISCARD, 12 }, /* 0 iTOW, velN, velE */
444 { UBLOX_U32, offsetof(struct nav_velned, vel_d) }, /* 12 velD */
445 { UBLOX_DISCARD, 4 }, /* 16 speed */
446 { UBLOX_U32, offsetof(struct nav_velned, g_speed) }, /* 20 gSpeed */
447 { UBLOX_U32, offsetof(struct nav_velned, heading) }, /* 24 heading */
448 { UBLOX_DISCARD, 8 }, /* 28 sAcc, cAcc */
453 ao_ublox_parse_nav_velned(void)
455 ao_ublox_parse(&nav_velned, nav_velned_packet);
459 * Set the protocol mode and baud rate
468 * A bunch of nulls so the start bit
472 for (i = 0; i < 64; i++)
473 ao_gps_putchar(0x00);
481 ao_delay(AO_SEC_TO_TICKS(3));
483 ao_gps_dbg(DBG_INIT, "Set speed 9600\n");
484 ao_gps_set_speed(AO_SERIAL_SPEED_9600);
487 * Send the baud-rate setting and protocol-setting
488 * command three times
490 for (k = 0; k < 3; k++) {
493 ao_gps_dbg(DBG_INIT, "Send initial setting\n");
494 for (i = 0; i < sizeof (ao_gps_set_nmea); i++)
495 ao_gps_putchar(ao_gps_set_nmea[i]);
500 #if AO_SERIAL_SPEED_UBLOX != AO_SERIAL_SPEED_9600
501 ao_gps_dbg(DBG_INIT, "Set speed high\n");
503 * Increase the baud rate
505 ao_gps_set_speed(AO_SERIAL_SPEED_UBLOX);
512 ao_ublox_putstart(uint8_t class, uint8_t id, uint16_t len)
514 ao_ublox_init_cksum();
515 ao_gps_putchar(0xb5);
516 ao_gps_putchar(0x62);
517 ao_ublox_put_u8(class);
519 ao_ublox_put_u8((uint8_t) len);
520 ao_ublox_put_u8((uint8_t) (len >> 8));
524 ao_ublox_putend(void)
526 ao_gps_putchar(ao_ublox_cksum.a);
527 ao_gps_putchar(ao_ublox_cksum.b);
531 ao_ublox_set_message_rate(uint8_t class, uint8_t msgid, uint8_t rate)
533 ao_ublox_putstart(0x06, 0x01, 3);
534 ao_ublox_put_u8(class);
535 ao_ublox_put_u8(msgid);
536 ao_ublox_put_u8(rate);
541 ao_ublox_set_navigation_settings(uint16_t mask,
545 uint32_t fixed_alt_var,
552 uint8_t static_hold_thresh,
553 uint8_t dgps_time_out)
555 ao_ublox_putstart(UBLOX_CFG, UBLOX_CFG_NAV5, 36);
556 ao_ublox_put_u16(mask);
557 ao_ublox_put_u8(dyn_model);
558 ao_ublox_put_u8(fix_mode);
559 ao_ublox_put_i32(fixed_alt);
560 ao_ublox_put_u32(fixed_alt_var);
561 ao_ublox_put_i8(min_elev);
562 ao_ublox_put_u8(dr_limit);
563 ao_ublox_put_u16(pdop);
564 ao_ublox_put_u16(tdop);
565 ao_ublox_put_u16(pacc);
566 ao_ublox_put_u16(tacc);
567 ao_ublox_put_u8(static_hold_thresh);
568 ao_ublox_put_u8(dgps_time_out);
577 * Disable all MON message
579 static const uint8_t ublox_disable_mon[] = {
580 0x0b, 0x09, 0x02, 0x06, 0x07, 0x21, 0x08, 0x04
584 * Disable all NAV messages. The desired
585 * ones will be explicitly re-enabled
588 static const uint8_t ublox_disable_nav[] = {
589 0x60, 0x22, 0x31, 0x04, 0x40, 0x01, 0x02, 0x32,
590 0x06, 0x03, 0x30, 0x20, 0x21, 0x11, 0x12
594 * Enable enough messages to get all of the data we want
596 static const uint8_t ublox_enable_nav[] = {
606 ao_gps_set_rate(uint8_t rate)
609 for (i = 0; i < sizeof (ublox_enable_nav); i++)
610 ao_ublox_set_message_rate(UBLOX_NAV, ublox_enable_nav[i], rate);
617 struct ao_ublox_cksum cksum;
619 AO_TICK_TYPE packet_start_tick;
620 AO_TICK_TYPE solution_tick = 0;
624 /* Disable all messages */
625 for (i = 0; i < sizeof (ublox_disable_mon); i++)
626 ao_ublox_set_message_rate(0x0a, ublox_disable_mon[i], 0);
627 for (i = 0; i < sizeof (ublox_disable_nav); i++)
628 ao_ublox_set_message_rate(UBLOX_NAV, ublox_disable_nav[i], 0);
630 /* Enable all of the messages we want */
633 ao_ublox_set_navigation_settings((1 << UBLOX_CFG_NAV5_MASK_DYN) | (1 << UBLOX_CFG_NAV5_MASK_FIXMODE),
634 UBLOX_CFG_NAV5_DYNMODEL_AIRBORNE_4G,
635 UBLOX_CFG_NAV5_FIXMODE_3D,
648 /* Locate the begining of the next record */
649 while (ao_ublox_byte() != (uint8_t) 0xb5)
651 packet_start_tick = ao_tick_count;
653 if (ao_ublox_byte() != (uint8_t) 0x62)
656 ao_ublox_init_cksum();
658 class = header_byte();
662 ao_ublox_len = header_byte();
663 ao_ublox_len |= (uint16_t) ((uint16_t) header_byte() << 8);
665 ao_gps_dbg(DBG_PROTO, "%6u class %02x id %02x len %d\n", packet_start_tick, class, id, ao_ublox_len);
667 if (ao_ublox_len > 1023)
674 if (ao_ublox_len != 18)
676 ao_ublox_parse_nav_dop();
678 case UBLOX_NAV_POSLLH:
679 if (ao_ublox_len != 28)
681 ao_ublox_parse_nav_posllh();
684 if (ao_ublox_len != 52)
686 ao_ublox_parse_nav_sol();
687 solution_tick = packet_start_tick;
689 case UBLOX_NAV_SVINFO:
690 if (ao_ublox_len < 8)
692 ao_ublox_parse_nav_svinfo();
694 case UBLOX_NAV_VELNED:
695 if (ao_ublox_len != 36)
697 ao_ublox_parse_nav_velned();
699 case UBLOX_NAV_TIMEUTC:
700 if (ao_ublox_len != 20)
702 ao_ublox_parse_nav_timeutc();
708 if (ao_ublox_len != 0) {
709 ao_gps_dbg(DBG_PROTO, "len left %d\n", ao_ublox_len);
713 /* verify checksum and end sequence */
714 cksum.a = ao_ublox_byte();
715 cksum.b = ao_ublox_byte();
716 if (ao_ublox_cksum.a != cksum.a || ao_ublox_cksum.b != cksum.b)
722 case UBLOX_NAV_TIMEUTC:
723 ao_mutex_get(&ao_gps_mutex);
724 ao_gps_tick = solution_tick;
726 ao_gps_data.flags = 0;
727 ao_gps_data.flags |= AO_GPS_RUNNING;
728 if (nav_sol.gps_fix & (1 << NAV_SOL_FLAGS_GPSFIXOK)) {
729 uint8_t nsat = nav_sol.nsat;
730 ao_gps_data.flags |= AO_GPS_VALID | AO_GPS_COURSE_VALID;
733 ao_gps_data.flags |= nsat;
735 if (nav_timeutc.valid & (1 << NAV_TIMEUTC_VALID_UTC))
736 ao_gps_data.flags |= AO_GPS_DATE_VALID;
738 AO_TELEMETRY_LOCATION_SET_ALTITUDE(&ao_gps_data, nav_posllh.alt_msl / 1000);
739 ao_gps_data.latitude = nav_posllh.lat;
740 ao_gps_data.longitude = nav_posllh.lon;
742 ao_gps_data.year = (uint8_t) (nav_timeutc.year - 2000);
743 ao_gps_data.month = nav_timeutc.month;
744 ao_gps_data.day = nav_timeutc.day;
746 ao_gps_data.hour = nav_timeutc.hour;
747 ao_gps_data.minute = nav_timeutc.min;
748 ao_gps_data.second = nav_timeutc.sec;
750 /* we report dop scaled by 10, but ublox provides dop scaled by 100
752 ao_gps_data.pdop = (uint8_t) (nav_dop.pdop / 10);
753 ao_gps_data.hdop = (uint8_t) (nav_dop.hdop / 10);
754 ao_gps_data.vdop = (uint8_t) (nav_dop.vdop / 10);
756 ao_gps_data.ground_speed = (uint16_t) nav_velned.g_speed;
757 ao_gps_data.climb_rate = -(int16_t) nav_velned.vel_d;
758 ao_gps_data.course = (uint8_t) (nav_velned.heading / 200000);
760 ao_gps_tracking_data.channels = 0;
762 struct ao_telemetry_satellite_info *dst = &ao_gps_tracking_data.sats[0];
763 struct nav_svinfo_sat *src = &nav_svinfo_sat[0];
765 for (i = 0; i < nav_svinfo_nsat; i++) {
766 if (!(src->flags & (1 << NAV_SVINFO_SAT_FLAGS_UNHEALTHY)) &&
767 src->quality >= NAV_SVINFO_SAT_QUALITY_ACQUIRED)
769 if (ao_gps_tracking_data.channels < AO_TELEMETRY_SATELLITE_MAX_SAT) {
770 dst->svid = src->svid;
771 dst->c_n_1 = src->cno;
773 ao_gps_tracking_data.channels++;
779 ao_mutex_put(&ao_gps_mutex);
780 ao_gps_new = AO_GPS_NEW_DATA | AO_GPS_NEW_TRACKING;
781 ao_wakeup(&ao_gps_new);
790 static void ao_gps_option(void)
792 uint8_t r = (uint8_t) ao_cmd_hex();
793 if (ao_cmd_status != ao_cmd_success) {
794 ao_cmd_status = ao_cmd_success;
797 ao_gps_dbg_enable = r;
798 printf ("gps debug set to %d\n", ao_gps_dbg_enable);
802 #define ao_gps_option ao_gps_show
805 const struct ao_cmds ao_gps_cmds[] = {
806 { ao_gps_option, "g\0Display GPS" },
810 struct ao_task ao_gps_task;
815 ao_cmd_register(&ao_gps_cmds[0]);
816 ao_add_task(&ao_gps_task, ao_gps, "gps");