ESPHome  2024.10.2
deep_sleep_esp32.cpp
Go to the documentation of this file.
1 #ifdef USE_ESP32
2 #include "deep_sleep_component.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace deep_sleep {
7 
8 static const char *const TAG = "deep_sleep";
9 
11  if (this->wakeup_cause_to_run_duration_.has_value()) {
12  esp_sleep_wakeup_cause_t wakeup_cause = esp_sleep_get_wakeup_cause();
13  switch (wakeup_cause) {
14  case ESP_SLEEP_WAKEUP_EXT0:
15  case ESP_SLEEP_WAKEUP_EXT1:
16  case ESP_SLEEP_WAKEUP_GPIO:
17  return this->wakeup_cause_to_run_duration_->gpio_cause;
18  case ESP_SLEEP_WAKEUP_TOUCHPAD:
19  return this->wakeup_cause_to_run_duration_->touch_cause;
20  default:
21  return this->wakeup_cause_to_run_duration_->default_cause;
22  }
23  }
24  return this->run_duration_;
25 }
26 
28  this->wakeup_pin_mode_ = wakeup_pin_mode;
29 }
30 
31 #if !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32C6)
32 void DeepSleepComponent::set_ext1_wakeup(Ext1Wakeup ext1_wakeup) { this->ext1_wakeup_ = ext1_wakeup; }
33 
34 void DeepSleepComponent::set_touch_wakeup(bool touch_wakeup) { this->touch_wakeup_ = touch_wakeup; }
35 #endif
36 
38  wakeup_cause_to_run_duration_ = wakeup_cause_to_run_duration;
39 }
40 
42  if (wakeup_pin_ != nullptr) {
43  LOG_PIN(" Wakeup Pin: ", this->wakeup_pin_);
44  }
45  if (this->wakeup_cause_to_run_duration_.has_value()) {
46  ESP_LOGCONFIG(TAG, " Default Wakeup Run Duration: %" PRIu32 " ms",
47  this->wakeup_cause_to_run_duration_->default_cause);
48  ESP_LOGCONFIG(TAG, " Touch Wakeup Run Duration: %" PRIu32 " ms", this->wakeup_cause_to_run_duration_->touch_cause);
49  ESP_LOGCONFIG(TAG, " GPIO Wakeup Run Duration: %" PRIu32 " ms", this->wakeup_cause_to_run_duration_->gpio_cause);
50  }
51 }
52 
54  if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_KEEP_AWAKE && this->wakeup_pin_ != nullptr &&
55  !this->sleep_duration_.has_value() && this->wakeup_pin_->digital_read()) {
56  // Defer deep sleep until inactive
57  if (!this->next_enter_deep_sleep_) {
58  this->status_set_warning();
59  ESP_LOGW(TAG, "Waiting for pin_ to switch state to enter deep sleep...");
60  }
61  this->next_enter_deep_sleep_ = true;
62  return false;
63  }
64  return true;
65 }
66 
68 #if !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32C6)
69  if (this->sleep_duration_.has_value())
70  esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
71  if (this->wakeup_pin_ != nullptr) {
72  bool level = !this->wakeup_pin_->is_inverted();
74  level = !level;
75  }
76  esp_sleep_enable_ext0_wakeup(gpio_num_t(this->wakeup_pin_->get_pin()), level);
77  }
78  if (this->ext1_wakeup_.has_value()) {
79  esp_sleep_enable_ext1_wakeup(this->ext1_wakeup_->mask, this->ext1_wakeup_->wakeup_mode);
80  }
81 
82  if (this->touch_wakeup_.has_value() && *(this->touch_wakeup_)) {
83  esp_sleep_enable_touchpad_wakeup();
84  esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
85  }
86 #endif
87 #if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6)
88  if (this->sleep_duration_.has_value())
89  esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
90  if (this->wakeup_pin_ != nullptr) {
91  bool level = !this->wakeup_pin_->is_inverted();
93  level = !level;
94  }
95  esp_deep_sleep_enable_gpio_wakeup(1 << this->wakeup_pin_->get_pin(),
96  static_cast<esp_deepsleep_gpio_wake_up_mode_t>(level));
97  }
98 #endif
99  esp_deep_sleep_start();
100 }
101 
102 } // namespace deep_sleep
103 } // namespace esphome
104 #endif
optional< uint32_t > get_run_duration_() const
optional< WakeupCauseToRunDuration > wakeup_cause_to_run_duration_
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
WakeupPinMode
The values of this enum define what should be done if deep sleep is set up with a wakeup pin on the E...
void set_ext1_wakeup(Ext1Wakeup ext1_wakeup)
bool has_value() const
Definition: optional.h:87
void set_wakeup_pin_mode(WakeupPinMode wakeup_pin_mode)
void set_run_duration(WakeupCauseToRunDuration wakeup_cause_to_run_duration)
virtual uint8_t get_pin() const =0
virtual bool digital_read()=0
As long as the wakeup pin is still in the wakeup state, keep awake.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Automatically invert the wakeup level.
virtual bool is_inverted() const =0