ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
bq27220.cpp
Go to the documentation of this file.
1#include "bq27220.h"
2
3#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6
7#include <cmath>
8
9namespace esphome::bq27220 {
10
11ESPHOME_LOG_TAG(TAG, "bq27220");
12
13// Standard command registers (16-bit, little-endian), SLUUBD4A
14static constexpr uint8_t REG_CONTROL = 0x00;
15static constexpr uint8_t REG_TEMPERATURE = 0x06; // 0.1 K
16static constexpr uint8_t REG_VOLTAGE = 0x08; // mV
17static constexpr uint8_t REG_CURRENT = 0x0C; // mA, signed
18static constexpr uint8_t REG_REMAINING_CAPACITY = 0x10; // mAh
19static constexpr uint8_t REG_FULL_CHARGE_CAPACITY = 0x12; // mAh
20static constexpr uint8_t REG_TIME_TO_EMPTY = 0x16; // min, 0xFFFF when not discharging
21static constexpr uint8_t REG_STATE_OF_CHARGE = 0x2C; // %
22static constexpr uint8_t REG_STATE_OF_HEALTH = 0x2E; // %
23// The DEVICE_NUMBER subcommand (0x0001, SLUUBD4A §2.2.2) answers through the MAC data buffer
24static constexpr uint8_t REG_MAC_DATA = 0x40;
25static constexpr uint16_t DEVICE_NUMBER = 0x0220;
26static constexpr uint16_t TIME_TO_EMPTY_NOT_APPLICABLE = 0xFFFF;
27
29 const uint8_t device_number_cmd[2] = {0x01, 0x00}; // DEVICE_NUMBER subcommand, little-endian
30 if (this->write_register(REG_CONTROL, device_number_cmd, 2) != i2c::ERROR_OK) {
31 this->mark_failed(LOG_STR("Failed to communicate with BQ27220"));
32 return;
33 }
34 delay(2); // SLUUBD4A §5.3: the subcommand result needs a moment before it can be read
35 uint16_t device_number = 0;
36 if (!this->read_byte_16_le(REG_MAC_DATA, &device_number)) {
37 this->mark_failed(LOG_STR("Failed to read device number from BQ27220"));
38 return;
39 }
40 if (device_number != DEVICE_NUMBER) {
41 ESP_LOGE(TAG, "Unexpected device number 0x%04X (expected 0x%04X)", device_number, DEVICE_NUMBER);
42 this->mark_failed(LOG_STR("Unexpected device number"));
43 }
44}
45
46void BQ27220Component::update() {
47 bool ok = true;
48 this->publish_(this->voltage_sensor_, REG_VOLTAGE, ok, [](uint16_t raw) { return raw * 0.001f; });
49 this->publish_(this->current_sensor_, REG_CURRENT, ok,
50 [](uint16_t raw) { return static_cast<int16_t>(raw) * 0.001f; });
51 this->publish_(this->battery_level_sensor_, REG_STATE_OF_CHARGE, ok, [](uint16_t raw) { return float(raw); });
52 this->publish_(this->temperature_sensor_, REG_TEMPERATURE, ok, [](uint16_t raw) { return raw * 0.1f - 273.15f; });
53 this->publish_(this->remaining_capacity_sensor_, REG_REMAINING_CAPACITY, ok, [](uint16_t raw) { return float(raw); });
54 this->publish_(this->full_charge_capacity_sensor_, REG_FULL_CHARGE_CAPACITY, ok,
55 [](uint16_t raw) { return float(raw); });
56 this->publish_(this->time_to_empty_sensor_, REG_TIME_TO_EMPTY, ok,
57 [](uint16_t raw) { return raw == TIME_TO_EMPTY_NOT_APPLICABLE ? NAN : float(raw); });
58 this->publish_(this->state_of_health_sensor_, REG_STATE_OF_HEALTH, ok, [](uint16_t raw) { return float(raw); });
59
60 this->status_comm_result(ok, LOG_STR("Failed to read one or more registers from BQ27220"));
61}
62
63void BQ27220Component::dump_config() {
64 ESP_LOGCONFIG(TAG, "BQ27220 Battery Fuel Gauge:");
65 LOG_I2C_DEVICE(this);
66 LOG_UPDATE_INTERVAL(this);
67 LOG_SENSOR(" ", "Voltage", this->voltage_sensor_);
68 LOG_SENSOR(" ", "Current", this->current_sensor_);
69 LOG_SENSOR(" ", "Battery Level", this->battery_level_sensor_);
70 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
71 LOG_SENSOR(" ", "Remaining Capacity", this->remaining_capacity_sensor_);
72 LOG_SENSOR(" ", "Full Charge Capacity", this->full_charge_capacity_sensor_);
73 LOG_SENSOR(" ", "Time To Empty", this->time_to_empty_sensor_);
74 LOG_SENSOR(" ", "State Of Health", this->state_of_health_sensor_);
75}
76
77} // namespace esphome::bq27220
uint8_t raw[35]
Definition bl0939.h:0
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void publish_(sensor::Sensor *sensor, uint8_t reg, bool &ok, F convert)
Read a standard command register and publish it through the sensor, if one is configured.
Definition bq27220.h:30
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:54
bool read_byte_16_le(uint8_t a_register, uint16_t *data)
Read a 16-bit value stored least significant byte first.
Definition i2c.h:296
ESPHOME_LOG_TAG(TAG, "bq27220")
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
FLAG_HAS_TRANSITION float
void HOT delay(uint32_t ms)
Definition hal.cpp:85