sleep_modes.c 46 KB

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