6static const char *
const TAG =
"bm8563";
8static constexpr uint8_t CONTROL_STATUS_1_REG = 0x00;
9static constexpr uint8_t CONTROL_STATUS_2_REG = 0x01;
10static constexpr uint8_t TIME_FIRST_REG = 0x02;
11static constexpr uint8_t DATE_FIRST_REG = 0x05;
12static constexpr uint8_t TIMER_CONTROL_REG = 0x0E;
13static constexpr uint8_t TIMER_VALUE_REG = 0x0F;
14static constexpr uint8_t CLOCK_1_HZ = 0x82;
15static constexpr uint8_t CLOCK_1_60_HZ = 0x83;
17static constexpr uint32_t MAX_TIMER_DURATION_S = 255 * 60;
29 ESP_LOGCONFIG(TAG,
"BM8563:");
32 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
38 this->set_timer_irq_(duration_s);
44 ESP_LOGE(TAG,
"Invalid system time, not syncing to RTC.");
57 this->get_time_(rtc_time);
58 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,
65 ESP_LOGE(TAG,
"Invalid RTC time, not syncing to system clock.");
71uint8_t BM8563::bcd2_to_byte_(uint8_t value) {
72 const uint8_t tmp = ((value & 0xF0) >> 0x4) * 10;
73 return tmp + (value & 0x0F);
76uint8_t BM8563::byte_to_bcd2_(uint8_t value) {
77 const uint8_t bcdhigh = value / 10;
78 value -= bcdhigh * 10;
79 return (bcdhigh << 4) | value;
82void BM8563::get_time_(ESPTime &time) {
86 time.second = this->bcd2_to_byte_(buf[0] & 0x7f);
87 time.minute = this->bcd2_to_byte_(buf[1] & 0x7f);
88 time.hour = this->bcd2_to_byte_(buf[2] & 0x3f);
91void BM8563::set_time_(
const ESPTime &time) {
92 uint8_t buf[3] = {this->byte_to_bcd2_(time.second), this->byte_to_bcd2_(time.minute), this->byte_to_bcd2_(time.hour)};
93 this->write_register_(TIME_FIRST_REG, buf, 3);
96void BM8563::get_date_(ESPTime &time) {
100 time.day_of_month = this->bcd2_to_byte_(buf[0] & 0x3f);
101 time.day_of_week = this->bcd2_to_byte_(buf[1] & 0x07);
102 time.month = this->bcd2_to_byte_(buf[2] & 0x1f);
104 uint8_t year_byte = this->bcd2_to_byte_(buf[3] & 0xff);
107 time.year = 1900 + year_byte;
109 time.year = 2000 + year_byte;
113void BM8563::set_date_(
const ESPTime &time) {
115 this->byte_to_bcd2_(time.day_of_month),
116 this->byte_to_bcd2_(time.day_of_week),
117 this->byte_to_bcd2_(time.month),
118 this->byte_to_bcd2_(time.year % 100),
121 if (time.year < 2000) {
122 buf[2] = buf[2] | 0x80;
125 this->write_register_(DATE_FIRST_REG, buf, 4);
128void BM8563::write_byte_(uint8_t reg, uint8_t value) {
130 ESP_LOGE(TAG,
"Failed to write byte 0x%02X with value 0x%02X",
reg, value);
134void BM8563::write_register_(uint8_t reg,
const uint8_t *data,
size_t len) {
136 ESP_LOGE(TAG,
"Failed to write register 0x%02X with %zu bytes",
reg,
len);
140optional<uint8_t> BM8563::read_register_(uint8_t reg) {
143 ESP_LOGE(TAG,
"Failed to read register 0x%02X",
reg);
149void BM8563::set_timer_irq_(uint32_t duration_s) {
150 ESP_LOGI(TAG,
"Timer Duration: %u s", duration_s);
152 if (duration_s > MAX_TIMER_DURATION_S) {
153 ESP_LOGW(TAG,
"Timer duration %u s exceeds maximum %u s", duration_s, MAX_TIMER_DURATION_S);
157 if (duration_s > 255) {
158 uint8_t duration_minutes = duration_s / 60;
159 this->write_byte_(TIMER_VALUE_REG, duration_minutes);
160 this->write_byte_(TIMER_CONTROL_REG, CLOCK_1_60_HZ);
162 this->write_byte_(TIMER_VALUE_REG, duration_s);
163 this->write_byte_(TIMER_CONTROL_REG, CLOCK_1_HZ);
166 auto maybe_ctrl_status_2 = this->read_register_(CONTROL_STATUS_2_REG);
167 if (!maybe_ctrl_status_2.has_value()) {
168 ESP_LOGE(TAG,
"Failed to read CONTROL_STATUS_2_REG");
171 uint8_t ctrl_status_2_reg_value = maybe_ctrl_status_2.value();
172 ctrl_status_2_reg_value |= (1 << 0);
173 ctrl_status_2_reg_value &= ~(1 << 7);
174 this->write_byte_(CONTROL_STATUS_2_REG, ctrl_status_2_reg_value);
177void BM8563::clear_irq_() {
178 auto maybe_data = this->read_register_(CONTROL_STATUS_2_REG);
179 if (!maybe_data.has_value()) {
180 ESP_LOGE(TAG,
"Failed to read CONTROL_STATUS_2_REG");
183 uint8_t
data = maybe_data.value();
184 this->write_byte_(CONTROL_STATUS_2_REG,
data & 0xf3);
187void BM8563::disable_irq_() {
189 auto maybe_data = this->read_register_(CONTROL_STATUS_2_REG);
190 if (!maybe_data.has_value()) {
191 ESP_LOGE(TAG,
"Failed to read CONTROL_STATUS_2_REG");
194 uint8_t
data = maybe_data.value();
195 this->write_byte_(CONTROL_STATUS_2_REG,
data & 0xfc);