37 if (
flags & (RX8111_FLAG_VLF | RX8111_FLAG_XST | RX8111_FLAG_POR)) {
38 ESP_LOGW(TAG,
"RTC time invalidated by power loss or oscillator stop; write trusted time");
45 if (!this->
read_bytes(RX8111_REG_SEC, buf,
sizeof(buf))) {
49 static constexpr uint8_t CALENDAR_MASKS[7] = {0x7F, 0x7F, 0x3F, 0x00, 0x3F, 0x1F, 0xFF};
50 for (
size_t i = 0; i <
sizeof(buf); i++) {
53 buf[i] &= CALENDAR_MASKS[i];
54 if ((buf[i] & 0x0F) > 9 || (buf[i] >> 4) > 9) {
55 ESP_LOGW(TAG,
"Invalid BCD in RTC time");
60 const uint8_t onehot = buf[3];
61 if (onehot == 0 || onehot > 0x40 || (onehot & (onehot - 1)) != 0) {
62 ESP_LOGW(TAG,
"Invalid RTC weekday");
66 const uint8_t weekday =
static_cast<uint8_t
>(__builtin_ctz(onehot) + 1);
68 .second = bcd_to_uint8(buf[0]),
69 .minute = bcd_to_uint8(buf[1]),
70 .hour = bcd_to_uint8(buf[2]),
71 .day_of_week = weekday,
72 .day_of_month = bcd_to_uint8(buf[4]),
73 .month = bcd_to_uint8(buf[5]),
74 .year =
static_cast<uint16_t
>(bcd_to_uint8(buf[6]) + 2000),
79 if (!rtc_time.is_valid(
true,
false) || rtc_time.second >= 60) {
80 ESP_LOGW(TAG,
"Invalid RTC time, not syncing to system clock");
85 ESP_LOGW(TAG,
"RTC weekday does not match calendar date");
91 ESP_LOGD(TAG,
"Read UTC time: %04d-%02d-%02d %02d:%02d:%02d", rtc_time.year, rtc_time.month, rtc_time.day_of_month,
92 rtc_time.hour, rtc_time.minute, rtc_time.second);