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(args...) do { } while (0)
22 /* #define debug_printf(args...) printf(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 - nema interval */
101 #define M_NEMAINTERVAL 0x08
110 uint8_t attributes; /*
118 /* 0x09 - Query Software Version */
119 #define M_OUTPUT 0x09
129 /* 0x0E - Update Rage */
132 uint8_t rate; /* Hz */
133 uint8_t attributes; /*
135 1 - update ram & flash
141 /* 0x37 - configure waas */
144 uint8_t enable; /* 01 = enable */
148 /* 0x3C - nav mode */
149 #define M_NAVMODE 0x3c
151 uint8_t navmode; /* 00 = car
161 void serial1_tx_cout(uint8_t c)
163 debug_printf("%.2X ", c);
164 uart_send(GPS_UART, c);
167 void venus634_send(uint8_t type, void *payload, uint16_t len)
171 debug_printf("SEND ");
173 /* now send the message */
175 serial1_tx_cout(START1);
176 serial1_tx_cout(START2);
177 serial1_tx_cout(((len+1) >> 8) & 0xff);
178 serial1_tx_cout((len+1) & 0xff);
179 /* type and payload */
180 serial1_tx_cout(type);
182 for (n = 0; n < len; n++) {
183 serial1_tx_cout(*(uint8_t *)payload);
184 crc ^= *(uint8_t *)payload;
187 /* checksum and tail */
188 serial1_tx_cout(crc);
189 serial1_tx_cout(END1);
190 serial1_tx_cout(END2);
195 void venus634_restart(void)
199 memset(&restart,0,sizeof(m_restart_t));
200 restart.start_mode = 0x03; /* COLD */
202 venus634_send(M_RESTART,&restart,sizeof(m_restart_t));
207 void venus634_config_serial(void)
211 memset(&serconf,0,sizeof(m_serconf_t));
216 venus634_send(M_SERCONF,&serconf,sizeof(m_serconf_t));
222 void venus634_msg_type(void)
226 memset(&output,0,sizeof(m_output_t));
227 output.msg_type = 0x02; /* binary msg */
229 venus634_send(M_OUTPUT,&output,sizeof(m_output_t));
235 void venus634_rate(void)
239 memset(&rate,0,sizeof(m_rate_t));
241 //rate.rate = 0; /* ram */
243 venus634_send(M_RATE,&rate,sizeof(m_rate_t));
248 void venus634_waas(void)
252 memset(&waas,0,sizeof(m_waas_t));
254 waas.attributes = 0; /* ram */
256 venus634_send(M_WAAS,&waas,sizeof(m_waas_t));
262 void venus634_navmode(void)
266 memset(&navmode,0,sizeof(m_navmode_t));
267 navmode.navmode = 0; /* car */
268 navmode.attributes = 0; /* ram */
270 venus634_send(M_NAVMODE,&navmode,sizeof(m_navmode_t));
276 void venus634_nema_interval(void)
278 m_nemainterval_t nemainterval;
280 memset(&nemainterval,0,sizeof(m_nemainterval_t));
281 nemainterval.gga = 1;
282 nemainterval.gsa = 1;
283 nemainterval.gsv = 1;
284 nemainterval.gll = 1;
285 nemainterval.rmc = 1;
286 nemainterval.vtg = 1;
287 nemainterval.zda = 1;
289 nemainterval.attributes = 1; /* ram flash */
291 venus634_send(M_NEMAINTERVAL,&nemainterval,sizeof(m_nemainterval_t));
296 int8_t recv_cb(uint8_t byte)
300 /* bytes 0 and 1 are start bytes */
301 if (rxframe.cur_len == 0) {
302 if (byte != START1) {
303 debug_printf("bad start1 %.2X\n", byte);
307 else if (rxframe.cur_len == 1) {
308 if (byte != START2) {
309 debug_printf("bad start2 %.2X\n", byte);
313 /* bytes 2 and 3 are the length of frame in network order */
314 else if (rxframe.cur_len == 2) {
315 rxframe.len = (uint16_t)byte << 8;
317 else if (rxframe.cur_len == 3) {
318 rxframe.len |= (uint16_t)byte;
319 if (rxframe.len > MAX_LEN) {
320 debug_printf("bad len %d\n", rxframe.len);
324 /* next bytes are data (the 4 below is the size of header) */
325 else if ((rxframe.cur_len - 4) < rxframe.len) {
326 rxframe.data[rxframe.cur_len - 4] = byte;
328 /* then it's the crc */
329 else if ((rxframe.cur_len - 4) == rxframe.len) {
332 for (i = 0; i < rxframe.len; i++)
333 crc ^= rxframe.data[i];
335 debug_printf("invalid crc\n");
340 /* and the last 2 bytes are stop bytes */
341 else if ((rxframe.cur_len - 4) == (rxframe.len + 1)) {
343 debug_printf("bad end1 %.2X\n", byte);
347 else if ((rxframe.cur_len - 4) == (rxframe.len + 2)) {
349 debug_printf("bad end2 %.2X\n", byte);
352 debug_printf("valid frame received\n");
356 else /* should not happen */
373 /* XXX use select for timeout */
374 c = uart_recv(GPS_UART);
381 int wait_ack(int msg_type)
390 /* retry if it's not the expected message */
391 if (rxframe.data[0] != 0x83 && rxframe.data[0] != 0x84)
393 if (rxframe.data[1] != msg_type)
396 if (rxframe.data[0] == 0x83)
398 else if (rxframe.data[0] == 0x84)
408 static int decode_gps_pos(uint8_t *buf, uint16_t len)
410 struct gps_pos *pos = (struct gps_pos *)buf;
412 if (len != sizeof(*pos))
415 gps_pos.gps_week = ntohs(pos->gps_week);
416 gps_pos.tow = ntohl(pos->tow);
418 gps_pos.latitude = ntohl(pos->latitude);
419 gps_pos.longitude = ntohl(pos->longitude);
420 gps_pos.altitude = ntohl(pos->altitude);
422 gps_pos.sea_altitude = ntohl(pos->sea_altitude);
424 gps_pos.gdop = ntohs(pos->gdop);
425 gps_pos.pdop = ntohs(pos->pdop);
426 gps_pos.hdop = ntohs(pos->hdop);
427 gps_pos.vdop = ntohs(pos->vdop);
428 gps_pos.tdop = ntohs(pos->tdop);
430 gps_pos.ecef_vx = ntohl(pos->ecef_vx);
431 gps_pos.ecef_vy = ntohl(pos->ecef_vy);
432 gps_pos.ecef_vz = ntohl(pos->ecef_vz);
437 /* display current GPS position stored in the global variable */
438 static void display_gps(void)
440 printf("id %.2X mode %.2X svnum %.2X gpsw %.4X tow %.10"PRIu32"\t",
447 printf("lat %.8"PRIx32" long %.8"PRIx32" alt %.8"PRIx32"\n",
452 printf("gdop %3.3f pdop %3.3f hdop %3.3f vdop %3.3f tdop %3.3f\n",
453 (double)gps_pos.gdop/100.,
454 (double)gps_pos.pdop/100.,
455 (double)gps_pos.hdop/100.,
456 (double)gps_pos.vdop/100.,
457 (double)gps_pos.tdop/100.);
459 printf("lat %3.5f long %3.5f alt %3.5f sea_alt %3.5f\n",
460 (double)gps_pos.latitude/10000000.,
461 (double)gps_pos.longitude/10000000.,
462 (double)gps_pos.altitude/100.,
463 (double)gps_pos.sea_altitude/100.);
465 printf("vx %3.3f vy %3.3f vz %3.3f\n",
466 (double)gps_pos.ecef_vx/100.,
467 (double)gps_pos.ecef_vy/100.,
468 (double)gps_pos.ecef_vz/100.);
471 static void gps_venus_cb(struct callout_mgr *cm, struct callout *tim, void *arg)
481 c = uart_recv_nowait(GPS_UART);
482 if (c < 0) /* no more char */
487 decode_gps_pos(rxframe.data, rxframe.len);
494 callout_schedule(cm, tim, 2);
497 static void venus634_configure(void)
499 /* ask the GPS to reset */
504 /* it seems we must wait that the GPS is restarted, else it doesn't work
508 printf("binmode...");
520 printf("GPS configuration done !\n");
524 https://www.sparkfun.com/datasheets/GPS/Modules/AN0003_v1.4.14_FlashOnly.pdf
527 int gps_venus_init(void)
529 venus634_configure();
531 callout_init(&gps_timer, gps_venus_cb, NULL, GPS_PRIO);
532 callout_schedule(&imuboard.intr_cm, &gps_timer, 2); /* every 2ms */
537 void gps_get_pos(struct gps_pos *pos)
539 memcpy(pos, &gps_pos, sizeof(*pos));
544 struct fat_file_struct *fd = NULL;
559 /* get position (prevent modification of gps pos during copy) */
560 prio = callout_mgr_set_prio(&imuboard.intr_cm, GPS_PRIO);
562 callout_mgr_restore_prio(&imuboard.intr_cm, prio);
565 len = snprintf(buf, sizeof(buf),
567 "svnum %.2X lat %3.5f long %3.5f "
568 "alt %3.5f sea_alt %3.5f\n",
570 (double)gps_pos.latitude/10000000.,
571 (double)gps_pos.longitude/10000000.,
572 (double)gps_pos.altitude/100.,
573 (double)gps_pos.sea_altitude/100.);
578 if (fat_write_file(fd, (unsigned char *)buf, len) != len) {
579 printf_P(PSTR("error writing to file\n"));