diff --git a/include/rtctime.h b/include/rtctime.h index 4cc0d842..ae9925b4 100644 --- a/include/rtctime.h +++ b/include/rtctime.h @@ -2,9 +2,14 @@ #define _RTCTIME_H #include "globals.h" +#include #include // must be included here so that Arduino library object file references work #include +#ifdef HAS_GPS +#include "gpsread.h" +#endif + typedef enum { useless = 0, // waiting for good enough signal dirty = 1, // time data available but inconfident @@ -15,10 +20,11 @@ typedef enum { int rtc_init(void); void sync_rtctime(void); -int set_rtctime(uint32_t UTCTime, clock_state_t state); -int set_rtctime(RtcDateTime now, clock_state_t state); +int set_rtctime(uint32_t UTCTime); +int set_rtctime(RtcDateTime now); void sync_rtctime(void); time_t get_rtctime(void); float get_rtctemp(void); +time_t gpsTimeSync(void); #endif // _RTCTIME_H \ No newline at end of file diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 09514423..6b4b5c88 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -123,32 +123,34 @@ void reset_counters() { void do_timesync() { #ifdef TIME_SYNC_INTERVAL -// sync time & date by GPS if we have valid gps time +// set system time to time source GPS, if we have valid gps time #ifdef HAS_GPS if (gps.time.isValid()) { setTime(gps.time.hour(), gps.time.minute(), gps.time.second(), gps.date.day(), gps.date.month(), gps.date.year()); +// set RTC time to time source GPS, if RTC is present #ifdef HAS_RTC - RtcDateTime now; - now.year = gps.time.year(); - now.month = gps.date.month(); - now.dayOfMonth = gps.date.day(); - now.hour = gps.time.hour(); - now.minute = gps.time.minute(); - now.second = gps.time.second(); - set_rtctime(now, synced_GPS); + RtcDateTime t; + t.year = gps.time.year(); + t.month = gps.date.month(); + t.dayOfMonth = gps.date.day(); + t.hour = gps.time.hour(); + t.minute = gps.time.minute(); + t.second = gps.time.second(); + set_rtctime(t); #endif - ESP_LOGI(TAG, "Time synced by GPS to %02d:%02d:%02d", hour(), minute(), - second()); + time_t t = now(); + ESP_LOGI(TAG, "GPS has set system time to %02d/%02d/%d %02d:%02d:%02d", + month(t), day(t), year(t), hour(t), minute(t), second(t)); return; } else { ESP_LOGI(TAG, "No valid GPS time"); } #endif // HAS_GPS -// sync time by LoRa Network if network supports DevTimeReq +// set system time to time source LoRa Network, if network supports DevTimeReq #ifdef LMIC_ENABLE_DeviceTimeReq - // Schedule a network time request at the next possible time + // Schedule a network time sync request at the next possible time LMIC_requestNetworkTime(user_request_network_time_callback, &userUTCTime); ESP_LOGI(TAG, "Network time request scheduled"); #endif diff --git a/src/lorawan.cpp b/src/lorawan.cpp index b495eda0..437a7e61 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -456,8 +456,10 @@ void user_request_network_time_callback(void *pVoidUserUTCTime, // Update system time with time read from the network setTime(*pUserUTCTime); #ifdef HAS_RTC - set_rtctime(*pUserUTCTime, synced_LORA); + set_rtctime(*pUserUTCTime); #endif - ESP_LOGI(TAG, "Time synced by LoRa network to %02d:%02d:%02d", hour(), - minute(), second()); + time_t t = now(); + ESP_LOGI(TAG, + "LORA Network has set system time to %02d/%02d/%d %02d:%02d:%02d", + month(t), day(t), year(t), hour(t), minute(t), second(t)); } \ No newline at end of file diff --git a/src/rtctime.cpp b/src/rtctime.cpp index 29c10f30..a58c0352 100644 --- a/src/rtctime.cpp +++ b/src/rtctime.cpp @@ -7,10 +7,8 @@ static const char TAG[] = "main"; RtcDS3231 Rtc(Wire); -clock_state_t RTC_state = useless; - // initialize RTC -int rtc_init() { +int rtc_init(void) { // return = 0 -> error / return = 1 -> success @@ -27,7 +25,6 @@ int rtc_init() { ESP_LOGW(TAG, "RTC has no valid RTC date/time, setting to compilation date"); Rtc.SetDateTime(compiled); - RTC_state = useless; } if (!Rtc.GetIsRunning()) { @@ -36,12 +33,10 @@ 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 @@ -62,77 +57,60 @@ error: } // rtc_init() -int set_rtctime(uint32_t UTCTime, clock_state_t state) { +int set_rtctime(uint32_t UTCTime) { // return = 0 -> error / return = 1 -> success // block i2c bus access while (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) { -#ifdef TIME_SYNC_INTERVAL_RTC - // shortly stop sync. - setSyncProvider(NULL); -#endif Rtc.SetDateTime(RtcDateTime(UTCTime)); -#ifdef TIME_SYNC_INTERVAL_RTC - // restart sync. - setSyncProvider(get_rtctime); -#endif xSemaphoreGive(I2Caccess); // release i2c bus access - RTC_state = state; return 1; } return 0; } // set_rtctime() -int set_rtctime(RtcDateTime now, clock_state_t state) { +int set_rtctime(RtcDateTime t) { // return = 0 -> error / return = 1 -> success // block i2c bus access while (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) { -#ifdef TIME_SYNC_INTERVAL_RTC - // shortly stop sync. - setSyncProvider(NULL); -#endif - Rtc.SetDateTime(now); -#ifdef TIME_SYNC_INTERVAL_RTC - // restart sync. - setSyncProvider(get_rtctime); -#endif + Rtc.SetDateTime(t); xSemaphoreGive(I2Caccess); // release i2c bus access - RTC_state = state; return 1; } return 0; } // set_rtctime() -time_t get_rtctime() { - time_t rslt = now(); +time_t get_rtctime(void) { + // never call now() in this function, would cause recursion! + time_t tt = 0; // block i2c bus access - while (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) { - if (!Rtc.IsDateTimeValid()) + if (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) { + if (!Rtc.IsDateTimeValid()) { ESP_LOGW(TAG, "RTC lost confidence in the DateTime"); - else - rslt = (time_t)(Rtc.GetDateTime()).Epoch32Time(); + } else { + RtcDateTime t = Rtc.GetDateTime(); + tt = t.Epoch32Time(); + } xSemaphoreGive(I2Caccess); // release i2c bus access - return rslt; + return tt; } - return rslt; -} // get_rtc() + return tt; +} // get_rtctime() -void sync_rtctime() { - time_t t = get_rtctime(); - ESP_LOGI(TAG, "RTC has set system time to %02d/%02d/%d %02d:%02d:%02d", - month(t), day(t), year(t), hour(t), minute(t), second(t)); +void sync_rtctime(void) { #ifdef TIME_SYNC_INTERVAL_RTC - setSyncInterval((time_t)TIME_SYNC_INTERVAL_RTC); - //setSyncProvider(get_rtctime); // <<<-- BUG here, causes watchdog timer1 group reboot - setSyncProvider(NULL); // dummy supressing time sync, to be removed after bug is solved + setSyncProvider(&get_rtctime); + setSyncInterval(TIME_SYNC_INTERVAL_RTC); if (timeStatus() != timeSet) { ESP_LOGE(TAG, "Unable to sync with the RTC"); } else { - ESP_LOGI(TAG, "RTC has set the system time"); + time_t t = now(); + ESP_LOGI(TAG, "RTC has set system time to %02d/%02d/%d %02d:%02d:%02d", + month(t), day(t), year(t), hour(t), minute(t), second(t)); } #endif } // sync_rtctime; -float get_rtctemp() { +float get_rtctemp(void) { // block i2c bus access while (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) { RtcTemperature temp = Rtc.GetTemperature(); @@ -142,4 +120,17 @@ float get_rtctemp() { return 0; } // get_rtc() +time_t gpsTimeSync(void) { +#ifdef HAS_GPS + tmElements_t tm; + tm.Second = gps.time.second(); + tm.Minute = gps.time.minute(); + tm.Hour = gps.time.hour(); + tm.Day = gps.date.day(); + tm.Month = gps.date.month(); + tm.Year = CalendarYrToTm(gps.date.year()); + return makeTime(tm); +#endif // HAS_GPS +} + #endif // HAS_RTC \ No newline at end of file