12static constexpr uint8_t CONTROL_STATUS_1_REG = 0x00;
13static constexpr uint8_t CONTROL_STATUS_2_REG = 0x01;
14static constexpr uint8_t TIME_FIRST_REG = 0x02;
15static constexpr uint8_t DATE_FIRST_REG = 0x05;
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;
33 ESP_LOGCONFIG(TAG,
"BM8563:");
39 this->set_timer_irq_(duration_s);
45 ESP_LOGE(TAG,
"Invalid system time, not syncing to RTC.");
58 this->get_time_(rtc_time);
59 this->get_date_(rtc_time);
60 ESP_LOGD(TAG,
"Read time: %i-%i-%i %i, %i:%i:%i", rtc_time.
year, rtc_time.
month, rtc_time.
day_of_month,
64 if (!rtc_time.
is_valid(
true,
false)) {
65 ESP_LOGE(TAG,
"Invalid RTC time, not syncing to system clock.");
71void BM8563::get_time_(
ESPTime &time) {
80void BM8563::set_time_(
const ESPTime &time) {
82 this->write_register_(TIME_FIRST_REG, buf, 3);
85void BM8563::get_date_(ESPTime &time) {
91 time.day_of_week = (buf[1] & 0x07) + 1;
97 time.year = 1900 + year_byte;
99 time.year = 2000 + year_byte;
103void BM8563::set_date_(
const ESPTime &time) {
106 static_cast<uint8_t
>(time.day_of_week - 1),
111 if (time.year < 2000) {
112 buf[2] = buf[2] | 0x80;
115 this->write_register_(DATE_FIRST_REG, buf, 4);
118void BM8563::write_byte_(uint8_t reg, uint8_t
value) {
120 ESP_LOGE(TAG,
"Failed to write byte 0x%02X with value 0x%02X",
reg,
value);
124void BM8563::write_register_(uint8_t reg,
const uint8_t *data,
size_t len) {
126 ESP_LOGE(TAG,
"Failed to write register 0x%02X with %zu bytes",
reg,
len);
130optional<uint8_t> BM8563::read_register_(uint8_t reg) {
133 ESP_LOGE(TAG,
"Failed to read register 0x%02X",
reg);
139void BM8563::set_timer_irq_(
uint32_t duration_s) {
140 ESP_LOGI(TAG,
"Timer Duration: %" PRIu32
" s", duration_s);
142 if (duration_s > MAX_TIMER_DURATION_S) {
143 ESP_LOGW(TAG,
"Timer duration %" PRIu32
" s exceeds maximum %" PRIu32
" s", duration_s, MAX_TIMER_DURATION_S);
147 if (duration_s > 255) {
148 uint8_t duration_minutes = duration_s / 60;
149 this->write_byte_(TIMER_VALUE_REG, duration_minutes);
150 this->write_byte_(TIMER_CONTROL_REG, CLOCK_1_60_HZ);
152 this->write_byte_(TIMER_VALUE_REG, duration_s);
153 this->write_byte_(TIMER_CONTROL_REG, CLOCK_1_HZ);
156 auto maybe_ctrl_status_2 = this->read_register_(CONTROL_STATUS_2_REG);
157 if (!maybe_ctrl_status_2.has_value()) {
158 ESP_LOGE(TAG,
"Failed to read CONTROL_STATUS_2_REG");
161 uint8_t ctrl_status_2_reg_value = maybe_ctrl_status_2.value();
162 ctrl_status_2_reg_value |= (1 << 0);
163 ctrl_status_2_reg_value &= ~(1 << 7);
164 this->write_byte_(CONTROL_STATUS_2_REG, ctrl_status_2_reg_value);
167void BM8563::clear_irq_() {
168 auto maybe_data = this->read_register_(CONTROL_STATUS_2_REG);
169 if (!maybe_data.has_value()) {
170 ESP_LOGE(TAG,
"Failed to read CONTROL_STATUS_2_REG");
173 uint8_t
data = maybe_data.value();
174 this->write_byte_(CONTROL_STATUS_2_REG, data & 0xf3);
177void BM8563::disable_irq_() {
179 auto maybe_data = this->read_register_(CONTROL_STATUS_2_REG);
180 if (!maybe_data.has_value()) {
181 ESP_LOGE(TAG,
"Failed to read CONTROL_STATUS_2_REG");
184 uint8_t
data = maybe_data.value();
185 this->write_byte_(CONTROL_STATUS_2_REG, data & 0xfc);