sleep_modes.c 26 KB

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