ESPHome 2025.5.0
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 ESP_LOGCONFIG(TAG, "Setting up MAX17043...");
45
46 uint16_t config_reg;
47 if (this->write(&MAX17043_CONFIG, 1) != i2c::ERROR_OK) {
48 this->status_set_warning();
49 return;
50 }
51
52 if (this->read(reinterpret_cast<uint8_t *>(&config_reg), 2) != i2c::ERROR_OK) {
53 this->status_set_warning();
54 return;
55 }
56
57 config_reg = i2c::i2ctohs(config_reg) & MAX17043_CONFIG_SAFE_MASK;
58 ESP_LOGV(TAG, "MAX17043 CONFIG register reads 0x%X", config_reg);
59
60 if (config_reg != MAX17043_CONFIG_POWER_UP_DEFAULT) {
61 ESP_LOGE(TAG, "Device does not appear to be a MAX17043");
62 this->status_set_error("unrecognised");
63 this->mark_failed();
64 return;
65 }
66
67 // need to write back to config register to reset the sleep bit
68 if (!this->write_byte_16(MAX17043_CONFIG, MAX17043_CONFIG_POWER_UP_DEFAULT)) {
69 this->status_set_error("sleep reset failed");
70 this->mark_failed();
71 return;
72 }
73}
74
76 ESP_LOGCONFIG(TAG, "MAX17043:");
77 LOG_I2C_DEVICE(this);
78 if (this->is_failed()) {
79 ESP_LOGE(TAG, "Communication with MAX17043 failed");
80 }
81 LOG_UPDATE_INTERVAL(this);
82 LOG_SENSOR(" ", "Battery Voltage", this->voltage_sensor_);
83 LOG_SENSOR(" ", "Battery Level", this->battery_remaining_sensor_);
84}
85
87
89 if (!this->is_failed()) {
90 if (!this->write_byte_16(MAX17043_CONFIG, MAX17043_CONFIG_POWER_UP_DEFAULT | MAX17043_CONFIG_SLEEP_MASK)) {
91 ESP_LOGW(TAG, "Unable to write the sleep bit to config register");
92 this->status_set_warning();
93 }
94 }
95}
96
97} // namespace max17043
98} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message="unspecified")
void status_set_error(const char *message="unspecified")
void status_clear_warning()
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:86
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
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:19
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7