diff --git a/include/lorawan.h b/include/lorawan.h index 394284be..0c870bb9 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -1,3 +1,5 @@ +#if (HAS_LORA) + #ifndef _LORAWAN_H #define _LORAWAN_H @@ -48,4 +50,6 @@ const char *getCrName(rps_t rps); void showLoraKeys(void); #endif // VERBOSE -#endif \ No newline at end of file +#endif + +#endif // HAS_LORA \ No newline at end of file diff --git a/include/timekeeper.h b/include/timekeeper.h index bb97a043..ee20913e 100644 --- a/include/timekeeper.h +++ b/include/timekeeper.h @@ -10,6 +10,8 @@ #include "dcf77.h" #include "esp_sntp.h" +#define HAS_LORA_TIME ((HAS_LORA) && ((TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN))) + #define SECS_YR_2000 (946684800UL) // the time at the start of y2k #define GPS_UTC_DIFF 315964800UL // seconds diff between gps and utc epoch #define LEAP_SECS_SINCE_GPSEPOCH 18UL // state of 2021 @@ -32,7 +34,7 @@ void setTimeSyncIRQ(void); uint8_t timepulse_init(void); bool timeIsValid(time_t const t); void calibrateTime(void); -void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, +bool IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, timesource_t mytimesource); time_t compileTime(void); time_t mkgmtime(const struct tm *ptm); diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 6a4d2904..f2765bf7 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -1,13 +1,5 @@ #include "timekeeper.h" -#if !(HAS_LORA) -#if (TIME_SYNC_LORASERVER) -#error TIME_SYNC_LORASERVER defined, but device has no LORA configured -#elif (TIME_SYNC_LORAWAN) -#error TIME_SYNC_LORAWAN defined, but device has no LORA configured -#endif -#endif - #if (defined HAS_DCF77 && defined HAS_IF482) #error You must define at most one of IF482 or DCF77! #endif @@ -44,48 +36,38 @@ void calibrateTime(void) { time_t t = 0; uint16_t t_msec = 0; - // kick off asychronous lora timesync if we have -#if (HAS_LORA) && ((TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN)) + // kick off asynchronous lora timesync if we have +#if (HAS_LORA_TIME) timesync_request(); -#endif - - // if no LORA timesource is available, or if we lost time, then fallback to - // local time source RTS or GPS - if (((!TIME_SYNC_LORASERVER) && (!TIME_SYNC_LORAWAN)) || - (timeSource == _unsynced)) { - -// has RTC -> fallback to RTC time -#ifdef HAS_RTC - t = get_rtctime(&t_msec); - // set time from RTC - method will check if time is valid - setMyTime((uint32_t)t, t_msec, _rtc); -#endif - -// no RTC -> fallback to GPS time -#if (HAS_GPS) - t = get_gpstime(&t_msec); - // set time from GPS - method will check if time is valid - setMyTime((uint32_t)t, t_msec, _gps); -#endif - - } // fallback - - else - - // no fallback time source available -> we can't set time + if (timeSource == _lora) // did have lora time before? return; +#endif + +// get GPS time, if we have +#if (HAS_GPS) + t = get_gpstime(&t_msec); + if (setMyTime((uint32_t)t, t_msec, _gps)) + return; +#endif + +// fallback to RTC time, if we have +#ifdef HAS_RTC + t = get_rtctime(&t_msec); + if (setMyTime((uint32_t)t, t_msec, _rtc)) + return; +#endif } // calibrateTime() // set system time (UTC), calibrate RTC and RTC_INT pps -void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, +bool IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, timesource_t mytimesource) { struct timeval tv = {0}; // called with invalid timesource? if (mytimesource == _unsynced) - return; + return false; // increment t_sec if t_msec > 1000 time_t time_to_set = (time_t)(t_sec + t_msec / 1000); @@ -126,13 +108,17 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, timesyncer.attach(TIME_SYNC_INTERVAL * 60, setTimeSyncIRQ); ESP_LOGD(TAG, "[%0.3f] Timesync finished, time was set | timesource=%d", _seconds(), mytimesource); + return true; + } else { + timesyncer.attach(TIME_SYNC_INTERVAL_RETRY * 60, setTimeSyncIRQ); ESP_LOGV(TAG, "[%0.3f] Failed to synchronise time from source %c | unix sec " "obtained from source: %d | unix sec at program compilation: %d", _seconds(), timeSetSymbols[mytimesource], time_to_set, compileTime()); + return false; } } @@ -172,7 +158,7 @@ uint8_t timepulse_init() { // use ESP32 hardware timer as time base for calendar time ppsIRQ = timerBegin(1, 8000, true); // set 80 MHz prescaler to 1/10000 sec timerAlarmWrite(ppsIRQ, 10000, true); // 1000ms - timerAttachInterrupt(ppsIRQ, &CLOCKIRQ, true); + timerAttachInterrupt(ppsIRQ, &CLOCKIRQ, false); timerAlarmEnable(ppsIRQ); ESP_LOGI(TAG, "Timepulse: internal (ESP32 hardware timer)"); return 1; // success diff --git a/src/timesync.cpp b/src/timesync.cpp index 9b2c5f99..80355eb8 100644 --- a/src/timesync.cpp +++ b/src/timesync.cpp @@ -13,6 +13,8 @@ accept this. */ +#if (HAS_LORA) + #if (TIME_SYNC_LORASERVER) && (TIME_SYNC_LORAWAN) #error Duplicate timesync method selected. You must select either LORASERVER or LORAWAN timesync. #endif @@ -173,7 +175,7 @@ void timesync_store(uint32_t timestamp, timesync_t timestamp_type) { // callback function to receive time answer from network or answer void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag) { -#if (TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN) +#if (HAS_LORA_TIME) // if no timesync handshake is pending then exit if (!timeSyncPending) @@ -277,5 +279,7 @@ Exit: xTaskNotify(timeSyncProcTask, (rc ? rcv_seqNo : TIME_SYNC_END_FLAG), eSetBits); -#endif // (TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN) -} \ No newline at end of file +#endif // (HAS_LORA_TIME) +} + +#endif // HAS_LORA \ No newline at end of file