ESPHome 2026.4.0-dev
Loading...
Searching...
No Matches
bm8563.cpp
Go to the documentation of this file.
1#include "bm8563.h"
2
3#include <cinttypes>
4
5#include "esphome/core/log.h"
6
7namespace esphome::bm8563 {
8
9static const char *const TAG = "bm8563";
10
11static constexpr uint8_t CONTROL_STATUS_1_REG = 0x00;
12static constexpr uint8_t CONTROL_STATUS_2_REG = 0x01;
13static constexpr uint8_t TIME_FIRST_REG = 0x02; // Time uses reg 2, 3, 4
14static constexpr uint8_t DATE_FIRST_REG = 0x05; // Date uses reg 5, 6, 7, 8
15static constexpr uint8_t TIMER_CONTROL_REG = 0x0E;
16static constexpr uint8_t TIMER_VALUE_REG = 0x0F;
17static constexpr uint8_t CLOCK_1_HZ = 0x82;
18static constexpr uint8_t CLOCK_1_60_HZ = 0x83;
19// Maximum duration: 255 minutes (at 1/60 Hz) = 15300 seconds
20static constexpr uint32_t MAX_TIMER_DURATION_S = 255 * 60;
21
23 if (!this->write_byte_16(CONTROL_STATUS_1_REG, 0)) {
24 this->mark_failed();
25 return;
26 }
27}
28
29void BM8563::update() { this->read_time(); }
30
32 ESP_LOGCONFIG(TAG, "BM8563:");
33 LOG_I2C_DEVICE(this);
34 if (this->is_failed()) {
35 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
36 }
37}
38
39void BM8563::start_timer(uint32_t duration_s) {
40 this->clear_irq_();
41 this->set_timer_irq_(duration_s);
42}
43
46 if (!now.is_valid()) {
47 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
48 return;
49 }
50
51 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,
53
54 this->set_time_(now);
55 this->set_date_(now);
56}
57
59 ESPTime rtc_time;
60 this->get_time_(rtc_time);
61 this->get_date_(rtc_time);
62 ESP_LOGD(TAG, "Read time: %i-%i-%i %i, %i:%i:%i", rtc_time.year, rtc_time.month, rtc_time.day_of_month,
63 rtc_time.day_of_week, rtc_time.hour, rtc_time.minute, rtc_time.second);
64
65 rtc_time.recalc_timestamp_utc(false);
66 if (!rtc_time.is_valid()) {
67 ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
68 return;
69 }
71}
72
73uint8_t BM8563::bcd2_to_byte_(uint8_t value) {
74 const uint8_t tmp = ((value & 0xF0) >> 0x4) * 10;
75 return tmp + (value & 0x0F);
76}
77
78uint8_t BM8563::byte_to_bcd2_(uint8_t value) {
79 const uint8_t bcdhigh = value / 10;
80 value -= bcdhigh * 10;
81 return (bcdhigh << 4) | value;
82}
83
84void BM8563::get_time_(ESPTime &time) {
85 uint8_t buf[3] = {0};
86 this->read_register(TIME_FIRST_REG, buf, 3);
87
88 time.second = this->bcd2_to_byte_(buf[0] & 0x7f);
89 time.minute = this->bcd2_to_byte_(buf[1] & 0x7f);
90 time.hour = this->bcd2_to_byte_(buf[2] & 0x3f);
91}
92
93void BM8563::set_time_(const ESPTime &time) {
94 uint8_t buf[3] = {this->byte_to_bcd2_(time.second), this->byte_to_bcd2_(time.minute), this->byte_to_bcd2_(time.hour)};
95 this->write_register_(TIME_FIRST_REG, buf, 3);
96}
97
98void BM8563::get_date_(ESPTime &time) {
99 uint8_t buf[4] = {0};
100 this->read_register(DATE_FIRST_REG, buf, sizeof(buf));
101
102 time.day_of_month = this->bcd2_to_byte_(buf[0] & 0x3f);
103 time.day_of_week = this->bcd2_to_byte_(buf[1] & 0x07);
104 time.month = this->bcd2_to_byte_(buf[2] & 0x1f);
105
106 uint8_t year_byte = this->bcd2_to_byte_(buf[3] & 0xff);
107
108 if (buf[2] & 0x80) {
109 time.year = 1900 + year_byte;
110 } else {
111 time.year = 2000 + year_byte;
112 }
113}
114
115void BM8563::set_date_(const ESPTime &time) {
116 uint8_t buf[4] = {
117 this->byte_to_bcd2_(time.day_of_month),
118 this->byte_to_bcd2_(time.day_of_week),
119 this->byte_to_bcd2_(time.month),
120 this->byte_to_bcd2_(time.year % 100),
121 };
122
123 if (time.year < 2000) {
124 buf[2] = buf[2] | 0x80;
125 }
126
127 this->write_register_(DATE_FIRST_REG, buf, 4);
128}
129
130void BM8563::write_byte_(uint8_t reg, uint8_t value) {
131 if (!this->write_byte(reg, value)) {
132 ESP_LOGE(TAG, "Failed to write byte 0x%02X with value 0x%02X", reg, value);
133 }
134}
135
136void BM8563::write_register_(uint8_t reg, const uint8_t *data, size_t len) {
137 if (auto error = this->write_register(reg, data, len); error != i2c::ErrorCode::NO_ERROR) {
138 ESP_LOGE(TAG, "Failed to write register 0x%02X with %zu bytes", reg, len);
139 }
140}
141
142optional<uint8_t> BM8563::read_register_(uint8_t reg) {
143 uint8_t data;
144 if (auto error = this->read_register(reg, &data, 1); error != i2c::ErrorCode::NO_ERROR) {
145 ESP_LOGE(TAG, "Failed to read register 0x%02X", reg);
146 return {};
147 }
148 return data;
149}
150
151void BM8563::set_timer_irq_(uint32_t duration_s) {
152 ESP_LOGI(TAG, "Timer Duration: %" PRIu32 " s", duration_s);
153
154 if (duration_s > MAX_TIMER_DURATION_S) {
155 ESP_LOGW(TAG, "Timer duration %" PRIu32 " s exceeds maximum %" PRIu32 " s", duration_s, MAX_TIMER_DURATION_S);
156 return;
157 }
158
159 if (duration_s > 255) {
160 uint8_t duration_minutes = duration_s / 60;
161 this->write_byte_(TIMER_VALUE_REG, duration_minutes);
162 this->write_byte_(TIMER_CONTROL_REG, CLOCK_1_60_HZ);
163 } else {
164 this->write_byte_(TIMER_VALUE_REG, duration_s);
165 this->write_byte_(TIMER_CONTROL_REG, CLOCK_1_HZ);
166 }
167
168 auto maybe_ctrl_status_2 = this->read_register_(CONTROL_STATUS_2_REG);
169 if (!maybe_ctrl_status_2.has_value()) {
170 ESP_LOGE(TAG, "Failed to read CONTROL_STATUS_2_REG");
171 return;
172 }
173 uint8_t ctrl_status_2_reg_value = maybe_ctrl_status_2.value();
174 ctrl_status_2_reg_value |= (1 << 0);
175 ctrl_status_2_reg_value &= ~(1 << 7);
176 this->write_byte_(CONTROL_STATUS_2_REG, ctrl_status_2_reg_value);
177}
178
179void BM8563::clear_irq_() {
180 auto maybe_data = this->read_register_(CONTROL_STATUS_2_REG);
181 if (!maybe_data.has_value()) {
182 ESP_LOGE(TAG, "Failed to read CONTROL_STATUS_2_REG");
183 return;
184 }
185 uint8_t data = maybe_data.value();
186 this->write_byte_(CONTROL_STATUS_2_REG, data & 0xf3);
187}
188
189void BM8563::disable_irq_() {
190 this->clear_irq_();
191 auto maybe_data = this->read_register_(CONTROL_STATUS_2_REG);
192 if (!maybe_data.has_value()) {
193 ESP_LOGE(TAG, "Failed to read CONTROL_STATUS_2_REG");
194 return;
195 }
196 uint8_t data = maybe_data.value();
197 this->write_byte_(CONTROL_STATUS_2_REG, data & 0xfc);
198}
199
200} // namespace esphome::bm8563
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:271
void update() override
Definition bm8563.cpp:29
void dump_config() override
Definition bm8563.cpp:31
void start_timer(uint32_t duration_s)
Definition bm8563.cpp:39
void setup() override
Definition bm8563.cpp:22
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:34
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:152
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:25
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.h:267
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.
@ NO_ERROR
No error found during execution of method.
Definition i2c_bus.h:13
const std::vector< uint8_t > & data
std::string size_t len
Definition helpers.h:1045
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:282
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() const
Check if this ESPTime is valid (all fields in range and year is greater than or equal to 2019)
Definition time.h:80
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