sleep_modes.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stddef.h>
  7. #include <string.h>
  8. #include <sys/lock.h>
  9. #include <sys/param.h>
  10. #include "esp_attr.h"
  11. #include "esp_memory_utils.h"
  12. #include "esp_sleep.h"
  13. #include "esp_private/esp_sleep_internal.h"
  14. #include "esp_private/esp_timer_private.h"
  15. #include "esp_private/system_internal.h"
  16. #include "esp_log.h"
  17. #include "esp_newlib.h"
  18. #include "esp_timer.h"
  19. #include "esp_ipc_isr.h"
  20. #include "freertos/FreeRTOS.h"
  21. #include "freertos/task.h"
  22. #include "soc/soc_caps.h"
  23. #include "driver/rtc_io.h"
  24. #include "hal/rtc_io_hal.h"
  25. #if SOC_PM_SUPPORT_PMU_MODEM_STATE
  26. #include "esp_private/pm_impl.h"
  27. #endif
  28. #if SOC_LP_AON_SUPPORTED
  29. #include "hal/lp_aon_hal.h"
  30. #else
  31. #include "hal/rtc_cntl_ll.h"
  32. #include "hal/rtc_hal.h"
  33. #endif
  34. #include "driver/uart.h"
  35. #include "soc/rtc.h"
  36. #include "soc/soc_caps.h"
  37. #include "regi2c_ctrl.h" //For `REGI2C_ANA_CALI_PD_WORKAROUND`, temp
  38. #include "hal/cache_hal.h"
  39. #include "hal/wdt_hal.h"
  40. #include "hal/uart_hal.h"
  41. #if SOC_TOUCH_SENSOR_SUPPORTED
  42. #include "hal/touch_sensor_hal.h"
  43. #include "driver/touch_sensor.h"
  44. #include "driver/touch_sensor_common.h"
  45. #endif
  46. #include "hal/clk_gate_ll.h"
  47. #include "sdkconfig.h"
  48. #include "esp_rom_uart.h"
  49. #include "esp_rom_sys.h"
  50. #include "esp_private/brownout.h"
  51. #include "esp_private/sleep_cpu.h"
  52. #include "esp_private/sleep_modem.h"
  53. #include "esp_private/esp_clk.h"
  54. #include "esp_private/esp_task_wdt.h"
  55. #include "esp_private/sar_periph_ctrl.h"
  56. #include "esp_private/mspi_timing_tuning.h"
  57. #ifdef CONFIG_IDF_TARGET_ESP32
  58. #include "esp32/rom/cache.h"
  59. #include "esp32/rom/rtc.h"
  60. #include "esp_private/gpio.h"
  61. #include "esp_private/sleep_gpio.h"
  62. #elif CONFIG_IDF_TARGET_ESP32S2
  63. #include "esp32s2/rom/rtc.h"
  64. #include "soc/extmem_reg.h"
  65. #include "esp_private/gpio.h"
  66. #elif CONFIG_IDF_TARGET_ESP32S3
  67. #include "esp32s3/rom/rtc.h"
  68. #include "esp_private/mspi_timing_tuning.h"
  69. #elif CONFIG_IDF_TARGET_ESP32C3
  70. #include "esp32c3/rom/rtc.h"
  71. #elif CONFIG_IDF_TARGET_ESP32C2
  72. #include "esp32c2/rom/rtc.h"
  73. #elif CONFIG_IDF_TARGET_ESP32C6
  74. #include "esp32c6/rom/rtc.h"
  75. #include "hal/lp_timer_hal.h"
  76. #include "hal/gpio_ll.h"
  77. #elif CONFIG_IDF_TARGET_ESP32H2
  78. #include "esp32h2/rom/rtc.h"
  79. #include "esp32h2/rom/cache.h"
  80. #include "esp32h2/rom/rtc.h"
  81. #include "soc/extmem_reg.h"
  82. #include "hal/gpio_ll.h"
  83. #endif
  84. #if SOC_LP_TIMER_SUPPORTED
  85. #include "hal/lp_timer_hal.h"
  86. #endif
  87. #if SOC_PMU_SUPPORTED
  88. #include "esp_private/esp_pmu.h"
  89. #include "esp_private/sleep_sys_periph.h"
  90. #include "esp_private/sleep_clock.h"
  91. #endif
  92. #if SOC_PM_RETENTION_HAS_REGDMA_POWER_BUG
  93. #include "esp_private/sleep_retention.h"
  94. #endif
  95. // If light sleep time is less than that, don't power down flash
  96. #define FLASH_PD_MIN_SLEEP_TIME_US 2000
  97. // Time from VDD_SDIO power up to first flash read in ROM code
  98. #define VDD_SDIO_POWERUP_TO_FLASH_READ_US 700
  99. // Cycles for RTC Timer clock source (internal oscillator) calibrate
  100. #define RTC_CLK_SRC_CAL_CYCLES (10)
  101. #define FAST_CLK_SRC_CAL_CYCLES (2048) /* ~ 127.4 us */
  102. #ifdef CONFIG_IDF_TARGET_ESP32
  103. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (212)
  104. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (60)
  105. #elif CONFIG_IDF_TARGET_ESP32S2
  106. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (147)
  107. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (28)
  108. #elif CONFIG_IDF_TARGET_ESP32S3
  109. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (382)
  110. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (133)
  111. #elif CONFIG_IDF_TARGET_ESP32C3
  112. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (105)
  113. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (37)
  114. #elif CONFIG_IDF_TARGET_ESP32C2
  115. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (118)
  116. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (9)
  117. #elif CONFIG_IDF_TARGET_ESP32C6
  118. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (318)
  119. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (56)
  120. #elif CONFIG_IDF_TARGET_ESP32H2
  121. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (118)// TODO: IDF-6267
  122. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (9)
  123. #endif
  124. // Actually costs 80us, using the fastest slow clock 150K calculation takes about 16 ticks
  125. #define SLEEP_TIMER_ALARM_TO_SLEEP_TICKS (16)
  126. #define SLEEP_UART_FLUSH_DONE_TO_SLEEP_US (450)
  127. #if SOC_PM_SUPPORT_TOP_PD
  128. // IDF console uses 8 bits data mode without parity, so each char occupy 8(data)+1(start)+1(stop)=10bits
  129. #define UART_FLUSH_US_PER_CHAR (10*1000*1000 / CONFIG_ESP_CONSOLE_UART_BAUDRATE)
  130. #define CONCATENATE_HELPER(x, y) (x##y)
  131. #define CONCATENATE(x, y) CONCATENATE_HELPER(x, y)
  132. #define CONSOLE_UART_DEV (&CONCATENATE(UART, CONFIG_ESP_CONSOLE_UART_NUM))
  133. #endif
  134. #define LIGHT_SLEEP_TIME_OVERHEAD_US DEFAULT_HARDWARE_OUT_OVERHEAD_US
  135. #ifdef CONFIG_ESP_SYSTEM_RTC_EXT_XTAL
  136. #define DEEP_SLEEP_TIME_OVERHEAD_US (650 + 100 * 240 / CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ)
  137. #else
  138. #define DEEP_SLEEP_TIME_OVERHEAD_US (250 + 100 * 240 / CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ)
  139. #endif
  140. #if CONFIG_ESP_SLEEP_DEEP_SLEEP_WAKEUP_DELAY
  141. #define DEEP_SLEEP_WAKEUP_DELAY CONFIG_ESP_SLEEP_DEEP_SLEEP_WAKEUP_DELAY
  142. #else
  143. #define DEEP_SLEEP_WAKEUP_DELAY 0
  144. #endif
  145. // Minimal amount of time we can sleep for
  146. #define LIGHT_SLEEP_MIN_TIME_US 200
  147. #define RTC_MODULE_SLEEP_PREPARE_CYCLES (6)
  148. #define CHECK_SOURCE(source, value, mask) ((s_config.wakeup_triggers & mask) && \
  149. (source == value))
  150. #define MAX_DSLP_HOOKS 3
  151. static esp_deep_sleep_cb_t s_dslp_cb[MAX_DSLP_HOOKS]={0};
  152. /**
  153. * Internal structure which holds all requested sleep parameters
  154. */
  155. typedef struct {
  156. struct {
  157. esp_sleep_pd_option_t pd_option;
  158. int16_t refs;
  159. uint16_t reserved; /* reserved for 4 bytes aligned */
  160. } domain[ESP_PD_DOMAIN_MAX];
  161. portMUX_TYPE lock;
  162. uint64_t sleep_duration;
  163. uint32_t wakeup_triggers : 15;
  164. #if SOC_PM_SUPPORT_EXT1_WAKEUP
  165. uint32_t ext1_trigger_mode : 22; // 22 is the maximum RTCIO number in all chips
  166. uint32_t ext1_rtc_gpio_mask : 22;
  167. #endif
  168. #if SOC_PM_SUPPORT_EXT0_WAKEUP
  169. uint32_t ext0_trigger_level : 1;
  170. uint32_t ext0_rtc_gpio_num : 5;
  171. #endif
  172. uint32_t gpio_wakeup_mask : 8; // 8 is the maximum RTCIO number in all chips that support GPIO wakeup
  173. uint32_t gpio_trigger_mode : 8;
  174. uint32_t sleep_time_adjustment;
  175. uint32_t ccount_ticks_record;
  176. uint32_t sleep_time_overhead_out;
  177. uint32_t rtc_clk_cal_period;
  178. uint32_t fast_clk_cal_period;
  179. uint64_t rtc_ticks_at_sleep_start;
  180. } sleep_config_t;
  181. static uint32_t s_lightsleep_cnt = 0;
  182. _Static_assert(22 >= SOC_RTCIO_PIN_COUNT, "Chip has more RTCIOs than 22, should increase ext1_rtc_gpio_mask field size");
  183. static sleep_config_t s_config = {
  184. .domain = {
  185. [0 ... ESP_PD_DOMAIN_MAX - 1] = {
  186. .pd_option = ESP_PD_OPTION_AUTO,
  187. .refs = 0
  188. }
  189. },
  190. .lock = portMUX_INITIALIZER_UNLOCKED,
  191. .ccount_ticks_record = 0,
  192. .sleep_time_overhead_out = DEFAULT_SLEEP_OUT_OVERHEAD_US,
  193. .wakeup_triggers = 0
  194. };
  195. /* Internal variable used to track if light sleep wakeup sources are to be
  196. expected when determining wakeup cause. */
  197. static bool s_light_sleep_wakeup = false;
  198. /* Updating RTC_MEMORY_CRC_REG register via set_rtc_memory_crc()
  199. is not thread-safe, so we need to disable interrupts before going to deep sleep. */
  200. static portMUX_TYPE spinlock_rtc_deep_sleep = portMUX_INITIALIZER_UNLOCKED;
  201. static const char *TAG = "sleep";
  202. static RTC_FAST_ATTR bool s_adc_tsen_enabled = false;
  203. //in this mode, 2uA is saved, but RTC memory can't use at high temperature, and RTCIO can't be used as INPUT.
  204. static bool s_ultra_low_enabled = false;
  205. static bool s_periph_use_8m_flag = false;
  206. void esp_sleep_periph_use_8m(bool use_or_not)
  207. {
  208. s_periph_use_8m_flag = use_or_not;
  209. }
  210. static uint32_t get_power_down_flags(void);
  211. #if SOC_PM_SUPPORT_EXT0_WAKEUP
  212. static void ext0_wakeup_prepare(void);
  213. #endif
  214. #if SOC_PM_SUPPORT_EXT1_WAKEUP
  215. static void ext1_wakeup_prepare(void);
  216. #endif
  217. static esp_err_t timer_wakeup_prepare(int64_t sleep_duration);
  218. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  219. static void touch_wakeup_prepare(void);
  220. #endif
  221. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  222. static void gpio_deep_sleep_wakeup_prepare(void);
  223. #endif
  224. #if SOC_RTC_FAST_MEM_SUPPORTED
  225. #if SOC_PM_SUPPORT_DEEPSLEEP_CHECK_STUB_ONLY
  226. static RTC_FAST_ATTR esp_deep_sleep_wake_stub_fn_t wake_stub_fn_handler = NULL;
  227. static void RTC_IRAM_ATTR __attribute__((used, noinline)) esp_wake_stub_start(void)
  228. {
  229. if (wake_stub_fn_handler) {
  230. (*wake_stub_fn_handler)();
  231. }
  232. }
  233. /* We must have a default deep sleep wake stub entry function, which must be
  234. * located at the start address of the RTC fast memory, and its implementation
  235. * must be simple enough to ensure that there is no litteral data before the
  236. * wake stub entry, otherwise, the litteral data before the wake stub entry
  237. * will not be CRC checked. */
  238. static void __attribute__((section(".rtc.entry.text"))) esp_wake_stub_entry(void)
  239. {
  240. #define _SYM2STR(s) # s
  241. #define SYM2STR(s) _SYM2STR(s)
  242. #ifdef __riscv
  243. __asm__ __volatile__ (
  244. "addi sp, sp, -16 \n"
  245. "sw ra, 0(sp) \n"
  246. "jal ra, " SYM2STR(esp_wake_stub_start) "\n"
  247. "lw ra, 0(sp) \n"
  248. "addi sp, sp, 16 \n"
  249. );
  250. #else
  251. // call4 has a larger effective addressing range (-524284 to 524288 bytes),
  252. // which is sufficient for instruction addressing in RTC fast memory.
  253. __asm__ __volatile__ ("call4 " SYM2STR(esp_wake_stub_start) "\n");
  254. #endif
  255. }
  256. void RTC_IRAM_ATTR esp_set_deep_sleep_wake_stub_default_entry(void)
  257. {
  258. extern char _rtc_text_start[];
  259. #if CONFIG_ESP32S3_RTCDATA_IN_FAST_MEM
  260. extern char _rtc_noinit_end[];
  261. size_t rtc_fast_length = (size_t)_rtc_noinit_end - (size_t)_rtc_text_start;
  262. #else
  263. extern char _rtc_force_fast_end[];
  264. size_t rtc_fast_length = (size_t)_rtc_force_fast_end - (size_t)_rtc_text_start;
  265. #endif
  266. esp_rom_set_rtc_wake_addr((esp_rom_wake_func_t)esp_wake_stub_entry, rtc_fast_length);
  267. }
  268. #endif // SOC_PM_SUPPORT_DEEPSLEEP_CHECK_STUB_ONLY
  269. /* Wake from deep sleep stub
  270. See esp_deepsleep.h esp_wake_deep_sleep() comments for details.
  271. */
  272. esp_deep_sleep_wake_stub_fn_t esp_get_deep_sleep_wake_stub(void)
  273. {
  274. #if SOC_PM_SUPPORT_DEEPSLEEP_CHECK_STUB_ONLY
  275. esp_deep_sleep_wake_stub_fn_t stub_ptr = wake_stub_fn_handler;
  276. #else
  277. esp_deep_sleep_wake_stub_fn_t stub_ptr = (esp_deep_sleep_wake_stub_fn_t) REG_READ(RTC_ENTRY_ADDR_REG);
  278. #endif
  279. if (!esp_ptr_executable(stub_ptr)) {
  280. return NULL;
  281. }
  282. return stub_ptr;
  283. }
  284. #if CONFIG_IDF_TARGET_ESP32
  285. /* APP core of esp32 can't access to RTC FAST MEMORY, do not define it with RTC_IRAM_ATTR */
  286. void
  287. #else
  288. void RTC_IRAM_ATTR
  289. #endif
  290. esp_set_deep_sleep_wake_stub(esp_deep_sleep_wake_stub_fn_t new_stub)
  291. {
  292. #if SOC_PM_SUPPORT_DEEPSLEEP_CHECK_STUB_ONLY
  293. wake_stub_fn_handler = new_stub;
  294. #else
  295. REG_WRITE(RTC_ENTRY_ADDR_REG, (uint32_t)new_stub);
  296. #endif
  297. }
  298. void RTC_IRAM_ATTR esp_default_wake_deep_sleep(void)
  299. {
  300. /* Clear MMU for CPU 0 */
  301. #if CONFIG_IDF_TARGET_ESP32
  302. _DPORT_REG_WRITE(DPORT_PRO_CACHE_CTRL1_REG,
  303. _DPORT_REG_READ(DPORT_PRO_CACHE_CTRL1_REG) | DPORT_PRO_CACHE_MMU_IA_CLR);
  304. _DPORT_REG_WRITE(DPORT_PRO_CACHE_CTRL1_REG,
  305. _DPORT_REG_READ(DPORT_PRO_CACHE_CTRL1_REG) & (~DPORT_PRO_CACHE_MMU_IA_CLR));
  306. #if DEEP_SLEEP_WAKEUP_DELAY > 0
  307. // ROM code has not started yet, so we need to set delay factor
  308. // used by esp_rom_delay_us first.
  309. ets_update_cpu_frequency_rom(ets_get_detected_xtal_freq() / 1000000);
  310. // This delay is configured in menuconfig, it can be used to give
  311. // the flash chip some time to become ready.
  312. esp_rom_delay_us(DEEP_SLEEP_WAKEUP_DELAY);
  313. #endif
  314. #elif CONFIG_IDF_TARGET_ESP32S2
  315. REG_SET_BIT(EXTMEM_CACHE_DBG_INT_ENA_REG, EXTMEM_CACHE_DBG_EN);
  316. #endif
  317. }
  318. void __attribute__((weak, alias("esp_default_wake_deep_sleep"))) esp_wake_deep_sleep(void);
  319. #endif // SOC_RTC_FAST_MEM_SUPPORTED
  320. void esp_deep_sleep(uint64_t time_in_us)
  321. {
  322. esp_sleep_enable_timer_wakeup(time_in_us);
  323. esp_deep_sleep_start();
  324. }
  325. esp_err_t esp_deep_sleep_register_hook(esp_deep_sleep_cb_t new_dslp_cb)
  326. {
  327. portENTER_CRITICAL(&spinlock_rtc_deep_sleep);
  328. for(int n = 0; n < MAX_DSLP_HOOKS; n++){
  329. if (s_dslp_cb[n]==NULL || s_dslp_cb[n]==new_dslp_cb) {
  330. s_dslp_cb[n]=new_dslp_cb;
  331. portEXIT_CRITICAL(&spinlock_rtc_deep_sleep);
  332. return ESP_OK;
  333. }
  334. }
  335. portEXIT_CRITICAL(&spinlock_rtc_deep_sleep);
  336. ESP_LOGE(TAG, "Registered deepsleep callbacks exceeds MAX_DSLP_HOOKS");
  337. return ESP_ERR_NO_MEM;
  338. }
  339. void esp_deep_sleep_deregister_hook(esp_deep_sleep_cb_t old_dslp_cb)
  340. {
  341. portENTER_CRITICAL(&spinlock_rtc_deep_sleep);
  342. for(int n = 0; n < MAX_DSLP_HOOKS; n++){
  343. if(s_dslp_cb[n] == old_dslp_cb) {
  344. s_dslp_cb[n] = NULL;
  345. }
  346. }
  347. portEXIT_CRITICAL(&spinlock_rtc_deep_sleep);
  348. }
  349. static int s_cache_suspend_cnt = 0;
  350. // Must be called from critical sections.
  351. static void IRAM_ATTR suspend_cache(void) {
  352. s_cache_suspend_cnt++;
  353. if (s_cache_suspend_cnt == 1) {
  354. cache_hal_suspend(CACHE_TYPE_ALL);
  355. }
  356. }
  357. // Must be called from critical sections.
  358. static void IRAM_ATTR resume_cache(void) {
  359. s_cache_suspend_cnt--;
  360. assert(s_cache_suspend_cnt >= 0 && DRAM_STR("cache resume doesn't match suspend ops"));
  361. if (s_cache_suspend_cnt == 0) {
  362. cache_hal_resume(CACHE_TYPE_ALL);
  363. }
  364. }
  365. // [refactor-todo] provide target logic for body of uart functions below
  366. static void IRAM_ATTR flush_uarts(void)
  367. {
  368. for (int i = 0; i < SOC_UART_HP_NUM; ++i) {
  369. #ifdef CONFIG_IDF_TARGET_ESP32
  370. esp_rom_uart_tx_wait_idle(i);
  371. #else
  372. if (periph_ll_uart_enabled(i)) {
  373. esp_rom_uart_tx_wait_idle(i);
  374. }
  375. #endif
  376. }
  377. }
  378. static uint32_t s_suspended_uarts_bmap = 0;
  379. /**
  380. * Suspend enabled uarts and return suspended uarts bit map.
  381. * Must be called from critical sections.
  382. */
  383. FORCE_INLINE_ATTR void suspend_uarts(void)
  384. {
  385. s_suspended_uarts_bmap = 0;
  386. for (int i = 0; i < SOC_UART_HP_NUM; ++i) {
  387. #ifndef CONFIG_IDF_TARGET_ESP32
  388. if (!periph_ll_uart_enabled(i)) {
  389. continue;
  390. }
  391. #endif
  392. uart_ll_force_xoff(i);
  393. s_suspended_uarts_bmap |= BIT(i);
  394. #if SOC_UART_SUPPORT_FSM_TX_WAIT_SEND
  395. uint32_t uart_fsm = 0;
  396. do {
  397. uart_fsm = uart_ll_get_fsm_status(i);
  398. } while (!(uart_fsm == UART_LL_FSM_IDLE || uart_fsm == UART_LL_FSM_TX_WAIT_SEND));
  399. #else
  400. while (uart_ll_get_fsm_status(i) != 0) {}
  401. #endif
  402. }
  403. }
  404. // Must be called from critical sections
  405. FORCE_INLINE_ATTR void resume_uarts(void)
  406. {
  407. for (int i = 0; i < SOC_UART_HP_NUM; ++i) {
  408. if (s_suspended_uarts_bmap & 0x1) {
  409. uart_ll_force_xon(i);
  410. }
  411. s_suspended_uarts_bmap >>= 1;
  412. }
  413. }
  414. /*
  415. UART prepare strategy in sleep:
  416. Deepsleep : flush the fifo before enter sleep to avoid data loss
  417. Lightsleep:
  418. Chips not support PD_TOP: Suspend uart before cpu freq switch
  419. Chips support PD_TOP:
  420. For sleep which will not power down the TOP domain (uart belongs it), we can just suspend the UART.
  421. For sleep which will power down the TOP domain, we need to consider whether the uart flushing will
  422. block the sleep process and cause the rtos target tick to be missed upon waking up. It's need to
  423. estimate the flush time based on the number of bytes in the uart FIFO, if the predicted flush
  424. completion time has exceeded the wakeup time, we should abandon the flush, skip the sleep and
  425. return ESP_ERR_SLEEP_REJECT.
  426. */
  427. FORCE_INLINE_ATTR bool light_sleep_uart_prepare(uint32_t pd_flags, int64_t sleep_duration)
  428. {
  429. bool should_skip_sleep = false;
  430. #if !SOC_PM_SUPPORT_TOP_PD
  431. suspend_uarts();
  432. #else
  433. if (pd_flags & PMU_SLEEP_PD_TOP) {
  434. if ((s_config.wakeup_triggers & RTC_TIMER_TRIG_EN) &&
  435. // +1 is for cover the last charactor flush time
  436. (sleep_duration < (int64_t)((UART_LL_FIFO_DEF_LEN - uart_ll_get_txfifo_len(CONSOLE_UART_DEV) + 1) * UART_FLUSH_US_PER_CHAR) + SLEEP_UART_FLUSH_DONE_TO_SLEEP_US)) {
  437. should_skip_sleep = true;
  438. } else {
  439. /* Only flush the uart_num configured to console, the transmission integrity of
  440. other uarts is guaranteed by the UART driver */
  441. esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  442. }
  443. } else {
  444. suspend_uarts();
  445. }
  446. #endif
  447. return should_skip_sleep;
  448. }
  449. /**
  450. * These save-restore workaround should be moved to lower layer
  451. */
  452. FORCE_INLINE_ATTR void misc_modules_sleep_prepare(bool deep_sleep)
  453. {
  454. if (deep_sleep){
  455. for (int n = 0; n < MAX_DSLP_HOOKS; n++) {
  456. if (s_dslp_cb[n] != NULL) {
  457. s_dslp_cb[n]();
  458. }
  459. }
  460. } else {
  461. #if CONFIG_MAC_BB_PD
  462. mac_bb_power_down_cb_execute();
  463. #endif
  464. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  465. gpio_sleep_mode_config_apply();
  466. #endif
  467. #if SOC_PM_SUPPORT_CPU_PD && SOC_PM_CPU_RETENTION_BY_RTCCNTL
  468. sleep_enable_cpu_retention();
  469. #endif
  470. #if REGI2C_ANA_CALI_PD_WORKAROUND
  471. regi2c_analog_cali_reg_read();
  472. #endif
  473. #if SOC_PM_RETENTION_HAS_REGDMA_POWER_BUG
  474. sleep_retention_do_system_retention(true);
  475. #endif
  476. }
  477. // TODO: IDF-7370
  478. if (!(deep_sleep && s_adc_tsen_enabled)){
  479. sar_periph_ctrl_power_disable();
  480. }
  481. }
  482. /**
  483. * These save-restore workaround should be moved to lower layer
  484. */
  485. FORCE_INLINE_ATTR void misc_modules_wake_prepare(void)
  486. {
  487. #if SOC_PM_RETENTION_HAS_REGDMA_POWER_BUG
  488. sleep_retention_do_system_retention(false);
  489. #endif
  490. sar_periph_ctrl_power_enable();
  491. #if SOC_PM_SUPPORT_CPU_PD && SOC_PM_CPU_RETENTION_BY_RTCCNTL
  492. sleep_disable_cpu_retention();
  493. #endif
  494. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  495. gpio_sleep_mode_config_unapply();
  496. #endif
  497. #if CONFIG_MAC_BB_PD
  498. mac_bb_power_up_cb_execute();
  499. #endif
  500. #if REGI2C_ANA_CALI_PD_WORKAROUND
  501. regi2c_analog_cali_reg_write();
  502. #endif
  503. }
  504. inline static uint32_t call_rtc_sleep_start(uint32_t reject_triggers, uint32_t lslp_mem_inf_fpu, bool dslp);
  505. static esp_err_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags, esp_sleep_mode_t mode)
  506. {
  507. // Stop UART output so that output is not lost due to APB frequency change.
  508. // For light sleep, suspend UART output — it will resume after wakeup.
  509. // For deep sleep, wait for the contents of UART FIFO to be sent.
  510. bool deep_sleep = (mode == ESP_SLEEP_MODE_DEEP_SLEEP);
  511. bool should_skip_sleep = false;
  512. #if CONFIG_ESP_SLEEP_DEBUG
  513. // The following three logs are used to confirm whether the digital domain and modem are powered off.
  514. // Some CI tests depend on these three logs and it is best not to modify them.
  515. ESP_EARLY_LOGD(TAG, "pd_lags %lu", pd_flags);
  516. ESP_EARLY_LOGD(TAG, "PMU_SLEEP_PD_TOP: %s", (pd_flags & PMU_SLEEP_PD_TOP) ? "True":"False");
  517. ESP_EARLY_LOGD(TAG, "PMU_SLEEP_PD_MODEM: %s", (pd_flags & PMU_SLEEP_PD_MODEM) ? "True":"False");
  518. #endif
  519. int64_t sleep_duration = (int64_t) s_config.sleep_duration - (int64_t) s_config.sleep_time_adjustment;
  520. #if SOC_RTC_SLOW_CLK_SUPPORT_RC_FAST_D256
  521. //Keep the RTC8M_CLK on if RTC clock is rc_fast_d256.
  522. bool rtc_using_8md256 = (rtc_clk_slow_src_get() == SOC_RTC_SLOW_CLK_SRC_RC_FAST_D256);
  523. #else
  524. bool rtc_using_8md256 = false;
  525. #endif
  526. //Keep the RTC8M_CLK on if the ledc low-speed channel is clocked by RTC8M_CLK in lightsleep mode
  527. bool periph_using_8m = !deep_sleep && s_periph_use_8m_flag;
  528. //Override user-configured power modes.
  529. if (rtc_using_8md256 || periph_using_8m) {
  530. pd_flags &= ~RTC_SLEEP_PD_INT_8M;
  531. }
  532. // Sleep UART prepare
  533. if (deep_sleep) {
  534. flush_uarts();
  535. } else {
  536. should_skip_sleep = light_sleep_uart_prepare(pd_flags, sleep_duration);
  537. }
  538. // Will switch to XTAL turn down MSPI speed
  539. mspi_timing_change_speed_mode_cache_safe(true);
  540. // Save current frequency and switch to XTAL
  541. rtc_cpu_freq_config_t cpu_freq_config;
  542. rtc_clk_cpu_freq_get_config(&cpu_freq_config);
  543. rtc_clk_cpu_freq_set_xtal();
  544. #if SOC_PM_SUPPORT_EXT0_WAKEUP
  545. // Configure pins for external wakeup
  546. if (s_config.wakeup_triggers & RTC_EXT0_TRIG_EN) {
  547. ext0_wakeup_prepare();
  548. }
  549. #endif
  550. #if SOC_PM_SUPPORT_EXT1_WAKEUP
  551. if (s_config.wakeup_triggers & RTC_EXT1_TRIG_EN) {
  552. ext1_wakeup_prepare();
  553. }
  554. #endif
  555. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  556. if (deep_sleep && (s_config.wakeup_triggers & RTC_GPIO_TRIG_EN)) {
  557. gpio_deep_sleep_wakeup_prepare();
  558. }
  559. #endif
  560. #if CONFIG_ULP_COPROC_TYPE_FSM
  561. // Enable ULP wakeup
  562. if (s_config.wakeup_triggers & RTC_ULP_TRIG_EN) {
  563. #ifdef CONFIG_IDF_TARGET_ESP32
  564. rtc_hal_ulp_wakeup_enable();
  565. #else
  566. rtc_hal_ulp_int_clear();
  567. #endif
  568. }
  569. #endif
  570. misc_modules_sleep_prepare(deep_sleep);
  571. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  572. if (deep_sleep) {
  573. if (s_config.wakeup_triggers & RTC_TOUCH_TRIG_EN) {
  574. touch_wakeup_prepare();
  575. #if CONFIG_IDF_TARGET_ESP32S2
  576. /* Workaround: In deep sleep, for ESP32S2, Power down the RTC_PERIPH will change the slope configuration of Touch sensor sleep pad.
  577. * The configuration change will change the reading of the sleep pad, which will cause the touch wake-up sensor to trigger falsely.
  578. */
  579. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  580. #endif
  581. }
  582. } else {
  583. /* In light sleep, the RTC_PERIPH power domain should be in the power-on state (Power on the touch circuit in light sleep),
  584. * otherwise the touch sensor FSM will be cleared, causing touch sensor false triggering.
  585. */
  586. if (touch_ll_get_fsm_state()) { // Check if the touch sensor is working properly.
  587. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  588. }
  589. }
  590. #endif
  591. /* Enable sleep reject for faster return from this function,
  592. * in case the wakeup is already triggerred.
  593. */
  594. uint32_t reject_triggers = (s_config.wakeup_triggers & RTC_SLEEP_REJECT_MASK) | sleep_modem_reject_triggers();
  595. //Append some flags in addition to power domains
  596. uint32_t sleep_flags = pd_flags;
  597. if (s_adc_tsen_enabled) {
  598. sleep_flags |= RTC_SLEEP_USE_ADC_TESEN_MONITOR;
  599. }
  600. if (!s_ultra_low_enabled) {
  601. sleep_flags |= RTC_SLEEP_NO_ULTRA_LOW;
  602. }
  603. if (periph_using_8m) {
  604. sleep_flags |= RTC_SLEEP_DIG_USE_8M;
  605. }
  606. // Enter sleep
  607. esp_err_t result;
  608. #if SOC_PMU_SUPPORTED
  609. pmu_sleep_config_t config;
  610. pmu_sleep_init(pmu_sleep_config_default(&config, pd_flags, s_config.sleep_time_adjustment,
  611. s_config.rtc_clk_cal_period, s_config.fast_clk_cal_period,
  612. deep_sleep), deep_sleep);
  613. #else
  614. rtc_sleep_config_t config;
  615. rtc_sleep_get_default_config(sleep_flags, &config);
  616. rtc_sleep_init(config);
  617. // Set state machine time for light sleep
  618. if (!deep_sleep) {
  619. rtc_sleep_low_init(s_config.rtc_clk_cal_period);
  620. }
  621. #endif
  622. // Configure timer wakeup
  623. if (!should_skip_sleep && (s_config.wakeup_triggers & RTC_TIMER_TRIG_EN)) {
  624. if (timer_wakeup_prepare(sleep_duration) != ESP_OK) {
  625. should_skip_sleep = true;
  626. }
  627. }
  628. #if CONFIG_ESP_SLEEP_SYSTIMER_STALL_WORKAROUND
  629. if (!(pd_flags & RTC_SLEEP_PD_XTAL)) {
  630. rtc_sleep_systimer_enable(false);
  631. }
  632. #endif
  633. if (should_skip_sleep) {
  634. result = ESP_ERR_SLEEP_REJECT;
  635. } else {
  636. if (deep_sleep) {
  637. #if !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
  638. esp_sleep_isolate_digital_gpio();
  639. #endif
  640. #if SOC_PM_SUPPORT_DEEPSLEEP_CHECK_STUB_ONLY
  641. esp_set_deep_sleep_wake_stub_default_entry();
  642. // Enter Deep Sleep
  643. #if SOC_PMU_SUPPORTED
  644. result = call_rtc_sleep_start(reject_triggers, config.power.hp_sys.dig_power.mem_dslp, deep_sleep);
  645. #else
  646. result = call_rtc_sleep_start(reject_triggers, config.lslp_mem_inf_fpu, deep_sleep);
  647. #endif
  648. #else
  649. #if !CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  650. /* If not possible stack is in RTC FAST memory, use the ROM function to calculate the CRC and save ~140 bytes IRAM */
  651. #if SOC_RTC_FAST_MEM_SUPPORTED
  652. set_rtc_memory_crc();
  653. #endif
  654. result = call_rtc_sleep_start(reject_triggers, config.lslp_mem_inf_fpu, deep_sleep);
  655. #else
  656. /* Otherwise, need to call the dedicated soc function for this */
  657. result = rtc_deep_sleep_start(s_config.wakeup_triggers, reject_triggers);
  658. #endif
  659. #endif // SOC_PM_SUPPORT_DEEPSLEEP_CHECK_STUB_ONLY
  660. } else {
  661. /* Cache Suspend 1: will wait cache idle in cache suspend */
  662. suspend_cache();
  663. /* On esp32c6, only the lp_aon pad hold function can only hold the GPIO state in the active mode.
  664. In order to avoid the leakage of the SPI cs pin, hold it here */
  665. #if (CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP && CONFIG_ESP_SLEEP_FLASH_LEAKAGE_WORKAROUND)
  666. #if !CONFIG_IDF_TARGET_ESP32H2 // ESP32H2 TODO IDF-7359: related rtcio ll func not supported yet
  667. if(!(pd_flags & RTC_SLEEP_PD_VDDSDIO)) {
  668. /* Cache suspend also means SPI bus IDLE, then we can hold SPI CS pin safely */
  669. gpio_ll_hold_en(&GPIO, SPI_CS0_GPIO_NUM);
  670. }
  671. #endif
  672. #endif
  673. #if SOC_PMU_SUPPORTED
  674. #if SOC_PM_CPU_RETENTION_BY_SW
  675. if (pd_flags & PMU_SLEEP_PD_CPU) {
  676. result = esp_sleep_cpu_retention(pmu_sleep_start, s_config.wakeup_triggers, reject_triggers, config.power.hp_sys.dig_power.mem_dslp, deep_sleep);
  677. } else {
  678. #endif
  679. result = call_rtc_sleep_start(reject_triggers, config.power.hp_sys.dig_power.mem_dslp, deep_sleep);
  680. }
  681. #else
  682. result = call_rtc_sleep_start(reject_triggers, config.lslp_mem_inf_fpu, deep_sleep);
  683. #endif
  684. /* Unhold the SPI CS pin */
  685. #if (CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP && CONFIG_ESP_SLEEP_FLASH_LEAKAGE_WORKAROUND)
  686. #if !CONFIG_IDF_TARGET_ESP32H2 // ESP32H2 TODO IDF-7359: related rtcio ll func not supported yet
  687. if(!(pd_flags & RTC_SLEEP_PD_VDDSDIO)) {
  688. gpio_ll_hold_dis(&GPIO, SPI_CS0_GPIO_NUM);
  689. }
  690. #endif
  691. #endif
  692. /* Cache Resume 1: Resume cache for continue running*/
  693. resume_cache();
  694. }
  695. #if CONFIG_ESP_SLEEP_CACHE_SAFE_ASSERTION
  696. if (pd_flags & RTC_SLEEP_PD_VDDSDIO) {
  697. /* Cache Suspend 2: If previous sleep powerdowned the flash, suspend cache here so that the
  698. access to flash before flash ready can be explicitly exposed. */
  699. suspend_cache();
  700. }
  701. #endif
  702. #if CONFIG_ESP_SLEEP_SYSTIMER_STALL_WORKAROUND
  703. if (!(pd_flags & RTC_SLEEP_PD_XTAL)) {
  704. rtc_sleep_systimer_enable(true);
  705. }
  706. #endif
  707. }
  708. // Restore CPU frequency
  709. #if SOC_PM_SUPPORT_PMU_MODEM_STATE
  710. if (pmu_sleep_pll_already_enabled()) {
  711. rtc_clk_cpu_freq_to_pll_and_pll_lock_release(esp_pm_impl_get_cpu_freq(PM_MODE_CPU_MAX));
  712. } else
  713. #endif
  714. {
  715. rtc_clk_cpu_freq_set_config(&cpu_freq_config);
  716. }
  717. if (cpu_freq_config.source == SOC_CPU_CLK_SRC_PLL) {
  718. // Turn up MSPI speed if switch to PLL
  719. mspi_timing_change_speed_mode_cache_safe(false);
  720. }
  721. if (!deep_sleep) {
  722. s_config.ccount_ticks_record = esp_cpu_get_cycle_count();
  723. misc_modules_wake_prepare();
  724. }
  725. // re-enable UART output
  726. resume_uarts();
  727. s_lightsleep_cnt++;
  728. return result ? ESP_ERR_SLEEP_REJECT : ESP_OK;
  729. }
  730. inline static uint32_t IRAM_ATTR call_rtc_sleep_start(uint32_t reject_triggers, uint32_t lslp_mem_inf_fpu, bool dslp)
  731. {
  732. #ifdef CONFIG_IDF_TARGET_ESP32
  733. return rtc_sleep_start(s_config.wakeup_triggers, reject_triggers);
  734. #elif SOC_PMU_SUPPORTED
  735. return pmu_sleep_start(s_config.wakeup_triggers, reject_triggers, lslp_mem_inf_fpu, dslp);
  736. #else
  737. return rtc_sleep_start(s_config.wakeup_triggers, reject_triggers, lslp_mem_inf_fpu);
  738. #endif
  739. }
  740. void IRAM_ATTR esp_deep_sleep_start(void)
  741. {
  742. #if CONFIG_IDF_TARGET_ESP32S2
  743. /* Due to hardware limitations, on S2 the brownout detector sometimes trigger during deep sleep
  744. to circumvent this we disable the brownout detector before sleeping */
  745. esp_brownout_disable();
  746. #endif //CONFIG_IDF_TARGET_ESP32S2
  747. esp_sync_timekeeping_timers();
  748. /* Disable interrupts and stall another core in case another task writes
  749. * to RTC memory while we calculate RTC memory CRC.
  750. */
  751. portENTER_CRITICAL(&spinlock_rtc_deep_sleep);
  752. esp_ipc_isr_stall_other_cpu();
  753. // record current RTC time
  754. s_config.rtc_ticks_at_sleep_start = rtc_time_get();
  755. #if SOC_RTC_FAST_MEM_SUPPORTED
  756. // Configure wake stub
  757. if (esp_get_deep_sleep_wake_stub() == NULL) {
  758. esp_set_deep_sleep_wake_stub(esp_wake_deep_sleep);
  759. }
  760. #endif // SOC_RTC_FAST_MEM_SUPPORTED
  761. // Decide which power domains can be powered down
  762. uint32_t pd_flags = get_power_down_flags();
  763. s_config.rtc_clk_cal_period = esp_clk_slowclk_cal_get();
  764. // Correct the sleep time
  765. s_config.sleep_time_adjustment = DEEP_SLEEP_TIME_OVERHEAD_US;
  766. #if SOC_PMU_SUPPORTED
  767. uint32_t force_pd_flags = PMU_SLEEP_PD_TOP | PMU_SLEEP_PD_VDDSDIO | PMU_SLEEP_PD_MODEM | PMU_SLEEP_PD_HP_PERIPH \
  768. | PMU_SLEEP_PD_CPU | PMU_SLEEP_PD_MEM | PMU_SLEEP_PD_XTAL;
  769. #if SOC_PM_SUPPORT_HP_AON_PD
  770. force_pd_flags |= PMU_SLEEP_PD_HP_AON;
  771. #endif
  772. #else
  773. uint32_t force_pd_flags = RTC_SLEEP_PD_DIG | RTC_SLEEP_PD_VDDSDIO | RTC_SLEEP_PD_INT_8M | RTC_SLEEP_PD_XTAL;
  774. #endif
  775. /**
  776. * If all wireless modules share one power domain, we name this power domain "modem".
  777. * If wireless modules have their own power domain, we give these power domains separate
  778. * names.
  779. */
  780. #if SOC_PM_SUPPORT_MODEM_PD
  781. force_pd_flags |= RTC_SLEEP_PD_MODEM;
  782. #endif
  783. #if SOC_PM_SUPPORT_WIFI_PD
  784. force_pd_flags |= RTC_SLEEP_PD_WIFI;
  785. #endif
  786. #if SOC_PM_SUPPORT_BT_PD
  787. force_pd_flags |= RTC_SLEEP_PD_BT;
  788. #endif
  789. // Enter sleep
  790. if (esp_sleep_start(force_pd_flags | pd_flags, ESP_SLEEP_MODE_DEEP_SLEEP) == ESP_ERR_SLEEP_REJECT) {
  791. #if CONFIG_ESP_SLEEP_CACHE_SAFE_ASSERTION
  792. /* Cache Resume 2: if CONFIG_ESP_SLEEP_CACHE_SAFE_ASSERTION is enabled, cache has been suspended in esp_sleep_start */
  793. resume_cache();
  794. #endif
  795. ESP_EARLY_LOGE(TAG, "Deep sleep request is rejected");
  796. } else {
  797. // Because RTC is in a slower clock domain than the CPU, it
  798. // can take several CPU cycles for the sleep mode to start.
  799. while (1) {
  800. ;
  801. }
  802. }
  803. // Never returns here, except that the sleep is rejected.
  804. esp_ipc_isr_release_other_cpu();
  805. portEXIT_CRITICAL(&spinlock_rtc_deep_sleep);
  806. }
  807. /**
  808. * Helper function which handles entry to and exit from light sleep
  809. * Placed into IRAM as flash may need some time to be powered on.
  810. */
  811. static esp_err_t esp_light_sleep_inner(uint32_t pd_flags,
  812. uint32_t flash_enable_time_us) IRAM_ATTR __attribute__((noinline));
  813. static esp_err_t esp_light_sleep_inner(uint32_t pd_flags,
  814. uint32_t flash_enable_time_us)
  815. {
  816. #if SOC_CONFIGURABLE_VDDSDIO_SUPPORTED
  817. rtc_vddsdio_config_t vddsdio_config = rtc_vddsdio_get_config();
  818. #endif
  819. // Enter sleep
  820. esp_err_t reject = esp_sleep_start(pd_flags, ESP_SLEEP_MODE_LIGHT_SLEEP);
  821. #if SOC_CONFIGURABLE_VDDSDIO_SUPPORTED
  822. // If VDDSDIO regulator was controlled by RTC registers before sleep,
  823. // restore the configuration.
  824. if (vddsdio_config.force) {
  825. rtc_vddsdio_set_config(vddsdio_config);
  826. }
  827. #endif
  828. // If SPI flash was powered down, wait for it to become ready
  829. if (pd_flags & RTC_SLEEP_PD_VDDSDIO) {
  830. // Wait for the flash chip to start up
  831. esp_rom_delay_us(flash_enable_time_us);
  832. }
  833. #if CONFIG_ESP_SLEEP_CACHE_SAFE_ASSERTION
  834. if (pd_flags & RTC_SLEEP_PD_VDDSDIO) {
  835. /* Cache Resume 2: flash is ready now, we can resume the cache and access flash safely after */
  836. resume_cache();
  837. }
  838. #endif
  839. return reject;
  840. }
  841. /**
  842. * vddsdio is used for power supply of spi flash
  843. *
  844. * pd flash via menuconfig | pd flash via `esp_sleep_pd_config` | result
  845. * ---------------------------------------------------------------------------------------------------
  846. * 0 | 0 | no pd flash
  847. * x | 1 | pd flash with relaxed conditions(force_pd)
  848. * 1 | 0 | pd flash with strict conditions(safe_pd)
  849. */
  850. FORCE_INLINE_ATTR bool can_power_down_vddsdio(uint32_t pd_flags, const uint32_t vddsdio_pd_sleep_duration)
  851. {
  852. bool force_pd = !(s_config.wakeup_triggers & RTC_TIMER_TRIG_EN) || (s_config.sleep_duration > vddsdio_pd_sleep_duration);
  853. bool safe_pd = (s_config.wakeup_triggers == RTC_TIMER_TRIG_EN) && (s_config.sleep_duration > vddsdio_pd_sleep_duration);
  854. return (pd_flags & RTC_SLEEP_PD_VDDSDIO) ? force_pd : safe_pd;
  855. }
  856. esp_err_t esp_light_sleep_start(void)
  857. {
  858. s_config.ccount_ticks_record = esp_cpu_get_cycle_count();
  859. #if CONFIG_ESP_TASK_WDT_USE_ESP_TIMER
  860. esp_err_t timerret = ESP_OK;
  861. /* If a task watchdog timer is running, we have to stop it. */
  862. timerret = esp_task_wdt_stop();
  863. #endif // CONFIG_ESP_TASK_WDT_USE_ESP_TIMER
  864. portENTER_CRITICAL(&s_config.lock);
  865. /*
  866. Note: We are about to stall the other CPU via the esp_ipc_isr_stall_other_cpu(). However, there is a chance of
  867. deadlock if after stalling the other CPU, we attempt to take spinlocks already held by the other CPU that is.
  868. Thus any functions that we call after stalling the other CPU will need to have the locks taken first to avoid
  869. deadlock.
  870. Todo: IDF-5257
  871. */
  872. /* We will be calling esp_timer_private_set inside DPORT access critical
  873. * section. Make sure the code on the other CPU is not holding esp_timer
  874. * lock, otherwise there will be deadlock.
  875. */
  876. esp_timer_private_lock();
  877. /* We will be calling esp_rtc_get_time_us() below. Make sure the code on the other CPU is not holding the
  878. * esp_rtc_get_time_us() lock, otherwise there will be deadlock. esp_rtc_get_time_us() is called via:
  879. *
  880. * - esp_clk_slowclk_cal_set() -> esp_rtc_get_time_us()
  881. */
  882. esp_clk_private_lock();
  883. #if SOC_LP_TIMER_SUPPORTED
  884. s_config.rtc_ticks_at_sleep_start = lp_timer_hal_get_cycle_count();
  885. #else
  886. s_config.rtc_ticks_at_sleep_start = rtc_time_get();
  887. #endif
  888. uint32_t ccount_at_sleep_start = esp_cpu_get_cycle_count();
  889. uint64_t high_res_time_at_start = esp_timer_get_time();
  890. uint32_t sleep_time_overhead_in = (ccount_at_sleep_start - s_config.ccount_ticks_record) / (esp_clk_cpu_freq() / 1000000ULL);
  891. esp_ipc_isr_stall_other_cpu();
  892. #if CONFIG_ESP_SLEEP_CACHE_SAFE_ASSERTION && CONFIG_PM_SLP_IRAM_OPT
  893. /* Cache Suspend 0: if CONFIG_PM_SLP_IRAM_OPT is enabled, suspend cache here so that the access to flash
  894. during the sleep process can be explicitly exposed. */
  895. suspend_cache();
  896. #endif
  897. // Decide which power domains can be powered down
  898. uint32_t pd_flags = get_power_down_flags();
  899. #ifdef CONFIG_ESP_SLEEP_RTC_BUS_ISO_WORKAROUND
  900. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  901. #endif
  902. // Re-calibrate the RTC Timer clock
  903. #ifdef CONFIG_ESP_SYSTEM_RTC_EXT_XTAL
  904. if (rtc_clk_slow_src_get() == SOC_RTC_SLOW_CLK_SRC_XTAL32K) {
  905. uint64_t time_per_us = 1000000ULL;
  906. s_config.rtc_clk_cal_period = (time_per_us << RTC_CLK_CAL_FRACT) / rtc_clk_slow_freq_get_hz();
  907. } else {
  908. // If the external 32 kHz XTAL does not exist, use the internal 150 kHz RC oscillator
  909. // as the RTC slow clock source.
  910. s_config.rtc_clk_cal_period = rtc_clk_cal(RTC_CAL_RTC_MUX, RTC_CLK_SRC_CAL_CYCLES);
  911. esp_clk_slowclk_cal_set(s_config.rtc_clk_cal_period);
  912. }
  913. #elif CONFIG_RTC_CLK_SRC_INT_RC && CONFIG_IDF_TARGET_ESP32S2
  914. s_config.rtc_clk_cal_period = rtc_clk_cal_cycling(RTC_CAL_RTC_MUX, RTC_CLK_SRC_CAL_CYCLES);
  915. esp_clk_slowclk_cal_set(s_config.rtc_clk_cal_period);
  916. #else
  917. #if CONFIG_PM_ENABLE
  918. if (s_lightsleep_cnt % CONFIG_PM_LIGHTSLEEP_RTC_OSC_CAL_INTERVAL == 0)
  919. #endif
  920. {
  921. s_config.rtc_clk_cal_period = rtc_clk_cal(RTC_CAL_RTC_MUX, RTC_CLK_SRC_CAL_CYCLES);
  922. esp_clk_slowclk_cal_set(s_config.rtc_clk_cal_period);
  923. }
  924. #endif
  925. /*
  926. * Adjustment time consists of parts below:
  927. * 1. Hardware time waiting for internal 8M oscilate clock and XTAL;
  928. * 2. Hardware state swithing time of the rtc main state machine;
  929. * 3. Code execution time when clock is not stable;
  930. * 4. Code execution time which can be measured;
  931. */
  932. #if SOC_PMU_SUPPORTED
  933. #if CONFIG_PM_ENABLE
  934. if (s_lightsleep_cnt % CONFIG_PM_LIGHTSLEEP_RTC_OSC_CAL_INTERVAL == 0)
  935. #endif
  936. {
  937. s_config.fast_clk_cal_period = rtc_clk_cal(RTC_CAL_RC_FAST, FAST_CLK_SRC_CAL_CYCLES);
  938. }
  939. int sleep_time_sw_adjustment = LIGHT_SLEEP_TIME_OVERHEAD_US + sleep_time_overhead_in + s_config.sleep_time_overhead_out;
  940. int sleep_time_hw_adjustment = pmu_sleep_calculate_hw_wait_time(pd_flags, s_config.rtc_clk_cal_period, s_config.fast_clk_cal_period);
  941. s_config.sleep_time_adjustment = sleep_time_sw_adjustment + sleep_time_hw_adjustment;
  942. #else
  943. 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);
  944. s_config.sleep_time_adjustment = LIGHT_SLEEP_TIME_OVERHEAD_US + sleep_time_overhead_in + s_config.sleep_time_overhead_out
  945. + 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);
  946. #endif
  947. #if CONFIG_IDF_TARGET_ESP32C6 // TODO: IDF-6930
  948. const uint32_t flash_enable_time_us = 0;
  949. #else
  950. // Decide if VDD_SDIO needs to be powered down;
  951. // If it needs to be powered down, adjust sleep time.
  952. const uint32_t flash_enable_time_us = VDD_SDIO_POWERUP_TO_FLASH_READ_US + DEEP_SLEEP_WAKEUP_DELAY;
  953. /**
  954. * If VDD_SDIO power domain is requested to be turned off, bit `RTC_SLEEP_PD_VDDSDIO`
  955. * will be set in `pd_flags`.
  956. */
  957. if (pd_flags & RTC_SLEEP_PD_VDDSDIO) {
  958. /*
  959. * When VDD_SDIO power domain has to be turned off, the minimum sleep time of the
  960. * system needs to meet the sum below:
  961. * 1. Wait time for the flash power-on after waking up;
  962. * 2. The execution time of codes between RTC Timer get start time
  963. * with hardware starts to switch state to sleep;
  964. * 3. The hardware state switching time of the rtc state machine during
  965. * sleep and wake-up. This process requires 6 cycles to complete.
  966. * The specific hardware state switching process and the cycles
  967. * consumed are rtc_cpu_run_stall(1), cut_pll_rtl(2), cut_8m(1),
  968. * min_protect(2);
  969. * 4. All the adjustment time which is s_config.sleep_time_adjustment below.
  970. */
  971. const uint32_t vddsdio_pd_sleep_duration = MAX(FLASH_PD_MIN_SLEEP_TIME_US,
  972. flash_enable_time_us + LIGHT_SLEEP_MIN_TIME_US + s_config.sleep_time_adjustment
  973. + rtc_time_slowclk_to_us(RTC_MODULE_SLEEP_PREPARE_CYCLES, s_config.rtc_clk_cal_period));
  974. if (can_power_down_vddsdio(pd_flags, vddsdio_pd_sleep_duration)) {
  975. if (s_config.sleep_time_overhead_out < flash_enable_time_us) {
  976. s_config.sleep_time_adjustment += flash_enable_time_us;
  977. }
  978. } else {
  979. /**
  980. * Minimum sleep time is not enough, then keep the VDD_SDIO power
  981. * domain on.
  982. */
  983. pd_flags &= ~RTC_SLEEP_PD_VDDSDIO;
  984. if (s_config.sleep_time_overhead_out > flash_enable_time_us) {
  985. s_config.sleep_time_adjustment -= flash_enable_time_us;
  986. }
  987. }
  988. }
  989. #endif
  990. periph_inform_out_light_sleep_overhead(s_config.sleep_time_adjustment - sleep_time_overhead_in);
  991. // Safety net: enable WDT in case exit from light sleep fails
  992. wdt_hal_context_t rtc_wdt_ctx = RWDT_HAL_CONTEXT_DEFAULT();
  993. 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.
  994. if (!wdt_was_enabled) {
  995. wdt_hal_init(&rtc_wdt_ctx, WDT_RWDT, 0, false);
  996. uint32_t stage_timeout_ticks = (uint32_t)(1000ULL * rtc_clk_slow_freq_get_hz() / 1000ULL);
  997. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  998. wdt_hal_config_stage(&rtc_wdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_RTC);
  999. wdt_hal_enable(&rtc_wdt_ctx);
  1000. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  1001. }
  1002. esp_err_t err = ESP_OK;
  1003. int64_t final_sleep_duration_us = (int64_t)s_config.sleep_duration - (int64_t)s_config.sleep_time_adjustment;
  1004. int64_t min_sleep_duration_us = rtc_time_slowclk_to_us(RTC_CNTL_MIN_SLP_VAL_MIN, s_config.rtc_clk_cal_period);
  1005. // reset light sleep wakeup flag before a new light sleep
  1006. s_light_sleep_wakeup = false;
  1007. // if rtc timer wakeup source is enabled, need to compare final sleep duration and min sleep duration to avoid late wakeup
  1008. if ((s_config.wakeup_triggers & RTC_TIMER_TRIG_EN) && (final_sleep_duration_us <= min_sleep_duration_us)) {
  1009. err = ESP_ERR_SLEEP_TOO_SHORT_SLEEP_DURATION;
  1010. } else {
  1011. // Enter sleep, then wait for flash to be ready on wakeup
  1012. err = esp_light_sleep_inner(pd_flags, flash_enable_time_us);
  1013. }
  1014. // light sleep wakeup flag only makes sense after a successful light sleep
  1015. s_light_sleep_wakeup = (err == ESP_OK);
  1016. // System timer has been stopped for the duration of the sleep, correct for that.
  1017. #if SOC_LP_TIMER_SUPPORTED
  1018. uint64_t rtc_ticks_at_end = lp_timer_hal_get_cycle_count();
  1019. #else
  1020. uint64_t rtc_ticks_at_end = rtc_time_get();
  1021. #endif
  1022. 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);
  1023. /**
  1024. * If sleep duration is too small(less than 1 rtc_slow_clk cycle), rtc_time_diff will be zero.
  1025. * In this case, just ignore the time compensation and keep esp_timer monotonic.
  1026. */
  1027. if (rtc_time_diff > 0) {
  1028. esp_timer_private_set(high_res_time_at_start + rtc_time_diff);
  1029. }
  1030. esp_set_time_from_rtc();
  1031. esp_clk_private_unlock();
  1032. esp_timer_private_unlock();
  1033. #if CONFIG_ESP_SLEEP_CACHE_SAFE_ASSERTION && CONFIG_PM_SLP_IRAM_OPT
  1034. /* Cache Resume 0: sleep process done, resume cache for continue running */
  1035. resume_cache();
  1036. #endif
  1037. esp_ipc_isr_release_other_cpu();
  1038. if (!wdt_was_enabled) {
  1039. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  1040. wdt_hal_disable(&rtc_wdt_ctx);
  1041. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  1042. }
  1043. portEXIT_CRITICAL(&s_config.lock);
  1044. #if CONFIG_ESP_TASK_WDT_USE_ESP_TIMER
  1045. /* Restart the Task Watchdog timer as it was stopped before sleeping. */
  1046. if (timerret == ESP_OK) {
  1047. esp_task_wdt_restart();
  1048. }
  1049. #endif // CONFIG_ESP_TASK_WDT_USE_ESP_TIMER
  1050. s_config.sleep_time_overhead_out = (esp_cpu_get_cycle_count() - s_config.ccount_ticks_record) / (esp_clk_cpu_freq() / 1000000ULL);
  1051. return err;
  1052. }
  1053. esp_err_t esp_sleep_disable_wakeup_source(esp_sleep_source_t source)
  1054. {
  1055. // For most of sources it is enough to set trigger mask in local
  1056. // configuration structure. The actual RTC wake up options
  1057. // will be updated by esp_sleep_start().
  1058. if (source == ESP_SLEEP_WAKEUP_ALL) {
  1059. s_config.wakeup_triggers = 0;
  1060. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_TIMER, RTC_TIMER_TRIG_EN)) {
  1061. s_config.wakeup_triggers &= ~RTC_TIMER_TRIG_EN;
  1062. s_config.sleep_duration = 0;
  1063. #if SOC_PM_SUPPORT_EXT0_WAKEUP
  1064. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_EXT0, RTC_EXT0_TRIG_EN)) {
  1065. s_config.ext0_rtc_gpio_num = 0;
  1066. s_config.ext0_trigger_level = 0;
  1067. s_config.wakeup_triggers &= ~RTC_EXT0_TRIG_EN;
  1068. #endif
  1069. #if SOC_PM_SUPPORT_EXT1_WAKEUP
  1070. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_EXT1, RTC_EXT1_TRIG_EN)) {
  1071. s_config.ext1_rtc_gpio_mask = 0;
  1072. s_config.ext1_trigger_mode = 0;
  1073. s_config.wakeup_triggers &= ~RTC_EXT1_TRIG_EN;
  1074. #endif
  1075. #if SOC_PM_SUPPORT_TOUCH_SENSOR_WAKEUP
  1076. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_TOUCHPAD, RTC_TOUCH_TRIG_EN)) {
  1077. s_config.wakeup_triggers &= ~RTC_TOUCH_TRIG_EN;
  1078. #endif
  1079. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_GPIO, RTC_GPIO_TRIG_EN)) {
  1080. s_config.wakeup_triggers &= ~RTC_GPIO_TRIG_EN;
  1081. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_UART, (RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN))) {
  1082. s_config.wakeup_triggers &= ~(RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN);
  1083. }
  1084. #if CONFIG_ULP_COPROC_TYPE_FSM
  1085. else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_ULP, RTC_ULP_TRIG_EN)) {
  1086. s_config.wakeup_triggers &= ~RTC_ULP_TRIG_EN;
  1087. }
  1088. #endif
  1089. else {
  1090. ESP_LOGE(TAG, "Incorrect wakeup source (%d) to disable.", (int) source);
  1091. return ESP_ERR_INVALID_STATE;
  1092. }
  1093. return ESP_OK;
  1094. }
  1095. esp_err_t esp_sleep_enable_ulp_wakeup(void)
  1096. {
  1097. #ifndef CONFIG_ULP_COPROC_ENABLED
  1098. return ESP_ERR_INVALID_STATE;
  1099. #endif // CONFIG_ULP_COPROC_ENABLED
  1100. #if CONFIG_IDF_TARGET_ESP32
  1101. #if ((defined CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT) || (defined CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT_V2))
  1102. ESP_LOGE(TAG, "Failed to enable wakeup when provide current to external 32kHz crystal");
  1103. return ESP_ERR_NOT_SUPPORTED;
  1104. #endif
  1105. if (s_config.wakeup_triggers & RTC_EXT0_TRIG_EN) {
  1106. ESP_LOGE(TAG, "Conflicting wake-up trigger: ext0");
  1107. return ESP_ERR_INVALID_STATE;
  1108. }
  1109. #endif //CONFIG_IDF_TARGET_ESP32
  1110. #if CONFIG_ULP_COPROC_TYPE_FSM
  1111. s_config.wakeup_triggers |= RTC_ULP_TRIG_EN;
  1112. return ESP_OK;
  1113. #elif CONFIG_ULP_COPROC_TYPE_RISCV
  1114. s_config.wakeup_triggers |= (RTC_COCPU_TRIG_EN | RTC_COCPU_TRAP_TRIG_EN);
  1115. return ESP_OK;
  1116. #elif CONFIG_ULP_COPROC_TYPE_LP_CORE
  1117. s_config.wakeup_triggers |= RTC_LP_CORE_TRIG_EN;
  1118. return ESP_OK;
  1119. #else
  1120. return ESP_ERR_NOT_SUPPORTED;
  1121. #endif //CONFIG_ULP_COPROC_TYPE_FSM
  1122. }
  1123. esp_err_t esp_sleep_enable_timer_wakeup(uint64_t time_in_us)
  1124. {
  1125. s_config.wakeup_triggers |= RTC_TIMER_TRIG_EN;
  1126. s_config.sleep_duration = time_in_us;
  1127. return ESP_OK;
  1128. }
  1129. static esp_err_t timer_wakeup_prepare(int64_t sleep_duration)
  1130. {
  1131. if (sleep_duration < 0) {
  1132. sleep_duration = 0;
  1133. }
  1134. int64_t ticks = rtc_time_us_to_slowclk(sleep_duration, s_config.rtc_clk_cal_period);
  1135. int64_t target_wakeup_tick = s_config.rtc_ticks_at_sleep_start + ticks;
  1136. #if SOC_LP_TIMER_SUPPORTED
  1137. #if CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP
  1138. // Last timer wake-up validity check
  1139. if ((sleep_duration == 0) || \
  1140. (target_wakeup_tick < lp_timer_hal_get_cycle_count() + SLEEP_TIMER_ALARM_TO_SLEEP_TICKS)) {
  1141. // Treat too short sleep duration setting as timer reject
  1142. return ESP_ERR_SLEEP_REJECT;
  1143. }
  1144. #endif
  1145. lp_timer_hal_set_alarm_target(0, target_wakeup_tick);
  1146. #else
  1147. rtc_hal_set_wakeup_timer(target_wakeup_tick);
  1148. #endif
  1149. return ESP_OK;
  1150. }
  1151. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  1152. /* In deep sleep mode, only the sleep channel is supported, and other touch channels should be turned off. */
  1153. static void touch_wakeup_prepare(void)
  1154. {
  1155. uint16_t sleep_cycle = 0;
  1156. uint16_t meas_times = 0;
  1157. touch_pad_t touch_num = TOUCH_PAD_NUM0;
  1158. touch_ll_sleep_get_channel_num(&touch_num); // Check if the sleep pad is enabled.
  1159. if ((touch_num > TOUCH_PAD_NUM0) && (touch_num < TOUCH_PAD_MAX) && touch_ll_get_fsm_state()) {
  1160. touch_ll_stop_fsm();
  1161. touch_ll_clear_channel_mask(TOUCH_PAD_BIT_MASK_ALL);
  1162. touch_ll_intr_clear(TOUCH_PAD_INTR_MASK_ALL); // Clear state from previous wakeup
  1163. touch_hal_sleep_channel_get_work_time(&sleep_cycle, &meas_times);
  1164. touch_ll_set_meas_times(meas_times);
  1165. touch_ll_set_sleep_time(sleep_cycle);
  1166. touch_ll_set_channel_mask(BIT(touch_num));
  1167. touch_ll_start_fsm();
  1168. }
  1169. }
  1170. #endif
  1171. #if SOC_TOUCH_SENSOR_SUPPORTED
  1172. esp_err_t esp_sleep_enable_touchpad_wakeup(void)
  1173. {
  1174. #if CONFIG_IDF_TARGET_ESP32
  1175. #if ((defined CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT) || (defined CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT_V2))
  1176. ESP_LOGE(TAG, "Failed to enable wakeup when provide current to external 32kHz crystal");
  1177. return ESP_ERR_NOT_SUPPORTED;
  1178. #endif
  1179. if (s_config.wakeup_triggers & (RTC_EXT0_TRIG_EN)) {
  1180. ESP_LOGE(TAG, "Conflicting wake-up trigger: ext0");
  1181. return ESP_ERR_INVALID_STATE;
  1182. }
  1183. #endif //CONFIG_IDF_TARGET_ESP32
  1184. s_config.wakeup_triggers |= RTC_TOUCH_TRIG_EN;
  1185. return ESP_OK;
  1186. }
  1187. touch_pad_t esp_sleep_get_touchpad_wakeup_status(void)
  1188. {
  1189. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_TOUCHPAD) {
  1190. return TOUCH_PAD_MAX;
  1191. }
  1192. touch_pad_t pad_num;
  1193. esp_err_t ret = touch_pad_get_wakeup_status(&pad_num); //TODO 723diff commit id:fda9ada1b
  1194. assert(ret == ESP_OK && "wakeup reason is RTC_TOUCH_TRIG_EN but SENS_TOUCH_MEAS_EN is zero");
  1195. return (ret == ESP_OK) ? pad_num : TOUCH_PAD_MAX;
  1196. }
  1197. #endif // SOC_TOUCH_SENSOR_SUPPORTED
  1198. bool esp_sleep_is_valid_wakeup_gpio(gpio_num_t gpio_num)
  1199. {
  1200. #if SOC_RTCIO_PIN_COUNT > 0
  1201. return RTC_GPIO_IS_VALID_GPIO(gpio_num);
  1202. #else
  1203. return GPIO_IS_DEEP_SLEEP_WAKEUP_VALID_GPIO(gpio_num);
  1204. #endif
  1205. }
  1206. #if SOC_PM_SUPPORT_EXT0_WAKEUP
  1207. esp_err_t esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level)
  1208. {
  1209. if (level < 0 || level > 1) {
  1210. return ESP_ERR_INVALID_ARG;
  1211. }
  1212. if (!esp_sleep_is_valid_wakeup_gpio(gpio_num)) {
  1213. return ESP_ERR_INVALID_ARG;
  1214. }
  1215. #if CONFIG_IDF_TARGET_ESP32
  1216. if (s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) {
  1217. ESP_LOGE(TAG, "Conflicting wake-up triggers: touch / ULP");
  1218. return ESP_ERR_INVALID_STATE;
  1219. }
  1220. #endif //CONFIG_IDF_TARGET_ESP32
  1221. s_config.ext0_rtc_gpio_num = rtc_io_number_get(gpio_num);
  1222. s_config.ext0_trigger_level = level;
  1223. s_config.wakeup_triggers |= RTC_EXT0_TRIG_EN;
  1224. return ESP_OK;
  1225. }
  1226. static void ext0_wakeup_prepare(void)
  1227. {
  1228. int rtc_gpio_num = s_config.ext0_rtc_gpio_num;
  1229. rtcio_hal_ext0_set_wakeup_pin(rtc_gpio_num, s_config.ext0_trigger_level);
  1230. rtcio_hal_function_select(rtc_gpio_num, RTCIO_FUNC_RTC);
  1231. rtcio_hal_input_enable(rtc_gpio_num);
  1232. }
  1233. #endif // SOC_PM_SUPPORT_EXT0_WAKEUP
  1234. #if SOC_PM_SUPPORT_EXT1_WAKEUP
  1235. esp_err_t esp_sleep_enable_ext1_wakeup(uint64_t io_mask, esp_sleep_ext1_wakeup_mode_t level_mode)
  1236. {
  1237. if (level_mode > ESP_EXT1_WAKEUP_ANY_HIGH) {
  1238. return ESP_ERR_INVALID_ARG;
  1239. }
  1240. // Translate bit map of GPIO numbers into the bit map of RTC IO numbers
  1241. uint32_t rtc_gpio_mask = 0;
  1242. for (int gpio = 0; io_mask; ++gpio, io_mask >>= 1) {
  1243. if ((io_mask & 1) == 0) {
  1244. continue;
  1245. }
  1246. if (!esp_sleep_is_valid_wakeup_gpio(gpio)) {
  1247. ESP_LOGE(TAG, "Not an RTC IO: GPIO%d", gpio);
  1248. return ESP_ERR_INVALID_ARG;
  1249. }
  1250. rtc_gpio_mask |= BIT(rtc_io_number_get(gpio));
  1251. }
  1252. s_config.ext1_rtc_gpio_mask = rtc_gpio_mask;
  1253. if (level_mode) {
  1254. s_config.ext1_trigger_mode = io_mask;
  1255. } else {
  1256. s_config.ext1_trigger_mode = 0;
  1257. }
  1258. s_config.wakeup_triggers |= RTC_EXT1_TRIG_EN;
  1259. return ESP_OK;
  1260. }
  1261. #if SOC_PM_SUPPORT_EXT1_WAKEUP_MODE_PER_PIN
  1262. esp_err_t esp_sleep_enable_ext1_wakeup_with_level_mask(uint64_t io_mask, uint64_t level_mask)
  1263. {
  1264. if ((level_mask & io_mask) != level_mask) {
  1265. return ESP_ERR_INVALID_ARG;
  1266. }
  1267. // Translate bit map of GPIO numbers into the bit map of RTC IO numbers
  1268. // Translate bit map of GPIO wakeup mode into the bit map of RTC IO wakeup mode
  1269. uint32_t rtc_gpio_mask = 0, rtc_gpio_wakeup_mode_mask = 0;
  1270. for (int gpio = 0; io_mask; ++gpio, io_mask >>= 1, level_mask >>= 1) {
  1271. if ((io_mask & 1) == 0) {
  1272. continue;
  1273. }
  1274. if (!esp_sleep_is_valid_wakeup_gpio(gpio)) {
  1275. ESP_LOGE(TAG, "Not an RTC IO Considering io_mask: GPIO%d", gpio);
  1276. return ESP_ERR_INVALID_ARG;
  1277. }
  1278. rtc_gpio_mask |= BIT(rtc_io_number_get(gpio));
  1279. if ((level_mask & 1) == 1) {
  1280. rtc_gpio_wakeup_mode_mask |= BIT(rtc_io_number_get(gpio));
  1281. }
  1282. }
  1283. s_config.ext1_rtc_gpio_mask = rtc_gpio_mask;
  1284. s_config.ext1_trigger_mode = rtc_gpio_wakeup_mode_mask;
  1285. s_config.wakeup_triggers |= RTC_EXT1_TRIG_EN;
  1286. return ESP_OK;
  1287. }
  1288. #endif
  1289. static void ext1_wakeup_prepare(void)
  1290. {
  1291. // Configure all RTC IOs selected as ext1 wakeup inputs
  1292. uint32_t rtc_gpio_mask = s_config.ext1_rtc_gpio_mask;
  1293. for (int gpio = 0; gpio < GPIO_PIN_COUNT && rtc_gpio_mask != 0; ++gpio) {
  1294. int rtc_pin = rtc_io_number_get(gpio);
  1295. if ((rtc_gpio_mask & BIT(rtc_pin)) == 0) {
  1296. continue;
  1297. }
  1298. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  1299. // Route pad to RTC
  1300. rtcio_hal_function_select(rtc_pin, RTCIO_FUNC_RTC);
  1301. // set input enable in sleep mode
  1302. rtcio_hal_input_enable(rtc_pin);
  1303. #if SOC_PM_SUPPORT_RTC_PERIPH_PD
  1304. // Pad configuration depends on RTC_PERIPH state in sleep mode
  1305. if (s_config.domain[ESP_PD_DOMAIN_RTC_PERIPH].pd_option != ESP_PD_OPTION_ON) {
  1306. rtcio_hal_hold_enable(rtc_pin);
  1307. }
  1308. #endif
  1309. #else
  1310. /* ESP32H2 use hp iomux to config rtcio, and there is no complete
  1311. * rtcio functionality. In the case of EXT1 wakeup, rtcio only provides
  1312. * a pathway to EXT1. */
  1313. // Route pad to DIGITAL
  1314. rtcio_hal_function_select(rtc_pin, RTCIO_FUNC_DIGITAL);
  1315. // set input enable
  1316. gpio_ll_input_enable(&GPIO, gpio);
  1317. // hold rtc_pin to use it during sleep state
  1318. rtcio_hal_hold_enable(rtc_pin);
  1319. #endif
  1320. // Keep track of pins which are processed to bail out early
  1321. rtc_gpio_mask &= ~BIT(rtc_pin);
  1322. }
  1323. // Clear state from previous wakeup
  1324. rtc_hal_ext1_clear_wakeup_status();
  1325. // Set RTC IO pins and mode to be used for wakeup
  1326. rtc_hal_ext1_set_wakeup_pins(s_config.ext1_rtc_gpio_mask, s_config.ext1_trigger_mode);
  1327. }
  1328. uint64_t esp_sleep_get_ext1_wakeup_status(void)
  1329. {
  1330. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_EXT1) {
  1331. return 0;
  1332. }
  1333. uint32_t status = rtc_hal_ext1_get_wakeup_status();
  1334. // Translate bit map of RTC IO numbers into the bit map of GPIO numbers
  1335. uint64_t gpio_mask = 0;
  1336. for (int gpio = 0; gpio < GPIO_PIN_COUNT; ++gpio) {
  1337. if (!esp_sleep_is_valid_wakeup_gpio(gpio)) {
  1338. continue;
  1339. }
  1340. int rtc_pin = rtc_io_number_get(gpio);
  1341. if ((status & BIT(rtc_pin)) == 0) {
  1342. continue;
  1343. }
  1344. gpio_mask |= 1ULL << gpio;
  1345. }
  1346. return gpio_mask;
  1347. }
  1348. #endif // SOC_PM_SUPPORT_EXT1_WAKEUP
  1349. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  1350. uint64_t esp_sleep_get_gpio_wakeup_status(void)
  1351. {
  1352. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_GPIO) {
  1353. return 0;
  1354. }
  1355. return rtc_hal_gpio_get_wakeup_status();
  1356. }
  1357. static void gpio_deep_sleep_wakeup_prepare(void)
  1358. {
  1359. for (gpio_num_t gpio_idx = GPIO_NUM_0; gpio_idx < GPIO_NUM_MAX; gpio_idx++) {
  1360. if (((1ULL << gpio_idx) & s_config.gpio_wakeup_mask) == 0) {
  1361. continue;
  1362. }
  1363. if (s_config.gpio_trigger_mode & BIT(gpio_idx)) {
  1364. ESP_ERROR_CHECK(gpio_pullup_dis(gpio_idx));
  1365. ESP_ERROR_CHECK(gpio_pulldown_en(gpio_idx));
  1366. } else {
  1367. ESP_ERROR_CHECK(gpio_pullup_en(gpio_idx));
  1368. ESP_ERROR_CHECK(gpio_pulldown_dis(gpio_idx));
  1369. }
  1370. ESP_ERROR_CHECK(gpio_hold_en(gpio_idx));
  1371. }
  1372. // Clear state from previous wakeup
  1373. rtc_hal_gpio_clear_wakeup_status();
  1374. }
  1375. esp_err_t esp_deep_sleep_enable_gpio_wakeup(uint64_t gpio_pin_mask, esp_deepsleep_gpio_wake_up_mode_t mode)
  1376. {
  1377. if (mode > ESP_GPIO_WAKEUP_GPIO_HIGH) {
  1378. ESP_LOGE(TAG, "invalid mode");
  1379. return ESP_ERR_INVALID_ARG;
  1380. }
  1381. gpio_int_type_t intr_type = ((mode == ESP_GPIO_WAKEUP_GPIO_LOW) ? GPIO_INTR_LOW_LEVEL : GPIO_INTR_HIGH_LEVEL);
  1382. esp_err_t err = ESP_OK;
  1383. for (gpio_num_t gpio_idx = GPIO_NUM_0; gpio_idx < GPIO_NUM_MAX; gpio_idx++, gpio_pin_mask >>= 1) {
  1384. if ((gpio_pin_mask & 1) == 0) {
  1385. continue;
  1386. }
  1387. if (!esp_sleep_is_valid_wakeup_gpio(gpio_idx)) {
  1388. ESP_LOGE(TAG, "gpio %d is an invalid deep sleep wakeup IO", gpio_idx);
  1389. return ESP_ERR_INVALID_ARG;
  1390. }
  1391. err = gpio_deep_sleep_wakeup_enable(gpio_idx, intr_type);
  1392. s_config.gpio_wakeup_mask |= BIT(gpio_idx);
  1393. if (mode == ESP_GPIO_WAKEUP_GPIO_HIGH) {
  1394. s_config.gpio_trigger_mode |= (mode << gpio_idx);
  1395. } else {
  1396. s_config.gpio_trigger_mode &= ~(mode << gpio_idx);
  1397. }
  1398. }
  1399. s_config.wakeup_triggers |= RTC_GPIO_TRIG_EN;
  1400. return err;
  1401. }
  1402. #endif //SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  1403. esp_err_t esp_sleep_enable_gpio_wakeup(void)
  1404. {
  1405. #if CONFIG_IDF_TARGET_ESP32
  1406. if (s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) {
  1407. ESP_LOGE(TAG, "Conflicting wake-up triggers: touch / ULP");
  1408. return ESP_ERR_INVALID_STATE;
  1409. }
  1410. #endif
  1411. s_config.wakeup_triggers |= RTC_GPIO_TRIG_EN;
  1412. return ESP_OK;
  1413. }
  1414. esp_err_t esp_sleep_enable_uart_wakeup(int uart_num)
  1415. {
  1416. if (uart_num == UART_NUM_0) {
  1417. s_config.wakeup_triggers |= RTC_UART0_TRIG_EN;
  1418. } else if (uart_num == UART_NUM_1) {
  1419. s_config.wakeup_triggers |= RTC_UART1_TRIG_EN;
  1420. } else {
  1421. return ESP_ERR_INVALID_ARG;
  1422. }
  1423. return ESP_OK;
  1424. }
  1425. esp_err_t esp_sleep_enable_wifi_wakeup(void)
  1426. {
  1427. #if SOC_PM_SUPPORT_WIFI_WAKEUP
  1428. s_config.wakeup_triggers |= RTC_WIFI_TRIG_EN;
  1429. return ESP_OK;
  1430. #else
  1431. return ESP_ERR_NOT_SUPPORTED;
  1432. #endif
  1433. }
  1434. esp_err_t esp_sleep_disable_wifi_wakeup(void)
  1435. {
  1436. #if SOC_PM_SUPPORT_WIFI_WAKEUP
  1437. s_config.wakeup_triggers &= (~RTC_WIFI_TRIG_EN);
  1438. return ESP_OK;
  1439. #else
  1440. return ESP_ERR_NOT_SUPPORTED;
  1441. #endif
  1442. }
  1443. esp_err_t esp_sleep_enable_wifi_beacon_wakeup(void)
  1444. {
  1445. #if SOC_PM_SUPPORT_BEACON_WAKEUP
  1446. s_config.wakeup_triggers |= PMU_WIFI_BEACON_WAKEUP_EN;
  1447. return ESP_OK;
  1448. #else
  1449. return ESP_ERR_NOT_SUPPORTED;
  1450. #endif
  1451. }
  1452. esp_err_t esp_sleep_disable_wifi_beacon_wakeup(void)
  1453. {
  1454. #if SOC_PM_SUPPORT_BEACON_WAKEUP
  1455. s_config.wakeup_triggers &= (~PMU_WIFI_BEACON_WAKEUP_EN);
  1456. return ESP_OK;
  1457. #else
  1458. return ESP_ERR_NOT_SUPPORTED;
  1459. #endif
  1460. }
  1461. esp_err_t esp_sleep_enable_bt_wakeup(void)
  1462. {
  1463. #if SOC_PM_SUPPORT_BT_WAKEUP
  1464. s_config.wakeup_triggers |= RTC_BT_TRIG_EN;
  1465. return ESP_OK;
  1466. #else
  1467. return ESP_ERR_NOT_SUPPORTED;
  1468. #endif
  1469. }
  1470. esp_err_t esp_sleep_disable_bt_wakeup(void)
  1471. {
  1472. #if SOC_PM_SUPPORT_BT_WAKEUP
  1473. s_config.wakeup_triggers &= (~RTC_BT_TRIG_EN);
  1474. return ESP_OK;
  1475. #else
  1476. return ESP_ERR_NOT_SUPPORTED;
  1477. #endif
  1478. }
  1479. esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause(void)
  1480. {
  1481. if (esp_rom_get_reset_reason(0) != RESET_REASON_CORE_DEEP_SLEEP && !s_light_sleep_wakeup) {
  1482. return ESP_SLEEP_WAKEUP_UNDEFINED;
  1483. }
  1484. #if SOC_PMU_SUPPORTED
  1485. uint32_t wakeup_cause = pmu_ll_hp_get_wakeup_cause(&PMU);
  1486. #else
  1487. uint32_t wakeup_cause = rtc_cntl_ll_get_wakeup_cause();
  1488. #endif
  1489. if (wakeup_cause & RTC_TIMER_TRIG_EN) {
  1490. return ESP_SLEEP_WAKEUP_TIMER;
  1491. } else if (wakeup_cause & RTC_GPIO_TRIG_EN) {
  1492. return ESP_SLEEP_WAKEUP_GPIO;
  1493. } else if (wakeup_cause & (RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN)) {
  1494. return ESP_SLEEP_WAKEUP_UART;
  1495. #if SOC_PM_SUPPORT_EXT0_WAKEUP
  1496. } else if (wakeup_cause & RTC_EXT0_TRIG_EN) {
  1497. return ESP_SLEEP_WAKEUP_EXT0;
  1498. #endif
  1499. #if SOC_PM_SUPPORT_EXT1_WAKEUP
  1500. } else if (wakeup_cause & RTC_EXT1_TRIG_EN) {
  1501. return ESP_SLEEP_WAKEUP_EXT1;
  1502. #endif
  1503. #if SOC_PM_SUPPORT_TOUCH_SENSOR_WAKEUP
  1504. } else if (wakeup_cause & RTC_TOUCH_TRIG_EN) {
  1505. return ESP_SLEEP_WAKEUP_TOUCHPAD;
  1506. #endif
  1507. #if SOC_ULP_FSM_SUPPORTED
  1508. } else if (wakeup_cause & RTC_ULP_TRIG_EN) {
  1509. return ESP_SLEEP_WAKEUP_ULP;
  1510. #endif
  1511. #if SOC_PM_SUPPORT_WIFI_WAKEUP
  1512. } else if (wakeup_cause & RTC_WIFI_TRIG_EN) {
  1513. return ESP_SLEEP_WAKEUP_WIFI;
  1514. #endif
  1515. #if SOC_PM_SUPPORT_BT_WAKEUP
  1516. } else if (wakeup_cause & RTC_BT_TRIG_EN) {
  1517. return ESP_SLEEP_WAKEUP_BT;
  1518. #endif
  1519. #if SOC_RISCV_COPROC_SUPPORTED
  1520. } else if (wakeup_cause & RTC_COCPU_TRIG_EN) {
  1521. return ESP_SLEEP_WAKEUP_ULP;
  1522. } else if (wakeup_cause & RTC_COCPU_TRAP_TRIG_EN) {
  1523. return ESP_SLEEP_WAKEUP_COCPU_TRAP_TRIG;
  1524. #endif
  1525. #if SOC_LP_CORE_SUPPORTED
  1526. } else if (wakeup_cause & RTC_LP_CORE_TRIG_EN) {
  1527. return ESP_SLEEP_WAKEUP_ULP;
  1528. #endif
  1529. } else {
  1530. return ESP_SLEEP_WAKEUP_UNDEFINED;
  1531. }
  1532. }
  1533. esp_err_t esp_sleep_pd_config(esp_sleep_pd_domain_t domain, esp_sleep_pd_option_t option)
  1534. {
  1535. if (domain >= ESP_PD_DOMAIN_MAX || option > ESP_PD_OPTION_AUTO) {
  1536. return ESP_ERR_INVALID_ARG;
  1537. }
  1538. portENTER_CRITICAL_SAFE(&s_config.lock);
  1539. int refs = (option == ESP_PD_OPTION_ON) ? s_config.domain[domain].refs++ \
  1540. : (option == ESP_PD_OPTION_OFF) ? --s_config.domain[domain].refs \
  1541. : s_config.domain[domain].refs;
  1542. if (refs == 0) {
  1543. s_config.domain[domain].pd_option = option;
  1544. }
  1545. portEXIT_CRITICAL_SAFE(&s_config.lock);
  1546. assert(refs >= 0);
  1547. return ESP_OK;
  1548. }
  1549. /**
  1550. * The modules in the CPU and modem power domains still depend on the top power domain.
  1551. * To be safe, the CPU and Modem power domains must also be powered off and saved when
  1552. * the TOP is powered off. If not power down XTAL, power down TOP is meaningless, and
  1553. * the XTAL clock control of some chips(esp32c6/esp32h2) depends on the top domain.
  1554. */
  1555. #if SOC_PM_SUPPORT_TOP_PD
  1556. FORCE_INLINE_ATTR bool top_domain_pd_allowed(void) {
  1557. return (cpu_domain_pd_allowed() && \
  1558. clock_domain_pd_allowed() && \
  1559. peripheral_domain_pd_allowed() && \
  1560. modem_domain_pd_allowed() && \
  1561. s_config.domain[ESP_PD_DOMAIN_XTAL].pd_option != ESP_PD_OPTION_ON);
  1562. }
  1563. #endif
  1564. static uint32_t get_power_down_flags(void)
  1565. {
  1566. // Where needed, convert AUTO options to ON. Later interpret AUTO as OFF.
  1567. // RTC_SLOW_MEM is needed for the ULP, so keep RTC_SLOW_MEM powered up if ULP
  1568. // is used and RTC_SLOW_MEM is Auto.
  1569. // If there is any data placed into .rtc.data or .rtc.bss segments, and
  1570. // RTC_SLOW_MEM is Auto, keep it powered up as well.
  1571. #if SOC_PM_SUPPORT_RTC_SLOW_MEM_PD && SOC_ULP_SUPPORTED
  1572. // Labels are defined in the linker script
  1573. extern int _rtc_slow_length, _rtc_reserved_length;
  1574. /**
  1575. * Compiler considers "(size_t) &_rtc_slow_length > 0" to always be true.
  1576. * So use a volatile variable to prevent compiler from doing this optimization.
  1577. */
  1578. volatile size_t rtc_slow_mem_used = (size_t)&_rtc_slow_length + (size_t)&_rtc_reserved_length;
  1579. if ((s_config.domain[ESP_PD_DOMAIN_RTC_SLOW_MEM].pd_option == ESP_PD_OPTION_AUTO) &&
  1580. (rtc_slow_mem_used > 0 || (s_config.wakeup_triggers & RTC_ULP_TRIG_EN))) {
  1581. s_config.domain[ESP_PD_DOMAIN_RTC_SLOW_MEM].pd_option = ESP_PD_OPTION_ON;
  1582. }
  1583. #endif
  1584. #if SOC_PM_SUPPORT_RTC_FAST_MEM_PD
  1585. #if !CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  1586. /* RTC_FAST_MEM is needed for deep sleep stub.
  1587. If RTC_FAST_MEM is Auto, keep it powered on, so that deep sleep stub can run.
  1588. In the new chip revision, deep sleep stub will be optional, and this can be changed. */
  1589. if (s_config.domain[ESP_PD_DOMAIN_RTC_FAST_MEM].pd_option == ESP_PD_OPTION_AUTO) {
  1590. s_config.domain[ESP_PD_DOMAIN_RTC_FAST_MEM].pd_option = ESP_PD_OPTION_ON;
  1591. }
  1592. #else
  1593. /* If RTC_FAST_MEM is used for heap, force RTC_FAST_MEM to be powered on. */
  1594. s_config.domain[ESP_PD_DOMAIN_RTC_FAST_MEM].pd_option = ESP_PD_OPTION_ON;
  1595. #endif
  1596. #endif
  1597. #if SOC_PM_SUPPORT_RTC_PERIPH_PD
  1598. // RTC_PERIPH is needed for EXT0 wakeup and GPIO wakeup.
  1599. // If RTC_PERIPH is left auto (EXT0/GPIO aren't enabled), RTC_PERIPH will be powered off by default.
  1600. if (s_config.domain[ESP_PD_DOMAIN_RTC_PERIPH].pd_option == ESP_PD_OPTION_AUTO) {
  1601. if (s_config.wakeup_triggers & (RTC_EXT0_TRIG_EN | RTC_GPIO_TRIG_EN)) {
  1602. s_config.domain[ESP_PD_DOMAIN_RTC_PERIPH].pd_option = ESP_PD_OPTION_ON;
  1603. }
  1604. #if CONFIG_IDF_TARGET_ESP32
  1605. else if (s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) {
  1606. // On ESP32, forcing power up of RTC_PERIPH
  1607. // prevents ULP timer and touch FSMs from working correctly.
  1608. s_config.domain[ESP_PD_DOMAIN_RTC_PERIPH].pd_option = ESP_PD_OPTION_OFF;
  1609. }
  1610. #endif //CONFIG_IDF_TARGET_ESP32
  1611. #if SOC_LP_CORE_SUPPORTED
  1612. else if (s_config.wakeup_triggers & RTC_LP_CORE_TRIG_EN) {
  1613. // Need to keep RTC_PERIPH on to allow lp core to wakeup during sleep (e.g. from lp timer)
  1614. s_config.domain[ESP_PD_DOMAIN_RTC_PERIPH].pd_option = ESP_PD_OPTION_ON;
  1615. }
  1616. #endif //CONFIG_IDF_TARGET_ESP32
  1617. }
  1618. #endif // SOC_PM_SUPPORT_RTC_PERIPH_PD
  1619. /**
  1620. * VDD_SDIO power domain shall be kept on during the light sleep
  1621. * when CONFIG_ESP_SLEEP_POWER_DOWN_FLASH is not set and off when it is set.
  1622. * The application can still force the power domain to remain on by calling
  1623. * `esp_sleep_pd_config` before getting into light sleep mode.
  1624. *
  1625. * In deep sleep mode, the power domain will be turned off, regardless the
  1626. * value of this field.
  1627. */
  1628. #if SOC_PM_SUPPORT_VDDSDIO_PD
  1629. if (s_config.domain[ESP_PD_DOMAIN_VDDSDIO].pd_option == ESP_PD_OPTION_AUTO) {
  1630. #ifndef CONFIG_ESP_SLEEP_POWER_DOWN_FLASH
  1631. s_config.domain[ESP_PD_DOMAIN_VDDSDIO].pd_option = ESP_PD_OPTION_ON;
  1632. #endif
  1633. }
  1634. #endif
  1635. #ifdef CONFIG_IDF_TARGET_ESP32
  1636. s_config.domain[ESP_PD_DOMAIN_XTAL].pd_option = ESP_PD_OPTION_OFF;
  1637. #endif
  1638. const __attribute__((unused)) char *option_str[] = {DRAM_STR("OFF"), DRAM_STR("ON"), DRAM_STR("AUTO(OFF)") /* Auto works as OFF */};
  1639. /* This function is called from a critical section, log with ESP_EARLY_LOGD. */
  1640. #if SOC_PM_SUPPORT_RTC_PERIPH_PD
  1641. ESP_EARLY_LOGD(TAG, "RTC_PERIPH: %s", option_str[s_config.domain[ESP_PD_DOMAIN_RTC_PERIPH].pd_option]);
  1642. #endif
  1643. #if SOC_PM_SUPPORT_RTC_SLOW_MEM_PD
  1644. ESP_EARLY_LOGD(TAG, "RTC_SLOW_MEM: %s", option_str[s_config.domain[ESP_PD_DOMAIN_RTC_SLOW_MEM].pd_option]);
  1645. #endif
  1646. #if SOC_PM_SUPPORT_RTC_FAST_MEM_PD
  1647. ESP_EARLY_LOGD(TAG, "RTC_FAST_MEM: %s", option_str[s_config.domain[ESP_PD_DOMAIN_RTC_FAST_MEM].pd_option]);
  1648. #endif
  1649. // Prepare flags based on the selected options
  1650. uint32_t pd_flags = 0;
  1651. #if SOC_PM_SUPPORT_RTC_FAST_MEM_PD
  1652. if (s_config.domain[ESP_PD_DOMAIN_RTC_FAST_MEM].pd_option != ESP_PD_OPTION_ON) {
  1653. pd_flags |= RTC_SLEEP_PD_RTC_FAST_MEM;
  1654. }
  1655. #endif
  1656. #if SOC_PM_SUPPORT_RTC_SLOW_MEM_PD
  1657. if (s_config.domain[ESP_PD_DOMAIN_RTC_SLOW_MEM].pd_option != ESP_PD_OPTION_ON) {
  1658. pd_flags |= RTC_SLEEP_PD_RTC_SLOW_MEM;
  1659. }
  1660. #endif
  1661. #if SOC_PM_SUPPORT_RTC_PERIPH_PD
  1662. if (s_config.domain[ESP_PD_DOMAIN_RTC_PERIPH].pd_option != ESP_PD_OPTION_ON) {
  1663. pd_flags |= RTC_SLEEP_PD_RTC_PERIPH;
  1664. }
  1665. #endif
  1666. #if SOC_PM_SUPPORT_CPU_PD
  1667. if ((s_config.domain[ESP_PD_DOMAIN_CPU].pd_option != ESP_PD_OPTION_ON) && cpu_domain_pd_allowed()) {
  1668. pd_flags |= RTC_SLEEP_PD_CPU;
  1669. }
  1670. #endif
  1671. #if SOC_PM_SUPPORT_XTAL32K_PD
  1672. if (s_config.domain[ESP_PD_DOMAIN_XTAL32K].pd_option != ESP_PD_OPTION_ON) {
  1673. pd_flags |= PMU_SLEEP_PD_XTAL32K;
  1674. }
  1675. #endif
  1676. #if SOC_PM_SUPPORT_RC32K_PD
  1677. if (s_config.domain[ESP_PD_DOMAIN_RC32K].pd_option != ESP_PD_OPTION_ON) {
  1678. pd_flags |= PMU_SLEEP_PD_RC32K;
  1679. }
  1680. #endif
  1681. #if SOC_PM_SUPPORT_RC_FAST_PD
  1682. if (s_config.domain[ESP_PD_DOMAIN_RC_FAST].pd_option != ESP_PD_OPTION_ON) {
  1683. pd_flags |= RTC_SLEEP_PD_INT_8M;
  1684. }
  1685. #endif
  1686. if (s_config.domain[ESP_PD_DOMAIN_XTAL].pd_option != ESP_PD_OPTION_ON) {
  1687. pd_flags |= RTC_SLEEP_PD_XTAL;
  1688. }
  1689. #if SOC_PM_SUPPORT_TOP_PD
  1690. if ((s_config.domain[ESP_PD_DOMAIN_TOP].pd_option != ESP_PD_OPTION_ON) && top_domain_pd_allowed()) {
  1691. pd_flags |= PMU_SLEEP_PD_TOP;
  1692. }
  1693. #endif
  1694. #if SOC_PM_SUPPORT_MODEM_PD
  1695. if ((s_config.domain[ESP_PD_DOMAIN_MODEM].pd_option != ESP_PD_OPTION_ON) && modem_domain_pd_allowed()) {
  1696. pd_flags |= RTC_SLEEP_PD_MODEM;
  1697. }
  1698. #endif
  1699. #if SOC_PM_SUPPORT_VDDSDIO_PD
  1700. if (s_config.domain[ESP_PD_DOMAIN_VDDSDIO].pd_option != ESP_PD_OPTION_ON) {
  1701. pd_flags |= RTC_SLEEP_PD_VDDSDIO;
  1702. }
  1703. #endif
  1704. #if ((defined CONFIG_RTC_CLK_SRC_EXT_CRYS) && (defined CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT) && (SOC_PM_SUPPORT_RTC_PERIPH_PD))
  1705. if ((s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) == 0) {
  1706. // If enabled EXT1 only and enable the additional current by touch, should be keep RTC_PERIPH power on.
  1707. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  1708. }
  1709. #endif
  1710. return pd_flags;
  1711. }
  1712. #if CONFIG_IDF_TARGET_ESP32
  1713. /* APP core of esp32 can't access to RTC FAST MEMORY, do not define it with RTC_IRAM_ATTR */
  1714. void
  1715. #else
  1716. void RTC_IRAM_ATTR
  1717. #endif
  1718. esp_deep_sleep_disable_rom_logging(void)
  1719. {
  1720. rtc_suppress_rom_log();
  1721. }
  1722. void esp_sleep_enable_adc_tsens_monitor(bool enable)
  1723. {
  1724. s_adc_tsen_enabled = enable;
  1725. }
  1726. void rtc_sleep_enable_ultra_low(bool enable)
  1727. {
  1728. s_ultra_low_enabled = enable;
  1729. }