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";
|
|
|
|
|
2019-02-08 22:19:44 +01:00
|
|
|
#ifdef HAS_RTC // we have hardware RTC
|
|
|
|
|
|
|
|
RtcDS3231<TwoWire> Rtc(Wire); // RTC hardware i2c interface
|
|
|
|
|
2019-01-19 17:53:21 +01:00
|
|
|
// initialize RTC
|
2019-01-21 16:16:39 +01:00
|
|
|
int rtc_init(void) {
|
2019-01-19 17:53:21 +01:00
|
|
|
|
2019-02-22 22:28:35 +01:00
|
|
|
if (I2C_MUTEX_LOCK()) { // block i2c bus access
|
2019-01-19 17:53:21 +01:00
|
|
|
|
|
|
|
Wire.begin(HAS_RTC);
|
|
|
|
Rtc.Begin();
|
|
|
|
|
2019-02-22 22:28:35 +01:00
|
|
|
// configure RTC chip
|
|
|
|
Rtc.Enable32kHzPin(false);
|
|
|
|
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
|
2019-01-19 17:53:21 +01:00
|
|
|
|
|
|
|
if (!Rtc.GetIsRunning()) {
|
|
|
|
ESP_LOGI(TAG, "RTC not running, starting now");
|
|
|
|
Rtc.SetIsRunning(true);
|
|
|
|
}
|
|
|
|
|
2019-02-22 22:28:35 +01:00
|
|
|
RtcDateTime tt = Rtc.GetDateTime();
|
|
|
|
time_t t = tt.Epoch32Time(); // sec2000 -> epoch
|
2019-01-19 17:53:21 +01:00
|
|
|
|
2019-02-22 22:28:35 +01:00
|
|
|
if (!Rtc.IsDateTimeValid() || !TimeIsValid(t)) {
|
|
|
|
ESP_LOGW(TAG, "RTC has no recent time, setting to compilation date");
|
|
|
|
Rtc.SetDateTime(
|
|
|
|
RtcDateTime(compiledUTC() - SECS_YR_2000)); // epoch -> sec2000
|
2019-01-19 17:53:21 +01:00
|
|
|
}
|
|
|
|
|
2019-02-22 22:28:35 +01:00
|
|
|
I2C_MUTEX_UNLOCK(); // release i2c bus access
|
|
|
|
ESP_LOGI(TAG, "RTC initialized");
|
|
|
|
return 1; // success
|
2019-01-19 17:53:21 +01:00
|
|
|
} else {
|
2019-02-22 22:28:35 +01:00
|
|
|
ESP_LOGE(TAG, "RTC initialization error, I2C bus busy");
|
|
|
|
return 0; // failure
|
2019-01-19 17:53:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // rtc_init()
|
|
|
|
|
2019-02-22 22:28:35 +01:00
|
|
|
int set_rtctime(time_t t) { // t is UTC in seconds epoch time
|
2019-01-26 12:32:17 +01:00
|
|
|
if (I2C_MUTEX_LOCK()) {
|
2019-02-22 22:28:35 +01:00
|
|
|
Rtc.SetDateTime(RtcDateTime(t - SECS_YR_2000)); // epoch -> sec2000
|
|
|
|
I2C_MUTEX_UNLOCK();
|
2019-02-21 23:17:01 +01:00
|
|
|
ESP_LOGI(TAG, "RTC time synced");
|
2019-02-11 23:37:45 +01:00
|
|
|
return 1; // success
|
2019-02-22 22:28:35 +01:00
|
|
|
} else {
|
|
|
|
ESP_LOGE(TAG, "RTC set time failure");
|
|
|
|
return 0;
|
|
|
|
} // failure
|
2019-01-20 22:38:53 +01:00
|
|
|
} // set_rtctime()
|
2019-01-19 17:53:21 +01:00
|
|
|
|
2019-02-22 22:28:35 +01:00
|
|
|
int set_rtctime(uint32_t t) { // t is UTC in seconds epoch time
|
2019-02-02 09:15:31 +01:00
|
|
|
return set_rtctime(static_cast<time_t>(t));
|
|
|
|
// set_rtctime()
|
|
|
|
}
|
2019-01-29 22:54:16 +01:00
|
|
|
|
2019-01-21 16:16:39 +01:00
|
|
|
time_t get_rtctime(void) {
|
2019-02-16 15:02:07 +01:00
|
|
|
// !! never call now() or delay in this function, this would break this
|
|
|
|
// function to be used as SyncProvider for Time.h
|
2019-02-22 22:28:35 +01:00
|
|
|
time_t t = 0;
|
2019-01-26 12:32:17 +01:00
|
|
|
if (I2C_MUTEX_LOCK()) {
|
2019-02-21 23:17:01 +01:00
|
|
|
if (Rtc.IsDateTimeValid() && Rtc.GetIsRunning()) {
|
2019-01-28 23:59:52 +01:00
|
|
|
RtcDateTime tt = Rtc.GetDateTime();
|
2019-02-22 22:28:35 +01:00
|
|
|
t = tt.Epoch32Time(); // sec2000 -> epoch
|
2019-01-21 16:16:39 +01:00
|
|
|
}
|
2019-02-22 22:28:35 +01:00
|
|
|
I2C_MUTEX_UNLOCK();
|
2019-01-20 22:38:53 +01:00
|
|
|
}
|
2019-01-28 23:59:52 +01:00
|
|
|
return t;
|
2019-01-21 16:16:39 +01:00
|
|
|
} // get_rtctime()
|
2019-01-19 17:53:21 +01:00
|
|
|
|
2019-01-21 16:16:39 +01:00
|
|
|
float get_rtctemp(void) {
|
2019-01-26 12:32:17 +01:00
|
|
|
if (I2C_MUTEX_LOCK()) {
|
2019-01-19 17:53:21 +01:00
|
|
|
RtcTemperature temp = Rtc.GetTemperature();
|
2019-02-22 22:28:35 +01:00
|
|
|
I2C_MUTEX_UNLOCK();
|
2019-01-19 17:53:21 +01:00
|
|
|
return temp.AsFloatDegC();
|
2019-02-22 22:28:35 +01:00
|
|
|
}
|
2019-01-19 17:53:21 +01:00
|
|
|
return 0;
|
2019-01-28 23:59:52 +01:00
|
|
|
} // get_rtctemp()
|
2019-01-19 17:53:21 +01:00
|
|
|
|
2019-02-14 23:01:20 +01:00
|
|
|
#endif // HAS_RTC
|