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 #define debug_printf(fmt, a...) do { } while (0)
22 /* #define debug_printf(fmt, ...) printf_P(PSTR(fmt), ##__VA_ARGS__) */
27 https://code.google.com/p/hardware-ntp/source/browse/trunk/pcb-1/venus634.c?r=12
30 /* some useful defines */
36 /* proccessing states */
54 uint8_t data[MAX_LEN];
57 static struct gps_frame rxframe;
59 static struct gps_pos gps_pos;
61 static struct callout gps_timer;
66 /* 0x01 - System Restart */
67 #define M_RESTART 0x01
69 uint8_t start_mode; /* 01 = Hot; 02 = Warm; 03 = Cold */
70 uint16_t utc_year; /* >= 1980 */
76 int16_t latitude; /* -9000 to 9000, 1/100th degree, positive north */
77 int16_t longitude; /* -18000 to 18000, 1/100th degree, positive east */
78 int16_t altitude; /* -1000 to 18300, meters */
82 /* 0x02 - Query Software Version */
83 #define M_QUERYVER 0x02
85 uint8_t software_type; /* 01 = System Code */
89 /* 0x05 - Serial Port Configuration */
90 #define M_SERCONF 0x05
92 uint8_t port; /* 00 = COM1 (the only port) */
93 uint8_t baud; /* 00 = 4800; 01 = 9600; 02 = 19200; 03 = 38400;
94 04 = 57600; 05 = 115200; */
95 uint8_t update; /* 00 = SRAM only; 01 = SRAM and FLASH */
100 /* 0x08 - nmea interval */
101 #define M_NMEAINTERVAL 0x08
110 uint8_t attributes; /*
118 /* 0x09 - Query Software Version */
119 #define M_OUTPUT 0x09
126 uint8_t attributes; /*
128 1 - update ram & flash
133 /* 0x0E - Update Rage */
136 uint8_t rate; /* Hz */
137 uint8_t attributes; /*
139 1 - update ram & flash
145 /* 0x37 - configure waas */
148 uint8_t enable; /* 01 = enable */
152 /* 0x3C - nav mode */
153 #define M_NAVMODE 0x3c
155 uint8_t navmode; /* 00 = car
165 void serial1_tx_cout(uint8_t c)
167 debug_printf("%.2X ", c);
168 uart_send(GPS_UART, c);
171 void venus634_send(uint8_t type, void *payload, uint16_t len)
175 debug_printf("SEND ");
177 /* now send the message */
179 serial1_tx_cout(START1);
180 serial1_tx_cout(START2);
181 serial1_tx_cout(((len+1) >> 8) & 0xff);
182 serial1_tx_cout((len+1) & 0xff);
183 /* type and payload */
184 serial1_tx_cout(type);
186 for (n = 0; n < len; n++) {
187 serial1_tx_cout(*(uint8_t *)payload);
188 crc ^= *(uint8_t *)payload;
191 /* checksum and tail */
192 serial1_tx_cout(crc);
193 serial1_tx_cout(END1);
194 serial1_tx_cout(END2);
199 void venus634_restart(void)
203 memset(&restart,0,sizeof(m_restart_t));
204 restart.start_mode = 0x03; /* COLD */
206 venus634_send(M_RESTART,&restart,sizeof(m_restart_t));
211 void venus634_config_serial(void)
215 memset(&serconf,0,sizeof(m_serconf_t));
220 venus634_send(M_SERCONF,&serconf,sizeof(m_serconf_t));
226 void venus634_msg_type(void)
230 memset(&output,0,sizeof(m_output_t));
231 output.msg_type = 0x02; /* binary msg */
232 output.attributes = 0; /* ram, init may freeze when update in flash */
234 venus634_send(M_OUTPUT,&output,sizeof(m_output_t));
240 void venus634_rate(void)
244 memset(&rate,0,sizeof(m_rate_t));
246 rate.attributes = 0; /* ram */
248 venus634_send(M_RATE,&rate,sizeof(m_rate_t));
253 /* Wide Area Augmentation System: use ground stations to increase the precision
255 void venus634_waas(void)
259 memset(&waas,0,sizeof(m_waas_t));
261 waas.attributes = 0; /* ram */
263 venus634_send(M_WAAS,&waas,sizeof(m_waas_t));
268 /* Tell we are a car instead of a pedestrian */
269 void venus634_navmode(void)
273 memset(&navmode,0,sizeof(m_navmode_t));
274 navmode.navmode = 0; /* car */
275 navmode.attributes = 0; /* ram */
277 venus634_send(M_NAVMODE,&navmode,sizeof(m_navmode_t));
282 /* frequency of NMEA messages */
283 void venus634_nmea_interval(void)
285 m_nmeainterval_t nmeainterval;
287 memset(&nmeainterval,0,sizeof(m_nmeainterval_t));
289 /* set frequency for nmea: 1 = once every one position fix, 2 = once
290 * every two position fix, ... */
291 nmeainterval.gga = 1; /* GPGGA interval - GPS Fix Data*/
292 nmeainterval.gsa = 1; /* GNSS DOPS and Active Satellites */
293 nmeainterval.gsv = 1; /* GNSS Satellites in View */
294 nmeainterval.gll = 1; /* Geographic Position - Latitude longitude */
295 nmeainterval.rmc = 1; /* Recomended Minimum Specific GNSS Sentence */
296 nmeainterval.vtg = 1; /* Course Over Ground and Ground Speed */
297 nmeainterval.zda = 1; /* Time & Date*/
299 nmeainterval.attributes = 1; /* ram flash */
301 venus634_send(M_NMEAINTERVAL,&nmeainterval,sizeof(m_nmeainterval_t));
306 int8_t recv_cb(uint8_t byte)
310 /* bytes 0 and 1 are start bytes */
311 if (rxframe.cur_len == 0) {
312 if (byte != START1) {
313 debug_printf("bad start1 %.2X\n", byte);
317 else if (rxframe.cur_len == 1) {
318 if (byte != START2) {
319 debug_printf("bad start2 %.2X\n", byte);
323 /* bytes 2 and 3 are the length of frame in network order */
324 else if (rxframe.cur_len == 2) {
325 rxframe.len = (uint16_t)byte << 8;
327 else if (rxframe.cur_len == 3) {
328 rxframe.len |= (uint16_t)byte;
329 if (rxframe.len > MAX_LEN) {
330 debug_printf("bad len %d\n", rxframe.len);
334 /* next bytes are data (the 4 below is the size of header) */
335 else if ((rxframe.cur_len - 4) < rxframe.len) {
336 rxframe.data[rxframe.cur_len - 4] = byte;
338 /* then it's the crc */
339 else if ((rxframe.cur_len - 4) == rxframe.len) {
342 for (i = 0; i < rxframe.len; i++)
343 crc ^= rxframe.data[i];
345 debug_printf("invalid crc\n");
350 /* and the last 2 bytes are stop bytes */
351 else if ((rxframe.cur_len - 4) == (rxframe.len + 1)) {
353 debug_printf("bad end1 %.2X\n", byte);
357 else if ((rxframe.cur_len - 4) == (rxframe.len + 2)) {
359 debug_printf("bad end2 %.2X\n", byte);
362 debug_printf("valid frame received\n");
366 else /* should not happen */
383 /* XXX use select for timeout */
384 c = uart_recv(GPS_UART);
391 int wait_ack(int msg_type)
400 /* retry if it's not the expected message */
401 if (rxframe.data[0] != 0x83 && rxframe.data[0] != 0x84)
403 if (rxframe.data[1] != msg_type)
406 if (rxframe.data[0] == 0x83)
407 printf_P(PSTR("ACK\n"));
408 else if (rxframe.data[0] == 0x84)
409 printf_P(PSTR("NACK\n"));
411 printf_P(PSTR("ZARB\n"));
418 static int decode_gps_pos(uint8_t *buf, uint16_t len)
420 struct gps_pos *pos = (struct gps_pos *)buf;
423 if (len != sizeof(*pos))
426 if (pos->msg_id != 0xA8) /* XXX not tested */
429 gps_pos.gps_week = ntohs(pos->gps_week);
430 gps_pos.tow = ntohl(pos->tow);
432 gps_pos.latitude = ntohl(pos->latitude);
433 gps_pos.longitude = ntohl(pos->longitude);
434 gps_pos.altitude = ntohl(pos->altitude);
436 gps_pos.sea_altitude = ntohl(pos->sea_altitude);
438 gps_pos.gdop = ntohs(pos->gdop);
439 gps_pos.pdop = ntohs(pos->pdop);
440 gps_pos.hdop = ntohs(pos->hdop);
441 gps_pos.vdop = ntohs(pos->vdop);
442 gps_pos.tdop = ntohs(pos->tdop);
444 gps_pos.ecef_vx = ntohl(pos->ecef_vx);
445 gps_pos.ecef_vy = ntohl(pos->ecef_vy);
446 gps_pos.ecef_vz = ntohl(pos->ecef_vz);
448 /* update global structure */
450 memcpy(&gps_pos, pos, sizeof(gps_pos));
451 IRQ_UNLOCK(irq_flags);
456 /* display current GPS position stored in the global variable */
457 static void display_gps(void)
459 printf_P(PSTR("id %.2X mode %.2X svnum %.2X gpsw %.4X "
460 "tow %.10"PRIu32"\t"),
467 printf_P(PSTR("lat %.8"PRIx32" long %.8"PRIx32" alt %.8"PRIx32"\n"),
472 printf_P(PSTR("gdop %3.3f pdop %3.3f hdop %3.3f vdop %3.3f "
474 (double)gps_pos.gdop/100.,
475 (double)gps_pos.pdop/100.,
476 (double)gps_pos.hdop/100.,
477 (double)gps_pos.vdop/100.,
478 (double)gps_pos.tdop/100.);
480 printf_P(PSTR("lat %3.5f long %3.5f alt %3.5f sea_alt %3.5f\n"),
481 (double)gps_pos.latitude/10000000.,
482 (double)gps_pos.longitude/10000000.,
483 (double)gps_pos.altitude/100.,
484 (double)gps_pos.sea_altitude/100.);
486 printf_P(PSTR("vx %3.3f vy %3.3f vz %3.3f\n"),
487 (double)gps_pos.ecef_vx/100.,
488 (double)gps_pos.ecef_vy/100.,
489 (double)gps_pos.ecef_vz/100.);
492 static void gps_venus_cb(struct callout_mgr *cm, struct callout *tim, void *arg)
502 c = uart_recv_nowait(GPS_UART);
503 if (c < 0) /* no more char */
508 decode_gps_pos(rxframe.data, rxframe.len);
515 callout_schedule(cm, tim, 2);
518 static void venus634_configure(void)
520 /* ask the GPS to reset */
521 printf_P(PSTR("init..."));
525 /* it seems we must wait that the GPS is restarted, else it doesn't work
529 printf_P(PSTR("binmode..."));
533 printf_P(PSTR("waas..."));
537 printf_P(PSTR("rate..."));
541 printf_P(PSTR("GPS configuration done !\n"));
545 https://www.sparkfun.com/datasheets/GPS/Modules/AN0003_v1.4.14_FlashOnly.pdf
548 int gps_venus_init(void)
550 venus634_configure();
552 callout_init(&gps_timer, gps_venus_cb, NULL, GPS_PRIO);
553 callout_schedule(&imuboard.intr_cm, &gps_timer, 2); /* every 2ms */
558 void gps_get_pos(struct gps_pos *pos)
560 memcpy(pos, &gps_pos, sizeof(*pos));
577 /* get position (prevent modification of gps pos during copy) */
578 prio = callout_mgr_set_prio(&imuboard.intr_cm, GPS_PRIO);
580 callout_mgr_restore_prio(&imuboard.intr_cm, prio);
583 len = snprintf(buf, sizeof(buf),
585 "svnum %.2X lat %3.5f long %3.5f "
586 "alt %3.5f sea_alt %3.5f\n",
588 (double)gps_pos.latitude/10000000.,
589 (double)gps_pos.longitude/10000000.,
590 (double)gps_pos.altitude/100.,
591 (double)gps_pos.sea_altitude/100.);
594 if (sd_log_enabled()) {
596 if (sd_log_write(buf, len) != len) {
597 printf_P(PSTR("error writing to file\n"));
602 printf_P(PSTR("%s"), buf);