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