ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
bq25186.h
Go to the documentation of this file.
1#pragma once
2
7#ifdef USE_BQ25186_PG_GPO
9#endif
10#ifdef USE_TEXT_SENSOR
13#endif
14
15#include <array>
16
17namespace esphome::bq25186 {
18
19static constexpr uint8_t BQ25186_REG_STAT0 = 0x00;
20static constexpr uint8_t BQ25186_REG_STAT1 = 0x01;
21static constexpr uint8_t BQ25186_REG_FLAG0 = 0x02;
22static constexpr uint8_t BQ25186_REG_VBAT_CTRL = 0x03;
23static constexpr uint8_t BQ25186_REG_ICHG_CTRL = 0x04;
24static constexpr uint8_t BQ25186_REG_CHARGECTRL0 = 0x05;
25static constexpr uint8_t BQ25186_REG_CHARGECTRL1 = 0x06;
26static constexpr uint8_t BQ25186_REG_IC_CTRL = 0x07;
27static constexpr uint8_t BQ25186_REG_TMR_ILIM = 0x08;
28static constexpr uint8_t BQ25186_REG_SHIP_RST = 0x09;
29static constexpr uint8_t BQ25186_REG_SYS_REG = 0x0A;
30static constexpr uint8_t BQ25186_REG_TS_CONTROL = 0x0B;
31static constexpr uint8_t BQ25186_REG_MASK_ID = 0x0C;
32static constexpr size_t BQ25186_CONFIG_REGISTER_COUNT = BQ25186_REG_MASK_ID - BQ25186_REG_VBAT_CTRL + 1;
33// STAT0 to MASK_ID, read together by every poll: the status for the entities, then the configuration
34// registers, which show whether the chip was reset or a write failed
35static constexpr size_t BQ25186_STATUS_REGISTER_COUNT = BQ25186_REG_MASK_ID - BQ25186_REG_STAT0 + 1;
36
37// VBAT_CTRL pg_mode: the /PG pin is a general purpose output driven by SYS_REG pg_gpo_level
38static constexpr uint8_t BQ25186_VBAT_CTRL_PG_MODE = 0x80;
39static constexpr uint8_t BQ25186_SYS_REG_PG_GPO_LEVEL = 0x10;
40
41// Register values for VBAT_CTRL..MASK_ID, packed by code generation
42using BQ25186ConfigRegisters = std::array<uint8_t, BQ25186_CONFIG_REGISTER_COUNT>;
43using BQ25186StatusRegisters = std::array<uint8_t, BQ25186_STATUS_REGISTER_COUNT>;
44
45#ifdef USE_TEXT_SENSOR
46// STAT0[6:5] charge status, then STAT1[4:3] TS status
47PROGMEM_STRING_TABLE(BQ25186StatusStrings, "Not Charging", "Constant Current", "Constant Voltage",
48 "Charge Done/Disabled", "Normal", "Hot/Cold (Charging Suspended)", "Cool (Current Reduced)",
49 "Warm (Voltage Reduced)");
50#endif
51
53 public:
54 void setup() override;
55 void update() override;
56 void dump_config() override;
57
59 this->regs_ = regs;
60 this->configure_on_boot_ = true;
61 }
62 bool apply_config(const BQ25186ConfigRegisters &regs);
63#ifdef USE_BQ25186_STATUS_PUBLISHER
65#endif
66#ifdef USE_BQ25186_PG_GPO
68 SUB_SWITCH(pg_gpo)
69 bool write_pg_gpo_level(bool level) {
70 return this->update_register_(BQ25186_REG_SYS_REG, BQ25186_SYS_REG_PG_GPO_LEVEL,
71 level ? BQ25186_SYS_REG_PG_GPO_LEVEL : 0);
72 }
73#endif
75 bool write_ship_rst(uint8_t mask, uint8_t value) { return this->update_register_(BQ25186_REG_SHIP_RST, mask, value); }
76
77 protected:
78 bool write_register_(uint8_t reg, uint8_t value);
79 bool update_register_(uint8_t reg, uint8_t mask, uint8_t value);
80 uint8_t expected_register_(size_t index) const;
81 bool config_matches_(const BQ25186StatusRegisters &regs) const;
82#ifdef USE_BQ25186_PG_GPO
83 void check_pg_gpo_(const BQ25186StatusRegisters &status, bool &pending);
84#endif
85
86 BQ25186ConfigRegisters regs_{}; // the configuration last applied, written again after a chip reset
87 bool configure_on_boot_{false};
88 bool configured_{false}; // regs_ went to the chip at least once
89};
90
91} // namespace esphome::bq25186
This class simplifies creating components that periodically check a state.
Definition component.h:542
BQ25186ConfigRegisters regs_
Definition bq25186.h:86
void check_pg_gpo_(const BQ25186StatusRegisters &status, bool &pending)
Definition bq25186.cpp:95
HUB_PUBLISHER(status, const BQ25186StatusRegisters &) SUB_SWITCH(pg_gpo) bool write_pg_gpo_level(bool level)
The /PG/GPO pin is put in general purpose output mode when a switch drives it.
Definition bq25186.h:64
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 write_ship_rst(uint8_t mask, uint8_t value)
Write the masked bits of SHIP_RST.
Definition bq25186.h:75
void set_setup_registers(const BQ25186ConfigRegisters &regs)
Definition bq25186.h:58
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
std::array< uint8_t, BQ25186_CONFIG_REGISTER_COUNT > BQ25186ConfigRegisters
Definition bq25186.h:42
PROGMEM_STRING_TABLE(BQ25186StatusStrings, "Not Charging", "Constant Current", "Constant Voltage", "Charge Done/Disabled", "Normal", "Hot/Cold (Charging Suspended)", "Cool (Current Reduced)", "Warm (Voltage Reduced)")
std::array< uint8_t, BQ25186_STATUS_REGISTER_COUNT > BQ25186StatusRegisters
Definition bq25186.h:43
uint32_t value
Definition hal.cpp:59