#include "sti_gnss_lib.h" #include "GNSS.h" const uint8_t blue_led = 0; double ticks_per_us = 0; void setup() { const char reboot_msg[] = "$REBOOTED*00\r\n"; // put your setup code here, to run once: GnssConf.init(); /* do initialization for GNSS */ GnssInfo.timestamp.setTrigCapture(TS_TRIG_ON, TS_TRIG_RISING, (void*)userFuncforTimeStamp); gnss_gpio_set_output(blue_led); gnss_gpio_set_output(GPIO10_TRIG); // change timestmap trigger to be an output gnss_uart_putline(0, (U08*)reboot_msg, strlen(reboot_msg)); // needed for micros() ticks_per_us = gnss_get_cpu_clock() / 1000000.0; } volatile uint32_t *dsu_timetag = (uint32_t *)0x90000008; // DSU Trace Buffer Time Tag Counter Register // dsu_timetag counts up once per clock cycle and wraps at 2^30 // this function wraps roughly every 13 seconds uint32_t micros() { return (uint32_t)(*dsu_timetag / ticks_per_us); } uint32_t tag_to_micros(uint32_t tag) { return (uint32_t)(tag / ticks_per_us); } unsigned long printTime; int printValid = 0; uint32_t startTrig = 0; uint32_t endTrig = 0; uint32_t foundTrig = 0; void loop() { char buf[64]; uint8_t checksum = 0, i; // each run through has somewhere around 500us before the next ms interrupt // synchronize to right after millis changes (happens right after ms interrupt) if(printValid > 0 && millis() > printTime) { printValid++; switch(printValid) { case 2: // trigger timestamp to find the GPS time for a specific CPU clock time startTrig = *dsu_timetag; gnss_gpio_high(GPIO10_TRIG); gnss_uart_tx_send(0, '$'); endTrig = *dsu_timetag; break; case 3: // print GPS timestamp if (GnssInfo.timestamp.numRecord() != 0) { if (GnssInfo.timestamp.pop() == true) { gnss_uart_putline(0, (U08*)"TS", 2); GnssInfo.timestamp.convertTimeStampToUTC(); GnssInfo.timestamp.formatUTCString(buf); for(i = 0; i < strlen(buf); i++) { checksum ^= buf[i]; } gnss_uart_putline(0, (U08*)buf, strlen(buf)); sprintf(buf, "*%02X\r\n", checksum); gnss_uart_putline(0, (U08*)buf, strlen(buf)); } } else { gnss_uart_putline(0, (U08*)"TS*07\r\n", 7); } gnss_gpio_low(GPIO10_TRIG); break; case 4: // print CPU clock time sprintf(buf, "$ZZAAC start=%u end=%u found=%u", tag_to_micros(startTrig), tag_to_micros(endTrig), tag_to_micros(foundTrig)); for(i = 0; i < strlen(buf); i++) { checksum ^= buf[i]; } gnss_uart_putline(0, (U08*)buf, strlen(buf)); sprintf(buf, "*%02X\r\n", checksum); gnss_uart_putline(0, (U08*)buf, strlen(buf)); break; default: // end printValid = 0; break; } printTime = millis() + 8; // should take roughly 7ms to print 80 bytes, wait longer than that before moving on } } void flash_blue_led_on_lock() { static uint8_t val = 0; GnssInfo.update(); if (GnssInfo.isUpdated() != true) { val = 0; } else { val = ( val + 1 ) % 2; } if(val == 1) { gnss_gpio_high(blue_led); } else { gnss_gpio_low(blue_led); } } /* NOTE: "task_called_after_GNSS_update()" will be called about every 1 second in case of update rate is 1Hz, so we display the running time here. */ void task_called_after_GNSS_update(void) { flash_blue_led_on_lock(); // start our serial output in 220ms, avoiding NMEA output that's currently in the serial buffer and run after the next 2 nav updates (every 100ms) printTime = millis() + 220; printValid = 1; } /* NOTE: Since this callback function will occupy the time slot for GNSS interrupt, * user should NOT do complex jobs here or otherwise the NavSpark may fail to * lock GNSS. */ void userFuncforTimeStamp(TIME_STAMPING_STATUS_T ts) { foundTrig = *dsu_timetag; if (ts.time_is_valid) { GnssInfo.timestamp.push(ts); } }