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