From 8c0ad6f84927e3f9c3d662509f1168b84cb018a2 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 29 Jan 2019 22:54:16 +0100 Subject: [PATCH] another bugfixes in realtime handling --- include/rtctime.h | 12 ++---------- src/cyclic.cpp | 14 ++++++++++++++ src/lorawan.cpp | 2 +- src/main.cpp | 2 +- src/paxcounter.conf | 7 ++++--- src/rtctime.cpp | 19 ++++--------------- 6 files changed, 26 insertions(+), 30 deletions(-) diff --git a/include/rtctime.h b/include/rtctime.h index d47ae19c..d55c3469 100644 --- a/include/rtctime.h +++ b/include/rtctime.h @@ -9,19 +9,11 @@ #include "gpsread.h" #endif -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; - extern RtcDS3231 Rtc; // make RTC instance globally available int rtc_init(void); -int set_rtctime(uint32_t UTCTime); -int set_rtctime(RtcDateTime now); +int set_rtctime(RtcDateTime t); +int set_rtctime(uint32_t t); void sync_rtctime(void); time_t get_rtctime(void); float get_rtctemp(void); diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 543b6e93..89c44df4 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -9,6 +9,7 @@ static const char TAG[] = "main"; time_t userUTCTime; // Seconds since the UTC epoch unsigned long nextLoraTimeSync = millis(); +unsigned long nextRTCTimeSync = millis() + TIME_WRITE_INTERVAL_RTC * 60000; // do all housekeeping void doHousekeeping() { @@ -34,6 +35,19 @@ void doHousekeeping() { } #endif +// do cyclic write back system time to RTC if we have an external time source +#if (defined TIME_SYNC_INTERVAL_LORA || defined TIME_SYNC_INTERVAL_GPS) && \ + defined HAS_RTC + if ((millis() >= nextRTCTimeSync) && timeSet) { + nextRTCTimeSync = millis() + TIME_WRITE_INTERVAL_RTC * + 60000; // set up next time sync period + if (!set_rtctime(now())) + ESP_LOGE(TAG, "RTC set time failure"); + else + ESP_LOGI(TAG, "RTC time updated"); + } +#endif + // task storage debugging // ESP_LOGD(TAG, "Wifiloop %d bytes left | Taskstate = %d", uxTaskGetStackHighWaterMark(wifiSwitchTask), diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 654b1469..d8350b5b 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -457,7 +457,7 @@ void user_request_network_time_callback(void *pVoidUserUTCTime, setTime(*pUserUTCTime); ESP_LOGI(TAG, "LoRaWAN network has set the system time"); #ifdef HAS_RTC - if (set_rtctime(*pUserUTCTime)) + if (!set_rtctime(*pUserUTCTime)) ESP_LOGE(TAG, "RTC set time failure"); #endif } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5703501f..7bee39c1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -429,7 +429,7 @@ void setup() { else { ESP_LOGI(TAG, "GPS has set the system time"); #ifdef HAS_RTC - if (set_rtctime(now())) + if (!set_rtctime(now())) ESP_LOGE(TAG, "RTC set time failure"); #endif } diff --git a/src/paxcounter.conf b/src/paxcounter.conf index a97f8b9e..1c3c5618 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -82,9 +82,10 @@ #define RESPONSE_TIMEOUT_MS 60000 // firmware binary server connection timeout [milliseconds] // settings for syncing time of node and external time sources -#define TIME_SYNC_INTERVAL_GPS 60 // sync time each ... minutes with GPS [default = 60], comment out means off -#define TIME_SYNC_INTERVAL_RTC 60 // sync time each ... minutes with RTC [default = 60], comment out means off -//#define TIME_SYNC_INTERVAL_LORA 60 // sync time each ... minutes with LORA network [default = 60], comment out means off +#define TIME_SYNC_INTERVAL_GPS 60 // sync time each .. minutes from source GPS [default = 60], comment out means off +#define TIME_SYNC_INTERVAL_RTC 60 // sync time each .. minutes from RTC [default = 60], comment out means off +#define TIME_WRITE_INTERVAL_RTC 60 // write time each .. minutes from GPS/LORA to RTC [default = 60], comment out means off +//#define TIME_SYNC_INTERVAL_LORA 60 // sync time each .. minutes from LORA network [default = 60], comment out means off #define IF482_OFFSET 984 // 1sec minus IF482 serial transmit time [ms]: e.g. 9 bits * 17 bytes * 1/9600 bps = 16ms // time zone, see https://github.com/JChristensen/Timezone/blob/master/examples/WorldClock/WorldClock.ino diff --git a/src/rtctime.cpp b/src/rtctime.cpp index 044a8a96..54801e1e 100644 --- a/src/rtctime.cpp +++ b/src/rtctime.cpp @@ -57,28 +57,17 @@ error: } // rtc_init() -int set_rtctime(uint32_t t) { - // return = 0 -> error / return = 1 -> success - // block i2c bus access - if (I2C_MUTEX_LOCK()) { - Rtc.SetDateTime(RtcDateTime(t)); - I2C_MUTEX_UNLOCK(); // release i2c bus access - return 1; - } - return 0; -} // set_rtctime() - int set_rtctime(RtcDateTime t) { - // return = 0 -> error / return = 1 -> success - // block i2c bus access if (I2C_MUTEX_LOCK()) { Rtc.SetDateTime(t); I2C_MUTEX_UNLOCK(); // release i2c bus access - return 1; + return 1; // success } - return 0; + return 0; // failure } // set_rtctime() +int set_rtctime(uint32_t t) { return set_rtctime(RtcDateTime(t)); }; + time_t get_rtctime(void) { // never call now() in this function, this would cause a recursion! time_t t = 0;