10static constexpr uint8_t BQ76972_REG_SUBCOMMAND = 0x3E;
11static constexpr uint8_t BQ76972_REG_TRANSFER_BUFFER = 0x40;
12static constexpr uint8_t BQ76972_REG_CHECKSUM = 0x60;
13static constexpr uint8_t BQ76972_CMD_BAT_STATUS = 0x12;
14static constexpr uint8_t BQ76972_CMD_CELL_START = 0x14;
15static constexpr uint8_t BQ76972_CMD_STACK_VOLTAGE = 0x34;
16static constexpr uint8_t BQ76972_CMD_TEMP_START = 0x68;
17static constexpr uint16_t BQ76972_SUBCMD_ENTER_CFG_UPDATE = 0x0090;
18static constexpr uint16_t BQ76972_SUBCMD_EXIT_CFG_UPDATE = 0x0092;
19static constexpr uint16_t BQ76972_MEM_REG12_CONFIG = 0x9236;
20static constexpr uint16_t BQ76972_MEM_REG0_CONFIG = 0x9237;
21static constexpr uint16_t BQ76972_MEM_I2C_ADDRESS = 0x923A;
22static constexpr uint16_t BQ76972_MEM_SWAP_COMM_MODE = 0x29BC;
24static constexpr uint16_t BQ76972_MEM_TEMP_PIN_CONFIG_BASE = 0x92F9;
25static constexpr size_t BQ76972_TS1_INDEX = 4;
26static constexpr uint8_t BQ76972_PIN_CONFIG_THERMISTOR = 0x07;
29static constexpr size_t BQ76972_MAX_TRANSFER = 64;
30static constexpr uint8_t BQ76972_MAX_POLLS = 50;
32static float raw_to_celsius(uint16_t
raw) {
return static_cast<int16_t
>(
raw) * 0.1f - 273.15f; }
39 std::array<uint8_t, BQ76972_MAX_TRANSFER>
buffer;
46 const uint8_t crc0_input[4] = {
static_cast<uint8_t
>(this->
address_ << 1), start_register,
47 static_cast<uint8_t
>((this->
address_ << 1) | 1), buffer[0]};
48 if (
crc8(crc0_input, 4, 0x00, 0x07,
true) !=
buffer[1]) {
49 ESP_LOGD(TAG,
"CRC mismatch at register 0x%02X byte 0", start_register);
53 for (
size_t i = 1; i <
len; ++i) {
55 ESP_LOGD(TAG,
"CRC mismatch at register 0x%02X byte %u", start_register,
static_cast<unsigned>(i));
67 std::array<uint8_t, BQ76972_MAX_TRANSFER>
buffer;
72 const uint8_t crc0_input[3] = {
static_cast<uint8_t
>(this->
address_ << 1), start_register, data[0]};
74 buffer[1] =
crc8(crc0_input, 3, 0x00, 0x07,
true);
75 for (
size_t i = 1; i <
len; ++i) {
77 buffer[i * 2 + 1] =
crc8(&data[i], 1, 0x00, 0x07,
true);
83 if (!this->
read_block_(start_register,
reinterpret_cast<uint8_t *
>(data), num_words * 2))
85 for (
size_t i = 0; i < num_words; i++)
91 const uint8_t bytes[2] = {
static_cast<uint8_t
>(subcommand & 0xFF),
static_cast<uint8_t
>(subcommand >> 8)};
92 return this->
write_block_(BQ76972_REG_SUBCOMMAND, bytes, 2);
97 for (uint8_t i = 0; i < BQ76972_MAX_POLLS; i++) {
98 if (!this->
read_block_(BQ76972_REG_SUBCOMMAND, bytes, 2))
100 if (bytes[0] != 0xFF && bytes[1] != 0xFF)
109 for (uint8_t i = 0; i < BQ76972_MAX_POLLS; i++) {
134 sum_bytes(data,
len,
static_cast<uint8_t
>(subcommand & 0xFF) +
static_cast<uint8_t
>(subcommand >> 8));
135 const uint8_t tail[2] = {
static_cast<uint8_t
>(~sum),
static_cast<uint8_t
>(4 +
len)};
136 return this->
write_block_(BQ76972_REG_CHECKSUM, tail, 2);
156 out = reg12 == 0 && (reg0 & 0x01) == 0;
161 uint16_t internal_temp;
162 if (!this->
read_words_(BQ76972_CMD_TEMP_START, &internal_temp, 1))
163 return LOG_STR(ESP_LOG_MSG_COMM_FAIL);
167 bool regs_disabled =
true;
169 return LOG_STR(
"Reading regulator config failed");
170 const bool write_regs = !regs_disabled;
172 bool write_thermistors =
false;
174 for (
size_t i = 1; i < BQ76972_NUM_TEMPERATURES; i++) {
176 write_thermistors =
true;
180 if (write_regs || write_thermistors) {
182 return LOG_STR(
"Entering config update mode failed");
186 error = LOG_STR(
"Exiting config update mode failed");
187 if (error !=
nullptr)
189 if (write_regs && (!this->
regs_disabled_(regs_disabled) || !regs_disabled))
190 return LOG_STR(
"Disabling regulators failed");
193 ESP_LOGD(TAG,
"Internal temperature: %.1f°C", raw_to_celsius(internal_temp));
199 const uint8_t zero = 0;
202 return LOG_STR(
"Writing regulator config failed");
205 for (
size_t i = 1; i < BQ76972_NUM_TEMPERATURES; i++) {
208 if (!this->
write_subcommand_(BQ76972_MEM_TEMP_PIN_CONFIG_BASE + i, &BQ76972_PIN_CONFIG_THERMISTOR, 1))
209 return LOG_STR(
"Writing thermistor pin config failed");
216 if (
const LogString *error = this->
configure_(); error !=
nullptr)
221 ESP_LOGCONFIG(TAG,
"BQ76972:");
222 LOG_I2C_DEVICE(
this);
223 LOG_UPDATE_INTERVAL(
this);
226 " Regulators disabled: %s",
227 this->
crc_mode_ ? LOG_STR_LITERAL(
"Enabled") : LOG_STR_LITERAL(
"Disabled"), YESNO(this->
reg_disable_));
233 const uint8_t shifted =
address << 1;
250 ESP_LOGI(TAG,
"I2C address set to 0x%02X",
address);
254template<
size_t N>
static bool any_set(
const std::array<sensor::Sensor *, N> &sensors) {
255 for (
auto *sensor : sensors) {
256 if (sensor !=
nullptr)
265 uint16_t words[BQ76972_NUM_CELLS];
266 if (this->stack_voltage_sensor_ !=
nullptr) {
267 if (this->
read_words_(BQ76972_CMD_STACK_VOLTAGE, words, 1)) {
269 this->stack_voltage_sensor_->publish_state(words[0] * 0.01f);
274 if (any_set(this->cell_sensors_)) {
275 if (this->
read_words_(BQ76972_CMD_CELL_START, words, BQ76972_NUM_CELLS)) {
276 for (
size_t i = 0; i < BQ76972_NUM_CELLS; i++) {
277 if (this->cell_sensors_[i] !=
nullptr && words[i] != 0)
278 this->cell_sensors_[i]->publish_state(words[i] * 0.001f);
285 if (this->
read_words_(BQ76972_CMD_TEMP_START, words, BQ76972_NUM_TEMPERATURES)) {
286 for (
size_t i = 0; i < BQ76972_NUM_TEMPERATURES; i++) {
void mark_failed()
Mark this component as failed.
void status_set_warning()
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool read_block_(uint8_t start_register, uint8_t *data, size_t len)
bool wait_for_subcommand_()
void dump_config() override
bool read_subcommand_(uint16_t subcommand, uint8_t *data, size_t len)
bool wait_for_cfgupdate_()
bool read_words_(uint8_t start_register, uint16_t *data, size_t num_words)
const LogString * write_config_(bool write_regs)
SUB_SENSOR(stack_voltage) protected std::array< sensor::Sensor *, BQ76972_NUM_TEMPERATURES > temperature_sensors_
bool write_subcommand_(uint16_t subcommand, const uint8_t *data, size_t len)
bool write_block_(uint8_t start_register, const uint8_t *data, size_t len)
void program_i2c_address(uint8_t address)
const LogString * configure_()
bool regs_disabled_(bool &out)
bool send_subcommand_(uint16_t subcommand)
uint8_t address_
store the address of the device on the bus
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
std::vector< uint8_t > & out
ESPHOME_LOG_TAG(TAG, "bq76972")
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...
constexpr T convert_little_endian(T val)
Convert a value between host byte order and little endian (least significant byte first) order.
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.
void HOT delay(uint32_t ms)