ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
deep_sleep_esp32.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32
2#include "driver/gpio.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace deep_sleep {
8
9static const char *const TAG = "deep_sleep";
10
12 if (this->wakeup_cause_to_run_duration_.has_value()) {
13 esp_sleep_wakeup_cause_t wakeup_cause = esp_sleep_get_wakeup_cause();
14 switch (wakeup_cause) {
15 case ESP_SLEEP_WAKEUP_EXT0:
16 case ESP_SLEEP_WAKEUP_EXT1:
17 case ESP_SLEEP_WAKEUP_GPIO:
18 return this->wakeup_cause_to_run_duration_->gpio_cause;
19 case ESP_SLEEP_WAKEUP_TOUCHPAD:
20 return this->wakeup_cause_to_run_duration_->touch_cause;
21 default:
22 return this->wakeup_cause_to_run_duration_->default_cause;
23 }
24 }
25 return this->run_duration_;
26}
27
29 this->wakeup_pin_mode_ = wakeup_pin_mode;
30}
31
32#if !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32C6)
33void DeepSleepComponent::set_ext1_wakeup(Ext1Wakeup ext1_wakeup) { this->ext1_wakeup_ = ext1_wakeup; }
34
35#if !defined(USE_ESP32_VARIANT_ESP32H2)
36void DeepSleepComponent::set_touch_wakeup(bool touch_wakeup) { this->touch_wakeup_ = touch_wakeup; }
37#endif
38
39#endif
40
42 wakeup_cause_to_run_duration_ = wakeup_cause_to_run_duration;
43}
44
46 if (wakeup_pin_ != nullptr) {
47 LOG_PIN(" Wakeup Pin: ", this->wakeup_pin_);
48 }
49 if (this->wakeup_cause_to_run_duration_.has_value()) {
50 ESP_LOGCONFIG(TAG,
51 " Default Wakeup Run Duration: %" PRIu32 " ms\n"
52 " Touch Wakeup Run Duration: %" PRIu32 " ms\n"
53 " GPIO Wakeup Run Duration: %" PRIu32 " ms",
54 this->wakeup_cause_to_run_duration_->default_cause, this->wakeup_cause_to_run_duration_->touch_cause,
55 this->wakeup_cause_to_run_duration_->gpio_cause);
56 }
57}
58
60 if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_KEEP_AWAKE && this->wakeup_pin_ != nullptr &&
61 this->wakeup_pin_->digital_read()) {
62 // Defer deep sleep until inactive
63 if (!this->next_enter_deep_sleep_) {
64 this->status_set_warning();
65 ESP_LOGW(TAG, "Waiting for wakeup pin state change");
66 }
67 this->next_enter_deep_sleep_ = true;
68 return false;
69 }
70 return true;
71}
72
74#if !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32C6) && !defined(USE_ESP32_VARIANT_ESP32H2)
75 if (this->sleep_duration_.has_value())
76 esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
77 if (this->wakeup_pin_ != nullptr) {
78 const auto gpio_pin = gpio_num_t(this->wakeup_pin_->get_pin());
80 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLUP_ONLY);
81 } else if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLDOWN) {
82 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLDOWN_ONLY);
83 }
84 gpio_sleep_set_direction(gpio_pin, GPIO_MODE_INPUT);
85 gpio_hold_en(gpio_pin);
86 gpio_deep_sleep_hold_en();
87 bool level = !this->wakeup_pin_->is_inverted();
89 level = !level;
90 }
91 esp_sleep_enable_ext0_wakeup(gpio_pin, level);
92 }
93 if (this->ext1_wakeup_.has_value()) {
94 esp_sleep_enable_ext1_wakeup(this->ext1_wakeup_->mask, this->ext1_wakeup_->wakeup_mode);
95 }
96
97 if (this->touch_wakeup_.has_value() && *(this->touch_wakeup_)) {
98 esp_sleep_enable_touchpad_wakeup();
99 esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
100 }
101#endif
102
103#if defined(USE_ESP32_VARIANT_ESP32H2)
104 if (this->sleep_duration_.has_value())
105 esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
106 if (this->ext1_wakeup_.has_value()) {
107 esp_sleep_enable_ext1_wakeup(this->ext1_wakeup_->mask, this->ext1_wakeup_->wakeup_mode);
108 }
109#endif
110
111#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6)
112 if (this->sleep_duration_.has_value())
113 esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
114 if (this->wakeup_pin_ != nullptr) {
115 const auto gpio_pin = gpio_num_t(this->wakeup_pin_->get_pin());
116 if (this->wakeup_pin_->get_flags() && gpio::FLAG_PULLUP) {
117 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLUP_ONLY);
118 } else if (this->wakeup_pin_->get_flags() && gpio::FLAG_PULLDOWN) {
119 gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLDOWN_ONLY);
120 }
121 gpio_sleep_set_direction(gpio_pin, GPIO_MODE_INPUT);
122 gpio_hold_en(gpio_pin);
123 gpio_deep_sleep_hold_en();
124 bool level = !this->wakeup_pin_->is_inverted();
126 level = !level;
127 }
128 esp_deep_sleep_enable_gpio_wakeup(1 << this->wakeup_pin_->get_pin(),
129 static_cast<esp_deepsleep_gpio_wake_up_mode_t>(level));
130 }
131#endif
132 esp_deep_sleep_start();
133}
134
135} // namespace deep_sleep
136} // namespace esphome
137#endif
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