esp_sleep.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include "esp_err.h"
  9. #include "hal/touch_sensor_types.h"
  10. #include "hal/gpio_types.h"
  11. #include "soc/soc_caps.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /**
  16. * @brief Logic function used for EXT1 wakeup mode.
  17. */
  18. typedef enum {
  19. ESP_EXT1_WAKEUP_ALL_LOW = 0, //!< Wake the chip when all selected GPIOs go low
  20. ESP_EXT1_WAKEUP_ANY_HIGH = 1 //!< Wake the chip when any of the selected GPIOs go high
  21. } esp_sleep_ext1_wakeup_mode_t;
  22. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  23. typedef enum {
  24. ESP_GPIO_WAKEUP_GPIO_LOW = 0,
  25. ESP_GPIO_WAKEUP_GPIO_HIGH = 1
  26. } esp_deepsleep_gpio_wake_up_mode_t;
  27. #endif
  28. /**
  29. * @brief Power domains which can be powered down in sleep mode
  30. */
  31. typedef enum {
  32. ESP_PD_DOMAIN_RTC_PERIPH, //!< RTC IO, sensors and ULP co-processor
  33. ESP_PD_DOMAIN_RTC_SLOW_MEM, //!< RTC slow memory
  34. ESP_PD_DOMAIN_RTC_FAST_MEM, //!< RTC fast memory
  35. ESP_PD_DOMAIN_XTAL, //!< XTAL oscillator
  36. #if SOC_PM_SUPPORT_CPU_PD
  37. ESP_PD_DOMAIN_CPU, //!< CPU core
  38. #endif
  39. ESP_PD_DOMAIN_RTC8M, //!< Internal 8M oscillator
  40. ESP_PD_DOMAIN_VDDSDIO, //!< VDD_SDIO
  41. ESP_PD_DOMAIN_MAX //!< Number of domains
  42. } esp_sleep_pd_domain_t;
  43. /**
  44. * @brief Power down options
  45. */
  46. typedef enum {
  47. ESP_PD_OPTION_OFF, //!< Power down the power domain in sleep mode
  48. ESP_PD_OPTION_ON, //!< Keep power domain enabled during sleep mode
  49. ESP_PD_OPTION_AUTO //!< Keep power domain enabled in sleep mode, if it is needed by one of the wakeup options. Otherwise power it down.
  50. } esp_sleep_pd_option_t;
  51. /**
  52. * @brief Sleep wakeup cause
  53. */
  54. typedef enum {
  55. ESP_SLEEP_WAKEUP_UNDEFINED, //!< In case of deep sleep, reset was not caused by exit from deep sleep
  56. ESP_SLEEP_WAKEUP_ALL, //!< Not a wakeup cause, used to disable all wakeup sources with esp_sleep_disable_wakeup_source
  57. ESP_SLEEP_WAKEUP_EXT0, //!< Wakeup caused by external signal using RTC_IO
  58. ESP_SLEEP_WAKEUP_EXT1, //!< Wakeup caused by external signal using RTC_CNTL
  59. ESP_SLEEP_WAKEUP_TIMER, //!< Wakeup caused by timer
  60. ESP_SLEEP_WAKEUP_TOUCHPAD, //!< Wakeup caused by touchpad
  61. ESP_SLEEP_WAKEUP_ULP, //!< Wakeup caused by ULP program
  62. ESP_SLEEP_WAKEUP_GPIO, //!< Wakeup caused by GPIO (light sleep only)
  63. ESP_SLEEP_WAKEUP_UART, //!< Wakeup caused by UART (light sleep only)
  64. ESP_SLEEP_WAKEUP_WIFI, //!< Wakeup caused by WIFI (light sleep only)
  65. ESP_SLEEP_WAKEUP_COCPU, //!< Wakeup caused by COCPU int
  66. ESP_SLEEP_WAKEUP_COCPU_TRAP_TRIG, //!< Wakeup caused by COCPU crash
  67. ESP_SLEEP_WAKEUP_BT, //!< Wakeup caused by BT (light sleep only)
  68. } esp_sleep_source_t;
  69. /* Leave this type define for compatibility */
  70. typedef esp_sleep_source_t esp_sleep_wakeup_cause_t;
  71. /**
  72. * @brief Disable wakeup source
  73. *
  74. * This function is used to deactivate wake up trigger for source
  75. * defined as parameter of the function.
  76. *
  77. * @note This function does not modify wake up configuration in RTC.
  78. * It will be performed in esp_deep_sleep_start/esp_light_sleep_start function.
  79. *
  80. * See docs/sleep-modes.rst for details.
  81. *
  82. * @param source - number of source to disable of type esp_sleep_source_t
  83. * @return
  84. * - ESP_OK on success
  85. * - ESP_ERR_INVALID_STATE if trigger was not active
  86. */
  87. esp_err_t esp_sleep_disable_wakeup_source(esp_sleep_source_t source);
  88. #if SOC_ULP_SUPPORTED
  89. /**
  90. * @brief Enable wakeup by ULP coprocessor
  91. * @note In revisions 0 and 1 of the ESP32, ULP wakeup source
  92. * cannot be used when RTC_PERIPH power domain is forced
  93. * to be powered on (ESP_PD_OPTION_ON) or when
  94. * ext0 wakeup source is used.
  95. * @return
  96. * - ESP_OK on success
  97. * - ESP_ERR_NOT_SUPPORTED if additional current by touch (CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT) is enabled.
  98. * - ESP_ERR_INVALID_STATE if ULP co-processor is not enabled or if wakeup triggers conflict
  99. */
  100. esp_err_t esp_sleep_enable_ulp_wakeup(void);
  101. #endif // SOC_ULP_SUPPORTED
  102. /**
  103. * @brief Enable wakeup by timer
  104. * @param time_in_us time before wakeup, in microseconds
  105. * @return
  106. * - ESP_OK on success
  107. * - ESP_ERR_INVALID_ARG if value is out of range (TBD)
  108. */
  109. esp_err_t esp_sleep_enable_timer_wakeup(uint64_t time_in_us);
  110. #if SOC_TOUCH_SENSOR_NUM > 0
  111. /**
  112. * @brief Enable wakeup by touch sensor
  113. *
  114. * @note In revisions 0 and 1 of the ESP32, touch wakeup source
  115. * can not be used when RTC_PERIPH power domain is forced
  116. * to be powered on (ESP_PD_OPTION_ON) or when ext0 wakeup
  117. * source is used.
  118. *
  119. * @note The FSM mode of the touch button should be configured
  120. * as the timer trigger mode.
  121. *
  122. * @return
  123. * - ESP_OK on success
  124. * - ESP_ERR_NOT_SUPPORTED if additional current by touch (CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT) is enabled.
  125. * - ESP_ERR_INVALID_STATE if wakeup triggers conflict
  126. */
  127. esp_err_t esp_sleep_enable_touchpad_wakeup(void);
  128. /**
  129. * @brief Get the touch pad which caused wakeup
  130. *
  131. * If wakeup was caused by another source, this function will return TOUCH_PAD_MAX;
  132. *
  133. * @return touch pad which caused wakeup
  134. */
  135. touch_pad_t esp_sleep_get_touchpad_wakeup_status(void);
  136. #endif // SOC_TOUCH_SENSOR_NUM > 0
  137. /**
  138. * @brief Returns true if a GPIO number is valid for use as wakeup source.
  139. *
  140. * @note For SoCs with RTC IO capability, this can be any valid RTC IO input pin.
  141. *
  142. * @param gpio_num Number of the GPIO to test for wakeup source capability
  143. *
  144. * @return True if this GPIO number will be accepted as a sleep wakeup source.
  145. */
  146. bool esp_sleep_is_valid_wakeup_gpio(gpio_num_t gpio_num);
  147. #if SOC_PM_SUPPORT_EXT_WAKEUP
  148. /**
  149. * @brief Enable wakeup using a pin
  150. *
  151. * This function uses external wakeup feature of RTC_IO peripheral.
  152. * It will work only if RTC peripherals are kept on during sleep.
  153. *
  154. * This feature can monitor any pin which is an RTC IO. Once the pin transitions
  155. * into the state given by level argument, the chip will be woken up.
  156. *
  157. * @note This function does not modify pin configuration. The pin is
  158. * configured in esp_deep_sleep_start/esp_light_sleep_start,
  159. * immediately before entering sleep mode.
  160. *
  161. * @note In revisions 0 and 1 of the ESP32, ext0 wakeup source
  162. * can not be used together with touch or ULP wakeup sources.
  163. *
  164. * @param gpio_num GPIO number used as wakeup source. Only GPIOs which are have RTC
  165. * functionality can be used: 0,2,4,12-15,25-27,32-39.
  166. * @param level input level which will trigger wakeup (0=low, 1=high)
  167. * @return
  168. * - ESP_OK on success
  169. * - ESP_ERR_INVALID_ARG if the selected GPIO is not an RTC GPIO,
  170. * or the mode is invalid
  171. * - ESP_ERR_INVALID_STATE if wakeup triggers conflict
  172. */
  173. esp_err_t esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level);
  174. /**
  175. * @brief Enable wakeup using multiple pins
  176. *
  177. * This function uses external wakeup feature of RTC controller.
  178. * It will work even if RTC peripherals are shut down during sleep.
  179. *
  180. * This feature can monitor any number of pins which are in RTC IOs.
  181. * Once any of the selected pins goes into the state given by mode argument,
  182. * the chip will be woken up.
  183. *
  184. * @note This function does not modify pin configuration. The pins are
  185. * configured in esp_deep_sleep_start/esp_light_sleep_start,
  186. * immediately before entering sleep mode.
  187. *
  188. * @note Internal pullups and pulldowns don't work when RTC peripherals are
  189. * shut down. In this case, external resistors need to be added.
  190. * Alternatively, RTC peripherals (and pullups/pulldowns) may be
  191. * kept enabled using esp_sleep_pd_config function.
  192. *
  193. * @param mask bit mask of GPIO numbers which will cause wakeup. Only GPIOs
  194. * which have RTC functionality can be used in this bit map:
  195. * 0,2,4,12-15,25-27,32-39.
  196. * @param mode select logic function used to determine wakeup condition:
  197. * - ESP_EXT1_WAKEUP_ALL_LOW: wake up when all selected GPIOs are low
  198. * - ESP_EXT1_WAKEUP_ANY_HIGH: wake up when any of the selected GPIOs is high
  199. * @return
  200. * - ESP_OK on success
  201. * - ESP_ERR_INVALID_ARG if any of the selected GPIOs is not an RTC GPIO,
  202. * or mode is invalid
  203. */
  204. esp_err_t esp_sleep_enable_ext1_wakeup(uint64_t mask, esp_sleep_ext1_wakeup_mode_t mode);
  205. #endif // SOC_PM_SUPPORT_EXT_WAKEUP
  206. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  207. /**
  208. * @brief Enable wakeup using specific gpio pins
  209. *
  210. * This function enables an IO pin to wake up the chip from deep sleep.
  211. *
  212. * @note This function does not modify pin configuration. The pins are
  213. * configured in esp_deep_sleep_start/esp_light_sleep_start,
  214. * immediately before entering sleep mode.
  215. *
  216. * @note You don't need to care to pull-up or pull-down before using this
  217. * function, because this will be done in esp_deep_sleep_start/esp_light_sleep_start
  218. * based on param mask you give. BTW, when you use low level to wake up the
  219. * chip, we strongly recommand you to add external registors (pull-up).
  220. *
  221. * @param gpio_pin_mask Bit mask of GPIO numbers which will cause wakeup. Only GPIOs
  222. * which are have RTC functionality can be used in this bit map.
  223. * @param mode Select logic function used to determine wakeup condition:
  224. * - ESP_GPIO_WAKEUP_GPIO_LOW: wake up when the gpio turn to low.
  225. * - ESP_GPIO_WAKEUP_GPIO_HIGH: wake up when the gpio turn to high.
  226. * @return
  227. * - ESP_OK on success
  228. * - ESP_ERR_INVALID_ARG if gpio num is more than 5 or mode is invalid,
  229. */
  230. esp_err_t esp_deep_sleep_enable_gpio_wakeup(uint64_t gpio_pin_mask, esp_deepsleep_gpio_wake_up_mode_t mode);
  231. #endif
  232. /**
  233. * @brief Enable wakeup from light sleep using GPIOs
  234. *
  235. * Each GPIO supports wakeup function, which can be triggered on either low level
  236. * or high level. Unlike EXT0 and EXT1 wakeup sources, this method can be used
  237. * both for all IOs: RTC IOs and digital IOs. It can only be used to wakeup from
  238. * light sleep though.
  239. *
  240. * To enable wakeup, first call gpio_wakeup_enable, specifying gpio number and
  241. * wakeup level, for each GPIO which is used for wakeup.
  242. * Then call this function to enable wakeup feature.
  243. *
  244. * @note In revisions 0 and 1 of the ESP32, GPIO wakeup source
  245. * can not be used together with touch or ULP wakeup sources.
  246. *
  247. * @return
  248. * - ESP_OK on success
  249. * - ESP_ERR_INVALID_STATE if wakeup triggers conflict
  250. */
  251. esp_err_t esp_sleep_enable_gpio_wakeup(void);
  252. /**
  253. * @brief Enable wakeup from light sleep using UART
  254. *
  255. * Use uart_set_wakeup_threshold function to configure UART wakeup threshold.
  256. *
  257. * Wakeup from light sleep takes some time, so not every character sent
  258. * to the UART can be received by the application.
  259. *
  260. * @note ESP32 does not support wakeup from UART2.
  261. *
  262. * @param uart_num UART port to wake up from
  263. * @return
  264. * - ESP_OK on success
  265. * - ESP_ERR_INVALID_ARG if wakeup from given UART is not supported
  266. */
  267. esp_err_t esp_sleep_enable_uart_wakeup(int uart_num);
  268. /**
  269. * @brief Enable wakeup by WiFi MAC
  270. * @return
  271. * - ESP_OK on success
  272. */
  273. esp_err_t esp_sleep_enable_wifi_wakeup(void);
  274. /**
  275. * @brief Disable wakeup by WiFi MAC
  276. * @return
  277. * - ESP_OK on success
  278. */
  279. esp_err_t esp_sleep_disable_wifi_wakeup(void);
  280. /**
  281. * @brief Get the bit mask of GPIOs which caused wakeup (ext1)
  282. *
  283. * If wakeup was caused by another source, this function will return 0.
  284. *
  285. * @return bit mask, if GPIOn caused wakeup, BIT(n) will be set
  286. */
  287. uint64_t esp_sleep_get_ext1_wakeup_status(void);
  288. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  289. /**
  290. * @brief Get the bit mask of GPIOs which caused wakeup (gpio)
  291. *
  292. * If wakeup was caused by another source, this function will return 0.
  293. *
  294. * @return bit mask, if GPIOn caused wakeup, BIT(n) will be set
  295. */
  296. uint64_t esp_sleep_get_gpio_wakeup_status(void);
  297. #endif //SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  298. /**
  299. * @brief Set power down mode for an RTC power domain in sleep mode
  300. *
  301. * If not set set using this API, all power domains default to ESP_PD_OPTION_AUTO.
  302. *
  303. * @param domain power domain to configure
  304. * @param option power down option (ESP_PD_OPTION_OFF, ESP_PD_OPTION_ON, or ESP_PD_OPTION_AUTO)
  305. * @return
  306. * - ESP_OK on success
  307. * - ESP_ERR_INVALID_ARG if either of the arguments is out of range
  308. */
  309. esp_err_t esp_sleep_pd_config(esp_sleep_pd_domain_t domain,
  310. esp_sleep_pd_option_t option);
  311. /**
  312. * @brief Enter deep sleep with the configured wakeup options
  313. *
  314. * This function does not return.
  315. */
  316. void esp_deep_sleep_start(void) __attribute__((noreturn));
  317. /**
  318. * @brief Enter light sleep with the configured wakeup options
  319. *
  320. * @return
  321. * - ESP_OK on success (returned after wakeup)
  322. * - ESP_ERR_INVALID_STATE if WiFi or BT is not stopped
  323. */
  324. esp_err_t esp_light_sleep_start(void);
  325. /**
  326. * @brief Enter deep-sleep mode
  327. *
  328. * The device will automatically wake up after the deep-sleep time
  329. * Upon waking up, the device calls deep sleep wake stub, and then proceeds
  330. * to load application.
  331. *
  332. * Call to this function is equivalent to a call to esp_deep_sleep_enable_timer_wakeup
  333. * followed by a call to esp_deep_sleep_start.
  334. *
  335. * esp_deep_sleep does not shut down WiFi, BT, and higher level protocol
  336. * connections gracefully.
  337. * Make sure relevant WiFi and BT stack functions are called to close any
  338. * connections and deinitialize the peripherals. These include:
  339. * - esp_bluedroid_disable
  340. * - esp_bt_controller_disable
  341. * - esp_wifi_stop
  342. *
  343. * This function does not return.
  344. *
  345. * @note The device will wake up immediately if the deep-sleep time is set to 0
  346. *
  347. * @param time_in_us deep-sleep time, unit: microsecond
  348. */
  349. void esp_deep_sleep(uint64_t time_in_us) __attribute__((noreturn));
  350. /**
  351. * @brief Get the wakeup source which caused wakeup from sleep
  352. *
  353. * @return cause of wake up from last sleep (deep sleep or light sleep)
  354. */
  355. esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause(void);
  356. /**
  357. * @brief Default stub to run on wake from deep sleep.
  358. *
  359. * Allows for executing code immediately on wake from sleep, before
  360. * the software bootloader or ESP-IDF app has started up.
  361. *
  362. * This function is weak-linked, so you can implement your own version
  363. * to run code immediately when the chip wakes from
  364. * sleep.
  365. *
  366. * See docs/deep-sleep-stub.rst for details.
  367. */
  368. void esp_wake_deep_sleep(void);
  369. /**
  370. * @brief Function type for stub to run on wake from sleep.
  371. *
  372. */
  373. typedef void (*esp_deep_sleep_wake_stub_fn_t)(void);
  374. /**
  375. * @brief Install a new stub at runtime to run on wake from deep sleep
  376. *
  377. * If implementing esp_wake_deep_sleep() then it is not necessary to
  378. * call this function.
  379. *
  380. * However, it is possible to call this function to substitute a
  381. * different deep sleep stub. Any function used as a deep sleep stub
  382. * must be marked RTC_IRAM_ATTR, and must obey the same rules given
  383. * for esp_wake_deep_sleep().
  384. */
  385. void esp_set_deep_sleep_wake_stub(esp_deep_sleep_wake_stub_fn_t new_stub);
  386. /**
  387. * @brief Get current wake from deep sleep stub
  388. * @return Return current wake from deep sleep stub, or NULL if
  389. * no stub is installed.
  390. */
  391. esp_deep_sleep_wake_stub_fn_t esp_get_deep_sleep_wake_stub(void);
  392. /**
  393. * @brief The default esp-idf-provided esp_wake_deep_sleep() stub.
  394. *
  395. * See docs/deep-sleep-stub.rst for details.
  396. */
  397. void esp_default_wake_deep_sleep(void);
  398. /**
  399. * @brief Disable logging from the ROM code after deep sleep.
  400. *
  401. * Using LSB of RTC_STORE4.
  402. */
  403. void esp_deep_sleep_disable_rom_logging(void);
  404. #ifdef SOC_PM_SUPPORT_CPU_PD
  405. /**
  406. * @brief CPU Power down low-level initialize
  407. *
  408. * @param enable enable or disable CPU power down during light sleep
  409. * @return
  410. * - ESP_OK on success
  411. * - ESP_ERR_NO_MEM not enough retention memory
  412. */
  413. esp_err_t esp_sleep_cpu_pd_low_init(bool enable);
  414. #endif
  415. #if SOC_GPIO_SUPPORT_SLP_SWITCH
  416. /**
  417. * @brief Configure to isolate all GPIO pins in sleep state
  418. */
  419. void esp_sleep_config_gpio_isolate(void);
  420. /**
  421. * @brief Enable or disable GPIO pins status switching between slept status and waked status.
  422. * @param enable decide whether to switch status or not
  423. */
  424. void esp_sleep_enable_gpio_switch(bool enable);
  425. #endif
  426. #if CONFIG_MAC_BB_PD
  427. /**
  428. * @brief Function type for stub to run mac bb power down.
  429. */
  430. typedef void (* mac_bb_power_down_cb_t)(void);
  431. /**
  432. * @brief Function type for stub to run mac bb power up.
  433. */
  434. typedef void (* mac_bb_power_up_cb_t)(void);
  435. /**
  436. * @brief Registet mac bb power down callback.
  437. * @param cb mac bb power down callback.
  438. * @return
  439. * - ESP_OK on success
  440. */
  441. esp_err_t esp_register_mac_bb_pd_callback(mac_bb_power_down_cb_t cb);
  442. /**
  443. * @brief Unregistet mac bb power down callback.
  444. * @param cb mac bb power down callback.
  445. * @return
  446. * - ESP_OK on success
  447. */
  448. esp_err_t esp_unregister_mac_bb_pd_callback(mac_bb_power_down_cb_t cb);
  449. /**
  450. * @brief Registet mac bb power up callback.
  451. * @param cb mac bb power up callback.
  452. * @return
  453. * - ESP_OK on success
  454. */
  455. esp_err_t esp_register_mac_bb_pu_callback(mac_bb_power_up_cb_t cb);
  456. /**
  457. * @brief Unregistet mac bb power up callback.
  458. * @param cb mac bb power up callback.
  459. * @return
  460. * - ESP_OK on success
  461. */
  462. esp_err_t esp_unregister_mac_bb_pu_callback(mac_bb_power_up_cb_t cb);
  463. #endif
  464. #ifdef __cplusplus
  465. }
  466. #endif