From f74f103e80ea0c8b900bac0554196091f43ded50 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 5 Mar 2022 18:49:50 +0100 Subject: [PATCH 1/2] fix for RTC syncing (experimental) --- include/timekeeper.h | 5 +++++ src/rtctime.cpp | 14 ++++---------- src/timekeeper.cpp | 13 +++++++++++++ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/include/timekeeper.h b/include/timekeeper.h index fe4dfb9e..aa13d5ea 100644 --- a/include/timekeeper.h +++ b/include/timekeeper.h @@ -24,7 +24,12 @@ extern Ticker timesyncer; extern timesource_t timeSource; extern TaskHandle_t ClockTask; extern DRAM_ATTR bool TimePulseTick; // 1sec pps flag set by GPS or RTC +#ifdef GPS_INT extern DRAM_ATTR unsigned long lastPPS; +#endif +#ifdef RTC_INT +extern DRAM_ATTR unsigned long lastRTCpulse; +#endif extern hw_timer_t *ppsIRQ; void setTimeSyncIRQ(void); diff --git a/src/rtctime.cpp b/src/rtctime.cpp index 50280d3b..7bbb4844 100644 --- a/src/rtctime.cpp +++ b/src/rtctime.cpp @@ -52,9 +52,6 @@ uint8_t set_rtctime(time_t t) { // t is sec epoch time ESP_LOGI(TAG, "RTC time synced"); return 1; // success - // failure - // return 0 - } // set_rtctime() time_t get_rtctime(uint16_t *msec) { @@ -65,15 +62,12 @@ time_t get_rtctime(uint16_t *msec) { RtcDateTime tt = Rtc.GetDateTime(); t = tt.Epoch32Time(); // sec2000 -> epoch } - /* + +// if we have a RTC pulse, we calculate msec #ifdef RTC_INT - // adjust time to top of next second by waiting TimePulseTick to flip - bool lastTick = TimePulseTick; - while (TimePulseTick == lastTick) { - }; - t++; + *msec = lastRTCpulse % 1000; #endif -*/ + return t; } // get_rtctime() diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 896f49ec..97c63064 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -12,7 +12,13 @@ static const char TAG[] = __FILE__; const char timeSetSymbols[] = {'G', 'R', 'L', '*', '?'}; DRAM_ATTR bool TimePulseTick = false; +#ifdef GPS_INT DRAM_ATTR unsigned long lastPPS = millis(); +#endif +#ifdef RTC_INT +DRAM_ATTR unsigned long lastRTCpulse = millis(); +#endif + timesource_t timeSource = _unsynced; TaskHandle_t ClockTask = NULL; hw_timer_t *ppsIRQ = NULL; @@ -31,6 +37,7 @@ Ticker timesyncer; void setTimeSyncIRQ() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); } +#ifdef GPS_INT // interrupt service routine triggered by GPS PPS void IRAM_ATTR GPSIRQ(void) { @@ -43,6 +50,7 @@ void IRAM_ATTR GPSIRQ(void) { if (xHigherPriorityTaskWoken) portYIELD_FROM_ISR(); } +#endif // interrupt service routine triggered by esp32 hardware timer void IRAM_ATTR CLOCKIRQ(void) { @@ -60,6 +68,11 @@ void IRAM_ATTR CLOCKIRQ(void) { TimePulseTick = !TimePulseTick; // flip global variable pulse ticker #endif +// take timestamp if we have RTC pulse +#ifdef RTC_INT + lastRTCpulse = millis(); // last time of RTC pulse +#endif + // yield only if we should if (xHigherPriorityTaskWoken) portYIELD_FROM_ISR(); From fbb187880728bb39024f80e1bf0776821935511a Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 5 Mar 2022 19:08:35 +0100 Subject: [PATCH 2/2] fix timesync from RTC --- src/rtctime.cpp | 8 +++++++- src/timekeeper.cpp | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/rtctime.cpp b/src/rtctime.cpp index 7bbb4844..0de8e176 100644 --- a/src/rtctime.cpp +++ b/src/rtctime.cpp @@ -65,7 +65,13 @@ time_t get_rtctime(uint16_t *msec) { // if we have a RTC pulse, we calculate msec #ifdef RTC_INT - *msec = lastRTCpulse % 1000; + uint16_t ppsDiff = millis() - lastRTCpulse; + if (ppsDiff < 1000) + *msec = ppsDiff; + else { + ESP_LOGD(TAG, "no pulse from RTC"); + return 0; + } #endif return t; diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 97c63064..ee0a4ed1 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -203,8 +203,10 @@ void timepulse_init(void) { #endif // get time if we don't have one - if (timeSource != _set) + if (timeSource != _set) { + delay(1000); // wait for first PPS time stamp to arrive setTimeSyncIRQ(); // init systime by RTC or GPS or LORA + } // start cyclic time sync timesyncer.attach(TIME_SYNC_INTERVAL * 60, setTimeSyncIRQ);