sleep_gpio.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stddef.h>
  7. #include <string.h>
  8. #include <sys/lock.h>
  9. #include <sys/param.h>
  10. #include "esp_attr.h"
  11. #include "esp_sleep.h"
  12. #include "esp_log.h"
  13. #include "esp_memory_utils.h"
  14. #include "soc/soc_caps.h"
  15. #include "sdkconfig.h"
  16. #include "driver/gpio.h"
  17. #include "hal/gpio_hal.h"
  18. #include "hal/rtc_io_hal.h"
  19. #include "soc/rtc_io_periph.h"
  20. #if SOC_LP_AON_SUPPORTED
  21. #include "hal/lp_aon_hal.h"
  22. #else
  23. #include "hal/rtc_hal.h"
  24. #endif
  25. #include "esp_private/gpio.h"
  26. #include "esp_private/sleep_gpio.h"
  27. #include "esp_private/spi_flash_os.h"
  28. #include "esp_private/startup_internal.h"
  29. #include "bootloader_flash.h"
  30. static const char *TAG = "sleep";
  31. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  32. void gpio_sleep_mode_config_apply(void)
  33. {
  34. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  35. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  36. gpio_sleep_pupd_config_apply(gpio_num);
  37. }
  38. }
  39. }
  40. IRAM_ATTR void gpio_sleep_mode_config_unapply(void)
  41. {
  42. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  43. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  44. gpio_sleep_pupd_config_unapply(gpio_num);
  45. }
  46. }
  47. }
  48. #endif
  49. void esp_sleep_config_gpio_isolate(void)
  50. {
  51. ESP_EARLY_LOGI(TAG, "Configure to isolate all GPIO pins in sleep state");
  52. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  53. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  54. gpio_sleep_set_direction(gpio_num, GPIO_MODE_DISABLE);
  55. gpio_sleep_set_pull_mode(gpio_num, GPIO_FLOATING);
  56. }
  57. }
  58. #if CONFIG_ESP_SLEEP_PSRAM_LEAKAGE_WORKAROUND && CONFIG_SPIRAM
  59. gpio_sleep_set_pull_mode(esp_mspi_get_io(ESP_MSPI_IO_CS1), GPIO_PULLUP_ONLY);
  60. #endif // CONFIG_ESP_SLEEP_PSRAM_LEAKAGE_WORKAROUND && CONFIG_SPIRAM
  61. #if CONFIG_ESP_SLEEP_FLASH_LEAKAGE_WORKAROUND
  62. gpio_sleep_set_pull_mode(esp_mspi_get_io(ESP_MSPI_IO_CS0), GPIO_PULLUP_ONLY);
  63. #endif // CONFIG_ESP_SLEEP_FLASH_LEAKAGE_WORKAROUND
  64. #if CONFIG_ESP_SLEEP_MSPI_NEED_ALL_IO_PU
  65. gpio_sleep_set_pull_mode(esp_mspi_get_io(ESP_MSPI_IO_CLK), GPIO_PULLUP_ONLY);
  66. gpio_sleep_set_pull_mode(esp_mspi_get_io(ESP_MSPI_IO_Q), GPIO_PULLUP_ONLY);
  67. gpio_sleep_set_pull_mode(esp_mspi_get_io(ESP_MSPI_IO_D), GPIO_PULLUP_ONLY);
  68. gpio_sleep_set_pull_mode(esp_mspi_get_io(ESP_MSPI_IO_HD), GPIO_PULLUP_ONLY);
  69. gpio_sleep_set_pull_mode(esp_mspi_get_io(ESP_MSPI_IO_WP), GPIO_PULLUP_ONLY);
  70. #if SOC_SPI_MEM_SUPPORT_OPI_MODE
  71. bool octal_mspi_required = bootloader_flash_is_octal_mode_enabled();
  72. #if CONFIG_SPIRAM_MODE_OCT
  73. octal_mspi_required |= true;
  74. #endif // CONFIG_SPIRAM_MODE_OCT
  75. if (octal_mspi_required) {
  76. gpio_sleep_set_pull_mode(esp_mspi_get_io(ESP_MSPI_IO_DQS), GPIO_PULLUP_ONLY);
  77. gpio_sleep_set_pull_mode(esp_mspi_get_io(ESP_MSPI_IO_D4), GPIO_PULLUP_ONLY);
  78. gpio_sleep_set_pull_mode(esp_mspi_get_io(ESP_MSPI_IO_D5), GPIO_PULLUP_ONLY);
  79. gpio_sleep_set_pull_mode(esp_mspi_get_io(ESP_MSPI_IO_D6), GPIO_PULLUP_ONLY);
  80. gpio_sleep_set_pull_mode(esp_mspi_get_io(ESP_MSPI_IO_D7), GPIO_PULLUP_ONLY);
  81. }
  82. #endif // SOC_SPI_MEM_SUPPORT_OPI_MODE
  83. #endif // CONFIG_ESP_SLEEP_MSPI_NEED_ALL_IO_PU
  84. }
  85. void esp_sleep_enable_gpio_switch(bool enable)
  86. {
  87. ESP_EARLY_LOGI(TAG, "%s automatic switching of GPIO sleep configuration", enable ? "Enable" : "Disable");
  88. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  89. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  90. if (enable) {
  91. gpio_sleep_sel_en(gpio_num);
  92. } else {
  93. gpio_sleep_sel_dis(gpio_num);
  94. }
  95. }
  96. }
  97. }
  98. #if !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
  99. IRAM_ATTR void esp_sleep_isolate_digital_gpio(void)
  100. {
  101. gpio_hal_context_t gpio_hal = {
  102. .dev = GPIO_HAL_GET_HW(GPIO_PORT_0)
  103. };
  104. /* no need to do isolate if digital IOs are not being held in deep sleep */
  105. if (!gpio_hal_deep_sleep_hold_is_en(&gpio_hal)) {
  106. return;
  107. }
  108. /**
  109. * there is a situation where we cannot isolate digital IO before deep sleep:
  110. * - task stack is located in external ram(mspi ram), since we will isolate mspi io
  111. *
  112. * assert here instead of returning directly, because if digital IO is not isolated,
  113. * the bottom current of deep sleep will be higher than light sleep, and there is no
  114. * reason to use deep sleep at this time.
  115. */
  116. assert(esp_ptr_internal(&gpio_hal) && "If hold digital IO, the stack of the task calling esp_deep_sleep_start must be in internal ram!");
  117. /* isolate digital IO that is not held(keep the configuration of digital IOs held by users) */
  118. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  119. if (GPIO_IS_VALID_DIGITAL_IO_PAD(gpio_num) && !gpio_hal_is_digital_io_hold(&gpio_hal, gpio_num)) {
  120. /* disable I/O */
  121. gpio_hal_input_disable(&gpio_hal, gpio_num);
  122. gpio_hal_output_disable(&gpio_hal, gpio_num);
  123. /* disable pull up/down */
  124. gpio_hal_pullup_dis(&gpio_hal, gpio_num);
  125. gpio_hal_pulldown_dis(&gpio_hal, gpio_num);
  126. /* make pad work as gpio(otherwise, deep sleep bottom current will rise) */
  127. gpio_hal_func_sel(&gpio_hal, gpio_num, PIN_FUNC_GPIO);
  128. }
  129. }
  130. }
  131. #endif // !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
  132. void esp_deep_sleep_wakeup_io_reset(void)
  133. {
  134. #if SOC_PM_SUPPORT_EXT1_WAKEUP
  135. uint32_t rtc_io_mask = rtc_hal_ext1_get_wakeup_pins();
  136. // Disable ext1 wakeup before releasing hold, such that wakeup status can reflect the correct wakeup pin
  137. rtc_hal_ext1_clear_wakeup_pins();
  138. for (int gpio_num = 0; gpio_num < SOC_GPIO_PIN_COUNT && rtc_io_mask != 0; ++gpio_num) {
  139. int rtcio_num = rtc_io_num_map[gpio_num];
  140. if ((rtc_io_mask & BIT(rtcio_num)) == 0) {
  141. continue;
  142. }
  143. rtcio_hal_hold_disable(rtcio_num);
  144. rtc_io_mask &= ~BIT(rtcio_num);
  145. }
  146. #endif
  147. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  148. uint32_t dl_io_mask = SOC_GPIO_DEEP_SLEEP_WAKE_VALID_GPIO_MASK;
  149. gpio_hal_context_t gpio_hal = {
  150. .dev = GPIO_HAL_GET_HW(GPIO_PORT_0)
  151. };
  152. while (dl_io_mask) {
  153. int gpio_num = __builtin_ffs(dl_io_mask) - 1;
  154. bool wakeup_io_enabled = gpio_hal_deepsleep_wakeup_is_enabled(&gpio_hal, gpio_num);
  155. if (wakeup_io_enabled) {
  156. // Disable the wakeup before releasing hold, such that wakeup status can reflect the correct wakeup pin
  157. gpio_hal_deepsleep_wakeup_disable(&gpio_hal, gpio_num);
  158. gpio_hal_hold_dis(&gpio_hal, gpio_num);
  159. }
  160. dl_io_mask &= ~BIT(gpio_num);
  161. }
  162. #endif
  163. }
  164. #if CONFIG_ESP_SLEEP_GPIO_RESET_WORKAROUND || CONFIG_PM_SLP_DISABLE_GPIO
  165. ESP_SYSTEM_INIT_FN(esp_sleep_startup_init, BIT(0), 105)
  166. {
  167. /* If the TOP domain is powered off, the GPIO will also be powered off during sleep,
  168. and all configurations in the sleep state of GPIO will not take effect.*/
  169. #if !CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP
  170. // Configure to isolate (disable the Input/Output/Pullup/Pulldown
  171. // function of the pin) all GPIO pins in sleep state
  172. esp_sleep_config_gpio_isolate();
  173. #endif
  174. // Enable automatic switching of GPIO configuration
  175. esp_sleep_enable_gpio_switch(true);
  176. return ESP_OK;
  177. }
  178. #endif