ESP32-PaxCounter/src/rtctime.cpp

108 lines
2.4 KiB
C++
Raw Normal View History

#ifdef HAS_RTC
2019-01-20 22:38:53 +01:00
#include "rtctime.h"
// Local logging tag
static const char TAG[] = "main";
2019-01-28 00:38:31 +01:00
RtcDS3231<TwoWire> Rtc(Wire); // RTC hardware i2c interface
// initialize RTC
2019-01-21 16:16:39 +01:00
int rtc_init(void) {
// return = 0 -> error / return = 1 -> success
// block i2c bus access
2019-01-26 12:32:17 +01:00
if (I2C_MUTEX_LOCK()) {
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");
Rtc.SetDateTime(compiled);
}
if (!Rtc.GetIsRunning()) {
ESP_LOGI(TAG, "RTC not running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled) {
2019-01-28 00:38:31 +01:00
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;
}
2019-01-26 12:32:17 +01:00
I2C_MUTEX_UNLOCK(); // release i2c bus access
ESP_LOGI(TAG, "RTC initialized");
return 1;
error:
2019-01-26 12:32:17 +01:00
I2C_MUTEX_UNLOCK(); // release i2c bus access
return 0;
} // rtc_init()
2019-01-28 23:59:52 +01:00
int set_rtctime(uint32_t t) {
// return = 0 -> error / return = 1 -> success
// block i2c bus access
2019-01-26 12:32:17 +01:00
if (I2C_MUTEX_LOCK()) {
2019-01-28 23:59:52 +01:00
Rtc.SetDateTime(RtcDateTime(t));
2019-01-26 12:32:17 +01:00
I2C_MUTEX_UNLOCK(); // release i2c bus access
return 1;
2019-01-20 22:38:53 +01:00
}
return 0;
2019-01-20 22:38:53 +01:00
} // set_rtctime()
2019-01-21 16:16:39 +01:00
int set_rtctime(RtcDateTime t) {
// return = 0 -> error / return = 1 -> success
// block i2c bus access
2019-01-26 12:32:17 +01:00
if (I2C_MUTEX_LOCK()) {
2019-01-21 16:16:39 +01:00
Rtc.SetDateTime(t);
2019-01-26 12:32:17 +01:00
I2C_MUTEX_UNLOCK(); // release i2c bus access
return 1;
2019-01-20 22:38:53 +01:00
}
return 0;
2019-01-20 22:38:53 +01:00
} // set_rtctime()
2019-01-21 16:16:39 +01:00
time_t get_rtctime(void) {
2019-01-28 00:38:31 +01:00
// never call now() in this function, this would cause a recursion!
2019-01-28 23:59:52 +01:00
time_t t = 0;
// block i2c bus access
2019-01-26 12:32:17 +01:00
if (I2C_MUTEX_LOCK()) {
2019-01-28 23:59:52 +01:00
if (Rtc.IsDateTimeValid()) {
RtcDateTime tt = Rtc.GetDateTime();
t = tt.Epoch32Time();
2019-01-21 16:16:39 +01:00
} else {
2019-01-28 23:59:52 +01:00
ESP_LOGW(TAG, "RTC has no confident time");
2019-01-21 16:16:39 +01:00
}
2019-01-26 12:32:17 +01:00
I2C_MUTEX_UNLOCK(); // release i2c bus access
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-21 16:16:39 +01:00
float get_rtctemp(void) {
// block i2c bus access
2019-01-26 12:32:17 +01:00
if (I2C_MUTEX_LOCK()) {
RtcTemperature temp = Rtc.GetTemperature();
2019-01-26 12:32:17 +01:00
I2C_MUTEX_UNLOCK(); // release i2c bus access
return temp.AsFloatDegC();
} // while
return 0;
2019-01-28 23:59:52 +01:00
} // get_rtctemp()
#endif // HAS_RTC