diff --git a/include/rtc.h b/include/rtc.h index 20576580..ffe46da5 100644 --- a/include/rtc.h +++ b/include/rtc.h @@ -5,9 +5,17 @@ #include // must be included here so that Arduino library object file references work #include +typedef enum { + useless = 0, // waiting for good enough signal + dirty = 1, // time data available but inconfident + reserve = 2, // clock was once synced but now may deviate + synced_LORA = 3, // clock driven by LORAWAN network + synced_GPS = 4 // best possible quality, clock is driven by GPS +} clock_state_t; + int rtc_init(void); -int set_rtc(uint32_t UTCTime); -int set_rtc(RtcDateTime now); +int set_rtc(uint32_t UTCTime, clock_state_t state); +int set_rtc(RtcDateTime now, clock_state_t state); uint32_t get_rtc(); float get_rtc_temp(); diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 90a1b512..57575c92 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -136,7 +136,7 @@ void do_timesync() { now.hour = gps.time.hour(); now.minute = gps.time.minute(); now.second = gps.time.second(); - set_rtc(now); + set_rtc(now, synced_GPS); #endif ESP_LOGI(TAG, "Time synced by GPS to %02d:%02d:%02d", hour(), minute(), second()); @@ -153,6 +153,13 @@ void do_timesync() { ESP_LOGI(TAG, "Network time request scheduled"); #endif +// use on board time if board has RTC +#ifdef HAS_RTC + setTime(get_rtc()); + ESP_LOGI(TAG, "Time synced by RTC to %02d:%02d:%02d", hour(), minute(), + second()); +#endif + #endif // TIME_SYNC_INTERVAL } // do_timesync() diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 6477ad0b..d11d8af4 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -456,7 +456,7 @@ void user_request_network_time_callback(void *pVoidUserUTCTime, // Update system time with time read from the network setTime(*pUserUTCTime); #ifdef HAS_RTC - set_rtc(*pUserUTCTime); + set_rtc(*pUserUTCTime, synced_LORA); #endif ESP_LOGI(TAG, "Time synced by LoRa network to %02d:%02d:%02d", hour(), minute(), second()); diff --git a/src/main.cpp b/src/main.cpp index 635e943d..2fb98294 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -175,6 +175,9 @@ void setup() { #ifdef HAS_RTC strcat_P(features, " RTC"); assert(rtc_init()); + setTime(get_rtc()); + ESP_LOGI(TAG, "Time synced by RTC to %02d:%02d:%02d", hour(), minute(), + second()); #endif // initialize wifi antenna diff --git a/src/rtc.cpp b/src/rtc.cpp index a936faea..3e4cf90b 100644 --- a/src/rtc.cpp +++ b/src/rtc.cpp @@ -7,6 +7,8 @@ static const char TAG[] = "main"; RtcDS3231 Rtc(Wire); +clock_state_t RTC_state = useless; + // initialize RTC int rtc_init() { @@ -24,6 +26,7 @@ int rtc_init() { if (!Rtc.IsDateTimeValid()) { ESP_LOGW(TAG, "RTC has no valid RTC date/time, setting to compilation date"); Rtc.SetDateTime(compiled); + RTC_state = useless; } if (!Rtc.GetIsRunning()) { @@ -32,10 +35,12 @@ int rtc_init() { } RtcDateTime now = Rtc.GetDateTime(); + RTC_state = reserve; if (now < compiled) { ESP_LOGI(TAG, "RTC date/time is older than compilation date, updating)"); Rtc.SetDateTime(compiled); + RTC_state = useless; } // configure RTC chip @@ -56,26 +61,28 @@ error: } // rtc_init() -int set_rtc(uint32_t UTCTime) { +int set_rtc(uint32_t UTCTime, clock_state_t state) { // return = 0 -> error / return = 1 -> success #ifdef HAS_RTC // block i2c bus access while (xSemaphoreTake(I2Caccess, 2 * DISPLAYREFRESH_MS) == pdTRUE) { Rtc.SetDateTime(RtcDateTime(UTCTime)); xSemaphoreGive(I2Caccess); // release i2c bus access + RTC_state = state; return 1; } // while return 0; #endif } // set_rtc() -int set_rtc(RtcDateTime now) { +int set_rtc(RtcDateTime now, clock_state_t state) { // return = 0 -> error / return = 1 -> success #ifdef HAS_RTC // block i2c bus access while (xSemaphoreTake(I2Caccess, 2 * DISPLAYREFRESH_MS) == pdTRUE) { Rtc.SetDateTime(now); xSemaphoreGive(I2Caccess); // release i2c bus access + RTC_state = state; return 1; } // while return 0;