ESPHome 2025.10.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
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-H2 | | ✓ | | |
22//
23// Notes:
24// - (✓) = Supported by hardware but not yet implemented in ESPHome
25// - ext0: Single pin wakeup using RTC GPIO (esp_sleep_enable_ext0_wakeup)
26// - ext1: Multiple pin wakeup (esp_sleep_enable_ext1_wakeup)
27// - Touch: Touch pad wakeup (esp_sleep_enable_touchpad_wakeup)
28// - GPIO wakeup: GPIO wakeup for non-RTC pins (esp_deep_sleep_enable_gpio_wakeup)
29
30static const char *const TAG = "deep_sleep";
31
33 if (this->wakeup_cause_to_run_duration_.has_value()) {
34 esp_sleep_wakeup_cause_t wakeup_cause = esp_sleep_get_wakeup_cause();
35 switch (wakeup_cause) {
36 case ESP_SLEEP_WAKEUP_EXT0:
37 case ESP_SLEEP_WAKEUP_EXT1:
38 case ESP_SLEEP_WAKEUP_GPIO:
39 return this->wakeup_cause_to_run_duration_->gpio_cause;
40 case ESP_SLEEP_WAKEUP_TOUCHPAD:
41 return this->wakeup_cause_to_run_duration_->touch_cause;
42 default:
43 return this->wakeup_cause_to_run_duration_->default_cause;
44 }
45 }
46 return this->run_duration_;
47}
48
50 this->wakeup_pin_mode_ = wakeup_pin_mode;
51}
52
53#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3)
54void DeepSleepComponent::set_ext1_wakeup(Ext1Wakeup ext1_wakeup) { this->ext1_wakeup_ = ext1_wakeup; }
55#endif
56
57#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
58 !defined(USE_ESP32_VARIANT_ESP32C6) && !defined(USE_ESP32_VARIANT_ESP32H2)
59void DeepSleepComponent::set_touch_wakeup(bool touch_wakeup) { this->touch_wakeup_ = touch_wakeup; }
60#endif
61
63 wakeup_cause_to_run_duration_ = wakeup_cause_to_run_duration;
64}
65
67 if (wakeup_pin_ != nullptr) {
68 LOG_PIN(" Wakeup Pin: ", this->wakeup_pin_);
69 }
70 if (this->wakeup_cause_to_run_duration_.has_value()) {
71 ESP_LOGCONFIG(TAG,
72 " Default Wakeup Run Duration: %" PRIu32 " ms\n"
73 " Touch Wakeup Run Duration: %" PRIu32 " ms\n"
74 " GPIO Wakeup Run Duration: %" PRIu32 " ms",
75 this->wakeup_cause_to_run_duration_->default_cause, this->wakeup_cause_to_run_duration_->touch_cause,
76 this->wakeup_cause_to_run_duration_->gpio_cause);
77 }
78}
79
81 if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_KEEP_AWAKE && this->wakeup_pin_ != nullptr &&
82 this->wakeup_pin_->digital_read()) {
83 // Defer deep sleep until inactive
84 if (!this->next_enter_deep_sleep_) {
85 this->status_set_warning();
86 ESP_LOGW(TAG, "Waiting for wakeup pin state change");
87 }
88 this->next_enter_deep_sleep_ = true;
89 return false;
90 }
91 return true;
92}
93
95 // Timer wakeup - all variants support this
96 if (this->sleep_duration_.has_value())
97 esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
98
99 // Single pin wakeup (ext0) - ESP32, S2, S3 only
100#if !defined(USE_ESP32_VARIANT_ESP32C2) && !defined(USE_ESP32_VARIANT_ESP32C3) && \
101 !defined(USE_ESP32_VARIANT_ESP32C6) && !defined(USE_ESP32_VARIANT_ESP32H2)
102 if (this->wakeup_pin_ != nullptr) {
103 const auto gpio_pin = gpio_num_t(this->wakeup_pin_->get_pin());
104 if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLUP) {
105 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLUP_ONLY);
106 } else if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLDOWN) {
107 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLDOWN_ONLY);
108 }
109 gpio_sleep_set_direction(gpio_pin, GPIO_MODE_INPUT);
110 gpio_hold_en(gpio_pin);
111#if !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
112 // Some ESP32 variants support holding a single GPIO during deep sleep without this function
113 // For those variants, gpio_hold_en() is sufficient to hold the pin state during deep sleep
114 gpio_deep_sleep_hold_en();
115#endif
116 bool level = !this->wakeup_pin_->is_inverted();
118 level = !level;
119 }
120 esp_sleep_enable_ext0_wakeup(gpio_pin, level);
121 }
122#endif
123
124 // GPIO wakeup - C2, C3, C6 only
125#if defined(USE_ESP32_VARIANT_ESP32C2) || defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6)
126 if (this->wakeup_pin_ != nullptr) {
127 const auto gpio_pin = gpio_num_t(this->wakeup_pin_->get_pin());
128 if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLUP) {
129 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLUP_ONLY);
130 } else if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLDOWN) {
131 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLDOWN_ONLY);
132 }
133 gpio_sleep_set_direction(gpio_pin, GPIO_MODE_INPUT);
134 gpio_hold_en(gpio_pin);
135#if !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
136 // Some ESP32 variants support holding a single GPIO during deep sleep without this function
137 // For those variants, gpio_hold_en() is sufficient to hold the pin state during deep sleep
138 gpio_deep_sleep_hold_en();
139#endif
140 bool level = !this->wakeup_pin_->is_inverted();
142 level = !level;
143 }
144 esp_deep_sleep_enable_gpio_wakeup(1 << this->wakeup_pin_->get_pin(),
145 static_cast<esp_deepsleep_gpio_wake_up_mode_t>(level));
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_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
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