altos: Use #define values for ublox packet types
[fw/altos] / src / drivers / ao_gps_ublox.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #ifndef AO_GPS_TEST
19 #include "ao.h"
20 #endif
21
22 #include "ao_gps_ublox.h"
23
24 #define AO_UBLOX_DEBUG 1
25
26 #include <stdarg.h>
27
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;
32
33 #undef AO_SERIAL_SPEED_UBLOX
34
35 #ifndef AO_SERIAL_SPEED_UBLOX
36 #define AO_SERIAL_SPEED_UBLOX AO_SERIAL_SPEED_9600
37 #endif
38
39 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_57600
40 #define SERIAL_SPEED_STRING     "57600"
41 #define SERIAL_SPEED_CHECKSUM   "2d"
42 #endif
43 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_19200
44 #define SERIAL_SPEED_STRING     "19200"
45 #define SERIAL_SPEED_CHECKSUM   "23"
46 #endif
47 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_9600
48 #define SERIAL_SPEED_STRING     "9600"
49 #define SERIAL_SPEED_CHECKSUM   "16"
50 #endif
51
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";
54
55 struct ao_ublox_cksum {
56         uint8_t a, b;
57 };
58
59 static __pdata struct ao_ublox_cksum ao_ublox_cksum;
60 static __pdata uint16_t ao_ublox_len;
61
62 #if AO_UBLOX_DEBUG
63
64 static uint8_t ao_gps_dbg_enable;
65
66 #define DBG_PROTO       1
67 #define DBG_CHAR        2
68 #define DBG_INIT        4
69
70 static void ao_gps_dbg(int level, char *format, ...) {
71         va_list a;
72
73         if (level & ao_gps_dbg_enable) {
74                 va_start(a, format);
75                 vprintf(format, a);
76                 va_end(a);
77                 flush();
78         }
79 }
80
81 #else
82 #define ao_gps_dbg(fmt, ...)
83 #endif
84
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);
88         return c;
89 }
90
91 static inline void add_cksum(struct ao_ublox_cksum *cksum, uint8_t c)
92 {
93         cksum->a += c;
94         cksum->b += cksum->a;
95 }
96
97 static void ao_ublox_init_cksum(void)
98 {
99         ao_ublox_cksum.a = ao_ublox_cksum.b = 0;
100 }
101
102 static void ao_ublox_put_u8(uint8_t c)
103 {
104         add_cksum(&ao_ublox_cksum, c);
105         ao_gps_dbg(DBG_CHAR, " (%02x)", c);
106         ao_gps_putchar(c);
107 }
108
109 static void ao_ublox_put_i8(int8_t c)
110 {
111         ao_ublox_put_u8((uint8_t) c);
112 }
113
114 static void ao_ublox_put_u16(uint16_t c)
115 {
116         ao_ublox_put_u8(c);
117         ao_ublox_put_u8(c>>8);
118 }
119
120 #if 0
121 static void ao_ublox_put_i16(int16_t c)
122 {
123         ao_ublox_put_u16((uint16_t) c);
124 }
125 #endif
126
127 static void ao_ublox_put_u32(uint32_t c)
128 {
129         ao_ublox_put_u8(c);
130         ao_ublox_put_u8(c>>8);
131         ao_ublox_put_u8(c>>16);
132         ao_ublox_put_u8(c>>24);
133 }
134
135 static void ao_ublox_put_i32(int32_t c)
136 {
137         ao_ublox_put_u32((uint32_t) c);
138 }
139
140 static uint8_t header_byte(void)
141 {
142         uint8_t c = ao_ublox_byte();
143         add_cksum(&ao_ublox_cksum, c);
144         return c;
145 }
146
147 static uint8_t data_byte(void)
148 {
149         --ao_ublox_len;
150         return header_byte();
151 }
152
153 static char __xdata *ublox_target;
154
155 static void ublox_u16(uint8_t offset)
156 {
157         uint16_t __xdata *ptr = (uint16_t __xdata *) (ublox_target + offset);
158         uint16_t val;
159
160         val = data_byte();
161         val |= data_byte () << 8;
162         *ptr = val;
163 }
164
165 static void ublox_u8(uint8_t offset)
166 {
167         uint8_t __xdata *ptr = (uint8_t __xdata *) (ublox_target + offset);
168         uint8_t val;
169
170         val = data_byte ();
171         *ptr = val;
172 }
173
174 static void ublox_u32(uint8_t offset) __reentrant
175 {
176         uint32_t __xdata *ptr = (uint32_t __xdata *) (ublox_target + offset);
177         uint32_t val;
178
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;
183         *ptr = val;
184 }
185
186 static void ublox_discard(uint8_t len)
187 {
188         while (len--)
189                 data_byte();
190 }
191
192 #define UBLOX_END       0
193 #define UBLOX_DISCARD   1
194 #define UBLOX_U8        2
195 #define UBLOX_U16       3
196 #define UBLOX_U32       4
197
198 struct ublox_packet_parse {
199         uint8_t type;
200         uint8_t offset;
201 };
202
203 static void
204 ao_ublox_parse(void __xdata *target, const struct ublox_packet_parse *parse) __reentrant
205 {
206         uint8_t i, offset;
207
208         ublox_target = target;
209         for (i = 0; ; i++) {
210                 offset = parse[i].offset;
211                 switch (parse[i].type) {
212                 case UBLOX_END:
213                         return;
214                 case UBLOX_DISCARD:
215                         ublox_discard(offset);
216                         break;
217                 case UBLOX_U8:
218                         ublox_u8(offset);
219                         break;
220                 case UBLOX_U16:
221                         ublox_u16(offset);
222                         break;
223                 case UBLOX_U32:
224                         ublox_u32(offset);
225                         break;
226                 }
227         }
228         ao_gps_dbg(DBG_PROTO, "\n");
229 }
230
231 /*
232  * NAV-DOP message parsing
233  */
234
235 static struct nav_dop {
236         uint16_t        pdop;
237         uint16_t        hdop;
238         uint16_t        vdop;
239 } nav_dop;
240
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 */
248         { UBLOX_END, 0 }
249 };
250
251 static void
252 ao_ublox_parse_nav_dop(void)
253 {
254         ao_ublox_parse(&nav_dop, nav_dop_packet);
255 }
256
257 /*
258  * NAV-POSLLH message parsing
259  */
260 static struct nav_posllh {
261         int32_t         lat;
262         int32_t         lon;
263         int32_t         alt_msl;
264 } nav_posllh;
265
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 */
274 };
275
276 static void
277 ao_ublox_parse_nav_posllh(void)
278 {
279         ao_ublox_parse(&nav_posllh, nav_posllh_packet);
280 }
281
282 /*
283  * NAV-SOL message parsing
284  */
285 static struct nav_sol {
286         uint8_t         gps_fix;
287         uint8_t         flags;
288         uint8_t         nsat;
289 } nav_sol;
290
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 */
298         { UBLOX_END, 0 }
299 };
300
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
305
306 static void
307 ao_ublox_parse_nav_sol(void)
308 {
309         ao_ublox_parse(&nav_sol, nav_sol_packet);
310 }
311
312 /*
313  * NAV-SVINFO message parsing
314  */
315
316 static struct nav_svinfo {
317         uint8_t num_ch;
318         uint8_t flags;
319 } nav_svinfo;
320
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 */
326         { UBLOX_END, 0 }
327 };
328
329 #define NAV_SVINFO_MAX_SAT      16
330
331 static struct nav_svinfo_sat {
332         uint8_t chn;
333         uint8_t svid;
334         uint8_t flags;
335         uint8_t quality;
336         uint8_t cno;
337 } nav_svinfo_sat[NAV_SVINFO_MAX_SAT];
338
339 static uint8_t  nav_svinfo_nsat;
340
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 */
348         { UBLOX_END, 0 }
349 };
350
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
359
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
366
367 static void
368 ao_ublox_parse_nav_svinfo(void)
369 {
370         uint8_t nsat;
371         nav_svinfo_nsat = 0;
372
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);
377                 } else {
378                         ublox_discard(12);
379                 }
380         }
381 #if AO_UBLOX_DEBUG
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",
385                             nsat,
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);
391 #endif
392 }
393
394 /*
395  * NAV-TIMEUTC message parsing
396  */
397 static struct nav_timeutc {
398         uint16_t        year;
399         uint8_t         month;
400         uint8_t         day;
401         uint8_t         hour;
402         uint8_t         min;
403         uint8_t         sec;
404         uint8_t         valid;
405 } nav_timeutc;
406
407 #define NAV_TIMEUTC_VALID_TOW   0
408 #define NAV_TIMEUTC_VALID_WKN   1
409 #define NAV_TIMEUTC_VALID_UTC   2
410
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 */
420         { UBLOX_END, 0 }
421 };
422
423 static void
424 ao_ublox_parse_nav_timeutc(void)
425 {
426         ao_ublox_parse(&nav_timeutc, nav_timeutc_packet);
427 }
428
429 /*
430  * NAV-VELNED message parsing
431  */
432
433 static struct nav_velned {
434         int32_t         vel_d;
435         uint32_t        g_speed;
436         int32_t         heading;
437 } nav_velned;
438
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 */
446         { UBLOX_END, 0 }
447 };
448
449 static void
450 ao_ublox_parse_nav_velned(void)
451 {
452         ao_ublox_parse(&nav_velned, nav_velned_packet);
453 }
454
455 /*
456  * Set the protocol mode and baud rate
457  */
458
459 static void
460 ao_gps_delay(void)
461 {
462         uint8_t i;
463
464         /*
465          * A bunch of nulls so the start bit
466          * is clear
467          */
468
469         for (i = 0; i < 64; i++)
470                 ao_gps_putchar(0x00);
471 }
472
473 static void
474 ao_gps_setup(void)
475 {
476         uint8_t i, k;
477
478         ao_delay(AO_SEC_TO_TICKS(3));
479
480         ao_gps_dbg(DBG_INIT, "Set speed 9600\n");
481         ao_gps_set_speed(AO_SERIAL_SPEED_9600);
482
483         /*
484          * Send the baud-rate setting and protocol-setting
485          * command three times
486          */
487         for (k = 0; k < 3; k++) {
488                 ao_gps_delay();
489
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]);
493         }
494
495         ao_gps_delay();
496
497 #if AO_SERIAL_SPEED_UBLOX != AO_SERIAL_SPEED_9600
498         ao_gps_dbg(DBG_INIT, "Set speed high\n");
499         /*
500          * Increase the baud rate
501          */
502         ao_gps_set_speed(AO_SERIAL_SPEED_UBLOX);
503 #endif
504
505         ao_gps_delay();
506 }
507
508 static void
509 ao_ublox_putstart(uint8_t class, uint8_t id, uint16_t len)
510 {
511         ao_ublox_init_cksum();
512         ao_gps_putchar(0xb5);
513         ao_gps_putchar(0x62);
514         ao_ublox_put_u8(class);
515         ao_ublox_put_u8(id);
516         ao_ublox_put_u8(len);
517         ao_ublox_put_u8(len >> 8);
518 }
519
520 static void
521 ao_ublox_putend(void)
522 {
523         ao_gps_putchar(ao_ublox_cksum.a);
524         ao_gps_putchar(ao_ublox_cksum.b);
525 }
526
527 static void
528 ao_ublox_set_message_rate(uint8_t class, uint8_t msgid, uint8_t rate)
529 {
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);
534         ao_ublox_putend();
535 }
536
537 static void
538 ao_ublox_set_navigation_settings(uint16_t mask,
539                                  uint8_t dyn_model,
540                                  uint8_t fix_mode,
541                                  int32_t fixed_alt,
542                                  uint32_t fixed_alt_var,
543                                  int8_t min_elev,
544                                  uint8_t dr_limit,
545                                  uint16_t pdop,
546                                  uint16_t tdop,
547                                  uint16_t pacc,
548                                  uint16_t tacc,
549                                  uint8_t static_hold_thresh,
550                                  uint8_t dgps_time_out)
551 {
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);
566         ao_ublox_put_u32(0);
567         ao_ublox_put_u32(0);
568         ao_ublox_put_u32(0);
569         ao_ublox_putend();
570 }
571
572
573 /*
574  * Disable all MON message
575  */
576 static const uint8_t ublox_disable_mon[] = {
577         0x0b, 0x09, 0x02, 0x06, 0x07, 0x21, 0x08, 0x04
578 };
579
580 /*
581  * Disable all NAV messages. The desired
582  * ones will be explicitly re-enabled
583  */
584
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
588 };
589
590 /*
591  * Enable enough messages to get all of the data we want
592  */
593 static const uint8_t ublox_enable_nav[] = {
594         UBLOX_NAV_DOP,
595         UBLOX_NAV_POSLLH,
596         UBLOX_NAV_SOL,
597         UBLOX_NAV_SVINFO,
598         UBLOX_NAV_VELNED,
599         UBLOX_NAV_TIMEUTC
600 };
601
602 void
603 ao_gps(void) __reentrant
604 {
605         uint8_t                 class, id;
606         struct ao_ublox_cksum   cksum;
607         uint8_t                 i;
608
609         ao_gps_setup();
610
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);
616
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);
620
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,
624                                          0,
625                                          0,
626                                          0,
627                                          0,
628                                          0,
629                                          0,
630                                          0,
631                                          0,
632                                          0,
633                                          0);
634
635         for (;;) {
636                 /* Locate the begining of the next record */
637                 while (ao_ublox_byte() != (uint8_t) 0xb5)
638                         ;
639                 if (ao_ublox_byte() != (uint8_t) 0x62)
640                         continue;
641
642                 ao_ublox_init_cksum();
643
644                 class = header_byte();
645                 id = header_byte();
646
647                 /* Length */
648                 ao_ublox_len = header_byte();
649                 ao_ublox_len |= header_byte() << 8;
650
651                 ao_gps_dbg(DBG_PROTO, "class %02x id %02x len %d\n", class, id, ao_ublox_len);
652
653                 if (ao_ublox_len > 1023)
654                         continue;
655
656                 switch (class) {
657                 case UBLOX_NAV:
658                         switch (id) {
659                         case UBLOX_NAV_DOP:
660                                 if (ao_ublox_len != 18)
661                                         break;
662                                 ao_ublox_parse_nav_dop();
663                                 break;
664                         case UBLOX_NAV_POSLLH:
665                                 if (ao_ublox_len != 28)
666                                         break;
667                                 ao_ublox_parse_nav_posllh();
668                                 break;
669                         case UBLOX_NAV_SOL:
670                                 if (ao_ublox_len != 52)
671                                         break;
672                                 ao_ublox_parse_nav_sol();
673                                 break;
674                         case UBLOX_NAV_SVINFO:
675                                 if (ao_ublox_len < 8)
676                                         break;
677                                 ao_ublox_parse_nav_svinfo();
678                                 break;
679                         case UBLOX_NAV_VELNED:
680                                 if (ao_ublox_len != 36)
681                                         break;
682                                 ao_ublox_parse_nav_velned();
683                                 break;
684                         case UBLOX_NAV_TIMEUTC:
685                                 if (ao_ublox_len != 20)
686                                         break;
687                                 ao_ublox_parse_nav_timeutc();
688                                 break;
689                         }
690                         break;
691                 }
692
693                 if (ao_ublox_len != 0) {
694                         ao_gps_dbg(DBG_PROTO, "len left %d\n", ao_ublox_len);
695                         continue;
696                 }
697
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)
702                         continue;
703
704                 switch (class) {
705                 case UBLOX_NAV:
706                         switch (id) {
707                         case UBLOX_NAV_TIMEUTC:
708                                 ao_mutex_get(&ao_gps_mutex);
709                                 ao_gps_tick = ao_time();
710
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;
716                                         if (nsat > 15)
717                                                 nsat = 15;
718                                         ao_gps_data.flags |= nsat;
719                                 }
720                                 if (nav_timeutc.valid & (1 << NAV_TIMEUTC_VALID_UTC))
721                                         ao_gps_data.flags |= AO_GPS_DATE_VALID;
722
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;
726
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;
730
731                                 ao_gps_data.hour = nav_timeutc.hour;
732                                 ao_gps_data.minute = nav_timeutc.min;
733                                 ao_gps_data.second = nav_timeutc.sec;
734
735                                 ao_gps_data.pdop = nav_dop.pdop;
736                                 ao_gps_data.hdop = nav_dop.hdop;
737                                 ao_gps_data.vdop = nav_dop.vdop;
738
739                                 /* mode is not set */
740
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;
744
745                                 ao_gps_tracking_data.channels = 0;
746
747                                 struct ao_telemetry_satellite_info *dst = &ao_gps_tracking_data.sats[0];
748
749                                 for (i = 0; i < nav_svinfo_nsat; i++) {
750                                         struct nav_svinfo_sat *src = &nav_svinfo_sat[i];
751
752                                         if (!(src->flags & (1 << NAV_SVINFO_SAT_FLAGS_UNHEALTHY)) &&
753                                             src->quality >= NAV_SVINFO_SAT_QUALITY_ACQUIRED)
754                                         {
755                                                 dst->svid = src->svid;
756                                                 dst->c_n_1 = src->cno;
757                                                 dst++;
758                                                 ao_gps_tracking_data.channels++;
759                                         }
760                                 }
761
762                                 ao_mutex_put(&ao_gps_mutex);
763                                 ao_wakeup(&ao_gps_data);
764                                 ao_wakeup(&ao_gps_tracking_data);
765                                 break;
766                         }
767                         break;
768                 }
769         }
770 }
771
772 #if AO_UBLOX_DEBUG
773 static void ao_gps_option(void)
774 {
775         ao_cmd_hex();
776         if (ao_cmd_status != ao_cmd_success) {
777                 ao_cmd_status = ao_cmd_success;
778                 ao_gps_show();
779         } else {
780                 ao_gps_dbg_enable = ao_cmd_lex_i;
781                 printf ("gps debug set to %d\n", ao_gps_dbg_enable);
782         }
783 }
784 #else
785 #define ao_gps_option ao_gps_show
786 #endif
787
788 __code struct ao_cmds ao_gps_cmds[] = {
789         { ao_gps_option,        "g\0Display GPS" },
790         { 0, NULL },
791 };
792
793 __xdata struct ao_task ao_gps_task;
794
795 void
796 ao_gps_init(void)
797 {
798         ao_cmd_register(&ao_gps_cmds[0]);
799         ao_add_task(&ao_gps_task, ao_gps, "gps");
800 }