ESPHome 2026.12.0-dev
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 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;
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->setup_check(this->write_byte_16(CONTROL_STATUS_1_REG, 0)))
25 return;
26}
27
28void BM8563::update() { this->read_time(); }
29
31 ESP_LOGCONFIG(TAG, "BM8563:");
32 LOG_I2C_DEVICE(this);
33}
34
35void BM8563::start_timer(uint32_t duration_s) {
36 this->clear_irq_();
37 this->set_timer_irq_(duration_s);
38}
39
42 if (!now.is_valid()) {
43 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
44 return;
45 }
46
47 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,
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);
52}
53
55 uint8_t raw[CALENDAR_LEN];
56 if (this->read_register(CALENDAR_REG, raw, CALENDAR_LEN) != i2c::ERROR_OK) {
57 ESP_LOGE(TAG, "Failed to read the time");
58 return;
59 }
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,
62 rtc_time.day_of_week, rtc_time.hour, rtc_time.minute, rtc_time.second);
63 if (!rtc_time.is_valid(/*check_day_of_week=*/true, /*check_day_of_year=*/false)) {
64 ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
65 return;
66 }
68}
69
70void BM8563::write_byte_(uint8_t reg, uint8_t value) {
71 if (!this->write_byte(reg, value)) {
72 ESP_LOGE(TAG, "Failed to write byte 0x%02X with value 0x%02X", reg, value);
73 }
74}
75
76void BM8563::write_register_(uint8_t reg, const uint8_t *data, size_t len) {
77 if (auto error = this->write_register(reg, data, len); error != i2c::ErrorCode::NO_ERROR) {
78 ESP_LOGE(TAG, "Failed to write register 0x%02X with %zu bytes", reg, len);
79 }
80}
81
82optional<uint8_t> BM8563::read_register_(uint8_t reg) {
83 uint8_t data;
84 if (auto error = this->read_register(reg, &data, 1); error != i2c::ErrorCode::NO_ERROR) {
85 ESP_LOGE(TAG, "Failed to read register 0x%02X", reg);
86 return {};
87 }
88 return data;
89}
90
91void BM8563::set_timer_irq_(uint32_t duration_s) {
92 ESP_LOGI(TAG, "Timer Duration: %" PRIu32 " s", duration_s);
93
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);
96 return;
97 }
98
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);
103 } else {
104 this->write_byte_(TIMER_VALUE_REG, duration_s);
105 this->write_byte_(TIMER_CONTROL_REG, CLOCK_1_HZ);
106 }
107
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");
111 return;
112 }
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);
117}
118
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");
123 return;
124 }
125 uint8_t data = maybe_data.value();
126 this->write_byte_(CONTROL_STATUS_2_REG, data & 0xf3);
127}
128
129void BM8563::disable_irq_() {
130 this->clear_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");
134 return;
135 }
136 uint8_t data = maybe_data.value();
137 this->write_byte_(CONTROL_STATUS_2_REG, data & 0xfc);
138}
139
140} // namespace esphome::bm8563
uint8_t raw[35]
Definition bl0939.h:0
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
void update() override
Definition bm8563.cpp:28
void dump_config() override
Definition bm8563.cpp:30
void start_timer(uint32_t duration_s)
Definition bm8563.cpp:35
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:79
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:58
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.cpp:141
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")
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
@ NO_ERROR
No error found during execution of method.
Definition i2c_bus.h:12
std::span< const uint8_t > data
uint32_t value
Definition hal.cpp:59
const void size_t len
Definition hal.h:68
static void uint32_t
A more user-friendly version of struct tm from time.h.
Definition time.h:25
uint8_t minute
minutes after the hour [0-59]
Definition time.h:34
uint8_t second
seconds after the minute [0-60]
Definition time.h:32
uint8_t hour
hours since midnight [0-23]
Definition time.h:36
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:50
bool is_valid(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if this ESPTime is valid (year >= internal::MIN_VALID_YEAR and the requested fields are in rang...
Definition time.h:84
uint8_t day_of_month
day of the month [1-31]
Definition time.h:40
uint16_t year
year
Definition time.h:46
uint8_t month
month; january=1 [1-12]
Definition time.h:44
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:38