ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
deep_sleep_esp32.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32
2#include "soc/soc_caps.h"
3#include "driver/gpio.h"
5#include "esphome/core/log.h"
6#include <esp_idf_version.h>
7
8namespace esphome::deep_sleep {
9
10// Deep Sleep feature support matrix for ESP32 variants:
11//
12// | Variant | ext0 | ext1 | Touch | GPIO wakeup |
13// |-----------|------|------|-------|-------------|
14// | ESP32 | ✓ | ✓ | ✓ | |
15// | ESP32-S2 | ✓ | ✓ | ✓ | |
16// | ESP32-S3 | ✓ | ✓ | ✓ | |
17// | ESP32-C2 | | | | ✓ |
18// | ESP32-C3 | | | | ✓ |
19// | ESP32-C5 | | ✓ | | ✓ |
20// | ESP32-C6 | | ✓ | | ✓ |
21// | ESP32-C61 | | ✓ | | ✓ |
22// | ESP32-H2 | | ✓ | | |
23// | ESP32-P4 | | ✓ | ✓ | ✓ |
24//
25// Each wakeup source below is gated on the SOC capability macro for it, so a
26// new variant gets the sources its hardware has.
27//
28// Notes:
29// - (✓) = Supported by hardware but not yet implemented in ESPHome
30// - ext0: Single pin wakeup using RTC GPIO (esp_sleep_enable_ext0_wakeup)
31// - ext1: Multiple pin wakeup (esp_sleep_enable_ext1_wakeup)
32// - Touch: Touch pad wakeup (esp_sleep_enable_touchpad_wakeup)
33// - GPIO wakeup: GPIO wakeup for RTC pins
34
35ESPHOME_LOG_TAG(TAG, "deep_sleep");
36
38 switch (esp_sleep_get_wakeup_cause()) {
39 case ESP_SLEEP_WAKEUP_EXT0:
40 case ESP_SLEEP_WAKEUP_EXT1:
41 case ESP_SLEEP_WAKEUP_GPIO:
42 return WAKEUP_CAUSE_GPIO;
43 case ESP_SLEEP_WAKEUP_TIMER:
44 return WAKEUP_CAUSE_TIMER;
45 case ESP_SLEEP_WAKEUP_TOUCHPAD:
46 return WAKEUP_CAUSE_TOUCH;
47 case ESP_SLEEP_WAKEUP_UNDEFINED:
48 return WAKEUP_CAUSE_NONE;
49 default:
51 }
52}
53
54optional<uint32_t> DeepSleepComponent::get_run_duration_() const {
55 if (this->wakeup_cause_to_run_duration_.has_value()) {
56 esp_sleep_wakeup_cause_t wakeup_cause = esp_sleep_get_wakeup_cause();
57 switch (wakeup_cause) {
58 case ESP_SLEEP_WAKEUP_EXT0:
59 case ESP_SLEEP_WAKEUP_EXT1:
60 case ESP_SLEEP_WAKEUP_GPIO:
61 return this->wakeup_cause_to_run_duration_->gpio_cause;
62 case ESP_SLEEP_WAKEUP_TOUCHPAD:
63 return this->wakeup_cause_to_run_duration_->touch_cause;
64 default:
65 return this->wakeup_cause_to_run_duration_->default_cause;
66 }
67 }
68 return this->run_duration_;
69}
70
72 this->wakeup_pin_mode_ = wakeup_pin_mode;
73}
74
75#if SOC_PM_SUPPORT_EXT1_WAKEUP
76void DeepSleepComponent::set_ext1_wakeup(Ext1Wakeup ext1_wakeup) { this->ext1_wakeup_ = ext1_wakeup; }
77#endif
78
80 wakeup_cause_to_run_duration_ = wakeup_cause_to_run_duration;
81}
82
84 if (wakeup_pin_ != nullptr) {
85 LOG_PIN(" Wakeup Pin: ", this->wakeup_pin_);
86 }
87 if (this->wakeup_cause_to_run_duration_.has_value()) {
88 ESP_LOGCONFIG(TAG,
89 " Default Wakeup Run Duration: %" PRIu32 " ms\n"
90 " Touch Wakeup Run Duration: %" PRIu32 " ms\n"
91 " GPIO Wakeup Run Duration: %" PRIu32 " ms",
92 this->wakeup_cause_to_run_duration_->default_cause, this->wakeup_cause_to_run_duration_->touch_cause,
93 this->wakeup_cause_to_run_duration_->gpio_cause);
94 }
95}
96
98 if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_KEEP_AWAKE && this->wakeup_pin_ != nullptr &&
99 this->wakeup_pin_->digital_read()) {
100 // Defer deep sleep until inactive
101 if (!this->next_enter_deep_sleep_) {
102 this->status_set_warning();
103 ESP_LOGW(TAG, "Waiting for wakeup pin state change");
104 }
105 this->defer_sleep_();
106 return false;
107 }
108 return true;
109}
110
112 // Timer wakeup - all variants support this
113 if (this->sleep_duration_.has_value())
114 esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
115
116#ifndef SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
117#ifdef USE_GPIO_HOLD
118 // Variants without per-pin deep sleep hold need the global enable
119 gpio_deep_sleep_hold_en();
120#else
121 if (this->wakeup_pin_ != nullptr)
122 gpio_deep_sleep_hold_en();
123#endif
124#endif
125
126 // Single pin wakeup (ext0) - ESP32, S2, S3
127#if SOC_PM_SUPPORT_EXT0_WAKEUP
128 if (this->wakeup_pin_ != nullptr) {
129 const auto gpio_pin = gpio_num_t(this->wakeup_pin_->get_pin());
130 if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLUP) {
131 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLUP_ONLY);
132 } else if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLDOWN) {
133 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLDOWN_ONLY);
134 }
135 gpio_sleep_set_direction(gpio_pin, GPIO_MODE_INPUT);
136 gpio_hold_en(gpio_pin);
137 bool level = !this->wakeup_pin_->is_inverted();
139 level = !level;
140 }
141 esp_sleep_enable_ext0_wakeup(gpio_pin, level);
142 }
143
144 // GPIO wakeup - C2, C3, C5, C6, C61, P4 (no variant has both ext0 and this)
145#elif SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
146 if (this->wakeup_pin_ != nullptr) {
147 const auto gpio_pin = gpio_num_t(this->wakeup_pin_->get_pin());
148 // Make sure GPIO is in input mode, not all RTC GPIO pins are input by default
149 gpio_set_direction(gpio_pin, GPIO_MODE_INPUT);
150 bool level = !this->wakeup_pin_->is_inverted();
152 level = !level;
153 }
154 // Internal pullup/pulldown resistors are enabled automatically, when
155 // ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS is set (by default it is)
156#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
157 esp_sleep_enable_gpio_wakeup_on_hp_periph_powerdown(1ULL << this->wakeup_pin_->get_pin(),
158 static_cast<esp_sleep_gpio_wake_up_mode_t>(level));
159#else
160 esp_deep_sleep_enable_gpio_wakeup(1ULL << this->wakeup_pin_->get_pin(),
161 static_cast<esp_deepsleep_gpio_wake_up_mode_t>(level));
162#endif
163 }
164#endif
165
166 // Multiple pin wakeup (ext1) - All except C2, C3
167#if SOC_PM_SUPPORT_EXT1_WAKEUP
168 if (this->ext1_wakeup_.has_value()) {
169 esp_sleep_enable_ext1_wakeup(this->ext1_wakeup_->mask, this->ext1_wakeup_->wakeup_mode);
170 }
171#endif
172
173 // Touch wakeup - ESP32, S2, S3, P4
174#if SOC_PM_SUPPORT_TOUCH_SENSOR_WAKEUP
175 if (this->touch_wakeup_.has_value() && *(this->touch_wakeup_)) {
176 esp_sleep_enable_touchpad_wakeup();
177 esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
178 }
179#endif
180
181 esp_deep_sleep_start();
182}
183
184bool DeepSleepComponent::should_teardown_() { return true; }
185
186} // namespace esphome::deep_sleep
187
188#endif // USE_ESP32
void set_run_duration(WakeupCauseToRunDuration wakeup_cause_to_run_duration)
optional< uint32_t > get_run_duration_() const
void set_wakeup_pin_mode(WakeupPinMode wakeup_pin_mode)
void set_ext1_wakeup(Ext1Wakeup ext1_wakeup)
optional< WakeupCauseToRunDuration > wakeup_cause_to_run_duration_
gpio::Flags get_flags() const override
Definition gpio.h:34
WakeupPinMode
The values of this enum define what should be done if deep sleep is set up with a wakeup pin on the E...
@ 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_TOUCH
The device was woken by a touch pad.
@ 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).