7 #include <aversive/endian.h>
8 #include <aversive/wait.h>
13 #include "gps_venus.h"
15 #include "fat_config.h"
16 #include "partition.h"
18 #include "sd_raw_config.h"
21 /* note: enabling debug will make the code fail due to rx queue full */
22 #define debug_printf(fmt, a...) do { } while (0)
23 /* #define debug_printf(fmt, ...) printf_P(PSTR(fmt), ##__VA_ARGS__) */
27 #define GPS_PERIOD_MS 2
30 https://code.google.com/p/hardware-ntp/source/browse/trunk/pcb-1/venus634.c?r=12
33 /* some useful defines */
39 /* proccessing states */
57 uint8_t data[MAX_LEN];
60 static struct gps_frame rxframe;
62 static struct gps_pos gps_pos;
64 static struct callout gps_timer;
69 /* 0x01 - System Restart */
70 #define M_RESTART 0x01
72 uint8_t start_mode; /* 01 = Hot; 02 = Warm; 03 = Cold */
73 uint16_t utc_year; /* >= 1980 */
79 int16_t latitude; /* -9000 to 9000, 1/100th degree, positive north */
80 int16_t longitude; /* -18000 to 18000, 1/100th degree, positive east */
81 int16_t altitude; /* -1000 to 18300, meters */
85 /* 0x02 - Query Software Version */
86 #define M_QUERYVER 0x02
88 uint8_t software_type; /* 01 = System Code */
92 /* 0x05 - Serial Port Configuration */
93 #define M_SERCONF 0x05
95 uint8_t port; /* 00 = COM1 (the only port) */
96 uint8_t baud; /* 00 = 4800; 01 = 9600; 02 = 19200; 03 = 38400;
97 04 = 57600; 05 = 115200; */
98 uint8_t update; /* 00 = SRAM only; 01 = SRAM and FLASH */
103 /* 0x08 - nmea interval */
104 #define M_NMEAINTERVAL 0x08
113 uint8_t attributes; /*
121 /* 0x09 - Query Software Version */
122 #define M_OUTPUT 0x09
129 uint8_t attributes; /*
131 1 - update ram & flash
136 /* 0x0E - Update Rage */
139 uint8_t rate; /* Hz */
140 uint8_t attributes; /*
142 1 - update ram & flash
148 /* 0x37 - configure waas */
151 uint8_t enable; /* 01 = enable */
155 /* 0x39 - configure position pinning */
156 #define M_PINNING 0x39
158 uint8_t enable; /* 0 = default, 1 = enable, 2 = disable */
159 uint8_t attributes; /* 0 = ram, 1 = ram+flash */
162 /* 0x3B - configure position pinning */
163 #define M_PINNING_PARAMS 0x3B
167 uint16_t unpin_speed;
170 uint8_t attributes; /* 0 = ram, 1 = ram+flash */
171 } m_pinning_params_t;
173 /* 0x3C - nav mode */
174 #define M_NAVMODE 0x3c
176 uint8_t navmode; /* 00 = car
183 void serial1_tx_cout(uint8_t c)
185 debug_printf("%.2X ", c);
186 uart_send(GPS_UART, c);
189 void venus634_send(uint8_t type, void *payload, uint16_t len)
193 debug_printf("SEND ");
195 /* now send the message */
197 serial1_tx_cout(START1);
198 serial1_tx_cout(START2);
199 serial1_tx_cout(((len+1) >> 8) & 0xff);
200 serial1_tx_cout((len+1) & 0xff);
201 /* type and payload */
202 serial1_tx_cout(type);
204 for (n = 0; n < len; n++) {
205 serial1_tx_cout(*(uint8_t *)payload);
206 crc ^= *(uint8_t *)payload;
209 /* checksum and tail */
210 serial1_tx_cout(crc);
211 serial1_tx_cout(END1);
212 serial1_tx_cout(END2);
217 void venus634_restart(void)
221 memset(&restart, 0, sizeof(m_restart_t));
222 restart.start_mode = 0x03; /* COLD */
224 venus634_send(M_RESTART, &restart, sizeof(m_restart_t));
229 void venus634_config_serial(void)
233 memset(&serconf, 0, sizeof(m_serconf_t));
238 venus634_send(M_SERCONF, &serconf, sizeof(m_serconf_t));
244 void venus634_msg_type(uint8_t mode)
248 memset(&output, 0, sizeof(m_output_t));
249 output.msg_type = mode;
250 output.attributes = 0; /* ram, init may freeze when update in flash */
252 venus634_send(M_OUTPUT, &output, sizeof(m_output_t));
258 void venus634_rate(void)
262 memset(&rate, 0, sizeof(m_rate_t));
264 rate.attributes = 0; /* ram, init may freeze when update in flash */
266 venus634_send(M_RATE, &rate, sizeof(m_rate_t));
271 /* Wide Area Augmentation System: use ground stations to increase the precision
273 void venus634_waas(void)
277 memset(&waas, 0, sizeof(m_waas_t));
279 waas.attributes = 0; /* ram, init may freeze when update in flash */
281 venus634_send(M_WAAS, &waas, sizeof(m_waas_t));
286 /* Configure position pinning – Enable or disable position pinning of GNSS
288 void venus634_pinning(void)
292 memset(&pinning, 0, sizeof(m_pinning_t));
294 pinning.attributes = 0; /* ram */
296 venus634_send(M_PINNING, &pinning, sizeof(m_pinning_t));
299 /* Configure position pinning parameters */
300 void venus634_pinning_params(void)
302 m_pinning_params_t pinning_params;
304 /* just set everything to 0 */
305 memset(&pinning_params, 0, sizeof(m_pinning_params_t));
306 pinning_params.attributes = 0; /* ram */
308 venus634_send(M_PINNING_PARAMS, &pinning_params,
309 sizeof(m_pinning_params_t));
313 /* Tell we are a car instead of a pedestrian */
314 void venus634_navmode(void)
318 memset(&navmode, 0, sizeof(m_navmode_t));
319 navmode.navmode = 0; /* car */
320 navmode.attributes = 0; /* ram, init may freeze when update in flash */
322 venus634_send(M_NAVMODE, &navmode, sizeof(m_navmode_t));
327 /* frequency of NMEA messages */
328 void venus634_nmea_interval(void)
330 m_nmeainterval_t nmeainterval;
332 memset(&nmeainterval, 0, sizeof(m_nmeainterval_t));
334 /* set frequency for nmea: 1 = once every one position fix, 2 = once
335 * every two position fix, ... */
336 nmeainterval.gga = 1; /* GPGGA interval - GPS Fix Data*/
337 nmeainterval.gsa = 1; /* GNSS DOPS and Active Satellites */
338 nmeainterval.gsv = 1; /* GNSS Satellites in View */
339 nmeainterval.gll = 1; /* Geographic Position - Latitude longitude */
340 nmeainterval.rmc = 1; /* Recomended Minimum Specific GNSS Sentence */
341 nmeainterval.vtg = 1; /* Course Over Ground and Ground Speed */
342 nmeainterval.zda = 1; /* Time & Date*/
344 nmeainterval.attributes = 1; /* ram & flash */
346 venus634_send(M_NMEAINTERVAL, &nmeainterval, sizeof(m_nmeainterval_t));
351 int8_t recv_cb(uint8_t byte)
355 /* bytes 0 and 1 are start bytes */
356 if (rxframe.cur_len == 0) {
357 if (byte != START1) {
358 debug_printf("bad start1 %.2X\n", byte);
362 else if (rxframe.cur_len == 1) {
363 if (byte != START2) {
364 debug_printf("bad start2 %.2X\n", byte);
368 /* bytes 2 and 3 are the length of frame in network order */
369 else if (rxframe.cur_len == 2) {
370 rxframe.len = (uint16_t)byte << 8;
372 else if (rxframe.cur_len == 3) {
373 rxframe.len |= (uint16_t)byte;
374 if (rxframe.len > MAX_LEN) {
375 debug_printf("bad len %d\n", rxframe.len);
379 /* next bytes are data (the 4 below is the size of header) */
380 else if ((rxframe.cur_len - 4) < rxframe.len) {
381 rxframe.data[rxframe.cur_len - 4] = byte;
383 /* then it's the crc */
384 else if ((rxframe.cur_len - 4) == rxframe.len) {
387 for (i = 0; i < rxframe.len; i++)
388 crc ^= rxframe.data[i];
390 debug_printf("invalid crc\n");
395 /* and the last 2 bytes are stop bytes */
396 else if ((rxframe.cur_len - 4) == (rxframe.len + 1)) {
398 debug_printf("bad end1 %.2X\n", byte);
402 else if ((rxframe.cur_len - 4) == (rxframe.len + 2)) {
404 debug_printf("bad end2 %.2X\n", byte);
407 debug_printf("valid frame received\n");
411 else /* should not happen */
427 uint16_t t1, t2, diff;
431 IRQ_UNLOCK(irq_flags);
433 printf_P(PSTR("recv_msg()\n"));
437 IRQ_UNLOCK(irq_flags);
440 printf_P(PSTR("recv_msg timeout\n"));
444 c = uart_recv_nowait(GPS_UART);
448 debug_printf("%2.2x\n", c);
456 int wait_ack(int msg_type)
460 uint16_t t1, t2, diff;
464 IRQ_UNLOCK(irq_flags);
474 IRQ_UNLOCK(irq_flags);
477 printf_P(PSTR("wait_ack timeout\n"));
481 /* retry if it's not the expected message */
482 if (rxframe.data[0] != 0x83 && rxframe.data[0] != 0x84)
484 if (rxframe.data[1] != msg_type)
487 if (rxframe.data[0] == 0x83)
488 printf_P(PSTR("ACK\n"));
489 else if (rxframe.data[0] == 0x84)
490 printf_P(PSTR("NACK\n"));
492 printf_P(PSTR("ZARB\n"));
499 static int decode_gps_pos(uint8_t *buf, uint16_t len)
501 struct gps_pos *pos = (struct gps_pos *)buf;
504 if (len != sizeof(*pos))
507 if (pos->msg_id != 0xA8)
510 /* fix endianness inside the frame */
511 pos->gps_week = ntohs(pos->gps_week);
512 pos->tow = ntohl(pos->tow);
514 pos->latitude = ntohl(pos->latitude);
515 pos->longitude = ntohl(pos->longitude);
516 pos->altitude = ntohl(pos->altitude);
518 pos->sea_altitude = ntohl(pos->sea_altitude);
520 pos->gdop = ntohs(pos->gdop);
521 pos->pdop = ntohs(pos->pdop);
522 pos->hdop = ntohs(pos->hdop);
523 pos->vdop = ntohs(pos->vdop);
524 pos->tdop = ntohs(pos->tdop);
526 pos->ecef_vx = ntohl(pos->ecef_vx);
527 pos->ecef_vy = ntohl(pos->ecef_vy);
528 pos->ecef_vz = ntohl(pos->ecef_vz);
530 /* update global structure */
532 memcpy(&gps_pos, pos, sizeof(gps_pos));
533 IRQ_UNLOCK(irq_flags);
538 /* display current GPS position stored in the global variable */
539 static void display_gps(void)
541 /* no need to call ntohs/ntohl because the frame is already fixed by
542 * decode_gps_pos() */
543 printf_P(PSTR("id %.2X mode %.2X svnum %.2X gpsw %.4X "
544 "tow %.10"PRIu32"\t"),
551 printf_P(PSTR("lat %.8"PRIx32" long %.8"PRIx32" alt %.8"PRIx32"\n"),
556 printf_P(PSTR("gdop %3.3f pdop %3.3f hdop %3.3f vdop %3.3f "
558 (double)gps_pos.gdop/100.,
559 (double)gps_pos.pdop/100.,
560 (double)gps_pos.hdop/100.,
561 (double)gps_pos.vdop/100.,
562 (double)gps_pos.tdop/100.);
564 printf_P(PSTR("lat %3.5f long %3.5f alt %3.5f sea_alt %3.5f\n"),
565 (double)gps_pos.latitude/10000000.,
566 (double)gps_pos.longitude/10000000.,
567 (double)gps_pos.altitude/100.,
568 (double)gps_pos.sea_altitude/100.);
570 printf_P(PSTR("vx %3.3f vy %3.3f vz %3.3f\n"),
571 (double)gps_pos.ecef_vx/100.,
572 (double)gps_pos.ecef_vy/100.,
573 (double)gps_pos.ecef_vz/100.);
576 /* called on timer event */
577 static void gps_venus_cb(struct callout_mgr *cm, struct callout *tim, void *arg)
579 static int16_t last_valid = 0;
580 static uint8_t is_valid = 0;
592 IRQ_UNLOCK(irq_flags);
595 c = uart_recv_nowait(GPS_UART);
596 if (c < 0) /* no more char */
601 decode_gps_pos(rxframe.data, rxframe.len);
610 diff = ms - last_valid;
611 if (is_valid == 1 && diff > 500) {
614 /* update global structure */
616 memset(&gps_pos, 0, sizeof(gps_pos));
617 gps_pos.latitude = 0xffffffff;
618 IRQ_UNLOCK(irq_flags);
621 callout_schedule(cm, tim, GPS_PERIOD_MS);
624 static void venus634_configure(void)
626 /* ask the GPS to reset */
627 printf_P(PSTR("init..."));
631 printf_P(PSTR("binmode..."));
632 venus634_msg_type(2); /* binary */
635 printf_P(PSTR("waas..."));
639 printf_P(PSTR("pinning..."));
643 printf_P(PSTR("pinning parameters..."));
644 venus634_pinning_params();
645 wait_ack(M_PINNING_PARAMS);
647 printf_P(PSTR("rate..."));
651 printf_P(PSTR("GPS configuration done !\n"));
655 https://www.sparkfun.com/datasheets/GPS/Modules/AN0003_v1.4.14_FlashOnly.pdf
658 int gps_venus_init(void)
660 venus634_configure();
662 callout_init(&gps_timer, gps_venus_cb, NULL, GPS_PRIO);
663 callout_schedule(&imuboard.intr_cm, &gps_timer, GPS_PERIOD_MS); /* every 2ms */
668 void gps_get_pos(struct gps_pos *pos)
670 memcpy(pos, &gps_pos, sizeof(*pos));
673 int gps_log(uint8_t to_stdout)
685 /* get position (prevent modification of gps pos during copy) */
686 prio = callout_mgr_set_prio(&imuboard.intr_cm, GPS_PRIO);
688 callout_mgr_restore_prio(&imuboard.intr_cm, prio);
691 len = snprintf(buf, sizeof(buf),
693 "svnum %.2X lat %3.5f long %3.5f "
694 "alt %3.5f sea_alt %3.5f\n",
696 (double)gps_pos.latitude / 10000000.,
697 (double)gps_pos.longitude / 10000000.,
698 (double)gps_pos.altitude / 100.,
699 (double)gps_pos.sea_altitude / 100.);
702 if (!to_stdout && sd_log_enabled()) {
703 if (sd_log_write(buf, len) != len) {
704 printf_P(PSTR("error writing to file\n"));
708 else if (to_stdout) {
709 printf_P(PSTR("%s"), buf);