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..0de8e176 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,18 @@ 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++; + uint16_t ppsDiff = millis() - lastRTCpulse; + if (ppsDiff < 1000) + *msec = ppsDiff; + else { + ESP_LOGD(TAG, "no pulse from RTC"); + return 0; + } #endif -*/ + return t; } // get_rtctime() diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 896f49ec..ee0a4ed1 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(); @@ -190,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);