ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
max17043.cpp
Go to the documentation of this file.
1#include "max17043.h"
2#include "esphome/core/log.h"
3
4namespace esphome::max17043 {
5
6// MAX174043 is a 1-Cell Fuel Gauge with ModelGauge and Low-Battery Alert
7// Consult the datasheet at https://www.analog.com/en/products/max17043.html
8
9static const char *const TAG = "max17043";
10
11static const uint8_t MAX17043_VCELL = 0x02;
12static const uint8_t MAX17043_SOC = 0x04;
13static const uint8_t MAX17043_CONFIG = 0x0c;
14
15static const uint16_t MAX17043_CONFIG_POWER_UP_DEFAULT = 0x971C;
16static const uint16_t MAX17043_CONFIG_SAFE_MASK = 0xFF1F; // mask out sleep bit (7), unused bit (6) and alert bit (4)
17static const uint16_t MAX17043_CONFIG_SLEEP_MASK = 0x0080;
18
20 uint16_t raw_voltage, raw_percent;
21
22 if (this->voltage_sensor_ != nullptr) {
23 if (!this->read_byte_16(MAX17043_VCELL, &raw_voltage)) {
24 this->status_set_warning(LOG_STR("Unable to read MAX17043_VCELL"));
25 } else {
26 float voltage = (1.25 * (float) (raw_voltage >> 4)) / 1000.0;
27 this->voltage_sensor_->publish_state(voltage);
29 }
30 }
31 if (this->battery_remaining_sensor_ != nullptr) {
32 if (!this->read_byte_16(MAX17043_SOC, &raw_percent)) {
33 this->status_set_warning(LOG_STR("Unable to read MAX17043_SOC"));
34 } else {
35 float percent = (float) ((raw_percent >> 8) + 0.003906f * (raw_percent & 0x00ff));
38 }
39 }
40}
41
43 uint16_t config_reg;
44 if (this->write(&MAX17043_CONFIG, 1) != i2c::ERROR_OK) {
45 this->status_set_warning();
46 return;
47 }
48
49 if (this->read(reinterpret_cast<uint8_t *>(&config_reg), 2) != i2c::ERROR_OK) {
50 this->status_set_warning();
51 return;
52 }
53
54 config_reg = i2c::i2ctohs(config_reg) & MAX17043_CONFIG_SAFE_MASK;
55 ESP_LOGV(TAG, "MAX17043 CONFIG register reads 0x%X", config_reg);
56
57 if (config_reg != MAX17043_CONFIG_POWER_UP_DEFAULT) {
58 ESP_LOGE(TAG, "Device does not appear to be a MAX17043");
59 this->status_set_error(LOG_STR("unrecognised"));
60 this->mark_failed();
61 return;
62 }
63
64 // need to write back to config register to reset the sleep bit
65 if (!this->write_byte_16(MAX17043_CONFIG, MAX17043_CONFIG_POWER_UP_DEFAULT)) {
66 this->status_set_error(LOG_STR("sleep reset failed"));
67 this->mark_failed();
68 return;
69 }
70}
71
73 ESP_LOGCONFIG(TAG, "MAX17043:");
74 LOG_I2C_DEVICE(this);
75 if (this->is_failed()) {
76 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
77 }
78 LOG_UPDATE_INTERVAL(this);
79 LOG_SENSOR(" ", "Battery Voltage", this->voltage_sensor_);
80 LOG_SENSOR(" ", "Battery Level", this->battery_remaining_sensor_);
81}
82
84 if (!this->is_failed()) {
85 if (!this->write_byte_16(MAX17043_CONFIG, MAX17043_CONFIG_POWER_UP_DEFAULT | MAX17043_CONFIG_SLEEP_MASK)) {
86 ESP_LOGW(TAG, "Unable to write the sleep bit to config register");
87 this->status_set_warning();
88 }
89 }
90}
91
92} // namespace esphome::max17043
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
void status_clear_warning()
Definition component.h:306
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:249
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.h:267
sensor::Sensor * battery_remaining_sensor_
Definition max17043.h:23
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
u_int8_t raw_voltage
uint16_t i2ctohs(uint16_t i2cshort)
Definition i2c.h:127
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14