ESPHome 2026.1.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
7namespace esphome {
8namespace 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 non-RTC pins (esp_deep_sleep_enable_gpio_wakeup)
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_ESP32C6) && !defined(USE_ESP32_VARIANT_ESP32C61) && !defined(USE_ESP32_VARIANT_ESP32H2)
60void DeepSleepComponent::set_touch_wakeup(bool touch_wakeup) { this->touch_wakeup_ = touch_wakeup; }
61#endif
62
64 wakeup_cause_to_run_duration_ = wakeup_cause_to_run_duration;
65}
66
68 if (wakeup_pin_ != nullptr) {
69 LOG_PIN(" Wakeup Pin: ", this->wakeup_pin_);
70 }
71 if (this->wakeup_cause_to_run_duration_.has_value()) {
72 ESP_LOGCONFIG(TAG,
73 " Default Wakeup Run Duration: %" PRIu32 " ms\n"
74 " Touch Wakeup Run Duration: %" PRIu32 " ms\n"
75 " GPIO Wakeup Run Duration: %" PRIu32 " ms",
76 this->wakeup_cause_to_run_duration_->default_cause, this->wakeup_cause_to_run_duration_->touch_cause,
77 this->wakeup_cause_to_run_duration_->gpio_cause);
78 }
79}
80
82 if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_KEEP_AWAKE && this->wakeup_pin_ != nullptr &&
83 this->wakeup_pin_->digital_read()) {
84 // Defer deep sleep until inactive
85 if (!this->next_enter_deep_sleep_) {
86 this->status_set_warning();
87 ESP_LOGW(TAG, "Waiting for wakeup pin state change");
88 }
89 this->next_enter_deep_sleep_ = true;
90 return false;
91 }
92 return true;
93}
94
96 // Timer wakeup - all variants support this
97 if (this->sleep_duration_.has_value())
98 esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
99
100 // Single pin wakeup (ext0) - ESP32, S2, S3 only
101#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
102 !defined(USE_ESP32_VARIANT_ESP32C6) && !defined(USE_ESP32_VARIANT_ESP32H2)
103 if (this->wakeup_pin_ != nullptr) {
104 const auto gpio_pin = gpio_num_t(this->wakeup_pin_->get_pin());
105 if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLUP) {
106 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLUP_ONLY);
107 } else if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLDOWN) {
108 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLDOWN_ONLY);
109 }
110 gpio_sleep_set_direction(gpio_pin, GPIO_MODE_INPUT);
111 gpio_hold_en(gpio_pin);
112#if !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
113 // Some ESP32 variants support holding a single GPIO during deep sleep without this function
114 // For those variants, gpio_hold_en() is sufficient to hold the pin state during deep sleep
115 gpio_deep_sleep_hold_en();
116#endif
117 bool level = !this->wakeup_pin_->is_inverted();
119 level = !level;
120 }
121 esp_sleep_enable_ext0_wakeup(gpio_pin, level);
122 }
123#endif
124
125 // GPIO wakeup - C2, C3, C6, C61 only
126#if defined(USE_ESP32_VARIANT_ESP32C2) || defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || \
127 defined(USE_ESP32_VARIANT_ESP32C61)
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#if !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
138 // Some ESP32 variants support holding a single GPIO during deep sleep without this function
139 // For those variants, gpio_hold_en() is sufficient to hold the pin state during deep sleep
140 gpio_deep_sleep_hold_en();
141#endif
142 bool level = !this->wakeup_pin_->is_inverted();
144 level = !level;
145 }
146 esp_deep_sleep_enable_gpio_wakeup(1 << this->wakeup_pin_->get_pin(),
147 static_cast<esp_deepsleep_gpio_wake_up_mode_t>(level));
148 }
149#endif
150
151 // Multiple pin wakeup (ext1) - All except C2, C3
152#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
153 if (this->ext1_wakeup_.has_value()) {
154 esp_sleep_enable_ext1_wakeup(this->ext1_wakeup_->mask, this->ext1_wakeup_->wakeup_mode);
155 }
156#endif
157
158 // Touch wakeup - ESP32, S2, S3 only
159#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
160 !defined(USE_ESP32_VARIANT_ESP32C6) && !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
170} // namespace deep_sleep
171} // namespace esphome
172#endif // USE_ESP32
void status_set_warning(const char *message=nullptr)
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_
bool has_value() const
Definition optional.h:92
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:21
@ FLAG_PULLDOWN
Definition gpio.h:22
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7