ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
deep_sleep_bk72xx.cpp
Go to the documentation of this file.
1#ifdef USE_BK72XX
2
4#include "esphome/core/log.h"
5
7
8ESPHOME_LOG_TAG(TAG, "deep_sleep");
9
11 switch (lt_get_reboot_reason()) {
12 case REBOOT_REASON_SLEEP_GPIO:
13 return WAKEUP_CAUSE_GPIO;
14 case REBOOT_REASON_SLEEP_RTC:
15 return WAKEUP_CAUSE_TIMER;
16 case REBOOT_REASON_SLEEP_USB:
18 default:
19 return WAKEUP_CAUSE_NONE;
20 }
21}
22
23optional<uint32_t> DeepSleepComponent::get_run_duration_() const { return this->run_duration_; }
24
26 for (const WakeUpPinItem &item : this->wakeup_pins_) {
27 LOG_PIN(" Wakeup Pin: ", item.wakeup_pin);
28 }
29}
30
32 // Apply a configured pull. Without it the pins float and the wake-up level is chosen
33 // from a bogus reading. A plain input is left alone (reading the pin already makes it
34 // one), so a pull set by another component sharing the pin is not undone.
35 for (WakeUpPinItem &item : this->wakeup_pins_) {
36 if (item.wakeup_pin->get_flags() & (gpio::FLAG_PULLUP | gpio::FLAG_PULLDOWN))
37 item.wakeup_pin->setup();
38 }
39}
40
42 return (pin_item.wakeup_pin_mode == WAKEUP_PIN_MODE_KEEP_AWAKE && pin_item.wakeup_pin != nullptr &&
43 !this->sleep_duration_.has_value() && (pin_item.wakeup_level == get_real_pin_state_(*pin_item.wakeup_pin)));
44}
45
47 // deep_sleep.enter can run before setup(), e.g. from on_boot at its default priority
48 if (!this->is_ready())
49 this->setup_wakeup_pins_();
50
51 if (!this->wakeup_pins_.empty()) {
52 for (WakeUpPinItem &item : this->wakeup_pins_) {
53 if (this->pin_prevents_sleep_(item)) {
54 // Defer deep sleep until inactive
55 if (!this->next_enter_deep_sleep_) {
56 this->status_set_warning();
57 ESP_LOGV(TAG, "Waiting for pin to switch state to enter deep sleep...");
58 }
59 this->defer_sleep_();
60 return false;
61 }
62 }
63 }
64 return true;
65}
66
68 for (WakeUpPinItem &item : this->wakeup_pins_) {
69 if (item.wakeup_pin_mode == WAKEUP_PIN_MODE_INVERT_WAKEUP) {
70 if (item.wakeup_level == get_real_pin_state_(*item.wakeup_pin)) {
71 item.wakeup_level = !item.wakeup_level;
72 }
73 }
74 ESP_LOGI(TAG, "Wake-up on P%u %s (%" PRId32 ")", item.wakeup_pin->get_pin(), item.wakeup_level ? "HIGH" : "LOW",
75 static_cast<int32_t>(item.wakeup_pin_mode));
76 }
77
78 if (this->sleep_duration_.has_value())
79 lt_deep_sleep_config_timer((*this->sleep_duration_ / 1000) & 0xFFFFFFFF);
80
81 for (WakeUpPinItem &item : this->wakeup_pins_) {
82 lt_deep_sleep_config_gpio(1 << item.wakeup_pin->get_pin(), item.wakeup_level);
83 lt_deep_sleep_keep_floating_gpio(1 << item.wakeup_pin->get_pin(), true);
84 }
85
86 lt_deep_sleep_enter();
87}
88
90
91} // namespace esphome::deep_sleep
92
93#endif // USE_BK72XX
bool is_ready() const
bool pin_prevents_sleep_(WakeUpPinItem &pin_item) const
SlotList< WakeUpPinItem, DEEP_SLEEP_WAKEUP_PIN_COUNT > wakeup_pins_
optional< uint32_t > get_run_duration_() const
bool get_real_pin_state_(InternalGPIOPin &pin) const
@ WAKEUP_PIN_MODE_KEEP_AWAKE
As long as the wakeup pin is still in the wakeup state, keep awake.
@ WAKEUP_PIN_MODE_INVERT_WAKEUP
Automatically invert the wakeup level.
ESPHOME_LOG_TAG(TAG, "deep_sleep")
WakeupCause get_wakeup_cause()
Return why the device woke from deep sleep. Implemented per platform.
WakeupCause
Why the device woke from deep sleep. Passed to on_wake automations.
@ WAKEUP_CAUSE_GPIO
The device was woken by a GPIO pin (wakeup_pin or esp32_ext1_wakeup).
@ WAKEUP_CAUSE_TIMER
The device was woken by the sleep timer.
@ WAKEUP_CAUSE_UNKNOWN
The device woke from deep sleep, but the source could not be identified.
@ WAKEUP_CAUSE_NONE
The device did not wake from deep sleep (for example a cold boot, reset or OTA restart).