ESPHome 2026.5.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 {
9namespace deep_sleep {
10
11// Deep Sleep feature support matrix for ESP32 variants:
12//
13// | Variant | ext0 | ext1 | Touch | GPIO wakeup |
14// |-----------|------|------|-------|-------------|
15// | ESP32 | ✓ | ✓ | ✓ | |
16// | ESP32-S2 | ✓ | ✓ | ✓ | |
17// | ESP32-S3 | ✓ | ✓ | ✓ | |
18// | ESP32-C2 | | | | ✓ |
19// | ESP32-C3 | | | | ✓ |
20// | ESP32-C5 | | (✓) | | (✓) |
21// | ESP32-C6 | | ✓ | | ✓ |
22// | ESP32-C61 | | ✓ | | ✓ |
23// | ESP32-H2 | | ✓ | | |
24//
25// Notes:
26// - (✓) = Supported by hardware but not yet implemented in ESPHome
27// - ext0: Single pin wakeup using RTC GPIO (esp_sleep_enable_ext0_wakeup)
28// - ext1: Multiple pin wakeup (esp_sleep_enable_ext1_wakeup)
29// - Touch: Touch pad wakeup (esp_sleep_enable_touchpad_wakeup)
30// - GPIO wakeup: GPIO wakeup for RTC pins
31
32static const char *const TAG = "deep_sleep";
33
34optional<uint32_t> DeepSleepComponent::get_run_duration_() const {
35 if (this->wakeup_cause_to_run_duration_.has_value()) {
36 esp_sleep_wakeup_cause_t wakeup_cause = esp_sleep_get_wakeup_cause();
37 switch (wakeup_cause) {
38 case ESP_SLEEP_WAKEUP_EXT0:
39 case ESP_SLEEP_WAKEUP_EXT1:
40 case ESP_SLEEP_WAKEUP_GPIO:
41 return this->wakeup_cause_to_run_duration_->gpio_cause;
42 case ESP_SLEEP_WAKEUP_TOUCHPAD:
43 return this->wakeup_cause_to_run_duration_->touch_cause;
44 default:
45 return this->wakeup_cause_to_run_duration_->default_cause;
46 }
47 }
48 return this->run_duration_;
49}
50
52 this->wakeup_pin_mode_ = wakeup_pin_mode;
53}
54
55#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
56void DeepSleepComponent::set_ext1_wakeup(Ext1Wakeup ext1_wakeup) { this->ext1_wakeup_ = ext1_wakeup; }
57#endif
58
59#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
60 !defined(USE_ESP32_VARIANT_ESP32C6) && !defined(USE_ESP32_VARIANT_ESP32C61) && !defined(USE_ESP32_VARIANT_ESP32H2)
61void DeepSleepComponent::set_touch_wakeup(bool touch_wakeup) { this->touch_wakeup_ = touch_wakeup; }
62#endif
63
65 wakeup_cause_to_run_duration_ = wakeup_cause_to_run_duration;
66}
67
69 if (wakeup_pin_ != nullptr) {
70 LOG_PIN(" Wakeup Pin: ", this->wakeup_pin_);
71 }
72 if (this->wakeup_cause_to_run_duration_.has_value()) {
73 ESP_LOGCONFIG(TAG,
74 " Default Wakeup Run Duration: %" PRIu32 " ms\n"
75 " Touch Wakeup Run Duration: %" PRIu32 " ms\n"
76 " GPIO Wakeup Run Duration: %" PRIu32 " ms",
77 this->wakeup_cause_to_run_duration_->default_cause, this->wakeup_cause_to_run_duration_->touch_cause,
78 this->wakeup_cause_to_run_duration_->gpio_cause);
79 }
80}
81
83 if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_KEEP_AWAKE && this->wakeup_pin_ != nullptr &&
84 this->wakeup_pin_->digital_read()) {
85 // Defer deep sleep until inactive
86 if (!this->next_enter_deep_sleep_) {
87 this->status_set_warning();
88 ESP_LOGW(TAG, "Waiting for wakeup pin state change");
89 }
90 this->next_enter_deep_sleep_ = true;
91 return false;
92 }
93 return true;
94}
95
97 // Timer wakeup - all variants support this
98 if (this->sleep_duration_.has_value())
99 esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
100
101 // Single pin wakeup (ext0) - ESP32, S2, S3 only
102#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
103 !defined(USE_ESP32_VARIANT_ESP32C6) && !defined(USE_ESP32_VARIANT_ESP32H2)
104 if (this->wakeup_pin_ != nullptr) {
105 const auto gpio_pin = gpio_num_t(this->wakeup_pin_->get_pin());
106 if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLUP) {
107 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLUP_ONLY);
108 } else if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLDOWN) {
109 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLDOWN_ONLY);
110 }
111 gpio_sleep_set_direction(gpio_pin, GPIO_MODE_INPUT);
112 gpio_hold_en(gpio_pin);
113#if !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
114 // Some ESP32 variants support holding a single GPIO during deep sleep without this function
115 // For those variants, gpio_hold_en() is sufficient to hold the pin state during deep sleep
116 gpio_deep_sleep_hold_en();
117#endif
118 bool level = !this->wakeup_pin_->is_inverted();
120 level = !level;
121 }
122 esp_sleep_enable_ext0_wakeup(gpio_pin, level);
123 }
124#endif
125
126 // GPIO wakeup - C2, C3, C6, C61 only
127#if defined(USE_ESP32_VARIANT_ESP32C2) || defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || \
128 defined(USE_ESP32_VARIANT_ESP32C61)
129 if (this->wakeup_pin_ != nullptr) {
130 const auto gpio_pin = gpio_num_t(this->wakeup_pin_->get_pin());
131 // Make sure GPIO is in input mode, not all RTC GPIO pins are input by default
132 gpio_set_direction(gpio_pin, GPIO_MODE_INPUT);
133 bool level = !this->wakeup_pin_->is_inverted();
135 level = !level;
136 }
137 // Internal pullup/pulldown resistors are enabled automatically, when
138 // ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS is set (by default it is)
139#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
140 esp_sleep_enable_gpio_wakeup_on_hp_periph_powerdown(1ULL << this->wakeup_pin_->get_pin(),
141 static_cast<esp_sleep_gpio_wake_up_mode_t>(level));
142#else
143 esp_deep_sleep_enable_gpio_wakeup(1ULL << this->wakeup_pin_->get_pin(),
144 static_cast<esp_deepsleep_gpio_wake_up_mode_t>(level));
145#endif
146 }
147#endif
148
149 // Multiple pin wakeup (ext1) - All except C2, C3
150#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
151 if (this->ext1_wakeup_.has_value()) {
152 esp_sleep_enable_ext1_wakeup(this->ext1_wakeup_->mask, this->ext1_wakeup_->wakeup_mode);
153 }
154#endif
155
156 // Touch wakeup - ESP32, S2, S3 only
157#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
158 !defined(USE_ESP32_VARIANT_ESP32C6) && !defined(USE_ESP32_VARIANT_ESP32C61) && !defined(USE_ESP32_VARIANT_ESP32H2)
159 if (this->touch_wakeup_.has_value() && *(this->touch_wakeup_)) {
160 esp_sleep_enable_touchpad_wakeup();
161 esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
162 }
163#endif
164
165 esp_deep_sleep_start();
166}
167
168} // namespace deep_sleep
169} // namespace esphome
170#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.
@ FLAG_PULLUP
Definition gpio.h:30
@ FLAG_PULLDOWN
Definition gpio.h:31
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7