ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
bq25820.h
Go to the documentation of this file.
1#pragma once
2
6
7#ifdef USE_BQ25820_SENSOR
9#endif
10#ifdef USE_BINARY_SENSOR
12#endif
13#ifdef USE_TEXT_SENSOR
16#endif
18
19#include <array>
20#include <cmath>
21
22namespace esphome::bq25820 {
23
24static constexpr size_t BQ25820_LIMIT_COUNT = 5;
25static constexpr size_t BQ25820_CONTROL_COUNT = 7;
26// TS_ADC this close to REGN means no NTC is connected
27static constexpr uint16_t NTC_OPEN_CODE = 1020;
28
29// Register values built by code generation into a PROGMEM table, in the order of the register tables in bq25820.cpp
31 // ICHG, IPRECHG, IAC_DPM, VAC_DPM and VAC_REV; 0 keeps the reset value
32 std::array<uint16_t, BQ25820_LIMIT_COUNT> limits;
33 // VFB_REG, timer, power path, MPPT, TS threshold, TS region and charger control
34 std::array<uint8_t, BQ25820_CONTROL_COUNT> controls;
35 // Amps per current ADC step, from the sense resistors
37 float iac_scale;
38};
39
40// CHARGER_STATUS_1-3 and FAULT_STATUS (0x21-0x24), read together by every poll
41using BQ25820StatusRegisters = std::array<uint8_t, 4>;
42#ifdef USE_BQ25820_SENSOR
43// ADC_CONTROL in the low byte of the first word, then the IAC, IBAT, VAC, VBAT, VSYS and TS ADCs (0x2B-0x38)
44using BQ25820AdcRegisters = std::array<int16_t, 7>;
45#else
46// ADC_CONTROL (0x2B)
47using BQ25820AdcRegisters = std::array<uint8_t, 1>;
48#endif
49
50#ifdef USE_TEXT_SENSOR
51// CHARGE_STAT codes, then TS_STAT codes; undefined TS codes 5-7 fall back to index 5
52PROGMEM_STRING_TABLE(BQ25820StatusStrings, "Not Charging", "Trickle", "Precharge", "Fast Charge", "Taper", "Unknown",
53 "Top-Off", "Done", "Normal", "Warm", "Cool", "Cold", "Hot");
54
56inline void publish_status_text(text_sensor::TextSensor *sensor, uint8_t &last, uint8_t value, uint8_t first) {
57 value &= 0x07;
58 if (value == last)
59 return;
60 last = value;
61 sensor->publish_state(BQ25820StatusStrings::get_progmem_str(first + value, 5));
62}
63#endif
64
65class BQ25820Component final : public PollingComponent, public i2c::I2CDevice {
66 public:
68 BQ25820Component(const BQ25820Config *config, bool force_mppt_sweep)
69 : config_(config), force_mppt_sweep_(force_mppt_sweep) {}
70
71 void setup() override;
72 void dump_config() override;
73 void update() override;
74
75#ifdef USE_BQ25820_SENSOR
77 SUB_SENSOR(vsys)
78 SUB_SENSOR(vac)
79 SUB_SENSOR(ibat)
80 SUB_SENSOR(iac)
81 SUB_SENSOR(temp_in_c)
82 // TS thermistor B-parameter fit: 1/T = a + b * ln(R_ntc / R_pullup), from code generation
83 void set_ntc_calibration(float a, float b) {
84 this->ntc_a_ = a;
85 this->ntc_b_ = b;
86 }
87#endif
88#ifdef USE_BQ25820_STATUS_PUBLISHER
90 HUB_PUBLISHER(dump, const char *)
91#endif
92
93 protected:
95 bool watchdog_enabled_() const;
98 bool configure_();
99#ifdef USE_BQ25820_SENSOR
100 void publish_adc_(const BQ25820AdcRegisters &adc);
102 float ntc_temperature_(int16_t ts_adc) const {
103 const uint16_t code = ts_adc & 0x3FF;
104 if (code == 0 || code >= NTC_OPEN_CODE)
105 return NAN;
106 const float ratio = code / 1024.0f;
107 return 1.0f / (this->ntc_a_ + this->ntc_b_ * logf(ratio / (1.0f - ratio))) - 273.15f;
108 }
109
110 float ntc_a_{NAN};
111 float ntc_b_{NAN};
112#endif
113
116 // Cleared by a configuration write until the delayed poll, so no poll publishes unconverted ADC readings
117 bool adc_ready_{false};
118 // A failed write may still have set ADC_EN, so the next poll writes the configuration again regardless
119 bool rewrite_{false};
120};
121
122} // namespace esphome::bq25820
This class simplifies creating components that periodically check a state.
Definition component.h:541
BQ25820Component(const BQ25820Config *config, bool force_mppt_sweep)
config points into PROGMEM
Definition bq25820.h:68
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
SUB_SENSOR(vbat) SUB_SENSOR(vsys) SUB_SENSOR(vac) SUB_SENSOR(ibat) SUB_SENSOR(iac) SUB_SENSOR(temp_in_c) void set_ntc_calibration(float a
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
HUB_PUBLISHER(status, const BQ25820StatusRegisters &) HUB_PUBLISHER(dump
void publish_adc_(const BQ25820AdcRegisters &adc)
Definition bq25820.cpp:224
const BQ25820Config * config_
Definition bq25820.h:114
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
void publish_state(const std::string &state)
Definition text_sensor.h:42
std::array< uint8_t, 4 > BQ25820StatusRegisters
Definition bq25820.h:41
void publish_status_text(text_sensor::TextSensor *sensor, uint8_t &last, uint8_t value, uint8_t first)
Publishes string first + (value & 7), only on change.
Definition bq25820.h:56
PROGMEM_STRING_TABLE(BQ25820StatusStrings, "Not Charging", "Trickle", "Precharge", "Fast Charge", "Taper", "Unknown", "Top-Off", "Done", "Normal", "Warm", "Cool", "Cold", "Hot")
std::array< int16_t, 7 > BQ25820AdcRegisters
Definition bq25820.h:44
uint32_t value
Definition hal.cpp:59
std::array< uint8_t, BQ25820_CONTROL_COUNT > controls
Definition bq25820.h:34
std::array< uint16_t, BQ25820_LIMIT_COUNT > limits
Definition bq25820.h:32