ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
tps65185.cpp
Go to the documentation of this file.
1#include "tps65185.h"
2
5#include "esphome/core/hal.h"
6#include "esphome/core/log.h"
7
9
10ESPHOME_LOG_TAG(TAG, "tps65185");
11
12static constexpr uint8_t REG_TMST_VALUE = 0x00;
13static constexpr uint8_t REG_ENABLE = 0x01;
14static constexpr uint8_t REG_VCOM1 = 0x03;
15static constexpr uint8_t REG_VCOM2 = 0x04;
16static constexpr uint8_t REG_INT1 = 0x07;
17static constexpr uint8_t REG_INT2 = 0x08;
18static constexpr uint8_t REG_TMST1 = 0x0D;
19static constexpr uint8_t REG_PG = 0x0F;
20static constexpr uint8_t REG_REVID = 0x10;
21
22// V3P3_EN, VCOM_EN, VDDH_EN, VPOS_EN, VEE_EN and VNEG_EN
23static constexpr uint8_t ENABLE_ALL_RAILS = 0x3F;
24// The PWR_GOOD pin covers VDDH, VPOS, VEE and VNEG
25static constexpr uint8_t PG_PIN_RAILS = 0x5A;
26// PWR_GOOD rails plus the two boost converters
27static constexpr uint8_t PG_ALL_RAILS = 0xFA;
28// VCOM2 bit 2 is unused and resets to 1
29static constexpr uint8_t VCOM2_RESET_BITS = 0x04;
30static constexpr uint8_t TMST1_READ_THERM = 0x80;
31static constexpr uint8_t TMST1_CONV_END = 0x20;
32
33// The datasheet minimum from WAKEUP high to I2C is 1.8 ms; millis() counts whole milliseconds
34static constexpr uint32_t WAKEUP_DELAY_MS = 3;
35static constexpr uint32_t POWER_UP_TIMEOUT_MS = 500;
36static constexpr uint32_t CONVERSION_TIMEOUT_MS = 100;
37// The default power-down sequence (3 + 6 + 24 + 48 ms)
38static constexpr uint32_t POWER_DOWN_MS = 81;
39// The power-down sequence and the 100 ms discharge delay, which end before the chip sleeps and resets its registers
40static constexpr uint32_t SLEEP_ENTRY_MS = POWER_DOWN_MS + 100;
41
43 setup_output_pin(this->pwrup_pin_, false);
44 setup_output_pin(this->vcom_ctrl_pin_, false);
45 setup_output_pin(this->wakeup_pin_, true);
46 if (this->power_good_pin_ != nullptr)
47 this->power_good_pin_->setup();
48 delay(WAKEUP_DELAY_MS);
49 const bool ok = this->read_byte(REG_REVID, &this->revision_);
50 // After a restart that skipped the shutdown hooks the rails may still be on, so the wake waits for their discharge
51 this->sleep_();
52 // loop() disables itself when idle; a request made before setup() keeps it running
53 this->setup_check(ok);
54}
55
57 ESP_LOGCONFIG(TAG,
58 "TPS65185:\n"
59 " Revision: 0x%02X\n"
60 " VCOM: -%u.%02u V",
61 this->revision_, this->vcom_ / 100, this->vcom_ % 100);
62 LOG_I2C_DEVICE(this);
63 LOG_PIN(" WAKEUP Pin: ", this->wakeup_pin_);
64 LOG_PIN(" PWRUP Pin: ", this->pwrup_pin_);
65 LOG_PIN(" VCOM_CTRL Pin: ", this->vcom_ctrl_pin_);
66 LOG_PIN(" PWR_GOOD Pin: ", this->power_good_pin_);
67}
68
70 if (this->is_failed() || !this->power_requested_)
71 return true;
72 this->tearing_down_ = true;
73 // Teardown passes don't move the loop clock on, which loop() times the power sequence by
74 LoopBlockingGuard clock{this, LOG_STR("teardown"), millis()};
75 this->loop();
76 return false;
77}
78
80 this->power_requested_ = false;
81 this->power_down_();
82 this->sleep_();
83}
84
86 if (on)
87 this->power_failed_ = false;
88 this->power_requested_ = on;
89 this->start_work_();
90}
91
101
103 // A failed component never runs loop(), which would release the request
104 if (this->is_failed())
105 return;
106 // The loop interval could otherwise hold back the first step
107 this->high_freq_.start();
108 this->enable_loop();
109}
110
113 switch (this->state_) {
116 break;
117 if (now - this->state_start_ < SLEEP_ENTRY_MS)
118 break;
119 this->wakeup_pin_->digital_write(true);
120 // An I/O expander write can take a while, so the wait starts after it, not at the cached loop time
121 this->state_start_ = millis();
123 break;
124
126 if (millis() - this->state_start_ < WAKEUP_DELAY_MS)
127 break;
128 // Sleep resets the registers, so VCOM is written after every wake up
129 if (!this->write_vcom_()) {
130 if (this->power_requested_)
131 this->power_failed_ = true;
132 this->power_requested_ = false;
133 this->sleep_();
134 // A logger callback may request power again, so the warning comes after the state changes
135 this->status_set_warning(LOG_STR("VCOM write failed"));
136 this->fail_temperature_();
137 break;
138 }
140 break;
141
143 if (this->handle_temperature_(now))
144 break;
145 if (!this->power_requested_) {
146 this->sleep_();
147 break;
148 }
149 this->pwrup_pin_->digital_write(true);
150 this->vcom_ctrl_pin_->digital_write(true);
151 this->state_start_ = now;
153 break;
154
156 // A power-up started before the sequence ends would read power good from rails still going down
157 if (this->power_requested_ && now - this->state_start_ < POWER_DOWN_MS)
158 break;
160 break;
161
164 if (!this->power_requested_) {
165 this->power_down_();
166 break;
167 }
168 if (this->power_good_()) {
170 ESP_LOGV(TAG, "Rails up in %" PRIu32 " ms", now - this->state_start_);
171 this->status_clear_warning();
173 break;
174 }
175 // Also turns on the 3.3 V switch, which the power-up sequence leaves off. A failed write is tried again
176 // until the power-up timeout.
177 if (this->write_byte(REG_ENABLE, ENABLE_ALL_RAILS)) {
179 break;
180 }
181 }
182 if (now - this->state_start_ >= POWER_UP_TIMEOUT_MS)
183 this->power_fault_();
184 break;
185
187 // A reading in progress continues after the power down; power off does not wait for it
188 if (this->power_requested_ && this->handle_temperature_(now))
189 break;
190 // A sensor callback run by handle_temperature_() may have asked to turn the power off
191 if (!this->power_requested_)
192 this->power_down_();
193 break;
194 }
197 (this->state_ == ChipState::CHIP_STATE_ACTIVE && this->power_requested_));
198 this->high_freq_.set(!idle);
199 if (idle)
200 this->disable_loop();
201}
202
204 uint8_t mask = PG_ALL_RAILS;
206 // An expander's cached input is not refreshed during teardown, as its loop() no longer runs
207 if (this->power_good_pin_ != nullptr && !this->tearing_down_)
208 return this->power_good_pin_->digital_read();
209 mask = PG_PIN_RAILS;
210 }
211 uint8_t pg;
212 return this->read_byte(REG_PG, &pg) && (pg & mask) == mask;
213}
214
216 return this->write_byte(REG_VCOM1, this->vcom_ & 0xFF) &&
217 this->write_byte(REG_VCOM2, VCOM2_RESET_BITS | (this->vcom_ >> 8));
218}
219
226
228 uint8_t pg;
229 uint8_t int1;
230 uint8_t int2;
231 const bool read =
232 this->read_byte(REG_PG, &pg) && this->read_byte(REG_INT1, &int1) && this->read_byte(REG_INT2, &int2);
233 this->power_requested_ = false;
234 this->power_failed_ = true;
235 this->power_down_();
236 // Sleep resets the fault bits even when reading INT1 and INT2 failed, so the next power up starts clean
237 this->sleep_();
238 // Logged after the state changes, as a logger callback may request power again
239 if (read) {
240 ESP_LOGW(TAG, "Power up timed out: PG 0x%02X, INT1 0x%02X, INT2 0x%02X", pg, int1, int2);
241 } else {
242 ESP_LOGW(TAG, "Power up timed out; status registers unreadable");
243 }
244 this->status_set_warning(LOG_STR("Power up failed"));
245}
246
248 // The chip finishes discharging the rails before it sleeps
249 this->wakeup_pin_->digital_write(false);
252}
253
254#ifdef USE_TPS65185_TEMPERATURE
255void TPS65185::request_temperature() {
256 // The define is set for every hub when any of them has a sensor
257 if (this->temperature_sensor_ == nullptr)
258 return;
259 if (this->is_failed()) {
260 // Marking a reading as pending makes a retry from a state callback return here without recursing
262 return;
264 this->temperature_sensor_->publish_state(NAN);
266 return;
267 }
270 this->start_work_();
271}
272
273void TPS65185TemperatureSensor::dump_config() { LOG_SENSOR("", "TPS65185 Temperature", this); }
274#endif
275
277#ifdef USE_TPS65185_TEMPERATURE
279 return;
280 // Reset first, so a callback run by publish_state() can request a new reading
282 this->status_set_warning(LOG_STR("Temperature read failed"));
283 this->temperature_sensor_->publish_state(NAN);
284#endif
285}
286
288#ifdef USE_TPS65185_TEMPERATURE
289 switch (this->temperature_read_) {
291 return false;
292
294 if (!this->write_byte(REG_TMST1, TMST1_READ_THERM))
295 break;
296 this->state_start_ = now;
298 return true;
299
301 uint8_t tmst1;
302 if (!this->read_byte(REG_TMST1, &tmst1))
303 break;
304 if ((tmst1 & TMST1_CONV_END) == 0) {
305 if (now - this->state_start_ < CONVERSION_TIMEOUT_MS)
306 return true;
307 break;
308 }
309 uint8_t value;
310 if (!this->read_byte(REG_TMST_VALUE, &value))
311 break;
313 if (!this->power_failed_)
314 this->status_clear_warning();
315 this->temperature_sensor_->publish_state(static_cast<int8_t>(value));
316 return false;
317 }
318 }
319 this->fail_temperature_();
320#endif
321 return false;
322}
323
324} // namespace esphome::tps65185
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
bool is_failed() const
Definition component.h:288
void enable_loop()
Enable this component's loop.
Definition component.h:262
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
void disable_loop()
Disable this component's loop.
void status_clear_warning()
Definition component.h:318
virtual void setup()=0
virtual void digital_write(bool value)=0
virtual bool digital_read()=0
void set(bool active)
Run the loop continuously while active is true. Repeating the current value does nothing.
Definition helpers.cpp:863
void start()
Start running the loop continuously.
Definition helpers.h:2606
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:175
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
TemperatureRead temperature_read_
Definition tps65185.h:85
HighFrequencyLoopRequester high_freq_
Definition tps65185.h:89
void request_power(bool on)
Definition tps65185.cpp:85
void on_powerdown() override
Definition tps65185.cpp:79
PowerState get_power_state() const
Definition tps65185.cpp:92
bool teardown() override
Definition tps65185.cpp:69
bool handle_temperature_(uint32_t now)
Definition tps65185.cpp:287
void dump_config() override
Definition tps65185.cpp:56
ESPHOME_LOG_TAG(TAG, "tps65185")
uint32_t value
Definition hal.cpp:59
void setup_output_pin(GPIOPin *pin, bool level)
Set up pin and drive it to level. Does nothing when pin is null, so an optional pin needs no check.
Definition gpio.cpp:43
void HOT delay(uint32_t ms)
Definition hal.cpp:78
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t