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