ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
bm8563.cpp
Go to the documentation of this file.
1#include "bm8563.h"
2
3#include <cinttypes>
4
6#include "esphome/core/log.h"
7
8namespace esphome::bm8563 {
9
10ESPHOME_LOG_TAG(TAG, "bm8563");
11
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; // Time uses reg 2, 3, 4
15static constexpr uint8_t DATE_FIRST_REG = 0x05; // Date uses reg 5, 6, 7, 8
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;
20// Maximum duration: 255 minutes (at 1/60 Hz) = 15300 seconds
21static constexpr uint32_t MAX_TIMER_DURATION_S = 255 * 60;
22
24 if (!this->write_byte_16(CONTROL_STATUS_1_REG, 0)) {
25 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
26 return;
27 }
28}
29
30void BM8563::update() { this->read_time(); }
31
33 ESP_LOGCONFIG(TAG, "BM8563:");
34 LOG_I2C_DEVICE(this);
35}
36
37void BM8563::start_timer(uint32_t duration_s) {
38 this->clear_irq_();
39 this->set_timer_irq_(duration_s);
40}
41
44 if (!now.is_valid()) {
45 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
46 return;
47 }
48
49 ESP_LOGD(TAG, "Writing time: %i-%i-%i %i, %i:%i:%i", now.year, now.month, now.day_of_month, now.day_of_week, now.hour,
51
52 this->set_time_(now);
53 this->set_date_(now);
54}
55
57 ESPTime rtc_time;
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,
61 rtc_time.day_of_week, rtc_time.hour, rtc_time.minute, rtc_time.second);
62
63 rtc_time.recalc_timestamp_utc(false);
64 if (!rtc_time.is_valid(/*check_day_of_week=*/true, /*check_day_of_year=*/false)) {
65 ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
66 return;
67 }
69}
70
71void BM8563::get_time_(ESPTime &time) {
72 uint8_t buf[3] = {0};
73 this->read_register(TIME_FIRST_REG, buf, 3);
74
75 time.second = bcd_to_uint8(buf[0] & 0x7f);
76 time.minute = bcd_to_uint8(buf[1] & 0x7f);
77 time.hour = bcd_to_uint8(buf[2] & 0x3f);
78}
79
80void BM8563::set_time_(const ESPTime &time) {
81 uint8_t buf[3] = {uint8_to_bcd(time.second), uint8_to_bcd(time.minute), uint8_to_bcd(time.hour)};
82 this->write_register_(TIME_FIRST_REG, buf, 3);
83}
84
85void BM8563::get_date_(ESPTime &time) {
86 uint8_t buf[4] = {0};
87 this->read_register(DATE_FIRST_REG, buf, sizeof(buf));
88
89 time.day_of_month = bcd_to_uint8(buf[0] & 0x3f);
90 // The chip counts weekdays 0-6 from Sunday (not BCD), ESPTime counts 1-7
91 time.day_of_week = (buf[1] & 0x07) + 1;
92 time.month = bcd_to_uint8(buf[2] & 0x1f);
93
94 uint8_t year_byte = bcd_to_uint8(buf[3] & 0xff);
95
96 if (buf[2] & 0x80) {
97 time.year = 1900 + year_byte;
98 } else {
99 time.year = 2000 + year_byte;
100 }
101}
102
103void BM8563::set_date_(const ESPTime &time) {
104 uint8_t buf[4] = {
105 uint8_to_bcd(time.day_of_month),
106 static_cast<uint8_t>(time.day_of_week - 1),
107 uint8_to_bcd(time.month),
108 uint8_to_bcd(time.year % 100),
109 };
110
111 if (time.year < 2000) {
112 buf[2] = buf[2] | 0x80;
113 }
114
115 this->write_register_(DATE_FIRST_REG, buf, 4);
116}
117
118void BM8563::write_byte_(uint8_t reg, uint8_t value) {
119 if (!this->write_byte(reg, value)) {
120 ESP_LOGE(TAG, "Failed to write byte 0x%02X with value 0x%02X", reg, value);
121 }
122}
123
124void BM8563::write_register_(uint8_t reg, const uint8_t *data, size_t len) {
125 if (auto error = this->write_register(reg, data, len); error != i2c::ErrorCode::NO_ERROR) {
126 ESP_LOGE(TAG, "Failed to write register 0x%02X with %zu bytes", reg, len);
127 }
128}
129
130optional<uint8_t> BM8563::read_register_(uint8_t reg) {
131 uint8_t data;
132 if (auto error = this->read_register(reg, &data, 1); error != i2c::ErrorCode::NO_ERROR) {
133 ESP_LOGE(TAG, "Failed to read register 0x%02X", reg);
134 return {};
135 }
136 return data;
137}
138
139void BM8563::set_timer_irq_(uint32_t duration_s) {
140 ESP_LOGI(TAG, "Timer Duration: %" PRIu32 " s", duration_s);
141
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);
144 return;
145 }
146
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);
151 } else {
152 this->write_byte_(TIMER_VALUE_REG, duration_s);
153 this->write_byte_(TIMER_CONTROL_REG, CLOCK_1_HZ);
154 }
155
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");
159 return;
160 }
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);
165}
166
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");
171 return;
172 }
173 uint8_t data = maybe_data.value();
174 this->write_byte_(CONTROL_STATUS_2_REG, data & 0xf3);
175}
176
177void BM8563::disable_irq_() {
178 this->clear_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");
182 return;
183 }
184 uint8_t data = maybe_data.value();
185 this->write_byte_(CONTROL_STATUS_2_REG, data & 0xfc);
186}
187
188} // namespace esphome::bm8563
void mark_failed()
Mark this component as failed.
void update() override
Definition bm8563.cpp:30
void dump_config() override
Definition bm8563.cpp:32
void start_timer(uint32_t duration_s)
Definition bm8563.cpp:37
void setup() override
Definition bm8563.cpp:23
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:54
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:45
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.cpp:118
ESPTime now()
Get the time in the currently defined timezone.
ESPTime utcnow()
Get the time without any time zone or DST corrections.
void synchronize_epoch_(uint32_t epoch)
Report a unix epoch as current time.
ESPHOME_LOG_TAG(TAG, "bm8563")
@ NO_ERROR
No error found during execution of method.
Definition i2c_bus.h:14
std::span< const uint8_t > data
constexpr uint8_t uint8_to_bcd(uint8_t value)
Encode a value from 0 to 99 as a two-digit packed BCD byte, e.g. 59 to 0x59.
Definition helpers.h:1146
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
constexpr uint8_t bcd_to_uint8(uint8_t bcd)
Decode a two-digit packed BCD byte (tens in the high nibble) to its value, e.g. 0x59 to 59.
Definition helpers.h:1144
static void uint32_t
A more user-friendly version of struct tm from time.h.
Definition time.h:23
uint8_t minute
minutes after the hour [0-59]
Definition time.h:32
void recalc_timestamp_utc(bool use_day_of_year=true)
Recalculate the timestamp field from the other fields of this ESPTime instance (must be UTC).
Definition time.cpp:280
uint8_t second
seconds after the minute [0-60]
Definition time.h:30
uint8_t hour
hours since midnight [0-23]
Definition time.h:34
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:48
bool is_valid(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if this ESPTime is valid (year >= 2019 and the requested fields are in range).
Definition time.h:82
uint8_t day_of_month
day of the month [1-31]
Definition time.h:38
uint16_t year
year
Definition time.h:44
uint8_t month
month; january=1 [1-12]
Definition time.h:42
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:36