Feature RTC support added (experimental)
This commit is contained in:
1 parent
1efaf9a04e
commit
a3414c5622
7 files changed
+159
-2
No files matched your search
@@ -128,6 +128,16 @@ void do_timesync() {
|
||||
if (gps.time.isValid()) {
|
||||
setTime(gps.time.hour(), gps.time.minute(), gps.time.second(),
|
||||
gps.date.day(), gps.date.month(), gps.date.year());
|
||||
#ifdef HAS_RTC
|
||||
RtcDateTime now;
|
||||
now.year = gps.time.year();
|
||||
now.month = gps.date.month();
|
||||
now.dayOfMonth = gps.date.day();
|
||||
now.hour = gps.time.hour();
|
||||
now.minute = gps.time.minute();
|
||||
now.second = gps.time.second();
|
||||
set_rtc(now);
|
||||
#endif
|
||||
ESP_LOGI(TAG, "Time synced by GPS to %02d:%02d:%02d", hour(), minute(),
|
||||
second());
|
||||
return;
|
||||
|
||||
+5
-2
@@ -455,6 +455,9 @@ void user_request_network_time_callback(void *pVoidUserUTCTime,
|
||||
|
||||
// Update system time with time read from the network
|
||||
setTime(*pUserUTCTime);
|
||||
ESP_LOGI(TAG, "Time synced by LoRa network to %02d:%02d:%02d", hour(),
|
||||
minute(), second());
|
||||
#ifdef HAS_RTC
|
||||
set_rtc(*pUserUTCTime);
|
||||
#endif
|
||||
ESP_LOGI(TAG, "Time synced by LoRa network to %02d:%02d:%02d", hour(),
|
||||
minute(), second());
|
||||
}
|
||||
@@ -171,6 +171,12 @@ void setup() {
|
||||
0); // CPU core
|
||||
#endif
|
||||
|
||||
// initialize RTC
|
||||
#ifdef HAS_RTC
|
||||
strcat_P(features, " RTC");
|
||||
assert(rtc_init());
|
||||
#endif
|
||||
|
||||
// initialize wifi antenna
|
||||
#ifdef HAS_ANTENNA_SWITCH
|
||||
strcat_P(features, " ANT");
|
||||
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
#ifdef HAS_RTC
|
||||
|
||||
#include "rtc.h"
|
||||
|
||||
// Local logging tag
|
||||
static const char TAG[] = "main";
|
||||
|
||||
RtcDS3231<TwoWire> Rtc(Wire);
|
||||
|
||||
// initialize RTC
|
||||
int rtc_init() {
|
||||
|
||||
// return = 0 -> error / return = 1 -> success
|
||||
|
||||
// block i2c bus access
|
||||
if (xSemaphoreTake(I2Caccess, (2 * DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) ==
|
||||
pdTRUE) {
|
||||
|
||||
Wire.begin(HAS_RTC);
|
||||
Rtc.Begin();
|
||||
|
||||
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
|
||||
|
||||
if (!Rtc.IsDateTimeValid()) {
|
||||
ESP_LOGW(TAG, "RTC has no valid RTC date/time, setting to compilation date");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
if (!Rtc.GetIsRunning()) {
|
||||
ESP_LOGI(TAG, "RTC not running, starting now");
|
||||
Rtc.SetIsRunning(true);
|
||||
}
|
||||
|
||||
RtcDateTime now = Rtc.GetDateTime();
|
||||
|
||||
if (now < compiled) {
|
||||
ESP_LOGI(TAG, "RTC date/time is older than compilation date, updating)");
|
||||
Rtc.SetDateTime(compiled);
|
||||
}
|
||||
|
||||
// configure RTC chip
|
||||
Rtc.Enable32kHzPin(false);
|
||||
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "I2c bus busy - RTC initialization error");
|
||||
goto error;
|
||||
}
|
||||
|
||||
xSemaphoreGive(I2Caccess); // release i2c bus access
|
||||
ESP_LOGI(TAG, "RTC initialized");
|
||||
return 1;
|
||||
|
||||
error:
|
||||
xSemaphoreGive(I2Caccess); // release i2c bus access
|
||||
return 0;
|
||||
|
||||
} // rtc_init()
|
||||
|
||||
int set_rtc(uint32_t UTCTime) {
|
||||
// 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
|
||||
return 1;
|
||||
} // while
|
||||
return 0;
|
||||
#endif
|
||||
} // set_rtc()
|
||||
|
||||
int set_rtc(RtcDateTime now) {
|
||||
// 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
|
||||
return 1;
|
||||
} // while
|
||||
return 0;
|
||||
#endif
|
||||
} // set_rtc()
|
||||
|
||||
uint32_t get_rtc() {
|
||||
#ifdef HAS_RTC
|
||||
// block i2c bus access
|
||||
while (xSemaphoreTake(I2Caccess, 2 * DISPLAYREFRESH_MS) == pdTRUE) {
|
||||
if (!Rtc.IsDateTimeValid()) {
|
||||
ESP_LOGW(TAG, "RTC lost confidence in the DateTime");
|
||||
return 0;
|
||||
}
|
||||
xSemaphoreGive(I2Caccess); // release i2c bus access
|
||||
return Rtc.GetDateTime();
|
||||
} // while
|
||||
return 0;
|
||||
#endif
|
||||
} // get_rtc()
|
||||
|
||||
float get_rtc_temp() {
|
||||
#ifdef HAS_RTC
|
||||
// block i2c bus access
|
||||
while (xSemaphoreTake(I2Caccess, 2 * 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
|
||||
Reference in new issue
Block a user