domints

Weird ESP32 wakeup

Mar 22nd, 2022
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #define PIN_A 32
  2. #define PIN_B 33
  3. #define PIN_C 34
  4. #define PIN_D 35
  5.  
  6. #define P(x) P_(x)
  7. #define P_(x) GPIO_NUM_##x
  8.  
  9. #define BUTTON_PIN_BITMASK ((uint64_t)1 << PIN_A) | ((uint64_t)1 << PIN_B) | ((uint64_t)1 << PIN_C) | ((uint64_t)1 << PIN_D)
  10.  
  11. void goto_sleep();
  12. void after_wake();
  13.  
  14. void print_GPIO_wake_up(){
  15.   uint64_t GPIO_reason = esp_sleep_get_ext1_wakeup_status();
  16.   Serial.print("GPIO that triggered the wake up: GPIO ");
  17.   Serial.println((log(GPIO_reason))/log(2), 0);
  18. }
  19.  
  20. void print_wakeup_reason(){
  21.   esp_sleep_wakeup_cause_t wakeup_reason;
  22.  
  23.   wakeup_reason = esp_sleep_get_wakeup_cause();
  24.  
  25.   switch(wakeup_reason)
  26.   {
  27.     case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO");  break;
  28.     case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); print_GPIO_wake_up(); break;
  29.     case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
  30.     case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
  31.     case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
  32.     default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
  33.   }
  34. }
  35.  
  36. extern void goto_sleep() {
  37.   esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK, ESP_EXT1_WAKEUP_ANY_HIGH);
  38.   esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  39.   //rtc_gpio_set_level(P(PIN_A), 0);
  40.   //rtc_gpio_set_level(P(PIN_B), 0);
  41.   rtc_gpio_set_level(P(PIN_C), 0);
  42.   rtc_gpio_set_level(P(PIN_D), 0);
  43.   //rtc_gpio_set_direction(P(PIN_A), RTC_GPIO_MODE_INPUT_ONLY);
  44.   //rtc_gpio_set_direction(P(PIN_B), RTC_GPIO_MODE_INPUT_ONLY);
  45.   rtc_gpio_set_direction(P(PIN_C), RTC_GPIO_MODE_INPUT_ONLY);
  46.   rtc_gpio_set_direction(P(PIN_D), RTC_GPIO_MODE_INPUT_ONLY);
  47.   //rtc_gpio_pullup_dis(P(PIN_A));
  48.   //rtc_gpio_pulldown_en(P(PIN_A));
  49.   //rtc_gpio_pullup_dis(P(PIN_B));
  50.   //rtc_gpio_pulldown_en(P(PIN_B));
  51.   rtc_gpio_pullup_dis(P(PIN_C));
  52.   rtc_gpio_pulldown_en(P(PIN_C));
  53.   rtc_gpio_pullup_dis(P(PIN_D));
  54.   rtc_gpio_pulldown_en(P(PIN_D));
  55.   //rtc_gpio_hold_en(P(PIN_A));
  56.   //rtc_gpio_hold_en(P(PIN_B));
  57.   rtc_gpio_hold_en(P(PIN_C));
  58.   rtc_gpio_hold_en(P(PIN_D));
  59.   esp_deep_sleep_start();
  60. }
  61.  
  62. extern void after_wake()
  63. {
  64.     print_wakeup_reason();
  65.     rtc_gpio_deinit(P(PIN_A));
  66.     rtc_gpio_deinit(P(PIN_B));
  67.     rtc_gpio_deinit(P(PIN_C));
  68.     rtc_gpio_deinit(P(PIN_D));
  69. }
Advertisement
Add Comment
Please, Sign In to add comment