sleep_modes.c 45 KB

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