2019-01-19 17:53:21 +01:00
|
|
|
#ifdef HAS_RTC
|
|
|
|
|
2019-01-20 22:38:53 +01:00
|
|
|
#include "rtctime.h"
|
2019-01-19 17:53:21 +01:00
|
|
|
|
|
|
|
// Local logging tag
|
|
|
|
static const char TAG[] = "main";
|
|
|
|
|
|
|
|
RtcDS3231<TwoWire> Rtc(Wire);
|
|
|
|
|
|
|
|
// initialize RTC
|
2019-01-21 16:16:39 +01:00
|
|
|
int rtc_init(void) {
|
2019-01-19 17:53:21 +01:00
|
|
|
|
|
|
|
// return = 0 -> error / return = 1 -> success
|
|
|
|
|
|
|
|
// block i2c bus access
|
2019-01-20 22:38:53 +01:00
|
|
|
if (xSemaphoreTake(I2Caccess, (DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) ==
|
2019-01-19 17:53:21 +01:00
|
|
|
pdTRUE) {
|
|
|
|
|
|
|
|
Wire.begin(HAS_RTC);
|
|
|
|
Rtc.Begin();
|
|
|
|
|
|
|
|
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
|
|
|
|
|
|
|
|
if (!Rtc.IsDateTimeValid()) {
|
2019-01-20 22:38:53 +01:00
|
|
|
ESP_LOGW(TAG,
|
|
|
|
"RTC has no valid RTC date/time, setting to compilation date");
|
2019-01-19 17:53:21 +01:00
|
|
|
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()
|
|
|
|
|
2019-01-21 16:16:39 +01:00
|
|
|
int set_rtctime(uint32_t UTCTime) {
|
2019-01-19 17:53:21 +01:00
|
|
|
// return = 0 -> error / return = 1 -> success
|
|
|
|
// block i2c bus access
|
2019-01-20 22:38:53 +01:00
|
|
|
while (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) {
|
2019-01-19 17:53:21 +01:00
|
|
|
Rtc.SetDateTime(RtcDateTime(UTCTime));
|
|
|
|
xSemaphoreGive(I2Caccess); // release i2c bus access
|
|
|
|
return 1;
|
2019-01-20 22:38:53 +01:00
|
|
|
}
|
2019-01-19 17:53:21 +01:00
|
|
|
return 0;
|
2019-01-20 22:38:53 +01:00
|
|
|
} // set_rtctime()
|
2019-01-19 17:53:21 +01:00
|
|
|
|
2019-01-21 16:16:39 +01:00
|
|
|
int set_rtctime(RtcDateTime t) {
|
2019-01-19 17:53:21 +01:00
|
|
|
// return = 0 -> error / return = 1 -> success
|
|
|
|
// block i2c bus access
|
2019-01-20 22:38:53 +01:00
|
|
|
while (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) {
|
2019-01-21 16:16:39 +01:00
|
|
|
Rtc.SetDateTime(t);
|
2019-01-19 17:53:21 +01:00
|
|
|
xSemaphoreGive(I2Caccess); // release i2c bus access
|
|
|
|
return 1;
|
2019-01-20 22:38:53 +01:00
|
|
|
}
|
2019-01-19 17:53:21 +01:00
|
|
|
return 0;
|
2019-01-20 22:38:53 +01:00
|
|
|
} // set_rtctime()
|
2019-01-19 17:53:21 +01:00
|
|
|
|
2019-01-21 16:16:39 +01:00
|
|
|
time_t get_rtctime(void) {
|
|
|
|
// never call now() in this function, would cause recursion!
|
|
|
|
time_t tt = 0;
|
2019-01-19 17:53:21 +01:00
|
|
|
// block i2c bus access
|
2019-01-21 16:16:39 +01:00
|
|
|
if (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) {
|
|
|
|
if (!Rtc.IsDateTimeValid()) {
|
2019-01-19 17:53:21 +01:00
|
|
|
ESP_LOGW(TAG, "RTC lost confidence in the DateTime");
|
2019-01-21 16:16:39 +01:00
|
|
|
} else {
|
|
|
|
RtcDateTime t = Rtc.GetDateTime();
|
|
|
|
tt = t.Epoch32Time();
|
|
|
|
}
|
2019-01-19 17:53:21 +01:00
|
|
|
xSemaphoreGive(I2Caccess); // release i2c bus access
|
2019-01-21 16:16:39 +01:00
|
|
|
return tt;
|
2019-01-20 22:38:53 +01:00
|
|
|
}
|
2019-01-21 16:16:39 +01:00
|
|
|
return tt;
|
|
|
|
} // get_rtctime()
|
2019-01-19 17:53:21 +01:00
|
|
|
|
2019-01-21 16:16:39 +01:00
|
|
|
void sync_rtctime(void) {
|
2019-01-20 22:38:53 +01:00
|
|
|
#ifdef TIME_SYNC_INTERVAL_RTC
|
2019-01-21 16:16:39 +01:00
|
|
|
setSyncProvider(&get_rtctime);
|
|
|
|
setSyncInterval(TIME_SYNC_INTERVAL_RTC);
|
2019-01-20 22:38:53 +01:00
|
|
|
if (timeStatus() != timeSet) {
|
|
|
|
ESP_LOGE(TAG, "Unable to sync with the RTC");
|
|
|
|
} else {
|
2019-01-21 16:16:39 +01:00
|
|
|
time_t t = now();
|
|
|
|
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));
|
2019-01-20 22:38:53 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} // sync_rtctime;
|
|
|
|
|
2019-01-21 16:16:39 +01:00
|
|
|
float get_rtctemp(void) {
|
2019-01-19 17:53:21 +01:00
|
|
|
// block i2c bus access
|
2019-01-20 22:38:53 +01:00
|
|
|
while (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) {
|
2019-01-19 17:53:21 +01:00
|
|
|
RtcTemperature temp = Rtc.GetTemperature();
|
|
|
|
xSemaphoreGive(I2Caccess); // release i2c bus access
|
|
|
|
return temp.AsFloatDegC();
|
|
|
|
} // while
|
|
|
|
return 0;
|
|
|
|
} // get_rtc()
|
|
|
|
|
2019-01-21 16:16:39 +01:00
|
|
|
time_t gpsTimeSync(void) {
|
|
|
|
#ifdef HAS_GPS
|
|
|
|
tmElements_t tm;
|
|
|
|
tm.Second = gps.time.second();
|
|
|
|
tm.Minute = gps.time.minute();
|
|
|
|
tm.Hour = gps.time.hour();
|
|
|
|
tm.Day = gps.date.day();
|
|
|
|
tm.Month = gps.date.month();
|
|
|
|
tm.Year = CalendarYrToTm(gps.date.year());
|
|
|
|
return makeTime(tm);
|
|
|
|
#endif // HAS_GPS
|
|
|
|
}
|
|
|
|
|
2019-01-19 17:53:21 +01:00
|
|
|
#endif // HAS_RTC
|