From 79303d6b7a97ddd8a9de1ab6c845a2d77a62308a Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sun, 20 Jan 2019 22:38:53 +0100 Subject: [PATCH] rtc support (experimental) --- include/cyclic.h | 2 +- include/lorawan.h | 2 +- include/{rtc.h => rtctime.h} | 16 ++++--- src/cyclic.cpp | 9 +--- src/lorawan.cpp | 2 +- src/main.cpp | 12 +++--- src/paxcounter.conf | 3 +- src/{rtc.cpp => rtctime.cpp} | 84 +++++++++++++++++++++++------------- 8 files changed, 75 insertions(+), 55 deletions(-) rename include/{rtc.h => rtctime.h} (64%) rename src/{rtc.cpp => rtctime.cpp} (53%) diff --git a/include/cyclic.h b/include/cyclic.h index 8a83217c..8e578009 100644 --- a/include/cyclic.h +++ b/include/cyclic.h @@ -13,7 +13,7 @@ // Needed for RTC time sync if RTC present on board #ifdef HAS_RTC -#include "rtc.h" +#include "rtctime.h" #endif void doHousekeeping(void); diff --git a/include/lorawan.h b/include/lorawan.h index a7f2cea0..2bd1b55b 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -19,7 +19,7 @@ // Needed for RTC time sync if RTC present on board #ifdef HAS_RTC -#include "rtc.h" +#include "rtctime.h" #endif extern QueueHandle_t LoraSendQueue; diff --git a/include/rtc.h b/include/rtctime.h similarity index 64% rename from include/rtc.h rename to include/rtctime.h index ffe46da5..4cc0d842 100644 --- a/include/rtc.h +++ b/include/rtctime.h @@ -1,5 +1,5 @@ -#ifndef _RTC_H -#define _RTC_H +#ifndef _RTCTIME_H +#define _RTCTIME_H #include "globals.h" #include // must be included here so that Arduino library object file references work @@ -14,9 +14,11 @@ typedef enum { } clock_state_t; int rtc_init(void); -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(); +void sync_rtctime(void); +int set_rtctime(uint32_t UTCTime, clock_state_t state); +int set_rtctime(RtcDateTime now, clock_state_t state); +void sync_rtctime(void); +time_t get_rtctime(void); +float get_rtctemp(void); -#endif \ No newline at end of file +#endif // _RTCTIME_H \ No newline at end of file diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 57575c92..09514423 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, synced_GPS); + set_rtctime(now, synced_GPS); #endif ESP_LOGI(TAG, "Time synced by GPS to %02d:%02d:%02d", hour(), minute(), second()); @@ -153,13 +153,6 @@ 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 d11d8af4..b495eda0 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, synced_LORA); + set_rtctime(*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 2fb98294..c5844dc9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -175,12 +175,10 @@ 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()); + sync_rtctime(); #endif - // initialize wifi antenna +// initialize wifi antenna #ifdef HAS_ANTENNA_SWITCH strcat_P(features, " ANT"); antenna_init(); @@ -321,7 +319,7 @@ void setup() { ESP_LOGI(TAG, "Features:%s", features); #ifdef HAS_LORA - // output LoRaWAN keys to console +// output LoRaWAN keys to console #ifdef VERBOSE showLoraKeys(); #endif @@ -355,7 +353,7 @@ void setup() { &wifiSwitchTask, // task handle 0); // CPU core - // initialize bme +// initialize bme #ifdef HAS_BME strcat_P(features, " BME"); if (bme_init()) { @@ -379,7 +377,7 @@ void setup() { timerAlarmEnable(homeCycle); timerAlarmEnable(channelSwitch); - // start button interrupt +// start button interrupt #ifdef HAS_BUTTON #ifdef BUTTON_PULLUP attachInterrupt(digitalPinToInterrupt(HAS_BUTTON), ButtonIRQ, RISING); diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 007561c2..41420cc8 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -82,7 +82,8 @@ #define RESPONSE_TIMEOUT_MS 60000 // firmware binary server connection timeout [milliseconds] // setting for syncing time of node -//#define TIME_SYNC_INTERVAL 60 // sync time each ... minutes [default = 60], comment out means off +//#define TIME_SYNC_INTERVAL 60 // sync time each ... minutes with external source [default = 60], comment out means off +#define TIME_SYNC_INTERVAL_RTC 5 // sync time each ... minutes with RTC [default = 5], comment out means off // LMIC settings // moved to src/lmic_config.h diff --git a/src/rtc.cpp b/src/rtctime.cpp similarity index 53% rename from src/rtc.cpp rename to src/rtctime.cpp index 3e4cf90b..29c10f30 100644 --- a/src/rtc.cpp +++ b/src/rtctime.cpp @@ -1,6 +1,6 @@ #ifdef HAS_RTC -#include "rtc.h" +#include "rtctime.h" // Local logging tag static const char TAG[] = "main"; @@ -15,7 +15,7 @@ int rtc_init() { // return = 0 -> error / return = 1 -> success // block i2c bus access - if (xSemaphoreTake(I2Caccess, (2 * DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) == + if (xSemaphoreTake(I2Caccess, (DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) == pdTRUE) { Wire.begin(HAS_RTC); @@ -24,7 +24,8 @@ int rtc_init() { RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); if (!Rtc.IsDateTimeValid()) { - ESP_LOGW(TAG, "RTC has no valid RTC date/time, setting to compilation date"); + ESP_LOGW(TAG, + "RTC has no valid RTC date/time, setting to compilation date"); Rtc.SetDateTime(compiled); RTC_state = useless; } @@ -61,59 +62,84 @@ error: } // rtc_init() -int set_rtc(uint32_t UTCTime, clock_state_t state) { +int set_rtctime(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) { + 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; - } // while + } return 0; -#endif -} // set_rtc() +} // set_rtctime() -int set_rtc(RtcDateTime now, clock_state_t state) { +int set_rtctime(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) { + 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 xSemaphoreGive(I2Caccess); // release i2c bus access RTC_state = state; return 1; - } // while + } return 0; -#endif -} // set_rtc() +} // set_rtctime() -uint32_t get_rtc() { -#ifdef HAS_RTC +time_t get_rtctime() { + time_t rslt = now(); // block i2c bus access - while (xSemaphoreTake(I2Caccess, 2 * DISPLAYREFRESH_MS) == pdTRUE) { - if (!Rtc.IsDateTimeValid()) { + while (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) { + if (!Rtc.IsDateTimeValid()) ESP_LOGW(TAG, "RTC lost confidence in the DateTime"); - return 0; - } + else + rslt = (time_t)(Rtc.GetDateTime()).Epoch32Time(); xSemaphoreGive(I2Caccess); // release i2c bus access - return Rtc.GetDateTime(); - } // while - return 0; -#endif + return rslt; + } + return rslt; } // get_rtc() -float get_rtc_temp() { -#ifdef HAS_RTC +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)); +#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 + if (timeStatus() != timeSet) { + ESP_LOGE(TAG, "Unable to sync with the RTC"); + } else { + ESP_LOGI(TAG, "RTC has set the system time"); + } +#endif +} // sync_rtctime; + +float get_rtctemp() { // block i2c bus access - while (xSemaphoreTake(I2Caccess, 2 * DISPLAYREFRESH_MS) == pdTRUE) { + while (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) { RtcTemperature temp = Rtc.GetTemperature(); xSemaphoreGive(I2Caccess); // release i2c bus access return temp.AsFloatDegC(); } // while return 0; -#endif } // get_rtc() #endif // HAS_RTC \ No newline at end of file