sleep_modes.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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 <sys/lock.h>
  16. #include <sys/param.h>
  17. #include "esp_attr.h"
  18. #include "esp_sleep.h"
  19. #include "esp_private/esp_timer_private.h"
  20. #include "esp_log.h"
  21. #include "esp32/clk.h"
  22. #include "esp_newlib.h"
  23. #include "esp_spi_flash.h"
  24. #include "esp32/rom/cache.h"
  25. #include "esp32/rom/rtc.h"
  26. #include "esp32/rom/uart.h"
  27. #include "soc/cpu.h"
  28. #include "soc/rtc.h"
  29. #include "soc/spi_periph.h"
  30. #include "soc/dport_reg.h"
  31. #include "soc/soc_memory_layout.h"
  32. #include "hal/wdt_hal.h"
  33. #include "driver/rtc_io.h"
  34. #include "driver/uart.h"
  35. #include "freertos/FreeRTOS.h"
  36. #include "freertos/task.h"
  37. #include "sdkconfig.h"
  38. // If light sleep time is less than that, don't power down flash
  39. #define FLASH_PD_MIN_SLEEP_TIME_US 2000
  40. // Time from VDD_SDIO power up to first flash read in ROM code
  41. #define VDD_SDIO_POWERUP_TO_FLASH_READ_US 700
  42. // Extra time it takes to enter and exit light sleep and deep sleep
  43. // For deep sleep, this is until the wake stub runs (not the app).
  44. #ifdef CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS
  45. #define LIGHT_SLEEP_TIME_OVERHEAD_US (650 + 30 * 240 / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ)
  46. #define DEEP_SLEEP_TIME_OVERHEAD_US (650 + 100 * 240 / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ)
  47. #else
  48. #define LIGHT_SLEEP_TIME_OVERHEAD_US (250 + 30 * 240 / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ)
  49. #define DEEP_SLEEP_TIME_OVERHEAD_US (250 + 100 * 240 / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ)
  50. #endif // CONFIG_ESP32_RTC_CLK_SRC
  51. // Minimal amount of time we can sleep for
  52. #define LIGHT_SLEEP_MIN_TIME_US 200
  53. #define CHECK_SOURCE(source, value, mask) ((s_config.wakeup_triggers & mask) && \
  54. (source == value))
  55. /**
  56. * Internal structure which holds all requested deep sleep parameters
  57. */
  58. typedef struct {
  59. esp_sleep_pd_option_t pd_options[ESP_PD_DOMAIN_MAX];
  60. uint64_t sleep_duration;
  61. uint32_t wakeup_triggers : 11;
  62. uint32_t ext1_trigger_mode : 1;
  63. uint32_t ext1_rtc_gpio_mask : 18;
  64. uint32_t ext0_trigger_level : 1;
  65. uint32_t ext0_rtc_gpio_num : 5;
  66. uint32_t sleep_time_adjustment;
  67. uint64_t rtc_ticks_at_sleep_start;
  68. } sleep_config_t;
  69. static sleep_config_t s_config = {
  70. .pd_options = { ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO },
  71. .wakeup_triggers = 0
  72. };
  73. /* Internal variable used to track if light sleep wakeup sources are to be
  74. expected when determining wakeup cause. */
  75. static bool s_light_sleep_wakeup = false;
  76. /* Updating RTC_MEMORY_CRC_REG register via set_rtc_memory_crc()
  77. is not thread-safe, so we need to disable interrupts before going to deep sleep. */
  78. static portMUX_TYPE spinlock_rtc_deep_sleep = portMUX_INITIALIZER_UNLOCKED;
  79. static const char* TAG = "sleep";
  80. static uint32_t get_power_down_flags(void);
  81. static void ext0_wakeup_prepare(void);
  82. static void ext1_wakeup_prepare(void);
  83. static void timer_wakeup_prepare(void);
  84. /* Wake from deep sleep stub
  85. See esp_deepsleep.h esp_wake_deep_sleep() comments for details.
  86. */
  87. esp_deep_sleep_wake_stub_fn_t esp_get_deep_sleep_wake_stub(void)
  88. {
  89. esp_deep_sleep_wake_stub_fn_t stub_ptr = (esp_deep_sleep_wake_stub_fn_t) REG_READ(RTC_ENTRY_ADDR_REG);
  90. if (!esp_ptr_executable(stub_ptr)) {
  91. return NULL;
  92. }
  93. return stub_ptr;
  94. }
  95. void esp_set_deep_sleep_wake_stub(esp_deep_sleep_wake_stub_fn_t new_stub)
  96. {
  97. REG_WRITE(RTC_ENTRY_ADDR_REG, (uint32_t)new_stub);
  98. }
  99. void RTC_IRAM_ATTR esp_default_wake_deep_sleep(void) {
  100. /* Clear MMU for CPU 0 */
  101. _DPORT_REG_WRITE(DPORT_PRO_CACHE_CTRL1_REG,
  102. _DPORT_REG_READ(DPORT_PRO_CACHE_CTRL1_REG) | DPORT_PRO_CACHE_MMU_IA_CLR);
  103. _DPORT_REG_WRITE(DPORT_PRO_CACHE_CTRL1_REG,
  104. _DPORT_REG_READ(DPORT_PRO_CACHE_CTRL1_REG) & (~DPORT_PRO_CACHE_MMU_IA_CLR));
  105. #if CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY > 0
  106. // ROM code has not started yet, so we need to set delay factor
  107. // used by ets_delay_us first.
  108. ets_update_cpu_frequency_rom(ets_get_detected_xtal_freq() / 1000000);
  109. // This delay is configured in menuconfig, it can be used to give
  110. // the flash chip some time to become ready.
  111. ets_delay_us(CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY);
  112. #endif
  113. }
  114. void __attribute__((weak, alias("esp_default_wake_deep_sleep"))) esp_wake_deep_sleep(void);
  115. void esp_deep_sleep(uint64_t time_in_us)
  116. {
  117. esp_sleep_enable_timer_wakeup(time_in_us);
  118. esp_deep_sleep_start();
  119. }
  120. static void IRAM_ATTR flush_uarts(void)
  121. {
  122. for (int i = 0; i < 3; ++i) {
  123. uart_tx_wait_idle(i);
  124. }
  125. }
  126. static void IRAM_ATTR suspend_uarts(void)
  127. {
  128. for (int i = 0; i < 3; ++i) {
  129. REG_SET_BIT(UART_FLOW_CONF_REG(i), UART_FORCE_XOFF);
  130. while (REG_GET_FIELD(UART_STATUS_REG(i), UART_ST_UTX_OUT) != 0) {
  131. ;
  132. }
  133. }
  134. }
  135. static void IRAM_ATTR resume_uarts(void)
  136. {
  137. for (int i = 0; i < 3; ++i) {
  138. REG_CLR_BIT(UART_FLOW_CONF_REG(i), UART_FORCE_XOFF);
  139. REG_SET_BIT(UART_FLOW_CONF_REG(i), UART_FORCE_XON);
  140. REG_CLR_BIT(UART_FLOW_CONF_REG(i), UART_FORCE_XON);
  141. }
  142. }
  143. inline static uint32_t IRAM_ATTR call_rtc_sleep_start(uint32_t reject_triggers);
  144. static uint32_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags)
  145. {
  146. // Stop UART output so that output is not lost due to APB frequency change.
  147. // For light sleep, suspend UART output — it will resume after wakeup.
  148. // For deep sleep, wait for the contents of UART FIFO to be sent.
  149. bool deep_sleep = pd_flags & RTC_SLEEP_PD_DIG;
  150. if (deep_sleep) {
  151. flush_uarts();
  152. } else {
  153. suspend_uarts();
  154. }
  155. // Save current frequency and switch to XTAL
  156. rtc_cpu_freq_config_t cpu_freq_config;
  157. rtc_clk_cpu_freq_get_config(&cpu_freq_config);
  158. rtc_clk_cpu_freq_set_xtal();
  159. // Configure pins for external wakeup
  160. if (s_config.wakeup_triggers & RTC_EXT0_TRIG_EN) {
  161. ext0_wakeup_prepare();
  162. }
  163. if (s_config.wakeup_triggers & RTC_EXT1_TRIG_EN) {
  164. ext1_wakeup_prepare();
  165. }
  166. // Enable ULP wakeup
  167. if (s_config.wakeup_triggers & RTC_ULP_TRIG_EN) {
  168. SET_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_WAKEUP_FORCE_EN);
  169. }
  170. // Enter sleep
  171. rtc_sleep_config_t config = RTC_SLEEP_CONFIG_DEFAULT(pd_flags);
  172. rtc_sleep_init(config);
  173. // Configure timer wakeup
  174. if ((s_config.wakeup_triggers & RTC_TIMER_TRIG_EN) &&
  175. s_config.sleep_duration > 0) {
  176. timer_wakeup_prepare();
  177. }
  178. uint32_t result;
  179. if (deep_sleep) {
  180. /* Disable interrupts in case another task writes to RTC memory while we
  181. * calculate RTC memory CRC
  182. */
  183. portENTER_CRITICAL(&spinlock_rtc_deep_sleep);
  184. #if !CONFIG_ESP32_ALLOW_RTC_FAST_MEM_AS_HEAP
  185. /* If not possible stack is in RTC FAST memory, use the ROM function to calculate the CRC and save ~140 bytes IRAM */
  186. set_rtc_memory_crc();
  187. result = call_rtc_sleep_start(0);
  188. #else
  189. /* Otherwise, need to call the dedicated soc function for this */
  190. result = rtc_deep_sleep_start(s_config.wakeup_triggers, 0);
  191. #endif
  192. portEXIT_CRITICAL(&spinlock_rtc_deep_sleep);
  193. } else {
  194. result = call_rtc_sleep_start(0);
  195. }
  196. // Restore CPU frequency
  197. rtc_clk_cpu_freq_set_config(&cpu_freq_config);
  198. // re-enable UART output
  199. resume_uarts();
  200. return result;
  201. }
  202. inline static uint32_t IRAM_ATTR call_rtc_sleep_start(uint32_t reject_triggers)
  203. {
  204. #ifdef CONFIG_IDF_TARGET_ESP32
  205. return rtc_sleep_start(s_config.wakeup_triggers, reject_triggers);
  206. #else
  207. return rtc_sleep_start(s_config.wakeup_triggers, reject_triggers, 1);
  208. #endif
  209. }
  210. void IRAM_ATTR esp_deep_sleep_start(void)
  211. {
  212. // record current RTC time
  213. s_config.rtc_ticks_at_sleep_start = rtc_time_get();
  214. esp_sync_counters_rtc_and_frc();
  215. // Configure wake stub
  216. if (esp_get_deep_sleep_wake_stub() == NULL) {
  217. esp_set_deep_sleep_wake_stub(esp_wake_deep_sleep);
  218. }
  219. // Decide which power domains can be powered down
  220. uint32_t pd_flags = get_power_down_flags();
  221. // Correct the sleep time
  222. s_config.sleep_time_adjustment = DEEP_SLEEP_TIME_OVERHEAD_US;
  223. // Enter sleep
  224. esp_sleep_start(RTC_SLEEP_PD_DIG | RTC_SLEEP_PD_VDDSDIO | RTC_SLEEP_PD_XTAL | pd_flags);
  225. // Because RTC is in a slower clock domain than the CPU, it
  226. // can take several CPU cycles for the sleep mode to start.
  227. while (1) {
  228. ;
  229. }
  230. }
  231. /**
  232. * Helper function which handles entry to and exit from light sleep
  233. * Placed into IRAM as flash may need some time to be powered on.
  234. */
  235. static esp_err_t esp_light_sleep_inner(uint32_t pd_flags,
  236. uint32_t flash_enable_time_us,
  237. rtc_vddsdio_config_t vddsdio_config) IRAM_ATTR __attribute__((noinline));
  238. static esp_err_t esp_light_sleep_inner(uint32_t pd_flags,
  239. uint32_t flash_enable_time_us,
  240. rtc_vddsdio_config_t vddsdio_config)
  241. {
  242. // Enter sleep
  243. esp_err_t err = esp_sleep_start(pd_flags);
  244. // If VDDSDIO regulator was controlled by RTC registers before sleep,
  245. // restore the configuration.
  246. if (vddsdio_config.force) {
  247. rtc_vddsdio_set_config(vddsdio_config);
  248. }
  249. // If SPI flash was powered down, wait for it to become ready
  250. if (pd_flags & RTC_SLEEP_PD_VDDSDIO) {
  251. // Wait for the flash chip to start up
  252. ets_delay_us(flash_enable_time_us);
  253. }
  254. return err;
  255. }
  256. esp_err_t esp_light_sleep_start(void)
  257. {
  258. static portMUX_TYPE light_sleep_lock = portMUX_INITIALIZER_UNLOCKED;
  259. portENTER_CRITICAL(&light_sleep_lock);
  260. /* We will be calling esp_timer_private_advance inside DPORT access critical
  261. * section. Make sure the code on the other CPU is not holding esp_timer
  262. * lock, otherwise there will be deadlock.
  263. */
  264. esp_timer_private_lock();
  265. s_config.rtc_ticks_at_sleep_start = rtc_time_get();
  266. uint64_t frc_time_at_start = esp_timer_get_time();
  267. DPORT_STALL_OTHER_CPU_START();
  268. // Decide which power domains can be powered down
  269. uint32_t pd_flags = get_power_down_flags();
  270. // Amount of time to subtract from actual sleep time.
  271. // This is spent on entering and leaving light sleep.
  272. s_config.sleep_time_adjustment = LIGHT_SLEEP_TIME_OVERHEAD_US;
  273. // Decide if VDD_SDIO needs to be powered down;
  274. // If it needs to be powered down, adjust sleep time.
  275. const uint32_t flash_enable_time_us = VDD_SDIO_POWERUP_TO_FLASH_READ_US
  276. + CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY;
  277. #ifndef CONFIG_SPIRAM
  278. const uint32_t vddsdio_pd_sleep_duration = MAX(FLASH_PD_MIN_SLEEP_TIME_US,
  279. flash_enable_time_us + LIGHT_SLEEP_TIME_OVERHEAD_US + LIGHT_SLEEP_MIN_TIME_US);
  280. if (s_config.sleep_duration > vddsdio_pd_sleep_duration) {
  281. pd_flags |= RTC_SLEEP_PD_VDDSDIO;
  282. s_config.sleep_time_adjustment += flash_enable_time_us;
  283. }
  284. #endif //CONFIG_SPIRAM
  285. rtc_vddsdio_config_t vddsdio_config = rtc_vddsdio_get_config();
  286. // Safety net: enable WDT in case exit from light sleep fails
  287. wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
  288. 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.
  289. if (!wdt_was_enabled) {
  290. wdt_hal_init(&rtc_wdt_ctx, WDT_RWDT, 0, false);
  291. uint32_t stage_timeout_ticks = (uint32_t)(1000ULL * rtc_clk_slow_freq_get_hz() / 1000ULL);
  292. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  293. wdt_hal_config_stage(&rtc_wdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_RTC);
  294. wdt_hal_enable(&rtc_wdt_ctx);
  295. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  296. }
  297. // Enter sleep, then wait for flash to be ready on wakeup
  298. esp_err_t err = esp_light_sleep_inner(pd_flags,
  299. flash_enable_time_us, vddsdio_config);
  300. s_light_sleep_wakeup = true;
  301. // FRC1 has been clock gated for the duration of the sleep, correct for that.
  302. uint64_t rtc_ticks_at_end = rtc_time_get();
  303. uint64_t frc_time_at_end = esp_timer_get_time();
  304. uint64_t rtc_time_diff = rtc_time_slowclk_to_us(rtc_ticks_at_end - s_config.rtc_ticks_at_sleep_start,
  305. esp_clk_slowclk_cal_get());
  306. uint64_t frc_time_diff = frc_time_at_end - frc_time_at_start;
  307. int64_t time_diff = rtc_time_diff - frc_time_diff;
  308. /* Small negative values (up to 1 RTC_SLOW clock period) are possible,
  309. * for very small values of sleep_duration. Ignore those to keep esp_timer
  310. * monotonic.
  311. */
  312. if (time_diff > 0) {
  313. esp_timer_private_advance(time_diff);
  314. }
  315. esp_set_time_from_rtc();
  316. esp_timer_private_unlock();
  317. DPORT_STALL_OTHER_CPU_END();
  318. if (!wdt_was_enabled) {
  319. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  320. wdt_hal_disable(&rtc_wdt_ctx);
  321. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  322. }
  323. portEXIT_CRITICAL(&light_sleep_lock);
  324. return err;
  325. }
  326. esp_err_t esp_sleep_disable_wakeup_source(esp_sleep_source_t source)
  327. {
  328. // For most of sources it is enough to set trigger mask in local
  329. // configuration structure. The actual RTC wake up options
  330. // will be updated by esp_sleep_start().
  331. if (source == ESP_SLEEP_WAKEUP_ALL) {
  332. s_config.wakeup_triggers = 0;
  333. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_TIMER, RTC_TIMER_TRIG_EN)) {
  334. s_config.wakeup_triggers &= ~RTC_TIMER_TRIG_EN;
  335. s_config.sleep_duration = 0;
  336. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_EXT0, RTC_EXT0_TRIG_EN)) {
  337. s_config.ext0_rtc_gpio_num = 0;
  338. s_config.ext0_trigger_level = 0;
  339. s_config.wakeup_triggers &= ~RTC_EXT0_TRIG_EN;
  340. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_EXT1, RTC_EXT1_TRIG_EN)) {
  341. s_config.ext1_rtc_gpio_mask = 0;
  342. s_config.ext1_trigger_mode = 0;
  343. s_config.wakeup_triggers &= ~RTC_EXT1_TRIG_EN;
  344. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_TOUCHPAD, RTC_TOUCH_TRIG_EN)) {
  345. s_config.wakeup_triggers &= ~RTC_TOUCH_TRIG_EN;
  346. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_GPIO, RTC_GPIO_TRIG_EN)) {
  347. s_config.wakeup_triggers &= ~RTC_GPIO_TRIG_EN;
  348. } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_UART, (RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN))) {
  349. s_config.wakeup_triggers &= ~(RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN);
  350. }
  351. #ifdef CONFIG_ESP32_ULP_COPROC_ENABLED
  352. else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_ULP, RTC_ULP_TRIG_EN)) {
  353. s_config.wakeup_triggers &= ~RTC_ULP_TRIG_EN;
  354. }
  355. #endif
  356. else {
  357. ESP_LOGE(TAG, "Incorrect wakeup source (%d) to disable.", (int) source);
  358. return ESP_ERR_INVALID_STATE;
  359. }
  360. return ESP_OK;
  361. }
  362. esp_err_t esp_sleep_enable_ulp_wakeup(void)
  363. {
  364. #ifdef CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT
  365. return ESP_ERR_NOT_SUPPORTED;
  366. #endif
  367. #ifdef CONFIG_ESP32_ULP_COPROC_ENABLED
  368. if(s_config.wakeup_triggers & RTC_EXT0_TRIG_EN) {
  369. ESP_LOGE(TAG, "Conflicting wake-up trigger: ext0");
  370. return ESP_ERR_INVALID_STATE;
  371. }
  372. s_config.wakeup_triggers |= RTC_ULP_TRIG_EN;
  373. return ESP_OK;
  374. #else
  375. return ESP_ERR_INVALID_STATE;
  376. #endif
  377. }
  378. esp_err_t esp_sleep_enable_timer_wakeup(uint64_t time_in_us)
  379. {
  380. s_config.wakeup_triggers |= RTC_TIMER_TRIG_EN;
  381. s_config.sleep_duration = time_in_us;
  382. return ESP_OK;
  383. }
  384. static void timer_wakeup_prepare(void)
  385. {
  386. uint32_t period = esp_clk_slowclk_cal_get();
  387. int64_t sleep_duration = (int64_t) s_config.sleep_duration - (int64_t) s_config.sleep_time_adjustment;
  388. if (sleep_duration < 0) {
  389. sleep_duration = 0;
  390. }
  391. int64_t rtc_count_delta = rtc_time_us_to_slowclk(sleep_duration, period);
  392. rtc_sleep_set_wakeup_time(s_config.rtc_ticks_at_sleep_start + rtc_count_delta);
  393. }
  394. esp_err_t esp_sleep_enable_touchpad_wakeup(void)
  395. {
  396. #ifdef CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT
  397. return ESP_ERR_NOT_SUPPORTED;
  398. #endif
  399. if (s_config.wakeup_triggers & (RTC_EXT0_TRIG_EN)) {
  400. ESP_LOGE(TAG, "Conflicting wake-up trigger: ext0");
  401. return ESP_ERR_INVALID_STATE;
  402. }
  403. s_config.wakeup_triggers |= RTC_TOUCH_TRIG_EN;
  404. return ESP_OK;
  405. }
  406. touch_pad_t esp_sleep_get_touchpad_wakeup_status(void)
  407. {
  408. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_TOUCHPAD) {
  409. return TOUCH_PAD_MAX;
  410. }
  411. touch_pad_t pad_num;
  412. esp_err_t ret = touch_pad_get_wakeup_status(&pad_num);
  413. assert(ret == ESP_OK && "wakeup reason is RTC_TOUCH_TRIG_EN but SENS_TOUCH_MEAS_EN is zero");
  414. return pad_num;
  415. }
  416. esp_err_t esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level)
  417. {
  418. if (level < 0 || level > 1) {
  419. return ESP_ERR_INVALID_ARG;
  420. }
  421. if (!RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
  422. return ESP_ERR_INVALID_ARG;
  423. }
  424. if (s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) {
  425. ESP_LOGE(TAG, "Conflicting wake-up triggers: touch / ULP");
  426. return ESP_ERR_INVALID_STATE;
  427. }
  428. s_config.ext0_rtc_gpio_num = rtc_io_number_get(gpio_num);
  429. s_config.ext0_trigger_level = level;
  430. s_config.wakeup_triggers |= RTC_EXT0_TRIG_EN;
  431. return ESP_OK;
  432. }
  433. static void ext0_wakeup_prepare(void)
  434. {
  435. int rtc_gpio_num = s_config.ext0_rtc_gpio_num;
  436. // Set GPIO to be used for wakeup
  437. REG_SET_FIELD(RTC_IO_EXT_WAKEUP0_REG, RTC_IO_EXT_WAKEUP0_SEL, rtc_gpio_num);
  438. // Set level which will trigger wakeup
  439. SET_PERI_REG_BITS(RTC_CNTL_EXT_WAKEUP_CONF_REG, 0x1,
  440. s_config.ext0_trigger_level, RTC_CNTL_EXT_WAKEUP0_LV_S);
  441. // Find GPIO descriptor in the rtc_io_desc table and configure the pad
  442. const rtc_io_desc_t* desc = &rtc_io_desc[rtc_gpio_num];
  443. REG_SET_BIT(desc->reg, desc->mux);
  444. SET_PERI_REG_BITS(desc->reg, 0x3, 0, desc->func);
  445. REG_SET_BIT(desc->reg, desc->ie);
  446. }
  447. esp_err_t esp_sleep_enable_ext1_wakeup(uint64_t mask, esp_sleep_ext1_wakeup_mode_t mode)
  448. {
  449. if (mode > ESP_EXT1_WAKEUP_ANY_HIGH) {
  450. return ESP_ERR_INVALID_ARG;
  451. }
  452. // Translate bit map of GPIO numbers into the bit map of RTC IO numbers
  453. uint32_t rtc_gpio_mask = 0;
  454. for (int gpio = 0; mask; ++gpio, mask >>= 1) {
  455. if ((mask & 1) == 0) {
  456. continue;
  457. }
  458. if (!RTC_GPIO_IS_VALID_GPIO(gpio)) {
  459. ESP_LOGE(TAG, "Not an RTC IO: GPIO%d", gpio);
  460. return ESP_ERR_INVALID_ARG;
  461. }
  462. rtc_gpio_mask |= BIT(rtc_io_number_get(gpio));
  463. }
  464. s_config.ext1_rtc_gpio_mask = rtc_gpio_mask;
  465. s_config.ext1_trigger_mode = mode;
  466. s_config.wakeup_triggers |= RTC_EXT1_TRIG_EN;
  467. return ESP_OK;
  468. }
  469. static void ext1_wakeup_prepare(void)
  470. {
  471. // Configure all RTC IOs selected as ext1 wakeup inputs
  472. uint32_t rtc_gpio_mask = s_config.ext1_rtc_gpio_mask;
  473. for (int gpio = 0; gpio < GPIO_PIN_COUNT && rtc_gpio_mask != 0; ++gpio) {
  474. int rtc_pin = rtc_io_number_get(gpio);
  475. if ((rtc_gpio_mask & BIT(rtc_pin)) == 0) {
  476. continue;
  477. }
  478. const rtc_io_desc_t* desc = &rtc_io_desc[rtc_pin];
  479. // Route pad to RTC
  480. REG_SET_BIT(desc->reg, desc->mux);
  481. SET_PERI_REG_BITS(desc->reg, 0x3, 0, desc->func);
  482. // set input enable in sleep mode
  483. REG_SET_BIT(desc->reg, desc->ie);
  484. // Pad configuration depends on RTC_PERIPH state in sleep mode
  485. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] != ESP_PD_OPTION_ON) {
  486. // RTC_PERIPH will be powered down, so RTC_IO_ registers will
  487. // loose their state. Lock pad configuration.
  488. // Pullups/pulldowns also need to be disabled.
  489. REG_CLR_BIT(desc->reg, desc->pulldown);
  490. REG_CLR_BIT(desc->reg, desc->pullup);
  491. REG_SET_BIT(RTC_CNTL_HOLD_FORCE_REG, desc->hold_force);
  492. }
  493. // Keep track of pins which are processed to bail out early
  494. rtc_gpio_mask &= ~BIT(rtc_pin);
  495. }
  496. // Clear state from previous wakeup
  497. REG_SET_BIT(RTC_CNTL_EXT_WAKEUP1_REG, RTC_CNTL_EXT_WAKEUP1_STATUS_CLR);
  498. // Set pins to be used for wakeup
  499. REG_SET_FIELD(RTC_CNTL_EXT_WAKEUP1_REG, RTC_CNTL_EXT_WAKEUP1_SEL, s_config.ext1_rtc_gpio_mask);
  500. // Set logic function (any low, all high)
  501. SET_PERI_REG_BITS(RTC_CNTL_EXT_WAKEUP_CONF_REG, 0x1,
  502. s_config.ext1_trigger_mode, RTC_CNTL_EXT_WAKEUP1_LV_S);
  503. }
  504. uint64_t esp_sleep_get_ext1_wakeup_status(void)
  505. {
  506. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_EXT1) {
  507. return 0;
  508. }
  509. uint32_t status = REG_GET_FIELD(RTC_CNTL_EXT_WAKEUP1_STATUS_REG, RTC_CNTL_EXT_WAKEUP1_STATUS);
  510. // Translate bit map of RTC IO numbers into the bit map of GPIO numbers
  511. uint64_t gpio_mask = 0;
  512. for (int gpio = 0; gpio < GPIO_PIN_COUNT; ++gpio) {
  513. if (!RTC_GPIO_IS_VALID_GPIO(gpio)) {
  514. continue;
  515. }
  516. int rtc_pin = rtc_io_number_get(gpio);
  517. if ((status & BIT(rtc_pin)) == 0) {
  518. continue;
  519. }
  520. gpio_mask |= 1ULL << gpio;
  521. }
  522. return gpio_mask;
  523. }
  524. esp_err_t esp_sleep_enable_gpio_wakeup(void)
  525. {
  526. if (s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) {
  527. ESP_LOGE(TAG, "Conflicting wake-up triggers: touch / ULP");
  528. return ESP_ERR_INVALID_STATE;
  529. }
  530. s_config.wakeup_triggers |= RTC_GPIO_TRIG_EN;
  531. return ESP_OK;
  532. }
  533. esp_err_t esp_sleep_enable_uart_wakeup(int uart_num)
  534. {
  535. if (uart_num == UART_NUM_0) {
  536. s_config.wakeup_triggers |= RTC_UART0_TRIG_EN;
  537. } else if (uart_num == UART_NUM_1) {
  538. s_config.wakeup_triggers |= RTC_UART1_TRIG_EN;
  539. } else {
  540. return ESP_ERR_INVALID_ARG;
  541. }
  542. return ESP_OK;
  543. }
  544. esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause(void)
  545. {
  546. if (rtc_get_reset_reason(0) != DEEPSLEEP_RESET && !s_light_sleep_wakeup) {
  547. return ESP_SLEEP_WAKEUP_UNDEFINED;
  548. }
  549. uint32_t wakeup_cause = REG_GET_FIELD(RTC_CNTL_WAKEUP_STATE_REG, RTC_CNTL_WAKEUP_CAUSE);
  550. if (wakeup_cause & RTC_EXT0_TRIG_EN) {
  551. return ESP_SLEEP_WAKEUP_EXT0;
  552. } else if (wakeup_cause & RTC_EXT1_TRIG_EN) {
  553. return ESP_SLEEP_WAKEUP_EXT1;
  554. } else if (wakeup_cause & RTC_TIMER_TRIG_EN) {
  555. return ESP_SLEEP_WAKEUP_TIMER;
  556. } else if (wakeup_cause & RTC_TOUCH_TRIG_EN) {
  557. return ESP_SLEEP_WAKEUP_TOUCHPAD;
  558. } else if (wakeup_cause & RTC_ULP_TRIG_EN) {
  559. return ESP_SLEEP_WAKEUP_ULP;
  560. } else if (wakeup_cause & RTC_GPIO_TRIG_EN) {
  561. return ESP_SLEEP_WAKEUP_GPIO;
  562. } else if (wakeup_cause & (RTC_UART0_TRIG_EN | RTC_UART1_TRIG_EN)) {
  563. return ESP_SLEEP_WAKEUP_UART;
  564. } else {
  565. return ESP_SLEEP_WAKEUP_UNDEFINED;
  566. }
  567. }
  568. esp_err_t esp_sleep_pd_config(esp_sleep_pd_domain_t domain,
  569. esp_sleep_pd_option_t option)
  570. {
  571. if (domain >= ESP_PD_DOMAIN_MAX || option > ESP_PD_OPTION_AUTO) {
  572. return ESP_ERR_INVALID_ARG;
  573. }
  574. s_config.pd_options[domain] = option;
  575. return ESP_OK;
  576. }
  577. static uint32_t get_power_down_flags(void)
  578. {
  579. // Where needed, convert AUTO options to ON. Later interpret AUTO as OFF.
  580. // RTC_SLOW_MEM is needed for the ULP, so keep RTC_SLOW_MEM powered up if ULP
  581. // is used and RTC_SLOW_MEM is Auto.
  582. // If there is any data placed into .rtc.data or .rtc.bss segments, and
  583. // RTC_SLOW_MEM is Auto, keep it powered up as well.
  584. // Labels are defined in the linker script, see esp32.ld.
  585. extern int _rtc_slow_length;
  586. if ((s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] == ESP_PD_OPTION_AUTO) &&
  587. ((size_t) &_rtc_slow_length > 0 ||
  588. (s_config.wakeup_triggers & RTC_ULP_TRIG_EN))) {
  589. s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] = ESP_PD_OPTION_ON;
  590. }
  591. #if !CONFIG_ESP32_ALLOW_RTC_FAST_MEM_AS_HEAP
  592. /* RTC_FAST_MEM is needed for deep sleep stub.
  593. If RTC_FAST_MEM is Auto, keep it powered on, so that deep sleep stub can run.
  594. In the new chip revision, deep sleep stub will be optional, and this can be changed. */
  595. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] == ESP_PD_OPTION_AUTO) {
  596. s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] = ESP_PD_OPTION_ON;
  597. }
  598. #else
  599. /* If RTC_FAST_MEM is used for heap, force RTC_FAST_MEM to be powered on. */
  600. s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] = ESP_PD_OPTION_ON;
  601. #endif
  602. // RTC_PERIPH is needed for EXT0 wakeup and GPIO wakeup.
  603. // If RTC_PERIPH is auto, and EXT0/GPIO aren't enabled, power down RTC_PERIPH.
  604. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] == ESP_PD_OPTION_AUTO) {
  605. if (s_config.wakeup_triggers & (RTC_EXT0_TRIG_EN | RTC_GPIO_TRIG_EN)) {
  606. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_ON;
  607. } else if (s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) {
  608. // In both rev. 0 and rev. 1 of ESP32, forcing power up of RTC_PERIPH
  609. // prevents ULP timer and touch FSMs from working correctly.
  610. s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] = ESP_PD_OPTION_OFF;
  611. }
  612. }
  613. if (s_config.pd_options[ESP_PD_DOMAIN_XTAL] == ESP_PD_OPTION_AUTO) {
  614. s_config.pd_options[ESP_PD_DOMAIN_XTAL] = ESP_PD_OPTION_OFF;
  615. }
  616. const char* option_str[] = {"OFF", "ON", "AUTO(OFF)" /* Auto works as OFF */};
  617. ESP_LOGD(TAG, "RTC_PERIPH: %s, RTC_SLOW_MEM: %s, RTC_FAST_MEM: %s",
  618. option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH]],
  619. option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM]],
  620. option_str[s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM]]);
  621. // Prepare flags based on the selected options
  622. uint32_t pd_flags = 0;
  623. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_FAST_MEM] != ESP_PD_OPTION_ON) {
  624. pd_flags |= RTC_SLEEP_PD_RTC_FAST_MEM;
  625. }
  626. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_SLOW_MEM] != ESP_PD_OPTION_ON) {
  627. pd_flags |= RTC_SLEEP_PD_RTC_SLOW_MEM;
  628. }
  629. if (s_config.pd_options[ESP_PD_DOMAIN_RTC_PERIPH] != ESP_PD_OPTION_ON) {
  630. pd_flags |= RTC_SLEEP_PD_RTC_PERIPH;
  631. }
  632. if (s_config.pd_options[ESP_PD_DOMAIN_XTAL] != ESP_PD_OPTION_ON) {
  633. pd_flags |= RTC_SLEEP_PD_XTAL;
  634. }
  635. if ((s_config.wakeup_triggers & (RTC_TOUCH_TRIG_EN | RTC_ULP_TRIG_EN)) == 0) {
  636. // If enabled EXT1 only and enable the additional current by touch, should be keep RTC_PERIPH power on.
  637. #if ((defined CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS) && (defined CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT))
  638. pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH;
  639. #endif
  640. }
  641. return pd_flags;
  642. }
  643. void esp_deep_sleep_disable_rom_logging(void)
  644. {
  645. /* To disable logging in the ROM, only the least significant bit of the register is used,
  646. * but since this register is also used to store the frequency of the main crystal (RTC_XTAL_FREQ_REG),
  647. * you need to write to this register in the same format.
  648. * Namely, the upper 16 bits and lower should be the same.
  649. */
  650. REG_SET_BIT(RTC_CNTL_STORE4_REG, RTC_DISABLE_ROM_LOG);
  651. }