From 1c40572c9b76a6d9b1431b85ed81c636512f4fd2 Mon Sep 17 00:00:00 2001 From: Oliver Seiler Date: Mon, 28 Sep 2020 09:49:41 +1300 Subject: [PATCH 1/2] Synch time from GPX on first valid GPS time received --- src/gpsread.cpp | 15 +++++++++++++++ src/timekeeper.cpp | 7 +++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/gpsread.cpp b/src/gpsread.cpp index 45f1b65d..650c5866 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -21,6 +21,9 @@ static uint16_t nmea_txDelay_ms = static uint16_t nmea_txDelay_ms = 0; #endif +// did the last packet contain a valid time? +bool hasValidTime = false; + // initialize and configure GPS int gps_init(void) { @@ -159,6 +162,18 @@ void gps_loop(void *pvParameters) { delay(2); // 2ms delay according L76 datasheet } #endif + +#if !TIME_SYNC_LORASERVER && !TIME_SYNC_LORAWAN + if (!hasValidTime) { + if (gpstime.isUpdated() && gpstime.isValid()) { + hasValidTime = true; + if (timeSource == _unsynced) { + calibrateTime(); + } + } + } +#endif + } // if // show NMEA data in verbose mode, useful for debugging GPS, bu tvery noisy diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index bc9e52b5..01777be7 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -26,7 +26,8 @@ Ticker timesyncer; void timeSync() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); } void calibrateTime(void) { - + ESP_LOGD(TAG, "[%0.3f] calibrateTime, timeSource == %d", millis() / 1000.0, + timeSource); time_t t = 0; uint16_t t_msec = 0; @@ -49,7 +50,9 @@ void calibrateTime(void) { // no RTC -> fallback to GPS time #if (HAS_GPS) t = get_gpstime(&t_msec); - timeSource = _gps; + if (t) { + timeSource = _gps; + } #endif setMyTime((uint32_t)t, t_msec, timeSource); // set time From 0a510a0c33b64ef360a43d4e01a5fc0d3f1440f0 Mon Sep 17 00:00:00 2001 From: Oliver Seiler Date: Tue, 29 Sep 2020 18:59:53 +1300 Subject: [PATCH 2/2] Improved time synch handling for GPS based time This will update the time from GPS on first valid time received by GPS. --- src/gpsread.cpp | 16 ++++------------ src/main.cpp | 5 ----- src/timekeeper.cpp | 12 +++++------- 3 files changed, 9 insertions(+), 24 deletions(-) diff --git a/src/gpsread.cpp b/src/gpsread.cpp index 650c5866..78683b7a 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -21,9 +21,6 @@ static uint16_t nmea_txDelay_ms = static uint16_t nmea_txDelay_ms = 0; #endif -// did the last packet contain a valid time? -bool hasValidTime = false; - // initialize and configure GPS int gps_init(void) { @@ -163,16 +160,11 @@ void gps_loop(void *pvParameters) { } #endif -#if !TIME_SYNC_LORASERVER && !TIME_SYNC_LORAWAN - if (!hasValidTime) { - if (gpstime.isUpdated() && gpstime.isValid()) { - hasValidTime = true; - if (timeSource == _unsynced) { - calibrateTime(); - } - } + // if time hasn't been synchronised yet, and we have a valid GPS time, + // update time from GPS. + if (timeSource == _unsynced && gpstime.isUpdated() && gpstime.isValid()) { + calibrateTime(); } -#endif } // if diff --git a/src/main.cpp b/src/main.cpp index e7b497c7..175e78f0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -488,11 +488,6 @@ void setup() { #warning you did not specify a time source, time will not be synched #endif -// initialize gps time -#if (HAS_GPS) - get_gpstime(); -#endif - #if (defined HAS_IF482 || defined HAS_DCF77) ESP_LOGI(TAG, "Starting Clock Controller..."); clock_init(); diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 01777be7..0dba7caa 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -32,7 +32,7 @@ void calibrateTime(void) { uint16_t t_msec = 0; // kick off asychronous lora timesync if we have -#if (HAS_LORA) && (TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN) +#if (HAS_LORA) && ((TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN)) timesync_request(); #endif @@ -44,19 +44,17 @@ void calibrateTime(void) { // has RTC -> fallback to RTC time #ifdef HAS_RTC t = get_rtctime(); - timeSource = _rtc; + // set time from RTC + setMyTime((uint32_t)t, t_msec, _rtc); #endif // no RTC -> fallback to GPS time #if (HAS_GPS) t = get_gpstime(&t_msec); - if (t) { - timeSource = _gps; - } + // set time from GPS - method will check if time is valid + setMyTime((uint32_t)t, t_msec, _gps); #endif - setMyTime((uint32_t)t, t_msec, timeSource); // set time - } // fallback else