sleep_modes.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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 <string.h>
  16. #include <sys/lock.h>
  17. #include <sys/param.h>
  18. #include "esp_attr.h"
  19. #include "esp_sleep.h"
  20. #include "esp_private/esp_timer_private.h"
  21. #include "esp_private/system_internal.h"
  22. #include "esp_log.h"
  23. #include "esp_newlib.h"
  24. #include "esp_timer.h"
  25. #include "freertos/FreeRTOS.h"
  26. #include "freertos/task.h"
  27. #include "soc/soc_caps.h"
  28. #include "driver/rtc_io.h"
  29. #include "hal/rtc_io_hal.h"
  30. #include "driver/uart.h"
  31. #include "soc/cpu.h"
  32. #include "soc/rtc.h"
  33. #include "soc/soc_caps.h"
  34. #include "hal/wdt_hal.h"
  35. #include "hal/rtc_hal.h"
  36. #include "hal/uart_hal.h"
  37. #if SOC_TOUCH_SENSOR_NUM > 0
  38. #include "hal/touch_sensor_hal.h"
  39. #include "driver/touch_sensor.h"
  40. #include "driver/touch_sensor_common.h"
  41. #endif
  42. #include "hal/clk_gate_ll.h"
  43. #include "sdkconfig.h"
  44. #include "esp_rom_uart.h"
  45. #ifdef CONFIG_IDF_TARGET_ESP32
  46. #include "esp32/rom/cache.h"
  47. #include "esp32/clk.h"
  48. #include "esp32/rom/rtc.h"
  49. #include "driver/gpio.h"
  50. #elif CONFIG_IDF_TARGET_ESP32S2
  51. #include "esp32s2/clk.h"
  52. #include "esp32s2/rom/cache.h"
  53. #include "esp32s2/rom/rtc.h"
  54. #include "esp32s2/brownout.h"
  55. #include "soc/extmem_reg.h"
  56. #include "driver/gpio.h"
  57. #elif CONFIG_IDF_TARGET_ESP32S3
  58. #include "esp32s3/clk.h"
  59. #include "esp32s3/rom/cache.h"
  60. #include "esp32s3/rom/rtc.h"
  61. #include "soc/extmem_reg.h"
  62. #elif CONFIG_IDF_TARGET_ESP32C3
  63. #include "esp32c3/clk.h"
  64. #include "esp32s3/rom/cache.h"
  65. #include "esp32c3/rom/rtc.h"
  66. #include "soc/extmem_reg.h"
  67. #include "esp_heap_caps.h"
  68. #endif
  69. // If light sleep time is less than that, don't power down flash
  70. #define FLASH_PD_MIN_SLEEP_TIME_US 2000
  71. // Time from VDD_SDIO power up to first flash read in ROM code
  72. #define VDD_SDIO_POWERUP_TO_FLASH_READ_US 700
  73. // Cycles for RTC Timer clock source (internal oscillator) calibrate
  74. #define RTC_CLK_SRC_CAL_CYCLES (10)
  75. #ifdef CONFIG_IDF_TARGET_ESP32
  76. #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ
  77. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (212)
  78. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (60)
  79. #elif CONFIG_IDF_TARGET_ESP32S2
  80. #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S2_DEFAULT_CPU_FREQ_MHZ
  81. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (147)
  82. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (28)
  83. #elif CONFIG_IDF_TARGET_ESP32S3
  84. #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ
  85. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (0)
  86. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (0)
  87. #elif CONFIG_IDF_TARGET_ESP32C3
  88. #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32C3_DEFAULT_CPU_FREQ_MHZ
  89. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (105)
  90. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (37)
  91. #endif
  92. #define LIGHT_SLEEP_TIME_OVERHEAD_US DEFAULT_HARDWARE_OUT_OVERHEAD_US
  93. #if defined(CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS) || \
  94. defined(CONFIG_ESP32S2_RTC_CLK_SRC_EXT_CRYS) || \
  95. defined(CONFIG_ESP32C3_RTC_CLK_SRC_EXT_CRYS) || \
  96. defined(CONFIG_ESP32S3_RTC_CLK_SRC_EXT_CRYS)
  97. #define DEEP_SLEEP_TIME_OVERHEAD_US (650 + 100 * 240 / DEFAULT_CPU_FREQ_MHZ)
  98. #else
  99. #define DEEP_SLEEP_TIME_OVERHEAD_US (250 + 100 * 240 / DEFAULT_CPU_FREQ_MHZ)
  100. #endif
  101. #if defined(CONFIG_IDF_TARGET_ESP32) && defined(CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY)
  102. #define DEEP_SLEEP_WAKEUP_DELAY CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY
  103. #else
  104. #define DEEP_SLEEP_WAKEUP_DELAY 0
  105. #endif
  106. extern void periph_inform_out_light_sleep_overhead(uint32_t out_light_sleep_time);
  107. // Minimal amount of time we can sleep for
  108. #define LIGHT_SLEEP_MIN_TIME_US 200
  109. #define RTC_MODULE_SLEEP_PREPARE_CYCLES (6)
  110. #define CHECK_SOURCE(source, value, mask) ((s_config.wakeup_triggers & mask) && \
  111. (source == value))
  112. /**
  113. * Internal structure which holds all requested deep sleep parameters
  114. */
  115. typedef struct {
  116. esp_sleep_pd_option_t pd_options[ESP_PD_DOMAIN_MAX];
  117. uint64_t sleep_duration;
  118. uint32_t wakeup_triggers : 15;
  119. uint32_t ext1_trigger_mode : 1;
  120. uint32_t ext1_rtc_gpio_mask : 18;
  121. uint32_t ext0_trigger_level : 1;
  122. uint32_t ext0_rtc_gpio_num : 5;
  123. uint32_t gpio_wakeup_mask : 6;
  124. uint32_t gpio_trigger_mode : 6;
  125. uint32_t sleep_time_adjustment;
  126. uint32_t ccount_ticks_record;
  127. uint32_t sleep_time_overhead_out;
  128. uint32_t rtc_clk_cal_period;
  129. uint64_t rtc_ticks_at_sleep_start;
  130. #if SOC_PM_SUPPORT_CPU_PD
  131. void *cpu_pd_mem;
  132. #endif
  133. } sleep_config_t;
  134. static sleep_config_t s_config = {
  135. .pd_options = { ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO },
  136. .ccount_ticks_record = 0,
  137. .sleep_time_overhead_out = DEFAULT_SLEEP_OUT_OVERHEAD_US,
  138. .wakeup_triggers = 0
  139. };
  140. /* Internal variable used to track if light sleep wakeup sources are to be
  141. expected when determining wakeup cause. */
  142. static bool s_light_sleep_wakeup = false;
  143. /* Updating RTC_MEMORY_CRC_REG register via set_rtc_memory_crc()
  144. is not thread-safe, so we need to disable interrupts before going to deep sleep. */
  145. static portMUX_TYPE spinlock_rtc_deep_sleep = portMUX_INITIALIZER_UNLOCKED;
  146. static const char *TAG = "sleep";
  147. static uint32_t get_power_down_flags(void);
  148. #if SOC_PM_SUPPORT_EXT_WAKEUP
  149. static void ext0_wakeup_prepare(void);
  150. static void ext1_wakeup_prepare(void);
  151. #endif
  152. static void timer_wakeup_prepare(void);
  153. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  154. static void touch_wakeup_prepare(void);
  155. #endif
  156. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  157. static void esp_deep_sleep_wakeup_prepare(void);
  158. #endif
  159. #if CONFIG_MAC_BB_PD
  160. #define MAC_BB_POWER_DOWN_CB_NO 2
  161. #define MAC_BB_POWER_UP_CB_NO 2
  162. static DRAM_ATTR mac_bb_power_down_cb_t s_mac_bb_power_down_cb[MAC_BB_POWER_DOWN_CB_NO];
  163. static DRAM_ATTR mac_bb_power_up_cb_t s_mac_bb_power_up_cb[MAC_BB_POWER_UP_CB_NO];
  164. esp_err_t esp_register_mac_bb_pd_callback(mac_bb_power_down_cb_t cb)
  165. {
  166. int index = MAC_BB_POWER_DOWN_CB_NO;
  167. for (int i = MAC_BB_POWER_DOWN_CB_NO - 1; i >= 0; i--) {
  168. if (s_mac_bb_power_down_cb[i] == cb) {
  169. return ESP_ERR_INVALID_STATE;
  170. }
  171. if (s_mac_bb_power_down_cb[i] == NULL) {
  172. index = i;
  173. }
  174. }
  175. if (index < MAC_BB_POWER_DOWN_CB_NO) {
  176. s_mac_bb_power_down_cb[index] = cb;
  177. return ESP_OK;
  178. }
  179. return ESP_ERR_NO_MEM;
  180. }
  181. esp_err_t esp_unregister_mac_bb_pd_callback(mac_bb_power_down_cb_t cb)
  182. {
  183. for (int i = MAC_BB_POWER_DOWN_CB_NO - 1; i >= 0; i--) {
  184. if (s_mac_bb_power_down_cb[i] == cb) {
  185. s_mac_bb_power_down_cb[i] = NULL;
  186. return ESP_OK;
  187. }
  188. }
  189. return ESP_ERR_INVALID_STATE;
  190. }
  191. static IRAM_ATTR void mac_bb_power_down_cb_execute(void)
  192. {
  193. for (int i = 0; i < MAC_BB_POWER_DOWN_CB_NO; i++) {
  194. if (s_mac_bb_power_down_cb[i]) {
  195. s_mac_bb_power_down_cb[i]();
  196. }
  197. }
  198. }
  199. esp_err_t esp_register_mac_bb_pu_callback(mac_bb_power_up_cb_t cb)
  200. {
  201. int index = MAC_BB_POWER_UP_CB_NO;
  202. for (int i = MAC_BB_POWER_UP_CB_NO - 1; i >= 0; i--) {
  203. if (s_mac_bb_power_up_cb[i] == cb) {
  204. return ESP_ERR_INVALID_STATE;
  205. }
  206. if (s_mac_bb_power_up_cb[i] == NULL) {
  207. index = i;
  208. }
  209. }
  210. if (index < MAC_BB_POWER_UP_CB_NO) {
  211. s_mac_bb_power_up_cb[index] = cb;
  212. return ESP_OK;
  213. }
  214. return ESP_ERR_NO_MEM;
  215. }
  216. esp_err_t esp_unregister_mac_bb_pu_callback(mac_bb_power_up_cb_t cb)
  217. {
  218. for (int i = MAC_BB_POWER_UP_CB_NO - 1; i >= 0; i--) {
  219. if (s_mac_bb_power_up_cb[i] == cb) {
  220. s_mac_bb_power_up_cb[i] = NULL;
  221. return ESP_OK;
  222. }
  223. }
  224. return ESP_ERR_INVALID_STATE;
  225. }
  226. static IRAM_ATTR void mac_bb_power_up_cb_execute(void)
  227. {
  228. for (int i = 0; i < MAC_BB_POWER_UP_CB_NO; i++) {
  229. if (s_mac_bb_power_up_cb[i]) {
  230. s_mac_bb_power_up_cb[i]();
  231. }
  232. }
  233. }
  234. #endif ///CONFIG_MAC_BB_PD
  235. /* Wake from deep sleep stub
  236. See esp_deepsleep.h esp_wake_deep_sleep() comments for details.
  237. */
  238. esp_deep_sleep_wake_stub_fn_t esp_get_deep_sleep_wake_stub(void)
  239. {
  240. esp_deep_sleep_wake_stub_fn_t stub_ptr = (esp_deep_sleep_wake_stub_fn_t) REG_READ(RTC_ENTRY_ADDR_REG);
  241. if (!esp_ptr_executable(stub_ptr)) {
  242. return NULL;
  243. }
  244. return stub_ptr;
  245. }
  246. void esp_set_deep_sleep_wake_stub(esp_deep_sleep_wake_stub_fn_t new_stub)
  247. {
  248. REG_WRITE(RTC_ENTRY_ADDR_REG, (uint32_t)new_stub);
  249. }
  250. void RTC_IRAM_ATTR esp_default_wake_deep_sleep(void)
  251. {
  252. /* Clear MMU for CPU 0 */
  253. #if CONFIG_IDF_TARGET_ESP32
  254. _DPORT_REG_WRITE(DPORT_PRO_CACHE_CTRL1_REG,
  255. _DPORT_REG_READ(DPORT_PRO_CACHE_CTRL1_REG) | DPORT_PRO_CACHE_MMU_IA_CLR);
  256. _DPORT_REG_WRITE(DPORT_PRO_CACHE_CTRL1_REG,
  257. _DPORT_REG_READ(DPORT_PRO_CACHE_CTRL1_REG) & (~DPORT_PRO_CACHE_MMU_IA_CLR));
  258. #if DEEP_SLEEP_WAKEUP_DELAY > 0
  259. // ROM code has not started yet, so we need to set delay factor
  260. // used by esp_rom_delay_us first.
  261. ets_update_cpu_frequency_rom(ets_get_detected_xtal_freq() / 1000000);
  262. // This delay is configured in menuconfig, it can be used to give
  263. // the flash chip some time to become ready.
  264. esp_rom_delay_us(DEEP_SLEEP_WAKEUP_DELAY);
  265. #endif
  266. #elif CONFIG_IDF_TARGET_ESP32S2
  267. REG_SET_BIT(EXTMEM_CACHE_DBG_INT_ENA_REG, EXTMEM_CACHE_DBG_EN);
  268. #endif
  269. }
  270. void __attribute__((weak, alias("esp_default_wake_deep_sleep"))) esp_wake_deep_sleep(void);
  271. void esp_deep_sleep(uint64_t time_in_us)
  272. {
  273. esp_sleep_enable_timer_wakeup(time_in_us);
  274. esp_deep_sleep_start();
  275. }
  276. // [refactor-todo] provide target logic for body of uart functions below
  277. static void IRAM_ATTR flush_uarts(void)
  278. {
  279. for (int i = 0; i < SOC_UART_NUM; ++i) {
  280. #ifdef CONFIG_IDF_TARGET_ESP32
  281. esp_rom_uart_tx_wait_idle(i);
  282. #else
  283. if (periph_ll_periph_enabled(PERIPH_UART0_MODULE + i)) {
  284. esp_rom_uart_tx_wait_idle(i);
  285. }
  286. #endif
  287. }
  288. }
  289. static void IRAM_ATTR suspend_uarts(void)
  290. {
  291. for (int i = 0; i < SOC_UART_NUM; ++i) {
  292. #ifndef CONFIG_IDF_TARGET_ESP32
  293. if (!periph_ll_periph_enabled(PERIPH_UART0_MODULE + i)) {
  294. continue;
  295. }
  296. #endif
  297. uart_ll_force_xoff(i);
  298. #if SOC_UART_SUPPORT_FSM_TX_WAIT_SEND
  299. uint32_t uart_fsm = 0;
  300. do {
  301. uart_fsm = uart_ll_get_fsm_status(i);
  302. } while (!(uart_fsm == UART_FSM_IDLE || uart_fsm == UART_FSM_TX_WAIT_SEND));
  303. #else
  304. while (uart_ll_get_fsm_status(i) != 0) {}
  305. #endif
  306. }
  307. }
  308. static void IRAM_ATTR resume_uarts(void)
  309. {
  310. for (int i = 0; i < SOC_UART_NUM; ++i) {
  311. #ifndef CONFIG_IDF_TARGET_ESP32
  312. if (!periph_ll_periph_enabled(PERIPH_UART0_MODULE + i)) {
  313. continue;
  314. }
  315. #endif
  316. uart_ll_force_xon(i);
  317. }
  318. }
  319. inline static uint32_t IRAM_ATTR call_rtc_sleep_start(uint32_t reject_triggers);
  320. #if SOC_PM_SUPPORT_CPU_PD
  321. esp_err_t esp_sleep_cpu_pd_low_init(bool enable)
  322. {
  323. if (enable) {
  324. if (s_config.cpu_pd_mem == NULL) {
  325. void *buf = heap_caps_aligned_alloc(RTC_CNTL_CPU_PD_DMA_ADDR_ALIGN,
  326. RTC_CNTL_CPU_PD_RETENTION_MEM_SIZE + RTC_HAL_DMA_LINK_NODE_SIZE,
  327. MALLOC_CAP_RETENTION | MALLOC_CAP_DEFAULT);
  328. if (buf) {
  329. memset(buf, 0, RTC_CNTL_CPU_PD_RETENTION_MEM_SIZE + RTC_HAL_DMA_LINK_NODE_SIZE);
  330. s_config.cpu_pd_mem = rtc_cntl_hal_dma_link_init(buf,
  331. buf + RTC_HAL_DMA_LINK_NODE_SIZE, RTC_CNTL_CPU_PD_RETENTION_MEM_SIZE, NULL);
  332. } else {
  333. return ESP_ERR_NO_MEM;
  334. }
  335. }
  336. } else {
  337. if (s_config.cpu_pd_mem) {
  338. heap_caps_free(s_config.cpu_pd_mem);
  339. s_config.cpu_pd_mem = NULL;
  340. }
  341. }
  342. return ESP_OK;
  343. }
  344. #endif // SOC_PM_SUPPORT_CPU_PD
  345. #if SOC_GPIO_SUPPORT_SLP_SWITCH
  346. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  347. static inline void gpio_sleep_mode_config_apply(void)
  348. {
  349. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  350. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  351. gpio_sleep_pupd_config_apply(gpio_num);
  352. }
  353. }
  354. }
  355. static inline void gpio_sleep_mode_config_unapply(void)
  356. {
  357. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  358. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  359. gpio_sleep_pupd_config_unapply(gpio_num);
  360. }
  361. }
  362. }
  363. #endif
  364. void esp_sleep_config_gpio_isolate(void)
  365. {
  366. ESP_LOGI(TAG, "Configure to isolate all GPIO pins in sleep state");
  367. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  368. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  369. gpio_sleep_set_direction(gpio_num, GPIO_MODE_DISABLE);
  370. gpio_sleep_set_pull_mode(gpio_num, GPIO_FLOATING);
  371. }
  372. }
  373. }
  374. void esp_sleep_enable_gpio_switch(bool enable)
  375. {
  376. ESP_LOGI(TAG, "%s automatic switching of GPIO sleep configuration", enable ? "Enable" : "Disable");
  377. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  378. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  379. if (enable) {
  380. gpio_sleep_sel_en(gpio_num);
  381. } else {
  382. gpio_sleep_sel_dis(gpio_num);
  383. }
  384. }
  385. }
  386. }
  387. #endif // SOC_GPIO_SUPPORT_SLP_SWITCH
  388. static uint32_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags)
  389. {
  390. // Stop UART output so that output is not lost due to APB frequency change.
  391. // For light sleep, suspend UART output — it will resume after wakeup.
  392. // For deep sleep, wait for the contents of UART FIFO to be sent.
  393. bool deep_sleep = pd_flags & RTC_SLEEP_PD_DIG;
  394. #if !CONFIG_FREERTOS_UNICORE && CONFIG_IDF_TARGET_ESP32S3 && CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  395. /* Currently only safe to use deep sleep wake stub & RTC memory as heap in single core mode.
  396. For ESP32-S3, either disable ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP in config or find a way to set the
  397. deep sleep wake stub to NULL.
  398. */
  399. assert(!deep_sleep || esp_get_deep_sleep_wake_stub() == NULL);
  400. #endif
  401. if (deep_sleep) {
  402. flush_uarts();
  403. } else {
  404. suspend_uarts();
  405. }
  406. // Save current frequency and switch to XTAL
  407. rtc_cpu_freq_config_t cpu_freq_config;
  408. rtc_clk_cpu_freq_get_config(&cpu_freq_config);
  409. rtc_clk_cpu_freq_set_xtal();
  410. #if CONFIG_MAC_BB_PD
  411. mac_bb_power_down_cb_execute();
  412. #endif
  413. #if SOC_PM_SUPPORT_EXT_WAKEUP
  414. // Configure pins for external wakeup
  415. if (s_config.wakeup_triggers & RTC_EXT0_TRIG_EN) {
  416. ext0_wakeup_prepare();
  417. }
  418. if (s_config.wakeup_triggers & RTC_EXT1_TRIG_EN) {
  419. ext1_wakeup_prepare();
  420. }
  421. #endif
  422. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  423. if (s_config.wakeup_triggers & RTC_GPIO_TRIG_EN) {
  424. esp_deep_sleep_wakeup_prepare();
  425. }
  426. #endif
  427. #ifdef CONFIG_IDF_TARGET_ESP32
  428. // Enable ULP wakeup
  429. if (s_config.wakeup_triggers & RTC_ULP_TRIG_EN) {
  430. rtc_hal_ulp_wakeup_enable();
  431. }
  432. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  433. gpio_sleep_mode_config_apply();
  434. #endif
  435. #endif
  436. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  437. if (deep_sleep) {
  438. if (s_config.wakeup_triggers & RTC_TOUCH_TRIG_EN) {
  439. touch_wakeup_prepare();
  440. /* Workaround: In deep sleep, for ESP32S2, Power down the RTC_PERIPH will change the slope configuration of Touch sensor sleep pad.
  441. * The configuration change will change the reading of the sleep pad, which will cause the touch wake-up sensor to trigger falsely.
  442. */
  443. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  444. }
  445. } else {
  446. /* In light sleep, the RTC_PERIPH power domain should be in the power-on state (Power on the touch circuit in light sleep),
  447. * otherwise the touch sensor FSM will be cleared, causing touch sensor false triggering.
  448. */
  449. if (touch_ll_get_fsm_state()) { // Check if the touch sensor is working properly.
  450. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  451. }
  452. }
  453. #endif
  454. uint32_t reject_triggers = 0;
  455. if ((pd_flags & RTC_SLEEP_PD_DIG) == 0 && (s_config.wakeup_triggers & RTC_GPIO_TRIG_EN)) {
  456. /* Light sleep, enable sleep reject for faster return from this function,
  457. * in case the wakeup is already triggerred.
  458. */
  459. #if CONFIG_IDF_TARGET_ESP32
  460. reject_triggers = RTC_CNTL_LIGHT_SLP_REJECT_EN_M | RTC_CNTL_GPIO_REJECT_EN_M;
  461. #else
  462. reject_triggers = s_config.wakeup_triggers;
  463. #endif
  464. }
  465. // Enter sleep
  466. rtc_sleep_config_t config = RTC_SLEEP_CONFIG_DEFAULT(pd_flags);
  467. rtc_sleep_init(config);
  468. rtc_sleep_low_init(s_config.rtc_clk_cal_period);
  469. // Set state machine time for light sleep
  470. if (!deep_sleep) {
  471. rtc_sleep_low_init(s_config.rtc_clk_cal_period);
  472. }
  473. // Configure timer wakeup
  474. if ((s_config.wakeup_triggers & RTC_TIMER_TRIG_EN) &&
  475. s_config.sleep_duration > 0) {
  476. timer_wakeup_prepare();
  477. }
  478. uint32_t result;
  479. if (deep_sleep) {
  480. /* Disable interrupts in case another task writes to RTC memory while we
  481. * calculate RTC memory CRC
  482. *
  483. * Note: for ESP32-S3 running in dual core mode this is currently not enough,
  484. * see the assert at top of this function.
  485. */
  486. portENTER_CRITICAL(&spinlock_rtc_deep_sleep);
  487. #if !CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  488. /* If not possible stack is in RTC FAST memory, use the ROM function to calculate the CRC and save ~140 bytes IRAM */
  489. set_rtc_memory_crc();
  490. result = call_rtc_sleep_start(reject_triggers);
  491. #else
  492. /* Otherwise, need to call the dedicated soc function for this */
  493. result = rtc_deep_sleep_start(s_config.wakeup_triggers, reject_triggers);
  494. #endif
  495. portEXIT_CRITICAL(&spinlock_rtc_deep_sleep);
  496. } else {
  497. result = call_rtc_sleep_start(reject_triggers);
  498. }
  499. // Restore CPU frequency
  500. rtc_clk_cpu_freq_set_config(&cpu_freq_config);
  501. if (!deep_sleep) {
  502. s_config.ccount_ticks_record = cpu_ll_get_cycle_count();
  503. }
  504. #if SOC_PM_SUPPORT_CPU_PD
  505. rtc_cntl_hal_disable_cpu_retention();
  506. #endif
  507. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  508. gpio_sleep_mode_config_unapply();
  509. #endif
  510. #if CONFIG_MAC_BB_PD
  511. mac_bb_power_up_cb_execute();
  512. #endif
  513. // re-enable UART output
  514. resume_uarts();
  515. return result;
  516. }
  517. inline static uint32_t IRAM_ATTR call_rtc_sleep_start(uint32_t reject_triggers)
  518. {
  519. #ifdef CONFIG_IDF_TARGET_ESP32
  520. return rtc_sleep_start(s_config.wakeup_triggers, reject_triggers);
  521. #else
  522. return rtc_sleep_start(s_config.wakeup_triggers, reject_triggers, 1);
  523. #endif
  524. }
  525. void IRAM_ATTR esp_deep_sleep_start(void)
  526. {
  527. #if CONFIG_IDF_TARGET_ESP32S2
  528. /* Due to hardware limitations, on S2 the brownout detector sometimes trigger during deep sleep
  529. to circumvent this we disable the brownout detector before sleeping */
  530. esp_brownout_disable();
  531. #endif //CONFIG_IDF_TARGET_ESP32S2
  532. // record current RTC time
  533. s_config.rtc_ticks_at_sleep_start = rtc_time_get();
  534. // record current RTC time
  535. esp_sync_counters_rtc_and_frc();
  536. // Configure wake stub
  537. if (esp_get_deep_sleep_wake_stub() == NULL) {
  538. esp_set_deep_sleep_wake_stub(esp_wake_deep_sleep);
  539. }
  540. // Decide which power domains can be powered down
  541. uint32_t pd_flags = get_power_down_flags();
  542. s_config.rtc_clk_cal_period = esp_clk_slowclk_cal_get();
  543. // Correct the sleep time
  544. s_config.sleep_time_adjustment = DEEP_SLEEP_TIME_OVERHEAD_US;
  545. uint32_t force_pd_flags = RTC_SLEEP_PD_DIG | RTC_SLEEP_PD_VDDSDIO;
  546. #if SOC_PM_SUPPORT_WIFI_PD
  547. force_pd_flags |= RTC_SLEEP_PD_WIFI;
  548. #endif
  549. #if SOC_PM_SUPPORT_BT_PD
  550. force_pd_flags |= RTC_SLEEP_PD_BT;
  551. #endif
  552. // Enter sleep
  553. esp_sleep_start(force_pd_flags | pd_flags);
  554. // Because RTC is in a slower clock domain than the CPU, it
  555. // can take several CPU cycles for the sleep mode to start.
  556. while (1) {
  557. ;
  558. }
  559. }
  560. /**
  561. * Helper function which handles entry to and exit from light sleep
  562. * Placed into IRAM as flash may need some time to be powered on.
  563. */
  564. static esp_err_t esp_light_sleep_inner(uint32_t pd_flags,
  565. uint32_t flash_enable_time_us,
  566. rtc_vddsdio_config_t vddsdio_config) IRAM_ATTR __attribute__((noinline));
  567. static esp_err_t esp_light_sleep_inner(uint32_t pd_flags,
  568. uint32_t flash_enable_time_us,
  569. rtc_vddsdio_config_t vddsdio_config)
  570. {
  571. // Enter sleep
  572. esp_err_t err = esp_sleep_start(pd_flags);
  573. // If VDDSDIO regulator was controlled by RTC registers before sleep,
  574. // restore the configuration.
  575. if (vddsdio_config.force) {
  576. rtc_vddsdio_set_config(vddsdio_config);
  577. }
  578. // If SPI flash was powered down, wait for it to become ready
  579. if (pd_flags & RTC_SLEEP_PD_VDDSDIO) {
  580. // Wait for the flash chip to start up
  581. esp_rom_delay_us(flash_enable_time_us);
  582. }
  583. return err;
  584. }
  585. esp_err_t esp_light_sleep_start(void)
  586. {
  587. s_config.ccount_ticks_record = cpu_ll_get_cycle_count();
  588. static portMUX_TYPE light_sleep_lock = portMUX_INITIALIZER_UNLOCKED;
  589. portENTER_CRITICAL(&light_sleep_lock);
  590. /* We will be calling esp_timer_private_advance inside DPORT access critical
  591. * section. Make sure the code on the other CPU is not holding esp_timer
  592. * lock, otherwise there will be deadlock.
  593. */
  594. esp_timer_private_lock();
  595. s_config.rtc_ticks_at_sleep_start = rtc_time_get();
  596. uint32_t ccount_at_sleep_start = cpu_ll_get_cycle_count();
  597. uint64_t frc_time_at_start = esp_system_get_time();
  598. uint32_t sleep_time_overhead_in = (ccount_at_sleep_start - s_config.ccount_ticks_record) / (esp_clk_cpu_freq() / 1000000ULL);
  599. DPORT_STALL_OTHER_CPU_START();
  600. // Decide which power domains can be powered down
  601. uint32_t pd_flags = get_power_down_flags();
  602. // Re-calibrate the RTC Timer clock
  603. #if defined(CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS) || defined(CONFIG_ESP32S2_RTC_CLK_SRC_EXT_CRYS) || defined(CONFIG_ESP32C3_RTC_CLK_SRC_EXT_CRYS)
  604. uint64_t time_per_us = 1000000ULL;
  605. s_config.rtc_clk_cal_period = (time_per_us << RTC_CLK_CAL_FRACT) / rtc_clk_slow_freq_get_hz();
  606. #elif defined(CONFIG_ESP32S2_RTC_CLK_SRC_INT_RC)
  607. s_config.rtc_clk_cal_period = rtc_clk_cal_cycling(RTC_CAL_RTC_MUX, RTC_CLK_SRC_CAL_CYCLES);
  608. esp_clk_slowclk_cal_set(s_config.rtc_clk_cal_period);
  609. #else
  610. s_config.rtc_clk_cal_period = rtc_clk_cal(RTC_CAL_RTC_MUX, RTC_CLK_SRC_CAL_CYCLES);
  611. #endif
  612. /*
  613. * Adjustment time consists of parts below:
  614. * 1. Hardware time waiting for internal 8M oscilate clock and XTAL;
  615. * 2. Hardware state swithing time of the rtc main state machine;
  616. * 3. Code execution time when clock is not stable;
  617. * 4. Code execution time which can be measured;
  618. */
  619. uint32_t rtc_cntl_xtl_buf_wait_slp_cycles = rtc_time_us_to_slowclk(RTC_CNTL_XTL_BUF_WAIT_SLP_US, s_config.rtc_clk_cal_period);
  620. s_config.sleep_time_adjustment = LIGHT_SLEEP_TIME_OVERHEAD_US + sleep_time_overhead_in + s_config.sleep_time_overhead_out
  621. + rtc_time_slowclk_to_us(rtc_cntl_xtl_buf_wait_slp_cycles + RTC_CNTL_CK8M_WAIT_SLP_CYCLES + RTC_CNTL_WAKEUP_DELAY_CYCLES, s_config.rtc_clk_cal_period);
  622. // Decide if VDD_SDIO needs to be powered down;
  623. // If it needs to be powered down, adjust sleep time.
  624. const uint32_t flash_enable_time_us = VDD_SDIO_POWERUP_TO_FLASH_READ_US + DEEP_SLEEP_WAKEUP_DELAY;
  625. /**
  626. * If VDD_SDIO power domain is requested to be turned off, bit `RTC_SLEEP_PD_VDDSDIO`
  627. * will be set in `pd_flags`.
  628. */
  629. if (pd_flags & RTC_SLEEP_PD_VDDSDIO) {
  630. /*
  631. * When VDD_SDIO power domain has to be turned off, the minimum sleep time of the
  632. * system needs to meet the sum below:
  633. * 1. Wait time for the flash power-on after waking up;
  634. * 2. The execution time of codes between RTC Timer get start time
  635. * with hardware starts to switch state to sleep;
  636. * 3. The hardware state switching time of the rtc state machine during
  637. * sleep and wake-up. This process requires 6 cycles to complete.
  638. * The specific hardware state switching process and the cycles
  639. * consumed are rtc_cpu_run_stall(1), cut_pll_rtl(2), cut_8m(1),
  640. * min_protect(2);
  641. * 4. All the adjustment time which is s_config.sleep_time_adjustment below.
  642. */
  643. const uint32_t vddsdio_pd_sleep_duration = MAX(FLASH_PD_MIN_SLEEP_TIME_US,
  644. flash_enable_time_us + LIGHT_SLEEP_MIN_TIME_US + s_config.sleep_time_adjustment
  645. + rtc_time_slowclk_to_us(RTC_MODULE_SLEEP_PREPARE_CYCLES, s_config.rtc_clk_cal_period));
  646. if (s_config.sleep_duration > vddsdio_pd_sleep_duration) {
  647. if (s_config.sleep_time_overhead_out < flash_enable_time_us) {
  648. s_config.sleep_time_adjustment += flash_enable_time_us;
  649. }
  650. } else {
  651. /**
  652. * Minimum sleep time is not enough, then keep the VDD_SDIO power
  653. * domain on.
  654. */
  655. pd_flags &= ~RTC_SLEEP_PD_VDDSDIO;
  656. if (s_config.sleep_time_overhead_out > flash_enable_time_us) {
  657. s_config.sleep_time_adjustment -= flash_enable_time_us;
  658. }
  659. }
  660. }
  661. periph_inform_out_light_sleep_overhead(s_config.sleep_time_adjustment - sleep_time_overhead_in);
  662. #if SOC_PM_SUPPORT_CPU_PD
  663. rtc_cntl_hal_enable_cpu_retention(s_config.cpu_pd_mem);
  664. #endif
  665. rtc_vddsdio_config_t vddsdio_config = rtc_vddsdio_get_config();
  666. // Safety net: enable WDT in case exit from light sleep fails
  667. wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
  668. bool wdt_was_enabled = wdt_hal_is_enabled(&rtc_wdt_ctx); // If WDT was enabled in the user code, then do not change it here.
  669. if (!wdt_was_enabled) {
  670. wdt_hal_init(&rtc_wdt_ctx, WDT_RWDT, 0, false);
  671. uint32_t stage_timeout_ticks = (uint32_t)(1000ULL * rtc_clk_slow_freq_get_hz() / 1000ULL);
  672. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  673. wdt_hal_config_stage(&rtc_wdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_RTC);
  674. wdt_hal_enable(&rtc_wdt_ctx);
  675. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  676. }
  677. // Enter sleep, then wait for flash to be ready on wakeup
  678. esp_err_t err = esp_light_sleep_inner(pd_flags,
  679. flash_enable_time_us, vddsdio_config);
  680. s_light_sleep_wakeup = true;
  681. // FRC1 has been clock gated for the duration of the sleep, correct for that.
  682. #ifdef CONFIG_IDF_TARGET_ESP32C3
  683. /**
  684. * On esp32c3, rtc_time_get() is non-blocking, esp_system_get_time() is
  685. * blocking, and the measurement data shows that this order is better.
  686. */
  687. uint64_t frc_time_at_end = esp_system_get_time();
  688. uint64_t rtc_ticks_at_end = rtc_time_get();
  689. #else
  690. uint64_t rtc_ticks_at_end = rtc_time_get();
  691. uint64_t frc_time_at_end = esp_system_get_time();
  692. #endif
  693. uint64_t rtc_time_diff = rtc_time_slowclk_to_us(rtc_ticks_at_end - s_config.rtc_ticks_at_sleep_start, s_config.rtc_clk_cal_period);
  694. uint64_t frc_time_diff = frc_time_at_end - frc_time_at_start;
  695. int64_t time_diff = rtc_time_diff - frc_time_diff;
  696. /* Small negative values (up to 1 RTC_SLOW clock period) are possible,
  697. * for very small values of sleep_duration. Ignore those to keep esp_timer
  698. * monotonic.
  699. */
  700. if (time_diff > 0) {
  701. esp_timer_private_advance(time_diff);
  702. }
  703. esp_set_time_from_rtc();
  704. esp_timer_private_unlock();
  705. DPORT_STALL_OTHER_CPU_END();
  706. if (!wdt_was_enabled) {
  707. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  708. wdt_hal_disable(&rtc_wdt_ctx);
  709. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  710. }
  711. portEXIT_CRITICAL(&light_sleep_lock);
  712. s_config.sleep_time_overhead_out = (cpu_ll_get_cycle_count() - s_config.ccount_ticks_record) / (esp_clk_cpu_freq() / 1000000ULL);
  713. return err;
  714. }
  715. esp_err_t esp_sleep_disable_wakeup_source(esp_sleep_source_t source)
  716. {
  717. // For most of sources it is enough to set trigger mask in local
  718. // configuration structure. The actual RTC wake up options
  719. // will be updated by esp_sleep_start().
  720. if (source == ESP_SLEEP_WAKEUP_ALL) {
  721. s_config.wakeup_triggers = 0;
  722. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_TIMER, RTC_TIMER_TRIG_EN)) {
  723. s_config.wakeup_triggers &= ~RTC_TIMER_TRIG_EN;
  724. s_config.sleep_duration = 0;
  725. #if SOC_PM_SUPPORT_EXT_WAKEUP
  726. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_EXT0, RTC_EXT0_TRIG_EN)) {
  727. s_config.ext0_rtc_gpio_num = 0;
  728. s_config.ext0_trigger_level = 0;
  729. s_config.wakeup_triggers &= ~RTC_EXT0_TRIG_EN;
  730. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_EXT1, RTC_EXT1_TRIG_EN)) {
  731. s_config.ext1_rtc_gpio_mask = 0;
  732. s_config.ext1_trigger_mode = 0;
  733. s_config.wakeup_triggers &= ~RTC_EXT1_TRIG_EN;
  734. #endif
  735. #if SOC_TOUCH_PAD_WAKE_SUPPORTED
  736. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_TOUCHPAD, RTC_TOUCH_TRIG_EN)) {
  737. s_config.wakeup_triggers &= ~RTC_TOUCH_TRIG_EN;
  738. #endif
  739. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_GPIO, RTC_GPIO_TRIG_EN)) {
  740. s_config.wakeup_triggers &= ~RTC_GPIO_TRIG_EN;
  741. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_UART, (RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN))) {
  742. s_config.wakeup_triggers &= ~(RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN);
  743. }
  744. #if defined(CONFIG_ESP32_ULP_COPROC_ENABLED) || defined(CONFIG_ESP32S2_ULP_COPROC_ENABLED)
  745. else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_ULP, RTC_ULP_TRIG_EN)) {
  746. s_config.wakeup_triggers &= ~RTC_ULP_TRIG_EN;
  747. }
  748. #endif
  749. else {
  750. ESP_LOGE(TAG, "Incorrect wakeup source (%d) to disable.", (int) source);
  751. return ESP_ERR_INVALID_STATE;
  752. }
  753. return ESP_OK;
  754. }
  755. esp_err_t esp_sleep_enable_ulp_wakeup(void)
  756. {
  757. #if CONFIG_IDF_TARGET_ESP32
  758. #if ((defined CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT) || (defined CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT_V2))
  759. ESP_LOGE(TAG, "Failed to enable wakeup when provide current to external 32kHz crystal");
  760. return ESP_ERR_NOT_SUPPORTED;
  761. #endif
  762. #ifdef CONFIG_ESP32_ULP_COPROC_ENABLED
  763. if (s_config.wakeup_triggers & RTC_EXT0_TRIG_EN) {
  764. ESP_LOGE(TAG, "Conflicting wake-up trigger: ext0");
  765. return ESP_ERR_INVALID_STATE;
  766. }
  767. s_config.wakeup_triggers |= RTC_ULP_TRIG_EN;
  768. return ESP_OK;
  769. #else // CONFIG_ESP32_ULP_COPROC_ENABLED
  770. return ESP_ERR_INVALID_STATE;
  771. #endif // CONFIG_ESP32_ULP_COPROC_ENABLED
  772. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  773. s_config.wakeup_triggers |= (RTC_ULP_TRIG_EN | RTC_COCPU_TRIG_EN | RTC_COCPU_TRAP_TRIG_EN);
  774. return ESP_OK;
  775. #else
  776. return ESP_ERR_NOT_SUPPORTED;
  777. #endif
  778. }
  779. esp_err_t esp_sleep_enable_timer_wakeup(uint64_t time_in_us)
  780. {
  781. s_config.wakeup_triggers |= RTC_TIMER_TRIG_EN;
  782. s_config.sleep_duration = time_in_us;
  783. return ESP_OK;
  784. }
  785. static void timer_wakeup_prepare(void)
  786. {
  787. int64_t sleep_duration = (int64_t) s_config.sleep_duration - (int64_t) s_config.sleep_time_adjustment;
  788. if (sleep_duration < 0) {
  789. sleep_duration = 0;
  790. }
  791. int64_t ticks = rtc_time_us_to_slowclk(sleep_duration, s_config.rtc_clk_cal_period);
  792. rtc_hal_set_wakeup_timer(s_config.rtc_ticks_at_sleep_start + ticks);
  793. }
  794. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  795. /* In deep sleep mode, only the sleep channel is supported, and other touch channels should be turned off. */
  796. static void touch_wakeup_prepare(void)
  797. {
  798. uint16_t sleep_cycle = 0;
  799. uint16_t meas_times = 0;
  800. touch_pad_t touch_num = TOUCH_PAD_NUM0;
  801. touch_ll_sleep_get_channel_num(&touch_num); // Check if the sleep pad is enabled.
  802. if ((touch_num > TOUCH_PAD_NUM0) && (touch_num < TOUCH_PAD_MAX) && touch_ll_get_fsm_state()) {
  803. touch_ll_stop_fsm();
  804. touch_ll_clear_channel_mask(TOUCH_PAD_BIT_MASK_ALL);
  805. touch_ll_intr_clear(TOUCH_PAD_INTR_MASK_ALL); // Clear state from previous wakeup
  806. touch_hal_sleep_channel_get_work_time(&sleep_cycle, &meas_times);
  807. touch_ll_set_meas_times(meas_times);
  808. touch_ll_set_sleep_time(sleep_cycle);
  809. touch_ll_set_channel_mask(BIT(touch_num));
  810. touch_ll_start_fsm();
  811. }
  812. }
  813. #endif
  814. #if SOC_TOUCH_SENSOR_NUM > 0
  815. esp_err_t esp_sleep_enable_touchpad_wakeup(void)
  816. {
  817. #if ((defined CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT) || (defined CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT_V2))
  818. ESP_LOGE(TAG, "Failed to enable wakeup when provide current to external 32kHz crystal");
  819. return ESP_ERR_NOT_SUPPORTED;
  820. #endif
  821. if (s_config.wakeup_triggers & (RTC_EXT0_TRIG_EN)) {
  822. ESP_LOGE(TAG, "Conflicting wake-up trigger: ext0");
  823. return ESP_ERR_INVALID_STATE;
  824. }
  825. s_config.wakeup_triggers |= RTC_TOUCH_TRIG_EN;
  826. return ESP_OK;
  827. }
  828. touch_pad_t esp_sleep_get_touchpad_wakeup_status(void)
  829. {
  830. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_TOUCHPAD) {
  831. return TOUCH_PAD_MAX;
  832. }
  833. touch_pad_t pad_num;
  834. esp_err_t ret = touch_pad_get_wakeup_status(&pad_num); //TODO 723diff commit id:fda9ada1b
  835. assert(ret == ESP_OK && "wakeup reason is RTC_TOUCH_TRIG_EN but SENS_TOUCH_MEAS_EN is zero");
  836. return (ret == ESP_OK) ? pad_num : TOUCH_PAD_MAX;
  837. }
  838. #endif // SOC_TOUCH_SENSOR_NUM > 0
  839. bool esp_sleep_is_valid_wakeup_gpio(gpio_num_t gpio_num)
  840. {
  841. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  842. return RTC_GPIO_IS_VALID_GPIO(gpio_num);
  843. #else
  844. return GPIO_IS_DEEP_SLEEP_WAKEUP_VALID_GPIO(gpio_num);
  845. #endif // SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  846. }
  847. #if SOC_PM_SUPPORT_EXT_WAKEUP
  848. esp_err_t esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level)
  849. {
  850. if (level < 0 || level > 1) {
  851. return ESP_ERR_INVALID_ARG;
  852. }
  853. if (!esp_sleep_is_valid_wakeup_gpio(gpio_num)) {
  854. return ESP_ERR_INVALID_ARG;
  855. }
  856. if (s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) {
  857. ESP_LOGE(TAG, "Conflicting wake-up triggers: touch / ULP");
  858. return ESP_ERR_INVALID_STATE;
  859. }
  860. s_config.ext0_rtc_gpio_num = rtc_io_number_get(gpio_num);
  861. s_config.ext0_trigger_level = level;
  862. s_config.wakeup_triggers |= RTC_EXT0_TRIG_EN;
  863. return ESP_OK;
  864. }
  865. static void ext0_wakeup_prepare(void)
  866. {
  867. int rtc_gpio_num = s_config.ext0_rtc_gpio_num;
  868. rtcio_hal_ext0_set_wakeup_pin(rtc_gpio_num, s_config.ext0_trigger_level);
  869. rtcio_hal_function_select(rtc_gpio_num, RTCIO_FUNC_RTC);
  870. rtcio_hal_input_enable(rtc_gpio_num);
  871. }
  872. esp_err_t esp_sleep_enable_ext1_wakeup(uint64_t mask, esp_sleep_ext1_wakeup_mode_t mode)
  873. {
  874. if (mode > ESP_EXT1_WAKEUP_ANY_HIGH) {
  875. return ESP_ERR_INVALID_ARG;
  876. }
  877. // Translate bit map of GPIO numbers into the bit map of RTC IO numbers
  878. uint32_t rtc_gpio_mask = 0;
  879. for (int gpio = 0; mask; ++gpio, mask >>= 1) {
  880. if ((mask & 1) == 0) {
  881. continue;
  882. }
  883. if (!esp_sleep_is_valid_wakeup_gpio(gpio)) {
  884. ESP_LOGE(TAG, "Not an RTC IO: GPIO%d", gpio);
  885. return ESP_ERR_INVALID_ARG;
  886. }
  887. rtc_gpio_mask |= BIT(rtc_io_number_get(gpio));
  888. }
  889. s_config.ext1_rtc_gpio_mask = rtc_gpio_mask;
  890. s_config.ext1_trigger_mode = mode;
  891. s_config.wakeup_triggers |= RTC_EXT1_TRIG_EN;
  892. return ESP_OK;
  893. }
  894. static void ext1_wakeup_prepare(void)
  895. {
  896. // Configure all RTC IOs selected as ext1 wakeup inputs
  897. uint32_t rtc_gpio_mask = s_config.ext1_rtc_gpio_mask;
  898. for (int gpio = 0; gpio < GPIO_PIN_COUNT && rtc_gpio_mask != 0; ++gpio) {
  899. int rtc_pin = rtc_io_number_get(gpio);
  900. if ((rtc_gpio_mask & BIT(rtc_pin)) == 0) {
  901. continue;
  902. }
  903. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  904. // Route pad to RTC
  905. rtcio_hal_function_select(rtc_pin, RTCIO_FUNC_RTC);
  906. // set input enable in sleep mode
  907. rtcio_hal_input_enable(rtc_pin);
  908. #endif
  909. // Pad configuration depends on RTC_PERIPH state in sleep mode
  910. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] != ESP_PD_OPTION_ON) {
  911. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  912. // RTC_PERIPH will be powered down, so RTC_IO_ registers will
  913. // loose their state. Lock pad configuration.
  914. // Pullups/pulldowns also need to be disabled.
  915. rtcio_hal_pullup_disable(rtc_pin);
  916. rtcio_hal_pulldown_disable(rtc_pin);
  917. #endif
  918. rtcio_hal_hold_enable(rtc_pin);
  919. }
  920. // Keep track of pins which are processed to bail out early
  921. rtc_gpio_mask &= ~BIT(rtc_pin);
  922. }
  923. // Clear state from previous wakeup
  924. rtc_hal_ext1_clear_wakeup_pins();
  925. // Set RTC IO pins and mode (any high, all low) to be used for wakeup
  926. rtc_hal_ext1_set_wakeup_pins(s_config.ext1_rtc_gpio_mask, s_config.ext1_trigger_mode);
  927. }
  928. uint64_t esp_sleep_get_ext1_wakeup_status(void)
  929. {
  930. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_EXT1) {
  931. return 0;
  932. }
  933. uint32_t status = rtc_hal_ext1_get_wakeup_pins();
  934. // Translate bit map of RTC IO numbers into the bit map of GPIO numbers
  935. uint64_t gpio_mask = 0;
  936. for (int gpio = 0; gpio < GPIO_PIN_COUNT; ++gpio) {
  937. if (!esp_sleep_is_valid_wakeup_gpio(gpio)) {
  938. continue;
  939. }
  940. int rtc_pin = rtc_io_number_get(gpio);
  941. if ((status & BIT(rtc_pin)) == 0) {
  942. continue;
  943. }
  944. gpio_mask |= 1ULL << gpio;
  945. }
  946. return gpio_mask;
  947. }
  948. #endif // SOC_PM_SUPPORT_EXT_WAKEUP
  949. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  950. uint64_t esp_sleep_get_gpio_wakeup_status(void)
  951. {
  952. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_GPIO) {
  953. return 0;
  954. }
  955. return rtc_hal_gpio_get_wakeup_pins();
  956. }
  957. static void esp_deep_sleep_wakeup_prepare(void)
  958. {
  959. for (gpio_num_t gpio_idx = GPIO_NUM_0; gpio_idx < GPIO_NUM_MAX; gpio_idx++) {
  960. if (((1ULL << gpio_idx) & s_config.gpio_wakeup_mask) == 0) {
  961. continue;
  962. }
  963. if (s_config.gpio_trigger_mode & BIT(gpio_idx)) {
  964. ESP_ERROR_CHECK(gpio_pullup_dis(gpio_idx));
  965. ESP_ERROR_CHECK(gpio_pulldown_en(gpio_idx));
  966. } else {
  967. ESP_ERROR_CHECK(gpio_pullup_en(gpio_idx));
  968. ESP_ERROR_CHECK(gpio_pulldown_dis(gpio_idx));
  969. }
  970. rtc_hal_gpio_set_wakeup_pins();
  971. ESP_ERROR_CHECK(gpio_hold_en(gpio_idx));
  972. }
  973. }
  974. esp_err_t esp_deep_sleep_enable_gpio_wakeup(uint64_t gpio_pin_mask, esp_deepsleep_gpio_wake_up_mode_t mode)
  975. {
  976. if (mode > ESP_GPIO_WAKEUP_GPIO_HIGH) {
  977. ESP_LOGE(TAG, "invalid mode");
  978. return ESP_ERR_INVALID_ARG;
  979. }
  980. gpio_int_type_t intr_type = ((mode == ESP_GPIO_WAKEUP_GPIO_LOW) ? GPIO_INTR_LOW_LEVEL : GPIO_INTR_HIGH_LEVEL);
  981. esp_err_t err = ESP_OK;
  982. for (gpio_num_t gpio_idx = GPIO_NUM_0; gpio_idx < GPIO_NUM_MAX; gpio_idx++, gpio_pin_mask >>= 1) {
  983. if ((gpio_pin_mask & 1) == 0) {
  984. continue;
  985. }
  986. if (!esp_sleep_is_valid_wakeup_gpio(gpio_idx)) {
  987. ESP_LOGE(TAG, "invalid mask, please ensure gpio number is no more than 5");
  988. return ESP_ERR_INVALID_ARG;
  989. }
  990. err = gpio_deep_sleep_wakeup_enable(gpio_idx, intr_type);
  991. s_config.gpio_wakeup_mask |= BIT(gpio_idx);
  992. if (mode == ESP_GPIO_WAKEUP_GPIO_HIGH) {
  993. s_config.gpio_trigger_mode |= (mode << gpio_idx);
  994. } else {
  995. s_config.gpio_trigger_mode &= ~(mode << gpio_idx);
  996. }
  997. }
  998. s_config.wakeup_triggers |= RTC_GPIO_TRIG_EN;
  999. rtc_hal_gpio_clear_wakeup_pins();
  1000. return err;
  1001. }
  1002. #endif //SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  1003. esp_err_t esp_sleep_enable_gpio_wakeup(void)
  1004. {
  1005. #if CONFIG_IDF_TARGET_ESP32
  1006. if (s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) {
  1007. ESP_LOGE(TAG, "Conflicting wake-up triggers: touch / ULP");
  1008. return ESP_ERR_INVALID_STATE;
  1009. }
  1010. #endif
  1011. s_config.wakeup_triggers |= RTC_GPIO_TRIG_EN;
  1012. return ESP_OK;
  1013. }
  1014. esp_err_t esp_sleep_enable_uart_wakeup(int uart_num)
  1015. {
  1016. if (uart_num == UART_NUM_0) {
  1017. s_config.wakeup_triggers |= RTC_UART0_TRIG_EN;
  1018. } else if (uart_num == UART_NUM_1) {
  1019. s_config.wakeup_triggers |= RTC_UART1_TRIG_EN;
  1020. } else {
  1021. return ESP_ERR_INVALID_ARG;
  1022. }
  1023. return ESP_OK;
  1024. }
  1025. esp_err_t esp_sleep_enable_wifi_wakeup(void)
  1026. {
  1027. #if SOC_PM_SUPPORT_WIFI_WAKEUP
  1028. s_config.wakeup_triggers |= RTC_WIFI_TRIG_EN;
  1029. return ESP_OK;
  1030. #else
  1031. return ESP_ERR_NOT_SUPPORTED;
  1032. #endif
  1033. }
  1034. esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause(void)
  1035. {
  1036. if (rtc_get_reset_reason(0) != DEEPSLEEP_RESET && !s_light_sleep_wakeup) {
  1037. return ESP_SLEEP_WAKEUP_UNDEFINED;
  1038. }
  1039. #ifdef CONFIG_IDF_TARGET_ESP32
  1040. uint32_t wakeup_cause = REG_GET_FIELD(RTC_CNTL_WAKEUP_STATE_REG, RTC_CNTL_WAKEUP_CAUSE);
  1041. #else
  1042. uint32_t wakeup_cause = REG_GET_FIELD(RTC_CNTL_SLP_WAKEUP_CAUSE_REG, RTC_CNTL_WAKEUP_CAUSE);
  1043. #endif
  1044. if (wakeup_cause & RTC_TIMER_TRIG_EN) {
  1045. return ESP_SLEEP_WAKEUP_TIMER;
  1046. } else if (wakeup_cause & RTC_GPIO_TRIG_EN) {
  1047. return ESP_SLEEP_WAKEUP_GPIO;
  1048. } else if (wakeup_cause & (RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN)) {
  1049. return ESP_SLEEP_WAKEUP_UART;
  1050. #if SOC_PM_SUPPORT_EXT_WAKEUP
  1051. } else if (wakeup_cause & RTC_EXT0_TRIG_EN) {
  1052. return ESP_SLEEP_WAKEUP_EXT0;
  1053. } else if (wakeup_cause & RTC_EXT1_TRIG_EN) {
  1054. return ESP_SLEEP_WAKEUP_EXT1;
  1055. #endif
  1056. #if SOC_TOUCH_PAD_WAKE_SUPPORTED
  1057. } else if (wakeup_cause & RTC_TOUCH_TRIG_EN) {
  1058. return ESP_SLEEP_WAKEUP_TOUCHPAD;
  1059. #endif
  1060. #if SOC_ULP_SUPPORTED
  1061. } else if (wakeup_cause & RTC_ULP_TRIG_EN) {
  1062. return ESP_SLEEP_WAKEUP_ULP;
  1063. #endif
  1064. #if SOC_PM_SUPPORT_WIFI_WAKEUP
  1065. } else if (wakeup_cause & RTC_WIFI_TRIG_EN) {
  1066. return ESP_SLEEP_WAKEUP_WIFI;
  1067. #endif
  1068. #if SOC_PM_SUPPORT_BT_WAKEUP
  1069. } else if (wakeup_cause & RTC_BT_TRIG_EN) {
  1070. return ESP_SLEEP_WAKEUP_BT;
  1071. #endif
  1072. #if CONFIG_IDF_TARGET_ESP32S2
  1073. } else if (wakeup_cause & RTC_COCPU_TRIG_EN) {
  1074. return ESP_SLEEP_WAKEUP_ULP;
  1075. } else if (wakeup_cause & RTC_COCPU_TRAP_TRIG_EN) {
  1076. return ESP_SLEEP_WAKEUP_COCPU_TRAP_TRIG;
  1077. #endif
  1078. } else {
  1079. return ESP_SLEEP_WAKEUP_UNDEFINED;
  1080. }
  1081. }
  1082. esp_err_t esp_sleep_pd_config(esp_sleep_pd_domain_t domain,
  1083. esp_sleep_pd_option_t option)
  1084. {
  1085. if (domain >= ESP_PD_DOMAIN_MAX || option > ESP_PD_OPTION_AUTO) {
  1086. return ESP_ERR_INVALID_ARG;
  1087. }
  1088. s_config.pd_options[domain] = option;
  1089. return ESP_OK;
  1090. }
  1091. static uint32_t get_power_down_flags(void)
  1092. {
  1093. // Where needed, convert AUTO options to ON. Later interpret AUTO as OFF.
  1094. // RTC_SLOW_MEM is needed for the ULP, so keep RTC_SLOW_MEM powered up if ULP
  1095. // is used and RTC_SLOW_MEM is Auto.
  1096. // If there is any data placed into .rtc.data or .rtc.bss segments, and
  1097. // RTC_SLOW_MEM is Auto, keep it powered up as well.
  1098. #if SOC_RTC_SLOW_MEM_SUPPORTED && SOC_ULP_SUPPORTED
  1099. // Labels are defined in the linker script
  1100. extern int _rtc_slow_length;
  1101. if ((s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] == ESP_PD_OPTION_AUTO) &&
  1102. ((size_t) &_rtc_slow_length > 0 ||
  1103. (s_config.wakeup_triggers & RTC_ULP_TRIG_EN))) {
  1104. s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] = ESP_PD_OPTION_ON;
  1105. }
  1106. #endif
  1107. #if !CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  1108. /* RTC_FAST_MEM is needed for deep sleep stub.
  1109. If RTC_FAST_MEM is Auto, keep it powered on, so that deep sleep stub can run.
  1110. In the new chip revision, deep sleep stub will be optional, and this can be changed. */
  1111. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] == ESP_PD_OPTION_AUTO) {
  1112. s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] = ESP_PD_OPTION_ON;
  1113. }
  1114. #else
  1115. /* If RTC_FAST_MEM is used for heap, force RTC_FAST_MEM to be powered on. */
  1116. s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] = ESP_PD_OPTION_ON;
  1117. #endif
  1118. // RTC_PERIPH is needed for EXT0 wakeup and GPIO wakeup.
  1119. // If RTC_PERIPH is auto, and EXT0/GPIO aren't enabled, power down RTC_PERIPH.
  1120. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] == ESP_PD_OPTION_AUTO) {
  1121. #if SOC_TOUCH_PAD_WAKE_SUPPORTED
  1122. uint32_t wakeup_source = RTC_TOUCH_TRIG_EN;
  1123. #if SOC_ULP_SUPPORTED
  1124. wakeup_source |= RTC_ULP_TRIG_EN;
  1125. #endif
  1126. if (s_config.wakeup_triggers & (RTC_EXT0_TRIG_EN | RTC_GPIO_TRIG_EN)) {
  1127. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_ON;
  1128. } else if (s_config.wakeup_triggers & wakeup_source) {
  1129. // In both rev. 0 and rev. 1 of ESP32, forcing power up of RTC_PERIPH
  1130. // prevents ULP timer and touch FSMs from working correctly.
  1131. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_OFF;
  1132. }
  1133. #else
  1134. if (s_config.wakeup_triggers & RTC_GPIO_TRIG_EN) {
  1135. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_ON;
  1136. } else {
  1137. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_OFF;
  1138. }
  1139. #endif // SOC_TOUCH_PAD_WAKE_SUPPORTED
  1140. }
  1141. #if SOC_PM_SUPPORT_CPU_PD
  1142. if (s_config.cpu_pd_mem == NULL) {
  1143. s_config.pd_options[ESP_PD_DOMAIN_CPU] = ESP_PD_OPTION_ON;
  1144. }
  1145. #else
  1146. if (s_config.pd_options[ESP_PD_DOMAIN_CPU] != ESP_PD_OPTION_ON) {
  1147. s_config.pd_options[ESP_PD_DOMAIN_CPU] = ESP_PD_OPTION_ON;
  1148. }
  1149. #endif
  1150. if (s_config.pd_options[ESP_PD_DOMAIN_XTAL] == ESP_PD_OPTION_AUTO) {
  1151. s_config.pd_options[ESP_PD_DOMAIN_XTAL] = ESP_PD_OPTION_OFF;
  1152. }
  1153. const char *option_str[] = {"OFF", "ON", "AUTO(OFF)" /* Auto works as OFF */};
  1154. ESP_LOGD(TAG, "RTC_PERIPH: %s", option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH]]);
  1155. #if SOC_RTC_SLOW_MEM_SUPPORTED
  1156. ESP_LOGD(TAG, "RTC_SLOW_MEM: %s", option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM]]);
  1157. #endif
  1158. ESP_LOGD(TAG, "RTC_FAST_MEM: %s", option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM]]);
  1159. // Prepare flags based on the selected options
  1160. uint32_t pd_flags = 0;
  1161. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] != ESP_PD_OPTION_ON) {
  1162. pd_flags |= RTC_SLEEP_PD_RTC_FAST_MEM;
  1163. }
  1164. #if SOC_RTC_SLOW_MEM_SUPPORTED
  1165. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] != ESP_PD_OPTION_ON) {
  1166. pd_flags |= RTC_SLEEP_PD_RTC_SLOW_MEM;
  1167. }
  1168. #endif
  1169. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] != ESP_PD_OPTION_ON) {
  1170. pd_flags |= RTC_SLEEP_PD_RTC_PERIPH;
  1171. }
  1172. #if SOC_PM_SUPPORT_CPU_PD
  1173. if (s_config.pd_options[ESP_PD_DOMAIN_CPU] != ESP_PD_OPTION_ON) {
  1174. pd_flags |= RTC_SLEEP_PD_CPU;
  1175. }
  1176. #endif
  1177. #ifdef CONFIG_IDF_TARGET_ESP32
  1178. pd_flags |= RTC_SLEEP_PD_XTAL;
  1179. #endif
  1180. /**
  1181. * VDD_SDIO power domain shall be kept on during the light sleep
  1182. * when CONFIG_ESP_SYSTEM_PD_FLASH is not set and off when it is set.
  1183. * The application can still force the power domain to remain on by calling
  1184. * `esp_sleep_pd_config` before getting into light sleep mode.
  1185. *
  1186. * In deep sleep mode, the power domain will be turned off, regardless the
  1187. * value of this field.
  1188. */
  1189. if (s_config.pd_options[ESP_PD_DOMAIN_VDDSDIO] == ESP_PD_OPTION_AUTO) {
  1190. #ifdef CONFIG_ESP_SYSTEM_PD_FLASH
  1191. s_config.pd_options[ESP_PD_DOMAIN_VDDSDIO] = ESP_PD_OPTION_OFF;
  1192. #else
  1193. s_config.pd_options[ESP_PD_DOMAIN_VDDSDIO] = ESP_PD_OPTION_ON;
  1194. #endif
  1195. }
  1196. if (s_config.pd_options[ESP_PD_DOMAIN_VDDSDIO] != ESP_PD_OPTION_ON) {
  1197. pd_flags |= RTC_SLEEP_PD_VDDSDIO;
  1198. }
  1199. #if ((defined CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS) && (defined CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT))
  1200. if ((s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) == 0) {
  1201. // If enabled EXT1 only and enable the additional current by touch, should be keep RTC_PERIPH power on.
  1202. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  1203. }
  1204. #endif
  1205. return pd_flags;
  1206. }
  1207. void esp_deep_sleep_disable_rom_logging(void)
  1208. {
  1209. rtc_suppress_rom_log();
  1210. }