ESPHome 2025.9.0-dev
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 {
5namespace max17043 {
6
7// MAX174043 is a 1-Cell Fuel Gauge with ModelGauge and Low-Battery Alert
8// Consult the datasheet at https://www.analog.com/en/products/max17043.html
9
10static const char *const TAG = "max17043";
11
12static const uint8_t MAX17043_VCELL = 0x02;
13static const uint8_t MAX17043_SOC = 0x04;
14static const uint8_t MAX17043_CONFIG = 0x0c;
15
16static const uint16_t MAX17043_CONFIG_POWER_UP_DEFAULT = 0x971C;
17static const uint16_t MAX17043_CONFIG_SAFE_MASK = 0xFF1F; // mask out sleep bit (7), unused bit (6) and alert bit (4)
18static const uint16_t MAX17043_CONFIG_SLEEP_MASK = 0x0080;
19
21 uint16_t raw_voltage, raw_percent;
22
23 if (this->voltage_sensor_ != nullptr) {
24 if (!this->read_byte_16(MAX17043_VCELL, &raw_voltage)) {
25 this->status_set_warning("Unable to read MAX17043_VCELL");
26 } else {
27 float voltage = (1.25 * (float) (raw_voltage >> 4)) / 1000.0;
28 this->voltage_sensor_->publish_state(voltage);
30 }
31 }
32 if (this->battery_remaining_sensor_ != nullptr) {
33 if (!this->read_byte_16(MAX17043_SOC, &raw_percent)) {
34 this->status_set_warning("Unable to read MAX17043_SOC");
35 } else {
36 float percent = (float) ((raw_percent >> 8) + 0.003906f * (raw_percent & 0x00ff));
39 }
40 }
41}
42
44 uint16_t config_reg;
45 if (this->write(&MAX17043_CONFIG, 1) != i2c::ERROR_OK) {
46 this->status_set_warning();
47 return;
48 }
49
50 if (this->read(reinterpret_cast<uint8_t *>(&config_reg), 2) != i2c::ERROR_OK) {
51 this->status_set_warning();
52 return;
53 }
54
55 config_reg = i2c::i2ctohs(config_reg) & MAX17043_CONFIG_SAFE_MASK;
56 ESP_LOGV(TAG, "MAX17043 CONFIG register reads 0x%X", config_reg);
57
58 if (config_reg != MAX17043_CONFIG_POWER_UP_DEFAULT) {
59 ESP_LOGE(TAG, "Device does not appear to be a MAX17043");
60 this->status_set_error("unrecognised");
61 this->mark_failed();
62 return;
63 }
64
65 // need to write back to config register to reset the sleep bit
66 if (!this->write_byte_16(MAX17043_CONFIG, MAX17043_CONFIG_POWER_UP_DEFAULT)) {
67 this->status_set_error("sleep reset failed");
68 this->mark_failed();
69 return;
70 }
71}
72
74 ESP_LOGCONFIG(TAG, "MAX17043:");
75 LOG_I2C_DEVICE(this);
76 if (this->is_failed()) {
77 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
78 }
79 LOG_UPDATE_INTERVAL(this);
80 LOG_SENSOR(" ", "Battery Voltage", this->voltage_sensor_);
81 LOG_SENSOR(" ", "Battery Level", this->battery_remaining_sensor_);
82}
83
85
87 if (!this->is_failed()) {
88 if (!this->write_byte_16(MAX17043_CONFIG, MAX17043_CONFIG_POWER_UP_DEFAULT | MAX17043_CONFIG_SLEEP_MASK)) {
89 ESP_LOGW(TAG, "Unable to write the sleep bit to config register");
90 this->status_set_warning();
91 }
92 }
93}
94
95} // namespace max17043
96} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
void status_set_error(const char *message=nullptr)
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition i2c.h:190
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:250
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition i2c.h:270
sensor::Sensor * battery_remaining_sensor_
Definition max17043.h:25
float get_setup_priority() const override
Definition max17043.cpp:84
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
u_int8_t raw_voltage
uint16_t i2ctohs(uint16_t i2cshort)
Definition i2c.h:128
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7