ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
bq25186.cpp
Go to the documentation of this file.
1#include "bq25186.h"
2#ifdef USE_BINARY_SENSOR
4#endif
5#ifdef USE_TEXT_SENSOR
7#endif
8
9#include "esphome/core/hal.h"
10#include "esphome/core/log.h"
11
13
14ESPHOME_LOG_TAG(TAG, "bq25186");
15
16// Bits the configuration owns in each register; the rest are the SYS_REG GPO level, reset
17// triggers (SHIP_RST) or the device id (MASK_ID) and are kept as read
18static const uint8_t BQ25186_CONFIG_MASKS[BQ25186_CONFIG_REGISTER_COUNT] PROGMEM = {
19 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1F, 0xEF, 0xFF, 0xF0,
20};
21
22bool BQ25186Component::write_register_(uint8_t reg, uint8_t value) {
23 if (!this->write_byte(reg, value)) {
24 ESP_LOGW(TAG, "Failed to write register 0x%02X", reg);
25 return false;
26 }
27 return true;
28}
29
30bool BQ25186Component::update_register_(uint8_t reg, uint8_t mask, uint8_t value) {
31 if (this->update_register(reg, mask, value))
32 return true;
33 ESP_LOGW(TAG, "Failed to update register 0x%02X", reg);
34 return false;
35}
36
37uint8_t BQ25186Component::expected_register_(size_t index) const {
38 uint8_t value = this->regs_[index];
39#ifdef USE_BQ25186_PG_GPO
40 if (index == 0 && this->pg_gpo_switch_ != nullptr)
41 value |= BQ25186_VBAT_CTRL_PG_MODE;
42#endif
43 return value;
44}
45
47 for (uint8_t reg = BQ25186_REG_VBAT_CTRL; reg <= BQ25186_REG_MASK_ID; reg++) {
48 const size_t index = reg - BQ25186_REG_VBAT_CTRL;
49 const uint8_t mask = progmem_read_byte(&BQ25186_CONFIG_MASKS[index]);
50 if (((regs[reg] ^ this->expected_register_(index)) & mask) != 0)
51 return false;
52 }
53 return true;
54}
55
57 // Setup rejects an unknown chip; the action must not program it before that check or after it failed
58 if (!this->is_ready() || this->is_failed()) {
59 ESP_LOGW(TAG, "Not applying configuration, setup has not succeeded");
60 return false;
61 }
62 this->regs_ = regs;
63 this->configured_ = true;
64 for (size_t i = 0; i < regs.size(); i++) {
65 const uint8_t reg = BQ25186_REG_VBAT_CTRL + i;
66 const uint8_t mask = progmem_read_byte(&BQ25186_CONFIG_MASKS[i]);
67 const uint8_t value = this->expected_register_(i);
68 const bool ok = mask == 0xFF ? this->write_register_(reg, value) : this->update_register_(reg, mask, value);
69 if (!ok) {
70 this->status_set_warning(LOG_STR("Configuration partly applied"));
71 return false;
72 }
73 }
75 return true;
76}
77
79 // Another charger at this address could lay its registers out differently, so do not write into it
80 if (!this->check_chip_id(this, BQ25186_REG_MASK_ID, 0x01, 0x0F))
81 return;
82
83 // A failed write only sets the warning status: the chip has identified itself, so a later
84 // apply_configuration may retry
85 if (this->configure_on_boot_)
86 this->apply_config(this->regs_);
87#ifdef USE_BQ25186_PG_GPO
88 if (this->pg_gpo_switch_ != nullptr) {
89 // The poll below puts the pin in output mode; without a restore mode the switch takes its level from it
90 auto initial = this->pg_gpo_switch_->get_initial_state_with_restore_mode();
91 if (initial.has_value())
92 this->pg_gpo_switch_->publish_state(*initial != this->pg_gpo_switch_->is_inverted()); // the first poll writes it
93 }
94#endif
95 // The poller first runs after a random share of the interval; the entities get a state now
96 this->update();
97}
98
99#ifdef USE_BQ25186_PG_GPO
100// The pin mode and level come from the switch, so every poll puts back whatever the chip lost
101void BQ25186Component::check_pg_gpo_(const BQ25186StatusRegisters &status, bool &pending) {
102 if (this->pg_gpo_switch_ == nullptr)
103 return;
104 if ((status[BQ25186_REG_VBAT_CTRL] & BQ25186_VBAT_CTRL_PG_MODE) == 0 &&
105 !this->update_register_(BQ25186_REG_VBAT_CTRL, BQ25186_VBAT_CTRL_PG_MODE, BQ25186_VBAT_CTRL_PG_MODE)) {
106 this->status_set_warning(LOG_STR("PG/GPO pin not in output mode"));
107 pending = true;
108 }
109 const bool level = (status[BQ25186_REG_SYS_REG] & BQ25186_SYS_REG_PG_GPO_LEVEL) != 0;
110 if (!this->pg_gpo_switch_->has_state()) {
111 this->pg_gpo_switch_->publish_state(level);
112 return;
113 }
114 // state is what the switch shows; an inverted switch drives the opposite level
115 const bool wanted = this->pg_gpo_switch_->state != this->pg_gpo_switch_->is_inverted();
116 if (level != wanted && !this->write_pg_gpo_level(wanted)) {
117 this->status_set_warning(LOG_STR("PG/GPO level write failed"));
118 pending = true;
119 }
120}
121#endif
122
123// Every poll also serves the I2C watchdog, which wants to see traffic within its period
126 if (!this->read_bytes(BQ25186_REG_STAT0, status.data(), status.size())) {
127 this->status_set_warning(LOG_STR("Status read failed"));
128 return;
129 }
130 // Whatever failed is tried again here, so the warning only stays while something is still wrong
131 bool pending = false;
132 // A watchdog or hardware reset puts the power-on values back, and a failed write leaves a register
133 // behind; either way the configuration is written again
134 if (this->configured_ && !this->config_matches_(status)) {
135 ESP_LOGW(TAG, "Configuration registers differ from the values applied, writing them again");
136 if (this->apply_config(this->regs_)) {
137 status[BQ25186_REG_VBAT_CTRL] |= BQ25186_VBAT_CTRL_PG_MODE; // part of the write when a switch is configured
138 } else {
139 pending = true;
140 }
141 }
142#ifdef USE_BQ25186_PG_GPO
143 this->check_pg_gpo_(status, pending);
144#endif
145 if (!pending)
146 this->status_clear_warning();
147#ifdef BQ25186_BINARY_SENSOR_COUNT
148 for (auto *sensor : this->binary_sensors_)
149 sensor->on_status(status);
150#endif
151#ifdef BQ25186_TEXT_SENSOR_COUNT
152 for (auto *sensor : this->text_sensors_)
153 sensor->on_status(status);
154#endif
155}
156
158 ESP_LOGCONFIG(TAG, "BQ25186:");
159 LOG_I2C_DEVICE(this);
160 LOG_UPDATE_INTERVAL(this);
161#ifdef USE_BQ25186_PG_GPO
162 LOG_SWITCH(" ", "PG/GPO", this->pg_gpo_switch_);
163#endif
164}
165
166} // namespace esphome::bq25186
uint8_t status
Definition bl0942.h:8
bool is_failed() const
Definition component.h:287
bool is_ready() const
void status_clear_warning()
Definition component.h:309
StaticVector< BQ25186TextSensor *, BQ25186_TEXT_SENSOR_COUNT > text_sensors_
Definition bq25186.h:85
BQ25186ConfigRegisters regs_
Definition bq25186.h:87
void check_pg_gpo_(const BQ25186StatusRegisters &status, bool &pending)
Definition bq25186.cpp:101
StaticVector< BQ25186BinarySensor *, BQ25186_BINARY_SENSOR_COUNT > binary_sensors_
Definition bq25186.h:82
bool write_register_(uint8_t reg, uint8_t value)
Definition bq25186.cpp:22
bool config_matches_(const BQ25186StatusRegisters &regs) const
Definition bq25186.cpp:46
bool apply_config(const BQ25186ConfigRegisters &regs)
Definition bq25186.cpp:56
uint8_t expected_register_(size_t index) const
Definition bq25186.cpp:37
bool update_register_(uint8_t reg, uint8_t mask, uint8_t value)
Definition bq25186.cpp:30
bool check_chip_id(Component *component, uint8_t a_register, uint8_t expected, uint8_t mask=0xFF)
Read the 8-bit ID at a_register and accept it when it matches expected in the bits set in mask.
Definition i2c.h:232
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
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
Definition i2c.cpp:95
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
ESPHOME_LOG_TAG(TAG, "bq25186")
std::array< uint8_t, BQ25186_CONFIG_REGISTER_COUNT > BQ25186ConfigRegisters
Definition bq25186.h:38
std::array< uint8_t, BQ25186_STATUS_REGISTER_COUNT > BQ25186StatusRegisters
Definition bq25186.h:39
uint32_t value
Definition hal.cpp:41
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51