sleep_modes.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stddef.h>
  15. #include <string.h>
  16. #include <sys/lock.h>
  17. #include <sys/param.h>
  18. #include "esp_attr.h"
  19. #include "esp_sleep.h"
  20. #include "esp_private/esp_timer_private.h"
  21. #include "esp_private/system_internal.h"
  22. #include "esp_log.h"
  23. #include "esp_newlib.h"
  24. #include "esp_timer.h"
  25. #include "freertos/FreeRTOS.h"
  26. #include "freertos/task.h"
  27. #include "soc/soc_caps.h"
  28. #include "driver/rtc_io.h"
  29. #include "hal/rtc_io_hal.h"
  30. #include "driver/uart.h"
  31. #include "soc/cpu.h"
  32. #include "soc/rtc.h"
  33. #include "soc/soc_caps.h"
  34. #include "hal/wdt_hal.h"
  35. #include "hal/rtc_hal.h"
  36. #include "hal/uart_hal.h"
  37. #if SOC_TOUCH_SENSOR_NUM > 0
  38. #include "hal/touch_sensor_hal.h"
  39. #include "driver/touch_sensor.h"
  40. #include "driver/touch_sensor_common.h"
  41. #endif
  42. #include "hal/clk_gate_ll.h"
  43. #include "sdkconfig.h"
  44. #include "esp_rom_uart.h"
  45. #ifdef CONFIG_IDF_TARGET_ESP32
  46. #include "esp32/rom/cache.h"
  47. #include "esp32/clk.h"
  48. #include "esp32/rom/rtc.h"
  49. #include "driver/gpio.h"
  50. #elif CONFIG_IDF_TARGET_ESP32S2
  51. #include "esp32s2/clk.h"
  52. #include "esp32s2/rom/cache.h"
  53. #include "esp32s2/rom/rtc.h"
  54. #include "soc/extmem_reg.h"
  55. #include "driver/gpio.h"
  56. #elif CONFIG_IDF_TARGET_ESP32S3
  57. #include "esp32s3/clk.h"
  58. #include "esp32s3/rom/cache.h"
  59. #include "esp32s3/rom/rtc.h"
  60. #include "soc/extmem_reg.h"
  61. #elif CONFIG_IDF_TARGET_ESP32C3
  62. #include "esp32c3/clk.h"
  63. #include "esp32s3/rom/cache.h"
  64. #include "esp32c3/rom/rtc.h"
  65. #include "soc/extmem_reg.h"
  66. #endif
  67. // If light sleep time is less than that, don't power down flash
  68. #define FLASH_PD_MIN_SLEEP_TIME_US 2000
  69. // Time from VDD_SDIO power up to first flash read in ROM code
  70. #define VDD_SDIO_POWERUP_TO_FLASH_READ_US 700
  71. // Cycles for RTC Timer clock source (internal oscillator) calibrate
  72. #define RTC_CLK_SRC_CAL_CYCLES (10)
  73. #ifdef CONFIG_IDF_TARGET_ESP32
  74. #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ
  75. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (212)
  76. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (60)
  77. #elif CONFIG_IDF_TARGET_ESP32S2
  78. #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S2_DEFAULT_CPU_FREQ_MHZ
  79. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (147)
  80. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (28)
  81. #elif CONFIG_IDF_TARGET_ESP32S3
  82. #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ
  83. #define DEFAULT_SLEEP_OUT_OVERHEAD_US (0)
  84. #define DEFAULT_HARDWARE_OUT_OVERHEAD_US (0)
  85. #elif CONFIG_IDF_TARGET_ESP32C3
  86. #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32C3_DEFAULT_CPU_FREQ_MHZ
  87. #endif
  88. #if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2)
  89. #define LIGHT_SLEEP_TIME_OVERHEAD_US DEFAULT_HARDWARE_OUT_OVERHEAD_US
  90. #if defined(CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS) || defined (CONFIG_ESP32S2_RTC_CLK_SRC_EXT_CRYS)
  91. #define DEEP_SLEEP_TIME_OVERHEAD_US (650 + 100 * 240 / DEFAULT_CPU_FREQ_MHZ)
  92. #else
  93. #define DEEP_SLEEP_TIME_OVERHEAD_US (250 + 100 * 240 / DEFAULT_CPU_FREQ_MHZ)
  94. #endif // defined(CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS) || defined (CONFIG_ESP32S2_RTC_CLK_SRC_EXT_CRYS)
  95. #elif defined(CONFIG_IDF_TARGET_ESP32C3)
  96. #ifdef CONFIG_ESP32C3_RTC_CLK_SRC_EXT_CRYS
  97. #define LIGHT_SLEEP_TIME_OVERHEAD_US (650 + 30 * 240 / CONFIG_ESP32C3_DEFAULT_CPU_FREQ_MHZ)
  98. #define DEEP_SLEEP_TIME_OVERHEAD_US (650 + 100 * 240 / CONFIG_ESP32C3_DEFAULT_CPU_FREQ_MHZ)
  99. #else
  100. #define LIGHT_SLEEP_TIME_OVERHEAD_US (250 + 30 * 240 / CONFIG_ESP32C3_DEFAULT_CPU_FREQ_MHZ)
  101. #define DEEP_SLEEP_TIME_OVERHEAD_US (250 + 100 * 240 / CONFIG_ESP32C3_DEFAULT_CPU_FREQ_MHZ)
  102. #endif // CONFIG_ESP32C3_RTC_CLK_SRC_EXT_CRYS
  103. #else // other target
  104. #define LIGHT_SLEEP_TIME_OVERHEAD_US 0
  105. #define DEEP_SLEEP_TIME_OVERHEAD_US 0
  106. #endif // CONFIG_IDF_TARGET_*
  107. #if defined(CONFIG_IDF_TARGET_ESP32) && defined(CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY)
  108. #define DEEP_SLEEP_WAKEUP_DELAY CONFIG_ESP32_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 : 18;
  127. uint32_t ext0_trigger_level : 1;
  128. uint32_t ext0_rtc_gpio_num : 5;
  129. uint32_t sleep_time_adjustment;
  130. uint32_t ccount_ticks_record;
  131. uint32_t sleep_time_overhead_out;
  132. uint32_t rtc_clk_cal_period;
  133. uint64_t rtc_ticks_at_sleep_start;
  134. } sleep_config_t;
  135. static sleep_config_t s_config = {
  136. .pd_options = { ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO },
  137. .ccount_ticks_record = 0,
  138. .sleep_time_overhead_out = DEFAULT_SLEEP_OUT_OVERHEAD_US,
  139. .wakeup_triggers = 0
  140. };
  141. /* Internal variable used to track if light sleep wakeup sources are to be
  142. expected when determining wakeup cause. */
  143. static bool s_light_sleep_wakeup = false;
  144. /* Updating RTC_MEMORY_CRC_REG register via set_rtc_memory_crc()
  145. is not thread-safe, so we need to disable interrupts before going to deep sleep. */
  146. static portMUX_TYPE spinlock_rtc_deep_sleep = portMUX_INITIALIZER_UNLOCKED;
  147. static const char* TAG = "sleep";
  148. static uint32_t get_power_down_flags(void);
  149. static void ext0_wakeup_prepare(void);
  150. static void ext1_wakeup_prepare(void);
  151. static void timer_wakeup_prepare(void);
  152. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  153. static void touch_wakeup_prepare(void);
  154. #endif
  155. /* Wake from deep sleep stub
  156. See esp_deepsleep.h esp_wake_deep_sleep() comments for details.
  157. */
  158. esp_deep_sleep_wake_stub_fn_t esp_get_deep_sleep_wake_stub(void)
  159. {
  160. esp_deep_sleep_wake_stub_fn_t stub_ptr = (esp_deep_sleep_wake_stub_fn_t) REG_READ(RTC_ENTRY_ADDR_REG);
  161. if (!esp_ptr_executable(stub_ptr)) {
  162. return NULL;
  163. }
  164. return stub_ptr;
  165. }
  166. void esp_set_deep_sleep_wake_stub(esp_deep_sleep_wake_stub_fn_t new_stub)
  167. {
  168. REG_WRITE(RTC_ENTRY_ADDR_REG, (uint32_t)new_stub);
  169. }
  170. void RTC_IRAM_ATTR esp_default_wake_deep_sleep(void) {
  171. /* Clear MMU for CPU 0 */
  172. #if CONFIG_IDF_TARGET_ESP32
  173. _DPORT_REG_WRITE(DPORT_PRO_CACHE_CTRL1_REG,
  174. _DPORT_REG_READ(DPORT_PRO_CACHE_CTRL1_REG) | DPORT_PRO_CACHE_MMU_IA_CLR);
  175. _DPORT_REG_WRITE(DPORT_PRO_CACHE_CTRL1_REG,
  176. _DPORT_REG_READ(DPORT_PRO_CACHE_CTRL1_REG) & (~DPORT_PRO_CACHE_MMU_IA_CLR));
  177. #if DEEP_SLEEP_WAKEUP_DELAY > 0
  178. // ROM code has not started yet, so we need to set delay factor
  179. // used by esp_rom_delay_us first.
  180. ets_update_cpu_frequency_rom(ets_get_detected_xtal_freq() / 1000000);
  181. // This delay is configured in menuconfig, it can be used to give
  182. // the flash chip some time to become ready.
  183. esp_rom_delay_us(DEEP_SLEEP_WAKEUP_DELAY);
  184. #endif
  185. #elif CONFIG_IDF_TARGET_ESP32S2
  186. REG_SET_BIT(EXTMEM_CACHE_DBG_INT_ENA_REG, EXTMEM_CACHE_DBG_EN);
  187. #endif
  188. }
  189. void __attribute__((weak, alias("esp_default_wake_deep_sleep"))) esp_wake_deep_sleep(void);
  190. void esp_deep_sleep(uint64_t time_in_us)
  191. {
  192. esp_sleep_enable_timer_wakeup(time_in_us);
  193. esp_deep_sleep_start();
  194. }
  195. // [refactor-todo] provide target logic for body of uart functions below
  196. static void IRAM_ATTR flush_uarts(void)
  197. {
  198. for (int i = 0; i < SOC_UART_NUM; ++i) {
  199. #ifdef CONFIG_IDF_TARGET_ESP32
  200. esp_rom_uart_tx_wait_idle(i);
  201. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  202. if (periph_ll_periph_enabled(PERIPH_UART0_MODULE + i)) {
  203. esp_rom_uart_tx_wait_idle(i);
  204. }
  205. #endif
  206. }
  207. }
  208. static void IRAM_ATTR suspend_uarts(void)
  209. {
  210. for (int i = 0; i < SOC_UART_NUM; ++i) {
  211. #ifdef CONFIG_IDF_TARGET_ESP32
  212. /* Note: Set `UART_FORCE_XOFF` can't stop new Tx request. */
  213. REG_SET_BIT(UART_FLOW_CONF_REG(i), UART_FORCE_XOFF);
  214. while (REG_GET_FIELD(UART_STATUS_REG(i), UART_ST_UTX_OUT) != 0) {
  215. ;
  216. }
  217. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  218. if (periph_ll_periph_enabled(PERIPH_UART0_MODULE + i)) {
  219. REG_CLR_BIT(UART_FLOW_CONF_REG(i), UART_FORCE_XON);
  220. REG_SET_BIT(UART_FLOW_CONF_REG(i), UART_SW_FLOW_CON_EN | UART_FORCE_XOFF);
  221. while (REG_GET_FIELD(UART_FSM_STATUS_REG(i), UART_ST_UTX_OUT) != 0) {
  222. ;
  223. }
  224. }
  225. #endif
  226. }
  227. }
  228. static void IRAM_ATTR resume_uarts(void)
  229. {
  230. for (int i = 0; i < SOC_UART_NUM; ++i) {
  231. #ifdef CONFIG_IDF_TARGET_ESP32
  232. REG_CLR_BIT(UART_FLOW_CONF_REG(i), UART_FORCE_XOFF);
  233. REG_SET_BIT(UART_FLOW_CONF_REG(i), UART_FORCE_XON);
  234. REG_CLR_BIT(UART_FLOW_CONF_REG(i), UART_FORCE_XON);
  235. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  236. if (periph_ll_periph_enabled(PERIPH_UART0_MODULE + i)) {
  237. REG_CLR_BIT(UART_FLOW_CONF_REG(i), UART_FORCE_XOFF);
  238. REG_SET_BIT(UART_FLOW_CONF_REG(i), UART_FORCE_XON);
  239. REG_CLR_BIT(UART_FLOW_CONF_REG(i), UART_SW_FLOW_CON_EN | UART_FORCE_XON);
  240. }
  241. #endif
  242. }
  243. }
  244. inline static uint32_t IRAM_ATTR call_rtc_sleep_start(uint32_t reject_triggers);
  245. #if SOC_GPIO_SUPPORT_SLP_SWITCH
  246. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  247. static inline void gpio_sleep_mode_config_apply(void)
  248. {
  249. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  250. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  251. gpio_sleep_pupd_config_apply(gpio_num);
  252. }
  253. }
  254. }
  255. static inline void gpio_sleep_mode_config_unapply(void)
  256. {
  257. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  258. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  259. gpio_sleep_pupd_config_unapply(gpio_num);
  260. }
  261. }
  262. }
  263. #endif
  264. void esp_sleep_gpio_status_init(void)
  265. {
  266. ESP_LOGI(TAG, "Init to disable all pins at light sleep");
  267. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  268. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  269. gpio_sleep_set_direction(gpio_num, GPIO_MODE_DISABLE);
  270. gpio_sleep_set_pull_mode(gpio_num, GPIO_FLOATING);
  271. }
  272. }
  273. }
  274. void esp_sleep_gpio_status_switch_configure(bool enable)
  275. {
  276. if (enable) {
  277. ESP_LOGI(TAG, "Light sleep enabled, start GPIO status switching");
  278. } else {
  279. ESP_LOGI(TAG, "Light sleep disabled, stop GPIO status switching");
  280. }
  281. for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
  282. if (GPIO_IS_VALID_GPIO(gpio_num)) {
  283. if (enable) {
  284. gpio_sleep_sel_en(gpio_num);
  285. } else {
  286. gpio_sleep_sel_dis(gpio_num);
  287. }
  288. }
  289. }
  290. }
  291. #endif
  292. static uint32_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags)
  293. {
  294. // Stop UART output so that output is not lost due to APB frequency change.
  295. // For light sleep, suspend UART output — it will resume after wakeup.
  296. // For deep sleep, wait for the contents of UART FIFO to be sent.
  297. bool deep_sleep = pd_flags & RTC_SLEEP_PD_DIG;
  298. #if !CONFIG_FREERTOS_UNICORE && CONFIG_IDF_TARGET_ESP32S3 && CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  299. /* Currently only safe to use deep sleep wake stub & RTC memory as heap in single core mode.
  300. For ESP32-S3, either disable ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP in config or find a way to set the
  301. deep sleep wake stub to NULL.
  302. */
  303. assert(!deep_sleep || esp_get_deep_sleep_wake_stub() == NULL);
  304. #endif
  305. if (deep_sleep) {
  306. flush_uarts();
  307. } else {
  308. suspend_uarts();
  309. }
  310. // Save current frequency and switch to XTAL
  311. rtc_cpu_freq_config_t cpu_freq_config;
  312. rtc_clk_cpu_freq_get_config(&cpu_freq_config);
  313. rtc_clk_cpu_freq_set_xtal();
  314. // Configure pins for external wakeup
  315. if (s_config.wakeup_triggers & RTC_EXT0_TRIG_EN) {
  316. ext0_wakeup_prepare();
  317. }
  318. if (s_config.wakeup_triggers & RTC_EXT1_TRIG_EN) {
  319. ext1_wakeup_prepare();
  320. }
  321. #ifdef CONFIG_IDF_TARGET_ESP32
  322. // Enable ULP wakeup
  323. if (s_config.wakeup_triggers & RTC_ULP_TRIG_EN) {
  324. rtc_hal_ulp_wakeup_enable();
  325. }
  326. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  327. gpio_sleep_mode_config_apply();
  328. #endif
  329. #endif
  330. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  331. if (deep_sleep) {
  332. if (s_config.wakeup_triggers & RTC_TOUCH_TRIG_EN) {
  333. touch_wakeup_prepare();
  334. /* Workaround: In deep sleep, for ESP32S2, Power down the RTC_PERIPH will change the slope configuration of Touch sensor sleep pad.
  335. * The configuration change will change the reading of the sleep pad, which will cause the touch wake-up sensor to trigger falsely.
  336. */
  337. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  338. }
  339. } else {
  340. /* In light sleep, the RTC_PERIPH power domain should be in the power-on state (Power on the touch circuit in light sleep),
  341. * otherwise the touch sensor FSM will be cleared, causing touch sensor false triggering.
  342. */
  343. if (touch_ll_get_fsm_state()) { // Check if the touch sensor is working properly.
  344. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  345. }
  346. }
  347. #endif
  348. uint32_t reject_triggers = 0;
  349. if ((pd_flags & RTC_SLEEP_PD_DIG) == 0 && (s_config.wakeup_triggers & RTC_GPIO_TRIG_EN)) {
  350. /* Light sleep, enable sleep reject for faster return from this function,
  351. * in case the wakeup is already triggerred.
  352. */
  353. #if CONFIG_IDF_TARGET_ESP32
  354. reject_triggers = RTC_CNTL_LIGHT_SLP_REJECT_EN_M | RTC_CNTL_GPIO_REJECT_EN_M;
  355. #elif CONFIG_IDF_TARGET_ESP32S2
  356. reject_triggers = s_config.wakeup_triggers;
  357. #endif
  358. }
  359. // Enter sleep
  360. rtc_sleep_config_t config = RTC_SLEEP_CONFIG_DEFAULT(pd_flags);
  361. rtc_sleep_init(config);
  362. // Set state machine time for light sleep
  363. if(!deep_sleep) {
  364. rtc_sleep_low_init(s_config.rtc_clk_cal_period);
  365. }
  366. // Configure timer wakeup
  367. if ((s_config.wakeup_triggers & RTC_TIMER_TRIG_EN) &&
  368. s_config.sleep_duration > 0) {
  369. timer_wakeup_prepare();
  370. }
  371. uint32_t result;
  372. if (deep_sleep) {
  373. /* Disable interrupts in case another task writes to RTC memory while we
  374. * calculate RTC memory CRC
  375. *
  376. * Note: for ESP32-S3 running in dual core mode this is currently not enough,
  377. * see the assert at top of this function.
  378. */
  379. portENTER_CRITICAL(&spinlock_rtc_deep_sleep);
  380. #if !CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  381. /* If not possible stack is in RTC FAST memory, use the ROM function to calculate the CRC and save ~140 bytes IRAM */
  382. set_rtc_memory_crc();
  383. result = call_rtc_sleep_start(reject_triggers);
  384. #else
  385. /* Otherwise, need to call the dedicated soc function for this */
  386. result = rtc_deep_sleep_start(s_config.wakeup_triggers, reject_triggers);
  387. #endif
  388. portEXIT_CRITICAL(&spinlock_rtc_deep_sleep);
  389. } else {
  390. result = call_rtc_sleep_start(reject_triggers);
  391. }
  392. // Restore CPU frequency
  393. rtc_clk_cpu_freq_set_config(&cpu_freq_config);
  394. if (!deep_sleep) {
  395. s_config.ccount_ticks_record = cpu_ll_get_cycle_count();
  396. }
  397. #if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
  398. gpio_sleep_mode_config_unapply();
  399. #endif
  400. // re-enable UART output
  401. resume_uarts();
  402. return result;
  403. }
  404. inline static uint32_t IRAM_ATTR call_rtc_sleep_start(uint32_t reject_triggers)
  405. {
  406. #ifdef CONFIG_IDF_TARGET_ESP32
  407. return rtc_sleep_start(s_config.wakeup_triggers, reject_triggers);
  408. #else
  409. return rtc_sleep_start(s_config.wakeup_triggers, reject_triggers, 1);
  410. #endif
  411. }
  412. void IRAM_ATTR esp_deep_sleep_start(void)
  413. {
  414. // record current RTC time
  415. s_config.rtc_ticks_at_sleep_start = rtc_time_get();
  416. // record current RTC time
  417. esp_sync_counters_rtc_and_frc();
  418. // Configure wake stub
  419. if (esp_get_deep_sleep_wake_stub() == NULL) {
  420. esp_set_deep_sleep_wake_stub(esp_wake_deep_sleep);
  421. }
  422. // Decide which power domains can be powered down
  423. uint32_t pd_flags = get_power_down_flags();
  424. s_config.rtc_clk_cal_period = esp_clk_slowclk_cal_get();
  425. // Correct the sleep time
  426. s_config.sleep_time_adjustment = DEEP_SLEEP_TIME_OVERHEAD_US;
  427. // Enter sleep
  428. esp_sleep_start(RTC_SLEEP_PD_DIG | RTC_SLEEP_PD_VDDSDIO | pd_flags);
  429. // Because RTC is in a slower clock domain than the CPU, it
  430. // can take several CPU cycles for the sleep mode to start.
  431. while (1) {
  432. ;
  433. }
  434. }
  435. /**
  436. * Helper function which handles entry to and exit from light sleep
  437. * Placed into IRAM as flash may need some time to be powered on.
  438. */
  439. static esp_err_t esp_light_sleep_inner(uint32_t pd_flags,
  440. uint32_t flash_enable_time_us,
  441. rtc_vddsdio_config_t vddsdio_config) IRAM_ATTR __attribute__((noinline));
  442. static esp_err_t esp_light_sleep_inner(uint32_t pd_flags,
  443. uint32_t flash_enable_time_us,
  444. rtc_vddsdio_config_t vddsdio_config)
  445. {
  446. // Enter sleep
  447. esp_err_t err = esp_sleep_start(pd_flags);
  448. // If VDDSDIO regulator was controlled by RTC registers before sleep,
  449. // restore the configuration.
  450. if (vddsdio_config.force) {
  451. rtc_vddsdio_set_config(vddsdio_config);
  452. }
  453. // If SPI flash was powered down, wait for it to become ready
  454. if (pd_flags & RTC_SLEEP_PD_VDDSDIO) {
  455. // Wait for the flash chip to start up
  456. esp_rom_delay_us(flash_enable_time_us);
  457. }
  458. return err;
  459. }
  460. esp_err_t esp_light_sleep_start(void)
  461. {
  462. s_config.ccount_ticks_record = cpu_ll_get_cycle_count();
  463. static portMUX_TYPE light_sleep_lock = portMUX_INITIALIZER_UNLOCKED;
  464. portENTER_CRITICAL(&light_sleep_lock);
  465. /* We will be calling esp_timer_private_advance inside DPORT access critical
  466. * section. Make sure the code on the other CPU is not holding esp_timer
  467. * lock, otherwise there will be deadlock.
  468. */
  469. esp_timer_private_lock();
  470. s_config.rtc_ticks_at_sleep_start = rtc_time_get();
  471. uint32_t ccount_at_sleep_start = cpu_ll_get_cycle_count();
  472. uint64_t frc_time_at_start = esp_system_get_time();
  473. uint32_t sleep_time_overhead_in = (ccount_at_sleep_start-s_config.ccount_ticks_record) / (esp_clk_cpu_freq() / 1000000ULL);
  474. DPORT_STALL_OTHER_CPU_START();
  475. // Decide which power domains can be powered down
  476. uint32_t pd_flags = get_power_down_flags();
  477. // Re-calibrate the RTC Timer clock
  478. #if defined(CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS) || defined(CONFIG_ESP32S2_RTC_CLK_SRC_EXT_CRYS)
  479. uint64_t time_per_us = 1000000ULL;
  480. s_config.rtc_clk_cal_period = (time_per_us << RTC_CLK_CAL_FRACT) / rtc_clk_slow_freq_get_hz();
  481. #elif defined(CONFIG_ESP32S2_RTC_CLK_SRC_INT_RC)
  482. s_config.rtc_clk_cal_period = rtc_clk_cal_cycling(RTC_CAL_RTC_MUX, RTC_CLK_SRC_CAL_CYCLES);
  483. #else
  484. s_config.rtc_clk_cal_period = rtc_clk_cal(RTC_CAL_RTC_MUX, RTC_CLK_SRC_CAL_CYCLES);
  485. #endif
  486. /*
  487. * Adjustment time consists of parts below:
  488. * 1. Hardware time waiting for internal 8M oscilate clock and XTAL;
  489. * 2. Hardware state swithing time of the rtc main state machine;
  490. * 3. Code execution time when clock is not stable;
  491. * 4. Code execution time which can be measured;
  492. */
  493. 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);
  494. s_config.sleep_time_adjustment = LIGHT_SLEEP_TIME_OVERHEAD_US + sleep_time_overhead_in + s_config.sleep_time_overhead_out
  495. + 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);
  496. // Decide if VDD_SDIO needs to be powered down;
  497. // If it needs to be powered down, adjust sleep time.
  498. const uint32_t flash_enable_time_us = VDD_SDIO_POWERUP_TO_FLASH_READ_US + DEEP_SLEEP_WAKEUP_DELAY;
  499. #if CONFIG_ESP_SYSTEM_PD_FLASH
  500. /*
  501. * When SPIRAM is disabled in menuconfig, the minimum sleep time of the
  502. * system needs to meet the sum below:
  503. * 1. Wait time for the flash power-on after waking up;
  504. * 2. The execution time of codes between RTC Timer get start time
  505. * with hardware starts to switch state to sleep;
  506. * 3. The hardware state switching time of the rtc state machine during
  507. * sleep and wake-up. This process requires 6 cycles to complete.
  508. * The specific hardware state switching process and the cycles
  509. * consumed are rtc_cpu_run_stall(1), cut_pll_rtl(2), cut_8m(1),
  510. * min_protect(2);
  511. * 4. All the adjustment time which is s_config.sleep_time_adjustment below.
  512. */
  513. const uint32_t vddsdio_pd_sleep_duration = MAX(FLASH_PD_MIN_SLEEP_TIME_US,
  514. flash_enable_time_us + LIGHT_SLEEP_MIN_TIME_US + s_config.sleep_time_adjustment
  515. + rtc_time_slowclk_to_us(RTC_MODULE_SLEEP_PREPARE_CYCLES, s_config.rtc_clk_cal_period));
  516. if (s_config.sleep_duration > vddsdio_pd_sleep_duration) {
  517. pd_flags |= RTC_SLEEP_PD_VDDSDIO;
  518. if (s_config.sleep_time_overhead_out < flash_enable_time_us) {
  519. s_config.sleep_time_adjustment += flash_enable_time_us;
  520. }
  521. } else {
  522. if (s_config.sleep_time_overhead_out > flash_enable_time_us) {
  523. s_config.sleep_time_adjustment -= flash_enable_time_us;
  524. }
  525. }
  526. #endif //CONFIG_ESP_SYSTEM_PD_FLASH
  527. periph_inform_out_light_sleep_overhead(s_config.sleep_time_adjustment - sleep_time_overhead_in);
  528. rtc_vddsdio_config_t vddsdio_config = rtc_vddsdio_get_config();
  529. // Safety net: enable WDT in case exit from light sleep fails
  530. wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
  531. 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.
  532. if (!wdt_was_enabled) {
  533. wdt_hal_init(&rtc_wdt_ctx, WDT_RWDT, 0, false);
  534. uint32_t stage_timeout_ticks = (uint32_t)(1000ULL * rtc_clk_slow_freq_get_hz() / 1000ULL);
  535. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  536. wdt_hal_config_stage(&rtc_wdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_RTC);
  537. wdt_hal_enable(&rtc_wdt_ctx);
  538. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  539. }
  540. // Enter sleep, then wait for flash to be ready on wakeup
  541. esp_err_t err = esp_light_sleep_inner(pd_flags,
  542. flash_enable_time_us, vddsdio_config);
  543. s_light_sleep_wakeup = true;
  544. // FRC1 has been clock gated for the duration of the sleep, correct for that.
  545. uint64_t rtc_ticks_at_end = rtc_time_get();
  546. uint64_t frc_time_at_end = esp_system_get_time();
  547. 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);
  548. uint64_t frc_time_diff = frc_time_at_end - frc_time_at_start;
  549. int64_t time_diff = rtc_time_diff - frc_time_diff;
  550. /* Small negative values (up to 1 RTC_SLOW clock period) are possible,
  551. * for very small values of sleep_duration. Ignore those to keep esp_timer
  552. * monotonic.
  553. */
  554. if (time_diff > 0) {
  555. esp_timer_private_advance(time_diff);
  556. }
  557. esp_set_time_from_rtc();
  558. esp_timer_private_unlock();
  559. DPORT_STALL_OTHER_CPU_END();
  560. if (!wdt_was_enabled) {
  561. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  562. wdt_hal_disable(&rtc_wdt_ctx);
  563. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  564. }
  565. portEXIT_CRITICAL(&light_sleep_lock);
  566. s_config.sleep_time_overhead_out = (cpu_ll_get_cycle_count() - s_config.ccount_ticks_record) / (esp_clk_cpu_freq() / 1000000ULL);
  567. return err;
  568. }
  569. esp_err_t esp_sleep_disable_wakeup_source(esp_sleep_source_t source)
  570. {
  571. // For most of sources it is enough to set trigger mask in local
  572. // configuration structure. The actual RTC wake up options
  573. // will be updated by esp_sleep_start().
  574. if (source == ESP_SLEEP_WAKEUP_ALL) {
  575. s_config.wakeup_triggers = 0;
  576. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_TIMER, RTC_TIMER_TRIG_EN)) {
  577. s_config.wakeup_triggers &= ~RTC_TIMER_TRIG_EN;
  578. s_config.sleep_duration = 0;
  579. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_EXT0, RTC_EXT0_TRIG_EN)) {
  580. s_config.ext0_rtc_gpio_num = 0;
  581. s_config.ext0_trigger_level = 0;
  582. s_config.wakeup_triggers &= ~RTC_EXT0_TRIG_EN;
  583. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_EXT1, RTC_EXT1_TRIG_EN)) {
  584. s_config.ext1_rtc_gpio_mask = 0;
  585. s_config.ext1_trigger_mode = 0;
  586. s_config.wakeup_triggers &= ~RTC_EXT1_TRIG_EN;
  587. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_TOUCHPAD, RTC_TOUCH_TRIG_EN)) {
  588. s_config.wakeup_triggers &= ~RTC_TOUCH_TRIG_EN;
  589. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_GPIO, RTC_GPIO_TRIG_EN)) {
  590. s_config.wakeup_triggers &= ~RTC_GPIO_TRIG_EN;
  591. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_UART, (RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN))) {
  592. s_config.wakeup_triggers &= ~(RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN);
  593. }
  594. #if defined(CONFIG_ESP32_ULP_COPROC_ENABLED) || defined(CONFIG_ESP32S2_ULP_COPROC_ENABLED)
  595. else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_ULP, RTC_ULP_TRIG_EN)) {
  596. s_config.wakeup_triggers &= ~RTC_ULP_TRIG_EN;
  597. }
  598. #endif
  599. else {
  600. ESP_LOGE(TAG, "Incorrect wakeup source (%d) to disable.", (int) source);
  601. return ESP_ERR_INVALID_STATE;
  602. }
  603. return ESP_OK;
  604. }
  605. esp_err_t esp_sleep_enable_ulp_wakeup(void)
  606. {
  607. #if CONFIG_IDF_TARGET_ESP32
  608. #if ((defined CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT) || (defined CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT_V2))
  609. ESP_LOGE(TAG, "Failed to enable wakeup when provide current to external 32kHz crystal");
  610. return ESP_ERR_NOT_SUPPORTED;
  611. #endif
  612. #ifdef CONFIG_ESP32_ULP_COPROC_ENABLED
  613. if(s_config.wakeup_triggers & RTC_EXT0_TRIG_EN) {
  614. ESP_LOGE(TAG, "Conflicting wake-up trigger: ext0");
  615. return ESP_ERR_INVALID_STATE;
  616. }
  617. s_config.wakeup_triggers |= RTC_ULP_TRIG_EN;
  618. return ESP_OK;
  619. #else // CONFIG_ESP32_ULP_COPROC_ENABLED
  620. return ESP_ERR_INVALID_STATE;
  621. #endif // CONFIG_ESP32_ULP_COPROC_ENABLED
  622. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  623. s_config.wakeup_triggers |= (RTC_ULP_TRIG_EN | RTC_COCPU_TRIG_EN | RTC_COCPU_TRAP_TRIG_EN);
  624. return ESP_OK;
  625. #endif
  626. }
  627. esp_err_t esp_sleep_enable_timer_wakeup(uint64_t time_in_us)
  628. {
  629. s_config.wakeup_triggers |= RTC_TIMER_TRIG_EN;
  630. s_config.sleep_duration = time_in_us;
  631. return ESP_OK;
  632. }
  633. static void timer_wakeup_prepare(void)
  634. {
  635. int64_t sleep_duration = (int64_t) s_config.sleep_duration - (int64_t) s_config.sleep_time_adjustment;
  636. if (sleep_duration < 0) {
  637. sleep_duration = 0;
  638. }
  639. int64_t ticks = rtc_time_us_to_slowclk(sleep_duration, s_config.rtc_clk_cal_period);
  640. rtc_hal_set_wakeup_timer(s_config.rtc_ticks_at_sleep_start + ticks);
  641. }
  642. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  643. /* In deep sleep mode, only the sleep channel is supported, and other touch channels should be turned off. */
  644. static void touch_wakeup_prepare(void)
  645. {
  646. touch_pad_t touch_num = TOUCH_PAD_NUM0;
  647. touch_ll_sleep_get_channel_num(&touch_num); // Check if the sleep pad is enabled.
  648. if ((touch_num > TOUCH_PAD_NUM0) && (touch_num < TOUCH_PAD_MAX) && touch_ll_get_fsm_state()) {
  649. touch_ll_stop_fsm();
  650. touch_ll_clear_channel_mask(TOUCH_PAD_BIT_MASK_ALL);
  651. touch_ll_set_channel_mask(BIT(touch_num));
  652. touch_ll_start_fsm();
  653. }
  654. }
  655. #endif
  656. esp_err_t esp_sleep_enable_touchpad_wakeup(void)
  657. {
  658. #if ((defined CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT) || (defined CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT_V2))
  659. ESP_LOGE(TAG, "Failed to enable wakeup when provide current to external 32kHz crystal");
  660. return ESP_ERR_NOT_SUPPORTED;
  661. #endif
  662. if (s_config.wakeup_triggers & (RTC_EXT0_TRIG_EN)) {
  663. ESP_LOGE(TAG, "Conflicting wake-up trigger: ext0");
  664. return ESP_ERR_INVALID_STATE;
  665. }
  666. s_config.wakeup_triggers |= RTC_TOUCH_TRIG_EN;
  667. return ESP_OK;
  668. }
  669. touch_pad_t esp_sleep_get_touchpad_wakeup_status(void)
  670. {
  671. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_TOUCHPAD) {
  672. return TOUCH_PAD_MAX;
  673. }
  674. touch_pad_t pad_num;
  675. esp_err_t ret = touch_pad_get_wakeup_status(&pad_num); //TODO 723diff commit id:fda9ada1b
  676. assert(ret == ESP_OK && "wakeup reason is RTC_TOUCH_TRIG_EN but SENS_TOUCH_MEAS_EN is zero");
  677. return pad_num;
  678. }
  679. bool esp_sleep_is_valid_wakeup_gpio(gpio_num_t gpio_num)
  680. {
  681. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  682. return RTC_GPIO_IS_VALID_GPIO(gpio_num);
  683. #else
  684. return GPIO_IS_VALID_GPIO(gpio_num);
  685. #endif
  686. }
  687. esp_err_t esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level)
  688. {
  689. if (level < 0 || level > 1) {
  690. return ESP_ERR_INVALID_ARG;
  691. }
  692. if (!esp_sleep_is_valid_wakeup_gpio(gpio_num)) {
  693. return ESP_ERR_INVALID_ARG;
  694. }
  695. if (s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) {
  696. ESP_LOGE(TAG, "Conflicting wake-up triggers: touch / ULP");
  697. return ESP_ERR_INVALID_STATE;
  698. }
  699. s_config.ext0_rtc_gpio_num = rtc_io_number_get(gpio_num);
  700. s_config.ext0_trigger_level = level;
  701. s_config.wakeup_triggers |= RTC_EXT0_TRIG_EN;
  702. return ESP_OK;
  703. }
  704. static void ext0_wakeup_prepare(void)
  705. {
  706. int rtc_gpio_num = s_config.ext0_rtc_gpio_num;
  707. rtcio_hal_ext0_set_wakeup_pin(rtc_gpio_num, s_config.ext0_trigger_level);
  708. rtcio_hal_function_select(rtc_gpio_num, RTCIO_FUNC_RTC);
  709. rtcio_hal_input_enable(rtc_gpio_num);
  710. }
  711. esp_err_t esp_sleep_enable_ext1_wakeup(uint64_t mask, esp_sleep_ext1_wakeup_mode_t mode)
  712. {
  713. if (mode > ESP_EXT1_WAKEUP_ANY_HIGH) {
  714. return ESP_ERR_INVALID_ARG;
  715. }
  716. // Translate bit map of GPIO numbers into the bit map of RTC IO numbers
  717. uint32_t rtc_gpio_mask = 0;
  718. for (int gpio = 0; mask; ++gpio, mask >>= 1) {
  719. if ((mask & 1) == 0) {
  720. continue;
  721. }
  722. if (!esp_sleep_is_valid_wakeup_gpio(gpio)) {
  723. ESP_LOGE(TAG, "Not an RTC IO: GPIO%d", gpio);
  724. return ESP_ERR_INVALID_ARG;
  725. }
  726. rtc_gpio_mask |= BIT(rtc_io_number_get(gpio));
  727. }
  728. s_config.ext1_rtc_gpio_mask = rtc_gpio_mask;
  729. s_config.ext1_trigger_mode = mode;
  730. s_config.wakeup_triggers |= RTC_EXT1_TRIG_EN;
  731. return ESP_OK;
  732. }
  733. static void ext1_wakeup_prepare(void)
  734. {
  735. // Configure all RTC IOs selected as ext1 wakeup inputs
  736. uint32_t rtc_gpio_mask = s_config.ext1_rtc_gpio_mask;
  737. for (int gpio = 0; gpio < GPIO_PIN_COUNT && rtc_gpio_mask != 0; ++gpio) {
  738. int rtc_pin = rtc_io_number_get(gpio);
  739. if ((rtc_gpio_mask & BIT(rtc_pin)) == 0) {
  740. continue;
  741. }
  742. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  743. // Route pad to RTC
  744. rtcio_hal_function_select(rtc_pin, RTCIO_FUNC_RTC);
  745. // set input enable in sleep mode
  746. rtcio_hal_input_enable(rtc_pin);
  747. #endif
  748. // Pad configuration depends on RTC_PERIPH state in sleep mode
  749. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] != ESP_PD_OPTION_ON) {
  750. #if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
  751. // RTC_PERIPH will be powered down, so RTC_IO_ registers will
  752. // loose their state. Lock pad configuration.
  753. // Pullups/pulldowns also need to be disabled.
  754. rtcio_hal_pullup_disable(rtc_pin);
  755. rtcio_hal_pulldown_disable(rtc_pin);
  756. #endif
  757. rtcio_hal_hold_enable(rtc_pin);
  758. }
  759. // Keep track of pins which are processed to bail out early
  760. rtc_gpio_mask &= ~BIT(rtc_pin);
  761. }
  762. // Clear state from previous wakeup
  763. rtc_hal_ext1_clear_wakeup_pins();
  764. // Set RTC IO pins and mode (any high, all low) to be used for wakeup
  765. rtc_hal_ext1_set_wakeup_pins(s_config.ext1_rtc_gpio_mask, s_config.ext1_trigger_mode);
  766. }
  767. uint64_t esp_sleep_get_ext1_wakeup_status(void)
  768. {
  769. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_EXT1) {
  770. return 0;
  771. }
  772. uint32_t status = rtc_hal_ext1_get_wakeup_pins();
  773. // Translate bit map of RTC IO numbers into the bit map of GPIO numbers
  774. uint64_t gpio_mask = 0;
  775. for (int gpio = 0; gpio < GPIO_PIN_COUNT; ++gpio) {
  776. if (!esp_sleep_is_valid_wakeup_gpio(gpio)) {
  777. continue;
  778. }
  779. int rtc_pin = rtc_io_number_get(gpio);
  780. if ((status & BIT(rtc_pin)) == 0) {
  781. continue;
  782. }
  783. gpio_mask |= 1ULL << gpio;
  784. }
  785. return gpio_mask;
  786. }
  787. esp_err_t esp_sleep_enable_gpio_wakeup(void)
  788. {
  789. #if CONFIG_IDF_TARGET_ESP32
  790. if (s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) {
  791. ESP_LOGE(TAG, "Conflicting wake-up triggers: touch / ULP");
  792. return ESP_ERR_INVALID_STATE;
  793. }
  794. #endif
  795. s_config.wakeup_triggers |= RTC_GPIO_TRIG_EN;
  796. return ESP_OK;
  797. }
  798. esp_err_t esp_sleep_enable_uart_wakeup(int uart_num)
  799. {
  800. if (uart_num == UART_NUM_0) {
  801. s_config.wakeup_triggers |= RTC_UART0_TRIG_EN;
  802. } else if (uart_num == UART_NUM_1) {
  803. s_config.wakeup_triggers |= RTC_UART1_TRIG_EN;
  804. } else {
  805. return ESP_ERR_INVALID_ARG;
  806. }
  807. return ESP_OK;
  808. }
  809. esp_err_t esp_sleep_enable_wifi_wakeup(void)
  810. {
  811. #if CONFIG_IDF_TARGET_ESP32
  812. return ESP_ERR_NOT_SUPPORTED;
  813. #elif CONFIG_IDF_TARGET_ESP32S2
  814. s_config.wakeup_triggers |= RTC_WIFI_TRIG_EN;
  815. return ESP_OK;
  816. #elif CONFIG_IDF_TARGET_ESP32S3
  817. s_config.wakeup_triggers |= RTC_MAC_TRIG_EN;
  818. return ESP_OK;
  819. #endif
  820. }
  821. esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause(void)
  822. {
  823. if (rtc_get_reset_reason(0) != DEEPSLEEP_RESET && !s_light_sleep_wakeup) {
  824. return ESP_SLEEP_WAKEUP_UNDEFINED;
  825. }
  826. #ifdef CONFIG_IDF_TARGET_ESP32
  827. uint32_t wakeup_cause = REG_GET_FIELD(RTC_CNTL_WAKEUP_STATE_REG, RTC_CNTL_WAKEUP_CAUSE);
  828. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  829. uint32_t wakeup_cause = REG_GET_FIELD(RTC_CNTL_SLP_WAKEUP_CAUSE_REG, RTC_CNTL_WAKEUP_CAUSE);
  830. #endif
  831. if (wakeup_cause & RTC_EXT0_TRIG_EN) {
  832. return ESP_SLEEP_WAKEUP_EXT0;
  833. } else if (wakeup_cause & RTC_EXT1_TRIG_EN) {
  834. return ESP_SLEEP_WAKEUP_EXT1;
  835. } else if (wakeup_cause & RTC_TIMER_TRIG_EN) {
  836. return ESP_SLEEP_WAKEUP_TIMER;
  837. } else if (wakeup_cause & RTC_TOUCH_TRIG_EN) {
  838. return ESP_SLEEP_WAKEUP_TOUCHPAD;
  839. #if SOC_ULP_SUPPORTED
  840. } else if (wakeup_cause & RTC_ULP_TRIG_EN) {
  841. return ESP_SLEEP_WAKEUP_ULP;
  842. #endif
  843. } else if (wakeup_cause & RTC_GPIO_TRIG_EN) {
  844. return ESP_SLEEP_WAKEUP_GPIO;
  845. } else if (wakeup_cause & (RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN)) {
  846. return ESP_SLEEP_WAKEUP_UART;
  847. #if CONFIG_IDF_TARGET_ESP32S2
  848. } else if (wakeup_cause & RTC_WIFI_TRIG_EN) {
  849. return ESP_SLEEP_WAKEUP_WIFI;
  850. } else if (wakeup_cause & RTC_COCPU_TRIG_EN) {
  851. return ESP_SLEEP_WAKEUP_ULP;
  852. } else if (wakeup_cause & RTC_COCPU_TRAP_TRIG_EN) {
  853. return ESP_SLEEP_WAKEUP_COCPU_TRAP_TRIG;
  854. #endif
  855. } else {
  856. return ESP_SLEEP_WAKEUP_UNDEFINED;
  857. }
  858. }
  859. esp_err_t esp_sleep_pd_config(esp_sleep_pd_domain_t domain,
  860. esp_sleep_pd_option_t option)
  861. {
  862. if (domain >= ESP_PD_DOMAIN_MAX || option > ESP_PD_OPTION_AUTO) {
  863. return ESP_ERR_INVALID_ARG;
  864. }
  865. s_config.pd_options[domain] = option;
  866. return ESP_OK;
  867. }
  868. static uint32_t get_power_down_flags(void)
  869. {
  870. // Where needed, convert AUTO options to ON. Later interpret AUTO as OFF.
  871. // RTC_SLOW_MEM is needed for the ULP, so keep RTC_SLOW_MEM powered up if ULP
  872. // is used and RTC_SLOW_MEM is Auto.
  873. // If there is any data placed into .rtc.data or .rtc.bss segments, and
  874. // RTC_SLOW_MEM is Auto, keep it powered up as well.
  875. #if SOC_RTC_SLOW_MEM_SUPPORTED && SOC_ULP_SUPPORTED
  876. // Labels are defined in the linker script
  877. extern int _rtc_slow_length;
  878. if ((s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] == ESP_PD_OPTION_AUTO) &&
  879. ((size_t) &_rtc_slow_length > 0 ||
  880. (s_config.wakeup_triggers & RTC_ULP_TRIG_EN))) {
  881. s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] = ESP_PD_OPTION_ON;
  882. }
  883. #endif
  884. #if !CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  885. /* RTC_FAST_MEM is needed for deep sleep stub.
  886. If RTC_FAST_MEM is Auto, keep it powered on, so that deep sleep stub can run.
  887. In the new chip revision, deep sleep stub will be optional, and this can be changed. */
  888. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] == ESP_PD_OPTION_AUTO) {
  889. s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] = ESP_PD_OPTION_ON;
  890. }
  891. #else
  892. /* If RTC_FAST_MEM is used for heap, force RTC_FAST_MEM to be powered on. */
  893. s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] = ESP_PD_OPTION_ON;
  894. #endif
  895. // RTC_PERIPH is needed for EXT0 wakeup and GPIO wakeup.
  896. // If RTC_PERIPH is auto, and EXT0/GPIO aren't enabled, power down RTC_PERIPH.
  897. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] == ESP_PD_OPTION_AUTO) {
  898. #if SOC_TOUCH_PAD_WAKE_SUPPORTED
  899. uint32_t wakeup_source = RTC_TOUCH_TRIG_EN;
  900. #if SOC_ULP_SUPPORTED
  901. wakeup_source |= RTC_ULP_TRIG_EN;
  902. #endif
  903. if (s_config.wakeup_triggers & (RTC_EXT0_TRIG_EN | RTC_GPIO_TRIG_EN)) {
  904. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_ON;
  905. } else if (s_config.wakeup_triggers & wakeup_source) {
  906. // In both rev. 0 and rev. 1 of ESP32, forcing power up of RTC_PERIPH
  907. // prevents ULP timer and touch FSMs from working correctly.
  908. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_OFF;
  909. }
  910. #else
  911. if (s_config.wakeup_triggers & RTC_GPIO_TRIG_EN) {
  912. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_ON;
  913. } else {
  914. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_OFF;
  915. }
  916. #endif // SOC_TOUCH_PAD_WAKE_SUPPORTED
  917. }
  918. if (s_config.pd_options[ESP_PD_DOMAIN_XTAL] == ESP_PD_OPTION_AUTO) {
  919. s_config.pd_options[ESP_PD_DOMAIN_XTAL] = ESP_PD_OPTION_OFF;
  920. }
  921. const char* option_str[] = {"OFF", "ON", "AUTO(OFF)" /* Auto works as OFF */};
  922. ESP_LOGD(TAG, "RTC_PERIPH: %s", option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH]]);
  923. #if SOC_RTC_SLOW_MEM_SUPPORTED
  924. ESP_LOGD(TAG, "RTC_SLOW_MEM: %s", option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM]]);
  925. #endif
  926. ESP_LOGD(TAG, "RTC_FAST_MEM: %s", option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM]]);
  927. // Prepare flags based on the selected options
  928. uint32_t pd_flags = 0;
  929. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] != ESP_PD_OPTION_ON) {
  930. pd_flags |= RTC_SLEEP_PD_RTC_FAST_MEM;
  931. }
  932. #if SOC_RTC_SLOW_MEM_SUPPORTED
  933. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] != ESP_PD_OPTION_ON) {
  934. pd_flags |= RTC_SLEEP_PD_RTC_SLOW_MEM;
  935. }
  936. #endif
  937. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] != ESP_PD_OPTION_ON) {
  938. pd_flags |= RTC_SLEEP_PD_RTC_PERIPH;
  939. }
  940. #ifdef CONFIG_IDF_TARGET_ESP32
  941. pd_flags |= RTC_SLEEP_PD_XTAL;
  942. #endif
  943. #if ((defined CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS) && (defined CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT))
  944. if ((s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) == 0) {
  945. // If enabled EXT1 only and enable the additional current by touch, should be keep RTC_PERIPH power on.
  946. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  947. }
  948. #endif
  949. return pd_flags;
  950. }
  951. void esp_deep_sleep_disable_rom_logging(void)
  952. {
  953. esp_rom_disable_logging();
  954. }