12static constexpr uint8_t CONTROL_STATUS_1_REG = 0x00;
13static constexpr uint8_t CONTROL_STATUS_2_REG = 0x01;
14static constexpr uint8_t CALENDAR_REG = 0x02;
15static constexpr uint8_t CALENDAR_LEN = 7;
16static constexpr uint8_t TIMER_CONTROL_REG = 0x0E;
17static constexpr uint8_t TIMER_VALUE_REG = 0x0F;
18static constexpr uint8_t CLOCK_1_HZ = 0x82;
19static constexpr uint8_t CLOCK_1_60_HZ = 0x83;
21static constexpr uint32_t MAX_TIMER_DURATION_S = 255 * 60;
31 ESP_LOGCONFIG(TAG,
"BM8563:");
37 this->set_timer_irq_(duration_s);
43 ESP_LOGE(TAG,
"Invalid system time, not syncing to RTC.");
49 uint8_t
raw[CALENDAR_LEN];
50 time::encode_bcd_calendar<time::PCF_BCD_CALENDAR>(
now,
raw);
51 this->write_register_(CALENDAR_REG,
raw, CALENDAR_LEN);
55 uint8_t
raw[CALENDAR_LEN];
57 ESP_LOGE(TAG,
"Failed to read the time");
60 const ESPTime rtc_time = time::decode_bcd_calendar<time::PCF_BCD_CALENDAR>(
raw);
61 ESP_LOGD(TAG,
"Read time: %i-%i-%i %i, %i:%i:%i", rtc_time.
year, rtc_time.
month, rtc_time.
day_of_month,
63 if (!rtc_time.
is_valid(
true,
false)) {
64 ESP_LOGE(TAG,
"Invalid RTC time, not syncing to system clock.");
70void BM8563::write_byte_(uint8_t reg, uint8_t
value) {
72 ESP_LOGE(TAG,
"Failed to write byte 0x%02X with value 0x%02X",
reg,
value);
76void BM8563::write_register_(uint8_t reg,
const uint8_t *data,
size_t len) {
78 ESP_LOGE(TAG,
"Failed to write register 0x%02X with %zu bytes",
reg,
len);
82optional<uint8_t> BM8563::read_register_(uint8_t reg) {
85 ESP_LOGE(TAG,
"Failed to read register 0x%02X",
reg);
91void BM8563::set_timer_irq_(
uint32_t duration_s) {
92 ESP_LOGI(TAG,
"Timer Duration: %" PRIu32
" s", duration_s);
94 if (duration_s > MAX_TIMER_DURATION_S) {
95 ESP_LOGW(TAG,
"Timer duration %" PRIu32
" s exceeds maximum %" PRIu32
" s", duration_s, MAX_TIMER_DURATION_S);
99 if (duration_s > 255) {
100 uint8_t duration_minutes = duration_s / 60;
101 this->write_byte_(TIMER_VALUE_REG, duration_minutes);
102 this->write_byte_(TIMER_CONTROL_REG, CLOCK_1_60_HZ);
104 this->write_byte_(TIMER_VALUE_REG, duration_s);
105 this->write_byte_(TIMER_CONTROL_REG, CLOCK_1_HZ);
108 auto maybe_ctrl_status_2 = this->read_register_(CONTROL_STATUS_2_REG);
109 if (!maybe_ctrl_status_2.has_value()) {
110 ESP_LOGE(TAG,
"Failed to read CONTROL_STATUS_2_REG");
113 uint8_t ctrl_status_2_reg_value = maybe_ctrl_status_2.value();
114 ctrl_status_2_reg_value |= (1 << 0);
115 ctrl_status_2_reg_value &= ~(1 << 7);
116 this->write_byte_(CONTROL_STATUS_2_REG, ctrl_status_2_reg_value);
119void BM8563::clear_irq_() {
120 auto maybe_data = this->read_register_(CONTROL_STATUS_2_REG);
121 if (!maybe_data.has_value()) {
122 ESP_LOGE(TAG,
"Failed to read CONTROL_STATUS_2_REG");
125 uint8_t
data = maybe_data.value();
126 this->write_byte_(CONTROL_STATUS_2_REG, data & 0xf3);
129void BM8563::disable_irq_() {
131 auto maybe_data = this->read_register_(CONTROL_STATUS_2_REG);
132 if (!maybe_data.has_value()) {
133 ESP_LOGE(TAG,
"Failed to read CONTROL_STATUS_2_REG");
136 uint8_t
data = maybe_data.value();
137 this->write_byte_(CONTROL_STATUS_2_REG, data & 0xfc);