gps: split gps_venus
[fpv.git] / imuboard / gps_venus.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
5
6 #include <aversive.h>
7 #include <aversive/endian.h>
8 #include <aversive/wait.h>
9 #include <callout.h>
10 #include <uart.h>
11
12 #include "main.h"
13 #include "gps.h"
14 #include "gps_venus.h"
15
16 /* note: enabling debug will make the code fail due to rx queue full */
17 #define debug_printf(fmt, a...) do { } while (0)
18 /* #define debug_printf(fmt, ...) printf_P(PSTR(fmt), ##__VA_ARGS__) */
19
20 #define GPS_UART 0
21
22 #define GPS_PERIOD_MS 2
23
24 /*
25   https://code.google.com/p/hardware-ntp/source/browse/trunk/pcb-1/venus634.c?r=12
26  */
27
28 /* some useful defines */
29 #define START1 0xA0
30 #define START2 0xA1
31 #define END1 0x0D
32 #define END2 0x0A
33
34 /* proccessing states */
35
36 #define S_RESET 0
37 #define S_POSTRESET 1
38 #define S_START1 2
39 #define S_START2 3
40 #define S_LENGTH1 4
41 #define S_LENGTH2 5
42 #define S_DISCARD 6
43 #define S_COPY 7
44 #define S_ENDCOPY 8
45 #define S_END 9
46 #define S_END1 10
47
48 #define MAX_LEN 0x200
49 struct gps_frame {
50         uint16_t len;
51         uint16_t cur_len;
52         uint8_t data[MAX_LEN];
53 };
54
55 static struct gps_frame rxframe;
56
57 static struct callout gps_timer;
58
59 /* INPUT FORMATS */
60
61 /* packet types */
62 /* 0x01 - System Restart */
63 #define M_RESTART 0x01
64 typedef struct {
65         uint8_t start_mode; /* 01 = Hot; 02 = Warm; 03 = Cold */
66         uint16_t utc_year; /* >= 1980 */
67         uint8_t utc_month;
68         uint8_t utc_day;
69         uint8_t utc_hour;
70         uint8_t utc_minute;
71         uint8_t utc_second;
72         int16_t latitude; /* -9000 to 9000, 1/100th degree, positive north */
73         int16_t longitude; /* -18000 to 18000, 1/100th degree, positive east */
74         int16_t altitude; /* -1000 to 18300, meters */
75 } m_restart_t;
76
77
78 /* 0x02 - Query Software Version */
79 #define M_QUERYVER 0x02
80 typedef struct {
81         uint8_t software_type; /* 01 = System Code */
82 } m_queryver_t;
83
84
85 /* 0x05 - Serial Port Configuration */
86 #define M_SERCONF 0x05
87 typedef struct {
88         uint8_t port; /* 00 = COM1 (the only port) */
89         uint8_t baud; /* 00 = 4800; 01 = 9600; 02 = 19200; 03 = 38400;
90                          04 = 57600; 05 = 115200; */
91         uint8_t update; /* 00 = SRAM only; 01 = SRAM and FLASH */
92 } m_serconf_t;
93
94
95
96 /* 0x08 - nmea interval */
97 #define M_NMEAINTERVAL 0x08
98 typedef struct {
99         uint8_t gga;
100         uint8_t gsa;
101         uint8_t gsv;
102         uint8_t gll;
103         uint8_t rmc;
104         uint8_t vtg;
105         uint8_t zda;
106         uint8_t attributes; /*
107                               0 - sram
108                               1 - flash
109                              */
110
111 } m_nmeainterval_t;
112
113
114 /* 0x09 - Query Software Version */
115 #define M_OUTPUT 0x09
116 typedef struct {
117         uint8_t msg_type; /*
118                             00 = no output
119                             01 = nmea output
120                             02 = binary output
121                            */
122         uint8_t attributes; /*
123                               0 - update ram
124                               1 - update ram & flash
125                             */
126 } m_output_t;
127
128
129 /* 0x0E - Update Rage */
130 #define M_RATE 0x0E
131 typedef struct {
132         uint8_t rate; /* Hz */
133         uint8_t attributes; /*
134                               0 - update ram
135                               1 - update ram & flash
136                             */
137 } m_rate_t;
138
139
140
141 /* 0x37 - configure waas */
142 #define M_WAAS 0x37
143 typedef struct {
144         uint8_t enable; /* 01 = enable */
145         uint8_t attributes;
146 } m_waas_t;
147
148 /* 0x39 - configure position pinning */
149 #define M_PINNING 0x39
150 typedef struct {
151         uint8_t enable; /* 0 = default, 1 = enable, 2 = disable */
152         uint8_t attributes; /* 0 = ram, 1 = ram+flash */
153 } m_pinning_t;
154
155 /* 0x3B - configure position pinning */
156 #define M_PINNING_PARAMS 0x3B
157 typedef struct {
158         uint16_t pin_speed;
159         uint16_t pin_cnt;
160         uint16_t unpin_speed;
161         uint16_t unpin_cnt;
162         uint16_t unpin_dist;
163         uint8_t attributes; /* 0 = ram, 1 = ram+flash */
164 } m_pinning_params_t;
165
166 /* 0x3C - nav mode */
167 #define M_NAVMODE 0x3c
168 typedef struct {
169         uint8_t navmode; /* 00 = car
170                              01 = pedestrian
171                            */
172         uint8_t attributes;
173 } m_navmode_t;
174
175
176 void serial1_tx_cout(uint8_t c)
177 {
178         debug_printf("%.2X ", c);
179         uart_send(GPS_UART, c);
180 }
181
182 void venus634_send(uint8_t type, void *payload, uint16_t len)
183 {
184         uint8_t crc = 0, n;
185
186         debug_printf("SEND ");
187
188         /* now send the message */
189         /* header */
190         serial1_tx_cout(START1);
191         serial1_tx_cout(START2);
192         serial1_tx_cout(((len+1) >> 8) & 0xff);
193         serial1_tx_cout((len+1) & 0xff);
194         /* type and payload */
195         serial1_tx_cout(type);
196         crc ^= type;
197         for (n = 0; n < len; n++) {
198                 serial1_tx_cout(*(uint8_t *)payload);
199                 crc ^= *(uint8_t *)payload;
200                 payload++;
201         }
202         /* checksum and tail */
203         serial1_tx_cout(crc);
204         serial1_tx_cout(END1);
205         serial1_tx_cout(END2);
206
207         debug_printf("\n");
208 }
209
210 void venus634_restart(void)
211 {
212         m_restart_t restart;
213
214         memset(&restart, 0, sizeof(m_restart_t));
215         restart.start_mode = 0x03; /* COLD */
216
217         venus634_send(M_RESTART, &restart, sizeof(m_restart_t));
218
219         return;
220 }
221
222 void venus634_config_serial(void)
223 {
224         m_serconf_t serconf;
225
226         memset(&serconf, 0, sizeof(m_serconf_t));
227         serconf.port = 0;
228         serconf.baud = 4;
229         serconf.update = 1;
230
231         venus634_send(M_SERCONF, &serconf, sizeof(m_serconf_t));
232
233         return;
234 }
235
236
237 void venus634_msg_type(uint8_t mode)
238 {
239         m_output_t output;
240
241         memset(&output, 0, sizeof(m_output_t));
242         output.msg_type = mode;
243         output.attributes = 0; /* ram, init may freeze when update in flash */
244
245         venus634_send(M_OUTPUT, &output, sizeof(m_output_t));
246
247         return;
248 }
249
250
251 void venus634_rate(void)
252 {
253         m_rate_t rate;
254
255         memset(&rate, 0, sizeof(m_rate_t));
256         rate.rate = 5;
257         rate.attributes = 0; /* ram, init may freeze when update in flash */
258
259         venus634_send(M_RATE, &rate, sizeof(m_rate_t));
260
261         return;
262 }
263
264 /* Wide Area Augmentation System: use ground stations to increase the precision
265  * of gps position */
266 void venus634_waas(void)
267 {
268         m_waas_t waas;
269
270         memset(&waas, 0, sizeof(m_waas_t));
271         waas.enable = 1;
272         waas.attributes = 0; /* ram, init may freeze when update in flash */
273
274         venus634_send(M_WAAS, &waas, sizeof(m_waas_t));
275
276         return;
277 }
278
279 /* Configure position pinning â€“ Enable or disable position pinning of GNSS
280  * receiver */
281 void venus634_pinning(void)
282 {
283         m_pinning_t pinning;
284
285         memset(&pinning, 0, sizeof(m_pinning_t));
286         pinning.enable = 2;
287         pinning.attributes = 0; /* ram */
288
289         venus634_send(M_PINNING, &pinning, sizeof(m_pinning_t));
290 }
291
292 /* Configure position pinning parameters */
293 void venus634_pinning_params(void)
294 {
295         m_pinning_params_t pinning_params;
296
297         /* just set everything to 0 */
298         memset(&pinning_params, 0, sizeof(m_pinning_params_t));
299         pinning_params.attributes = 0; /* ram */
300
301         venus634_send(M_PINNING_PARAMS, &pinning_params,
302                 sizeof(m_pinning_params_t));
303 }
304
305
306 /* Tell we are a car instead of a pedestrian */
307 void venus634_navmode(void)
308 {
309         m_navmode_t navmode;
310
311         memset(&navmode, 0, sizeof(m_navmode_t));
312         navmode.navmode = 0; /* car */
313         navmode.attributes = 0; /* ram, init may freeze when update in flash */
314
315         venus634_send(M_NAVMODE, &navmode, sizeof(m_navmode_t));
316
317         return;
318 }
319
320 /* frequency of NMEA messages */
321 void venus634_nmea_interval(void)
322 {
323         m_nmeainterval_t nmeainterval;
324
325         memset(&nmeainterval, 0, sizeof(m_nmeainterval_t));
326
327         /* set frequency for nmea: 1 = once every one position fix, 2 = once
328          * every two position fix, ... */
329         nmeainterval.gga = 1; /* GPGGA interval - GPS Fix Data*/
330         nmeainterval.gsa = 1; /* GNSS DOPS and Active Satellites */
331         nmeainterval.gsv = 1; /* GNSS Satellites in View */
332         nmeainterval.gll = 1; /* Geographic Position - Latitude longitude */
333         nmeainterval.rmc = 1; /* Recomended Minimum Specific GNSS Sentence */
334         nmeainterval.vtg = 1; /* Course Over Ground and Ground Speed */
335         nmeainterval.zda = 1; /* Time & Date*/
336
337         nmeainterval.attributes = 1; /* ram & flash */
338
339         venus634_send(M_NMEAINTERVAL, &nmeainterval, sizeof(m_nmeainterval_t));
340
341         return;
342 }
343
344 int8_t recv_cb(uint8_t byte)
345 {
346         uint16_t i;
347
348         /* bytes 0 and 1 are start bytes */
349         if (rxframe.cur_len == 0) {
350                 if (byte != START1) {
351                         debug_printf("bad start1 %.2X\n", byte);
352                         goto reset_buf;
353                 }
354         }
355         else if (rxframe.cur_len == 1) {
356                 if (byte != START2) {
357                         debug_printf("bad start2 %.2X\n", byte);
358                         goto reset_buf;
359                 }
360         }
361         /* bytes 2 and 3 are the length of frame in network order */
362         else if (rxframe.cur_len == 2) {
363                 rxframe.len = (uint16_t)byte << 8;
364         }
365         else if (rxframe.cur_len == 3) {
366                 rxframe.len |= (uint16_t)byte;
367                 if (rxframe.len > MAX_LEN) {
368                         debug_printf("bad len %d\n", rxframe.len);
369                         goto reset_buf;
370                 }
371         }
372         /* next bytes are data (the 4 below is the size of header) */
373         else if ((rxframe.cur_len - 4) < rxframe.len) {
374                 rxframe.data[rxframe.cur_len - 4] = byte;
375         }
376         /* then it's the crc */
377         else if ((rxframe.cur_len - 4) == rxframe.len) {
378                 uint8_t crc = 0;
379
380                 for (i = 0; i < rxframe.len; i++)
381                         crc ^= rxframe.data[i];
382                 if (byte != crc) {
383                         debug_printf("invalid crc\n");
384                         goto reset_buf;
385                 }
386
387         }
388         /* and the last 2 bytes are stop bytes */
389         else if ((rxframe.cur_len - 4) == (rxframe.len + 1)) {
390                 if (byte != END1) {
391                         debug_printf("bad end1 %.2X\n", byte);
392                         goto reset_buf;
393                 }
394         }
395         else if ((rxframe.cur_len - 4) == (rxframe.len + 2)) {
396                 if (byte != END2) {
397                         debug_printf("bad end2 %.2X\n", byte);
398                         goto reset_buf;
399                 }
400                 debug_printf("valid frame received\n");
401                 rxframe.cur_len = 0;
402                 return 0;
403         }
404         else /* should not happen */
405                 goto reset_buf;
406
407         rxframe.cur_len ++;
408         return 1;
409
410  reset_buf:
411         rxframe.cur_len = 0;
412         return 1;
413 }
414
415 int recv_msg(void)
416 {
417         int ret;
418         uint8_t irq_flags;
419         int16_t c;
420         uint16_t t1, t2, diff;
421
422         IRQ_LOCK(irq_flags);
423         t1 = global_ms;
424         IRQ_UNLOCK(irq_flags);
425
426         printf_P(PSTR("recv_msg()\n"));
427         while (1) {
428                 IRQ_LOCK(irq_flags);
429                 t2 = global_ms;
430                 IRQ_UNLOCK(irq_flags);
431                 diff = t2 - t1;
432                 if (diff > 1000) {
433                         printf_P(PSTR("recv_msg timeout\n"));
434                         return -1;
435                 }
436
437                 c = uart_recv_nowait(GPS_UART);
438                 if (c < 0)
439                         continue;
440
441                 debug_printf("%2.2x\n", c);
442
443                 ret = recv_cb(c);
444                 if (ret == 0)
445                         return 0;
446         }
447 }
448
449 int wait_ack(int msg_type)
450 {
451         uint8_t irq_flags;
452         int ret;
453         uint16_t t1, t2, diff;
454
455         IRQ_LOCK(irq_flags);
456         t1 = global_ms;
457         IRQ_UNLOCK(irq_flags);
458
459
460         while (1) {
461                 ret = recv_msg();
462                 if (ret < 0)
463                         return -1;
464
465                 IRQ_LOCK(irq_flags);
466                 t2 = global_ms;
467                 IRQ_UNLOCK(irq_flags);
468                 diff = t2 - t1;
469                 if (diff > 1000) {
470                         printf_P(PSTR("wait_ack timeout\n"));
471                         return -1;
472                 }
473
474                 /* retry if it's not the expected message */
475                 if (rxframe.data[0] != 0x83 && rxframe.data[0] != 0x84)
476                         continue;
477                 if (rxframe.data[1] != msg_type)
478                         continue;
479
480                 if (rxframe.data[0] == 0x83)
481                         printf_P(PSTR("ACK\n"));
482                 else if (rxframe.data[0] == 0x84)
483                         printf_P(PSTR("NACK\n"));
484                 else
485                         printf_P(PSTR("ZARB\n"));
486                 break;
487         }
488
489         return 0;
490 }
491
492 static int decode_gps_pos(uint8_t *buf, uint16_t len)
493 {
494         struct gps_pos *pos = (struct gps_pos *)buf;
495         uint8_t irq_flags;
496
497         if (len != sizeof(*pos))
498                 return -1;
499
500         if (pos->msg_id != 0xA8)
501                 return -1;
502
503         /* fix endianness inside the frame */
504         pos->gps_week = ntohs(pos->gps_week);
505         pos->tow = ntohl(pos->tow);
506
507         pos->latitude = ntohl(pos->latitude);
508         pos->longitude = ntohl(pos->longitude);
509         pos->altitude = ntohl(pos->altitude);
510
511         pos->sea_altitude = ntohl(pos->sea_altitude);
512
513         pos->gdop = ntohs(pos->gdop);
514         pos->pdop = ntohs(pos->pdop);
515         pos->hdop = ntohs(pos->hdop);
516         pos->vdop = ntohs(pos->vdop);
517         pos->tdop = ntohs(pos->tdop);
518
519         pos->ecef_vx = ntohl(pos->ecef_vx);
520         pos->ecef_vy = ntohl(pos->ecef_vy);
521         pos->ecef_vz = ntohl(pos->ecef_vz);
522
523         /* update global structure */
524         IRQ_LOCK(irq_flags);
525         memcpy(&gps_pos, pos, sizeof(gps_pos));
526         IRQ_UNLOCK(irq_flags);
527
528         return 0;
529 }
530
531 /* called on timer event */
532 static void gps_venus_cb(struct callout_mgr *cm, struct callout *tim, void *arg)
533 {
534         static int16_t last_valid = 0;
535         static uint8_t is_valid = 0;
536         int16_t ms, diff;
537         int16_t c;
538         int8_t ret;
539         uint8_t irq_flags;
540
541         (void)cm;
542         (void)tim;
543         (void)arg;
544
545         IRQ_LOCK(irq_flags);
546         ms = global_ms;
547         IRQ_UNLOCK(irq_flags);
548
549         while (1) {
550                 c = uart_recv_nowait(GPS_UART);
551                 if (c < 0) /* no more char */
552                         break;
553
554                 ret = recv_cb(c);
555                 if (ret == 0) {
556                         decode_gps_pos(rxframe.data, rxframe.len);
557                         if (0)
558                                 display_gps_pos();
559
560                         last_valid = ms;
561                         is_valid = 1;
562                 }
563         }
564
565         diff = ms - last_valid;
566         if (is_valid == 1 && diff > 500) {
567
568                 is_valid = 0;
569                 /* update global structure */
570                 IRQ_LOCK(irq_flags);
571                 memset(&gps_pos, 0, sizeof(gps_pos));
572                 gps_pos.latitude = 0xffffffff;
573                 IRQ_UNLOCK(irq_flags);
574         }
575
576         callout_schedule(cm, tim, GPS_PERIOD_MS);
577 }
578
579 static void venus634_configure(void)
580 {
581         /* ask the GPS to reset */
582         printf_P(PSTR("init..."));
583         venus634_restart();
584         wait_ack(M_RESTART);
585
586         printf_P(PSTR("binmode..."));
587         venus634_msg_type(2); /* binary */
588         wait_ack(M_OUTPUT);
589
590         printf_P(PSTR("waas..."));
591         venus634_waas();
592         wait_ack(M_WAAS);
593
594         printf_P(PSTR("pinning..."));
595         venus634_pinning();
596         wait_ack(M_PINNING);
597
598         printf_P(PSTR("pinning parameters..."));
599         venus634_pinning_params();
600         wait_ack(M_PINNING_PARAMS);
601
602         printf_P(PSTR("rate..."));
603         venus634_rate();
604         wait_ack(M_RATE);
605
606         printf_P(PSTR("GPS configuration done !\n"));
607 }
608
609 /*
610   https://www.sparkfun.com/datasheets/GPS/Modules/AN0003_v1.4.14_FlashOnly.pdf
611 */
612
613 int gps_venus_init(void)
614 {
615         venus634_configure();
616
617         callout_init(&gps_timer, gps_venus_cb, NULL, GPS_PRIO);
618         callout_schedule(&imuboard.intr_cm, &gps_timer, GPS_PERIOD_MS); /* every 2ms */
619
620         return 0;
621 }