ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
bq25820.cpp
Go to the documentation of this file.
1#include "bq25820.h"
2
3#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome::bq25820 {
7
8ESPHOME_LOG_TAG(TAG, "bq25820");
9
10static constexpr uint8_t REG_CHARGE_VOLTAGE_LIMIT = 0x00;
11static constexpr uint8_t REG_CHARGE_CURRENT_LIMIT = 0x02;
12static constexpr uint8_t REG_INPUT_CURRENT_LIMIT = 0x06;
13static constexpr uint8_t REG_INPUT_VOLTAGE_LIMIT = 0x08;
14static constexpr uint8_t REG_REVERSE_MODE_VOLTAGE_LIMIT = 0x0C;
15static constexpr uint8_t REG_PRECHARGE_CURRENT_LIMIT = 0x10;
16static constexpr uint8_t REG_TIMER_CONTROL = 0x15;
17static constexpr uint8_t REG_CHARGER_CONTROL = 0x17;
18static constexpr uint8_t REG_POWER_PATH_CONTROL = 0x19;
19static constexpr uint8_t REG_MPPT_CONTROL = 0x1A;
20static constexpr uint8_t REG_TS_THRESHOLD_CONTROL = 0x1B;
21static constexpr uint8_t REG_TS_REGION_CONTROL = 0x1C;
22static constexpr uint8_t REG_CHARGER_STATUS_1 = 0x21;
23static constexpr uint8_t REG_ADC_CONTROL = 0x2B;
24static constexpr uint8_t REG_PART_INFORMATION = 0x3D;
25
26// PART_NUM[5:3] 011b (BQ25820); DEV_REV[2:0] is not checked, so a new silicon revision still passes
27static constexpr uint8_t PART_NUM_BQ25820 = 0x18;
28static constexpr uint8_t PART_NUM_MASK = 0x38;
29
30// The 16-bit limits, written LSB first, in BQ25820Config::limits order
31static constexpr uint8_t LIMIT_REGISTERS[] PROGMEM = {
32 REG_CHARGE_CURRENT_LIMIT, REG_PRECHARGE_CURRENT_LIMIT, REG_INPUT_CURRENT_LIMIT,
33 REG_INPUT_VOLTAGE_LIMIT, REG_REVERSE_MODE_VOLTAGE_LIMIT,
34};
35// The 8-bit registers in BQ25820Config::controls order, and the bits the configuration owns in each
36struct ControlRegister {
37 uint8_t reg;
38 uint8_t mask;
39};
40static constexpr ControlRegister CONTROL_REGISTERS[] PROGMEM = {
41 {REG_CHARGE_VOLTAGE_LIMIT, 0x1F}, // VFB_REG
42 {REG_TIMER_CONTROL, 0xFE}, // everything but EN_TMR2X
43 {REG_POWER_PATH_CONTROL, 0x04}, // EN_BATFET_IDEAL_DIODE
44 {REG_MPPT_CONTROL, 0x07}, // FULL_SWEEP_TMR[2:1], EN_MPPT
45 {REG_TS_THRESHOLD_CONTROL, 0xFF}, // TS_T1, TS_T2, TS_T3, TS_T5
46 {REG_TS_REGION_CONTROL, 0x01}, // EN_TS
47 {REG_CHARGER_CONTROL, 0xFF}, // VRECHG, EN_HIZ, EN_CHG; after every limit, so charging starts with them
48};
49static_assert(std::size(LIMIT_REGISTERS) == BQ25820_LIMIT_COUNT);
50static_assert(std::size(CONTROL_REGISTERS) == BQ25820_CONTROL_COUNT);
51static constexpr size_t CONTROL_TIMER = 1;
52static constexpr size_t CONTROL_MPPT = 3;
53static constexpr size_t CONTROL_CHARGER = 6;
54static constexpr size_t LIMIT_CHARGE_CURRENT = 0;
55static constexpr size_t LIMIT_PRECHARGE_CURRENT = 1;
56static constexpr size_t LIMIT_INPUT_CURRENT = 2;
57
58// Timer control (0x15)
59static constexpr uint8_t TIMER_CONTROL_WATCHDOG = 0x30;
60// Charger control (0x17)
61static constexpr uint8_t WD_RST = 0x20;
62static constexpr uint8_t EN_CHG = 0x01;
63// Charger status 1 (0x21)
64static constexpr uint8_t WD_STAT = 0x08;
65// Power path control (0x19)
66static constexpr uint8_t REG_RST = 0x80;
67// MPPT control (0x1A)
68static constexpr uint8_t FORCE_SWEEP = 0x80;
69static constexpr uint8_t EN_MPPT = 0x01;
70// ADC control (0x2B); the configuration sets it last and a charger reset clears it
71static constexpr uint8_t ADC_EN = 0x80;
72static constexpr uint8_t ADC_EN_RATE_MASK = 0xC0;
73
74static constexpr uint32_t PUBLISH_TIMEOUT_ID = 0;
75// After a configuration write, long enough for the ADC to finish its first round of conversions
76static constexpr uint32_t FIRST_PUBLISH_DELAY_MS = 1000;
77
78// A current limit step is 250 uV across the sense resistor: 25 IBAT or 62.5 IAC ADC steps
79static constexpr float CHARGE_LIMIT_ADC_STEPS = 25.0f;
80static constexpr float INPUT_LIMIT_ADC_STEPS = 62.5f;
81
82static void log_current_limit(const LogString *name, uint16_t limit, float amps_per_code) {
83 if (limit != 0) {
84 ESP_LOGCONFIG(TAG, " %s: %.2f A", LOG_STR_ARG(name), (limit >> 2) * amps_per_code);
85 }
86}
87
89 if (!this->check_chip_id(this, REG_PART_INFORMATION, PART_NUM_BQ25820, PART_NUM_MASK))
90 return;
91
92 // Start from the reset values; the bit clears itself
93 if (!this->setup_check(this->update_register(REG_POWER_PATH_CONTROL, REG_RST, REG_RST)))
94 return;
95
96 // Written now, whatever the reads do, so charging never runs at the reset limits for an interval
97 this->configure_();
98}
99
101 const BQ25820Config config = this->load_config_();
102 ESP_LOGCONFIG(TAG,
103 "BQ25820:\n"
104 " Watchdog: %s\n"
105 " MPPT: %s",
106 YESNO(this->watchdog_enabled_()), YESNO(config.controls[CONTROL_MPPT] & EN_MPPT));
107 const float charge_step = CHARGE_LIMIT_ADC_STEPS * config.ibat_scale;
108 log_current_limit(LOG_STR("Charge current"), config.limits[LIMIT_CHARGE_CURRENT], charge_step);
109 log_current_limit(LOG_STR("Precharge current"), config.limits[LIMIT_PRECHARGE_CURRENT], charge_step);
110 log_current_limit(LOG_STR("Input current limit"), config.limits[LIMIT_INPUT_CURRENT],
111 INPUT_LIMIT_ADC_STEPS * config.iac_scale);
112 LOG_I2C_DEVICE(this);
113 LOG_UPDATE_INTERVAL(this);
114#ifdef USE_BQ25820_SENSOR
115 LOG_SENSOR(" ", "Battery Voltage", this->vbat_sensor_);
116 LOG_SENSOR(" ", "System Voltage", this->vsys_sensor_);
117 LOG_SENSOR(" ", "Input Voltage", this->vac_sensor_);
118 LOG_SENSOR(" ", "Battery Current", this->ibat_sensor_);
119 LOG_SENSOR(" ", "Input Current", this->iac_sensor_);
120 LOG_SENSOR(" ", "Temperature", this->temp_in_c_sensor_);
121#endif
122#ifdef USE_BQ25820_STATUS_PUBLISHER
123 if (this->dump_publisher_ != nullptr)
124 this->dump_publisher_(TAG);
125#endif
126}
127
129 BQ25820Config config;
130 progmem_memcpy(&config, this->config_, sizeof(config));
131 return config;
132}
133
135 return (progmem_read_byte(&this->config_->controls[CONTROL_TIMER]) & TIMER_CONTROL_WATCHDOG) != 0;
136}
137
138// ADC off, charging off, configure, charging on, ADC last: a failure before charger control leaves charging off,
139// and any failure leaves ADC_EN clear so the next poll writes it all again.
141 BQ25820Config config = this->load_config_();
142 // The charger control write also feeds the watchdog
143 if (this->watchdog_enabled_())
144 config.controls[CONTROL_CHARGER] |= WD_RST;
145 // Charging goes off even when the ADC write fails
146 bool ok = this->update_register(REG_ADC_CONTROL, ADC_EN, 0);
147 ok = this->update_register(REG_CHARGER_CONTROL, EN_CHG, 0) && ok;
148 for (size_t i = 0; ok && i < BQ25820_LIMIT_COUNT; i++) {
149 if (config.limits[i] != 0)
150 ok = this->write_byte_16_le(progmem_read_byte(&LIMIT_REGISTERS[i]), config.limits[i]);
151 }
152 for (size_t i = 0; ok && i < BQ25820_CONTROL_COUNT; i++) {
153 ControlRegister entry;
154 progmem_memcpy(&entry, &CONTROL_REGISTERS[i], sizeof(entry));
155 const uint8_t value = config.controls[i];
156 ok = entry.mask == 0xFF ? this->write_byte(entry.reg, value) : this->update_register(entry.reg, entry.mask, value);
157 }
158 // One panel sweep at startup with charging on, kept pending until it is written
159 if (ok && this->force_mppt_sweep_) {
160 ok = this->update_register(REG_MPPT_CONTROL, FORCE_SWEEP, FORCE_SWEEP);
161 this->force_mppt_sweep_ = !ok;
162 }
163 // ADC_RATE clear for continuous conversion
164 ok = ok && this->update_register(REG_ADC_CONTROL, ADC_EN_RATE_MASK, ADC_EN);
165 if (!ok) {
166 ESP_LOGW(TAG, "Configuration write failed");
167 }
168 return ok;
169}
170
172 // The ADC restarts, so the next publish waits for its first conversions
173 this->adc_ready_ = false;
174 this->rewrite_ = !this->write_config_registers_();
175 if (!this->status_comm_result(!this->rewrite_))
176 return false;
177 this->set_timeout(PUBLISH_TIMEOUT_ID, FIRST_PUBLISH_DELAY_MS, [this]() {
178 this->adc_ready_ = true;
179 this->update();
180 });
181 return true;
182}
183
184// Every poll also feeds the watchdog, which update_interval is validated to beat
188 bool ok = this->read_bytes(REG_CHARGER_STATUS_1, status.data(), status.size()) &&
189#ifdef USE_BQ25820_SENSOR
190 this->read_bytes_16_le(REG_ADC_CONTROL, adc.data(), adc.size());
191#else
192 this->read_byte(REG_ADC_CONTROL, adc.data());
193#endif
194 // Only the write or the feed below clears the warning, so a failing one is not reported as recovered
195 if (!this->status_comm_check(ok))
196 return;
197 const bool watchdog = this->watchdog_enabled_();
198 // ADC_EN clear: a partial write or a charger reset; WD_STAT: the watchdog expired
199 if (this->rewrite_ || (adc[0] & ADC_EN) == 0 || (watchdog && (status[0] & WD_STAT) != 0)) {
200 if (this->adc_ready_) {
201 ESP_LOGW(TAG, "Charger lost its configuration, writing it again");
202 }
203 if (this->configure_())
204 return;
205 } else {
206 // A failed feed still publishes what was read
207 this->status_comm_result(
208 !watchdog ||
209 this->write_byte(REG_CHARGER_CONTROL, progmem_read_byte(&this->config_->controls[CONTROL_CHARGER]) | WD_RST));
210 }
211#ifdef USE_BQ25820_STATUS_PUBLISHER
212 if (this->status_publisher_ != nullptr)
213 this->status_publisher_(status);
214#endif
215#ifdef USE_BQ25820_SENSOR
216 if (this->adc_ready_)
217 this->publish_adc_(adc);
218#endif
219}
220
221#ifdef USE_BQ25820_SENSOR
222static constexpr float VOLTAGE_ADC_STEP_V = 0.002f;
223
225 float iac_scale;
226 float ibat_scale;
227 progmem_memcpy(&iac_scale, &this->config_->iac_scale, sizeof(iac_scale));
228 progmem_memcpy(&ibat_scale, &this->config_->ibat_scale, sizeof(ibat_scale));
229 PUBLISH_STATE(this->iac_sensor_, adc[1] * iac_scale);
230 // Positive while charging
231 PUBLISH_STATE(this->ibat_sensor_, adc[2] * ibat_scale);
232 PUBLISH_STATE(this->vac_sensor_, static_cast<uint16_t>(adc[3]) * VOLTAGE_ADC_STEP_V);
233 PUBLISH_STATE(this->vbat_sensor_, static_cast<uint16_t>(adc[4]) * VOLTAGE_ADC_STEP_V);
234 PUBLISH_STATE(this->vsys_sensor_, static_cast<uint16_t>(adc[5]) * VOLTAGE_ADC_STEP_V);
235 PUBLISH_STATE(this->temp_in_c_sensor_, this->ntc_temperature_(adc[6]));
236}
237#endif
238
239} // namespace esphome::bq25820
uint8_t status
Definition bl0942.h:8
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:235
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
bool status_comm_check(bool ok)
Sets the warning ("Communication failed") when not ok but never clears it; returns ok.
Definition component.h:308
BQ25820Config load_config_() const
Definition bq25820.cpp:128
bool configure_()
Writes the configuration, then polls again once the ADC has converted.
Definition bq25820.cpp:171
float ntc_temperature_(int16_t ts_adc) const
Temperature from TS_ADC, the TS pin voltage as a 10-bit share of REGN; NAN for a shorted or open NTC.
Definition bq25820.h:102
void publish_adc_(const BQ25820AdcRegisters &adc)
Definition bq25820.cpp:224
const BQ25820Config * config_
Definition bq25820.h:114
bool write_byte_16_le(uint8_t a_register, uint16_t data) const
Write a 16-bit value least significant byte first.
Definition i2c.cpp:146
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 read_bytes_16_le(uint8_t a_register, uint16_t *data, uint8_t len)
Read len 16-bit values stored least significant byte first; no conversion is compiled on little-endia...
Definition i2c.h:284
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
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
std::array< uint8_t, 4 > BQ25820StatusRegisters
Definition bq25820.h:41
ESPHOME_LOG_TAG(TAG, "bq25820")
std::array< int16_t, 7 > BQ25820AdcRegisters
Definition bq25820.h:44
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
uint32_t value
Definition hal.cpp:59
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
static void uint32_t
std::array< uint8_t, BQ25820_CONTROL_COUNT > controls
Definition bq25820.h:34
std::array< uint16_t, BQ25820_LIMIT_COUNT > limits
Definition bq25820.h:32