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;
66 static uint8_t ao_gps_dbg_enable;
72 static void ao_gps_dbg(int level, char *format, ...) {
75 if (level & ao_gps_dbg_enable) {
84 #define ao_gps_dbg(fmt, ...)
87 static inline uint8_t ao_ublox_byte(void) {
88 uint8_t c = (uint8_t) ao_gps_getchar();
89 ao_gps_dbg(DBG_CHAR, " %02x", c);
93 static inline void add_cksum(struct ao_ublox_cksum *cksum, uint8_t c)
99 static void ao_ublox_init_cksum(void)
101 ao_ublox_cksum.a = ao_ublox_cksum.b = 0;
104 static void ao_ublox_put_u8(uint8_t c)
106 add_cksum(&ao_ublox_cksum, c);
107 ao_gps_dbg(DBG_CHAR, " (%02x)", c);
111 static void ao_ublox_put_i8(int8_t c)
113 ao_ublox_put_u8((uint8_t) c);
116 static void ao_ublox_put_u16(uint16_t c)
118 ao_ublox_put_u8((uint8_t) c);
119 ao_ublox_put_u8((uint8_t) (c>>8));
123 static void ao_ublox_put_i16(int16_t c)
125 ao_ublox_put_u16((uint16_t) c);
129 static void ao_ublox_put_u32(uint32_t c)
131 ao_ublox_put_u8((uint8_t) c);
132 ao_ublox_put_u8((uint8_t) (c>>8));
133 ao_ublox_put_u8((uint8_t) (c>>16));
134 ao_ublox_put_u8((uint8_t) (c>>24));
137 static void ao_ublox_put_i32(int32_t c)
139 ao_ublox_put_u32((uint32_t) c);
142 static uint8_t header_byte(void)
144 uint8_t c = ao_ublox_byte();
145 add_cksum(&ao_ublox_cksum, c);
149 static uint8_t data_byte(void)
152 return header_byte();
155 static char *ublox_target;
157 static void ublox_u16(uint8_t offset)
159 uint16_t *ptr = (uint16_t *) (void *) (ublox_target + offset);
163 val |= (uint16_t) ((uint16_t) data_byte () << 8);
167 static void ublox_u8(uint8_t offset)
169 uint8_t *ptr = (uint8_t *) (ublox_target + offset);
176 static void ublox_u32(uint8_t offset)
178 uint32_t *ptr = (uint32_t *) (void *) (ublox_target + offset);
181 val = ((uint32_t) data_byte ());
182 val |= ((uint32_t) data_byte ()) << 8;
183 val |= ((uint32_t) data_byte ()) << 16;
184 val |= ((uint32_t) data_byte ()) << 24;
188 static void ublox_discard(uint8_t len)
195 #define UBLOX_DISCARD 1
200 struct ublox_packet_parse {
206 ao_ublox_parse(void *target, const struct ublox_packet_parse *parse)
210 ublox_target = target;
212 offset = parse[i].offset;
213 switch (parse[i].type) {
217 ublox_discard(offset);
230 ao_gps_dbg(DBG_PROTO, "\n");
234 * NAV-DOP message parsing
237 static struct nav_dop {
243 static const struct ublox_packet_parse nav_dop_packet[] = {
244 { UBLOX_DISCARD, 6 }, /* 0 GPS Millisecond Time of Week, gDOP */
245 { UBLOX_U16, offsetof(struct nav_dop, pdop) }, /* 6 pDOP */
246 { UBLOX_DISCARD, 2 }, /* 8 tDOP */
247 { UBLOX_U16, offsetof(struct nav_dop, vdop) }, /* 10 vDOP */
248 { UBLOX_U16, offsetof(struct nav_dop, hdop) }, /* 12 hDOP */
249 { UBLOX_DISCARD, 4 }, /* 14 nDOP, eDOP */
254 ao_ublox_parse_nav_dop(void)
256 ao_ublox_parse(&nav_dop, nav_dop_packet);
260 * NAV-POSLLH message parsing
262 static struct nav_posllh {
268 static const struct ublox_packet_parse nav_posllh_packet[] = {
269 { UBLOX_DISCARD, 4 }, /* 0 GPS Millisecond Time of Week */
270 { UBLOX_U32, offsetof(struct nav_posllh, lon) }, /* 4 Longitude */
271 { UBLOX_U32, offsetof(struct nav_posllh, lat) }, /* 8 Latitude */
272 { UBLOX_DISCARD, 4 }, /* 12 Height above Ellipsoid */
273 { UBLOX_U32, offsetof(struct nav_posllh, alt_msl) }, /* 16 Height above mean sea level */
274 { UBLOX_DISCARD, 8 }, /* 20 hAcc, vAcc */
275 { UBLOX_END, 0 }, /* 28 */
279 ao_ublox_parse_nav_posllh(void)
281 ao_ublox_parse(&nav_posllh, nav_posllh_packet);
285 * NAV-SOL message parsing
287 static struct nav_sol {
293 static const struct ublox_packet_parse nav_sol_packet[] = {
294 { UBLOX_DISCARD, 10 }, /* 0 iTOW, fTOW, week */
295 { UBLOX_U8, offsetof(struct nav_sol, gps_fix) }, /* 10 gpsFix */
296 { UBLOX_U8, offsetof(struct nav_sol, flags) }, /* 11 flags */
297 { UBLOX_DISCARD, 35 }, /* 12 ecefX, ecefY, ecefZ, pAcc, ecefVX, ecefVY, ecefVZ, sAcc, pDOP, reserved1 */
298 { UBLOX_U8, offsetof(struct nav_sol, nsat) }, /* 47 numSV */
299 { UBLOX_DISCARD, 4 }, /* 48 reserved2 */
303 #define NAV_SOL_FLAGS_GPSFIXOK 0
304 #define NAV_SOL_FLAGS_DIFFSOLN 1
305 #define NAV_SOL_FLAGS_WKNSET 2
306 #define NAV_SOL_FLAGS_TOWSET 3
309 ao_ublox_parse_nav_sol(void)
311 ao_ublox_parse(&nav_sol, nav_sol_packet);
315 * NAV-SVINFO message parsing
318 static struct nav_svinfo {
323 static const struct ublox_packet_parse nav_svinfo_packet[] = {
324 { UBLOX_DISCARD, 4 }, /* 0 iTOW */
325 { UBLOX_U8, offsetof(struct nav_svinfo, num_ch) }, /* 4 numCh */
326 { UBLOX_U8, offsetof(struct nav_svinfo, flags) }, /* 5 globalFlags */
327 { UBLOX_DISCARD, 2 }, /* 6 reserved2 */
331 #define NAV_SVINFO_MAX_SAT 16
333 static struct nav_svinfo_sat {
339 } nav_svinfo_sat[NAV_SVINFO_MAX_SAT];
341 static uint8_t nav_svinfo_nsat;
343 static const struct ublox_packet_parse nav_svinfo_sat_packet[] = {
344 { UBLOX_U8, offsetof(struct nav_svinfo_sat, chn) }, /* 8 + 12*N chn */
345 { UBLOX_U8, offsetof(struct nav_svinfo_sat, svid) }, /* 9 + 12*N svid */
346 { UBLOX_U8, offsetof(struct nav_svinfo_sat, flags) }, /* 10 + 12*N flags */
347 { UBLOX_U8, offsetof(struct nav_svinfo_sat, quality) }, /* 11 + 12*N quality */
348 { UBLOX_U8, offsetof(struct nav_svinfo_sat, cno) }, /* 12 + 12*N cno */
349 { UBLOX_DISCARD, 7 }, /* 13 + 12*N elev, azim, prRes */
353 #define NAV_SVINFO_SAT_FLAGS_SVUSED 0
354 #define NAV_SVINFO_SAT_FLAGS_DIFFCORR 1
355 #define NAV_SVINFO_SAT_FLAGS_ORBITAVAIL 2
356 #define NAV_SVINFO_SAT_FLAGS_ORBITEPH 3
357 #define NAV_SVINFO_SAT_FLAGS_UNHEALTHY 4
358 #define NAV_SVINFO_SAT_FLAGS_ORBITALM 5
359 #define NAV_SVINFO_SAT_FLAGS_ORBITAOP 6
360 #define NAV_SVINFO_SAT_FLAGS_SMOOTHED 7
362 #define NAV_SVINFO_SAT_QUALITY_IDLE 0
363 #define NAV_SVINFO_SAT_QUALITY_SEARCHING 1
364 #define NAV_SVINFO_SAT_QUALITY_ACQUIRED 2
365 #define NAV_SVINFO_SAT_QUALITY_UNUSABLE 3
366 #define NAV_SVINFO_SAT_QUALITY_LOCKED 4
367 #define NAV_SVINFO_SAT_QUALITY_RUNNING 5
370 ao_ublox_parse_nav_svinfo(void)
375 ao_ublox_parse(&nav_svinfo, nav_svinfo_packet);
376 for (nsat = 0; nsat < nav_svinfo.num_ch && ao_ublox_len >= 12; nsat++) {
377 if (nsat < NAV_SVINFO_MAX_SAT) {
378 ao_ublox_parse(&nav_svinfo_sat[nav_svinfo_nsat++], nav_svinfo_sat_packet);
384 ao_gps_dbg(DBG_PROTO, "svinfo num_ch %d flags %02x\n", nav_svinfo.num_ch, nav_svinfo.flags);
385 for (nsat = 0; nsat < nav_svinfo.num_ch; nsat++)
386 ao_gps_dbg(DBG_PROTO, "\t%d: chn %d svid %d flags %02x quality %d cno %d\n",
388 nav_svinfo_sat[nsat].chn,
389 nav_svinfo_sat[nsat].svid,
390 nav_svinfo_sat[nsat].flags,
391 nav_svinfo_sat[nsat].quality,
392 nav_svinfo_sat[nsat].cno);
397 * NAV-TIMEUTC message parsing
399 static struct nav_timeutc {
409 #define NAV_TIMEUTC_VALID_TOW 0
410 #define NAV_TIMEUTC_VALID_WKN 1
411 #define NAV_TIMEUTC_VALID_UTC 2
413 static const struct ublox_packet_parse nav_timeutc_packet[] = {
414 { UBLOX_DISCARD, 12 }, /* 0 iTOW, tAcc, nano */
415 { UBLOX_U16, offsetof(struct nav_timeutc, year) }, /* 12 year */
416 { UBLOX_U8, offsetof(struct nav_timeutc, month) }, /* 14 month */
417 { UBLOX_U8, offsetof(struct nav_timeutc, day) }, /* 15 day */
418 { UBLOX_U8, offsetof(struct nav_timeutc, hour) }, /* 16 hour */
419 { UBLOX_U8, offsetof(struct nav_timeutc, min) }, /* 17 min */
420 { UBLOX_U8, offsetof(struct nav_timeutc, sec) }, /* 18 sec */
421 { UBLOX_U8, offsetof(struct nav_timeutc, valid) }, /* 19 valid */
426 ao_ublox_parse_nav_timeutc(void)
428 ao_ublox_parse(&nav_timeutc, nav_timeutc_packet);
432 * NAV-VELNED message parsing
435 static struct nav_velned {
441 static const struct ublox_packet_parse nav_velned_packet[] = {
442 { UBLOX_DISCARD, 12 }, /* 0 iTOW, velN, velE */
443 { UBLOX_U32, offsetof(struct nav_velned, vel_d) }, /* 12 velD */
444 { UBLOX_DISCARD, 4 }, /* 16 speed */
445 { UBLOX_U32, offsetof(struct nav_velned, g_speed) }, /* 20 gSpeed */
446 { UBLOX_U32, offsetof(struct nav_velned, heading) }, /* 24 heading */
447 { UBLOX_DISCARD, 8 }, /* 28 sAcc, cAcc */
452 ao_ublox_parse_nav_velned(void)
454 ao_ublox_parse(&nav_velned, nav_velned_packet);
458 * Set the protocol mode and baud rate
467 * A bunch of nulls so the start bit
471 for (i = 0; i < 64; i++)
472 ao_gps_putchar(0x00);
480 ao_delay(AO_SEC_TO_TICKS(3));
482 ao_gps_dbg(DBG_INIT, "Set speed 9600\n");
483 ao_gps_set_speed(AO_SERIAL_SPEED_9600);
486 * Send the baud-rate setting and protocol-setting
487 * command three times
489 for (k = 0; k < 3; k++) {
492 ao_gps_dbg(DBG_INIT, "Send initial setting\n");
493 for (i = 0; i < sizeof (ao_gps_set_nmea); i++)
494 ao_gps_putchar(ao_gps_set_nmea[i]);
499 #if AO_SERIAL_SPEED_UBLOX != AO_SERIAL_SPEED_9600
500 ao_gps_dbg(DBG_INIT, "Set speed high\n");
502 * Increase the baud rate
504 ao_gps_set_speed(AO_SERIAL_SPEED_UBLOX);
511 ao_ublox_putstart(uint8_t class, uint8_t id, uint16_t len)
513 ao_ublox_init_cksum();
514 ao_gps_putchar(0xb5);
515 ao_gps_putchar(0x62);
516 ao_ublox_put_u8(class);
518 ao_ublox_put_u8((uint8_t) len);
519 ao_ublox_put_u8((uint8_t) (len >> 8));
523 ao_ublox_putend(void)
525 ao_gps_putchar(ao_ublox_cksum.a);
526 ao_gps_putchar(ao_ublox_cksum.b);
530 ao_ublox_set_message_rate(uint8_t class, uint8_t msgid, uint8_t rate)
532 ao_ublox_putstart(0x06, 0x01, 3);
533 ao_ublox_put_u8(class);
534 ao_ublox_put_u8(msgid);
535 ao_ublox_put_u8(rate);
540 ao_ublox_set_navigation_settings(uint16_t mask,
544 uint32_t fixed_alt_var,
551 uint8_t static_hold_thresh,
552 uint8_t dgps_time_out)
554 ao_ublox_putstart(UBLOX_CFG, UBLOX_CFG_NAV5, 36);
555 ao_ublox_put_u16(mask);
556 ao_ublox_put_u8(dyn_model);
557 ao_ublox_put_u8(fix_mode);
558 ao_ublox_put_i32(fixed_alt);
559 ao_ublox_put_u32(fixed_alt_var);
560 ao_ublox_put_i8(min_elev);
561 ao_ublox_put_u8(dr_limit);
562 ao_ublox_put_u16(pdop);
563 ao_ublox_put_u16(tdop);
564 ao_ublox_put_u16(pacc);
565 ao_ublox_put_u16(tacc);
566 ao_ublox_put_u8(static_hold_thresh);
567 ao_ublox_put_u8(dgps_time_out);
576 * Disable all MON message
578 static const uint8_t ublox_disable_mon[] = {
579 0x0b, 0x09, 0x02, 0x06, 0x07, 0x21, 0x08, 0x04
583 * Disable all NAV messages. The desired
584 * ones will be explicitly re-enabled
587 static const uint8_t ublox_disable_nav[] = {
588 0x60, 0x22, 0x31, 0x04, 0x40, 0x01, 0x02, 0x32,
589 0x06, 0x03, 0x30, 0x20, 0x21, 0x11, 0x12
593 * Enable enough messages to get all of the data we want
595 static const uint8_t ublox_enable_nav[] = {
605 ao_gps_set_rate(uint8_t rate)
608 for (i = 0; i < sizeof (ublox_enable_nav); i++)
609 ao_ublox_set_message_rate(UBLOX_NAV, ublox_enable_nav[i], rate);
616 struct ao_ublox_cksum cksum;
618 AO_TICK_TYPE packet_start_tick;
619 AO_TICK_TYPE solution_tick = 0;
623 /* Disable all messages */
624 for (i = 0; i < sizeof (ublox_disable_mon); i++)
625 ao_ublox_set_message_rate(0x0a, ublox_disable_mon[i], 0);
626 for (i = 0; i < sizeof (ublox_disable_nav); i++)
627 ao_ublox_set_message_rate(UBLOX_NAV, ublox_disable_nav[i], 0);
629 /* Enable all of the messages we want */
632 ao_ublox_set_navigation_settings((1 << UBLOX_CFG_NAV5_MASK_DYN) | (1 << UBLOX_CFG_NAV5_MASK_FIXMODE),
633 UBLOX_CFG_NAV5_DYNMODEL_AIRBORNE_4G,
634 UBLOX_CFG_NAV5_FIXMODE_3D,
647 /* Locate the begining of the next record */
648 while (ao_ublox_byte() != (uint8_t) 0xb5)
650 packet_start_tick = ao_tick_count;
652 if (ao_ublox_byte() != (uint8_t) 0x62)
655 ao_ublox_init_cksum();
657 class = header_byte();
661 ao_ublox_len = header_byte();
662 ao_ublox_len |= (uint16_t) ((uint16_t) header_byte() << 8);
664 ao_gps_dbg(DBG_PROTO, "%6u class %02x id %02x len %d\n", packet_start_tick, class, id, ao_ublox_len);
666 if (ao_ublox_len > 1023)
673 if (ao_ublox_len != 18)
675 ao_ublox_parse_nav_dop();
677 case UBLOX_NAV_POSLLH:
678 if (ao_ublox_len != 28)
680 ao_ublox_parse_nav_posllh();
683 if (ao_ublox_len != 52)
685 ao_ublox_parse_nav_sol();
686 solution_tick = packet_start_tick;
688 case UBLOX_NAV_SVINFO:
689 if (ao_ublox_len < 8)
691 ao_ublox_parse_nav_svinfo();
693 case UBLOX_NAV_VELNED:
694 if (ao_ublox_len != 36)
696 ao_ublox_parse_nav_velned();
698 case UBLOX_NAV_TIMEUTC:
699 if (ao_ublox_len != 20)
701 ao_ublox_parse_nav_timeutc();
707 if (ao_ublox_len != 0) {
708 ao_gps_dbg(DBG_PROTO, "len left %d\n", ao_ublox_len);
712 /* verify checksum and end sequence */
713 cksum.a = ao_ublox_byte();
714 cksum.b = ao_ublox_byte();
715 if (ao_ublox_cksum.a != cksum.a || ao_ublox_cksum.b != cksum.b)
721 case UBLOX_NAV_TIMEUTC:
722 ao_mutex_get(&ao_gps_mutex);
723 ao_gps_tick = solution_tick;
725 ao_gps_data.flags = 0;
726 ao_gps_data.flags |= AO_GPS_RUNNING;
727 if (nav_sol.gps_fix & (1 << NAV_SOL_FLAGS_GPSFIXOK)) {
728 uint8_t nsat = nav_sol.nsat;
729 ao_gps_data.flags |= AO_GPS_VALID | AO_GPS_COURSE_VALID;
732 ao_gps_data.flags |= nsat;
734 if (nav_timeutc.valid & (1 << NAV_TIMEUTC_VALID_UTC))
735 ao_gps_data.flags |= AO_GPS_DATE_VALID;
737 AO_TELEMETRY_LOCATION_SET_ALTITUDE(&ao_gps_data, nav_posllh.alt_msl / 1000);
738 ao_gps_data.latitude = nav_posllh.lat;
739 ao_gps_data.longitude = nav_posllh.lon;
741 ao_gps_data.year = (uint8_t) (nav_timeutc.year - 2000);
742 ao_gps_data.month = nav_timeutc.month;
743 ao_gps_data.day = nav_timeutc.day;
745 ao_gps_data.hour = nav_timeutc.hour;
746 ao_gps_data.minute = nav_timeutc.min;
747 ao_gps_data.second = nav_timeutc.sec;
749 /* we report dop scaled by 10, but ublox provides dop scaled by 100
751 ao_gps_data.pdop = (uint8_t) (nav_dop.pdop / 10);
752 ao_gps_data.hdop = (uint8_t) (nav_dop.hdop / 10);
753 ao_gps_data.vdop = (uint8_t) (nav_dop.vdop / 10);
755 ao_gps_data.ground_speed = (uint16_t) nav_velned.g_speed;
756 ao_gps_data.climb_rate = -(int16_t) nav_velned.vel_d;
757 ao_gps_data.course = (uint8_t) (nav_velned.heading / 200000);
759 ao_gps_tracking_data.channels = 0;
761 struct ao_telemetry_satellite_info *dst = &ao_gps_tracking_data.sats[0];
762 struct nav_svinfo_sat *src = &nav_svinfo_sat[0];
764 for (i = 0; i < nav_svinfo_nsat; i++) {
765 if (!(src->flags & (1 << NAV_SVINFO_SAT_FLAGS_UNHEALTHY)) &&
766 src->quality >= NAV_SVINFO_SAT_QUALITY_ACQUIRED)
768 if (ao_gps_tracking_data.channels < AO_TELEMETRY_SATELLITE_MAX_SAT) {
769 dst->svid = src->svid;
770 dst->c_n_1 = src->cno;
772 ao_gps_tracking_data.channels++;
778 ao_mutex_put(&ao_gps_mutex);
779 ao_gps_new = AO_GPS_NEW_DATA | AO_GPS_NEW_TRACKING;
780 ao_wakeup(&ao_gps_new);
789 static void ao_gps_option(void)
791 uint16_t r = ao_cmd_hex();
792 if (ao_cmd_status != ao_cmd_success) {
793 ao_cmd_status = ao_cmd_success;
796 ao_gps_dbg_enable = r;
797 printf ("gps debug set to %d\n", ao_gps_dbg_enable);
801 #define ao_gps_option ao_gps_show
804 const struct ao_cmds ao_gps_cmds[] = {
805 { ao_gps_option, "g\0Display GPS" },
809 struct ao_task ao_gps_task;
814 ao_cmd_register(&ao_gps_cmds[0]);
815 ao_add_task(&ao_gps_task, ao_gps, "gps");