diff --git a/include/gpsread.h b/include/gpsread.h index 5413ffcb..6c20eef0 100644 --- a/include/gpsread.h +++ b/include/gpsread.h @@ -9,7 +9,7 @@ #include #endif -#define NMEA_FRAME_SIZE 80 // NEMA has a maxium of 80 bytes per record +#define NMEA_FRAME_SIZE 82 // NEMA has a maxium of 82 bytes per record #define NMEA_BUFFERTIME 50 // 50ms safety time regardless extern TinyGPSPlus gps; // Make TinyGPS++ instance globally availabe diff --git a/src/gpsread.cpp b/src/gpsread.cpp index ab06f199..c04eb432 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -1,4 +1,4 @@ -#if(HAS_GPS) +#if (HAS_GPS) #include "globals.h" @@ -11,6 +11,9 @@ TaskHandle_t GpsTask; #ifdef GPS_SERIAL HardwareSerial GPS_Serial(1); // use UART #1 +static TickType_t gps_txDelay = tx_Ticks(NMEA_FRAME_SIZE, GPS_SERIAL); +#else +static TickType_t gps_txDelay = 0; #endif // initialize and configure GPS @@ -77,22 +80,26 @@ void gps_read() { time_t get_gpstime(void) { // set time to wait for arrive next recent NMEA time record - static const uint32_t gpsDelay_ms = 500; + static const uint32_t gpsDelay_ms = 1000 - gps_txDelay / portTICK_PERIOD_MS; time_t t = 0; + uint32_t time_age = gps.time.age(); - if ((gps.time.age() < gpsDelay_ms) && gps.time.isValid() && - gps.date.isValid() && gps.time.isUpdated()) { - - gps.time.value(); // trigger isUpdated() - - ESP_LOGD(TAG, "GPS time age: %dms, is valid: %s, second: %d", - gps.time.age(), - (gps.time.isValid() && gps.date.isValid()) ? "yes" : "no", - gps.time.second()); + if ((time_age < gpsDelay_ms) && gps.time.isValid() && gps.date.isValid() && + gps.time.isUpdated()) { t = tmConvert(gps.date.year(), gps.date.month(), gps.date.day(), gps.time.hour(), gps.time.minute(), gps.time.second()); + + if (time_age < (gpsDelay_ms / 2)) + t--; + + ESP_LOGD(TAG, "GPS time age: %dms", time_age); + +#ifndef GPS_INT + // wait until top of second with millisecond precision + vTaskDelay(pdMS_TO_TICKS(1000 - time_age) - gps_txDelay); +#endif } return timeIsValid(t); } // get_gpstime() diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 7ea4c7a1..717c0463 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -20,9 +20,7 @@ HardwareSerial IF482(2); // use UART #2 (#1 may be in use for serial GPS) Ticker timesyncer; -void timeSync() { - xTaskNotifyFromISR(irqHandlerTask, TIMESYNC_IRQ, eSetBits, NULL); -} +void timeSync() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); } time_t timeProvider(void) { @@ -116,6 +114,7 @@ void timepulse_start(void) { timerAttachInterrupt(ppsIRQ, &CLOCKIRQ, true); timerAlarmEnable(ppsIRQ); #endif + now(); // refresh sysTime to pps // start cyclic time sync timeSync(); // init systime by RTC or GPS or LORA @@ -213,7 +212,7 @@ void clock_init(void) { void clock_loop(void *taskparameter) { // ClockTask // caveat: don't use now() in this task, it will cause a race condition - // due to concurrent access to i2c bus for setting rtc via SyncProvider! + // due to concurrent access to i2c bus for setting rtc! #define nextmin(t) (t + DCF77_FRAME_SIZE + 1) // next minute