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