rtc support (experimental)

This commit is contained in:
Klaus K Wilting 2019-01-20 22:38:53 +01:00
parent 6f4bf4142b
commit 79303d6b7a
8 changed files with 75 additions and 55 deletions

View File

@ -13,7 +13,7 @@
// Needed for RTC time sync if RTC present on board // Needed for RTC time sync if RTC present on board
#ifdef HAS_RTC #ifdef HAS_RTC
#include "rtc.h" #include "rtctime.h"
#endif #endif
void doHousekeeping(void); void doHousekeeping(void);

View File

@ -19,7 +19,7 @@
// Needed for RTC time sync if RTC present on board // Needed for RTC time sync if RTC present on board
#ifdef HAS_RTC #ifdef HAS_RTC
#include "rtc.h" #include "rtctime.h"
#endif #endif
extern QueueHandle_t LoraSendQueue; extern QueueHandle_t LoraSendQueue;

View File

@ -1,5 +1,5 @@
#ifndef _RTC_H #ifndef _RTCTIME_H
#define _RTC_H #define _RTCTIME_H
#include "globals.h" #include "globals.h"
#include <Wire.h> // must be included here so that Arduino library object file references work #include <Wire.h> // must be included here so that Arduino library object file references work
@ -14,9 +14,11 @@ typedef enum {
} clock_state_t; } clock_state_t;
int rtc_init(void); int rtc_init(void);
int set_rtc(uint32_t UTCTime, clock_state_t state); void sync_rtctime(void);
int set_rtc(RtcDateTime now, clock_state_t state); int set_rtctime(uint32_t UTCTime, clock_state_t state);
uint32_t get_rtc(); int set_rtctime(RtcDateTime now, clock_state_t state);
float get_rtc_temp(); void sync_rtctime(void);
time_t get_rtctime(void);
float get_rtctemp(void);
#endif #endif // _RTCTIME_H

View File

