sleep_modes.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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 "hal/wdt_hal.h"
  34. #include "hal/rtc_hal.h"
  35. #include "hal/uart_hal.h"
  36. #if SOC_TOUCH_SENSOR_NUM > 0
  37. #include "hal/touch_sensor_hal.h"
  38. #include "driver/touch_sensor.h"
  39. #include "driver/touch_sensor_common.h"
  40. #endif
  41. #include "hal/clk_gate_ll.h"
  42. #include "sdkconfig.h"
  43. #include "esp_rom_uart.h"
  44. #ifdef CONFIG_IDF_TARGET_ESP32
  45. #include "esp32/rom/cache.h"
  46. #include "esp32/clk.h"
  47. #include "esp32/rom/rtc.h"
  48. #include "esp_private/gpio.h"
  49. #elif CONFIG_IDF_TARGET_ESP32S2
  50. #include "esp32s2/clk.h"
  51. #include "esp32s2/rom/cache.h"
  52. #include "esp32s2/rom/rtc.h"
  53. #include "esp32s2/brownout.h"
  54. #include "soc/extmem_reg.h"
  55. #include "esp_private/gpio.h"
  56. #elif CONFIG_IDF_TARGET_ESP32S3
  57. #include "esp32s3/clk.h"
  58. #include "esp32s3/rom/cache.h"
  59. #include "esp32s3/rom/rtc.h"
  60. #include "soc/extmem_reg.h"
  61. #elif CONFIG_IDF_TARGET_ESP32C3
  62. #include "esp32c3/clk.h"
  63. #include "esp32c3/rom/cache.h"
  64. #include "esp32c3/rom/rtc.h"
  65. #include "soc/extmem_reg.h"
  66. #include "esp_heap_caps.h"
  67. #endif
  68. // If light sleep time is less than that, don't power down flash
  69. #define FLASH_PD_MIN_SLEEP_TIME_US 2000
  70. // Time from VDD_SDIO power up to first flash read in ROM code
  71. #define VDD_SDIO_POWERUP_TO_FLASH_READ_US 700
  72. // Cycles for RTC Timer clock source (internal oscillator) calibrate
  73. #define RTC_CLK_SRC_CAL_CYCLES (10)
  74. #ifdef CONFIG_IDF_TARGET_ESP32
  75. #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ
  76. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (212)
  77. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (60)
  78. #elif CONFIG_IDF_TARGET_ESP32S2
  79. #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S2_DEFAULT_CPU_FREQ_MHZ
  80. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (147)
  81. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (28)
  82. #elif CONFIG_IDF_TARGET_ESP32S3
  83. #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ
  84. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (0)
  85. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (0)
  86. #elif CONFIG_IDF_TARGET_ESP32C3
  87. #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32C3_DEFAULT_CPU_FREQ_MHZ
  88. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (105)
  89. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (37)
  90. #endif
  91. #define LIGHT_SLEEP_TIME_OVERHEAD_US DEFAULT_HARDWARE_OUT_OVERHEAD_US
  92. #if defined(CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS) || \
  93. defined(CONFIG_ESP32S2_RTC_CLK_SRC_EXT_CRYS) || \
  94. defined(CONFIG_ESP32C3_RTC_CLK_SRC_EXT_CRYS) || \
  95. defined(CONFIG_ESP32S3_RTC_CLK_SRC_EXT_CRYS)
  96. #define DEEP_SLEEP_TIME_OVERHEAD_US (650 + 100 * 240 / DEFAULT_CPU_FREQ_MHZ)
  97. #else
  98. #define DEEP_SLEEP_TIME_OVERHEAD_US (250 + 100 * 240 / DEFAULT_CPU_FREQ_MHZ)
  99. #endif
  100. #if defined(CONFIG_IDF_TARGET_ESP32) && defined(CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY)
  101. #define DEEP_SLEEP_WAKEUP_DELAY CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY
  102. #else
  103. #define DEEP_SLEEP_WAKEUP_DELAY 0
  104. #endif
  105. extern void periph_inform_out_light_sleep_overhead(uint32_t out_light_sleep_time);
  106. // Minimal amount of time we can sleep for
  107. #define LIGHT_SLEEP_MIN_TIME_US 200
  108. #define RTC_MODULE_SLEEP_PREPARE_CYCLES (6)
  109. #define CHECK_SOURCE(source, value, mask) ((s_config.wakeup_triggers & mask) && \
  110. (source == value))
  111. /**
  112. * Internal structure which holds all requested deep sleep parameters
  113. */
  114. typedef struct {
  115. esp_sleep_pd_option_t pd_options[ESP_PD_DOMAIN_MAX];
  116. uint64_t sleep_duration;
  117. uint32_t wakeup_triggers : 15;
  118. uint32_t ext1_trigger_mode : 1;
  119. uint32_t ext1_rtc_gpio_mask : 18;
  120. uint32_t ext0_trigger_level : 1;
  121. uint32_t ext0_rtc_gpio_num : 5;
  122. uint32_t gpio_wakeup_mask : 6;
  123. uint32_t gpio_trigger_mode : 6;
  124. uint32_t sleep_time_adjustment;
  125. uint32_t ccount_ticks_record;
  126. uint32_t sleep_time_overhead_out;
  127. uint32_t rtc_clk_cal_period;
  128. uint64_t rtc_ticks_at_sleep_start;
  129. #if SOC_PM_SUPPORT_CPU_PD
  130. void *cpu_pd_mem;
  131. #endif
  132. } sleep_config_t;
  133. static sleep_config_t s_config = {
  134. .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 },
  135. .ccount_ticks_record = 0,
  136. .sleep_time_overhead_out = DEFAULT_SLEEP_OUT_OVERHEAD_US,
  137. .wakeup_triggers = 0
  138. };
  139. /* Internal variable used to track if light sleep wakeup sources are to be
  140. expected when determining wakeup cause. */
  141. static bool s_light_sleep_wakeup = false;
  142. /* Updating RTC_MEMORY_CRC_REG register via set_rtc_memory_crc()
  143. is not thread-safe, so we need to disable interrupts before going to deep sleep. */
  144. static portMUX_TYPE spinlock_rtc_deep_sleep = portMUX_INITIALIZER_UNLOCKED;
  145. static const char *TAG = "sleep";
  146. static uint32_t get_power_down_flags(void);
  147. #if SOC_PM_SUPPORT_EXT_WAKEUP
  148. static void ext0_wakeup_prepare(void);
  149. static void ext1_wakeup_prepare(void);
  150. #endif
  151. static void timer_wakeup_prepare(void);
  152. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  153. static void touch_wakeup_prepare(void);
  154. #endif
  155. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  156. static void esp_deep_sleep_wakeup_prepare(void);
  157. #endif
  158. #if CONFIG_MAC_BB_PD
  159. #define MAC_BB_POWER_DOWN_CB_NO 2
  160. #define MAC_BB_POWER_UP_CB_NO 2
  161. static DRAM_ATTR mac_bb_power_down_cb_t s_mac_bb_power_down_cb[MAC_BB_POWER_DOWN_CB_NO];
  162. static DRAM_ATTR mac_bb_power_up_cb_t s_mac_bb_power_up_cb[MAC_BB_POWER_UP_CB_NO];
  163. esp_err_t esp_register_mac_bb_pd_callback(mac_bb_power_down_cb_t cb)
  164. {
  165. int index = MAC_BB_POWER_DOWN_CB_NO;
  166. for (int i = MAC_BB_POWER_DOWN_CB_NO - 1; i >= 0; i--) {
  167. if (s_mac_bb_power_down_cb[i] == cb) {
  168. return ESP_ERR_INVALID_STATE;
  169. }
  170. if (s_mac_bb_power_down_cb[i] == NULL) {
  171. index = i;
  172. }
  173. }
  174. if (index < MAC_BB_POWER_DOWN_CB_NO) {
  175. s_mac_bb_power_down_cb[index] = cb;
  176. return ESP_OK;
  177. }
  178. return ESP_ERR_NO_MEM;
  179. }
  180. esp_err_t esp_unregister_mac_bb_pd_callback(mac_bb_power_down_cb_t cb)
  181. {
  182. for (int i = MAC_BB_POWER_DOWN_CB_NO - 1; i >= 0; i--) {
  183. if (s_mac_bb_power_down_cb[i] == cb) {
  184. s_mac_bb_power_down_cb[i] = NULL;
  185. return ESP_OK;
  186. }
  187. }
  188. return ESP_ERR_INVALID_STATE;
  189. }
  190. static IRAM_ATTR void mac_bb_power_down_cb_execute(void)
  191. {
  192. for (int i = 0; i < MAC_BB_POWER_DOWN_CB_NO; i++) {
  193. if (s_mac_bb_power_down_cb[i]) {
  194. s_mac_bb_power_down_cb[i]();
  195. }
  196. }
  197. }
  198. esp_err_t esp_register_mac_bb_pu_callback(mac_bb_power_up_cb_t cb)
  199. {
  200. int index = MAC_BB_POWER_UP_CB_NO;
  201. for (int i = MAC_BB_POWER_UP_CB_NO - 1; i >= 0; i--) {
  202. if (s_mac_bb_power_up_cb[i] == cb) {
  203. return ESP_ERR_INVALID_STATE;
  204. }
  205. if (s_mac_bb_power_up_cb[i] == NULL) {
  206. index = i;
  207. }
  208. }
  209. if (index < MAC_BB_POWER_UP_CB_NO) {
  210. s_mac_bb_power_up_cb[index] = cb;
  211. return ESP_OK;
  212. }
  213. return ESP_ERR_NO_MEM;
  214. }
  215. esp_err_t esp_unregister_mac_bb_pu_callback(mac_bb_power_up_cb_t cb)
  216. {
  217. for (int i = MAC_BB_POWER_UP_CB_NO - 1; i >= 0; i--) {
  218. if (s_mac_bb_power_up_cb[i] == cb) {
  219. s_mac_bb_power_up_cb[i] = NULL;
  220. return ESP_OK;
  221. }
  222. }
  223. return ESP_ERR_INVALID_STATE;
  224. }
  225. static IRAM_ATTR void mac_bb_power_up_cb_execute(void)
  226. {
  227. for (int i = 0; i < MAC_BB_POWER_UP_CB_NO; i++) {
  228. if (s_mac_bb_power_up_cb[i]) {
  229. s_mac_bb_power_up_cb[i]();
  230. }
  231. }
  232. }
  233. #endif ///CONFIG_MAC_BB_PD
  234. /* Wake from deep sleep stub
  235. See esp_deepsleep.h esp_wake_deep_sleep() comments for details.
  236. */
  237. esp_deep_sleep_wake_stub_fn_t esp_get_deep_sleep_wake_stub(void)
  238. {
  239. esp_deep_sleep_wake_stub_fn_t stub_ptr = (esp_deep_sleep_wake_stub_fn_t) REG_READ(RTC_ENTRY_ADDR_REG);
  240. if (!esp_ptr_executable(stub_ptr)) {
  241. return NULL;
  242. }
  243. return stub_ptr;
  244. }
  245. void esp_set_deep_sleep_wake_stub(esp_deep_sleep_wake_stub_fn_t new_stub)
  246. {
  247. REG_WRITE(RTC_ENTRY_ADDR_REG, (uint32_t)new_stub);
  248. }
  249. void RTC_IRAM_ATTR esp_default_wake_deep_sleep(void)
  250. {
  251. /* Clear MMU for CPU 0 */
  252. #if CONFIG_IDF_TARGET_ESP32
  253. _DPORT_REG_WRITE(DPORT_PRO_CACHE_CTRL1_REG,
  254. _DPORT_REG_READ(DPORT_PRO_CACHE_CTRL1_REG) | DPORT_PRO_CACHE_MMU_IA_CLR);
  255. _DPORT_REG_WRITE(DPORT_PRO_CACHE_CTRL1_REG,
  256. _DPORT_REG_READ(DPORT_PRO_CACHE_CTRL1_REG) & (~DPORT_PRO_CACHE_MMU_IA_CLR));
  257. #if DEEP_SLEEP_WAKEUP_DELAY > 0
  258. // ROM code has not started yet, so we need to set delay factor
  259. // used by esp_rom_delay_us first.
  260. ets_update_cpu_frequency_rom(ets_get_detected_xtal_freq() / 1000000);
  261. // This delay is configured in menuconfig, it can be used to give
  262. // the flash chip some time to become ready.
  263. esp_rom_delay_us(DEEP_SLEEP_WAKEUP_DELAY);
  264. #endif
  265. #elif CONFIG_IDF_TARGET_ESP32S2
  266. REG_SET_BIT(EXTMEM_CACHE_DBG_INT_ENA_REG, EXTMEM_CACHE_DBG_EN);
  267. #endif
  268. }
  269. void __attribute__((weak, alias("esp_default_wake_deep_sleep"))) esp_wake_deep_sleep(void);
  270. void esp_deep_sleep(uint64_t time_in_us)
  271. {
  272. esp_sleep_enable_timer_wakeup(time_in_us);
  273. esp_deep_sleep_start();
  274. }
  275. // [refactor-todo] provide target logic for body of uart functions below
  276. static void IRAM_ATTR flush_uarts(void)
  277. {
  278. for (int i = 0; i < SOC_UART_NUM; ++i) {
  279. #ifdef CONFIG_IDF_TARGET_ESP32
  280. esp_rom_uart_tx_wait_idle(i);
  281. #else
  282. if (periph_ll_periph_enabled(PERIPH_UART0_MODULE + i)) {
  283. esp_rom_uart_tx_wait_idle(i);
  284. }
  285. #endif
  286. }
  287. }
  288. static void IRAM_ATTR suspend_uarts(void)
  289. {
  290. for (int i = 0; i < SOC_UART_NUM; ++i) {
  291. #ifndef CONFIG_IDF_TARGET_ESP32
  292. if (!periph_ll_periph_enabled(PERIPH_UART0_MODULE + i)) {
  293. continue;
  294. }
  295. #endif
  296. uart_ll_force_xoff(i);
  297. #if SOC_UART_SUPPORT_FSM_TX_WAIT_SEND
  298. uint32_t uart_fsm = 0;
  299. do {
  300. uart_fsm = uart_ll_get_fsm_status(i);
  301. } while (!(uart_fsm == UART_LL_FSM_IDLE || uart_fsm == UART_LL_FSM_TX_WAIT_SEND));
  302. #else
  303. while (uart_ll_get_fsm_status(i) != 0) {}
  304. #endif
  305. }
  306. }
  307. static void IRAM_ATTR resume_uarts(void)
  308. {
  309. for (int i = 0; i < SOC_UART_NUM; ++i) {
  310. #ifndef CONFIG_IDF_TARGET_ESP32
  311. if (!periph_ll_periph_enabled(PERIPH_UART0_MODULE + i)) {
  312. continue;
  313. }
  314. #endif
  315. uart_ll_force_xon(i);
  316. }
  317. }
  318. inline static uint32_t IRAM_ATTR call_rtc_sleep_start(uint32_t reject_triggers);
  319. #if SOC_PM_SUPPORT_CPU_PD
  320. esp_err_t esp_sleep_cpu_pd_low_init(bool enable)
  321. {
  322. if (enable) {
  323. if (s_config.cpu_pd_mem == NULL) {
  324. void *buf = heap_caps_aligned_alloc(SOC_RTC_CNTL_CPU_PD_DMA_ADDR_ALIGN,
  325. SOC_RTC_CNTL_CPU_PD_RETENTION_MEM_SIZE + RTC_HAL_DMA_LINK_NODE_SIZE,
  326. MALLOC_CAP_RETENTION | MALLOC_CAP_DEFAULT);
  327. if (buf) {
  328. memset(buf, 0, SOC_RTC_CNTL_CPU_PD_RETENTION_MEM_SIZE + RTC_HAL_DMA_LINK_NODE_SIZE);
  329. s_config.cpu_pd_mem = rtc_cntl_hal_dma_link_init(buf,
  330. buf + RTC_HAL_DMA_LINK_NODE_SIZE, SOC_RTC_CNTL_CPU_PD_RETENTION_MEM_SIZE, NULL);
  331. } else {
  332. return ESP_ERR_NO_MEM;
  333. }
  334. }
  335. } else {
  336. if (s_config.cpu_pd_mem) {
  337. heap_caps_free(s_config.cpu_pd_mem);
  338. s_config.cpu_pd_mem = NULL;
  339. }
  340. }
  341. return ESP_OK;
  342. }
  343. #endif // SOC_PM_SUPPORT_CPU_PD
  344. #if SOC_GPIO_SUPPORT_SLP_SWITCH
  345. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  346. static inline void gpio_sleep_mode_config_apply(void)
  347. {
  348. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  349. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  350. gpio_sleep_pupd_config_apply(gpio_num);
  351. }
  352. }
  353. }
  354. static inline void gpio_sleep_mode_config_unapply(void)
  355. {
  356. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  357. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  358. gpio_sleep_pupd_config_unapply(gpio_num);
  359. }
  360. }
  361. }
  362. #endif
  363. void esp_sleep_config_gpio_isolate(void)
  364. {
  365. ESP_LOGI(TAG, "Configure to isolate all GPIO pins in sleep state");
  366. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  367. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  368. gpio_sleep_set_direction(gpio_num, GPIO_MODE_DISABLE);
  369. gpio_sleep_set_pull_mode(gpio_num, GPIO_FLOATING);
  370. }
  371. }
  372. }
  373. void esp_sleep_enable_gpio_switch(bool enable)
  374. {
  375. ESP_LOGI(TAG, "%s automatic switching of GPIO sleep configuration", enable ? "Enable" : "Disable");
  376. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  377. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  378. if (enable) {
  379. gpio_sleep_sel_en(gpio_num);
  380. } else {
  381. gpio_sleep_sel_dis(gpio_num);
  382. }
  383. }
  384. }
  385. }
  386. #endif // SOC_GPIO_SUPPORT_SLP_SWITCH
  387. static uint32_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags)
  388. {
  389. // Stop UART output so that output is not lost due to APB frequency change.
  390. // For light sleep, suspend UART output — it will resume after wakeup.
  391. // For deep sleep, wait for the contents of UART FIFO to be sent.
  392. bool deep_sleep = pd_flags & RTC_SLEEP_PD_DIG;
  393. #if !CONFIG_FREERTOS_UNICORE && CONFIG_IDF_TARGET_ESP32S3 && CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  394. /* Currently only safe to use deep sleep wake stub & RTC memory as heap in single core mode.
  395. For ESP32-S3, either disable ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP in config or find a way to set the
  396. deep sleep wake stub to NULL.
  397. */
  398. assert(!deep_sleep || esp_get_deep_sleep_wake_stub() == NULL);
  399. #endif
  400. if (deep_sleep) {
  401. flush_uarts();
  402. } else {
  403. suspend_uarts();
  404. }
  405. // Save current frequency and switch to XTAL
  406. rtc_cpu_freq_config_t cpu_freq_config;
  407. rtc_clk_cpu_freq_get_config(&cpu_freq_config);
  408. rtc_clk_cpu_freq_set_xtal();
  409. #if CONFIG_MAC_BB_PD
  410. mac_bb_power_down_cb_execute();
  411. #endif
  412. #if SOC_PM_SUPPORT_EXT_WAKEUP
  413. // Configure pins for external wakeup
  414. if (s_config.wakeup_triggers & RTC_EXT0_TRIG_EN) {
  415. ext0_wakeup_prepare();
  416. }
  417. if (s_config.wakeup_triggers & RTC_EXT1_TRIG_EN) {
  418. ext1_wakeup_prepare();
  419. }
  420. #endif
  421. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  422. if (s_config.wakeup_triggers & RTC_GPIO_TRIG_EN) {
  423. esp_deep_sleep_wakeup_prepare();
  424. }
  425. #endif
  426. #ifdef CONFIG_IDF_TARGET_ESP32
  427. // Enable ULP wakeup
  428. if (s_config.wakeup_triggers & RTC_ULP_TRIG_EN) {
  429. rtc_hal_ulp_wakeup_enable();
  430. }
  431. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  432. gpio_sleep_mode_config_apply();
  433. #endif
  434. #endif
  435. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  436. if (deep_sleep) {
  437. if (s_config.wakeup_triggers & RTC_TOUCH_TRIG_EN) {
  438. touch_wakeup_prepare();
  439. /* Workaround: In deep sleep, for ESP32S2, Power down the RTC_PERIPH will change the slope configuration of Touch sensor sleep pad.
  440. * The configuration change will change the reading of the sleep pad, which will cause the touch wake-up sensor to trigger falsely.
  441. */
  442. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  443. }
  444. } else {
  445. /* In light sleep, the RTC_PERIPH power domain should be in the power-on state (Power on the touch circuit in light sleep),
  446. * otherwise the touch sensor FSM will be cleared, causing touch sensor false triggering.
  447. */
  448. if (touch_ll_get_fsm_state()) { // Check if the touch sensor is working properly.
  449. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  450. }
  451. }
  452. #endif
  453. uint32_t reject_triggers = 0;
  454. if ((pd_flags & RTC_SLEEP_PD_DIG) == 0 && (s_config.wakeup_triggers & RTC_GPIO_TRIG_EN)) {
  455. /* Light sleep, enable sleep reject for faster return from this function,
  456. * in case the wakeup is already triggerred.
  457. */
  458. #if CONFIG_IDF_TARGET_ESP32
  459. reject_triggers = RTC_CNTL_LIGHT_SLP_REJECT_EN_M | RTC_CNTL_GPIO_REJECT_EN_M;
  460. #else
  461. reject_triggers = s_config.wakeup_triggers;
  462. #endif
  463. }
  464. // Enter sleep
  465. rtc_sleep_config_t config = RTC_SLEEP_CONFIG_DEFAULT(pd_flags);
  466. rtc_sleep_init(config);
  467. rtc_sleep_low_init(s_config.rtc_clk_cal_period);
  468. // Set state machine time for light sleep
  469. if (!deep_sleep) {
  470. rtc_sleep_low_init(s_config.rtc_clk_cal_period);
  471. }
  472. // Configure timer wakeup
  473. if ((s_config.wakeup_triggers & RTC_TIMER_TRIG_EN) &&
  474. s_config.sleep_duration > 0) {
  475. timer_wakeup_prepare();
  476. }
  477. uint32_t result;
  478. if (deep_sleep) {
  479. /* Disable interrupts in case another task writes to RTC memory while we
  480. * calculate RTC memory CRC
  481. *
  482. * Note: for ESP32-S3 running in dual core mode this is currently not enough,
  483. * see the assert at top of this function.
  484. */
  485. portENTER_CRITICAL(&spinlock_rtc_deep_sleep);
  486. #if !CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  487. /* If not possible stack is in RTC FAST memory, use the ROM function to calculate the CRC and save ~140 bytes IRAM */
  488. set_rtc_memory_crc();
  489. result = call_rtc_sleep_start(reject_triggers);
  490. #else
  491. /* Otherwise, need to call the dedicated soc function for this */
  492. result = rtc_deep_sleep_start(s_config.wakeup_triggers, reject_triggers);
  493. #endif
  494. portEXIT_CRITICAL(&spinlock_rtc_deep_sleep);
  495. } else {
  496. result = call_rtc_sleep_start(reject_triggers);
  497. }
  498. // Restore CPU frequency
  499. rtc_clk_cpu_freq_set_config(&cpu_freq_config);
  500. if (!deep_sleep) {
  501. s_config.ccount_ticks_record = cpu_ll_get_cycle_count();
  502. }
  503. #if SOC_PM_SUPPORT_CPU_PD
  504. rtc_cntl_hal_disable_cpu_retention();
  505. #endif
  506. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  507. gpio_sleep_mode_config_unapply();
  508. #endif
  509. #if CONFIG_MAC_BB_PD
  510. mac_bb_power_up_cb_execute();
  511. #endif
  512. // re-enable UART output
  513. resume_uarts();
  514. return result;
  515. }
  516. inline static uint32_t IRAM_ATTR call_rtc_sleep_start(uint32_t reject_triggers)
  517. {
  518. #ifdef CONFIG_IDF_TARGET_ESP32
  519. return rtc_sleep_start(s_config.wakeup_triggers, reject_triggers);
  520. #else
  521. return rtc_sleep_start(s_config.wakeup_triggers, reject_triggers, 1);
  522. #endif
  523. }
  524. void IRAM_ATTR esp_deep_sleep_start(void)
  525. {
  526. #if CONFIG_IDF_TARGET_ESP32S2
  527. /* Due to hardware limitations, on S2 the brownout detector sometimes trigger during deep sleep
  528. to circumvent this we disable the brownout detector before sleeping */
  529. esp_brownout_disable();
  530. #endif //CONFIG_IDF_TARGET_ESP32S2
  531. // record current RTC time
  532. s_config.rtc_ticks_at_sleep_start = rtc_time_get();
  533. // record current RTC time
  534. esp_sync_counters_rtc_and_frc();
  535. // Configure wake stub
  536. if (esp_get_deep_sleep_wake_stub() == NULL) {
  537. esp_set_deep_sleep_wake_stub(esp_wake_deep_sleep);
  538. }
  539. // Decide which power domains can be powered down
  540. uint32_t pd_flags = get_power_down_flags();
  541. s_config.rtc_clk_cal_period = esp_clk_slowclk_cal_get();
  542. // Correct the sleep time
  543. s_config.sleep_time_adjustment = DEEP_SLEEP_TIME_OVERHEAD_US;
  544. uint32_t force_pd_flags = RTC_SLEEP_PD_DIG | RTC_SLEEP_PD_VDDSDIO;
  545. #if SOC_PM_SUPPORT_WIFI_PD
  546. force_pd_flags |= RTC_SLEEP_PD_WIFI;
  547. #endif
  548. #if SOC_PM_SUPPORT_BT_PD
  549. force_pd_flags |= RTC_SLEEP_PD_BT;
  550. #endif
  551. // Enter sleep
  552. esp_sleep_start(force_pd_flags | pd_flags);
  553. // Because RTC is in a slower clock domain than the CPU, it
  554. // can take several CPU cycles for the sleep mode to start.
  555. while (1) {
  556. ;
  557. }
  558. }
  559. /**
  560. * Helper function which handles entry to and exit from light sleep
  561. * Placed into IRAM as flash may need some time to be powered on.
  562. */
  563. static esp_err_t esp_light_sleep_inner(uint32_t pd_flags,
  564. uint32_t flash_enable_time_us,
  565. rtc_vddsdio_config_t vddsdio_config) IRAM_ATTR __attribute__((noinline));
  566. static esp_err_t esp_light_sleep_inner(uint32_t pd_flags,
  567. uint32_t flash_enable_time_us,
  568. rtc_vddsdio_config_t vddsdio_config)
  569. {
  570. // Enter sleep
  571. esp_err_t err = esp_sleep_start(pd_flags);
  572. // If VDDSDIO regulator was controlled by RTC registers before sleep,
  573. // restore the configuration.
  574. if (vddsdio_config.force) {
  575. rtc_vddsdio_set_config(vddsdio_config);
  576. }
  577. // If SPI flash was powered down, wait for it to become ready
  578. if (pd_flags & RTC_SLEEP_PD_VDDSDIO) {
  579. // Wait for the flash chip to start up
  580. esp_rom_delay_us(flash_enable_time_us);
  581. }
  582. return err;
  583. }
  584. esp_err_t esp_light_sleep_start(void)
  585. {
  586. s_config.ccount_ticks_record = cpu_ll_get_cycle_count();
  587. static portMUX_TYPE light_sleep_lock = portMUX_INITIALIZER_UNLOCKED;
  588. portENTER_CRITICAL(&light_sleep_lock);
  589. /* We will be calling esp_timer_private_advance inside DPORT access critical
  590. * section. Make sure the code on the other CPU is not holding esp_timer
  591. * lock, otherwise there will be deadlock.
  592. */
  593. esp_timer_private_lock();
  594. s_config.rtc_ticks_at_sleep_start = rtc_time_get();
  595. uint32_t ccount_at_sleep_start = cpu_ll_get_cycle_count();
  596. uint64_t frc_time_at_start = esp_system_get_time();
  597. uint32_t sleep_time_overhead_in = (ccount_at_sleep_start - s_config.ccount_ticks_record) / (esp_clk_cpu_freq() / 1000000ULL);
  598. DPORT_STALL_OTHER_CPU_START();
  599. // Decide which power domains can be powered down
  600. uint32_t pd_flags = get_power_down_flags();
  601. // Re-calibrate the RTC Timer clock
  602. #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)
  603. uint64_t time_per_us = 1000000ULL;
  604. s_config.rtc_clk_cal_period = (time_per_us << RTC_CLK_CAL_FRACT) / rtc_clk_slow_freq_get_hz();
  605. #elif defined(CONFIG_ESP32S2_RTC_CLK_SRC_INT_RC)
  606. s_config.rtc_clk_cal_period = rtc_clk_cal_cycling(RTC_CAL_RTC_MUX, RTC_CLK_SRC_CAL_CYCLES);
  607. esp_clk_slowclk_cal_set(s_config.rtc_clk_cal_period);
  608. #else
  609. s_config.rtc_clk_cal_period = rtc_clk_cal(RTC_CAL_RTC_MUX, RTC_CLK_SRC_CAL_CYCLES);
  610. esp_clk_slowclk_cal_set(s_config.rtc_clk_cal_period);
  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 pad_num;
  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_err_t esp_sleep_disable_wifi_wakeup(void)
  1035. {
  1036. #if SOC_PM_SUPPORT_WIFI_WAKEUP
  1037. s_config.wakeup_triggers &= (~RTC_WIFI_TRIG_EN);
  1038. return ESP_OK;
  1039. #else
  1040. return ESP_ERR_NOT_SUPPORTED;
  1041. #endif
  1042. }
  1043. esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause(void)
  1044. {
  1045. if (rtc_get_reset_reason(0) != DEEPSLEEP_RESET && !s_light_sleep_wakeup) {
  1046. return ESP_SLEEP_WAKEUP_UNDEFINED;
  1047. }
  1048. #ifdef CONFIG_IDF_TARGET_ESP32
  1049. uint32_t wakeup_cause = REG_GET_FIELD(RTC_CNTL_WAKEUP_STATE_REG, RTC_CNTL_WAKEUP_CAUSE);
  1050. #else
  1051. uint32_t wakeup_cause = REG_GET_FIELD(RTC_CNTL_SLP_WAKEUP_CAUSE_REG, RTC_CNTL_WAKEUP_CAUSE);
  1052. #endif
  1053. if (wakeup_cause & RTC_TIMER_TRIG_EN) {
  1054. return ESP_SLEEP_WAKEUP_TIMER;
  1055. } else if (wakeup_cause & RTC_GPIO_TRIG_EN) {
  1056. return ESP_SLEEP_WAKEUP_GPIO;
  1057. } else if (wakeup_cause & (RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN)) {
  1058. return ESP_SLEEP_WAKEUP_UART;
  1059. #if SOC_PM_SUPPORT_EXT_WAKEUP
  1060. } else if (wakeup_cause & RTC_EXT0_TRIG_EN) {
  1061. return ESP_SLEEP_WAKEUP_EXT0;
  1062. } else if (wakeup_cause & RTC_EXT1_TRIG_EN) {
  1063. return ESP_SLEEP_WAKEUP_EXT1;
  1064. #endif
  1065. #if SOC_TOUCH_PAD_WAKE_SUPPORTED
  1066. } else if (wakeup_cause & RTC_TOUCH_TRIG_EN) {
  1067. return ESP_SLEEP_WAKEUP_TOUCHPAD;
  1068. #endif
  1069. #if SOC_ULP_SUPPORTED
  1070. } else if (wakeup_cause & RTC_ULP_TRIG_EN) {
  1071. return ESP_SLEEP_WAKEUP_ULP;
  1072. #endif
  1073. #if SOC_PM_SUPPORT_WIFI_WAKEUP
  1074. } else if (wakeup_cause & RTC_WIFI_TRIG_EN) {
  1075. return ESP_SLEEP_WAKEUP_WIFI;
  1076. #endif
  1077. #if SOC_PM_SUPPORT_BT_WAKEUP
  1078. } else if (wakeup_cause & RTC_BT_TRIG_EN) {
  1079. return ESP_SLEEP_WAKEUP_BT;
  1080. #endif
  1081. #if CONFIG_IDF_TARGET_ESP32S2
  1082. } else if (wakeup_cause & RTC_COCPU_TRIG_EN) {
  1083. return ESP_SLEEP_WAKEUP_ULP;
  1084. } else if (wakeup_cause & RTC_COCPU_TRAP_TRIG_EN) {
  1085. return ESP_SLEEP_WAKEUP_COCPU_TRAP_TRIG;
  1086. #endif
  1087. } else {
  1088. return ESP_SLEEP_WAKEUP_UNDEFINED;
  1089. }
  1090. }
  1091. esp_err_t esp_sleep_pd_config(esp_sleep_pd_domain_t domain,
  1092. esp_sleep_pd_option_t option)
  1093. {
  1094. if (domain >= ESP_PD_DOMAIN_MAX || option > ESP_PD_OPTION_AUTO) {
  1095. return ESP_ERR_INVALID_ARG;
  1096. }
  1097. s_config.pd_options[domain] = option;
  1098. return ESP_OK;
  1099. }
  1100. static uint32_t get_power_down_flags(void)
  1101. {
  1102. // Where needed, convert AUTO options to ON. Later interpret AUTO as OFF.
  1103. // RTC_SLOW_MEM is needed for the ULP, so keep RTC_SLOW_MEM powered up if ULP
  1104. // is used and RTC_SLOW_MEM is Auto.
  1105. // If there is any data placed into .rtc.data or .rtc.bss segments, and
  1106. // RTC_SLOW_MEM is Auto, keep it powered up as well.
  1107. #if SOC_RTC_SLOW_MEM_SUPPORTED && SOC_ULP_SUPPORTED
  1108. // Labels are defined in the linker script
  1109. extern int _rtc_slow_length;
  1110. if ((s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] == ESP_PD_OPTION_AUTO) &&
  1111. ((size_t) &_rtc_slow_length > 0 ||
  1112. (s_config.wakeup_triggers & RTC_ULP_TRIG_EN))) {
  1113. s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] = ESP_PD_OPTION_ON;
  1114. }
  1115. #endif
  1116. #if !CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  1117. /* RTC_FAST_MEM is needed for deep sleep stub.
  1118. If RTC_FAST_MEM is Auto, keep it powered on, so that deep sleep stub can run.
  1119. In the new chip revision, deep sleep stub will be optional, and this can be changed. */
  1120. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] == ESP_PD_OPTION_AUTO) {
  1121. s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] = ESP_PD_OPTION_ON;
  1122. }
  1123. #else
  1124. /* If RTC_FAST_MEM is used for heap, force RTC_FAST_MEM to be powered on. */
  1125. s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] = ESP_PD_OPTION_ON;
  1126. #endif
  1127. // RTC_PERIPH is needed for EXT0 wakeup and GPIO wakeup.
  1128. // If RTC_PERIPH is auto, and EXT0/GPIO aren't enabled, power down RTC_PERIPH.
  1129. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] == ESP_PD_OPTION_AUTO) {
  1130. #if SOC_TOUCH_PAD_WAKE_SUPPORTED
  1131. uint32_t wakeup_source = RTC_TOUCH_TRIG_EN;
  1132. #if SOC_ULP_SUPPORTED
  1133. wakeup_source |= RTC_ULP_TRIG_EN;
  1134. #endif
  1135. if (s_config.wakeup_triggers & (RTC_EXT0_TRIG_EN | RTC_GPIO_TRIG_EN)) {
  1136. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_ON;
  1137. } else if (s_config.wakeup_triggers & wakeup_source) {
  1138. // In both rev. 0 and rev. 1 of ESP32, forcing power up of RTC_PERIPH
  1139. // prevents ULP timer and touch FSMs from working correctly.
  1140. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_OFF;
  1141. }
  1142. #else
  1143. if (s_config.wakeup_triggers & RTC_GPIO_TRIG_EN) {
  1144. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_ON;
  1145. } else {
  1146. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_OFF;
  1147. }
  1148. #endif // SOC_TOUCH_PAD_WAKE_SUPPORTED
  1149. }
  1150. #if SOC_PM_SUPPORT_CPU_PD
  1151. if (s_config.cpu_pd_mem == NULL) {
  1152. s_config.pd_options[ESP_PD_DOMAIN_CPU] = ESP_PD_OPTION_ON;
  1153. }
  1154. #else
  1155. if (s_config.pd_options[ESP_PD_DOMAIN_CPU] != ESP_PD_OPTION_ON) {
  1156. s_config.pd_options[ESP_PD_DOMAIN_CPU] = ESP_PD_OPTION_ON;
  1157. }
  1158. #endif
  1159. if (s_config.pd_options[ESP_PD_DOMAIN_XTAL] == ESP_PD_OPTION_AUTO) {
  1160. s_config.pd_options[ESP_PD_DOMAIN_XTAL] = ESP_PD_OPTION_OFF;
  1161. }
  1162. const char *option_str[] = {"OFF", "ON", "AUTO(OFF)" /* Auto works as OFF */};
  1163. ESP_LOGD(TAG, "RTC_PERIPH: %s", option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH]]);
  1164. #if SOC_RTC_SLOW_MEM_SUPPORTED
  1165. ESP_LOGD(TAG, "RTC_SLOW_MEM: %s", option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM]]);
  1166. #endif
  1167. ESP_LOGD(TAG, "RTC_FAST_MEM: %s", option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM]]);
  1168. // Prepare flags based on the selected options
  1169. uint32_t pd_flags = 0;
  1170. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] != ESP_PD_OPTION_ON) {
  1171. pd_flags |= RTC_SLEEP_PD_RTC_FAST_MEM;
  1172. }
  1173. #if SOC_RTC_SLOW_MEM_SUPPORTED
  1174. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] != ESP_PD_OPTION_ON) {
  1175. pd_flags |= RTC_SLEEP_PD_RTC_SLOW_MEM;
  1176. }
  1177. #endif
  1178. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] != ESP_PD_OPTION_ON) {
  1179. pd_flags |= RTC_SLEEP_PD_RTC_PERIPH;
  1180. }
  1181. #if SOC_PM_SUPPORT_CPU_PD
  1182. if (s_config.pd_options[ESP_PD_DOMAIN_CPU] != ESP_PD_OPTION_ON) {
  1183. pd_flags |= RTC_SLEEP_PD_CPU;
  1184. }
  1185. #endif
  1186. #ifdef CONFIG_IDF_TARGET_ESP32
  1187. pd_flags |= RTC_SLEEP_PD_XTAL;
  1188. #endif
  1189. /**
  1190. * VDD_SDIO power domain shall be kept on during the light sleep
  1191. * when CONFIG_ESP_SYSTEM_PD_FLASH is not set and off when it is set.
  1192. * The application can still force the power domain to remain on by calling
  1193. * `esp_sleep_pd_config` before getting into light sleep mode.
  1194. *
  1195. * In deep sleep mode, the power domain will be turned off, regardless the
  1196. * value of this field.
  1197. */
  1198. if (s_config.pd_options[ESP_PD_DOMAIN_VDDSDIO] == ESP_PD_OPTION_AUTO) {
  1199. #ifdef CONFIG_ESP_SYSTEM_PD_FLASH
  1200. s_config.pd_options[ESP_PD_DOMAIN_VDDSDIO] = ESP_PD_OPTION_OFF;
  1201. #else
  1202. s_config.pd_options[ESP_PD_DOMAIN_VDDSDIO] = ESP_PD_OPTION_ON;
  1203. #endif
  1204. }
  1205. if (s_config.pd_options[ESP_PD_DOMAIN_VDDSDIO] != ESP_PD_OPTION_ON) {
  1206. pd_flags |= RTC_SLEEP_PD_VDDSDIO;
  1207. }
  1208. #if ((defined CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS) && (defined CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT))
  1209. if ((s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) == 0) {
  1210. // If enabled EXT1 only and enable the additional current by touch, should be keep RTC_PERIPH power on.
  1211. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  1212. }
  1213. #endif
  1214. return pd_flags;
  1215. }
  1216. void esp_deep_sleep_disable_rom_logging(void)
  1217. {
  1218. esp_rom_disable_logging();
  1219. }