eb00224cf97fc99740ef6c9ccec65ea50d0c40ac
[fw/altos] / src / ao_gps_sirf.c
1 /*
2  * Copyright © 2009 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 __xdata uint8_t ao_gps_mutex;
23 __xdata struct ao_gps_data      ao_gps_data;
24 __xdata struct ao_gps_tracking_data     ao_gps_tracking_data;
25
26 static const char ao_gps_set_nmea[] = "\r\n$PSRF100,0,57600,8,1,0*37\r\n";
27
28 const char ao_gps_config[] = {
29
30         0xa0, 0xa2, 0x00, 0x0e, /* length: 14 bytes */
31         136,                    /* mode control */
32         0, 0,                   /* reserved */
33         0,                      /* degraded mode (allow 1-SV navigation) */
34         0, 0,                   /* reserved */
35         0, 0,                   /* user specified altitude */
36         2,                      /* alt hold mode (disabled, require 3d fixes) */
37         0,                      /* alt hold source (use last computed altitude) */
38         0,                      /* reserved */
39         10,                     /* Degraded time out (10 sec) */
40         10,                     /* Dead Reckoning time out (10 sec) */
41         0,                      /* Track smoothing (disabled) */
42         0x00, 0x8e, 0xb0, 0xb3,
43
44         0xa0, 0xa2, 0x00, 0x08, /* length: 8 bytes */
45         166,                    /* Set message rate */
46         2,                      /* enable/disable all messages */
47         0,                      /* message id (ignored) */
48         0,                      /* update rate (0 = disable) */
49         0, 0, 0, 0,             /* reserved */
50         0x00, 0xa8, 0xb0, 0xb3,
51
52         0xa0, 0xa2, 0x00, 0x02, /* length: 2 bytes */
53         143,                    /* static navigation */
54         0,                      /* disable */
55         0x00, 0x8f, 0xb0, 0xb3,
56 };
57
58 #define NAV_TYPE_GPS_FIX_TYPE_MASK                      (7 << 0)
59 #define NAV_TYPE_NO_FIX                                 (0 << 0)
60 #define NAV_TYPE_SV_KF                                  (1 << 0)
61 #define NAV_TYPE_2_SV_KF                                (2 << 0)
62 #define NAV_TYPE_3_SV_KF                                (3 << 0)
63 #define NAV_TYPE_4_SV_KF                                (4 << 0)
64 #define NAV_TYPE_2D_LEAST_SQUARES                       (5 << 0)
65 #define NAV_TYPE_3D_LEAST_SQUARES                       (6 << 0)
66 #define NAV_TYPE_DR                                     (7 << 0)
67 #define NAV_TYPE_TRICKLE_POWER                          (1 << 3)
68 #define NAV_TYPE_ALTITUDE_HOLD_MASK                     (3 << 4)
69 #define NAV_TYPE_ALTITUDE_HOLD_NONE                     (0 << 4)
70 #define NAV_TYPE_ALTITUDE_HOLD_KF                       (1 << 4)
71 #define NAV_TYPE_ALTITUDE_HOLD_USER                     (2 << 4)
72 #define NAV_TYPE_ALTITUDE_HOLD_ALWAYS                   (3 << 4)
73 #define NAV_TYPE_DOP_LIMIT_EXCEEDED                     (1 << 6)
74 #define NAV_TYPE_DGPS_APPLIED                           (1 << 7)
75 #define NAV_TYPE_SENSOR_DR                              (1 << 8)
76 #define NAV_TYPE_OVERDETERMINED                         (1 << 9)
77 #define NAV_TYPE_DR_TIMEOUT_EXCEEDED                    (1 << 10)
78 #define NAV_TYPE_FIX_MI_EDIT                            (1 << 11)
79 #define NAV_TYPE_INVALID_VELOCITY                       (1 << 12)
80 #define NAV_TYPE_ALTITUDE_HOLD_DISABLED                 (1 << 13)
81 #define NAV_TYPE_DR_ERROR_STATUS_MASK                   (3 << 14)
82 #define NAV_TYPE_DR_ERROR_STATUS_GPS_ONLY               (0 << 14)
83 #define NAV_TYPE_DR_ERROR_STATUS_DR_FROM_GPS            (1 << 14)
84 #define NAV_TYPE_DR_ERROR_STATUS_DR_SENSOR_ERROR        (2 << 14)
85 #define NAV_TYPE_DR_ERROR_STATUS_DR_IN_TEST             (3 << 14)
86
87 struct sirf_geodetic_nav_data {
88         uint16_t        nav_type;
89         uint16_t        utc_year;
90         uint8_t         utc_month;
91         uint8_t         utc_day;
92         uint8_t         utc_hour;
93         uint8_t         utc_minute;
94         uint16_t        utc_second;
95         int32_t         lat;
96         int32_t         lon;
97         int32_t         alt_msl;
98         uint16_t        ground_speed;
99         uint16_t        course;
100         int16_t         climb_rate;
101         uint32_t        h_error;
102         uint32_t        v_error;
103         uint8_t         num_sv;
104         uint8_t         hdop;
105 };
106
107 static __xdata struct sirf_geodetic_nav_data    ao_sirf_data;
108
109 struct sirf_measured_sat_data {
110         uint8_t         svid;
111         uint8_t         c_n_1;
112 };
113
114 struct sirf_measured_tracker_data {
115         int16_t                         gps_week;
116         uint32_t                        gps_tow;
117         uint8_t                         channels;
118         struct sirf_measured_sat_data   sats[12];
119 };
120
121 static __xdata struct sirf_measured_tracker_data        ao_sirf_tracker_data;
122
123 static __pdata uint16_t ao_sirf_cksum;
124 static __pdata uint16_t ao_sirf_len;
125
126 #define ao_sirf_byte()  ((uint8_t) ao_serial_getchar())
127
128 static uint8_t data_byte(void)
129 {
130         uint8_t c = ao_sirf_byte();
131         --ao_sirf_len;
132         ao_sirf_cksum += c;
133         return c;
134 }
135
136 static char __xdata *sirf_target;
137
138 static void sirf_u16(uint8_t offset)
139 {
140         uint16_t __xdata *ptr = (uint16_t __xdata *) (sirf_target + offset);
141         uint16_t val;
142
143         val = data_byte() << 8;
144         val |= data_byte ();
145         *ptr = val;
146 }
147
148 static void sirf_u8(uint8_t offset)
149 {
150         uint8_t __xdata *ptr = (uint8_t __xdata *) (sirf_target + offset);
151         uint8_t val;
152
153         val = data_byte ();
154         *ptr = val;
155 }
156
157 static void sirf_u32(uint8_t offset) __reentrant
158 {
159         uint32_t __xdata *ptr = (uint32_t __xdata *) (sirf_target + offset);
160         uint32_t val;
161
162         val = ((uint32_t) data_byte ()) << 24;
163         val |= ((uint32_t) data_byte ()) << 16;
164         val |= ((uint32_t) data_byte ()) << 8;
165         val |= ((uint32_t) data_byte ());
166         *ptr = val;
167 }
168
169 static void sirf_discard(uint8_t len)
170 {
171         while (len--)
172                 data_byte();
173 }
174
175 #define SIRF_END        0
176 #define SIRF_DISCARD    1
177 #define SIRF_U8         2
178 #define SIRF_U16        3
179 #define SIRF_U32        4
180 #define SIRF_U8X10      5
181
182 struct sirf_packet_parse {
183         uint8_t type;
184         uint8_t offset;
185 };
186
187 static void
188 ao_sirf_parse(void __xdata *target, const struct sirf_packet_parse *parse) __reentrant
189 {
190         uint8_t i, offset, j;
191
192         sirf_target = target;
193         for (i = 0; ; i++) {
194                 offset = parse[i].offset;
195                 switch (parse[i].type) {
196                 case SIRF_END:
197                         return;
198                 case SIRF_DISCARD:
199                         sirf_discard(offset);
200                         break;
201                 case SIRF_U8:
202                         sirf_u8(offset);
203                         break;
204                 case SIRF_U16:
205                         sirf_u16(offset);
206                         break;
207                 case SIRF_U32:
208                         sirf_u32(offset);
209                         break;
210                 case SIRF_U8X10:
211                         for (j = 10; j--;)
212                                 sirf_u8(offset++);
213                         break;
214                 }
215         }
216 }
217
218 static const struct sirf_packet_parse geodetic_nav_data_packet[] = {
219         { SIRF_DISCARD, 2 },                                                    /* 1 nav valid */
220         { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, nav_type) },        /* 3 */
221         { SIRF_DISCARD, 6 },                                                    /* 5 week number, time of week */
222         { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, utc_year) },        /* 11 */
223         { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_month) },        /* 13 */
224         { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_day) },          /* 14 */
225         { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_hour) },         /* 15 */
226         { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_minute) },       /* 16 */
227         { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, utc_second) },      /* 17 */
228         { SIRF_DISCARD, 4 },    /* satellite id list */                         /* 19 */
229         { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, lat) },             /* 23 */
230         { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, lon) },             /* 27 */
231         { SIRF_DISCARD, 4 },    /* altitude from ellipsoid */                   /* 31 */
232         { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, alt_msl) },         /* 35 */
233         { SIRF_DISCARD, 1 },    /* map datum */                                 /* 39 */
234         { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, ground_speed) },    /* 40 */
235         { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, course) },          /* 42 */
236         { SIRF_DISCARD, 2 },    /* magnetic variation */                        /* 44 */
237         { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, climb_rate) },      /* 46 */
238         { SIRF_DISCARD, 2 },    /* turn rate */                                 /* 48 */
239         { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, h_error) },         /* 50 */
240         { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, v_error) },         /* 54 */
241         { SIRF_DISCARD, 30 },   /* time error, h_vel error, clock_bias,
242                                    clock bias error, clock drift,
243                                    clock drift error, distance,
244                                    distance error, heading error */             /* 58 */
245         { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, num_sv) },           /* 88 */
246         { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, hdop) },             /* 89 */
247         { SIRF_DISCARD, 1 },    /* additional mode info */                      /* 90 */
248         { SIRF_END, 0 },                                                        /* 91 */
249 };
250
251 static void
252 ao_sirf_parse_41(void) __reentrant
253 {
254         ao_sirf_parse(&ao_sirf_data, geodetic_nav_data_packet);
255 }
256
257 static const struct sirf_packet_parse measured_tracker_data_packet[] = {
258         { SIRF_U16, offsetof (struct sirf_measured_tracker_data, gps_week) },   /* 1 week */
259         { SIRF_U32, offsetof (struct sirf_measured_tracker_data, gps_tow) },    /* 3 time of week */
260         { SIRF_U8, offsetof (struct sirf_measured_tracker_data, channels) },    /* 7 channels */
261         { SIRF_END, 0 },
262 };
263
264 static const struct sirf_packet_parse measured_sat_data_packet[] = {
265         { SIRF_U8, offsetof (struct sirf_measured_sat_data, svid) },            /* 0 SV id */
266         { SIRF_DISCARD, 4 },                                                    /* 1 azimuth, 2 elevation, 3 state */
267         { SIRF_U8, offsetof (struct sirf_measured_sat_data, c_n_1) },           /* C/N0 1 */
268         { SIRF_DISCARD, 9 },                                                    /* C/N0 2-10 */
269         { SIRF_END, 0 },
270 };
271
272 static void
273 ao_sirf_parse_4(void) __reentrant
274 {
275         uint8_t i;
276         ao_sirf_parse(&ao_sirf_tracker_data, measured_tracker_data_packet);
277         for (i = 0; i < 12; i++)
278                 ao_sirf_parse(&ao_sirf_tracker_data.sats[i], measured_sat_data_packet);
279 }
280
281 static void
282 ao_gps_setup(void) __reentrant
283 {
284         uint8_t i, k;
285         ao_serial_set_speed(AO_SERIAL_SPEED_4800);
286         for (i = 0; i < 64; i++)
287                 ao_serial_putchar(0x00);
288         for (k = 0; k < 3; k++)
289                 for (i = 0; i < sizeof (ao_gps_set_nmea); i++)
290                         ao_serial_putchar(ao_gps_set_nmea[i]);
291         ao_serial_set_speed(AO_SERIAL_SPEED_57600);
292         for (i = 0; i < 64; i++)
293                 ao_serial_putchar(0x00);
294 }
295
296 static const char ao_gps_set_message_rate[] = {
297         0xa0, 0xa2, 0x00, 0x08,
298         166,
299         0,
300 };
301
302 void
303 ao_sirf_set_message_rate(uint8_t msg, uint8_t rate) __reentrant
304 {
305         uint16_t        cksum = 0x00a6;
306         uint8_t         i;
307
308         for (i = 0; i < sizeof (ao_gps_set_message_rate); i++)
309                 ao_serial_putchar(ao_gps_set_message_rate[i]);
310         ao_serial_putchar(msg);
311         ao_serial_putchar(rate);
312         cksum = 0xa6 + msg + rate;
313         for (i = 0; i < 4; i++)
314                 ao_serial_putchar(0);
315         ao_serial_putchar((cksum >> 8) & 0x7f);
316         ao_serial_putchar(cksum & 0xff);
317         ao_serial_putchar(0xb0);
318         ao_serial_putchar(0xb3);
319 }
320
321 static const uint8_t sirf_disable[] = {
322         2,
323         9,
324         10,
325         27,
326         50,
327         52,
328 };
329
330 void
331 ao_gps(void) __reentrant
332 {
333         uint8_t i, k;
334         uint16_t cksum;
335
336         ao_gps_setup();
337         for (k = 0; k < 5; k++)
338         {
339                 for (i = 0; i < sizeof (ao_gps_config); i++)
340                         ao_serial_putchar(ao_gps_config[i]);
341                 for (i = 0; i < sizeof (sirf_disable); i++)
342                         ao_sirf_set_message_rate(sirf_disable[i], 0);
343                 ao_sirf_set_message_rate(41, 1);
344                 ao_sirf_set_message_rate(4, 1);
345         }
346         for (;;) {
347                 /* Locate the begining of the next record */
348                 while (ao_sirf_byte() != (uint8_t) 0xa0)
349                         ;
350                 if (ao_sirf_byte() != (uint8_t) 0xa2)
351                         continue;
352
353                 /* Length */
354                 ao_sirf_len = ao_sirf_byte() << 8;
355                 ao_sirf_len |= ao_sirf_byte();
356                 if (ao_sirf_len > 1023)
357                         continue;
358
359                 ao_sirf_cksum = 0;
360
361                 /* message ID */
362                 i = data_byte ();                                                       /* 0 */
363
364                 switch (i) {
365                 case 41:
366                         if (ao_sirf_len < 90)
367                                 break;
368                         ao_sirf_parse_41();
369                         break;
370                 case 4:
371                         if (ao_sirf_len < 187)
372                                 break;
373                         ao_sirf_parse_4();
374                         break;
375                 }
376                 if (ao_sirf_len != 0)
377                         continue;
378
379                 /* verify checksum and end sequence */
380                 ao_sirf_cksum &= 0x7fff;
381                 cksum = ao_sirf_byte() << 8;
382                 cksum |= ao_sirf_byte();
383                 if (ao_sirf_cksum != cksum)
384                         continue;
385                 if (ao_sirf_byte() != (uint8_t) 0xb0)
386                         continue;
387                 if (ao_sirf_byte() != (uint8_t) 0xb3)
388                         continue;
389
390                 switch (i) {
391                 case 41:
392                         ao_mutex_get(&ao_gps_mutex);
393                         ao_gps_data.hour = ao_sirf_data.utc_hour;
394                         ao_gps_data.minute = ao_sirf_data.utc_minute;
395                         ao_gps_data.second = ao_sirf_data.utc_second / 1000;
396                         ao_gps_data.flags = ((ao_sirf_data.num_sv << AO_GPS_NUM_SAT_SHIFT) & AO_GPS_NUM_SAT_MASK) | AO_GPS_RUNNING;
397                         if ((ao_sirf_data.nav_type & NAV_TYPE_GPS_FIX_TYPE_MASK) >= NAV_TYPE_4_SV_KF)
398                                 ao_gps_data.flags |= AO_GPS_VALID;
399                         ao_gps_data.latitude = ao_sirf_data.lat;
400                         ao_gps_data.longitude = ao_sirf_data.lon;
401                         ao_gps_data.altitude = ao_sirf_data.alt_msl / 100;
402                         ao_gps_data.ground_speed = ao_sirf_data.ground_speed;
403                         ao_gps_data.course = ao_sirf_data.course / 200;
404                         ao_gps_data.hdop = ao_sirf_data.hdop;
405                         ao_gps_data.climb_rate = ao_sirf_data.climb_rate;
406                         if (ao_sirf_data.h_error > 6553500)
407                                 ao_gps_data.h_error = 65535;
408                         else
409                                 ao_gps_data.h_error = ao_sirf_data.h_error / 100;
410                         if (ao_sirf_data.v_error > 6553500)
411                                 ao_gps_data.v_error = 65535;
412                         else
413                                 ao_gps_data.v_error = ao_sirf_data.v_error / 100;
414                         ao_mutex_put(&ao_gps_mutex);
415                         ao_wakeup(&ao_gps_data);
416                         break;
417                 case 4:
418                         ao_mutex_get(&ao_gps_mutex);
419                         ao_gps_tracking_data.channels = ao_sirf_tracker_data.channels;
420                         for (i = 0; i < 12; i++) {
421                                 ao_gps_tracking_data.sats[i].svid = ao_sirf_tracker_data.sats[i].svid;
422                                 ao_gps_tracking_data.sats[i].c_n_1 = ao_sirf_tracker_data.sats[i].c_n_1;
423                         }
424                         ao_mutex_put(&ao_gps_mutex);
425                         ao_wakeup(&ao_gps_tracking_data);
426                         break;
427                 }
428         }
429 }
430
431 __xdata struct ao_task ao_gps_task;
432
433 static void
434 gps_dump(void) __reentrant
435 {
436         ao_mutex_get(&ao_gps_mutex);
437         ao_gps_print(&ao_gps_data);
438         putchar('\n');
439         ao_gps_tracking_print(&ao_gps_tracking_data);
440         putchar('\n');
441         ao_mutex_put(&ao_gps_mutex);
442 }
443
444 __code struct ao_cmds ao_gps_cmds[] = {
445         { 'g', gps_dump,        "g                                  Display current GPS values" },
446         { 0, gps_dump, NULL },
447 };
448
449 void
450 ao_gps_init(void)
451 {
452         ao_add_task(&ao_gps_task, ao_gps, "gps");
453         ao_cmd_register(&ao_gps_cmds[0]);
454 }