ESPHome 2026.7.4
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
33optional<uint32_t> DeepSleepComponent::get_run_duration_() const {
34 if (this->wakeup_cause_to_run_duration_.has_value()) {
35 esp_sleep_wakeup_cause_t wakeup_cause = esp_sleep_get_wakeup_cause();
36 switch (wakeup_cause) {
37 case ESP_SLEEP_WAKEUP_EXT0:
38 case ESP_SLEEP_WAKEUP_EXT1:
39 case ESP_SLEEP_WAKEUP_GPIO:
40 return this->wakeup_cause_to_run_duration_->gpio_cause;
41 case ESP_SLEEP_WAKEUP_TOUCHPAD:
42 return this->wakeup_cause_to_run_duration_->touch_cause;
43 default:
44 return this->wakeup_cause_to_run_duration_->default_cause;
45 }
46 }
47 return this->run_duration_;
48}
49
51 this->wakeup_pin_mode_ = wakeup_pin_mode;
52}
53
54#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
55void DeepSleepComponent::set_ext1_wakeup(Ext1Wakeup ext1_wakeup) { this->ext1_wakeup_ = ext1_wakeup; }
56#endif
57
58#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
59 !defined(USE_ESP32_VARIANT_ESP32C5) && !defined(USE_ESP32_VARIANT_ESP32C6) && \
60 !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_ESP32C5) && !defined(USE_ESP32_VARIANT_ESP32C6) && \
104 !defined(USE_ESP32_VARIANT_ESP32C61) && !defined(USE_ESP32_VARIANT_ESP32H2)
105 if (this->wakeup_pin_ != nullptr) {
106 const auto gpio_pin = gpio_num_t(this->wakeup_pin_->get_pin());
107 if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLUP) {
108 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLUP_ONLY);
109 } else if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLDOWN) {
110 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLDOWN_ONLY);
111 }
112 gpio_sleep_set_direction(gpio_pin, GPIO_MODE_INPUT);
113 gpio_hold_en(gpio_pin);
114#if !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
115 // Some ESP32 variants support holding a single GPIO during deep sleep without this function
116 // For those variants, gpio_hold_en() is sufficient to hold the pin state during deep sleep
117 gpio_deep_sleep_hold_en();
118#endif
119 bool level = !this->wakeup_pin_->is_inverted();
121 level = !level;
122 }
123 esp_sleep_enable_ext0_wakeup(gpio_pin, level);
124 }
125#endif
126
127 // GPIO wakeup - C2, C3, C5, C6, C61 only
128#if defined(USE_ESP32_VARIANT_ESP32C2) || defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C5) || \
129 defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32C61)
130 if (this->wakeup_pin_ != nullptr) {
131 const auto gpio_pin = gpio_num_t(this->wakeup_pin_->get_pin());
132 // Make sure GPIO is in input mode, not all RTC GPIO pins are input by default
133 gpio_set_direction(gpio_pin, GPIO_MODE_INPUT);
134 bool level = !this->wakeup_pin_->is_inverted();
136 level = !level;
137 }
138 // Internal pullup/pulldown resistors are enabled automatically, when
139 // ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS is set (by default it is)
140#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
141 esp_sleep_enable_gpio_wakeup_on_hp_periph_powerdown(1ULL << this->wakeup_pin_->get_pin(),
142 static_cast<esp_sleep_gpio_wake_up_mode_t>(level));
143#else
144 esp_deep_sleep_enable_gpio_wakeup(1ULL << this->wakeup_pin_->get_pin(),
145 static_cast<esp_deepsleep_gpio_wake_up_mode_t>(level));
146#endif
147 }
148#endif
149
150 // Multiple pin wakeup (ext1) - All except C2, C3
151#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
152 if (this->ext1_wakeup_.has_value()) {
153 esp_sleep_enable_ext1_wakeup(this->ext1_wakeup_->mask, this->ext1_wakeup_->wakeup_mode);
154 }
155#endif
156
157 // Touch wakeup - ESP32, S2, S3 only
158#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
159 !defined(USE_ESP32_VARIANT_ESP32C5) && !defined(USE_ESP32_VARIANT_ESP32C6) && \
160 !defined(USE_ESP32_VARIANT_ESP32C61) && !defined(USE_ESP32_VARIANT_ESP32H2)
161 if (this->touch_wakeup_.has_value() && *(this->touch_wakeup_)) {
162 esp_sleep_enable_touchpad_wakeup();
163 esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
164 }
165#endif
166
167 esp_deep_sleep_start();
168}
169
170bool DeepSleepComponent::should_teardown_() { return true; }
171
172} // namespace esphome::deep_sleep
173
174#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:28
@ FLAG_PULLDOWN
Definition gpio.h:29