ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
max170xx.h
Go to the documentation of this file.
1#pragma once
2
5
6namespace esphome::max170xx {
7
8// Shared base for the ModelGauge fuel gauges.
9// MAX17043: https://www.analog.com/en/products/max17043.html
10// MAX17048: https://www.analog.com/en/products/max17048.html
11// The MAX17048 is register-compatible with the MAX17043, and adds a STATUS register and a MODE.EnSleep gate in
12// front of CONFIG.SLEEP.
13
14static constexpr uint8_t REG_VCELL = 0x02;
15static constexpr uint8_t REG_SOC = 0x04;
16static constexpr uint8_t REG_MODE = 0x06;
17static constexpr uint8_t REG_VERSION = 0x08;
18static constexpr uint8_t REG_CONFIG = 0x0C;
19static constexpr uint8_t REG_STATUS = 0x1A;
20
21static constexpr uint16_t CONFIG_POWER_UP_DEFAULT = 0x971C;
22// Mask out SLEEP (7), ALRT (5), and bit 6, which is a don't-care on the MAX17043 but ALSC on the MAX17048.
23static constexpr uint16_t CONFIG_SAFE_MASK = 0xFF1F;
24static constexpr uint16_t CONFIG_SLEEP = 0x0080;
25static constexpr uint16_t CONFIG_ALRT = 0x0020;
26
27// CONFIG.SLEEP is ignored unless MODE.EnSleep (bit 13) is set first.
28static constexpr uint16_t MODE_EN_SLEEP = 0x2000;
29static constexpr uint16_t STATUS_HD = 0x1000;
30
31static constexpr uint32_t WAKE_ALERT_CLEAR_TIMEOUT_ID = 0;
32// The gauge raises its false wake alert some time after the wake write; on hardware it rose 0.8 to 1.0 s later
33static constexpr uint32_t WAKE_ALERT_CLEAR_DELAY_MS = 2000;
34
35enum class Model : uint8_t {
38};
39
41template<Model MODEL> class MAX170xxComponent : public PollingComponent, public i2c::I2CDevice {
42 public:
43 void setup() override;
44 void sleep_mode();
45
46 protected:
49 bool ready_for_update_();
51 bool configure_();
53 void clear_wake_alert_();
54
55 bool configured_{false};
56};
57
58extern template class MAX170xxComponent<Model::MODEL_MAX17043>;
59extern template class MAX170xxComponent<Model::MODEL_MAX17048>;
60
61} // namespace esphome::max170xx
This class simplifies creating components that periodically check a state.
Definition component.h:519
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
Defined in max170xx.cpp, which instantiates both models.
Definition max170xx.h:41
void clear_wake_alert_()
Clear the false alert raised on waking, leaving every other CONFIG and STATUS bit as it is.
Definition max170xx.cpp:49
bool configure_()
Verify and wake the gauge; false if it did not answer.
Definition max170xx.cpp:21
bool ready_for_update_()
True once the gauge is configured; otherwise retries the configuration and warns if the gauge did not...
Definition max170xx.cpp:14
static void uint32_t