sleep_modes.c 25 KB

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