c06c635fd9fc3d67bf560671cca0572fbde8e974
[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 #undef AO_GPS_4800
26
27 #ifdef AO_GPS_4800
28 static const char ao_gps_set_nmea[] = "$PSRF100,0,57600,8,1,0*37\r\n";
29 #else
30 static const char ao_gps_set_nmea[] = "$PSRF100,0,4800,8,1,0*0F\r\n";
31 #endif
32
33 const char ao_gps_config[] = {
34         0xa0, 0xa2, 0x00, 0x19, /* length: 25 bytes */
35         128,                    /* Initialize Data Source */
36         0, 0, 0, 0,             /* ECEF X */
37         0, 0, 0, 0,             /* ECEF Y */
38         0, 0, 0, 0,             /* ECEF Z */
39         0, 0, 0, 0,             /* Clock Drift */
40         0, 0, 0, 0,             /* Time of Week */
41         0, 0,                   /* Week Number */
42         0,                      /* Channels */
43         0xc6,                   /* Clear user data, RTC not accurate, Clear history, clear ephemeris */
44         0x01, 0x46, 0xb0, 0xb3,
45
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, 0x08, /* length: 8 bytes */
61         166,                    /* Set message rate */
62         2,                      /* enable/disable all messages */
63         0,                      /* message id (ignored) */
64         0,                      /* update rate (0 = disable) */
65         0, 0, 0, 0,             /* reserved */
66         0x00, 0xa8, 0xb0, 0xb3,
67
68         0xa0, 0xa2, 0x00, 0x02, /* length: 2 bytes */
69         143,                    /* static navigation */
70         0,                      /* disable */
71         0x00, 0x8f, 0xb0, 0xb3,
72 };
73
74 #define NAV_TYPE_GPS_FIX_TYPE_MASK                      (7 << 0)
75 #define NAV_TYPE_NO_FIX                                 (0 << 0)
76 #define NAV_TYPE_SV_KF                                  (1 << 0)
77 #define NAV_TYPE_2_SV_KF                                (2 << 0)
78 #define NAV_TYPE_3_SV_KF                                (3 << 0)
79 #define NAV_TYPE_4_SV_KF                                (4 << 0)
80 #define NAV_TYPE_2D_LEAST_SQUARES                       (5 << 0)
81 #define NAV_TYPE_3D_LEAST_SQUARES                       (6 << 0)
82 #define NAV_TYPE_DR                                     (7 << 0)
83 #define NAV_TYPE_TRICKLE_POWER                          (1 << 3)
84 #define NAV_TYPE_ALTITUDE_HOLD_MASK                     (3 << 4)
85 #define NAV_TYPE_ALTITUDE_HOLD_NONE                     (0 << 4)
86 #define NAV_TYPE_ALTITUDE_HOLD_KF                       (1 << 4)
87 #define NAV_TYPE_ALTITUDE_HOLD_USER                     (2 << 4)
88 #define NAV_TYPE_ALTITUDE_HOLD_ALWAYS                   (3 << 4)
89 #define NAV_TYPE_DOP_LIMIT_EXCEEDED                     (1 << 6)
90 #define NAV_TYPE_DGPS_APPLIED                           (1 << 7)
91 #define NAV_TYPE_SENSOR_DR                              (1 << 8)
92 #define NAV_TYPE_OVERDETERMINED                         (1 << 9)
93 #define NAV_TYPE_DR_TIMEOUT_EXCEEDED                    (1 << 10)
94 #define NAV_TYPE_FIX_MI_EDIT                            (1 << 11)
95 #define NAV_TYPE_INVALID_VELOCITY                       (1 << 12)
96 #define NAV_TYPE_ALTITUDE_HOLD_DISABLED                 (1 << 13)
97 #define NAV_TYPE_DR_ERROR_STATUS_MASK                   (3 << 14)
98 #define NAV_TYPE_DR_ERROR_STATUS_GPS_ONLY               (0 << 14)
99 #define NAV_TYPE_DR_ERROR_STATUS_DR_FROM_GPS            (1 << 14)
100 #define NAV_TYPE_DR_ERROR_STATUS_DR_SENSOR_ERROR        (2 << 14)
101 #define NAV_TYPE_DR_ERROR_STATUS_DR_IN_TEST             (3 << 14)
102
103 struct sirf_geodetic_nav_data {
104         uint16_t        nav_type;
105         uint16_t        utc_year;
106         uint8_t         utc_month;
107         uint8_t         utc_day;
108         uint8_t         utc_hour;
109         uint8_t         utc_minute;
110         uint16_t        utc_second;
111         int32_t         lat;
112         int32_t         lon;
113         int32_t         alt_msl;
114         uint16_t        ground_speed;
115         uint16_t        course;
116         int16_t         climb_rate;
117         uint32_t        h_error;
118         uint32_t        v_error;
119         uint8_t         num_sv;
120         uint8_t         hdop;
121 };
122
123 static __xdata struct sirf_geodetic_nav_data    ao_sirf_data;
124
125 static __pdata uint16_t ao_sirf_cksum;
126 static __pdata uint16_t ao_sirf_len;
127
128 #define ao_sirf_byte()  ((uint8_t) ao_serial_getchar())
129
130 static uint8_t data_byte(void)
131 {
132         uint8_t c = ao_sirf_byte();
133         --ao_sirf_len;
134         ao_sirf_cksum += c;
135         return c;
136 }
137
138 static void sirf_u16(uint8_t offset)
139 {
140         uint16_t __xdata *ptr = (uint16_t __xdata *) (((char __xdata *) &ao_sirf_data) + offset);
141         uint16_t val;
142
143         val = data_byte() << 8;
144         val |= data_byte ();
145         *ptr = val;
146 }
147
148 static void sirf_u8(uint8_t offset)
149 {
150         uint8_t __xdata *ptr = (uint8_t __xdata *) (((char __xdata *) &ao_sirf_data) + offset);
151         uint8_t val;
152
153         val = data_byte ();
154         *ptr = val;
155 }
156
157 static void sirf_u32(uint8_t offset)
158 {
159         uint32_t __xdata *ptr = (uint32_t __xdata *) (((char __xdata *) &ao_sirf_data) + offset);
160         uint32_t val;
161
162         val = ((uint32_t) data_byte ()) << 24;
163         val |= ((uint32_t) data_byte ()) << 16;
164         val |= ((uint32_t) data_byte ()) << 8;
165         val |= ((uint32_t) data_byte ());
166         *ptr = val;
167 }
168
169 static void sirf_discard(uint8_t len)
170 {
171         while (len--)
172                 data_byte();
173 }
174
175 #define SIRF_END        0
176 #define SIRF_DISCARD    1
177 #define SIRF_U8         2
178 #define SIRF_U16        3
179 #define SIRF_U32        4
180
181 struct sirf_packet_parse {
182         uint8_t type;
183         uint8_t offset;
184 };
185
186 static const struct sirf_packet_parse geodetic_nav_data_packet[] = {
187         { SIRF_DISCARD, 2 },                                                    /* 1 nav valid */
188         { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, nav_type) },        /* 3 */
189         { SIRF_DISCARD, 6 },                                                    /* 5 week number, time of week */
190         { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, utc_year) },        /* 11 */
191         { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_month) },        /* 13 */
192         { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_day) },          /* 14 */
193         { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_hour) },         /* 15 */
194         { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, utc_minute) },       /* 16 */
195         { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, utc_second) },      /* 17 */
196         { SIRF_DISCARD, 4 },    /* satellite id list */                         /* 19 */
197         { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, lat) },             /* 23 */
198         { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, lon) },             /* 27 */
199         { SIRF_DISCARD, 4 },    /* altitude from ellipsoid */                   /* 31 */
200         { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, alt_msl) },         /* 35 */
201         { SIRF_DISCARD, 1 },    /* map datum */                                 /* 39 */
202         { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, ground_speed) },    /* 40 */
203         { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, course) },          /* 42 */
204         { SIRF_DISCARD, 2 },    /* magnetic variation */                        /* 44 */
205         { SIRF_U16, offsetof(struct sirf_geodetic_nav_data, climb_rate) },      /* 46 */
206         { SIRF_DISCARD, 2 },    /* turn rate */                                 /* 48 */
207         { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, h_error) },         /* 50 */
208         { SIRF_U32, offsetof(struct sirf_geodetic_nav_data, v_error) },         /* 54 */
209         { SIRF_DISCARD, 30 },   /* time error, h_vel error, clock_bias,
210                                    clock bias error, clock drift,
211                                    clock drift error, distance,
212                                    distance error, heading error */             /* 58 */
213         { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, num_sv) },           /* 88 */
214         { SIRF_U8, offsetof(struct sirf_geodetic_nav_data, hdop) },             /* 89 */
215         { SIRF_DISCARD, 1 },    /* additional mode info */                      /* 90 */
216         { SIRF_END, 0 },                                                        /* 91 */
217 };
218
219 static void
220 ao_sirf_parse_41(void)
221 {
222         uint8_t i, offset;
223
224         for (i = 0; ; i++) {
225                 offset = geodetic_nav_data_packet[i].offset;
226                 switch (geodetic_nav_data_packet[i].type) {
227                 case SIRF_END:
228                         return;
229                 case SIRF_DISCARD:
230                         sirf_discard(offset);
231                         break;
232                 case SIRF_U8:
233                         sirf_u8(offset);
234                         break;
235                 case SIRF_U16:
236                         sirf_u16(offset);
237                         break;
238                 case SIRF_U32:
239                         sirf_u32(offset);
240                         break;
241                 }
242         }
243 }
244
245 static void
246 ao_gps_setup(void) __reentrant
247 {
248         uint8_t i, k;
249 #ifdef AO_GPS_4800
250         ao_serial_set_speed(AO_SERIAL_SPEED_4800);
251 #endif
252         for (i = 0; i < 64; i++)
253                 ao_serial_putchar(0x00);
254         for (k = 0; k < 3; k++)
255                 for (i = 0; i < sizeof (ao_gps_set_nmea); i++)
256                         ao_serial_putchar(ao_gps_set_nmea[i]);
257 #ifdef AO_GPS_4800
258         ao_serial_set_speed(AO_SERIAL_SPEED_57600);
259 #endif
260         for (i = 0; i < 64; i++)
261                 ao_serial_putchar(0x00);
262 }
263
264 static const char ao_gps_set_message_rate[] = {
265         0xa0, 0xa2, 0x00, 0x08,
266         166,
267         0,
268 };
269
270 void
271 ao_sirf_set_message_rate(uint8_t msg, uint8_t rate)
272 {
273         uint16_t        cksum = 0x00a6;
274         uint8_t         i;
275
276         for (i = 0; i < sizeof (ao_gps_set_message_rate); i++)
277                 ao_serial_putchar(ao_gps_set_message_rate[i]);
278         ao_serial_putchar(msg);
279         ao_serial_putchar(rate);
280         cksum = 0xa6 + msg + rate;
281         for (i = 0; i < 4; i++)
282                 ao_serial_putchar(0);
283         ao_serial_putchar((cksum >> 8) & 0x7f);
284         ao_serial_putchar(cksum & 0xff);
285         ao_serial_putchar(0xb0);
286         ao_serial_putchar(0xb3);
287 }
288
289 static const uint8_t sirf_disable[] = {
290         2,
291         4,
292         9,
293         10,
294         27,
295         50,
296         52,
297 };
298
299 void
300 ao_gps(void) __reentrant
301 {
302         uint8_t i, k;
303         uint16_t cksum;
304
305         ao_gps_setup();
306         for (k = 0; k < 5; k++)
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         }
314         for (;;) {
315                 /* Locate the begining of the next record */
316                 while (ao_sirf_byte() != (uint8_t) 0xa0)
317                         ;
318                 if (ao_sirf_byte() != (uint8_t) 0xa2)
319                         continue;
320
321                 /* Length */
322                 ao_sirf_len = ao_sirf_byte() << 8;
323                 ao_sirf_len |= ao_sirf_byte();
324                 if (ao_sirf_len > 1023)
325                         continue;
326
327                 ao_sirf_cksum = 0;
328
329                 /* message ID */
330                 i = data_byte ();                                                       /* 0 */
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() != (uint8_t) 0xb0)
349                         continue;
350                 if (ao_sirf_byte() != (uint8_t) 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) | AO_GPS_RUNNING;
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 }