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