added RTC timesync during startup
This commit is contained in:
parent
ac28348acf
commit
bde7fb2bcf
@ -5,9 +5,17 @@
|
||||
#include <Wire.h> // must be included here so that Arduino library object file references work
|
||||
#include <RtcDS3231.h>
|
||||
|
||||
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;
|
||||
|
||||
int rtc_init(void);
|
||||
int set_rtc(uint32_t UTCTime);
|
||||
int set_rtc(RtcDateTime now);
|
||||
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();
|
||||
|
||||
|
@ -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);
|
||||
set_rtc(now, synced_GPS);
|
||||
#endif
|
||||
ESP_LOGI(TAG, "Time synced by GPS to %02d:%02d:%02d", hour(), minute(),
|
||||
second());
|
||||
@ -153,6 +153,13 @@ 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);
|
||||
set_rtc(*pUserUTCTime, synced_LORA);
|
||||
#endif
|
||||
ESP_LOGI(TAG, "Time synced by LoRa network to %02d:%02d:%02d", hour(),
|
||||
minute(), second());
|
||||
|
@ -175,6 +175,9 @@ 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());
|
||||
#endif
|
||||
|
||||
// initialize wifi antenna
|
||||
|
11
src/rtc.cpp
11
src/rtc.cpp
@ -7,6 +7,8 @@ static const char TAG[] = "main";
|
||||
|
||||
RtcDS3231<TwoWire> Rtc(Wire);
|
||||
|
||||
clock_state_t RTC_state = useless;
|
||||
|
||||
// initialize RTC
|
||||
int rtc_init() {
|
||||
|
||||
@ -24,6 +26,7 @@ int rtc_init() {
|
||||
if (!Rtc.IsDateTimeValid()) {
|
||||
ESP_LOGW(TAG, "RTC has no valid RTC date/time, setting to compilation date");
|
||||
Rtc.SetDateTime(compiled);
|
||||
RTC_state = useless;
|
||||
}
|
||||
|
||||
if (!Rtc.GetIsRunning()) {
|
||||
@ -32,10 +35,12 @@ 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
|
||||
@ -56,26 +61,28 @@ error:
|
||||
|
||||
} // rtc_init()
|
||||
|
||||
int set_rtc(uint32_t UTCTime) {
|
||||
int set_rtc(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) {
|
||||
Rtc.SetDateTime(RtcDateTime(UTCTime));
|
||||
xSemaphoreGive(I2Caccess); // release i2c bus access
|
||||
RTC_state = state;
|
||||
return 1;
|
||||
} // while
|
||||
return 0;
|
||||
#endif
|
||||
} // set_rtc()
|
||||
|
||||
int set_rtc(RtcDateTime now) {
|
||||
int set_rtc(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) {
|
||||
Rtc.SetDateTime(now);
|
||||
xSemaphoreGive(I2Caccess); // release i2c bus access
|
||||
RTC_state = state;
|
||||
return 1;
|
||||
} // while
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user