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