ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
max170xx.cpp
Go to the documentation of this file.
1#include "max170xx.h"
2#include "esphome/core/log.h"
3
5
6ESPHOME_LOG_TAG(TAG, "max170xx");
7
8template<Model MODEL> void MAX170xxComponent<MODEL>::setup() {
9 // A silent bus is retried from update(): some boards gate the I2C rail until after setup.
10 // An early on_boot may already have configured the gauge and put it to sleep, so this must not configure again.
11 this->ready_for_update_();
12}
13
14template<Model MODEL> bool MAX170xxComponent<MODEL>::ready_for_update_() {
15 if (this->configured_ || this->configure_())
16 return true;
17 this->status_set_warning(LOG_STR("Gauge did not answer"));
18 return false;
19}
20
21template<Model MODEL> bool MAX170xxComponent<MODEL>::configure_() {
22 uint16_t config;
23 if (!this->read_byte_16(REG_CONFIG, &config))
24 return false;
25
26 const bool was_asleep = (config & CONFIG_SLEEP) != 0;
27 // RCOMP and ATHD survive on the battery, so an earlier firmware's values are reset here, not treated as a fault
28 if ((config & CONFIG_SAFE_MASK) != CONFIG_POWER_UP_DEFAULT) {
29 ESP_LOGW(TAG, "CONFIG reads 0x%04X, expected 0x%04X; resetting it", config & CONFIG_SAFE_MASK,
30 CONFIG_POWER_UP_DEFAULT);
31 }
32
33 uint16_t version;
34 if (this->read_byte_16(REG_VERSION, &version)) {
35 ESP_LOGCONFIG(TAG, "IC version: 0x%04X", version);
36 }
37
38 // Write back the power-up value to clear a stale sleep bit; a failed write is retried like a silent bus
39 if (!this->write_byte_16(REG_CONFIG, CONFIG_POWER_UP_DEFAULT))
40 return false;
41
42 this->configured_ = true;
43 // Waking from sleep raises a false low-battery alert; a real one at an ordinary boot is left alone
44 if (was_asleep)
45 this->set_timeout(WAKE_ALERT_CLEAR_TIMEOUT_ID, WAKE_ALERT_CLEAR_DELAY_MS, [this]() { this->clear_wake_alert_(); });
46 return true;
47}
48
49template<Model MODEL> void MAX170xxComponent<MODEL>::clear_wake_alert_() {
50 uint16_t config;
51 if (!this->read_byte_16(REG_CONFIG, &config) || !this->write_byte_16(REG_CONFIG, config & ~CONFIG_ALRT)) {
52 this->status_set_warning(LOG_STR("Wake alert clear failed"));
53 return;
54 }
55 if constexpr (MODEL == Model::MODEL_MAX17048) {
56 uint16_t status;
57 if (!this->read_byte_16(REG_STATUS, &status) || !this->write_byte_16(REG_STATUS, status & ~STATUS_HD))
58 this->status_set_warning(LOG_STR("Wake alert clear failed"));
59 }
60}
61
62template<Model MODEL> void MAX170xxComponent<MODEL>::sleep_mode() {
63 if (this->is_failed())
64 return;
65 // The deferred configuration clears the sleep bit, so it has to run before the gauge is put to sleep
66 if (!this->ready_for_update_())
67 return;
68
69 // A clear still pending would otherwise run against a sleeping gauge
70 if (this->cancel_timeout(WAKE_ALERT_CLEAR_TIMEOUT_ID))
71 this->clear_wake_alert_();
72
73 if constexpr (MODEL == Model::MODEL_MAX17048) {
74 if (!this->write_byte_16(REG_MODE, MODE_EN_SLEEP)) {
75 this->status_set_warning(LOG_STR("Unable to enable sleep mode in the mode register"));
76 return;
77 }
78 }
79
80 if (!this->write_byte_16(REG_CONFIG, CONFIG_POWER_UP_DEFAULT | CONFIG_SLEEP))
81 this->status_set_warning(LOG_STR("Unable to write the sleep bit to config register"));
82}
83
86
87} // namespace esphome::max170xx
uint8_t status
Definition bl0942.h:8
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
ESPHOME_LOG_TAG(TAG, "max170xx")