sleep_modes.c 26 KB

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