@ -136,7 +136,7 @@ void do_timesync() {
now.hour = gps.time.hour(); now.hour = gps.time.hour();
now.minute = gps.time.minute(); now.minute = gps.time.minute();
now.second = gps.time.second(); now.second = gps.time.second();
set_rtc(now, synced_GPS); set_rtctime(now, synced_GPS);
#endif #endif
ESP_LOGI(TAG, "Time synced by GPS to %02d:%02d:%02d", hour(), minute(), ESP_LOGI(TAG, "Time synced by GPS to %02d:%02d:%02d", hour(), minute(),
second()); second());
@ -153,13 +153,6 @@ void do_timesync() {
ESP_LOGI(TAG, "Network time request scheduled"); ESP_LOGI(TAG, "Network time request scheduled");
#endif #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 #endif // TIME_SYNC_INTERVAL
} // do_timesync() } // do_timesync()

View File

@ -456,7 +456,7 @@ void user_request_network_time_callback(void *pVoidUserUTCTime,
// Update system time with time read from the network // Update system time with time read from the network
setTime(*pUserUTCTime); setTime(*pUserUTCTime);
#ifdef HAS_RTC #ifdef HAS_RTC
set_rtc(*pUserUTCTime, synced_LORA); set_rtctime(*pUserUTCTime, synced_LORA);
#endif #endif
ESP_LOGI(TAG, "Time synced by LoRa network to %02d:%02d:%02d", hour(), ESP_LOGI(TAG, "Time synced by LoRa network to %02d:%02d:%02d", hour(),
minute(), second()); minute(), second());

View File

@ -175,12 +175,10 @@ void setup() {
#ifdef HAS_RTC #ifdef HAS_RTC
strcat_P(features, " RTC"); strcat_P(features, " RTC");
assert(rtc_init()); assert(rtc_init());
setTime(get_rtc()); sync_rtctime();
ESP_LOGI(TAG, "Time synced by RTC to %02d:%02d:%02d", hour(), minute(),
second());
#endif #endif
// initialize wifi antenna // initialize wifi antenna
#ifdef HAS_ANTENNA_SWITCH #ifdef HAS_ANTENNA_SWITCH
strcat_P(features, " ANT"); strcat_P(features, " ANT");
antenna_init(); antenna_init();
@ -321,7 +319,7 @@ void setup() {
ESP_LOGI(TAG, "Features:%s", features); ESP_LOGI(TAG, "Features:%s", features);
#ifdef HAS_LORA #ifdef HAS_LORA
// output LoRaWAN keys to console // output LoRaWAN keys to console
#ifdef VERBOSE #ifdef VERBOSE
showLoraKeys(); showLoraKeys();
#endif #endif
@ -355,7 +353,7 @@ void setup() {
&wifiSwitchTask, // task handle &wifiSwitchTask, // task handle
0); // CPU core 0); // CPU core
// initialize bme // initialize bme
#ifdef HAS_BME #ifdef HAS_BME
strcat_P(features, " BME"); strcat_P(features, " BME");
if (bme_init()) { if (bme_init()) {
@ -379,7 +377,7 @@ void setup() {
timerAlarmEnable(homeCycle); timerAlarmEnable(homeCycle);
timerAlarmEnable(channelSwitch); timerAlarmEnable(channelSwitch);
// start button interrupt // start button interrupt
#ifdef HAS_BUTTON #ifdef HAS_BUTTON
#ifdef BUTTON_PULLUP #ifdef BUTTON_PULLUP
attachInterrupt(digitalPinToInterrupt(HAS_BUTTON), ButtonIRQ, RISING); attachInterrupt(digitalPinToInterrupt(HAS_BUTTON), ButtonIRQ, RISING);

View File

@ -82,7 +82,8 @@
#define RESPONSE_TIMEOUT_MS 60000 // firmware binary server connection timeout [milliseconds] #define RESPONSE_TIMEOUT_MS 60000 // firmware binary server connection timeout [milliseconds]
// setting for syncing time of node // 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 // LMIC settings
// moved to src/lmic_config.h // moved to src/lmic_config.h

View File

@ -1,6 +1,6 @@
#ifdef HAS_RTC #ifdef HAS_RTC
#include "rtc.h" #include "rtctime.h"
// Local logging tag // Local logging tag
static const char TAG[] = "main"; static const char TAG[] = "main";
@ -15,7 +15,7 @@ int rtc_init() {
// return = 0 -> error / return = 1 -> success // return = 0 -> error / return = 1 -> success
// block i2c bus access // block i2c bus access
if (xSemaphoreTake(I2Caccess, (2 * DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) == if (xSemaphoreTake(I2Caccess, (DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) ==
pdTRUE) { pdTRUE) {
Wire.begin(HAS_RTC); Wire.begin(HAS_RTC);
@ -24,7 +24,8 @@ int rtc_init() {
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
if (!Rtc.IsDateTimeValid()) { 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.SetDateTime(compiled);
RTC_state = useless; RTC_state = useless;
} }
@ -61,59 +62,84 @@ error:
} // rtc_init() } // 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 // return = 0 -> error / return = 1 -> success
#ifdef HAS_RTC
// block i2c bus access // 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)); Rtc.SetDateTime(RtcDateTime(UTCTime));
#ifdef TIME_SYNC_INTERVAL_RTC
// restart sync.
setSyncProvider(get_rtctime);
#endif
xSemaphoreGive(I2Caccess); // release i2c bus access xSemaphoreGive(I2Caccess); // release i2c bus access
RTC_state = state; RTC_state = state;
return 1; return 1;
} // while }
return 0; return 0;
#endif } // set_rtctime()
} // set_rtc()
int set_rtc(RtcDateTime now, clock_state_t state) { int set_rtctime(RtcDateTime now, clock_state_t state) {
// return = 0 -> error / return = 1 -> success // return = 0 -> error / return = 1 -> success
#ifdef HAS_RTC
// block i2c bus access // 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); Rtc.SetDateTime(now);
#ifdef TIME_SYNC_INTERVAL_RTC
// restart sync.
setSyncProvider(get_rtctime);
#endif
xSemaphoreGive(I2Caccess); // release i2c bus access xSemaphoreGive(I2Caccess); // release i2c bus access
RTC_state = state; RTC_state = state;
return 1; return 1;
} // while }
return 0; return 0;
#endif } // set_rtctime()
} // set_rtc()
uint32_t get_rtc() { time_t get_rtctime() {
#ifdef HAS_RTC time_t rslt = now();
// block i2c bus access // block i2c bus access
while (xSemaphoreTake(I2Caccess, 2 * DISPLAYREFRESH_MS) == pdTRUE) { while (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) {
if (!Rtc.IsDateTimeValid()) { if (!Rtc.IsDateTimeValid())
ESP_LOGW(TAG, "RTC lost confidence in the DateTime"); ESP_LOGW(TAG, "RTC lost confidence in the DateTime");
return 0; else
} rslt = (time_t)(Rtc.GetDateTime()).Epoch32Time();
xSemaphoreGive(I2Caccess); // release i2c bus access xSemaphoreGive(I2Caccess); // release i2c bus access
return Rtc.GetDateTime(); return rslt;
} // while }
return 0; return rslt;
#endif
} // get_rtc() } // get_rtc()
float get_rtc_temp() { void sync_rtctime() {
#ifdef HAS_RTC 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 // block i2c bus access
while (xSemaphoreTake(I2Caccess, 2 * DISPLAYREFRESH_MS) == pdTRUE) { while (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) {
RtcTemperature temp = Rtc.GetTemperature(); RtcTemperature temp = Rtc.GetTemperature();
xSemaphoreGive(I2Caccess); // release i2c bus access xSemaphoreGive(I2Caccess); // release i2c bus access
return temp.AsFloatDegC(); return temp.AsFloatDegC();
} // while } // while
return 0; return 0;
#endif
} // get_rtc() } // get_rtc()
#endif // HAS_RTC #endif // HAS_RTC