esp_sleep.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 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. Definitions for the deepsleep prepare callbacks
  17. */
  18. typedef void (*esp_deep_sleep_cb_t)(void);
  19. /**
  20. * @brief Logic function used for EXT1 wakeup mode.
  21. */
  22. #if SOC_PM_SUPPORT_EXT1_WAKEUP
  23. #if CONFIG_IDF_TARGET_ESP32
  24. typedef enum {
  25. ESP_EXT1_WAKEUP_ALL_LOW = 0, //!< Wake the chip when all selected GPIOs go low
  26. ESP_EXT1_WAKEUP_ANY_HIGH = 1 //!< Wake the chip when any of the selected GPIOs go high
  27. } esp_sleep_ext1_wakeup_mode_t;
  28. #else
  29. typedef enum {
  30. ESP_EXT1_WAKEUP_ANY_LOW = 0, //!< Wake the chip when any of the selected GPIOs go low
  31. ESP_EXT1_WAKEUP_ANY_HIGH = 1, //!< Wake the chip when any of the selected GPIOs go high
  32. ESP_EXT1_WAKEUP_ALL_LOW __attribute__((deprecated("wakeup mode \"ALL_LOW\" is no longer supported after ESP32, \
  33. please use ESP_EXT1_WAKEUP_ANY_LOW instead"))) = ESP_EXT1_WAKEUP_ANY_LOW
  34. } esp_sleep_ext1_wakeup_mode_t;
  35. #endif
  36. #endif
  37. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  38. typedef enum {
  39. ESP_GPIO_WAKEUP_GPIO_LOW = 0,
  40. ESP_GPIO_WAKEUP_GPIO_HIGH = 1
  41. } esp_deepsleep_gpio_wake_up_mode_t;
  42. #endif
  43. /**
  44. * @brief Power domains which can be powered down in sleep mode
  45. */
  46. typedef enum {
  47. #if SOC_PM_SUPPORT_RTC_PERIPH_PD
  48. ESP_PD_DOMAIN_RTC_PERIPH, //!< RTC IO, sensors and ULP co-processor
  49. #endif
  50. #if SOC_PM_SUPPORT_RTC_SLOW_MEM_PD
  51. ESP_PD_DOMAIN_RTC_SLOW_MEM, //!< RTC slow memory
  52. #endif
  53. #if SOC_PM_SUPPORT_RTC_FAST_MEM_PD
  54. ESP_PD_DOMAIN_RTC_FAST_MEM, //!< RTC fast memory
  55. #endif
  56. ESP_PD_DOMAIN_XTAL, //!< XTAL oscillator
  57. #if SOC_PM_SUPPORT_XTAL32K_PD
  58. ESP_PD_DOMAIN_XTAL32K, //!< External 32 kHz XTAL oscillator
  59. #endif
  60. #if SOC_PM_SUPPORT_RC32K_PD
  61. ESP_PD_DOMAIN_RC32K, //!< Internal 32 kHz RC oscillator
  62. #endif
  63. #if SOC_PM_SUPPORT_RC_FAST_PD
  64. ESP_PD_DOMAIN_RC_FAST, //!< Internal Fast oscillator
  65. #endif
  66. #if SOC_PM_SUPPORT_CPU_PD
  67. ESP_PD_DOMAIN_CPU, //!< CPU core
  68. #endif
  69. #if SOC_PM_SUPPORT_VDDSDIO_PD
  70. ESP_PD_DOMAIN_VDDSDIO, //!< VDD_SDIO
  71. #endif
  72. #if SOC_PM_SUPPORT_MODEM_PD
  73. ESP_PD_DOMAIN_MODEM, //!< MODEM, includes WiFi, Bluetooth and IEEE802.15.4
  74. #endif
  75. #if SOC_PM_SUPPORT_TOP_PD
  76. ESP_PD_DOMAIN_TOP, //!< SoC TOP
  77. #endif
  78. ESP_PD_DOMAIN_MAX //!< Number of domains
  79. } esp_sleep_pd_domain_t;
  80. #define ESP_PD_DOMAIN_RTC8M _Pragma("GCC warning \"'ESP_PD_DOMAIN_RTC8M' enum is deprecated\"") ESP_PD_DOMAIN_RC_FAST
  81. /**
  82. * @brief Power down options
  83. */
  84. typedef enum {
  85. ESP_PD_OPTION_OFF, //!< Power down the power domain in sleep mode
  86. ESP_PD_OPTION_ON, //!< Keep power domain enabled during sleep mode
  87. 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.
  88. } esp_sleep_pd_option_t;
  89. /**
  90. * @brief Sleep wakeup cause
  91. */
  92. typedef enum {
  93. ESP_SLEEP_WAKEUP_UNDEFINED, //!< In case of deep sleep, reset was not caused by exit from deep sleep
  94. ESP_SLEEP_WAKEUP_ALL, //!< Not a wakeup cause, used to disable all wakeup sources with esp_sleep_disable_wakeup_source
  95. ESP_SLEEP_WAKEUP_EXT0, //!< Wakeup caused by external signal using RTC_IO
  96. ESP_SLEEP_WAKEUP_EXT1, //!< Wakeup caused by external signal using RTC_CNTL
  97. ESP_SLEEP_WAKEUP_TIMER, //!< Wakeup caused by timer
  98. ESP_SLEEP_WAKEUP_TOUCHPAD, //!< Wakeup caused by touchpad
  99. ESP_SLEEP_WAKEUP_ULP, //!< Wakeup caused by ULP program
  100. ESP_SLEEP_WAKEUP_GPIO, //!< Wakeup caused by GPIO (light sleep only on ESP32, S2 and S3)
  101. ESP_SLEEP_WAKEUP_UART, //!< Wakeup caused by UART (light sleep only)
  102. ESP_SLEEP_WAKEUP_WIFI, //!< Wakeup caused by WIFI (light sleep only)
  103. ESP_SLEEP_WAKEUP_COCPU, //!< Wakeup caused by COCPU int
  104. ESP_SLEEP_WAKEUP_COCPU_TRAP_TRIG, //!< Wakeup caused by COCPU crash
  105. ESP_SLEEP_WAKEUP_BT, //!< Wakeup caused by BT (light sleep only)
  106. } esp_sleep_source_t;
  107. /**
  108. * @brief Sleep mode
  109. */
  110. typedef enum {
  111. ESP_SLEEP_MODE_LIGHT_SLEEP, //!< light sleep mode
  112. ESP_SLEEP_MODE_DEEP_SLEEP //!< deep sleep mode
  113. } esp_sleep_mode_t;
  114. /* Leave this type define for compatibility */
  115. typedef esp_sleep_source_t esp_sleep_wakeup_cause_t;
  116. enum {
  117. ESP_ERR_SLEEP_REJECT = ESP_ERR_INVALID_STATE,
  118. ESP_ERR_SLEEP_TOO_SHORT_SLEEP_DURATION = ESP_ERR_INVALID_ARG,
  119. };
  120. /**
  121. * @brief Disable wakeup source
  122. *
  123. * This function is used to deactivate wake up trigger for source
  124. * defined as parameter of the function.
  125. *
  126. * @note This function does not modify wake up configuration in RTC.
  127. * It will be performed in esp_deep_sleep_start/esp_light_sleep_start function.
  128. *
  129. * See docs/sleep-modes.rst for details.
  130. *
  131. * @param source - number of source to disable of type esp_sleep_source_t
  132. * @return
  133. * - ESP_OK on success
  134. * - ESP_ERR_INVALID_STATE if trigger was not active
  135. */
  136. esp_err_t esp_sleep_disable_wakeup_source(esp_sleep_source_t source);
  137. #if SOC_ULP_SUPPORTED
  138. /**
  139. * @brief Enable wakeup by ULP coprocessor
  140. * @note On ESP32, ULP wakeup source cannot be used when RTC_PERIPH power domain is forced,
  141. * to be powered on (ESP_PD_OPTION_ON) or when ext0 wakeup source is used.
  142. * @return
  143. * - ESP_OK on success
  144. * - ESP_ERR_NOT_SUPPORTED if additional current by touch (CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT) is enabled.
  145. * - ESP_ERR_INVALID_STATE if ULP co-processor is not enabled or if wakeup triggers conflict
  146. */
  147. esp_err_t esp_sleep_enable_ulp_wakeup(void);
  148. #endif // SOC_ULP_SUPPORTED
  149. /**
  150. * @brief Enable wakeup by timer
  151. * @param time_in_us time before wakeup, in microseconds
  152. * @return
  153. * - ESP_OK on success
  154. * - ESP_ERR_INVALID_ARG if value is out of range (TBD)
  155. */
  156. esp_err_t esp_sleep_enable_timer_wakeup(uint64_t time_in_us);
  157. #if SOC_TOUCH_SENSOR_SUPPORTED
  158. /**
  159. * @brief Enable wakeup by touch sensor
  160. *
  161. * @note On ESP32, touch wakeup source can not be used when RTC_PERIPH power domain is forced
  162. * to be powered on (ESP_PD_OPTION_ON) or when ext0 wakeup source is used.
  163. *
  164. * @note The FSM mode of the touch button should be configured
  165. * as the timer trigger mode.
  166. *
  167. * @return
  168. * - ESP_OK on success
  169. * - ESP_ERR_NOT_SUPPORTED if additional current by touch (CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT) is enabled.
  170. * - ESP_ERR_INVALID_STATE if wakeup triggers conflict
  171. */
  172. esp_err_t esp_sleep_enable_touchpad_wakeup(void);
  173. /**
  174. * @brief Get the touch pad which caused wakeup
  175. *
  176. * If wakeup was caused by another source, this function will return TOUCH_PAD_MAX;
  177. *
  178. * @return touch pad which caused wakeup
  179. */
  180. touch_pad_t esp_sleep_get_touchpad_wakeup_status(void);
  181. #endif // SOC_TOUCH_SENSOR_SUPPORTED
  182. /**
  183. * @brief Returns true if a GPIO number is valid for use as wakeup source.
  184. *
  185. * @note For SoCs with RTC IO capability, this can be any valid RTC IO input pin.
  186. *
  187. * @param gpio_num Number of the GPIO to test for wakeup source capability
  188. *
  189. * @return True if this GPIO number will be accepted as a sleep wakeup source.
  190. */
  191. bool esp_sleep_is_valid_wakeup_gpio(gpio_num_t gpio_num);
  192. #if SOC_PM_SUPPORT_EXT0_WAKEUP
  193. /**
  194. * @brief Enable wakeup using a pin
  195. *
  196. * This function uses external wakeup feature of RTC_IO peripheral.
  197. * It will work only if RTC peripherals are kept on during sleep.
  198. *
  199. * This feature can monitor any pin which is an RTC IO. Once the pin transitions
  200. * into the state given by level argument, the chip will be woken up.
  201. *
  202. * @note This function does not modify pin configuration. The pin is
  203. * configured in esp_deep_sleep_start/esp_light_sleep_start,
  204. * immediately before entering sleep mode.
  205. *
  206. * @note ESP32: ext0 wakeup source can not be used together with touch or ULP wakeup sources.
  207. *
  208. * @param gpio_num GPIO number used as wakeup source. Only GPIOs with the RTC
  209. * functionality can be used. For different SoCs, the related GPIOs are:
  210. * - ESP32: 0, 2, 4, 12-15, 25-27, 32-39;
  211. * - ESP32-S2: 0-21;
  212. * - ESP32-S3: 0-21.
  213. * @param level input level which will trigger wakeup (0=low, 1=high)
  214. * @return
  215. * - ESP_OK on success
  216. * - ESP_ERR_INVALID_ARG if the selected GPIO is not an RTC GPIO,
  217. * or the mode is invalid
  218. * - ESP_ERR_INVALID_STATE if wakeup triggers conflict
  219. */
  220. esp_err_t esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level);
  221. #endif // SOC_PM_SUPPORT_EXT0_WAKEUP
  222. #if SOC_PM_SUPPORT_EXT1_WAKEUP
  223. /**
  224. * @brief Enable wakeup using multiple pins
  225. *
  226. * This function uses external wakeup feature of RTC controller.
  227. * It will work even if RTC peripherals are shut down during sleep.
  228. *
  229. * This feature can monitor any number of pins which are in RTC IOs.
  230. * Once selected pins go into the state given by level_mode argument,
  231. * the chip will be woken up.
  232. *
  233. * @note This function does not modify pin configuration. The pins are
  234. * configured in esp_deep_sleep_start/esp_light_sleep_start,
  235. * immediately before entering sleep mode.
  236. *
  237. * @note Internal pullups and pulldowns don't work when RTC peripherals are
  238. * shut down. In this case, external resistors need to be added.
  239. * Alternatively, RTC peripherals (and pullups/pulldowns) may be
  240. * kept enabled using esp_sleep_pd_config function. If we turn off the
  241. * ``RTC_PERIPH`` domain or certain chips lack the ``RTC_PERIPH`` domain,
  242. * we will use the HOLD feature to maintain the pull-up and pull-down on
  243. * the pins during sleep. HOLD feature will be acted on the pin internally
  244. * before the system entering sleep, and this can further reduce power consumption.
  245. *
  246. * @param io_mask Bit mask of GPIO numbers which will cause wakeup. Only GPIOs
  247. * which have RTC functionality can be used in this bit map.
  248. * For different SoCs, the related GPIOs are:
  249. * - ESP32: 0, 2, 4, 12-15, 25-27, 32-39
  250. * - ESP32-S2: 0-21
  251. * - ESP32-S3: 0-21
  252. * - ESP32-C6: 0-7
  253. * - ESP32-H2: 7-14
  254. * @param level_mode Select logic function used to determine wakeup condition:
  255. * When target chip is ESP32:
  256. * - ESP_EXT1_WAKEUP_ALL_LOW: wake up when all selected GPIOs are low
  257. * - ESP_EXT1_WAKEUP_ANY_HIGH: wake up when any of the selected GPIOs is high
  258. * When target chip is ESP32-S2, ESP32-S3, ESP32-C6 or ESP32-H2:
  259. * - ESP_EXT1_WAKEUP_ANY_LOW: wake up when any of the selected GPIOs is low
  260. * - ESP_EXT1_WAKEUP_ANY_HIGH: wake up when any of the selected GPIOs is high
  261. * @return
  262. * - ESP_OK on success
  263. * - ESP_ERR_INVALID_ARG if any of the selected GPIOs is not an RTC GPIO,
  264. * or mode is invalid
  265. */
  266. esp_err_t esp_sleep_enable_ext1_wakeup(uint64_t io_mask, esp_sleep_ext1_wakeup_mode_t level_mode);
  267. #if SOC_PM_SUPPORT_EXT1_WAKEUP_MODE_PER_PIN
  268. /**
  269. * @brief Enable wakeup using multiple pins, allows different trigger mode per pin
  270. *
  271. * This function uses external wakeup feature of RTC controller.
  272. * It will work even if RTC peripherals are shut down during sleep.
  273. *
  274. * This feature can monitor any number of pins which are in RTC IOs.
  275. * Once selected pins go into the state given by level_mode argument,
  276. * the chip will be woken up.
  277. *
  278. * @note This function does not modify pin configuration. The pins are
  279. * configured in esp_deep_sleep_start/esp_light_sleep_start,
  280. * immediately before entering sleep mode.
  281. *
  282. * @note Internal pullups and pulldowns don't work when RTC peripherals are
  283. * shut down. In this case, external resistors need to be added.
  284. * Alternatively, RTC peripherals (and pullups/pulldowns) may be
  285. * kept enabled using esp_sleep_pd_config function. If we turn off the
  286. * ``RTC_PERIPH`` domain or certain chips lack the ``RTC_PERIPH`` domain,
  287. * we will use the HOLD feature to maintain the pull-up and pull-down on
  288. * the pins during sleep. HOLD feature will be acted on the pin internally
  289. * before the system entering sleep, and this can further reduce power consumption.
  290. *
  291. * @param io_mask Bit mask of GPIO numbers which will cause wakeup. Only GPIOs
  292. * which have RTC functionality can be used in this bit map.
  293. * For different SoCs, the related GPIOs are:
  294. * - ESP32-C6: 0-7.
  295. * - ESP32-H2: 7-14.
  296. * @param level_mask Select logic function used to determine wakeup condition per pin.
  297. * Each bit of the level_mask corresponds to the respective GPIO. Each bit's corresponding
  298. * position is set to 0, the wakeup level will be low, on the contrary,
  299. * each bit's corresponding position is set to 1, the wakeup level will be high.
  300. * @return
  301. * - ESP_OK on success
  302. * - ESP_ERR_INVALID_ARG if any of the selected GPIOs is not an RTC GPIO,
  303. * or mode is invalid
  304. */
  305. esp_err_t esp_sleep_enable_ext1_wakeup_with_level_mask(uint64_t io_mask, uint64_t level_mask);
  306. #endif // SOC_PM_SUPPORT_EXT1_WAKEUP_MODE_PER_PIN
  307. #endif // SOC_PM_SUPPORT_EXT1_WAKEUP
  308. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  309. /**
  310. * @brief Enable wakeup using specific gpio pins
  311. *
  312. * This function enables an IO pin to wake up the chip from deep sleep.
  313. *
  314. * @note This function does not modify pin configuration. The pins are
  315. * configured inside esp_deep_sleep_start, immediately before entering sleep mode.
  316. *
  317. * @note You don't need to care to pull-up or pull-down before using this
  318. * function, because this will be set internally in esp_deep_sleep_start
  319. * based on the wakeup mode. BTW, when you use low level to wake up the
  320. * chip, we strongly recommend you to add external resistors (pull-up).
  321. *
  322. * @param gpio_pin_mask Bit mask of GPIO numbers which will cause wakeup. Only GPIOs
  323. * which have RTC functionality (pads that powered by VDD3P3_RTC) can be used in this bit map.
  324. * @param mode Select logic function used to determine wakeup condition:
  325. * - ESP_GPIO_WAKEUP_GPIO_LOW: wake up when the gpio turn to low.
  326. * - ESP_GPIO_WAKEUP_GPIO_HIGH: wake up when the gpio turn to high.
  327. * @return
  328. * - ESP_OK on success
  329. * - ESP_ERR_INVALID_ARG if the mask contains any invalid deep sleep wakeup pin or wakeup mode is invalid
  330. */
  331. esp_err_t esp_deep_sleep_enable_gpio_wakeup(uint64_t gpio_pin_mask, esp_deepsleep_gpio_wake_up_mode_t mode);
  332. #endif
  333. /**
  334. * @brief Enable wakeup from light sleep using GPIOs
  335. *
  336. * Each GPIO supports wakeup function, which can be triggered on either low level
  337. * or high level. Unlike EXT0 and EXT1 wakeup sources, this method can be used
  338. * both for all IOs: RTC IOs and digital IOs. It can only be used to wakeup from
  339. * light sleep though.
  340. *
  341. * To enable wakeup, first call gpio_wakeup_enable, specifying gpio number and
  342. * wakeup level, for each GPIO which is used for wakeup.
  343. * Then call this function to enable wakeup feature.
  344. *
  345. * @note On ESP32, GPIO wakeup source can not be used together with touch or ULP wakeup sources.
  346. *
  347. * @return
  348. * - ESP_OK on success
  349. * - ESP_ERR_INVALID_STATE if wakeup triggers conflict
  350. */
  351. esp_err_t esp_sleep_enable_gpio_wakeup(void);
  352. /**
  353. * @brief Enable wakeup from light sleep using UART
  354. *
  355. * Use uart_set_wakeup_threshold function to configure UART wakeup threshold.
  356. *
  357. * Wakeup from light sleep takes some time, so not every character sent
  358. * to the UART can be received by the application.
  359. *
  360. * @note ESP32 does not support wakeup from UART2.
  361. *
  362. * @param uart_num UART port to wake up from
  363. * @return
  364. * - ESP_OK on success
  365. * - ESP_ERR_INVALID_ARG if wakeup from given UART is not supported
  366. */
  367. esp_err_t esp_sleep_enable_uart_wakeup(int uart_num);
  368. /**
  369. * @brief Enable wakeup by bluetooth
  370. * @return
  371. * - ESP_OK on success
  372. * - ESP_ERR_NOT_SUPPORTED if wakeup from bluetooth is not supported
  373. */
  374. esp_err_t esp_sleep_enable_bt_wakeup(void);
  375. /**
  376. * @brief Disable wakeup by bluetooth
  377. * @return
  378. * - ESP_OK on success
  379. * - ESP_ERR_NOT_SUPPORTED if wakeup from bluetooth is not supported
  380. */
  381. esp_err_t esp_sleep_disable_bt_wakeup(void);
  382. /**
  383. * @brief Enable wakeup by WiFi MAC
  384. * @return
  385. * - ESP_OK on success
  386. */
  387. esp_err_t esp_sleep_enable_wifi_wakeup(void);
  388. /**
  389. * @brief Disable wakeup by WiFi MAC
  390. * @return
  391. * - ESP_OK on success
  392. */
  393. esp_err_t esp_sleep_disable_wifi_wakeup(void);
  394. /**
  395. * @brief Enable beacon wakeup by WiFi MAC, it will wake up the system into modem state
  396. * @return
  397. * - ESP_OK on success
  398. */
  399. esp_err_t esp_sleep_enable_wifi_beacon_wakeup(void);
  400. /**
  401. * @brief Disable beacon wakeup by WiFi MAC
  402. * @return
  403. * - ESP_OK on success
  404. */
  405. esp_err_t esp_sleep_disable_wifi_beacon_wakeup(void);
  406. /**
  407. * @brief Get the bit mask of GPIOs which caused wakeup (ext1)
  408. *
  409. * If wakeup was caused by another source, this function will return 0.
  410. *
  411. * @return bit mask, if GPIOn caused wakeup, BIT(n) will be set
  412. */
  413. uint64_t esp_sleep_get_ext1_wakeup_status(void);
  414. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  415. /**
  416. * @brief Get the bit mask of GPIOs which caused wakeup (gpio)
  417. *
  418. * If wakeup was caused by another source, this function will return 0.
  419. *
  420. * @return bit mask, if GPIOn caused wakeup, BIT(n) will be set
  421. */
  422. uint64_t esp_sleep_get_gpio_wakeup_status(void);
  423. #endif //SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  424. /**
  425. * @brief Set power down mode for an RTC power domain in sleep mode
  426. *
  427. * If not set set using this API, all power domains default to ESP_PD_OPTION_AUTO.
  428. *
  429. * @param domain power domain to configure
  430. * @param option power down option (ESP_PD_OPTION_OFF, ESP_PD_OPTION_ON, or ESP_PD_OPTION_AUTO)
  431. * @return
  432. * - ESP_OK on success
  433. * - ESP_ERR_INVALID_ARG if either of the arguments is out of range
  434. */
  435. esp_err_t esp_sleep_pd_config(esp_sleep_pd_domain_t domain,
  436. esp_sleep_pd_option_t option);
  437. /**
  438. * @brief Enter deep sleep with the configured wakeup options
  439. *
  440. * @note In general, the function does not return, but if the sleep is rejected,
  441. * then it returns from it.
  442. *
  443. * The reason for the rejection can be such as a short sleep time.
  444. */
  445. void esp_deep_sleep_start(void);
  446. /**
  447. * @brief Enter light sleep with the configured wakeup options
  448. *
  449. * @return
  450. * - ESP_OK on success (returned after wakeup)
  451. * - ESP_ERR_SLEEP_REJECT sleep request is rejected(wakeup source set before the sleep request)
  452. * - ESP_ERR_SLEEP_TOO_SHORT_SLEEP_DURATION after deducting the sleep flow overhead, the final sleep duration
  453. * is too short to cover the minimum sleep duration of the chip, when
  454. * rtc timer wakeup source enabled
  455. */
  456. esp_err_t esp_light_sleep_start(void);
  457. /**
  458. * @brief Enter deep-sleep mode
  459. *
  460. * The device will automatically wake up after the deep-sleep time
  461. * Upon waking up, the device calls deep sleep wake stub, and then proceeds
  462. * to load application.
  463. *
  464. * Call to this function is equivalent to a call to esp_deep_sleep_enable_timer_wakeup
  465. * followed by a call to esp_deep_sleep_start.
  466. *
  467. * @note In general, the function does not return, but if the sleep is rejected,
  468. * then it returns from it.
  469. *
  470. * The reason for the rejection can be such as a short sleep time.
  471. *
  472. * @param time_in_us deep-sleep time, unit: microsecond
  473. */
  474. void esp_deep_sleep(uint64_t time_in_us);
  475. /**
  476. * @brief Register a callback to be called from the deep sleep prepare
  477. *
  478. * @warning deepsleep callbacks should without parameters, and MUST NOT,
  479. * UNDER ANY CIRCUMSTANCES, CALL A FUNCTION THAT MIGHT BLOCK.
  480. *
  481. * @param new_dslp_cb Callback to be called
  482. *
  483. * @return
  484. * - ESP_OK: Callback registered to the deepsleep misc_modules_sleep_prepare
  485. * - ESP_ERR_NO_MEM: No more hook space for register the callback
  486. */
  487. esp_err_t esp_deep_sleep_register_hook(esp_deep_sleep_cb_t new_dslp_cb);
  488. /**
  489. * @brief Unregister an deepsleep callback
  490. *
  491. * @param old_dslp_cb Callback to be unregistered
  492. */
  493. void esp_deep_sleep_deregister_hook(esp_deep_sleep_cb_t old_dslp_cb);
  494. /**
  495. * @brief Get the wakeup source which caused wakeup from sleep
  496. *
  497. * @return cause of wake up from last sleep (deep sleep or light sleep)
  498. */
  499. esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause(void);
  500. /**
  501. * @brief Default stub to run on wake from deep sleep.
  502. *
  503. * Allows for executing code immediately on wake from sleep, before
  504. * the software bootloader or ESP-IDF app has started up.
  505. *
  506. * This function is weak-linked, so you can implement your own version
  507. * to run code immediately when the chip wakes from
  508. * sleep.
  509. *
  510. * See docs/deep-sleep-stub.rst for details.
  511. */
  512. void esp_wake_deep_sleep(void);
  513. /**
  514. * @brief Function type for stub to run on wake from sleep.
  515. *
  516. */
  517. typedef void (*esp_deep_sleep_wake_stub_fn_t)(void);
  518. /**
  519. * @brief Install a new stub at runtime to run on wake from deep sleep
  520. *
  521. * If implementing esp_wake_deep_sleep() then it is not necessary to
  522. * call this function.
  523. *
  524. * However, it is possible to call this function to substitute a
  525. * different deep sleep stub. Any function used as a deep sleep stub
  526. * must be marked RTC_IRAM_ATTR, and must obey the same rules given
  527. * for esp_wake_deep_sleep().
  528. */
  529. void esp_set_deep_sleep_wake_stub(esp_deep_sleep_wake_stub_fn_t new_stub);
  530. /**
  531. * @brief Set wake stub entry to default `esp_wake_stub_entry`
  532. */
  533. void esp_set_deep_sleep_wake_stub_default_entry(void);
  534. /**
  535. * @brief Get current wake from deep sleep stub
  536. * @return Return current wake from deep sleep stub, or NULL if
  537. * no stub is installed.
  538. */
  539. esp_deep_sleep_wake_stub_fn_t esp_get_deep_sleep_wake_stub(void);
  540. /**
  541. * @brief The default esp-idf-provided esp_wake_deep_sleep() stub.
  542. *
  543. * See docs/deep-sleep-stub.rst for details.
  544. */
  545. void esp_default_wake_deep_sleep(void);
  546. /**
  547. * @brief Disable logging from the ROM code after deep sleep.
  548. *
  549. * Using LSB of RTC_STORE4.
  550. */
  551. void esp_deep_sleep_disable_rom_logging(void);
  552. #ifdef SOC_PM_SUPPORT_CPU_PD
  553. #if SOC_PM_CPU_RETENTION_BY_RTCCNTL
  554. /**
  555. * @brief CPU Power down low-level initialize, enable CPU power down during light sleep
  556. * @return
  557. * - ESP_OK on success
  558. * - ESP_ERR_NO_MEM not enough retention memory
  559. */
  560. esp_err_t esp_sleep_cpu_pd_low_init(void);
  561. /**
  562. * @brief CPU Power down low-level deinitialize, disable CPU power down during light sleep
  563. * @return
  564. * - ESP_OK on success
  565. * - ESP_ERR_NO_MEM not enough retention memory
  566. */
  567. esp_err_t esp_sleep_cpu_pd_low_deinit(void);
  568. #endif
  569. /**
  570. * @brief CPU Power down initialize
  571. *
  572. * @return
  573. * - ESP_OK on success
  574. * - ESP_ERR_NO_MEM not enough retention memory
  575. */
  576. esp_err_t esp_sleep_cpu_retention_init(void);
  577. /**
  578. * @brief CPU Power down de-initialize
  579. *
  580. * @return
  581. * - ESP_OK on success
  582. *
  583. * Release system retention memory.
  584. */
  585. esp_err_t esp_sleep_cpu_retention_deinit(void);
  586. #endif
  587. /**
  588. * @brief Configure to isolate all GPIO pins in sleep state
  589. */
  590. void esp_sleep_config_gpio_isolate(void);
  591. /**
  592. * @brief Enable or disable GPIO pins status switching between slept status and waked status.
  593. * @param enable decide whether to switch status or not
  594. */
  595. void esp_sleep_enable_gpio_switch(bool enable);
  596. #if CONFIG_MAC_BB_PD
  597. /**
  598. * @brief Function type for stub to run mac bb power down.
  599. */
  600. typedef void (* mac_bb_power_down_cb_t)(void);
  601. /**
  602. * @brief Function type for stub to run mac bb power up.
  603. */
  604. typedef void (* mac_bb_power_up_cb_t)(void);
  605. /**
  606. * @brief Registet mac bb power down callback.
  607. * @param cb mac bb power down callback.
  608. * @return
  609. * - ESP_OK on success
  610. */
  611. esp_err_t esp_register_mac_bb_pd_callback(mac_bb_power_down_cb_t cb);
  612. /**
  613. * @brief Unregistet mac bb power down callback.
  614. * @param cb mac bb power down callback.
  615. * @return
  616. * - ESP_OK on success
  617. */
  618. esp_err_t esp_unregister_mac_bb_pd_callback(mac_bb_power_down_cb_t cb);
  619. /**
  620. * @brief Registet mac bb power up callback.
  621. * @param cb mac bb power up callback.
  622. * @return
  623. * - ESP_OK on success
  624. */
  625. esp_err_t esp_register_mac_bb_pu_callback(mac_bb_power_up_cb_t cb);
  626. /**
  627. * @brief Unregistet mac bb power up callback.
  628. * @param cb mac bb power up callback.
  629. * @return
  630. * - ESP_OK on success
  631. */
  632. esp_err_t esp_unregister_mac_bb_pu_callback(mac_bb_power_up_cb_t cb);
  633. #endif
  634. #ifdef __cplusplus
  635. }
  636. #endif