ESPHome 2026.11.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
11#include <array>
12
13namespace esphome::bq25186 {
14
15static constexpr uint8_t BQ25186_REG_STAT0 = 0x00;
16static constexpr uint8_t BQ25186_REG_STAT1 = 0x01;
17static constexpr uint8_t BQ25186_REG_FLAG0 = 0x02;
18static constexpr uint8_t BQ25186_REG_VBAT_CTRL = 0x03;
19static constexpr uint8_t BQ25186_REG_ICHG_CTRL = 0x04;
20static constexpr uint8_t BQ25186_REG_CHARGECTRL0 = 0x05;
21static constexpr uint8_t BQ25186_REG_CHARGECTRL1 = 0x06;
22static constexpr uint8_t BQ25186_REG_IC_CTRL = 0x07;
23static constexpr uint8_t BQ25186_REG_TMR_ILIM = 0x08;
24static constexpr uint8_t BQ25186_REG_SHIP_RST = 0x09;
25static constexpr uint8_t BQ25186_REG_SYS_REG = 0x0A;
26static constexpr uint8_t BQ25186_REG_TS_CONTROL = 0x0B;
27static constexpr uint8_t BQ25186_REG_MASK_ID = 0x0C;
28static constexpr size_t BQ25186_CONFIG_REGISTER_COUNT = BQ25186_REG_MASK_ID - BQ25186_REG_VBAT_CTRL + 1;
29// STAT0 to MASK_ID, read together by every poll: the status for the entities, then the configuration
30// registers, which show whether the chip was reset or a write failed
31static constexpr size_t BQ25186_STATUS_REGISTER_COUNT = BQ25186_REG_MASK_ID - BQ25186_REG_STAT0 + 1;
32
33// VBAT_CTRL pg_mode: the /PG pin is a general purpose output driven by SYS_REG pg_gpo_level
34static constexpr uint8_t BQ25186_VBAT_CTRL_PG_MODE = 0x80;
35static constexpr uint8_t BQ25186_SYS_REG_PG_GPO_LEVEL = 0x10;
36
37// Register values for VBAT_CTRL..MASK_ID, packed by code generation
38using BQ25186ConfigRegisters = std::array<uint8_t, BQ25186_CONFIG_REGISTER_COUNT>;
39using BQ25186StatusRegisters = std::array<uint8_t, BQ25186_STATUS_REGISTER_COUNT>;
40
43
45 public:
46 void setup() override;
47 void update() override;
48 void dump_config() override;
49
51 this->regs_ = regs;
52 this->configure_on_boot_ = true;
53 }
54 bool apply_config(const BQ25186ConfigRegisters &regs);
55#ifdef BQ25186_BINARY_SENSOR_COUNT
56 void register_binary_sensor(BQ25186BinarySensor *sensor) { this->binary_sensors_.push_back(sensor); }
57#endif
58#ifdef BQ25186_TEXT_SENSOR_COUNT
59 void register_text_sensor(BQ25186TextSensor *sensor) { this->text_sensors_.push_back(sensor); }
60#endif
61#ifdef USE_BQ25186_PG_GPO
63 void set_pg_gpo_switch(switch_::Switch *pg_gpo_switch) { this->pg_gpo_switch_ = pg_gpo_switch; }
64 bool write_pg_gpo_level(bool level) {
65 return this->update_register_(BQ25186_REG_SYS_REG, BQ25186_SYS_REG_PG_GPO_LEVEL,
66 level ? BQ25186_SYS_REG_PG_GPO_LEVEL : 0);
67 }
68#endif
70 bool write_ship_rst(uint8_t mask, uint8_t value) { return this->update_register_(BQ25186_REG_SHIP_RST, mask, value); }
71
72 protected:
73 bool write_register_(uint8_t reg, uint8_t value);
74 uint8_t expected_register_(size_t index) const;
75 bool config_matches_(const BQ25186StatusRegisters &regs) const;
76#ifdef USE_BQ25186_PG_GPO
77 void check_pg_gpo_(const BQ25186StatusRegisters &status, bool &pending);
78#endif
79 bool update_register_(uint8_t reg, uint8_t mask, uint8_t value);
80
81#ifdef BQ25186_BINARY_SENSOR_COUNT
83#endif
84#ifdef BQ25186_TEXT_SENSOR_COUNT
86#endif
87#ifdef USE_BQ25186_PG_GPO
89#endif
90 BQ25186ConfigRegisters regs_{}; // the configuration last applied, written again after a chip reset
91 bool configure_on_boot_{false};
92 bool configured_{false}; // regs_ went to the chip at least once
93};
94
95} // namespace esphome::bq25186
This class simplifies creating components that periodically check a state.
Definition component.h:514
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:286
One field of a status register; match has a bit set for each field value that reads as on.
StaticVector< BQ25186TextSensor *, BQ25186_TEXT_SENSOR_COUNT > text_sensors_
Definition bq25186.h:85
BQ25186ConfigRegisters regs_
Definition bq25186.h:90
bool write_pg_gpo_level(bool level)
Definition bq25186.h:64
void check_pg_gpo_(const BQ25186StatusRegisters &status, bool &pending)
Definition bq25186.cpp:114
switch_::Switch * pg_gpo_switch_
Definition bq25186.h:88
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:49
bool apply_config(const BQ25186ConfigRegisters &regs)
Definition bq25186.cpp:59
uint8_t expected_register_(size_t index) const
Definition bq25186.cpp:40
bool update_register_(uint8_t reg, uint8_t mask, uint8_t value)
Definition bq25186.cpp:30
void register_text_sensor(BQ25186TextSensor *sensor)
Definition bq25186.h:59
bool write_ship_rst(uint8_t mask, uint8_t value)
Write the masked bits of SHIP_RST.
Definition bq25186.h:70
void set_setup_registers(const BQ25186ConfigRegisters &regs)
Definition bq25186.h:50
void set_pg_gpo_switch(switch_::Switch *pg_gpo_switch)
The /PG/GPO pin is put in general purpose output mode when a switch drives it.
Definition bq25186.h:63
void register_binary_sensor(BQ25186BinarySensor *sensor)
Definition bq25186.h:56
A two-bit status field; first is the string table index of the field's value 0.
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:132
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:152
Base class for all switches.
Definition switch.h:38
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