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 /* 0x3C - nav mode */
156 #define M_NAVMODE 0x3c
158 uint8_t navmode; /* 00 = car
168 void serial1_tx_cout(uint8_t c)
170 debug_printf("%.2X ", c);
171 uart_send(GPS_UART, c);
174 void venus634_send(uint8_t type, void *payload, uint16_t len)
178 debug_printf("SEND ");
180 /* now send the message */
182 serial1_tx_cout(START1);
183 serial1_tx_cout(START2);
184 serial1_tx_cout(((len+1) >> 8) & 0xff);
185 serial1_tx_cout((len+1) & 0xff);
186 /* type and payload */
187 serial1_tx_cout(type);
189 for (n = 0; n < len; n++) {
190 serial1_tx_cout(*(uint8_t *)payload);
191 crc ^= *(uint8_t *)payload;
194 /* checksum and tail */
195 serial1_tx_cout(crc);
196 serial1_tx_cout(END1);
197 serial1_tx_cout(END2);
202 void venus634_restart(void)
206 memset(&restart,0,sizeof(m_restart_t));
207 restart.start_mode = 0x03; /* COLD */
209 venus634_send(M_RESTART,&restart,sizeof(m_restart_t));
214 void venus634_config_serial(void)
218 memset(&serconf,0,sizeof(m_serconf_t));
223 venus634_send(M_SERCONF,&serconf,sizeof(m_serconf_t));
229 void venus634_msg_type(void)
233 memset(&output,0,sizeof(m_output_t));
234 output.msg_type = 0x02; /* binary msg */
235 output.attributes = 0; /* ram, init may freeze when update in flash */
237 venus634_send(M_OUTPUT,&output,sizeof(m_output_t));
243 void venus634_rate(void)
247 memset(&rate,0,sizeof(m_rate_t));
249 rate.attributes = 0; /* ram */
251 venus634_send(M_RATE,&rate,sizeof(m_rate_t));
256 /* Wide Area Augmentation System: use ground stations to increase the precision
258 void venus634_waas(void)
262 memset(&waas,0,sizeof(m_waas_t));
264 waas.attributes = 0; /* ram */
266 venus634_send(M_WAAS,&waas,sizeof(m_waas_t));
271 /* Tell we are a car instead of a pedestrian */
272 void venus634_navmode(void)
276 memset(&navmode,0,sizeof(m_navmode_t));
277 navmode.navmode = 0; /* car */
278 navmode.attributes = 0; /* ram */
280 venus634_send(M_NAVMODE,&navmode,sizeof(m_navmode_t));
285 /* frequency of NMEA messages */
286 void venus634_nmea_interval(void)
288 m_nmeainterval_t nmeainterval;
290 memset(&nmeainterval,0,sizeof(m_nmeainterval_t));
292 /* set frequency for nmea: 1 = once every one position fix, 2 = once
293 * every two position fix, ... */
294 nmeainterval.gga = 1; /* GPGGA interval - GPS Fix Data*/
295 nmeainterval.gsa = 1; /* GNSS DOPS and Active Satellites */
296 nmeainterval.gsv = 1; /* GNSS Satellites in View */
297 nmeainterval.gll = 1; /* Geographic Position - Latitude longitude */
298 nmeainterval.rmc = 1; /* Recomended Minimum Specific GNSS Sentence */
299 nmeainterval.vtg = 1; /* Course Over Ground and Ground Speed */
300 nmeainterval.zda = 1; /* Time & Date*/
302 nmeainterval.attributes = 1; /* ram flash */
304 venus634_send(M_NMEAINTERVAL,&nmeainterval,sizeof(m_nmeainterval_t));
309 int8_t recv_cb(uint8_t byte)
313 /* bytes 0 and 1 are start bytes */
314 if (rxframe.cur_len == 0) {
315 if (byte != START1) {
316 debug_printf("bad start1 %.2X\n", byte);
320 else if (rxframe.cur_len == 1) {
321 if (byte != START2) {
322 debug_printf("bad start2 %.2X\n", byte);
326 /* bytes 2 and 3 are the length of frame in network order */
327 else if (rxframe.cur_len == 2) {
328 rxframe.len = (uint16_t)byte << 8;
330 else if (rxframe.cur_len == 3) {
331 rxframe.len |= (uint16_t)byte;
332 if (rxframe.len > MAX_LEN) {
333 debug_printf("bad len %d\n", rxframe.len);
337 /* next bytes are data (the 4 below is the size of header) */
338 else if ((rxframe.cur_len - 4) < rxframe.len) {
339 rxframe.data[rxframe.cur_len - 4] = byte;
341 /* then it's the crc */
342 else if ((rxframe.cur_len - 4) == rxframe.len) {
345 for (i = 0; i < rxframe.len; i++)
346 crc ^= rxframe.data[i];
348 debug_printf("invalid crc\n");
353 /* and the last 2 bytes are stop bytes */
354 else if ((rxframe.cur_len - 4) == (rxframe.len + 1)) {
356 debug_printf("bad end1 %.2X\n", byte);
360 else if ((rxframe.cur_len - 4) == (rxframe.len + 2)) {
362 debug_printf("bad end2 %.2X\n", byte);
365 debug_printf("valid frame received\n");
369 else /* should not happen */
386 /* XXX use select for timeout */
387 c = uart_recv(GPS_UART);
394 int wait_ack(int msg_type)
403 /* retry if it's not the expected message */
404 if (rxframe.data[0] != 0x83 && rxframe.data[0] != 0x84)
406 if (rxframe.data[1] != msg_type)
409 if (rxframe.data[0] == 0x83)
410 printf_P(PSTR("ACK\n"));
411 else if (rxframe.data[0] == 0x84)
412 printf_P(PSTR("NACK\n"));
414 printf_P(PSTR("ZARB\n"));
421 static int decode_gps_pos(uint8_t *buf, uint16_t len)
423 struct gps_pos *pos = (struct gps_pos *)buf;
426 if (len != sizeof(*pos))
429 if (pos->msg_id != 0xA8)
432 /* fix endianness inside the frame */
433 pos->gps_week = ntohs(pos->gps_week);
434 pos->tow = ntohl(pos->tow);
436 pos->latitude = ntohl(pos->latitude);
437 pos->longitude = ntohl(pos->longitude);
438 pos->altitude = ntohl(pos->altitude);
440 pos->sea_altitude = ntohl(pos->sea_altitude);
442 pos->gdop = ntohs(pos->gdop);
443 pos->pdop = ntohs(pos->pdop);
444 pos->hdop = ntohs(pos->hdop);
445 pos->vdop = ntohs(pos->vdop);
446 pos->tdop = ntohs(pos->tdop);
448 pos->ecef_vx = ntohl(pos->ecef_vx);
449 pos->ecef_vy = ntohl(pos->ecef_vy);
450 pos->ecef_vz = ntohl(pos->ecef_vz);
452 /* update global structure */
454 memcpy(&gps_pos, pos, sizeof(gps_pos));
455 IRQ_UNLOCK(irq_flags);
460 /* display current GPS position stored in the global variable */
461 static void display_gps(void)
463 /* no need to call ntohs/ntohl because the frame is already fixed by
464 * decode_gps_pos() */
465 printf_P(PSTR("id %.2X mode %.2X svnum %.2X gpsw %.4X "
466 "tow %.10"PRIu32"\t"),
473 printf_P(PSTR("lat %.8"PRIx32" long %.8"PRIx32" alt %.8"PRIx32"\n"),
478 printf_P(PSTR("gdop %3.3f pdop %3.3f hdop %3.3f vdop %3.3f "
480 (double)gps_pos.gdop/100.,
481 (double)gps_pos.pdop/100.,
482 (double)gps_pos.hdop/100.,
483 (double)gps_pos.vdop/100.,
484 (double)gps_pos.tdop/100.);
486 printf_P(PSTR("lat %3.5f long %3.5f alt %3.5f sea_alt %3.5f\n"),
487 (double)gps_pos.latitude/10000000.,
488 (double)gps_pos.longitude/10000000.,
489 (double)gps_pos.altitude/100.,
490 (double)gps_pos.sea_altitude/100.);
492 printf_P(PSTR("vx %3.3f vy %3.3f vz %3.3f\n"),
493 (double)gps_pos.ecef_vx/100.,
494 (double)gps_pos.ecef_vy/100.,
495 (double)gps_pos.ecef_vz/100.);
498 /* called on timer event */
499 static void gps_venus_cb(struct callout_mgr *cm, struct callout *tim, void *arg)
501 static int16_t last_valid = 0;
502 static uint8_t is_valid = 0;
514 IRQ_UNLOCK(irq_flags);
517 c = uart_recv_nowait(GPS_UART);
518 if (c < 0) /* no more char */
523 decode_gps_pos(rxframe.data, rxframe.len);
532 diff = ms - last_valid;
533 if (is_valid == 1 && diff > 100) {
536 /* update global structure */
538 memset(&gps_pos, 0, sizeof(gps_pos));
539 gps_pos.latitude = 0xffffffff;
540 IRQ_UNLOCK(irq_flags);
543 callout_schedule(cm, tim, GPS_PERIOD_MS);
546 static void venus634_configure(void)
548 /* ask the GPS to reset */
549 printf_P(PSTR("init..."));
553 /* it seems we must wait that the GPS is restarted, else it doesn't work
557 printf_P(PSTR("binmode..."));
561 printf_P(PSTR("waas..."));
565 printf_P(PSTR("rate..."));
569 printf_P(PSTR("GPS configuration done !\n"));
573 https://www.sparkfun.com/datasheets/GPS/Modules/AN0003_v1.4.14_FlashOnly.pdf
576 int gps_venus_init(void)
578 venus634_configure();
580 callout_init(&gps_timer, gps_venus_cb, NULL, GPS_PRIO);
581 callout_schedule(&imuboard.intr_cm, &gps_timer, GPS_PERIOD_MS); /* every 2ms */
586 void gps_get_pos(struct gps_pos *pos)
588 memcpy(pos, &gps_pos, sizeof(*pos));
603 /* get position (prevent modification of gps pos during copy) */
604 prio = callout_mgr_set_prio(&imuboard.intr_cm, GPS_PRIO);
606 callout_mgr_restore_prio(&imuboard.intr_cm, prio);
609 len = snprintf(buf, sizeof(buf),
611 "svnum %.2X lat %3.5f long %3.5f "
612 "alt %3.5f sea_alt %3.5f\n",
614 (double)gps_pos.latitude / 10000000.,
615 (double)gps_pos.longitude / 10000000.,
616 (double)gps_pos.altitude / 100.,
617 (double)gps_pos.sea_altitude / 100.);
620 if (sd_log_enabled()) {
621 if (sd_log_write(buf, len) != len) {
622 printf_P(PSTR("error writing to file\n"));
627 printf_P(PSTR("%s"), buf);