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