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
29 __xdata uint8_t ao_gps_new;
30 __xdata uint8_t ao_gps_mutex;
31 __pdata uint16_t ao_gps_tick;
32 __xdata struct ao_telemetry_location ao_gps_data;
33 __xdata 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 __pdata struct ao_ublox_cksum ao_ublox_cksum;
62 static __pdata 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)
119 ao_ublox_put_u8(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)
132 ao_ublox_put_u8(c>>8);
133 ao_ublox_put_u8(c>>16);
134 ao_ublox_put_u8(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 __xdata *ublox_target;
157 static void ublox_u16(uint8_t offset)
159 uint16_t __xdata *ptr = (uint16_t __xdata *) (ublox_target + offset);
163 val |= data_byte () << 8;
167 static void ublox_u8(uint8_t offset)
169 uint8_t __xdata *ptr = (uint8_t __xdata *) (ublox_target + offset);
176 static void ublox_u32(uint8_t offset) __reentrant
178 uint32_t __xdata *ptr = (uint32_t __xdata *) (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 __xdata *target, const struct ublox_packet_parse *parse) __reentrant
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(len);
519 ao_ublox_put_u8(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);
613 ao_gps(void) __reentrant
616 struct ao_ublox_cksum cksum;
621 /* Disable all messages */
622 for (i = 0; i < sizeof (ublox_disable_mon); i++)
623 ao_ublox_set_message_rate(0x0a, ublox_disable_mon[i], 0);
624 for (i = 0; i < sizeof (ublox_disable_nav); i++)
625 ao_ublox_set_message_rate(UBLOX_NAV, ublox_disable_nav[i], 0);
627 /* Enable all of the messages we want */
630 ao_ublox_set_navigation_settings((1 << UBLOX_CFG_NAV5_MASK_DYN) | (1 << UBLOX_CFG_NAV5_MASK_FIXMODE),
631 UBLOX_CFG_NAV5_DYNMODEL_AIRBORNE_4G,
632 UBLOX_CFG_NAV5_FIXMODE_3D,
645 /* Locate the begining of the next record */
646 while (ao_ublox_byte() != (uint8_t) 0xb5)
648 if (ao_ublox_byte() != (uint8_t) 0x62)
651 ao_ublox_init_cksum();
653 class = header_byte();
657 ao_ublox_len = header_byte();
658 ao_ublox_len |= header_byte() << 8;
660 ao_gps_dbg(DBG_PROTO, "class %02x id %02x len %d\n", class, id, ao_ublox_len);
662 if (ao_ublox_len > 1023)
669 if (ao_ublox_len != 18)
671 ao_ublox_parse_nav_dop();
673 case UBLOX_NAV_POSLLH:
674 if (ao_ublox_len != 28)
676 ao_ublox_parse_nav_posllh();
679 if (ao_ublox_len != 52)
681 ao_ublox_parse_nav_sol();
683 case UBLOX_NAV_SVINFO:
684 if (ao_ublox_len < 8)
686 ao_ublox_parse_nav_svinfo();
688 case UBLOX_NAV_VELNED:
689 if (ao_ublox_len != 36)
691 ao_ublox_parse_nav_velned();
693 case UBLOX_NAV_TIMEUTC:
694 if (ao_ublox_len != 20)
696 ao_ublox_parse_nav_timeutc();
702 if (ao_ublox_len != 0) {
703 ao_gps_dbg(DBG_PROTO, "len left %d\n", ao_ublox_len);
707 /* verify checksum and end sequence */
708 cksum.a = ao_ublox_byte();
709 cksum.b = ao_ublox_byte();
710 if (ao_ublox_cksum.a != cksum.a || ao_ublox_cksum.b != cksum.b)
716 case UBLOX_NAV_TIMEUTC:
717 ao_mutex_get(&ao_gps_mutex);
718 ao_gps_tick = ao_time();
720 ao_gps_data.flags = 0;
721 ao_gps_data.flags |= AO_GPS_RUNNING;
722 if (nav_sol.gps_fix & (1 << NAV_SOL_FLAGS_GPSFIXOK)) {
723 uint8_t nsat = nav_sol.nsat;
724 ao_gps_data.flags |= AO_GPS_VALID | AO_GPS_COURSE_VALID;
727 ao_gps_data.flags |= nsat;
729 if (nav_timeutc.valid & (1 << NAV_TIMEUTC_VALID_UTC))
730 ao_gps_data.flags |= AO_GPS_DATE_VALID;
732 AO_TELEMETRY_LOCATION_SET_ALTITUDE(&ao_gps_data, nav_posllh.alt_msl / 1000);
733 ao_gps_data.latitude = nav_posllh.lat;
734 ao_gps_data.longitude = nav_posllh.lon;
736 ao_gps_data.year = nav_timeutc.year - 2000;
737 ao_gps_data.month = nav_timeutc.month;
738 ao_gps_data.day = nav_timeutc.day;
740 ao_gps_data.hour = nav_timeutc.hour;
741 ao_gps_data.minute = nav_timeutc.min;
742 ao_gps_data.second = nav_timeutc.sec;
744 /* we report dop scaled by 10, but ublox provides dop scaled by 100
746 ao_gps_data.pdop = nav_dop.pdop / 10;
747 ao_gps_data.hdop = nav_dop.hdop / 10;
748 ao_gps_data.vdop = nav_dop.vdop / 10;
750 ao_gps_data.ground_speed = nav_velned.g_speed;
751 ao_gps_data.climb_rate = -nav_velned.vel_d;
752 ao_gps_data.course = nav_velned.heading / 200000;
754 ao_gps_tracking_data.channels = 0;
756 struct ao_telemetry_satellite_info *dst = &ao_gps_tracking_data.sats[0];
757 struct nav_svinfo_sat *src = &nav_svinfo_sat[0];
759 for (i = 0; i < nav_svinfo_nsat; i++) {
760 if (!(src->flags & (1 << NAV_SVINFO_SAT_FLAGS_UNHEALTHY)) &&
761 src->quality >= NAV_SVINFO_SAT_QUALITY_ACQUIRED)
763 if (ao_gps_tracking_data.channels < AO_TELEMETRY_SATELLITE_MAX_SAT) {
764 dst->svid = src->svid;
765 dst->c_n_1 = src->cno;
767 ao_gps_tracking_data.channels++;
773 ao_mutex_put(&ao_gps_mutex);
774 ao_gps_new = AO_GPS_NEW_DATA | AO_GPS_NEW_TRACKING;
775 ao_wakeup(&ao_gps_new);
784 static void ao_gps_option(void)
787 if (ao_cmd_status != ao_cmd_success) {
788 ao_cmd_status = ao_cmd_success;
791 ao_gps_dbg_enable = ao_cmd_lex_i;
792 printf ("gps debug set to %d\n", ao_gps_dbg_enable);
796 #define ao_gps_option ao_gps_show
799 __code struct ao_cmds ao_gps_cmds[] = {
800 { ao_gps_option, "g\0Display GPS" },
804 __xdata struct ao_task ao_gps_task;
809 ao_cmd_register(&ao_gps_cmds[0]);
810 ao_add_task(&ao_gps_task, ao_gps, "gps");