drivers/ublox: Clean up some debug mode code
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 #ifndef AO_GPS_TEST
20 #include "ao.h"
21 #endif
22
23 #include "ao_gps_ublox.h"
24
25 #define AO_UBLOX_DEBUG 0
26
27 #include <stdarg.h>
28
29 uint8_t ao_gps_new;
30 uint8_t ao_gps_mutex;
31 AO_TICK_TYPE ao_gps_tick;
32 struct ao_telemetry_location    ao_gps_data;
33 struct ao_telemetry_satellite   ao_gps_tracking_data;
34
35 #undef AO_SERIAL_SPEED_UBLOX
36
37 #ifndef AO_SERIAL_SPEED_UBLOX
38 #define AO_SERIAL_SPEED_UBLOX AO_SERIAL_SPEED_9600
39 #endif
40
41 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_57600
42 #define SERIAL_SPEED_STRING     "57600"
43 #define SERIAL_SPEED_CHECKSUM   "2d"
44 #endif
45 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_19200
46 #define SERIAL_SPEED_STRING     "19200"
47 #define SERIAL_SPEED_CHECKSUM   "23"
48 #endif
49 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_9600
50 #define SERIAL_SPEED_STRING     "9600"
51 #define SERIAL_SPEED_CHECKSUM   "16"
52 #endif
53
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";
56
57 struct ao_ublox_cksum {
58         uint8_t a, b;
59 };
60
61 static struct ao_ublox_cksum ao_ublox_cksum;
62 static uint16_t ao_ublox_len;
63
64 #if AO_UBLOX_DEBUG
65
66
67 #define DBG_PROTO       1
68 #define DBG_CHAR        2
69 #define DBG_INIT        4
70
71 static uint8_t ao_gps_dbg_enable = DBG_PROTO|DBG_CHAR|DBG_INIT;
72
73 static void ao_gps_dbg(int level, char *format, ...) {
74         va_list a;
75
76         if (level & ao_gps_dbg_enable) {
77                 va_start(a, format);
78                 vprintf(format, a);
79                 va_end(a);
80                 flush();
81         }
82 }
83
84 #else
85 #define ao_gps_dbg(fmt, ...)
86 #endif
87
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);
91         return c;
92 }
93
94 static inline void add_cksum(struct ao_ublox_cksum *cksum, uint8_t c)
95 {
96         cksum->a += c;
97         cksum->b += cksum->a;
98 }
99
100 static void ao_ublox_init_cksum(void)
101 {
102         ao_ublox_cksum.a = ao_ublox_cksum.b = 0;
103 }
104
105 static void ao_ublox_put_u8(uint8_t c)
106 {
107         add_cksum(&ao_ublox_cksum, c);
108         ao_gps_dbg(DBG_CHAR, " (%02x)", c);
109         ao_gps_putchar(c);
110 }
111
112 static void ao_ublox_put_i8(int8_t c)
113 {
114         ao_ublox_put_u8((uint8_t) c);
115 }
116
117 static void ao_ublox_put_u16(uint16_t c)
118 {
119         ao_ublox_put_u8((uint8_t) c);
120         ao_ublox_put_u8((uint8_t) (c>>8));
121 }
122
123 #if 0
124 static void ao_ublox_put_i16(int16_t c)
125 {
126         ao_ublox_put_u16((uint16_t) c);
127 }
128 #endif
129
130 static void ao_ublox_put_u32(uint32_t c)
131 {
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));
136 }
137
138 static void ao_ublox_put_i32(int32_t c)
139 {
140         ao_ublox_put_u32((uint32_t) c);
141 }
142
143 static uint8_t header_byte(void)
144 {
145         uint8_t c = ao_ublox_byte();
146         add_cksum(&ao_ublox_cksum, c);
147         return c;
148 }
149
150 static uint8_t data_byte(void)
151 {
152         --ao_ublox_len;
153         return header_byte();
154 }
155
156 static char *ublox_target;
157
158 static void ublox_u16(uint8_t offset)
159 {
160         uint16_t *ptr = (uint16_t *) (void *) (ublox_target + offset);
161         uint16_t val;
162
163         val = data_byte();
164         val |= (uint16_t) ((uint16_t) data_byte () << 8);
165         *ptr = val;
166 }
167
168 static void ublox_u8(uint8_t offset)
169 {
170         uint8_t *ptr = (uint8_t *) (ublox_target + offset);
171         uint8_t val;
172
173         val = data_byte ();
174         *ptr = val;
175 }
176
177 static void ublox_u32(uint8_t offset) 
178 {
179         uint32_t *ptr = (uint32_t *) (void *) (ublox_target + offset);
180         uint32_t val;
181
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;
186         *ptr = val;
187 }
188
189 static void ublox_discard(uint8_t len)
190 {
191         while (len--)
192                 data_byte();
193 }
194
195 #define UBLOX_END       0
196 #define UBLOX_DISCARD   1
197 #define UBLOX_U8        2
198 #define UBLOX_U16       3
199 #define UBLOX_U32       4
200
201 struct ublox_packet_parse {
202         uint8_t type;
203         uint8_t offset;
204 };
205
206 static void
207 ao_ublox_parse(void *target, const struct ublox_packet_parse *parse) 
208 {
209         uint8_t i, offset;
210
211         ublox_target = target;
212         for (i = 0; ; i++) {
213                 offset = parse[i].offset;
214                 switch (parse[i].type) {
215                 case UBLOX_END:
216                         return;
217                 case UBLOX_DISCARD:
218                         ublox_discard(offset);
219                         break;
220                 case UBLOX_U8:
221                         ublox_u8(offset);
222                         break;
223                 case UBLOX_U16:
224                         ublox_u16(offset);
225                         break;
226                 case UBLOX_U32:
227                         ublox_u32(offset);
228                         break;
229                 }
230         }
231         ao_gps_dbg(DBG_PROTO, "\n");
232 }
233
234 /*
235  * NAV-DOP message parsing
236  */
237
238 static struct nav_dop {
239         uint16_t        pdop;
240         uint16_t        hdop;
241         uint16_t        vdop;
242 } nav_dop;
243
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 */
251         { UBLOX_END, 0 }
252 };
253
254 static void
255 ao_ublox_parse_nav_dop(void)
256 {
257         ao_ublox_parse(&nav_dop, nav_dop_packet);
258 }
259
260 /*
261  * NAV-POSLLH message parsing
262  */
263 static struct nav_posllh {
264         int32_t         lat;
265         int32_t         lon;
266         int32_t         alt_msl;
267 } nav_posllh;
268
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 */
277 };
278
279 static void
280 ao_ublox_parse_nav_posllh(void)
281 {
282         ao_ublox_parse(&nav_posllh, nav_posllh_packet);
283 }
284
285 /*
286  * NAV-SOL message parsing
287  */
288 static struct nav_sol {
289         uint8_t         gps_fix;
290         uint8_t         flags;
291         uint8_t         nsat;
292 } nav_sol;
293
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 */
301         { UBLOX_END, 0 }
302 };
303
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
308
309 static void
310 ao_ublox_parse_nav_sol(void)
311 {
312         ao_ublox_parse(&nav_sol, nav_sol_packet);
313 }
314
315 /*
316  * NAV-SVINFO message parsing
317  */
318
319 static struct nav_svinfo {
320         uint8_t num_ch;
321         uint8_t flags;
322 } nav_svinfo;
323
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 */
329         { UBLOX_END, 0 }
330 };
331
332 #define NAV_SVINFO_MAX_SAT      16
333
334 static struct nav_svinfo_sat {
335         uint8_t chn;
336         uint8_t svid;
337         uint8_t flags;
338         uint8_t quality;
339         uint8_t cno;
340 } nav_svinfo_sat[NAV_SVINFO_MAX_SAT];
341
342 static uint8_t  nav_svinfo_nsat;
343
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 */
351         { UBLOX_END, 0 }
352 };
353
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
362
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
369
370 static void
371 ao_ublox_parse_nav_svinfo(void)
372 {
373         uint8_t nsat;
374         nav_svinfo_nsat = 0;
375
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);
380                 } else {
381                         ublox_discard(12);
382                 }
383         }
384 #if AO_UBLOX_DEBUG
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",
388                             nsat,
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);
394 #endif
395 }
396
397 /*
398  * NAV-TIMEUTC message parsing
399  */
400 static struct nav_timeutc {
401         uint16_t        year;
402         uint8_t         month;
403         uint8_t         day;
404         uint8_t         hour;
405         uint8_t         min;
406         uint8_t         sec;
407         uint8_t         valid;
408 } nav_timeutc;
409
410 #define NAV_TIMEUTC_VALID_TOW   0
411 #define NAV_TIMEUTC_VALID_WKN   1
412 #define NAV_TIMEUTC_VALID_UTC   2
413
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 */
423         { UBLOX_END, 0 }
424 };
425
426 static void
427 ao_ublox_parse_nav_timeutc(void)
428 {
429         ao_ublox_parse(&nav_timeutc, nav_timeutc_packet);
430 }
431
432 /*
433  * NAV-VELNED message parsing
434  */
435
436 static struct nav_velned {
437         int32_t         vel_d;
438         uint32_t        g_speed;
439         int32_t         heading;
440 } nav_velned;
441
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 */
449         { UBLOX_END, 0 }
450 };
451
452 static void
453 ao_ublox_parse_nav_velned(void)
454 {
455         ao_ublox_parse(&nav_velned, nav_velned_packet);
456 }
457
458 /*
459  * Set the protocol mode and baud rate
460  */
461
462 static void
463 ao_gps_delay(void)
464 {
465         uint8_t i;
466
467         /*
468          * A bunch of nulls so the start bit
469          * is clear
470          */
471
472         for (i = 0; i < 64; i++)
473                 ao_gps_putchar(0x00);
474 }
475
476 static void
477 ao_gps_setup(void)
478 {
479         uint8_t i, k;
480
481         ao_delay(AO_SEC_TO_TICKS(3));
482
483         ao_gps_dbg(DBG_INIT, "Set speed 9600\n");
484         ao_gps_set_speed(AO_SERIAL_SPEED_9600);
485
486         /*
487          * Send the baud-rate setting and protocol-setting
488          * command three times
489          */
490         for (k = 0; k < 3; k++) {
491                 ao_gps_delay();
492
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]);
496         }
497
498         ao_gps_delay();
499
500 #if AO_SERIAL_SPEED_UBLOX != AO_SERIAL_SPEED_9600
501         ao_gps_dbg(DBG_INIT, "Set speed high\n");
502         /*
503          * Increase the baud rate
504          */
505         ao_gps_set_speed(AO_SERIAL_SPEED_UBLOX);
506 #endif
507
508         ao_gps_delay();
509 }
510
511 static void
512 ao_ublox_putstart(uint8_t class, uint8_t id, uint16_t len)
513 {
514         ao_ublox_init_cksum();
515         ao_gps_putchar(0xb5);
516         ao_gps_putchar(0x62);
517         ao_ublox_put_u8(class);
518         ao_ublox_put_u8(id);
519         ao_ublox_put_u8((uint8_t) len);
520         ao_ublox_put_u8((uint8_t) (len >> 8));
521 }
522
523 static void
524 ao_ublox_putend(void)
525 {
526         ao_gps_putchar(ao_ublox_cksum.a);
527         ao_gps_putchar(ao_ublox_cksum.b);
528 }
529
530 static void
531 ao_ublox_set_message_rate(uint8_t class, uint8_t msgid, uint8_t rate)
532 {
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);
537         ao_ublox_putend();
538 }
539
540 static void
541 ao_ublox_set_navigation_settings(uint16_t mask,
542                                  uint8_t dyn_model,
543                                  uint8_t fix_mode,
544                                  int32_t fixed_alt,
545                                  uint32_t fixed_alt_var,
546                                  int8_t min_elev,
547                                  uint8_t dr_limit,
548                                  uint16_t pdop,
549                                  uint16_t tdop,
550                                  uint16_t pacc,
551                                  uint16_t tacc,
552                                  uint8_t static_hold_thresh,
553                                  uint8_t dgps_time_out)
554 {
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);
569         ao_ublox_put_u32(0);
570         ao_ublox_put_u32(0);
571         ao_ublox_put_u32(0);
572         ao_ublox_putend();
573 }
574
575
576 /*
577  * Disable all MON message
578  */
579 static const uint8_t ublox_disable_mon[] = {
580         0x0b, 0x09, 0x02, 0x06, 0x07, 0x21, 0x08, 0x04
581 };
582
583 /*
584  * Disable all NAV messages. The desired
585  * ones will be explicitly re-enabled
586  */
587
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
591 };
592
593 /*
594  * Enable enough messages to get all of the data we want
595  */
596 static const uint8_t ublox_enable_nav[] = {
597         UBLOX_NAV_DOP,
598         UBLOX_NAV_POSLLH,
599         UBLOX_NAV_SOL,
600         UBLOX_NAV_SVINFO,
601         UBLOX_NAV_VELNED,
602         UBLOX_NAV_TIMEUTC
603 };
604
605 void
606 ao_gps_set_rate(uint8_t rate)
607 {
608         uint8_t i;
609         for (i = 0; i < sizeof (ublox_enable_nav); i++)
610                 ao_ublox_set_message_rate(UBLOX_NAV, ublox_enable_nav[i], rate);
611 }
612
613 void
614 ao_gps(void) 
615 {
616         uint8_t                 class, id;
617         struct ao_ublox_cksum   cksum;
618         uint8_t                 i;
619         AO_TICK_TYPE            packet_start_tick;
620         AO_TICK_TYPE            solution_tick = 0;
621
622         ao_gps_setup();
623
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);
629
630         /* Enable all of the messages we want */
631         ao_gps_set_rate(1);
632
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,
636                                          0,
637                                          0,
638                                          0,
639                                          0,
640                                          0,
641                                          0,
642                                          0,
643                                          0,
644                                          0,
645                                          0);
646
647         for (;;) {
648                 /* Locate the begining of the next record */
649                 while (ao_ublox_byte() != (uint8_t) 0xb5)
650                         ;
651                 packet_start_tick = ao_tick_count;
652
653                 if (ao_ublox_byte() != (uint8_t) 0x62)
654                         continue;
655
656                 ao_ublox_init_cksum();
657
658                 class = header_byte();
659                 id = header_byte();
660
661                 /* Length */
662                 ao_ublox_len = header_byte();
663                 ao_ublox_len |= (uint16_t) ((uint16_t) header_byte() << 8);
664
665                 ao_gps_dbg(DBG_PROTO, "%6u class %02x id %02x len %d\n", packet_start_tick, class, id, ao_ublox_len);
666
667                 if (ao_ublox_len > 1023)
668                         continue;
669
670                 switch (class) {
671                 case UBLOX_NAV:
672                         switch (id) {
673                         case UBLOX_NAV_DOP:
674                                 if (ao_ublox_len != 18)
675                                         break;
676                                 ao_ublox_parse_nav_dop();
677                                 break;
678                         case UBLOX_NAV_POSLLH:
679                                 if (ao_ublox_len != 28)
680                                         break;
681                                 ao_ublox_parse_nav_posllh();
682                                 break;
683                         case UBLOX_NAV_SOL:
684                                 if (ao_ublox_len != 52)
685                                         break;
686                                 ao_ublox_parse_nav_sol();
687                                 solution_tick = packet_start_tick;
688                                 break;
689                         case UBLOX_NAV_SVINFO:
690                                 if (ao_ublox_len < 8)
691                                         break;
692                                 ao_ublox_parse_nav_svinfo();
693                                 break;
694                         case UBLOX_NAV_VELNED:
695                                 if (ao_ublox_len != 36)
696                                         break;
697                                 ao_ublox_parse_nav_velned();
698                                 break;
699                         case UBLOX_NAV_TIMEUTC:
700                                 if (ao_ublox_len != 20)
701                                         break;
702                                 ao_ublox_parse_nav_timeutc();
703                                 break;
704                         }
705                         break;
706                 }
707
708                 if (ao_ublox_len != 0) {
709                         ao_gps_dbg(DBG_PROTO, "len left %d\n", ao_ublox_len);
710                         continue;
711                 }
712
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)
717                         continue;
718
719                 switch (class) {
720                 case UBLOX_NAV:
721                         switch (id) {
722                         case UBLOX_NAV_TIMEUTC:
723                                 ao_mutex_get(&ao_gps_mutex);
724                                 ao_gps_tick = solution_tick;
725
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;
731                                         if (nsat > 15)
732                                                 nsat = 15;
733                                         ao_gps_data.flags |= nsat;
734                                 }
735                                 if (nav_timeutc.valid & (1 << NAV_TIMEUTC_VALID_UTC))
736                                         ao_gps_data.flags |= AO_GPS_DATE_VALID;
737
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;
741
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;
745
746                                 ao_gps_data.hour = nav_timeutc.hour;
747                                 ao_gps_data.minute = nav_timeutc.min;
748                                 ao_gps_data.second = nav_timeutc.sec;
749
750                                 /* we report dop scaled by 10, but ublox provides dop scaled by 100
751                                  */
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);
755
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);
759
760                                 ao_gps_tracking_data.channels = 0;
761
762                                 struct ao_telemetry_satellite_info *dst = &ao_gps_tracking_data.sats[0];
763                                 struct nav_svinfo_sat *src = &nav_svinfo_sat[0];
764
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)
768                                         {
769                                                 if (ao_gps_tracking_data.channels < AO_TELEMETRY_SATELLITE_MAX_SAT) {
770                                                         dst->svid = src->svid;
771                                                         dst->c_n_1 = src->cno;
772                                                         dst++;
773                                                         ao_gps_tracking_data.channels++;
774                                                 }
775                                         }
776                                         src++;
777                                 }
778
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);
782                                 break;
783                         }
784                         break;
785                 }
786         }
787 }
788
789 #if AO_UBLOX_DEBUG
790 static void ao_gps_option(void)
791 {
792         uint8_t r = (uint8_t) ao_cmd_hex();
793         if (ao_cmd_status != ao_cmd_success) {
794                 ao_cmd_status = ao_cmd_success;
795                 ao_gps_show();
796         } else {
797                 ao_gps_dbg_enable = r;
798                 printf ("gps debug set to %d\n", ao_gps_dbg_enable);
799         }
800 }
801 #else
802 #define ao_gps_option ao_gps_show
803 #endif
804
805 const struct ao_cmds ao_gps_cmds[] = {
806         { ao_gps_option,        "g\0Display GPS" },
807         { 0, NULL },
808 };
809
810 struct ao_task ao_gps_task;
811
812 void
813 ao_gps_init(void)
814 {
815         ao_cmd_register(&ao_gps_cmds[0]);
816         ao_add_task(&ao_gps_task, ao_gps, "gps");
817 }