2022-02-22 13:11:46 +01:00
|
|
|
#ifdef HAS_RTC // we have hardware RTC
|
|
|
|
|
2019-01-20 22:38:53 +01:00
|
|
|
#include "rtctime.h"
|
2019-01-19 17:53:21 +01:00
|
|
|
|
|
|
|
// Local logging tag
|
2019-02-27 00:52:27 +01:00
|
|
|
static const char TAG[] = __FILE__;
|
2019-01-19 17:53:21 +01:00
|
|
|
|
2019-02-08 22:19:44 +01:00
|
|
|
RtcDS3231<TwoWire> Rtc(Wire); // RTC hardware i2c interface
|
|
|
|
|
2019-01-19 17:53:21 +01:00
|
|
|
// initialize RTC
|
2019-02-24 13:47:18 +01:00
|
|
|
uint8_t rtc_init(void) {
|
2022-03-05 13:30:34 +01:00
|
|
|
Wire.begin(HAS_RTC);
|
|
|
|
Rtc.Begin(MY_DISPLAY_SDA, MY_DISPLAY_SCL);
|
2019-01-19 17:53:21 +01:00
|
|
|
|
2022-03-05 13:30:34 +01:00
|
|
|
// configure RTC chip
|
|
|
|
Rtc.Enable32kHzPin(false);
|
|
|
|
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
|
2019-01-19 17:53:21 +01:00
|
|
|
|
2022-03-05 13:30:34 +01:00
|
|
|
if (!Rtc.GetIsRunning()) {
|
|
|
|
ESP_LOGI(TAG, "RTC not running, starting now");
|
|
|
|
Rtc.SetIsRunning(true);
|
|
|
|
}
|
2019-01-19 17:53:21 +01:00
|
|
|
|
2019-03-24 00:15:04 +01:00
|
|
|
#if (TIME_SYNC_COMPILEDATE)
|
2022-03-05 13:30:34 +01:00
|
|
|
// initialize a blank RTC without battery backup with build time
|
|
|
|
RtcDateTime tt = Rtc.GetDateTime();
|
|
|
|
time_t t = tt.Epoch32Time(); // sec2000 -> epoch
|
|
|
|
|
|
|
|
if (!Rtc.IsDateTimeValid() || !timeIsValid(t)) {
|
|
|
|
ESP_LOGW(TAG, "RTC has no recent time, setting to compiletime");
|
|
|
|
Rtc.SetDateTime(RtcDateTime(mkgmtime(compileTime()) -
|
|
|
|
SECS_YR_2000)); // epoch -> sec2000
|
|
|
|
}
|
2019-03-19 20:56:08 +01:00
|
|
|
#endif
|
2019-01-19 17:53:21 +01:00
|
|
|
|
2022-03-05 13:30:34 +01:00
|
|
|
ESP_LOGI(TAG, "RTC initialized");
|
|
|
|
return 1; // success
|
|
|
|
|
|
|
|
// failure
|
|
|
|
// return 0
|
2019-01-19 17:53:21 +01:00
|
|
|
} // rtc_init()
|
|
|
|
|
2019-07-29 14:43:37 +02:00
|
|
|
uint8_t set_rtctime(time_t t) { // t is sec epoch time
|
2019-03-19 01:46:20 +01:00
|
|
|
#ifdef RTC_INT // sync rtc 1Hz pulse on top of second
|
2022-03-05 13:30:34 +01:00
|
|
|
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone); // off
|
|
|
|
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeClock); // start
|
2019-03-19 01:46:20 +01:00
|
|
|
#endif
|
2022-03-05 13:30:34 +01:00
|
|
|
Rtc.SetDateTime(RtcDateTime(t - SECS_YR_2000)); // epoch -> sec2000
|
|
|
|
ESP_LOGI(TAG, "RTC time synced");
|
|
|
|
return 1; // success
|
2019-01-20 22:38:53 +01:00
|
|
|
} // set_rtctime()
|
2019-01-19 17:53:21 +01:00
|
|
|
|
2022-01-26 16:21:44 +01:00
|
|
|
time_t get_rtctime(uint16_t *msec) {
|
2019-02-22 22:28:35 +01:00
|
|
|
time_t t = 0;
|
2022-01-26 16:21:44 +01:00
|
|
|
*msec = 0;
|
2022-03-05 13:30:34 +01:00
|
|
|
if (Rtc.IsDateTimeValid() && Rtc.GetIsRunning()) {
|
|
|
|
RtcDateTime tt = Rtc.GetDateTime();
|
|
|
|
t = tt.Epoch32Time(); // sec2000 -> epoch
|
|
|
|
}
|
2022-03-05 18:49:50 +01:00
|
|
|
|
|
|
|
// if we have a RTC pulse, we calculate msec
|
2022-01-26 16:21:44 +01:00
|
|
|
#ifdef RTC_INT
|
2022-03-05 19:08:35 +01:00
|
|
|
uint16_t ppsDiff = millis() - lastRTCpulse;
|
|
|
|
if (ppsDiff < 1000)
|
|
|
|
*msec = ppsDiff;
|
|
|
|
else {
|
|
|
|
ESP_LOGD(TAG, "no pulse from RTC");
|
|
|
|
return 0;
|
|
|
|
}
|
2022-01-26 16:21:44 +01:00
|
|
|
#endif
|
2022-03-05 18:49:50 +01:00
|
|
|
|
2022-03-05 13:30:34 +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) {
|
2022-03-05 13:30:34 +01:00
|
|
|
RtcTemperature temp = Rtc.GetTemperature();
|
|
|
|
return temp.AsFloatDegC();
|
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
|