sleep_modes.c 48 KB

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