ESPHome 2026.8.0-dev
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//
24// Notes:
25// - (✓) = Supported by hardware but not yet implemented in ESPHome
26// - ext0: Single pin wakeup using RTC GPIO (esp_sleep_enable_ext0_wakeup)
27// - ext1: Multiple pin wakeup (esp_sleep_enable_ext1_wakeup)
28// - Touch: Touch pad wakeup (esp_sleep_enable_touchpad_wakeup)
29// - GPIO wakeup: GPIO wakeup for RTC pins
30
31static const char *const TAG = "deep_sleep";
32
33#ifdef USE_DEEP_SLEEP_ON_WAKE
35 switch (esp_sleep_get_wakeup_cause()) {
36 case ESP_SLEEP_WAKEUP_EXT0:
37 case ESP_SLEEP_WAKEUP_EXT1:
38 case ESP_SLEEP_WAKEUP_GPIO:
39 return WAKEUP_CAUSE_GPIO;
40 case ESP_SLEEP_WAKEUP_TIMER:
41 return WAKEUP_CAUSE_TIMER;
42 case ESP_SLEEP_WAKEUP_TOUCHPAD:
43 return WAKEUP_CAUSE_TOUCH;
44 case ESP_SLEEP_WAKEUP_UNDEFINED:
45 return WAKEUP_CAUSE_NONE;
46 default:
48 }
49}
50#endif // USE_DEEP_SLEEP_ON_WAKE
51
52optional<uint32_t> DeepSleepComponent::get_run_duration_() const {
53 if (this->wakeup_cause_to_run_duration_.has_value()) {
54 esp_sleep_wakeup_cause_t wakeup_cause = esp_sleep_get_wakeup_cause();
55 switch (wakeup_cause) {
56 case ESP_SLEEP_WAKEUP_EXT0:
57 case ESP_SLEEP_WAKEUP_EXT1:
58 case ESP_SLEEP_WAKEUP_GPIO:
59 return this->wakeup_cause_to_run_duration_->gpio_cause;
60 case ESP_SLEEP_WAKEUP_TOUCHPAD:
61 return this->wakeup_cause_to_run_duration_->touch_cause;
62 default:
63 return this->wakeup_cause_to_run_duration_->default_cause;
64 }
65 }
66 return this->run_duration_;
67}
68
70 this->wakeup_pin_mode_ = wakeup_pin_mode;
71}
72
73#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
74void DeepSleepComponent::set_ext1_wakeup(Ext1Wakeup ext1_wakeup) { this->ext1_wakeup_ = ext1_wakeup; }
75#endif
76
77#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
78 !defined(USE_ESP32_VARIANT_ESP32C5) && !defined(USE_ESP32_VARIANT_ESP32C6) && \
79 !defined(USE_ESP32_VARIANT_ESP32C61) && !defined(USE_ESP32_VARIANT_ESP32H2)
80void DeepSleepComponent::set_touch_wakeup(bool touch_wakeup) { this->touch_wakeup_ = touch_wakeup; }
81#endif
82
84 wakeup_cause_to_run_duration_ = wakeup_cause_to_run_duration;
85}
86
88 if (wakeup_pin_ != nullptr) {
89 LOG_PIN(" Wakeup Pin: ", this->wakeup_pin_);
90 }
91 if (this->wakeup_cause_to_run_duration_.has_value()) {
92 ESP_LOGCONFIG(TAG,
93 " Default Wakeup Run Duration: %" PRIu32 " ms\n"
94 " Touch Wakeup Run Duration: %" PRIu32 " ms\n"
95 " GPIO Wakeup Run Duration: %" PRIu32 " ms",
96 this->wakeup_cause_to_run_duration_->default_cause, this->wakeup_cause_to_run_duration_->touch_cause,
97 this->wakeup_cause_to_run_duration_->gpio_cause);
98 }
99}
100
102 if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_KEEP_AWAKE && this->wakeup_pin_ != nullptr &&
103 this->wakeup_pin_->digital_read()) {
104 // Defer deep sleep until inactive
105 if (!this->next_enter_deep_sleep_) {
106 this->status_set_warning();
107 ESP_LOGW(TAG, "Waiting for wakeup pin state change");
108 }
109 this->next_enter_deep_sleep_ = true;
110 return false;
111 }
112 return true;
113}
114
116 // Timer wakeup - all variants support this
117 if (this->sleep_duration_.has_value())
118 esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
119
120 // Single pin wakeup (ext0) - ESP32, S2, S3 only
121#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
122 !defined(USE_ESP32_VARIANT_ESP32C5) && !defined(USE_ESP32_VARIANT_ESP32C6) && \
123 !defined(USE_ESP32_VARIANT_ESP32C61) && !defined(USE_ESP32_VARIANT_ESP32H2)
124 if (this->wakeup_pin_ != nullptr) {
125 const auto gpio_pin = gpio_num_t(this->wakeup_pin_->get_pin());
126 if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLUP) {
127 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLUP_ONLY);
128 } else if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLDOWN) {
129 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLDOWN_ONLY);
130 }
131 gpio_sleep_set_direction(gpio_pin, GPIO_MODE_INPUT);
132 gpio_hold_en(gpio_pin);
133#if !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
134 // Some ESP32 variants support holding a single GPIO during deep sleep without this function
135 // For those variants, gpio_hold_en() is sufficient to hold the pin state during deep sleep
136 gpio_deep_sleep_hold_en();
137#endif
138 bool level = !this->wakeup_pin_->is_inverted();
140 level = !level;
141 }
142 esp_sleep_enable_ext0_wakeup(gpio_pin, level);
143 }
144#endif
145
146 // GPIO wakeup - C2, C3, C5, C6, C61 only
147#if defined(USE_ESP32_VARIANT_ESP32C2) || defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C5) || \
148 defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32C61)
149 if (this->wakeup_pin_ != nullptr) {
150 const auto gpio_pin = gpio_num_t(this->wakeup_pin_->get_pin());
151 // Make sure GPIO is in input mode, not all RTC GPIO pins are input by default
152 gpio_set_direction(gpio_pin, GPIO_MODE_INPUT);
153 bool level = !this->wakeup_pin_->is_inverted();
155 level = !level;
156 }
157 // Internal pullup/pulldown resistors are enabled automatically, when
158 // ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS is set (by default it is)
159#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
160 esp_sleep_enable_gpio_wakeup_on_hp_periph_powerdown(1ULL << this->wakeup_pin_->get_pin(),
161 static_cast<esp_sleep_gpio_wake_up_mode_t>(level));
162#else
163 esp_deep_sleep_enable_gpio_wakeup(1ULL << this->wakeup_pin_->get_pin(),
164 static_cast<esp_deepsleep_gpio_wake_up_mode_t>(level));
165#endif
166 }
167#endif
168
169 // Multiple pin wakeup (ext1) - All except C2, C3
170#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
171 if (this->ext1_wakeup_.has_value()) {
172 esp_sleep_enable_ext1_wakeup(this->ext1_wakeup_->mask, this->ext1_wakeup_->wakeup_mode);
173 }
174#endif
175
176 // Touch wakeup - ESP32, S2, S3 only
177#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
178 !defined(USE_ESP32_VARIANT_ESP32C5) && !defined(USE_ESP32_VARIANT_ESP32C6) && \
179 !defined(USE_ESP32_VARIANT_ESP32C61) && !defined(USE_ESP32_VARIANT_ESP32H2)
180 if (this->touch_wakeup_.has_value() && *(this->touch_wakeup_)) {
181 esp_sleep_enable_touchpad_wakeup();
182 esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
183 }
184#endif
185
186 esp_deep_sleep_start();
187}
188
189bool DeepSleepComponent::should_teardown_() { return true; }
190
191} // namespace esphome::deep_sleep
192
193#endif // USE_ESP32
virtual gpio::Flags get_flags() const =0
Retrieve GPIO pin flags.
virtual bool digital_read()=0
virtual uint8_t get_pin() const =0
virtual bool is_inverted() const =0
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_
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.
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).
@ FLAG_PULLUP
Definition gpio.h:28
@ FLAG_PULLDOWN
Definition gpio.h:29