rtc support (experimental)
This commit is contained in:
parent
6f4bf4142b
commit
79303d6b7a
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef _RTC_H
|
||||
#define _RTC_H
|
||||
#ifndef _RTCTIME_H
|
||||
#define _RTCTIME_H
|
||||
|
||||
#include "globals.h"
|
||||
#include <Wire.h> // 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
|
||||
#endif // _RTCTIME_H
|
@ -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()
|
||||
|
||||
|
@ -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());
|
||||
|
12
src/main.cpp
12
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);
|
||||
|
@ -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
|
||||
|
@ -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
|
Loading…
Reference in New Issue
Block a user