sleep_modes.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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_memory_utils.h"
  12. #include "esp_sleep.h"
  13. #include "esp_private/esp_timer_private.h"
  14. #include "esp_private/system_internal.h"
  15. #include "esp_log.h"
  16. #include "esp_newlib.h"
  17. #include "esp_timer.h"
  18. #include "esp_ipc_isr.h"
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/task.h"
  21. #include "soc/soc_caps.h"
  22. #include "driver/rtc_io.h"
  23. #include "hal/rtc_io_hal.h"
  24. #include "hal/rtc_cntl_ll.h"
  25. #include "driver/uart.h"
  26. #include "soc/rtc.h"
  27. #include "soc/soc_caps.h"
  28. #include "regi2c_ctrl.h" //For `REGI2C_ANA_CALI_PD_WORKAROUND`, temp
  29. #include "hal/wdt_hal.h"
  30. #include "hal/rtc_hal.h"
  31. #include "hal/uart_hal.h"
  32. #if SOC_TOUCH_SENSOR_SUPPORTED
  33. #include "hal/touch_sensor_hal.h"
  34. #include "driver/touch_sensor.h"
  35. #include "driver/touch_sensor_common.h"
  36. #endif
  37. #include "hal/clk_gate_ll.h"
  38. #include "sdkconfig.h"
  39. #include "esp_rom_uart.h"
  40. #include "esp_rom_sys.h"
  41. #include "esp_private/brownout.h"
  42. #include "esp_private/sleep_retention.h"
  43. #include "esp_private/esp_clk.h"
  44. #include "esp_private/startup_internal.h"
  45. #ifdef CONFIG_IDF_TARGET_ESP32
  46. #include "esp32/rom/cache.h"
  47. #include "esp32/rom/rtc.h"
  48. #include "esp_private/gpio.h"
  49. #include "esp_private/sleep_gpio.h"
  50. #elif CONFIG_IDF_TARGET_ESP32S2
  51. #include "esp32s2/rom/cache.h"
  52. #include "esp32s2/rom/rtc.h"
  53. #include "soc/extmem_reg.h"
  54. #include "esp_private/gpio.h"
  55. #elif CONFIG_IDF_TARGET_ESP32S3
  56. #include "esp32s3/rom/cache.h"
  57. #include "esp32s3/rom/rtc.h"
  58. #include "soc/extmem_reg.h"
  59. #include "esp_private/sleep_mac_bb.h"
  60. #elif CONFIG_IDF_TARGET_ESP32C3
  61. #include "esp32c3/rom/cache.h"
  62. #include "esp32c3/rom/rtc.h"
  63. #include "soc/extmem_reg.h"
  64. #include "esp_private/sleep_mac_bb.h"
  65. #elif CONFIG_IDF_TARGET_ESP32H2
  66. #include "esp32h2/rom/cache.h"
  67. #include "esp32h2/rom/rtc.h"
  68. #include "soc/extmem_reg.h"
  69. #elif CONFIG_IDF_TARGET_ESP32C2
  70. #include "esp32c2/rom/cache.h"
  71. #include "esp32c2/rom/rtc.h"
  72. #include "soc/extmem_reg.h"
  73. #endif
  74. // If light sleep time is less than that, don't power down flash
  75. #define FLASH_PD_MIN_SLEEP_TIME_US 2000
  76. // Time from VDD_SDIO power up to first flash read in ROM code
  77. #define VDD_SDIO_POWERUP_TO_FLASH_READ_US 700
  78. // Cycles for RTC Timer clock source (internal oscillator) calibrate
  79. #define RTC_CLK_SRC_CAL_CYCLES (10)
  80. #ifdef CONFIG_IDF_TARGET_ESP32
  81. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (212)
  82. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (60)
  83. #elif CONFIG_IDF_TARGET_ESP32S2
  84. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (147)
  85. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (28)
  86. #elif CONFIG_IDF_TARGET_ESP32S3
  87. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (382)
  88. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (133)
  89. #elif CONFIG_IDF_TARGET_ESP32C3
  90. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (105)
  91. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (37)
  92. #elif CONFIG_IDF_TARGET_ESP32H2
  93. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (105)
  94. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (37)
  95. #elif CONFIG_IDF_TARGET_ESP32C2
  96. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (118)
  97. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (9)
  98. #endif
  99. #define LIGHT_SLEEP_TIME_OVERHEAD_US DEFAULT_HARDWARE_OUT_OVERHEAD_US
  100. #ifdef CONFIG_ESP_SYSTEM_RTC_EXT_XTAL
  101. #define DEEP_SLEEP_TIME_OVERHEAD_US (650 + 100 * 240 / CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ)
  102. #else
  103. #define DEEP_SLEEP_TIME_OVERHEAD_US (250 + 100 * 240 / CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ)
  104. #endif
  105. #if CONFIG_ESP_SLEEP_DEEP_SLEEP_WAKEUP_DELAY
  106. #define DEEP_SLEEP_WAKEUP_DELAY CONFIG_ESP_SLEEP_DEEP_SLEEP_WAKEUP_DELAY
  107. #else
  108. #define DEEP_SLEEP_WAKEUP_DELAY 0
  109. #endif
  110. extern void periph_inform_out_light_sleep_overhead(uint32_t out_light_sleep_time);
  111. // Minimal amount of time we can sleep for
  112. #define LIGHT_SLEEP_MIN_TIME_US 200
  113. #define RTC_MODULE_SLEEP_PREPARE_CYCLES (6)
  114. #define CHECK_SOURCE(source, value, mask) ((s_config.wakeup_triggers & mask) && \
  115. (source == value))
  116. /**
  117. * Internal structure which holds all requested deep sleep parameters
  118. */
  119. typedef struct {
  120. esp_sleep_pd_option_t pd_options[ESP_PD_DOMAIN_MAX];
  121. uint64_t sleep_duration;
  122. uint32_t wakeup_triggers : 15;
  123. uint32_t ext1_trigger_mode : 1;
  124. uint32_t ext1_rtc_gpio_mask : 22; //22 is the maximum RTCIO number in all chips
  125. uint32_t ext0_trigger_level : 1;
  126. uint32_t ext0_rtc_gpio_num : 5;
  127. uint32_t gpio_wakeup_mask : 6;
  128. uint32_t gpio_trigger_mode : 6;
  129. uint32_t sleep_time_adjustment;
  130. uint32_t ccount_ticks_record;
  131. uint32_t sleep_time_overhead_out;
  132. uint32_t rtc_clk_cal_period;
  133. uint64_t rtc_ticks_at_sleep_start;
  134. } sleep_config_t;
  135. _Static_assert(22 >= SOC_RTCIO_PIN_COUNT, "Chip has more RTCIOs than 22, should increase ext1_rtc_gpio_mask field size");
  136. static sleep_config_t s_config = {
  137. .pd_options = {
  138. #if SOC_PM_SUPPORT_RTC_PERIPH_PD
  139. ESP_PD_OPTION_AUTO,
  140. #endif
  141. #if SOC_PM_SUPPORT_RTC_SLOW_MEM_PD
  142. ESP_PD_OPTION_AUTO,
  143. #endif
  144. #if SOC_PM_SUPPORT_RTC_FAST_MEM_PD
  145. ESP_PD_OPTION_AUTO,
  146. #endif
  147. ESP_PD_OPTION_AUTO,
  148. #if SOC_PM_SUPPORT_CPU_PD
  149. ESP_PD_OPTION_AUTO,
  150. #endif
  151. ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO
  152. },
  153. .ccount_ticks_record = 0,
  154. .sleep_time_overhead_out = DEFAULT_SLEEP_OUT_OVERHEAD_US,
  155. .wakeup_triggers = 0
  156. };
  157. /* Internal variable used to track if light sleep wakeup sources are to be
  158. expected when determining wakeup cause. */
  159. static bool s_light_sleep_wakeup = false;
  160. /* Updating RTC_MEMORY_CRC_REG register via set_rtc_memory_crc()
  161. is not thread-safe, so we need to disable interrupts before going to deep sleep. */
  162. static portMUX_TYPE spinlock_rtc_deep_sleep = portMUX_INITIALIZER_UNLOCKED;
  163. static const char *TAG = "sleep";
  164. static RTC_FAST_ATTR bool s_adc_tsen_enabled = false;
  165. //in this mode, 2uA is saved, but RTC memory can't use at high temperature, and RTCIO can't be used as INPUT.
  166. static bool s_ultra_low_enabled = false;
  167. static bool s_periph_use_8m_flag = false;
  168. void esp_sleep_periph_use_8m(bool use_or_not)
  169. {
  170. s_periph_use_8m_flag = use_or_not;
  171. }
  172. static uint32_t get_power_down_flags(void);
  173. #if SOC_PM_SUPPORT_EXT_WAKEUP
  174. static void ext0_wakeup_prepare(void);
  175. static void ext1_wakeup_prepare(void);
  176. #endif
  177. static void timer_wakeup_prepare(void);
  178. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  179. static void touch_wakeup_prepare(void);
  180. #endif
  181. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  182. static void esp_deep_sleep_wakeup_prepare(void);
  183. #endif
  184. #if SOC_PM_SUPPORT_DEEPSLEEP_CHECK_STUB_ONLY
  185. static RTC_FAST_ATTR esp_deep_sleep_wake_stub_fn_t wake_stub_fn_handler = NULL;
  186. static void RTC_IRAM_ATTR __attribute__((used, noinline)) esp_wake_stub_start(void)
  187. {
  188. if (wake_stub_fn_handler) {
  189. (*wake_stub_fn_handler)();
  190. }
  191. }
  192. /* We must have a default deep sleep wake stub entry function, which must be
  193. * located at the start address of the RTC fast memory, and its implementation
  194. * must be simple enough to ensure that there is no litteral data before the
  195. * wake stub entry, otherwise, the litteral data before the wake stub entry
  196. * will not be CRC checked. */
  197. static void __attribute__((section(".rtc.entry.text"))) esp_wake_stub_entry(void)
  198. {
  199. #define _SYM2STR(s) # s
  200. #define SYM2STR(s) _SYM2STR(s)
  201. // call4 has a larger effective addressing range (-524284 to 524288 bytes),
  202. // which is sufficient for instruction addressing in RTC fast memory.
  203. __asm__ __volatile__ ("call4 " SYM2STR(esp_wake_stub_start) "\n");
  204. }
  205. #endif // SOC_PM_SUPPORT_DEEPSLEEP_CHECK_STUB_ONLY
  206. /* Wake from deep sleep stub
  207. See esp_deepsleep.h esp_wake_deep_sleep() comments for details.
  208. */
  209. esp_deep_sleep_wake_stub_fn_t esp_get_deep_sleep_wake_stub(void)
  210. {
  211. #if SOC_PM_SUPPORT_DEEPSLEEP_CHECK_STUB_ONLY
  212. esp_deep_sleep_wake_stub_fn_t stub_ptr = wake_stub_fn_handler;
  213. #else
  214. esp_deep_sleep_wake_stub_fn_t stub_ptr = (esp_deep_sleep_wake_stub_fn_t) REG_READ(RTC_ENTRY_ADDR_REG);
  215. #endif
  216. if (!esp_ptr_executable(stub_ptr)) {
  217. return NULL;
  218. }
  219. return stub_ptr;
  220. }
  221. void esp_set_deep_sleep_wake_stub(esp_deep_sleep_wake_stub_fn_t new_stub)
  222. {
  223. #if SOC_PM_SUPPORT_DEEPSLEEP_CHECK_STUB_ONLY
  224. wake_stub_fn_handler = new_stub;
  225. #else
  226. REG_WRITE(RTC_ENTRY_ADDR_REG, (uint32_t)new_stub);
  227. #endif
  228. }
  229. void RTC_IRAM_ATTR esp_default_wake_deep_sleep(void)
  230. {
  231. /* Clear MMU for CPU 0 */
  232. #if CONFIG_IDF_TARGET_ESP32
  233. _DPORT_REG_WRITE(DPORT_PRO_CACHE_CTRL1_REG,
  234. _DPORT_REG_READ(DPORT_PRO_CACHE_CTRL1_REG) | DPORT_PRO_CACHE_MMU_IA_CLR);
  235. _DPORT_REG_WRITE(DPORT_PRO_CACHE_CTRL1_REG,
  236. _DPORT_REG_READ(DPORT_PRO_CACHE_CTRL1_REG) & (~DPORT_PRO_CACHE_MMU_IA_CLR));
  237. #if DEEP_SLEEP_WAKEUP_DELAY > 0
  238. // ROM code has not started yet, so we need to set delay factor
  239. // used by esp_rom_delay_us first.
  240. ets_update_cpu_frequency_rom(ets_get_detected_xtal_freq() / 1000000);
  241. // This delay is configured in menuconfig, it can be used to give
  242. // the flash chip some time to become ready.
  243. esp_rom_delay_us(DEEP_SLEEP_WAKEUP_DELAY);
  244. #endif
  245. #elif CONFIG_IDF_TARGET_ESP32S2
  246. REG_SET_BIT(EXTMEM_CACHE_DBG_INT_ENA_REG, EXTMEM_CACHE_DBG_EN);
  247. #endif
  248. }
  249. void __attribute__((weak, alias("esp_default_wake_deep_sleep"))) esp_wake_deep_sleep(void);
  250. void esp_deep_sleep(uint64_t time_in_us)
  251. {
  252. esp_sleep_enable_timer_wakeup(time_in_us);
  253. esp_deep_sleep_start();
  254. }
  255. // [refactor-todo] provide target logic for body of uart functions below
  256. static void IRAM_ATTR flush_uarts(void)
  257. {
  258. for (int i = 0; i < SOC_UART_NUM; ++i) {
  259. #ifdef CONFIG_IDF_TARGET_ESP32
  260. esp_rom_uart_tx_wait_idle(i);
  261. #else
  262. if (periph_ll_periph_enabled(PERIPH_UART0_MODULE + i)) {
  263. esp_rom_uart_tx_wait_idle(i);
  264. }
  265. #endif
  266. }
  267. }
  268. static void IRAM_ATTR suspend_uarts(void)
  269. {
  270. for (int i = 0; i < SOC_UART_NUM; ++i) {
  271. #ifndef CONFIG_IDF_TARGET_ESP32
  272. if (!periph_ll_periph_enabled(PERIPH_UART0_MODULE + i)) {
  273. continue;
  274. }
  275. #endif
  276. uart_ll_force_xoff(i);
  277. #if SOC_UART_SUPPORT_FSM_TX_WAIT_SEND
  278. uint32_t uart_fsm = 0;
  279. do {
  280. uart_fsm = uart_ll_get_fsm_status(i);
  281. } while (!(uart_fsm == UART_LL_FSM_IDLE || uart_fsm == UART_LL_FSM_TX_WAIT_SEND));
  282. #else
  283. while (uart_ll_get_fsm_status(i) != 0) {}
  284. #endif
  285. }
  286. }
  287. static void IRAM_ATTR resume_uarts(void)
  288. {
  289. for (int i = 0; i < SOC_UART_NUM; ++i) {
  290. #ifndef CONFIG_IDF_TARGET_ESP32
  291. if (!periph_ll_periph_enabled(PERIPH_UART0_MODULE + i)) {
  292. continue;
  293. }
  294. #endif
  295. uart_ll_force_xon(i);
  296. }
  297. }
  298. /**
  299. * These save-restore workaround should be moved to lower layer
  300. */
  301. inline static void IRAM_ATTR misc_modules_sleep_prepare(void)
  302. {
  303. #if CONFIG_MAC_BB_PD
  304. mac_bb_power_down_cb_execute();
  305. #endif
  306. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  307. gpio_sleep_mode_config_apply();
  308. #endif
  309. #if SOC_PM_SUPPORT_CPU_PD || SOC_PM_SUPPORT_TAGMEM_PD
  310. sleep_enable_memory_retention();
  311. #endif
  312. #if REGI2C_ANA_CALI_PD_WORKAROUND
  313. regi2c_analog_cali_reg_read();
  314. #endif
  315. }
  316. /**
  317. * These save-restore workaround should be moved to lower layer
  318. */
  319. inline static void IRAM_ATTR misc_modules_wake_prepare(void)
  320. {
  321. #if SOC_PM_SUPPORT_CPU_PD || SOC_PM_SUPPORT_TAGMEM_PD
  322. sleep_disable_memory_retention();
  323. #endif
  324. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  325. gpio_sleep_mode_config_unapply();
  326. #endif
  327. #if CONFIG_MAC_BB_PD
  328. mac_bb_power_up_cb_execute();
  329. #endif
  330. #if REGI2C_ANA_CALI_PD_WORKAROUND
  331. regi2c_analog_cali_reg_write();
  332. #endif
  333. }
  334. inline static uint32_t call_rtc_sleep_start(uint32_t reject_triggers, uint32_t lslp_mem_inf_fpu);
  335. inline static bool is_light_sleep(uint32_t pd_flags)
  336. {
  337. return (pd_flags & RTC_SLEEP_PD_DIG) == 0;
  338. }
  339. static uint32_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags)
  340. {
  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 periph_using_8m = !deep_sleep && s_periph_use_8m_flag;
  358. //Override user-configured power modes.
  359. if (rtc_using_8md256 || periph_using_8m) {
  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 (periph_using_8m) {
  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 = esp_cpu_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. /**
  566. * vddsdio is used for power supply of spi flash
  567. *
  568. * pd flash via menuconfig | pd flash via `esp_sleep_pd_config` | result
  569. * ---------------------------------------------------------------------------------------------------
  570. * 0 | 0 | no pd flash
  571. * x | 1 | pd flash with relaxed conditions(force_pd)
  572. * 1 | 0 | pd flash with strict conditions(safe_pd)
  573. */
  574. static inline bool can_power_down_vddsdio(const uint32_t vddsdio_pd_sleep_duration)
  575. {
  576. bool force_pd = !(s_config.wakeup_triggers & RTC_TIMER_TRIG_EN) || (s_config.sleep_duration > vddsdio_pd_sleep_duration);
  577. bool safe_pd = (s_config.wakeup_triggers == RTC_TIMER_TRIG_EN) && (s_config.sleep_duration > vddsdio_pd_sleep_duration);
  578. return (s_config.pd_options[ESP_PD_DOMAIN_VDDSDIO] == ESP_PD_OPTION_OFF) ? force_pd : safe_pd;
  579. }
  580. esp_err_t esp_light_sleep_start(void)
  581. {
  582. s_config.ccount_ticks_record = esp_cpu_get_cycle_count();
  583. static portMUX_TYPE light_sleep_lock = portMUX_INITIALIZER_UNLOCKED;
  584. portENTER_CRITICAL(&light_sleep_lock);
  585. /*
  586. Note: We are about to stall the other CPU via the esp_ipc_isr_stall_other_cpu(). However, there is a chance of
  587. deadlock if after stalling the other CPU, we attempt to take spinlocks already held by the other CPU that is.
  588. Thus any functions that we call after stalling the other CPU will need to have the locks taken first to avoid
  589. deadlock.
  590. Todo: IDF-5257
  591. */
  592. /* We will be calling esp_timer_private_set inside DPORT access critical
  593. * section. Make sure the code on the other CPU is not holding esp_timer
  594. * lock, otherwise there will be deadlock.
  595. */
  596. esp_timer_private_lock();
  597. /* We will be calling esp_rtc_get_time_us() below. Make sure the code on the other CPU is not holding the
  598. * esp_rtc_get_time_us() lock, otherwise there will be deadlock. esp_rtc_get_time_us() is called via:
  599. *
  600. * - esp_clk_slowclk_cal_set() -> esp_rtc_get_time_us()
  601. */
  602. esp_clk_private_lock();
  603. s_config.rtc_ticks_at_sleep_start = rtc_time_get();
  604. uint32_t ccount_at_sleep_start = esp_cpu_get_cycle_count();
  605. uint64_t high_res_time_at_start = esp_timer_get_time();
  606. uint32_t sleep_time_overhead_in = (ccount_at_sleep_start - s_config.ccount_ticks_record) / (esp_clk_cpu_freq() / 1000000ULL);
  607. esp_ipc_isr_stall_other_cpu();
  608. // Decide which power domains can be powered down
  609. uint32_t pd_flags = get_power_down_flags();
  610. #ifdef CONFIG_ESP_SLEEP_RTC_BUS_ISO_WORKAROUND
  611. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  612. #endif
  613. // Re-calibrate the RTC Timer clock
  614. #ifdef CONFIG_ESP_SYSTEM_RTC_EXT_XTAL
  615. if (rtc_clk_slow_src_get() == SOC_RTC_SLOW_CLK_SRC_XTAL32K) {
  616. uint64_t time_per_us = 1000000ULL;
  617. s_config.rtc_clk_cal_period = (time_per_us << RTC_CLK_CAL_FRACT) / rtc_clk_slow_freq_get_hz();
  618. } else {
  619. // If the external 32 kHz XTAL does not exist, use the internal 150 kHz RC oscillator
  620. // as the RTC slow clock source.
  621. s_config.rtc_clk_cal_period = rtc_clk_cal(RTC_CAL_RTC_MUX, RTC_CLK_SRC_CAL_CYCLES);
  622. esp_clk_slowclk_cal_set(s_config.rtc_clk_cal_period);
  623. }
  624. #elif CONFIG_RTC_CLK_SRC_INT_RC && CONFIG_IDF_TARGET_ESP32S2
  625. s_config.rtc_clk_cal_period = rtc_clk_cal_cycling(RTC_CAL_RTC_MUX, RTC_CLK_SRC_CAL_CYCLES);
  626. esp_clk_slowclk_cal_set(s_config.rtc_clk_cal_period);
  627. #else
  628. s_config.rtc_clk_cal_period = rtc_clk_cal(RTC_CAL_RTC_MUX, RTC_CLK_SRC_CAL_CYCLES);
  629. esp_clk_slowclk_cal_set(s_config.rtc_clk_cal_period);
  630. #endif
  631. /*
  632. * Adjustment time consists of parts below:
  633. * 1. Hardware time waiting for internal 8M oscilate clock and XTAL;
  634. * 2. Hardware state swithing time of the rtc main state machine;
  635. * 3. Code execution time when clock is not stable;
  636. * 4. Code execution time which can be measured;
  637. */
  638. 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);
  639. s_config.sleep_time_adjustment = LIGHT_SLEEP_TIME_OVERHEAD_US + sleep_time_overhead_in + s_config.sleep_time_overhead_out
  640. + 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);
  641. // Decide if VDD_SDIO needs to be powered down;
  642. // If it needs to be powered down, adjust sleep time.
  643. const uint32_t flash_enable_time_us = VDD_SDIO_POWERUP_TO_FLASH_READ_US + DEEP_SLEEP_WAKEUP_DELAY;
  644. /**
  645. * If VDD_SDIO power domain is requested to be turned off, bit `RTC_SLEEP_PD_VDDSDIO`
  646. * will be set in `pd_flags`.
  647. */
  648. if (pd_flags & RTC_SLEEP_PD_VDDSDIO) {
  649. /*
  650. * When VDD_SDIO power domain has to be turned off, the minimum sleep time of the
  651. * system needs to meet the sum below:
  652. * 1. Wait time for the flash power-on after waking up;
  653. * 2. The execution time of codes between RTC Timer get start time
  654. * with hardware starts to switch state to sleep;
  655. * 3. The hardware state switching time of the rtc state machine during
  656. * sleep and wake-up. This process requires 6 cycles to complete.
  657. * The specific hardware state switching process and the cycles
  658. * consumed are rtc_cpu_run_stall(1), cut_pll_rtl(2), cut_8m(1),
  659. * min_protect(2);
  660. * 4. All the adjustment time which is s_config.sleep_time_adjustment below.
  661. */
  662. const uint32_t vddsdio_pd_sleep_duration = MAX(FLASH_PD_MIN_SLEEP_TIME_US,
  663. flash_enable_time_us + LIGHT_SLEEP_MIN_TIME_US + s_config.sleep_time_adjustment
  664. + rtc_time_slowclk_to_us(RTC_MODULE_SLEEP_PREPARE_CYCLES, s_config.rtc_clk_cal_period));
  665. if (can_power_down_vddsdio(vddsdio_pd_sleep_duration)) {
  666. if (s_config.sleep_time_overhead_out < flash_enable_time_us) {
  667. s_config.sleep_time_adjustment += flash_enable_time_us;
  668. }
  669. } else {
  670. /**
  671. * Minimum sleep time is not enough, then keep the VDD_SDIO power
  672. * domain on.
  673. */
  674. pd_flags &= ~RTC_SLEEP_PD_VDDSDIO;
  675. if (s_config.sleep_time_overhead_out > flash_enable_time_us) {
  676. s_config.sleep_time_adjustment -= flash_enable_time_us;
  677. }
  678. }
  679. }
  680. periph_inform_out_light_sleep_overhead(s_config.sleep_time_adjustment - sleep_time_overhead_in);
  681. rtc_vddsdio_config_t vddsdio_config = rtc_vddsdio_get_config();
  682. // Safety net: enable WDT in case exit from light sleep fails
  683. wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
  684. 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.
  685. if (!wdt_was_enabled) {
  686. wdt_hal_init(&rtc_wdt_ctx, WDT_RWDT, 0, false);
  687. uint32_t stage_timeout_ticks = (uint32_t)(1000ULL * rtc_clk_slow_freq_get_hz() / 1000ULL);
  688. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  689. wdt_hal_config_stage(&rtc_wdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_RTC);
  690. wdt_hal_enable(&rtc_wdt_ctx);
  691. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  692. }
  693. // Enter sleep, then wait for flash to be ready on wakeup
  694. esp_err_t err = esp_light_sleep_inner(pd_flags,
  695. flash_enable_time_us, vddsdio_config);
  696. s_light_sleep_wakeup = true;
  697. // System timer has been stopped for the duration of the sleep, correct for that.
  698. uint64_t rtc_ticks_at_end = rtc_time_get();
  699. 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);
  700. /**
  701. * If sleep duration is too small(less than 1 rtc_slow_clk cycle), rtc_time_diff will be zero.
  702. * In this case, just ignore the time compensation and keep esp_timer monotonic.
  703. */
  704. if (rtc_time_diff > 0) {
  705. esp_timer_private_set(high_res_time_at_start + rtc_time_diff);
  706. }
  707. esp_set_time_from_rtc();
  708. esp_clk_private_unlock();
  709. esp_timer_private_unlock();
  710. esp_ipc_isr_release_other_cpu();
  711. if (!wdt_was_enabled) {
  712. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  713. wdt_hal_disable(&rtc_wdt_ctx);
  714. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  715. }
  716. portEXIT_CRITICAL(&light_sleep_lock);
  717. s_config.sleep_time_overhead_out = (esp_cpu_get_cycle_count() - s_config.ccount_ticks_record) / (esp_clk_cpu_freq() / 1000000ULL);
  718. return err;
  719. }
  720. esp_err_t esp_sleep_disable_wakeup_source(esp_sleep_source_t source)
  721. {
  722. // For most of sources it is enough to set trigger mask in local
  723. // configuration structure. The actual RTC wake up options
  724. // will be updated by esp_sleep_start().
  725. if (source == ESP_SLEEP_WAKEUP_ALL) {
  726. s_config.wakeup_triggers = 0;
  727. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_TIMER, RTC_TIMER_TRIG_EN)) {
  728. s_config.wakeup_triggers &= ~RTC_TIMER_TRIG_EN;
  729. s_config.sleep_duration = 0;
  730. #if SOC_PM_SUPPORT_EXT_WAKEUP
  731. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_EXT0, RTC_EXT0_TRIG_EN)) {
  732. s_config.ext0_rtc_gpio_num = 0;
  733. s_config.ext0_trigger_level = 0;
  734. s_config.wakeup_triggers &= ~RTC_EXT0_TRIG_EN;
  735. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_EXT1, RTC_EXT1_TRIG_EN)) {
  736. s_config.ext1_rtc_gpio_mask = 0;
  737. s_config.ext1_trigger_mode = 0;
  738. s_config.wakeup_triggers &= ~RTC_EXT1_TRIG_EN;
  739. #endif
  740. #if SOC_PM_SUPPORT_TOUCH_SENSOR_WAKEUP
  741. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_TOUCHPAD, RTC_TOUCH_TRIG_EN)) {
  742. s_config.wakeup_triggers &= ~RTC_TOUCH_TRIG_EN;
  743. #endif
  744. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_GPIO, RTC_GPIO_TRIG_EN)) {
  745. s_config.wakeup_triggers &= ~RTC_GPIO_TRIG_EN;
  746. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_UART, (RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN))) {
  747. s_config.wakeup_triggers &= ~(RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN);
  748. }
  749. #if CONFIG_ULP_COPROC_ENABLED
  750. else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_ULP, RTC_ULP_TRIG_EN)) {
  751. s_config.wakeup_triggers &= ~RTC_ULP_TRIG_EN;
  752. }
  753. #endif
  754. else {
  755. ESP_LOGE(TAG, "Incorrect wakeup source (%d) to disable.", (int) source);
  756. return ESP_ERR_INVALID_STATE;
  757. }
  758. return ESP_OK;
  759. }
  760. esp_err_t esp_sleep_enable_ulp_wakeup(void)
  761. {
  762. #ifndef CONFIG_ULP_COPROC_ENABLED
  763. return ESP_ERR_INVALID_STATE;
  764. #endif // CONFIG_ULP_COPROC_ENABLED
  765. #if CONFIG_IDF_TARGET_ESP32
  766. #if ((defined CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT) || (defined CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT_V2))
  767. ESP_LOGE(TAG, "Failed to enable wakeup when provide current to external 32kHz crystal");
  768. return ESP_ERR_NOT_SUPPORTED;
  769. #endif
  770. if (s_config.wakeup_triggers & RTC_EXT0_TRIG_EN) {
  771. ESP_LOGE(TAG, "Conflicting wake-up trigger: ext0");
  772. return ESP_ERR_INVALID_STATE;
  773. }
  774. #endif //CONFIG_IDF_TARGET_ESP32
  775. #if CONFIG_ULP_COPROC_TYPE_FSM
  776. s_config.wakeup_triggers |= RTC_ULP_TRIG_EN;
  777. return ESP_OK;
  778. #elif CONFIG_ULP_COPROC_TYPE_RISCV
  779. s_config.wakeup_triggers |= (RTC_COCPU_TRIG_EN | RTC_COCPU_TRAP_TRIG_EN);
  780. return ESP_OK;
  781. #else
  782. return ESP_ERR_NOT_SUPPORTED;
  783. #endif //CONFIG_ULP_COPROC_TYPE_FSM
  784. }
  785. esp_err_t esp_sleep_enable_timer_wakeup(uint64_t time_in_us)
  786. {
  787. s_config.wakeup_triggers |= RTC_TIMER_TRIG_EN;
  788. s_config.sleep_duration = time_in_us;
  789. return ESP_OK;
  790. }
  791. static void timer_wakeup_prepare(void)
  792. {
  793. int64_t sleep_duration = (int64_t) s_config.sleep_duration - (int64_t) s_config.sleep_time_adjustment;
  794. if (sleep_duration < 0) {
  795. sleep_duration = 0;
  796. }
  797. int64_t ticks = rtc_time_us_to_slowclk(sleep_duration, s_config.rtc_clk_cal_period);
  798. rtc_hal_set_wakeup_timer(s_config.rtc_ticks_at_sleep_start + ticks);
  799. }
  800. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  801. /* In deep sleep mode, only the sleep channel is supported, and other touch channels should be turned off. */
  802. static void touch_wakeup_prepare(void)
  803. {
  804. uint16_t sleep_cycle = 0;
  805. uint16_t meas_times = 0;
  806. touch_pad_t touch_num = TOUCH_PAD_NUM0;
  807. touch_ll_sleep_get_channel_num(&touch_num); // Check if the sleep pad is enabled.
  808. if ((touch_num > TOUCH_PAD_NUM0) && (touch_num < TOUCH_PAD_MAX) && touch_ll_get_fsm_state()) {
  809. touch_ll_stop_fsm();
  810. touch_ll_clear_channel_mask(TOUCH_PAD_BIT_MASK_ALL);
  811. touch_ll_intr_clear(TOUCH_PAD_INTR_MASK_ALL); // Clear state from previous wakeup
  812. touch_hal_sleep_channel_get_work_time(&sleep_cycle, &meas_times);
  813. touch_ll_set_meas_times(meas_times);
  814. touch_ll_set_sleep_time(sleep_cycle);
  815. touch_ll_set_channel_mask(BIT(touch_num));
  816. touch_ll_start_fsm();
  817. }
  818. }
  819. #endif
  820. #if SOC_TOUCH_SENSOR_SUPPORTED
  821. esp_err_t esp_sleep_enable_touchpad_wakeup(void)
  822. {
  823. #if ((defined CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT) || (defined CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT_V2))
  824. ESP_LOGE(TAG, "Failed to enable wakeup when provide current to external 32kHz crystal");
  825. return ESP_ERR_NOT_SUPPORTED;
  826. #endif
  827. if (s_config.wakeup_triggers & (RTC_EXT0_TRIG_EN)) {
  828. ESP_LOGE(TAG, "Conflicting wake-up trigger: ext0");
  829. return ESP_ERR_INVALID_STATE;
  830. }
  831. s_config.wakeup_triggers |= RTC_TOUCH_TRIG_EN;
  832. return ESP_OK;
  833. }
  834. touch_pad_t esp_sleep_get_touchpad_wakeup_status(void)
  835. {
  836. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_TOUCHPAD) {
  837. return TOUCH_PAD_MAX;
  838. }
  839. touch_pad_t pad_num;
  840. esp_err_t ret = touch_pad_get_wakeup_status(&pad_num); //TODO 723diff commit id:fda9ada1b
  841. assert(ret == ESP_OK && "wakeup reason is RTC_TOUCH_TRIG_EN but SENS_TOUCH_MEAS_EN is zero");
  842. return (ret == ESP_OK) ? pad_num : TOUCH_PAD_MAX;
  843. }
  844. #endif // SOC_TOUCH_SENSOR_SUPPORTED
  845. bool esp_sleep_is_valid_wakeup_gpio(gpio_num_t gpio_num)
  846. {
  847. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  848. return RTC_GPIO_IS_VALID_GPIO(gpio_num);
  849. #else
  850. return GPIO_IS_DEEP_SLEEP_WAKEUP_VALID_GPIO(gpio_num);
  851. #endif // SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  852. }
  853. #if SOC_PM_SUPPORT_EXT_WAKEUP
  854. esp_err_t esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level)
  855. {
  856. if (level < 0 || level > 1) {
  857. return ESP_ERR_INVALID_ARG;
  858. }
  859. if (!esp_sleep_is_valid_wakeup_gpio(gpio_num)) {
  860. return ESP_ERR_INVALID_ARG;
  861. }
  862. if (s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) {
  863. ESP_LOGE(TAG, "Conflicting wake-up triggers: touch / ULP");
  864. return ESP_ERR_INVALID_STATE;
  865. }
  866. s_config.ext0_rtc_gpio_num = rtc_io_number_get(gpio_num);
  867. s_config.ext0_trigger_level = level;
  868. s_config.wakeup_triggers |= RTC_EXT0_TRIG_EN;
  869. return ESP_OK;
  870. }
  871. static void ext0_wakeup_prepare(void)
  872. {
  873. int rtc_gpio_num = s_config.ext0_rtc_gpio_num;
  874. rtcio_hal_ext0_set_wakeup_pin(rtc_gpio_num, s_config.ext0_trigger_level);
  875. rtcio_hal_function_select(rtc_gpio_num, RTCIO_FUNC_RTC);
  876. rtcio_hal_input_enable(rtc_gpio_num);
  877. }
  878. esp_err_t esp_sleep_enable_ext1_wakeup(uint64_t mask, esp_sleep_ext1_wakeup_mode_t mode)
  879. {
  880. if (mode > ESP_EXT1_WAKEUP_ANY_HIGH) {
  881. return ESP_ERR_INVALID_ARG;
  882. }
  883. // Translate bit map of GPIO numbers into the bit map of RTC IO numbers
  884. uint32_t rtc_gpio_mask = 0;
  885. for (int gpio = 0; mask; ++gpio, mask >>= 1) {
  886. if ((mask & 1) == 0) {
  887. continue;
  888. }
  889. if (!esp_sleep_is_valid_wakeup_gpio(gpio)) {
  890. ESP_LOGE(TAG, "Not an RTC IO: GPIO%d", gpio);
  891. return ESP_ERR_INVALID_ARG;
  892. }
  893. rtc_gpio_mask |= BIT(rtc_io_number_get(gpio));
  894. }
  895. s_config.ext1_rtc_gpio_mask = rtc_gpio_mask;
  896. s_config.ext1_trigger_mode = mode;
  897. s_config.wakeup_triggers |= RTC_EXT1_TRIG_EN;
  898. return ESP_OK;
  899. }
  900. static void ext1_wakeup_prepare(void)
  901. {
  902. // Configure all RTC IOs selected as ext1 wakeup inputs
  903. uint32_t rtc_gpio_mask = s_config.ext1_rtc_gpio_mask;
  904. for (int gpio = 0; gpio < GPIO_PIN_COUNT && rtc_gpio_mask != 0; ++gpio) {
  905. int rtc_pin = rtc_io_number_get(gpio);
  906. if ((rtc_gpio_mask & BIT(rtc_pin)) == 0) {
  907. continue;
  908. }
  909. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  910. // Route pad to RTC
  911. rtcio_hal_function_select(rtc_pin, RTCIO_FUNC_RTC);
  912. // set input enable in sleep mode
  913. rtcio_hal_input_enable(rtc_pin);
  914. #endif
  915. #if SOC_PM_SUPPORT_RTC_PERIPH_PD
  916. // Pad configuration depends on RTC_PERIPH state in sleep mode
  917. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] != ESP_PD_OPTION_ON) {
  918. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  919. // RTC_PERIPH will be powered down, so RTC_IO_ registers will
  920. // loose their state. Lock pad configuration.
  921. // Pullups/pulldowns also need to be disabled.
  922. rtcio_hal_pullup_disable(rtc_pin);
  923. rtcio_hal_pulldown_disable(rtc_pin);
  924. #endif
  925. rtcio_hal_hold_enable(rtc_pin);
  926. }
  927. #endif
  928. // Keep track of pins which are processed to bail out early
  929. rtc_gpio_mask &= ~BIT(rtc_pin);
  930. }
  931. // Clear state from previous wakeup
  932. rtc_hal_ext1_clear_wakeup_pins();
  933. // Set RTC IO pins and mode (any high, all low) to be used for wakeup
  934. rtc_hal_ext1_set_wakeup_pins(s_config.ext1_rtc_gpio_mask, s_config.ext1_trigger_mode);
  935. }
  936. uint64_t esp_sleep_get_ext1_wakeup_status(void)
  937. {
  938. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_EXT1) {
  939. return 0;
  940. }
  941. uint32_t status = rtc_hal_ext1_get_wakeup_pins();
  942. // Translate bit map of RTC IO numbers into the bit map of GPIO numbers
  943. uint64_t gpio_mask = 0;
  944. for (int gpio = 0; gpio < GPIO_PIN_COUNT; ++gpio) {
  945. if (!esp_sleep_is_valid_wakeup_gpio(gpio)) {
  946. continue;
  947. }
  948. int rtc_pin = rtc_io_number_get(gpio);
  949. if ((status & BIT(rtc_pin)) == 0) {
  950. continue;
  951. }
  952. gpio_mask |= 1ULL << gpio;
  953. }
  954. return gpio_mask;
  955. }
  956. #endif // SOC_PM_SUPPORT_EXT_WAKEUP
  957. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  958. uint64_t esp_sleep_get_gpio_wakeup_status(void)
  959. {
  960. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_GPIO) {
  961. return 0;
  962. }
  963. return rtc_hal_gpio_get_wakeup_pins();
  964. }
  965. static void esp_deep_sleep_wakeup_prepare(void)
  966. {
  967. for (gpio_num_t gpio_idx = GPIO_NUM_0; gpio_idx < GPIO_NUM_MAX; gpio_idx++) {
  968. if (((1ULL << gpio_idx) & s_config.gpio_wakeup_mask) == 0) {
  969. continue;
  970. }
  971. if (s_config.gpio_trigger_mode & BIT(gpio_idx)) {
  972. ESP_ERROR_CHECK(gpio_pullup_dis(gpio_idx));
  973. ESP_ERROR_CHECK(gpio_pulldown_en(gpio_idx));
  974. } else {
  975. ESP_ERROR_CHECK(gpio_pullup_en(gpio_idx));
  976. ESP_ERROR_CHECK(gpio_pulldown_dis(gpio_idx));
  977. }
  978. rtc_hal_gpio_set_wakeup_pins();
  979. ESP_ERROR_CHECK(gpio_hold_en(gpio_idx));
  980. }
  981. }
  982. esp_err_t esp_deep_sleep_enable_gpio_wakeup(uint64_t gpio_pin_mask, esp_deepsleep_gpio_wake_up_mode_t mode)
  983. {
  984. if (mode > ESP_GPIO_WAKEUP_GPIO_HIGH) {
  985. ESP_LOGE(TAG, "invalid mode");
  986. return ESP_ERR_INVALID_ARG;
  987. }
  988. gpio_int_type_t intr_type = ((mode == ESP_GPIO_WAKEUP_GPIO_LOW) ? GPIO_INTR_LOW_LEVEL : GPIO_INTR_HIGH_LEVEL);
  989. esp_err_t err = ESP_OK;
  990. for (gpio_num_t gpio_idx = GPIO_NUM_0; gpio_idx < GPIO_NUM_MAX; gpio_idx++, gpio_pin_mask >>= 1) {
  991. if ((gpio_pin_mask & 1) == 0) {
  992. continue;
  993. }
  994. if (!esp_sleep_is_valid_wakeup_gpio(gpio_idx)) {
  995. ESP_LOGE(TAG, "invalid mask, please ensure gpio number is no more than 5");
  996. return ESP_ERR_INVALID_ARG;
  997. }
  998. err = gpio_deep_sleep_wakeup_enable(gpio_idx, intr_type);
  999. s_config.gpio_wakeup_mask |= BIT(gpio_idx);
  1000. if (mode == ESP_GPIO_WAKEUP_GPIO_HIGH) {
  1001. s_config.gpio_trigger_mode |= (mode << gpio_idx);
  1002. } else {
  1003. s_config.gpio_trigger_mode &= ~(mode << gpio_idx);
  1004. }
  1005. }
  1006. s_config.wakeup_triggers |= RTC_GPIO_TRIG_EN;
  1007. rtc_hal_gpio_clear_wakeup_pins();
  1008. return err;
  1009. }
  1010. #endif //SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  1011. esp_err_t esp_sleep_enable_gpio_wakeup(void)
  1012. {
  1013. #if CONFIG_IDF_TARGET_ESP32
  1014. if (s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) {
  1015. ESP_LOGE(TAG, "Conflicting wake-up triggers: touch / ULP");
  1016. return ESP_ERR_INVALID_STATE;
  1017. }
  1018. #endif
  1019. s_config.wakeup_triggers |= RTC_GPIO_TRIG_EN;
  1020. return ESP_OK;
  1021. }
  1022. esp_err_t esp_sleep_enable_uart_wakeup(int uart_num)
  1023. {
  1024. if (uart_num == UART_NUM_0) {
  1025. s_config.wakeup_triggers |= RTC_UART0_TRIG_EN;
  1026. } else if (uart_num == UART_NUM_1) {
  1027. s_config.wakeup_triggers |= RTC_UART1_TRIG_EN;
  1028. } else {
  1029. return ESP_ERR_INVALID_ARG;
  1030. }
  1031. return ESP_OK;
  1032. }
  1033. esp_err_t esp_sleep_enable_wifi_wakeup(void)
  1034. {
  1035. #if SOC_PM_SUPPORT_WIFI_WAKEUP
  1036. s_config.wakeup_triggers |= RTC_WIFI_TRIG_EN;
  1037. return ESP_OK;
  1038. #else
  1039. return ESP_ERR_NOT_SUPPORTED;
  1040. #endif
  1041. }
  1042. esp_err_t esp_sleep_disable_wifi_wakeup(void)
  1043. {
  1044. #if SOC_PM_SUPPORT_WIFI_WAKEUP
  1045. s_config.wakeup_triggers &= (~RTC_WIFI_TRIG_EN);
  1046. return ESP_OK;
  1047. #else
  1048. return ESP_ERR_NOT_SUPPORTED;
  1049. #endif
  1050. }
  1051. esp_err_t esp_sleep_enable_bt_wakeup(void)
  1052. {
  1053. #if SOC_PM_SUPPORT_BT_WAKEUP
  1054. s_config.wakeup_triggers |= RTC_BT_TRIG_EN;
  1055. return ESP_OK;
  1056. #else
  1057. return ESP_ERR_NOT_SUPPORTED;
  1058. #endif
  1059. }
  1060. esp_err_t esp_sleep_disable_bt_wakeup(void)
  1061. {
  1062. #if SOC_PM_SUPPORT_BT_WAKEUP
  1063. s_config.wakeup_triggers &= (~RTC_BT_TRIG_EN);
  1064. return ESP_OK;
  1065. #else
  1066. return ESP_ERR_NOT_SUPPORTED;
  1067. #endif
  1068. }
  1069. esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause(void)
  1070. {
  1071. if (esp_rom_get_reset_reason(0) != RESET_REASON_CORE_DEEP_SLEEP && !s_light_sleep_wakeup) {
  1072. return ESP_SLEEP_WAKEUP_UNDEFINED;
  1073. }
  1074. #ifdef CONFIG_IDF_TARGET_ESP32
  1075. uint32_t wakeup_cause = REG_GET_FIELD(RTC_CNTL_WAKEUP_STATE_REG, RTC_CNTL_WAKEUP_CAUSE);
  1076. #else
  1077. uint32_t wakeup_cause = REG_GET_FIELD(RTC_CNTL_SLP_WAKEUP_CAUSE_REG, RTC_CNTL_WAKEUP_CAUSE);
  1078. #endif
  1079. if (wakeup_cause & RTC_TIMER_TRIG_EN) {
  1080. return ESP_SLEEP_WAKEUP_TIMER;
  1081. } else if (wakeup_cause & RTC_GPIO_TRIG_EN) {
  1082. return ESP_SLEEP_WAKEUP_GPIO;
  1083. } else if (wakeup_cause & (RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN)) {
  1084. return ESP_SLEEP_WAKEUP_UART;
  1085. #if SOC_PM_SUPPORT_EXT_WAKEUP
  1086. } else if (wakeup_cause & RTC_EXT0_TRIG_EN) {
  1087. return ESP_SLEEP_WAKEUP_EXT0;
  1088. } else if (wakeup_cause & RTC_EXT1_TRIG_EN) {
  1089. return ESP_SLEEP_WAKEUP_EXT1;
  1090. #endif
  1091. #if SOC_PM_SUPPORT_TOUCH_SENSOR_WAKEUP
  1092. } else if (wakeup_cause & RTC_TOUCH_TRIG_EN) {
  1093. return ESP_SLEEP_WAKEUP_TOUCHPAD;
  1094. #endif
  1095. #if SOC_ULP_SUPPORTED
  1096. } else if (wakeup_cause & RTC_ULP_TRIG_EN) {
  1097. return ESP_SLEEP_WAKEUP_ULP;
  1098. #endif
  1099. #if SOC_PM_SUPPORT_WIFI_WAKEUP
  1100. } else if (wakeup_cause & RTC_WIFI_TRIG_EN) {
  1101. return ESP_SLEEP_WAKEUP_WIFI;
  1102. #endif
  1103. #if SOC_PM_SUPPORT_BT_WAKEUP
  1104. } else if (wakeup_cause & RTC_BT_TRIG_EN) {
  1105. return ESP_SLEEP_WAKEUP_BT;
  1106. #endif
  1107. #if SOC_RISCV_COPROC_SUPPORTED
  1108. } else if (wakeup_cause & RTC_COCPU_TRIG_EN) {
  1109. return ESP_SLEEP_WAKEUP_ULP;
  1110. } else if (wakeup_cause & RTC_COCPU_TRAP_TRIG_EN) {
  1111. return ESP_SLEEP_WAKEUP_COCPU_TRAP_TRIG;
  1112. #endif
  1113. } else {
  1114. return ESP_SLEEP_WAKEUP_UNDEFINED;
  1115. }
  1116. }
  1117. esp_err_t esp_sleep_pd_config(esp_sleep_pd_domain_t domain,
  1118. esp_sleep_pd_option_t option)
  1119. {
  1120. if (domain >= ESP_PD_DOMAIN_MAX || option > ESP_PD_OPTION_AUTO) {
  1121. return ESP_ERR_INVALID_ARG;
  1122. }
  1123. s_config.pd_options[domain] = option;
  1124. return ESP_OK;
  1125. }
  1126. static uint32_t get_power_down_flags(void)
  1127. {
  1128. // Where needed, convert AUTO options to ON. Later interpret AUTO as OFF.
  1129. // RTC_SLOW_MEM is needed for the ULP, so keep RTC_SLOW_MEM powered up if ULP
  1130. // is used and RTC_SLOW_MEM is Auto.
  1131. // If there is any data placed into .rtc.data or .rtc.bss segments, and
  1132. // RTC_SLOW_MEM is Auto, keep it powered up as well.
  1133. #if SOC_PM_SUPPORT_RTC_SLOW_MEM_PD && SOC_ULP_SUPPORTED
  1134. // Labels are defined in the linker script
  1135. extern int _rtc_slow_length;
  1136. /**
  1137. * Compiler considers "(size_t) &_rtc_slow_length > 0" to always be true.
  1138. * So use a volatile variable to prevent compiler from doing this optimization.
  1139. */
  1140. volatile size_t rtc_slow_mem_used = (size_t)&_rtc_slow_length;
  1141. if ((s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] == ESP_PD_OPTION_AUTO) &&
  1142. (rtc_slow_mem_used > 0 || (s_config.wakeup_triggers & RTC_ULP_TRIG_EN))) {
  1143. s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] = ESP_PD_OPTION_ON;
  1144. }
  1145. #endif
  1146. #if SOC_PM_SUPPORT_RTC_FAST_MEM_PD
  1147. #if !CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  1148. /* RTC_FAST_MEM is needed for deep sleep stub.
  1149. If RTC_FAST_MEM is Auto, keep it powered on, so that deep sleep stub can run.
  1150. In the new chip revision, deep sleep stub will be optional, and this can be changed. */
  1151. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] == ESP_PD_OPTION_AUTO) {
  1152. s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] = ESP_PD_OPTION_ON;
  1153. }
  1154. #else
  1155. /* If RTC_FAST_MEM is used for heap, force RTC_FAST_MEM to be powered on. */
  1156. s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] = ESP_PD_OPTION_ON;
  1157. #endif
  1158. #endif
  1159. #if SOC_PM_SUPPORT_RTC_PERIPH_PD
  1160. // RTC_PERIPH is needed for EXT0 wakeup and GPIO wakeup.
  1161. // If RTC_PERIPH is auto, and EXT0/GPIO aren't enabled, power down RTC_PERIPH.
  1162. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] == ESP_PD_OPTION_AUTO) {
  1163. #if SOC_PM_SUPPORT_TOUCH_SENSOR_WAKEUP
  1164. uint32_t wakeup_source = RTC_TOUCH_TRIG_EN;
  1165. #if SOC_ULP_SUPPORTED
  1166. wakeup_source |= RTC_ULP_TRIG_EN;
  1167. #endif
  1168. if (s_config.wakeup_triggers & (RTC_EXT0_TRIG_EN | RTC_GPIO_TRIG_EN)) {
  1169. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_ON;
  1170. } else if (s_config.wakeup_triggers & wakeup_source) {
  1171. // In both rev. 0 and rev. 1 of ESP32, forcing power up of RTC_PERIPH
  1172. // prevents ULP timer and touch FSMs from working correctly.
  1173. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_OFF;
  1174. }
  1175. #else
  1176. if (s_config.wakeup_triggers & RTC_GPIO_TRIG_EN) {
  1177. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_ON;
  1178. } else {
  1179. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_OFF;
  1180. }
  1181. #endif // SOC_PM_SUPPORT_TOUCH_SENSOR_WAKEUP
  1182. }
  1183. #endif // SOC_PM_SUPPORT_RTC_PERIPH_PD
  1184. #if SOC_PM_SUPPORT_CPU_PD
  1185. if (!cpu_domain_pd_allowed()) {
  1186. s_config.pd_options[ESP_PD_DOMAIN_CPU] = ESP_PD_OPTION_ON;
  1187. }
  1188. #endif
  1189. #ifdef CONFIG_IDF_TARGET_ESP32
  1190. s_config.pd_options[ESP_PD_DOMAIN_XTAL] = ESP_PD_OPTION_OFF;
  1191. #endif
  1192. const __attribute__((unused)) char *option_str[] = {"OFF", "ON", "AUTO(OFF)" /* Auto works as OFF */};
  1193. /* This function is called from a critical section, log with ESP_EARLY_LOGD. */
  1194. #if SOC_PM_SUPPORT_RTC_PERIPH_PD
  1195. ESP_EARLY_LOGD(TAG, "RTC_PERIPH: %s", option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH]]);
  1196. #endif
  1197. #if SOC_PM_SUPPORT_RTC_SLOW_MEM_PD
  1198. ESP_EARLY_LOGD(TAG, "RTC_SLOW_MEM: %s", option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM]]);
  1199. #endif
  1200. #if SOC_PM_SUPPORT_RTC_FAST_MEM_PD
  1201. ESP_EARLY_LOGD(TAG, "RTC_FAST_MEM: %s", option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM]]);
  1202. #endif
  1203. // Prepare flags based on the selected options
  1204. uint32_t pd_flags = 0;
  1205. #if SOC_PM_SUPPORT_RTC_FAST_MEM_PD
  1206. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] != ESP_PD_OPTION_ON) {
  1207. pd_flags |= RTC_SLEEP_PD_RTC_FAST_MEM;
  1208. }
  1209. #endif
  1210. #if SOC_PM_SUPPORT_RTC_SLOW_MEM_PD
  1211. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] != ESP_PD_OPTION_ON) {
  1212. pd_flags |= RTC_SLEEP_PD_RTC_SLOW_MEM;
  1213. }
  1214. #endif
  1215. #if SOC_PM_SUPPORT_RTC_PERIPH_PD
  1216. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] != ESP_PD_OPTION_ON) {
  1217. pd_flags |= RTC_SLEEP_PD_RTC_PERIPH;
  1218. }
  1219. #endif
  1220. #if SOC_PM_SUPPORT_CPU_PD
  1221. if (s_config.pd_options[ESP_PD_DOMAIN_CPU] != ESP_PD_OPTION_ON) {
  1222. pd_flags |= RTC_SLEEP_PD_CPU;
  1223. }
  1224. #endif
  1225. if (s_config.pd_options[ESP_PD_DOMAIN_RTC8M] != ESP_PD_OPTION_ON) {
  1226. pd_flags |= RTC_SLEEP_PD_INT_8M;
  1227. }
  1228. if (s_config.pd_options[ESP_PD_DOMAIN_XTAL] != ESP_PD_OPTION_ON) {
  1229. pd_flags |= RTC_SLEEP_PD_XTAL;
  1230. }
  1231. /**
  1232. * VDD_SDIO power domain shall be kept on during the light sleep
  1233. * when CONFIG_ESP_SLEEP_POWER_DOWN_FLASH is not set and off when it is set.
  1234. * The application can still force the power domain to remain on by calling
  1235. * `esp_sleep_pd_config` before getting into light sleep mode.
  1236. *
  1237. * In deep sleep mode, the power domain will be turned off, regardless the
  1238. * value of this field.
  1239. */
  1240. if (s_config.pd_options[ESP_PD_DOMAIN_VDDSDIO] == ESP_PD_OPTION_AUTO) {
  1241. #ifndef CONFIG_ESP_SLEEP_POWER_DOWN_FLASH
  1242. s_config.pd_options[ESP_PD_DOMAIN_VDDSDIO] = ESP_PD_OPTION_ON;
  1243. #endif
  1244. }
  1245. if (s_config.pd_options[ESP_PD_DOMAIN_VDDSDIO] != ESP_PD_OPTION_ON) {
  1246. pd_flags |= RTC_SLEEP_PD_VDDSDIO;
  1247. }
  1248. #if ((defined CONFIG_RTC_CLK_SRC_EXT_CRYS) && (defined CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT) && (SOC_PM_SUPPORT_RTC_PERIPH_PD))
  1249. if ((s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) == 0) {
  1250. // If enabled EXT1 only and enable the additional current by touch, should be keep RTC_PERIPH power on.
  1251. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  1252. }
  1253. #endif
  1254. return pd_flags;
  1255. }
  1256. void esp_deep_sleep_disable_rom_logging(void)
  1257. {
  1258. rtc_suppress_rom_log();
  1259. }
  1260. void esp_sleep_enable_adc_tsens_monitor(bool enable)
  1261. {
  1262. s_adc_tsen_enabled = enable;
  1263. }
  1264. void rtc_sleep_enable_ultra_low(bool enable)
  1265. {
  1266. s_ultra_low_enabled = enable;
  1267. }
  1268. #if CONFIG_ESP_SLEEP_GPIO_RESET_WORKAROUND && !CONFIG_PM_SLP_DISABLE_GPIO
  1269. ESP_SYSTEM_INIT_FN(esp_sleep_startup_init, BIT(0), 105)
  1270. {
  1271. // Configure to isolate (disable the Input/Output/Pullup/Pulldown
  1272. // function of the pin) all GPIO pins in sleep state
  1273. esp_sleep_config_gpio_isolate();
  1274. // Enable automatic switching of GPIO configuration
  1275. esp_sleep_enable_gpio_switch(true);
  1276. return ESP_OK;
  1277. }
  1278. #endif