sleep_modes.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stddef.h>
  15. #include <sys/lock.h>
  16. #include <sys/param.h>
  17. #include "esp_attr.h"
  18. #include "esp_sleep.h"
  19. #include "esp_log.h"
  20. #include "esp_clk.h"
  21. #include "esp_newlib.h"
  22. #include "esp_spi_flash.h"
  23. #include "rom/cache.h"
  24. #include "rom/rtc.h"
  25. #include "rom/uart.h"
  26. #include "soc/cpu.h"
  27. #include "soc/rtc.h"
  28. #include "soc/rtc_cntl_reg.h"
  29. #include "soc/rtc_io_reg.h"
  30. #include "soc/spi_reg.h"
  31. #include "soc/sens_reg.h"
  32. #include "soc/dport_reg.h"
  33. #include "driver/rtc_io.h"
  34. #include "freertos/FreeRTOS.h"
  35. #include "freertos/task.h"
  36. #include "sdkconfig.h"
  37. // If light sleep time is less than that, don't power down flash
  38. #define FLASH_PD_MIN_SLEEP_TIME_US 2000
  39. // Time from VDD_SDIO power up to first flash read in ROM code
  40. #define VDD_SDIO_POWERUP_TO_FLASH_READ_US 700
  41. /**
  42. * Internal structure which holds all requested deep sleep parameters
  43. */
  44. typedef struct {
  45. esp_sleep_pd_option_t pd_options[ESP_PD_DOMAIN_MAX];
  46. uint64_t sleep_duration;
  47. uint32_t wakeup_triggers : 11;
  48. uint32_t ext1_trigger_mode : 1;
  49. uint32_t ext1_rtc_gpio_mask : 18;
  50. uint32_t ext0_trigger_level : 1;
  51. uint32_t ext0_rtc_gpio_num : 5;
  52. } deep_sleep_config_t;
  53. static deep_sleep_config_t s_config = {
  54. .pd_options = { ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO },
  55. .wakeup_triggers = 0
  56. };
  57. /* Updating RTC_MEMORY_CRC_REG register via set_rtc_memory_crc()
  58. is not thread-safe. */
  59. static _lock_t lock_rtc_memory_crc;
  60. static const char* TAG = "sleep";
  61. static uint32_t get_power_down_flags();
  62. static void ext0_wakeup_prepare();
  63. static void ext1_wakeup_prepare();
  64. static void timer_wakeup_prepare();
  65. /* Wake from deep sleep stub
  66. See esp_deepsleep.h esp_wake_deep_sleep() comments for details.
  67. */
  68. esp_deep_sleep_wake_stub_fn_t esp_get_deep_sleep_wake_stub(void)
  69. {
  70. _lock_acquire(&lock_rtc_memory_crc);
  71. uint32_t stored_crc = REG_READ(RTC_MEMORY_CRC_REG);
  72. set_rtc_memory_crc();
  73. uint32_t calc_crc = REG_READ(RTC_MEMORY_CRC_REG);
  74. REG_WRITE(RTC_MEMORY_CRC_REG, stored_crc);
  75. _lock_release(&lock_rtc_memory_crc);
  76. if(stored_crc == calc_crc) {
  77. return (esp_deep_sleep_wake_stub_fn_t)REG_READ(RTC_ENTRY_ADDR_REG);
  78. } else {
  79. return NULL;
  80. }
  81. }
  82. void esp_set_deep_sleep_wake_stub(esp_deep_sleep_wake_stub_fn_t new_stub)
  83. {
  84. _lock_acquire(&lock_rtc_memory_crc);
  85. REG_WRITE(RTC_ENTRY_ADDR_REG, (uint32_t)new_stub);
  86. set_rtc_memory_crc();
  87. _lock_release(&lock_rtc_memory_crc);
  88. }
  89. void RTC_IRAM_ATTR esp_default_wake_deep_sleep(void) {
  90. /* Clear MMU for CPU 0 */
  91. _DPORT_REG_WRITE(DPORT_PRO_CACHE_CTRL1_REG,
  92. _DPORT_REG_READ(DPORT_PRO_CACHE_CTRL1_REG) | DPORT_PRO_CACHE_MMU_IA_CLR);
  93. _DPORT_REG_WRITE(DPORT_PRO_CACHE_CTRL1_REG,
  94. _DPORT_REG_READ(DPORT_PRO_CACHE_CTRL1_REG) & (~DPORT_PRO_CACHE_MMU_IA_CLR));
  95. #if CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY > 0
  96. // ROM code has not started yet, so we need to set delay factor
  97. // used by ets_delay_us first.
  98. ets_update_cpu_frequency_rom(ets_get_detected_xtal_freq() / 1000000);
  99. // This delay is configured in menuconfig, it can be used to give
  100. // the flash chip some time to become ready.
  101. ets_delay_us(CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY);
  102. #endif
  103. }
  104. void __attribute__((weak, alias("esp_default_wake_deep_sleep"))) esp_wake_deep_sleep(void);
  105. void esp_deep_sleep(uint64_t time_in_us)
  106. {
  107. esp_sleep_enable_timer_wakeup(time_in_us);
  108. esp_deep_sleep_start();
  109. }
  110. static uint32_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags)
  111. {
  112. // Flush UARTs so that output is not lost due to APB frequency change
  113. uart_tx_wait_idle(0);
  114. uart_tx_wait_idle(1);
  115. uart_tx_wait_idle(2);
  116. // Configure pins for external wakeup
  117. if (s_config.wakeup_triggers & RTC_EXT0_TRIG_EN) {
  118. ext0_wakeup_prepare();
  119. }
  120. if (s_config.wakeup_triggers & RTC_EXT1_TRIG_EN) {
  121. ext1_wakeup_prepare();
  122. }
  123. // Enable ULP wakeup
  124. if (s_config.wakeup_triggers & RTC_ULP_TRIG_EN) {
  125. SET_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_WAKEUP_FORCE_EN);
  126. }
  127. // Configure timer wakeup
  128. if ((s_config.wakeup_triggers & RTC_TIMER_TRIG_EN) &&
  129. s_config.sleep_duration > 0) {
  130. timer_wakeup_prepare();
  131. }
  132. // Enter sleep
  133. rtc_sleep_config_t config = RTC_SLEEP_CONFIG_DEFAULT(pd_flags);
  134. rtc_sleep_init(config);
  135. return rtc_sleep_start(s_config.wakeup_triggers, 0);
  136. }
  137. void IRAM_ATTR esp_deep_sleep_start()
  138. {
  139. // Configure wake stub
  140. if (esp_get_deep_sleep_wake_stub() == NULL) {
  141. esp_set_deep_sleep_wake_stub(esp_wake_deep_sleep);
  142. }
  143. // Decide which power domains can be powered down
  144. uint32_t pd_flags = get_power_down_flags();
  145. // Enter sleep
  146. esp_sleep_start(RTC_SLEEP_PD_DIG | RTC_SLEEP_PD_VDDSDIO | pd_flags);
  147. // Because RTC is in a slower clock domain than the CPU, it
  148. // can take several CPU cycles for the sleep mode to start.
  149. while (1) {
  150. ;
  151. }
  152. }
  153. static void rtc_wdt_enable(int time_ms)
  154. {
  155. WRITE_PERI_REG(RTC_CNTL_WDTWPROTECT_REG, RTC_CNTL_WDT_WKEY_VALUE);
  156. WRITE_PERI_REG(RTC_CNTL_WDTFEED_REG, 1);
  157. REG_SET_FIELD(RTC_CNTL_WDTCONFIG0_REG, RTC_CNTL_WDT_SYS_RESET_LENGTH, 7);
  158. REG_SET_FIELD(RTC_CNTL_WDTCONFIG0_REG, RTC_CNTL_WDT_CPU_RESET_LENGTH, 7);
  159. REG_SET_FIELD(RTC_CNTL_WDTCONFIG0_REG, RTC_CNTL_WDT_STG0, RTC_WDT_STG_SEL_RESET_RTC);
  160. WRITE_PERI_REG(RTC_CNTL_WDTCONFIG1_REG, rtc_clk_slow_freq_get_hz() * time_ms / 1000);
  161. SET_PERI_REG_MASK(RTC_CNTL_WDTCONFIG0_REG, RTC_CNTL_WDT_EN | RTC_CNTL_WDT_PAUSE_IN_SLP);
  162. WRITE_PERI_REG(RTC_CNTL_WDTWPROTECT_REG, 0);
  163. }
  164. static void rtc_wdt_disable()
  165. {
  166. WRITE_PERI_REG(RTC_CNTL_WDTWPROTECT_REG, RTC_CNTL_WDT_WKEY_VALUE);
  167. WRITE_PERI_REG(RTC_CNTL_WDTFEED_REG, 1);
  168. REG_SET_FIELD(RTC_CNTL_WDTCONFIG0_REG, RTC_CNTL_WDT_STG0, RTC_WDT_STG_SEL_OFF);
  169. REG_CLR_BIT(RTC_CNTL_WDTCONFIG0_REG, RTC_CNTL_WDT_EN);
  170. WRITE_PERI_REG(RTC_CNTL_WDTWPROTECT_REG, 0);
  171. }
  172. /**
  173. * Helper function which handles entry to and exit from light sleep
  174. * Placed into IRAM as flash may need some time to be powered on.
  175. */
  176. static esp_err_t esp_light_sleep_inner(uint32_t pd_flags,
  177. rtc_cpu_freq_t cpu_freq, uint32_t flash_enable_time_us,
  178. rtc_vddsdio_config_t vddsdio_config) IRAM_ATTR __attribute__((noinline));
  179. static esp_err_t esp_light_sleep_inner(uint32_t pd_flags,
  180. rtc_cpu_freq_t cpu_freq, uint32_t flash_enable_time_us,
  181. rtc_vddsdio_config_t vddsdio_config)
  182. {
  183. // Enter sleep
  184. esp_err_t err = esp_sleep_start(pd_flags);
  185. // If VDDSDIO regulator was controlled by RTC registers before sleep,
  186. // restore the configuration.
  187. if (vddsdio_config.force) {
  188. rtc_vddsdio_set_config(vddsdio_config);
  189. }
  190. // Restore CPU frequency
  191. rtc_clk_cpu_freq_set(cpu_freq);
  192. // If SPI flash was powered down, wait for it to become ready
  193. if (pd_flags & RTC_SLEEP_PD_VDDSDIO) {
  194. // Wait for the flash chip to start up
  195. ets_delay_us(flash_enable_time_us);
  196. }
  197. return err;
  198. }
  199. esp_err_t esp_light_sleep_start()
  200. {
  201. static portMUX_TYPE light_sleep_lock = portMUX_INITIALIZER_UNLOCKED;
  202. portENTER_CRITICAL(&light_sleep_lock);
  203. int other_cpu = xPortGetCoreID() ? 0 : 1;
  204. esp_cpu_stall(other_cpu);
  205. // Other CPU is stalled, need to disable DPORT protection
  206. esp_dport_access_int_pause();
  207. // Decide which power domains can be powered down
  208. uint32_t pd_flags = get_power_down_flags();
  209. // Decide if VDD_SDIO needs to be powered down;
  210. // If it needs to be powered down, adjust sleep time.
  211. const uint32_t flash_enable_time_us = VDD_SDIO_POWERUP_TO_FLASH_READ_US
  212. + CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY;
  213. // Don't power down VDD_SDIO if pSRAM is used.
  214. #ifndef CONFIG_SPIRAM_SUPPORT
  215. if (s_config.sleep_duration > FLASH_PD_MIN_SLEEP_TIME_US &&
  216. s_config.sleep_duration > flash_enable_time_us) {
  217. pd_flags |= RTC_SLEEP_PD_VDDSDIO;
  218. s_config.sleep_duration -= flash_enable_time_us;
  219. }
  220. #endif //CONFIG_SPIRAM_SUPPORT
  221. rtc_vddsdio_config_t vddsdio_config = rtc_vddsdio_get_config();
  222. // Safety net: enable WDT in case exit from light sleep fails
  223. rtc_wdt_enable(1000);
  224. // Save current CPU frequency, light sleep will switch to XTAL
  225. rtc_cpu_freq_t cpu_freq = rtc_clk_cpu_freq_get();
  226. // Enter sleep, then wait for flash to be ready on wakeup
  227. esp_err_t err = esp_light_sleep_inner(pd_flags, cpu_freq,
  228. flash_enable_time_us, vddsdio_config);
  229. // At this point, if FRC1 is used for timekeeping, time will be lagging behind.
  230. // This will update the microsecond count based on RTC timer.
  231. esp_set_time_from_rtc();
  232. // However, we do not advance RTOS ticks here; doing so would be rather messy,
  233. // as ticks can only be advanced on CPU0.
  234. // If this is needed by the application, automatic light sleep (tickless idle)
  235. // will handle that better.
  236. esp_cpu_unstall(other_cpu);
  237. esp_dport_access_int_resume();
  238. rtc_wdt_disable();
  239. portEXIT_CRITICAL(&light_sleep_lock);
  240. return err;
  241. }
  242. void system_deep_sleep(uint64_t) __attribute__((alias("esp_deep_sleep")));
  243. esp_err_t esp_sleep_enable_ulp_wakeup()
  244. {
  245. #ifdef CONFIG_ULP_COPROC_ENABLED
  246. if(s_config.wakeup_triggers & RTC_EXT0_TRIG_EN) {
  247. ESP_LOGE(TAG, "Conflicting wake-up trigger: ext0");
  248. return ESP_ERR_INVALID_STATE;
  249. }
  250. s_config.wakeup_triggers |= RTC_ULP_TRIG_EN;
  251. return ESP_OK;
  252. #else
  253. return ESP_ERR_INVALID_STATE;
  254. #endif
  255. }
  256. esp_err_t esp_sleep_enable_timer_wakeup(uint64_t time_in_us)
  257. {
  258. s_config.wakeup_triggers |= RTC_TIMER_TRIG_EN;
  259. s_config.sleep_duration = time_in_us;
  260. return ESP_OK;
  261. }
  262. static void timer_wakeup_prepare()
  263. {
  264. uint32_t period = esp_clk_slowclk_cal_get();
  265. uint64_t rtc_count_delta = rtc_time_us_to_slowclk(s_config.sleep_duration, period);
  266. uint64_t cur_rtc_count = rtc_time_get();
  267. rtc_sleep_set_wakeup_time(cur_rtc_count + rtc_count_delta);
  268. }
  269. esp_err_t esp_sleep_enable_touchpad_wakeup()
  270. {
  271. if (s_config.wakeup_triggers & (RTC_EXT0_TRIG_EN)) {
  272. ESP_LOGE(TAG, "Conflicting wake-up trigger: ext0");
  273. return ESP_ERR_INVALID_STATE;
  274. }
  275. s_config.wakeup_triggers |= RTC_TOUCH_TRIG_EN;
  276. return ESP_OK;
  277. }
  278. touch_pad_t esp_sleep_get_touchpad_wakeup_status()
  279. {
  280. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_TOUCHPAD) {
  281. return TOUCH_PAD_MAX;
  282. }
  283. uint32_t touch_mask = REG_GET_FIELD(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_MEAS_EN);
  284. assert(touch_mask != 0 && "wakeup reason is RTC_TOUCH_TRIG_EN but SENS_TOUCH_MEAS_EN is zero");
  285. return (touch_pad_t) (__builtin_ffs(touch_mask) - 1);
  286. }
  287. esp_err_t esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level)
  288. {
  289. if (level < 0 || level > 1) {
  290. return ESP_ERR_INVALID_ARG;
  291. }
  292. if (!RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
  293. return ESP_ERR_INVALID_ARG;
  294. }
  295. if (s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) {
  296. ESP_LOGE(TAG, "Conflicting wake-up triggers: touch / ULP");
  297. return ESP_ERR_INVALID_STATE;
  298. }
  299. s_config.ext0_rtc_gpio_num = rtc_gpio_desc[gpio_num].rtc_num;
  300. s_config.ext0_trigger_level = level;
  301. s_config.wakeup_triggers |= RTC_EXT0_TRIG_EN;
  302. return ESP_OK;
  303. }
  304. static void ext0_wakeup_prepare()
  305. {
  306. int rtc_gpio_num = s_config.ext0_rtc_gpio_num;
  307. // Set GPIO to be used for wakeup
  308. REG_SET_FIELD(RTC_IO_EXT_WAKEUP0_REG, RTC_IO_EXT_WAKEUP0_SEL, rtc_gpio_num);
  309. // Set level which will trigger wakeup
  310. SET_PERI_REG_BITS(RTC_CNTL_EXT_WAKEUP_CONF_REG, 0x1,
  311. s_config.ext0_trigger_level, RTC_CNTL_EXT_WAKEUP0_LV_S);
  312. // Find GPIO descriptor in the rtc_gpio_desc table and configure the pad
  313. for (size_t gpio_num = 0; gpio_num < GPIO_PIN_COUNT; ++gpio_num) {
  314. const rtc_gpio_desc_t* desc = &rtc_gpio_desc[gpio_num];
  315. if (desc->rtc_num == rtc_gpio_num) {
  316. REG_SET_BIT(desc->reg, desc->mux);
  317. SET_PERI_REG_BITS(desc->reg, 0x3, 0, desc->func);
  318. REG_SET_BIT(desc->reg, desc->slpsel);
  319. REG_SET_BIT(desc->reg, desc->slpie);
  320. break;
  321. }
  322. }
  323. }
  324. esp_err_t esp_sleep_enable_ext1_wakeup(uint64_t mask, esp_sleep_ext1_wakeup_mode_t mode)
  325. {
  326. if (mode > ESP_EXT1_WAKEUP_ANY_HIGH) {
  327. return ESP_ERR_INVALID_ARG;
  328. }
  329. // Translate bit map of GPIO numbers into the bit map of RTC IO numbers
  330. uint32_t rtc_gpio_mask = 0;
  331. for (int gpio = 0; mask; ++gpio, mask >>= 1) {
  332. if ((mask & 1) == 0) {
  333. continue;
  334. }
  335. if (!RTC_GPIO_IS_VALID_GPIO(gpio)) {
  336. ESP_LOGE(TAG, "Not an RTC IO: GPIO%d", gpio);
  337. return ESP_ERR_INVALID_ARG;
  338. }
  339. rtc_gpio_mask |= BIT(rtc_gpio_desc[gpio].rtc_num);
  340. }
  341. s_config.ext1_rtc_gpio_mask = rtc_gpio_mask;
  342. s_config.ext1_trigger_mode = mode;
  343. s_config.wakeup_triggers |= RTC_EXT1_TRIG_EN;
  344. return ESP_OK;
  345. }
  346. static void ext1_wakeup_prepare()
  347. {
  348. // Configure all RTC IOs selected as ext1 wakeup inputs
  349. uint32_t rtc_gpio_mask = s_config.ext1_rtc_gpio_mask;
  350. for (int gpio = 0; gpio < GPIO_PIN_COUNT && rtc_gpio_mask != 0; ++gpio) {
  351. int rtc_pin = rtc_gpio_desc[gpio].rtc_num;
  352. if ((rtc_gpio_mask & BIT(rtc_pin)) == 0) {
  353. continue;
  354. }
  355. const rtc_gpio_desc_t* desc = &rtc_gpio_desc[gpio];
  356. // Route pad to RTC
  357. REG_SET_BIT(desc->reg, desc->mux);
  358. SET_PERI_REG_BITS(desc->reg, 0x3, 0, desc->func);
  359. // Pad configuration depends on RTC_PERIPH state in sleep mode
  360. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] == ESP_PD_OPTION_ON) {
  361. // set input enable in sleep mode
  362. REG_SET_BIT(desc->reg, desc->slpie);
  363. // allow sleep status signal to control IE/SLPIE mux
  364. REG_SET_BIT(desc->reg, desc->slpsel);
  365. } else {
  366. // RTC_PERIPH will be disabled, so need to enable input and
  367. // lock pad configuration. Pullups/pulldowns also need to be disabled.
  368. REG_SET_BIT(desc->reg, desc->ie);
  369. REG_CLR_BIT(desc->reg, desc->pulldown);
  370. REG_CLR_BIT(desc->reg, desc->pullup);
  371. REG_SET_BIT(RTC_CNTL_HOLD_FORCE_REG, desc->hold_force);
  372. }
  373. // Keep track of pins which are processed to bail out early
  374. rtc_gpio_mask &= ~BIT(rtc_pin);
  375. }
  376. // Clear state from previous wakeup
  377. REG_SET_BIT(RTC_CNTL_EXT_WAKEUP1_REG, RTC_CNTL_EXT_WAKEUP1_STATUS_CLR);
  378. // Set pins to be used for wakeup
  379. REG_SET_FIELD(RTC_CNTL_EXT_WAKEUP1_REG, RTC_CNTL_EXT_WAKEUP1_SEL, s_config.ext1_rtc_gpio_mask);
  380. // Set logic function (any low, all high)
  381. SET_PERI_REG_BITS(RTC_CNTL_EXT_WAKEUP_CONF_REG, 0x1,
  382. s_config.ext1_trigger_mode, RTC_CNTL_EXT_WAKEUP1_LV_S);
  383. }
  384. uint64_t esp_sleep_get_ext1_wakeup_status()
  385. {
  386. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_EXT1) {
  387. return 0;
  388. }
  389. uint32_t status = REG_GET_FIELD(RTC_CNTL_EXT_WAKEUP1_STATUS_REG, RTC_CNTL_EXT_WAKEUP1_STATUS);
  390. // Translate bit map of RTC IO numbers into the bit map of GPIO numbers
  391. uint64_t gpio_mask = 0;
  392. for (int gpio = 0; gpio < GPIO_PIN_COUNT; ++gpio) {
  393. if (!RTC_GPIO_IS_VALID_GPIO(gpio)) {
  394. continue;
  395. }
  396. int rtc_pin = rtc_gpio_desc[gpio].rtc_num;
  397. if ((status & BIT(rtc_pin)) == 0) {
  398. continue;
  399. }
  400. gpio_mask |= 1ULL << gpio;
  401. }
  402. return gpio_mask;
  403. }
  404. esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause()
  405. {
  406. if (rtc_get_reset_reason(0) != DEEPSLEEP_RESET) {
  407. return ESP_SLEEP_WAKEUP_UNDEFINED;
  408. }
  409. uint32_t wakeup_cause = REG_GET_FIELD(RTC_CNTL_WAKEUP_STATE_REG, RTC_CNTL_WAKEUP_CAUSE);
  410. if (wakeup_cause & RTC_EXT0_TRIG_EN) {
  411. return ESP_SLEEP_WAKEUP_EXT0;
  412. } else if (wakeup_cause & RTC_EXT1_TRIG_EN) {
  413. return ESP_SLEEP_WAKEUP_EXT1;
  414. } else if (wakeup_cause & RTC_TIMER_TRIG_EN) {
  415. return ESP_SLEEP_WAKEUP_TIMER;
  416. } else if (wakeup_cause & RTC_TOUCH_TRIG_EN) {
  417. return ESP_SLEEP_WAKEUP_TOUCHPAD;
  418. } else if (wakeup_cause & RTC_ULP_TRIG_EN) {
  419. return ESP_SLEEP_WAKEUP_ULP;
  420. } else {
  421. return ESP_SLEEP_WAKEUP_UNDEFINED;
  422. }
  423. }
  424. esp_err_t esp_sleep_pd_config(esp_sleep_pd_domain_t domain,
  425. esp_sleep_pd_option_t option)
  426. {
  427. if (domain >= ESP_PD_DOMAIN_MAX || option > ESP_PD_OPTION_AUTO) {
  428. return ESP_ERR_INVALID_ARG;
  429. }
  430. s_config.pd_options[domain] = option;
  431. return ESP_OK;
  432. }
  433. static uint32_t get_power_down_flags()
  434. {
  435. // Where needed, convert AUTO options to ON. Later interpret AUTO as OFF.
  436. // RTC_SLOW_MEM is needed for the ULP, so keep RTC_SLOW_MEM powered up if ULP
  437. // is used and RTC_SLOW_MEM is Auto.
  438. // If there is any data placed into .rtc.data or .rtc.bss segments, and
  439. // RTC_SLOW_MEM is Auto, keep it powered up as well.
  440. // These labels are defined in the linker script:
  441. extern int _rtc_data_start, _rtc_data_end, _rtc_bss_start, _rtc_bss_end;
  442. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] == ESP_PD_OPTION_AUTO ||
  443. &_rtc_data_end > &_rtc_data_start ||
  444. &_rtc_bss_end > &_rtc_bss_start) {
  445. s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] = ESP_PD_OPTION_ON;
  446. }
  447. // RTC_FAST_MEM is needed for deep sleep stub.
  448. // If RTC_FAST_MEM is Auto, keep it powered on, so that deep sleep stub
  449. // can run.
  450. // In the new chip revision, deep sleep stub will be optional,
  451. // and this can be changed.
  452. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] == ESP_PD_OPTION_AUTO) {
  453. s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] = ESP_PD_OPTION_ON;
  454. }
  455. // RTC_PERIPH is needed for EXT0 wakeup.
  456. // If RTC_PERIPH is auto, and EXT0 isn't enabled, power down RTC_PERIPH.
  457. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] == ESP_PD_OPTION_AUTO) {
  458. if (s_config.wakeup_triggers & RTC_EXT0_TRIG_EN) {
  459. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_ON;
  460. } else if (s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) {
  461. // In both rev. 0 and rev. 1 of ESP32, forcing power up of RTC_PERIPH
  462. // prevents ULP timer and touch FSMs from working correctly.
  463. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_OFF;
  464. }
  465. }
  466. const char* option_str[] = {"OFF", "ON", "AUTO(OFF)" /* Auto works as OFF */};
  467. ESP_LOGD(TAG, "RTC_PERIPH: %s, RTC_SLOW_MEM: %s, RTC_FAST_MEM: %s",
  468. option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH]],
  469. option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM]],
  470. option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM]]);
  471. // Prepare flags based on the selected options
  472. uint32_t pd_flags = 0;
  473. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] != ESP_PD_OPTION_ON) {
  474. pd_flags |= RTC_SLEEP_PD_RTC_FAST_MEM;
  475. }
  476. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] != ESP_PD_OPTION_ON) {
  477. pd_flags |= RTC_SLEEP_PD_RTC_SLOW_MEM;
  478. }
  479. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] != ESP_PD_OPTION_ON) {
  480. pd_flags |= RTC_SLEEP_PD_RTC_PERIPH;
  481. }
  482. return pd_flags;
  483. }