ext_wakeup.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include "esp_sleep.h"
  8. #include "sdkconfig.h"
  9. #include "driver/rtc_io.h"
  10. #if CONFIG_EXAMPLE_EXT0_WAKEUP
  11. #if CONFIG_IDF_TARGET_ESP32
  12. const int ext_wakeup_pin_0 = 25;
  13. #else
  14. const int ext_wakeup_pin_0 = 3;
  15. #endif
  16. void example_deep_sleep_register_ext0_wakeup(void)
  17. {
  18. printf("Enabling EXT0 wakeup on pin GPIO%d\n", ext_wakeup_pin_0);
  19. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(ext_wakeup_pin_0, 1));
  20. // Configure pullup/downs via RTCIO to tie wakeup pins to inactive level during deepsleep.
  21. // EXT0 resides in the same power domain (RTC_PERIPH) as the RTC IO pullup/downs.
  22. // No need to keep that power domain explicitly, unlike EXT1.
  23. ESP_ERROR_CHECK(rtc_gpio_pullup_dis(ext_wakeup_pin_0));
  24. ESP_ERROR_CHECK(rtc_gpio_pulldown_en(ext_wakeup_pin_0));
  25. }
  26. #endif // CONFIG_EXAMPLE_EXT0_WAKEUP
  27. #if CONFIG_EXAMPLE_EXT1_WAKEUP
  28. void example_deep_sleep_register_ext1_wakeup(void)
  29. {
  30. const int ext_wakeup_pin_1 = CONFIG_EXAMPLE_EXT1_WAKEUP_PIN_1;
  31. const int ext_wakeup_pin_2 = CONFIG_EXAMPLE_EXT1_WAKEUP_PIN_2;
  32. const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1;
  33. const uint64_t ext_wakeup_pin_2_mask = 1ULL << ext_wakeup_pin_2;
  34. printf("Enabling EXT1 wakeup on pins GPIO%d, GPIO%d\n", ext_wakeup_pin_1, ext_wakeup_pin_2);
  35. #if SOC_PM_SUPPORT_EXT1_WAKEUP_MODE_PER_PIN
  36. const uint64_t ext_wakeup_mode = CONFIG_EXAMPLE_EXT1_WAKEUP_MODE_PIN_1 << ext_wakeup_pin_1 | \
  37. CONFIG_EXAMPLE_EXT1_WAKEUP_MODE_PIN_2 << ext_wakeup_pin_2;
  38. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup_with_level_mask(ext_wakeup_pin_1_mask | ext_wakeup_pin_2_mask, ext_wakeup_mode));
  39. #else
  40. const esp_sleep_ext1_wakeup_mode_t ext_wakeup_mode = CONFIG_EXAMPLE_EXT1_WAKEUP_MODE;
  41. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask | ext_wakeup_pin_2_mask, ext_wakeup_mode));
  42. #endif
  43. /* If there are no external pull-up/downs, tie wakeup pins to inactive level with internal pull-up/downs via RTC IO
  44. * during deepsleep. However, RTC IO relies on the RTC_PERIPH power domain. Keeping this power domain on will
  45. * increase some power comsumption. However, if we turn off the RTC_PERIPH domain or if certain chips lack the RTC_PERIPH
  46. * domain, we will use the HOLD feature to maintain the pull-up and pull-down on the pins during sleep.*/
  47. #if CONFIG_EXAMPLE_EXT1_USE_INTERNAL_PULLUPS
  48. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  49. #if SOC_PM_SUPPORT_EXT1_WAKEUP_MODE_PER_PIN
  50. if (CONFIG_EXAMPLE_EXT1_WAKEUP_MODE_PIN_1) {
  51. ESP_ERROR_CHECK(rtc_gpio_pullup_dis(ext_wakeup_pin_1));
  52. ESP_ERROR_CHECK(rtc_gpio_pulldown_en(ext_wakeup_pin_1));
  53. } else {
  54. ESP_ERROR_CHECK(rtc_gpio_pulldown_dis(ext_wakeup_pin_1));
  55. ESP_ERROR_CHECK(rtc_gpio_pullup_en(ext_wakeup_pin_1));
  56. }
  57. if (CONFIG_EXAMPLE_EXT1_WAKEUP_MODE_PIN_2) {
  58. ESP_ERROR_CHECK(rtc_gpio_pullup_dis(ext_wakeup_pin_2));
  59. ESP_ERROR_CHECK(rtc_gpio_pulldown_en(ext_wakeup_pin_2));
  60. } else {
  61. ESP_ERROR_CHECK(rtc_gpio_pulldown_dis(ext_wakeup_pin_2));
  62. ESP_ERROR_CHECK(rtc_gpio_pullup_en(ext_wakeup_pin_2));
  63. }
  64. #else // !SOC_PM_SUPPORT_EXT1_WAKEUP_MODE_PER_PIN
  65. if (CONFIG_EXAMPLE_EXT1_WAKEUP_MODE) {
  66. ESP_ERROR_CHECK(rtc_gpio_pullup_dis(ext_wakeup_pin_1));
  67. ESP_ERROR_CHECK(rtc_gpio_pulldown_en(ext_wakeup_pin_1));
  68. ESP_ERROR_CHECK(rtc_gpio_pullup_dis(ext_wakeup_pin_2));
  69. ESP_ERROR_CHECK(rtc_gpio_pulldown_en(ext_wakeup_pin_2));
  70. } else {
  71. ESP_ERROR_CHECK(rtc_gpio_pulldown_dis(ext_wakeup_pin_1));
  72. ESP_ERROR_CHECK(rtc_gpio_pullup_en(ext_wakeup_pin_1));
  73. ESP_ERROR_CHECK(rtc_gpio_pulldown_dis(ext_wakeup_pin_2));
  74. ESP_ERROR_CHECK(rtc_gpio_pullup_en(ext_wakeup_pin_2));
  75. }
  76. #endif
  77. #else // ! SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  78. #if SOC_PM_SUPPORT_EXT1_WAKEUP_MODE_PER_PIN
  79. if (CONFIG_EXAMPLE_EXT1_WAKEUP_MODE_PIN_1) {
  80. gpio_pullup_dis(ext_wakeup_pin_1);
  81. gpio_pulldown_en(ext_wakeup_pin_1);
  82. } else {
  83. gpio_pulldown_dis(ext_wakeup_pin_1);
  84. gpio_pullup_en(ext_wakeup_pin_1);
  85. }
  86. if (CONFIG_EXAMPLE_EXT1_WAKEUP_MODE_PIN_2) {
  87. gpio_pullup_dis(ext_wakeup_pin_2);
  88. gpio_pulldown_en(ext_wakeup_pin_2);
  89. } else {
  90. gpio_pulldown_dis(ext_wakeup_pin_2);
  91. gpio_pullup_en(ext_wakeup_pin_2);
  92. }
  93. #else
  94. if (CONFIG_EXAMPLE_EXT1_WAKEUP_MODE) {
  95. ESP_ERROR_CHECK(gpio_pullup_dis(ext_wakeup_pin_1));
  96. ESP_ERROR_CHECK(gpio_pulldown_en(ext_wakeup_pin_1));
  97. ESP_ERROR_CHECK(gpio_pullup_dis(ext_wakeup_pin_2));
  98. ESP_ERROR_CHECK(gpio_pulldown_en(ext_wakeup_pin_2));
  99. } else {
  100. ESP_ERROR_CHECK(gpio_pulldown_dis(ext_wakeup_pin_1));
  101. ESP_ERROR_CHECK(gpio_pullup_en(ext_wakeup_pin_1));
  102. ESP_ERROR_CHECK(gpio_pulldown_dis(ext_wakeup_pin_2));
  103. ESP_ERROR_CHECK(gpio_pullup_en(ext_wakeup_pin_2));
  104. }
  105. #endif
  106. #endif
  107. #endif // CONFIG_EXAMPLE_EXT1_USE_INTERNAL_PULLUPS
  108. }
  109. #endif // CONFIG_EXAMPLE_EXT1_WAKEUP