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 1
28 __xdata uint8_t ao_gps_mutex;
29 __pdata uint16_t ao_gps_tick;
30 __xdata struct ao_telemetry_location ao_gps_data;
31 __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
33 #undef AO_SERIAL_SPEED_UBLOX
35 #ifndef AO_SERIAL_SPEED_UBLOX
36 #define AO_SERIAL_SPEED_UBLOX AO_SERIAL_SPEED_9600
39 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_57600
40 #define SERIAL_SPEED_STRING "57600"
41 #define SERIAL_SPEED_CHECKSUM "2d"
43 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_19200
44 #define SERIAL_SPEED_STRING "19200"
45 #define SERIAL_SPEED_CHECKSUM "23"
47 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_9600
48 #define SERIAL_SPEED_STRING "9600"
49 #define SERIAL_SPEED_CHECKSUM "16"
52 static const char ao_gps_set_nmea[] =
53 "\r\n$PUBX,41,1,3,1," SERIAL_SPEED_STRING ",0*" SERIAL_SPEED_CHECKSUM "\r\n";
55 struct ao_ublox_cksum {
59 static __pdata struct ao_ublox_cksum ao_ublox_cksum;
60 static __pdata uint16_t ao_ublox_len;
64 static uint8_t ao_gps_dbg_enable;
70 static void ao_gps_dbg(int level, char *format, ...) {
73 if (level & ao_gps_dbg_enable) {
82 #define ao_gps_dbg(fmt, ...)
85 static inline uint8_t ao_ublox_byte(void) {
86 uint8_t c = (uint8_t) ao_gps_getchar();
87 ao_gps_dbg(DBG_CHAR, " %02x", c);
91 static inline void add_cksum(struct ao_ublox_cksum *cksum, uint8_t c)
97 static void ao_ublox_init_cksum(void)
99 ao_ublox_cksum.a = ao_ublox_cksum.b = 0;
102 static void ao_ublox_put_u8(uint8_t c)
104 add_cksum(&ao_ublox_cksum, c);
105 ao_gps_dbg(DBG_CHAR, " (%02x)", c);
109 static void ao_ublox_put_i8(int8_t c)
111 ao_ublox_put_u8((uint8_t) c);
114 static void ao_ublox_put_u16(uint16_t c)
117 ao_ublox_put_u8(c>>8);
121 static void ao_ublox_put_i16(int16_t c)
123 ao_ublox_put_u16((uint16_t) c);
127 static void ao_ublox_put_u32(uint32_t c)
130 ao_ublox_put_u8(c>>8);
131 ao_ublox_put_u8(c>>16);
132 ao_ublox_put_u8(c>>24);
135 static void ao_ublox_put_i32(int32_t c)
137 ao_ublox_put_u32((uint32_t) c);
140 static uint8_t header_byte(void)
142 uint8_t c = ao_ublox_byte();
143 add_cksum(&ao_ublox_cksum, c);
147 static uint8_t data_byte(void)
150 return header_byte();
153 static char __xdata *ublox_target;
155 static void ublox_u16(uint8_t offset)
157 uint16_t __xdata *ptr = (uint16_t __xdata *) (ublox_target + offset);
161 val |= data_byte () << 8;
165 static void ublox_u8(uint8_t offset)
167 uint8_t __xdata *ptr = (uint8_t __xdata *) (ublox_target + offset);
174 static void ublox_u32(uint8_t offset) __reentrant
176 uint32_t __xdata *ptr = (uint32_t __xdata *) (ublox_target + offset);
179 val = ((uint32_t) data_byte ());
180 val |= ((uint32_t) data_byte ()) << 8;
181 val |= ((uint32_t) data_byte ()) << 16;
182 val |= ((uint32_t) data_byte ()) << 24;
186 static void ublox_discard(uint8_t len)
193 #define UBLOX_DISCARD 1
198 struct ublox_packet_parse {
204 ao_ublox_parse(void __xdata *target, const struct ublox_packet_parse *parse) __reentrant
208 ublox_target = target;
210 offset = parse[i].offset;
211 switch (parse[i].type) {
215 ublox_discard(offset);
228 ao_gps_dbg(DBG_PROTO, "\n");
232 * NAV-DOP message parsing
235 static struct nav_dop {
241 static const struct ublox_packet_parse nav_dop_packet[] = {
242 { UBLOX_DISCARD, 6 }, /* 0 GPS Millisecond Time of Week, gDOP */
243 { UBLOX_U16, offsetof(struct nav_dop, pdop) }, /* 6 pDOP */
244 { UBLOX_DISCARD, 2 }, /* 8 tDOP */
245 { UBLOX_U16, offsetof(struct nav_dop, vdop) }, /* 10 vDOP */
246 { UBLOX_U16, offsetof(struct nav_dop, hdop) }, /* 12 hDOP */
247 { UBLOX_DISCARD, 4 }, /* 14 nDOP, eDOP */
252 ao_ublox_parse_nav_dop(void)
254 ao_ublox_parse(&nav_dop, nav_dop_packet);
258 * NAV-POSLLH message parsing
260 static struct nav_posllh {
266 static const struct ublox_packet_parse nav_posllh_packet[] = {
267 { UBLOX_DISCARD, 4 }, /* 0 GPS Millisecond Time of Week */
268 { UBLOX_U32, offsetof(struct nav_posllh, lon) }, /* 4 Longitude */
269 { UBLOX_U32, offsetof(struct nav_posllh, lat) }, /* 8 Latitude */
270 { UBLOX_DISCARD, 4 }, /* 12 Height above Ellipsoid */
271 { UBLOX_U32, offsetof(struct nav_posllh, alt_msl) }, /* 16 Height above mean sea level */
272 { UBLOX_DISCARD, 8 }, /* 20 hAcc, vAcc */
273 { UBLOX_END, 0 }, /* 28 */
277 ao_ublox_parse_nav_posllh(void)
279 ao_ublox_parse(&nav_posllh, nav_posllh_packet);
283 * NAV-SOL message parsing
285 static struct nav_sol {
291 static const struct ublox_packet_parse nav_sol_packet[] = {
292 { UBLOX_DISCARD, 10 }, /* 0 iTOW, fTOW, week */
293 { UBLOX_U8, offsetof(struct nav_sol, gps_fix) }, /* 10 gpsFix */
294 { UBLOX_U8, offsetof(struct nav_sol, flags) }, /* 11 flags */
295 { UBLOX_DISCARD, 35 }, /* 12 ecefX, ecefY, ecefZ, pAcc, ecefVX, ecefVY, ecefVZ, sAcc, pDOP, reserved1 */
296 { UBLOX_U8, offsetof(struct nav_sol, nsat) }, /* 47 numSV */
297 { UBLOX_DISCARD, 4 }, /* 48 reserved2 */
301 #define NAV_SOL_FLAGS_GPSFIXOK 0
302 #define NAV_SOL_FLAGS_DIFFSOLN 1
303 #define NAV_SOL_FLAGS_WKNSET 2
304 #define NAV_SOL_FLAGS_TOWSET 3
307 ao_ublox_parse_nav_sol(void)
309 ao_ublox_parse(&nav_sol, nav_sol_packet);
313 * NAV-SVINFO message parsing
316 static struct nav_svinfo {
321 static const struct ublox_packet_parse nav_svinfo_packet[] = {
322 { UBLOX_DISCARD, 4 }, /* 0 iTOW */
323 { UBLOX_U8, offsetof(struct nav_svinfo, num_ch) }, /* 4 numCh */
324 { UBLOX_U8, offsetof(struct nav_svinfo, flags) }, /* 5 globalFlags */
325 { UBLOX_DISCARD, 2 }, /* 6 reserved2 */
329 #define NAV_SVINFO_MAX_SAT 16
331 static struct nav_svinfo_sat {
337 } nav_svinfo_sat[NAV_SVINFO_MAX_SAT];
339 static uint8_t nav_svinfo_nsat;
341 static const struct ublox_packet_parse nav_svinfo_sat_packet[] = {
342 { UBLOX_U8, offsetof(struct nav_svinfo_sat, chn) }, /* 8 + 12*N chn */
343 { UBLOX_U8, offsetof(struct nav_svinfo_sat, svid) }, /* 9 + 12*N svid */
344 { UBLOX_U8, offsetof(struct nav_svinfo_sat, flags) }, /* 10 + 12*N flags */
345 { UBLOX_U8, offsetof(struct nav_svinfo_sat, quality) }, /* 11 + 12*N quality */
346 { UBLOX_U8, offsetof(struct nav_svinfo_sat, cno) }, /* 12 + 12*N cno */
347 { UBLOX_DISCARD, 7 }, /* 13 + 12*N elev, azim, prRes */
351 #define NAV_SVINFO_SAT_FLAGS_SVUSED 0
352 #define NAV_SVINFO_SAT_FLAGS_DIFFCORR 1
353 #define NAV_SVINFO_SAT_FLAGS_ORBITAVAIL 2
354 #define NAV_SVINFO_SAT_FLAGS_ORBITEPH 3
355 #define NAV_SVINFO_SAT_FLAGS_UNHEALTHY 4
356 #define NAV_SVINFO_SAT_FLAGS_ORBITALM 5
357 #define NAV_SVINFO_SAT_FLAGS_ORBITAOP 6
358 #define NAV_SVINFO_SAT_FLAGS_SMOOTHED 7
360 #define NAV_SVINFO_SAT_QUALITY_IDLE 0
361 #define NAV_SVINFO_SAT_QUALITY_SEARCHING 1
362 #define NAV_SVINFO_SAT_QUALITY_ACQUIRED 2
363 #define NAV_SVINFO_SAT_QUALITY_UNUSABLE 3
364 #define NAV_SVINFO_SAT_QUALITY_LOCKED 4
365 #define NAV_SVINFO_SAT_QUALITY_RUNNING 5
368 ao_ublox_parse_nav_svinfo(void)
373 ao_ublox_parse(&nav_svinfo, nav_svinfo_packet);
374 for (nsat = 0; nsat < nav_svinfo.num_ch && ao_ublox_len >= 12; nsat++) {
375 if (nsat < NAV_SVINFO_MAX_SAT) {
376 ao_ublox_parse(&nav_svinfo_sat[nav_svinfo_nsat++], nav_svinfo_sat_packet);
382 ao_gps_dbg(DBG_PROTO, "svinfo num_ch %d flags %02x\n", nav_svinfo.num_ch, nav_svinfo.flags);
383 for (nsat = 0; nsat < nav_svinfo.num_ch; nsat++)
384 ao_gps_dbg(DBG_PROTO, "\t%d: chn %d svid %d flags %02x quality %d cno %d\n",
386 nav_svinfo_sat[nsat].chn,
387 nav_svinfo_sat[nsat].svid,
388 nav_svinfo_sat[nsat].flags,
389 nav_svinfo_sat[nsat].quality,
390 nav_svinfo_sat[nsat].cno);
395 * NAV-TIMEUTC message parsing
397 static struct nav_timeutc {
407 #define NAV_TIMEUTC_VALID_TOW 0
408 #define NAV_TIMEUTC_VALID_WKN 1
409 #define NAV_TIMEUTC_VALID_UTC 2
411 static const struct ublox_packet_parse nav_timeutc_packet[] = {
412 { UBLOX_DISCARD, 12 }, /* 0 iTOW, tAcc, nano */
413 { UBLOX_U16, offsetof(struct nav_timeutc, year) }, /* 12 year */
414 { UBLOX_U8, offsetof(struct nav_timeutc, month) }, /* 14 month */
415 { UBLOX_U8, offsetof(struct nav_timeutc, day) }, /* 15 day */
416 { UBLOX_U8, offsetof(struct nav_timeutc, hour) }, /* 16 hour */
417 { UBLOX_U8, offsetof(struct nav_timeutc, min) }, /* 17 min */
418 { UBLOX_U8, offsetof(struct nav_timeutc, sec) }, /* 18 sec */
419 { UBLOX_U8, offsetof(struct nav_timeutc, valid) }, /* 19 valid */
424 ao_ublox_parse_nav_timeutc(void)
426 ao_ublox_parse(&nav_timeutc, nav_timeutc_packet);
430 * NAV-VELNED message parsing
433 static struct nav_velned {
439 static const struct ublox_packet_parse nav_velned_packet[] = {
440 { UBLOX_DISCARD, 12 }, /* 0 iTOW, velN, velE */
441 { UBLOX_U32, offsetof(struct nav_velned, vel_d) }, /* 12 velD */
442 { UBLOX_DISCARD, 4 }, /* 16 speed */
443 { UBLOX_U32, offsetof(struct nav_velned, g_speed) }, /* 20 gSpeed */
444 { UBLOX_U32, offsetof(struct nav_velned, heading) }, /* 24 heading */
445 { UBLOX_DISCARD, 8 }, /* 28 sAcc, cAcc */
450 ao_ublox_parse_nav_velned(void)
452 ao_ublox_parse(&nav_velned, nav_velned_packet);
456 * Set the protocol mode and baud rate
465 * A bunch of nulls so the start bit
469 for (i = 0; i < 64; i++)
470 ao_gps_putchar(0x00);
478 ao_delay(AO_SEC_TO_TICKS(3));
480 ao_gps_dbg(DBG_INIT, "Set speed 9600\n");
481 ao_gps_set_speed(AO_SERIAL_SPEED_9600);
484 * Send the baud-rate setting and protocol-setting
485 * command three times
487 for (k = 0; k < 3; k++) {
490 ao_gps_dbg(DBG_INIT, "Send initial setting\n");
491 for (i = 0; i < sizeof (ao_gps_set_nmea); i++)
492 ao_gps_putchar(ao_gps_set_nmea[i]);
497 #if AO_SERIAL_SPEED_UBLOX != AO_SERIAL_SPEED_9600
498 ao_gps_dbg(DBG_INIT, "Set speed high\n");
500 * Increase the baud rate
502 ao_gps_set_speed(AO_SERIAL_SPEED_UBLOX);
509 ao_ublox_putstart(uint8_t class, uint8_t id, uint16_t len)
511 ao_ublox_init_cksum();
512 ao_gps_putchar(0xb5);
513 ao_gps_putchar(0x62);
514 ao_ublox_put_u8(class);
516 ao_ublox_put_u8(len);
517 ao_ublox_put_u8(len >> 8);
521 ao_ublox_putend(void)
523 ao_gps_putchar(ao_ublox_cksum.a);
524 ao_gps_putchar(ao_ublox_cksum.b);
528 ao_ublox_set_message_rate(uint8_t class, uint8_t msgid, uint8_t rate)
530 ao_ublox_putstart(0x06, 0x01, 3);
531 ao_ublox_put_u8(class);
532 ao_ublox_put_u8(msgid);
533 ao_ublox_put_u8(rate);
538 ao_ublox_set_navigation_settings(uint16_t mask,
542 uint32_t fixed_alt_var,
549 uint8_t static_hold_thresh,
550 uint8_t dgps_time_out)
552 ao_ublox_putstart(UBLOX_CFG, UBLOX_CFG_NAV5, 36);
553 ao_ublox_put_u16(mask);
554 ao_ublox_put_u8(dyn_model);
555 ao_ublox_put_u8(fix_mode);
556 ao_ublox_put_i32(fixed_alt);
557 ao_ublox_put_u32(fixed_alt_var);
558 ao_ublox_put_i8(min_elev);
559 ao_ublox_put_u8(dr_limit);
560 ao_ublox_put_u16(pdop);
561 ao_ublox_put_u16(tdop);
562 ao_ublox_put_u16(pacc);
563 ao_ublox_put_u16(tacc);
564 ao_ublox_put_u8(static_hold_thresh);
565 ao_ublox_put_u8(dgps_time_out);
574 * Disable all MON message
576 static const uint8_t ublox_disable_mon[] = {
577 0x0b, 0x09, 0x02, 0x06, 0x07, 0x21, 0x08, 0x04
581 * Disable all NAV messages. The desired
582 * ones will be explicitly re-enabled
585 static const uint8_t ublox_disable_nav[] = {
586 0x60, 0x22, 0x31, 0x04, 0x40, 0x01, 0x02, 0x32,
587 0x06, 0x03, 0x30, 0x20, 0x21, 0x11, 0x12
591 * Enable enough messages to get all of the data we want
593 static const uint8_t ublox_enable_nav[] = {
603 ao_gps(void) __reentrant
606 struct ao_ublox_cksum cksum;
611 /* Disable all messages */
612 for (i = 0; i < sizeof (ublox_disable_mon); i++)
613 ao_ublox_set_message_rate(0x0a, ublox_disable_mon[i], 0);
614 for (i = 0; i < sizeof (ublox_disable_nav); i++)
615 ao_ublox_set_message_rate(UBLOX_NAV, ublox_disable_nav[i], 0);
617 /* Enable all of the messages we want */
618 for (i = 0; i < sizeof (ublox_enable_nav); i++)
619 ao_ublox_set_message_rate(UBLOX_NAV, ublox_enable_nav[i], 1);
621 ao_ublox_set_navigation_settings((1 << UBLOX_CFG_NAV5_MASK_DYN) | (1 << UBLOX_CFG_NAV5_MASK_FIXMODE),
622 UBLOX_CFG_NAV5_DYNMODEL_AIRBORNE_4G,
623 UBLOX_CFG_NAV5_FIXMODE_3D,
636 /* Locate the begining of the next record */
637 while (ao_ublox_byte() != (uint8_t) 0xb5)
639 if (ao_ublox_byte() != (uint8_t) 0x62)
642 ao_ublox_init_cksum();
644 class = header_byte();
648 ao_ublox_len = header_byte();
649 ao_ublox_len |= header_byte() << 8;
651 ao_gps_dbg(DBG_PROTO, "class %02x id %02x len %d\n", class, id, ao_ublox_len);
653 if (ao_ublox_len > 1023)
660 if (ao_ublox_len != 18)
662 ao_ublox_parse_nav_dop();
664 case UBLOX_NAV_POSLLH:
665 if (ao_ublox_len != 28)
667 ao_ublox_parse_nav_posllh();
670 if (ao_ublox_len != 52)
672 ao_ublox_parse_nav_sol();
674 case UBLOX_NAV_SVINFO:
675 if (ao_ublox_len < 8)
677 ao_ublox_parse_nav_svinfo();
679 case UBLOX_NAV_VELNED:
680 if (ao_ublox_len != 36)
682 ao_ublox_parse_nav_velned();
684 case UBLOX_NAV_TIMEUTC:
685 if (ao_ublox_len != 20)
687 ao_ublox_parse_nav_timeutc();
693 if (ao_ublox_len != 0) {
694 ao_gps_dbg(DBG_PROTO, "len left %d\n", ao_ublox_len);
698 /* verify checksum and end sequence */
699 cksum.a = ao_ublox_byte();
700 cksum.b = ao_ublox_byte();
701 if (ao_ublox_cksum.a != cksum.a || ao_ublox_cksum.b != cksum.b)
708 ao_mutex_get(&ao_gps_mutex);
709 ao_gps_tick = ao_time();
711 ao_gps_data.flags = 0;
712 ao_gps_data.flags |= AO_GPS_RUNNING;
713 if (nav_sol.gps_fix & (1 << NAV_SOL_FLAGS_GPSFIXOK)) {
714 uint8_t nsat = nav_sol.nsat;
715 ao_gps_data.flags |= AO_GPS_VALID;
718 ao_gps_data.flags |= nsat;
720 if (nav_timeutc.valid & (1 << NAV_TIMEUTC_VALID_UTC))
721 ao_gps_data.flags |= AO_GPS_DATE_VALID;
723 ao_gps_data.altitude = nav_posllh.alt_msl / 1000;
724 ao_gps_data.latitude = nav_posllh.lat;
725 ao_gps_data.longitude = nav_posllh.lon;
727 ao_gps_data.year = nav_timeutc.year - 2000;
728 ao_gps_data.month = nav_timeutc.month;
729 ao_gps_data.day = nav_timeutc.day;
731 ao_gps_data.hour = nav_timeutc.hour;
732 ao_gps_data.minute = nav_timeutc.min;
733 ao_gps_data.second = nav_timeutc.sec;
735 ao_gps_data.pdop = nav_dop.pdop;
736 ao_gps_data.hdop = nav_dop.hdop;
737 ao_gps_data.vdop = nav_dop.vdop;
739 /* mode is not set */
741 ao_gps_data.ground_speed = nav_velned.g_speed;
742 ao_gps_data.climb_rate = -nav_velned.vel_d;
743 ao_gps_data.course = nav_velned.heading / 200000;
745 ao_gps_tracking_data.channels = 0;
747 struct ao_telemetry_satellite_info *dst = &ao_gps_tracking_data.sats[0];
749 for (i = 0; i < nav_svinfo_nsat; i++) {
750 struct nav_svinfo_sat *src = &nav_svinfo_sat[i];
752 if (!(src->flags & (1 << NAV_SVINFO_SAT_FLAGS_UNHEALTHY)) &&
753 src->quality >= NAV_SVINFO_SAT_QUALITY_ACQUIRED)
755 dst->svid = src->svid;
756 dst->c_n_1 = src->cno;
758 ao_gps_tracking_data.channels++;
762 ao_mutex_put(&ao_gps_mutex);
763 ao_wakeup(&ao_gps_data);
764 ao_wakeup(&ao_gps_tracking_data);
773 static void ao_gps_option(void)
776 if (ao_cmd_status != ao_cmd_success) {
777 ao_cmd_status = ao_cmd_success;
780 ao_gps_dbg_enable = ao_cmd_lex_i;
781 printf ("gps debug set to %d\n", ao_gps_dbg_enable);
785 #define ao_gps_option ao_gps_show
788 __code struct ao_cmds ao_gps_cmds[] = {
789 { ao_gps_option, "g\0Display GPS" },
793 __xdata struct ao_task ao_gps_task;
798 ao_cmd_register(&ao_gps_cmds[0]);
799 ao_add_task(&ao_gps_task, ao_gps, "gps");