esp_sleep.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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 worry about pull-up or pull-down resistors before
  318. * using this function because the ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS
  319. * option is enabled by default. It will automatically set pull-up or pull-down
  320. * resistors internally in esp_deep_sleep_start based on the wakeup mode. However,
  321. * when using external pull-up or pull-down resistors, please be sure to disable
  322. * the ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS option, as the combination of internal
  323. * and external resistors may cause interference. BTW, when you use low level to wake up the
  324. * chip, we strongly recommend you to add external resistors (pull-up).
  325. *
  326. * @param gpio_pin_mask Bit mask of GPIO numbers which will cause wakeup. Only GPIOs
  327. * which have RTC functionality (pads that powered by VDD3P3_RTC) can be used in this bit map.
  328. * @param mode Select logic function used to determine wakeup condition:
  329. * - ESP_GPIO_WAKEUP_GPIO_LOW: wake up when the gpio turn to low.
  330. * - ESP_GPIO_WAKEUP_GPIO_HIGH: wake up when the gpio turn to high.
  331. * @return
  332. * - ESP_OK on success
  333. * - ESP_ERR_INVALID_ARG if the mask contains any invalid deep sleep wakeup pin or wakeup mode is invalid
  334. */
  335. esp_err_t esp_deep_sleep_enable_gpio_wakeup(uint64_t gpio_pin_mask, esp_deepsleep_gpio_wake_up_mode_t mode);
  336. #endif
  337. /**
  338. * @brief Enable wakeup from light sleep using GPIOs
  339. *
  340. * Each GPIO supports wakeup function, which can be triggered on either low level
  341. * or high level. Unlike EXT0 and EXT1 wakeup sources, this method can be used
  342. * both for all IOs: RTC IOs and digital IOs. It can only be used to wakeup from
  343. * light sleep though.
  344. *
  345. * To enable wakeup, first call gpio_wakeup_enable, specifying gpio number and
  346. * wakeup level, for each GPIO which is used for wakeup.
  347. * Then call this function to enable wakeup feature.
  348. *
  349. * @note On ESP32, GPIO wakeup source can not be used together with touch or ULP wakeup sources.
  350. *
  351. * @return
  352. * - ESP_OK on success
  353. * - ESP_ERR_INVALID_STATE if wakeup triggers conflict
  354. */
  355. esp_err_t esp_sleep_enable_gpio_wakeup(void);
  356. /**
  357. * @brief Enable wakeup from light sleep using UART
  358. *
  359. * Use uart_set_wakeup_threshold function to configure UART wakeup threshold.
  360. *
  361. * Wakeup from light sleep takes some time, so not every character sent
  362. * to the UART can be received by the application.
  363. *
  364. * @note ESP32 does not support wakeup from UART2.
  365. *
  366. * @param uart_num UART port to wake up from
  367. * @return
  368. * - ESP_OK on success
  369. * - ESP_ERR_INVALID_ARG if wakeup from given UART is not supported
  370. */
  371. esp_err_t esp_sleep_enable_uart_wakeup(int uart_num);
  372. /**
  373. * @brief Enable wakeup by bluetooth
  374. * @return
  375. * - ESP_OK on success
  376. * - ESP_ERR_NOT_SUPPORTED if wakeup from bluetooth is not supported
  377. */
  378. esp_err_t esp_sleep_enable_bt_wakeup(void);
  379. /**
  380. * @brief Disable wakeup by bluetooth
  381. * @return
  382. * - ESP_OK on success
  383. * - ESP_ERR_NOT_SUPPORTED if wakeup from bluetooth is not supported
  384. */
  385. esp_err_t esp_sleep_disable_bt_wakeup(void);
  386. /**
  387. * @brief Enable wakeup by WiFi MAC
  388. * @return
  389. * - ESP_OK on success
  390. */
  391. esp_err_t esp_sleep_enable_wifi_wakeup(void);
  392. /**
  393. * @brief Disable wakeup by WiFi MAC
  394. * @return
  395. * - ESP_OK on success
  396. */
  397. esp_err_t esp_sleep_disable_wifi_wakeup(void);
  398. /**
  399. * @brief Enable beacon wakeup by WiFi MAC, it will wake up the system into modem state
  400. * @return
  401. * - ESP_OK on success
  402. */
  403. esp_err_t esp_sleep_enable_wifi_beacon_wakeup(void);
  404. /**
  405. * @brief Disable beacon wakeup by WiFi MAC
  406. * @return
  407. * - ESP_OK on success
  408. */
  409. esp_err_t esp_sleep_disable_wifi_beacon_wakeup(void);
  410. /**
  411. * @brief Get the bit mask of GPIOs which caused wakeup (ext1)
  412. *
  413. * If wakeup was caused by another source, this function will return 0.
  414. *
  415. * @return bit mask, if GPIOn caused wakeup, BIT(n) will be set
  416. */
  417. uint64_t esp_sleep_get_ext1_wakeup_status(void);
  418. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  419. /**
  420. * @brief Get the bit mask of GPIOs which caused wakeup (gpio)
  421. *
  422. * If wakeup was caused by another source, this function will return 0.
  423. *
  424. * @return bit mask, if GPIOn caused wakeup, BIT(n) will be set
  425. */
  426. uint64_t esp_sleep_get_gpio_wakeup_status(void);
  427. #endif //SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  428. /**
  429. * @brief Set power down mode for an RTC power domain in sleep mode
  430. *
  431. * If not set set using this API, all power domains default to ESP_PD_OPTION_AUTO.
  432. *
  433. * @param domain power domain to configure
  434. * @param option power down option (ESP_PD_OPTION_OFF, ESP_PD_OPTION_ON, or ESP_PD_OPTION_AUTO)
  435. * @return
  436. * - ESP_OK on success
  437. * - ESP_ERR_INVALID_ARG if either of the arguments is out of range
  438. */
  439. esp_err_t esp_sleep_pd_config(esp_sleep_pd_domain_t domain,
  440. esp_sleep_pd_option_t option);
  441. /**
  442. * @brief Enter deep sleep with the configured wakeup options
  443. *
  444. * @note In general, the function does not return, but if the sleep is rejected,
  445. * then it returns from it.
  446. *
  447. * The reason for the rejection can be such as a short sleep time.
  448. *
  449. * @return
  450. * - No return - If the sleep is not rejected.
  451. * - ESP_ERR_SLEEP_REJECT sleep request is rejected(wakeup source set before the sleep request)
  452. */
  453. esp_err_t esp_deep_sleep_try_to_start(void);
  454. /**
  455. * @brief Enter deep sleep with the configured wakeup options
  456. *
  457. * @note The function does not do a return (no rejection). Even if wakeup source set before the sleep request
  458. * it goes to deep sleep anyway.
  459. */
  460. void esp_deep_sleep_start(void) __attribute__((__noreturn__));
  461. /**
  462. * @brief Enter light sleep with the configured wakeup options
  463. *
  464. * @return
  465. * - ESP_OK on success (returned after wakeup)
  466. * - ESP_ERR_SLEEP_REJECT sleep request is rejected(wakeup source set before the sleep request)
  467. * - ESP_ERR_SLEEP_TOO_SHORT_SLEEP_DURATION after deducting the sleep flow overhead, the final sleep duration
  468. * is too short to cover the minimum sleep duration of the chip, when
  469. * rtc timer wakeup source enabled
  470. */
  471. esp_err_t esp_light_sleep_start(void);
  472. /**
  473. * @brief Enter deep-sleep mode
  474. *
  475. * The device will automatically wake up after the deep-sleep time
  476. * Upon waking up, the device calls deep sleep wake stub, and then proceeds
  477. * to load application.
  478. *
  479. * Call to this function is equivalent to a call to esp_deep_sleep_enable_timer_wakeup
  480. * followed by a call to esp_deep_sleep_start.
  481. *
  482. * @note In general, the function does not return, but if the sleep is rejected,
  483. * then it returns from it.
  484. *
  485. * The reason for the rejection can be such as a short sleep time.
  486. *
  487. * @param time_in_us deep-sleep time, unit: microsecond
  488. *
  489. * @return
  490. * - No return - If the sleep is not rejected.
  491. * - ESP_ERR_SLEEP_REJECT sleep request is rejected(wakeup source set before the sleep request)
  492. */
  493. esp_err_t esp_deep_sleep_try(uint64_t time_in_us);
  494. /**
  495. * @brief Enter deep-sleep mode
  496. *
  497. * The device will automatically wake up after the deep-sleep time
  498. * Upon waking up, the device calls deep sleep wake stub, and then proceeds
  499. * to load application.
  500. *
  501. * Call to this function is equivalent to a call to esp_deep_sleep_enable_timer_wakeup
  502. * followed by a call to esp_deep_sleep_start.
  503. *
  504. * @note The function does not do a return (no rejection).. Even if wakeup source set before the sleep request
  505. * it goes to deep sleep anyway.
  506. *
  507. * @param time_in_us deep-sleep time, unit: microsecond
  508. */
  509. void esp_deep_sleep(uint64_t time_in_us) __attribute__((__noreturn__));
  510. /**
  511. * @brief Register a callback to be called from the deep sleep prepare
  512. *
  513. * @warning deepsleep callbacks should without parameters, and MUST NOT,
  514. * UNDER ANY CIRCUMSTANCES, CALL A FUNCTION THAT MIGHT BLOCK.
  515. *
  516. * @param new_dslp_cb Callback to be called
  517. *
  518. * @return
  519. * - ESP_OK: Callback registered to the deepsleep misc_modules_sleep_prepare
  520. * - ESP_ERR_NO_MEM: No more hook space for register the callback
  521. */
  522. esp_err_t esp_deep_sleep_register_hook(esp_deep_sleep_cb_t new_dslp_cb);
  523. /**
  524. * @brief Unregister an deepsleep callback
  525. *
  526. * @param old_dslp_cb Callback to be unregistered
  527. */
  528. void esp_deep_sleep_deregister_hook(esp_deep_sleep_cb_t old_dslp_cb);
  529. /**
  530. * @brief Get the wakeup source which caused wakeup from sleep
  531. *
  532. * @return cause of wake up from last sleep (deep sleep or light sleep)
  533. */
  534. esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause(void);
  535. /**
  536. * @brief Default stub to run on wake from deep sleep.
  537. *
  538. * Allows for executing code immediately on wake from sleep, before
  539. * the software bootloader or ESP-IDF app has started up.
  540. *
  541. * This function is weak-linked, so you can implement your own version
  542. * to run code immediately when the chip wakes from
  543. * sleep.
  544. *
  545. * See docs/deep-sleep-stub.rst for details.
  546. */
  547. void esp_wake_deep_sleep(void);
  548. /**
  549. * @brief Function type for stub to run on wake from sleep.
  550. *
  551. */
  552. typedef void (*esp_deep_sleep_wake_stub_fn_t)(void);
  553. /**
  554. * @brief Install a new stub at runtime to run on wake from deep sleep
  555. *
  556. * If implementing esp_wake_deep_sleep() then it is not necessary to
  557. * call this function.
  558. *
  559. * However, it is possible to call this function to substitute a
  560. * different deep sleep stub. Any function used as a deep sleep stub
  561. * must be marked RTC_IRAM_ATTR, and must obey the same rules given
  562. * for esp_wake_deep_sleep().
  563. */
  564. void esp_set_deep_sleep_wake_stub(esp_deep_sleep_wake_stub_fn_t new_stub);
  565. /**
  566. * @brief Set wake stub entry to default `esp_wake_stub_entry`
  567. */
  568. void esp_set_deep_sleep_wake_stub_default_entry(void);
  569. /**
  570. * @brief Get current wake from deep sleep stub
  571. * @return Return current wake from deep sleep stub, or NULL if
  572. * no stub is installed.
  573. */
  574. esp_deep_sleep_wake_stub_fn_t esp_get_deep_sleep_wake_stub(void);
  575. /**
  576. * @brief The default esp-idf-provided esp_wake_deep_sleep() stub.
  577. *
  578. * See docs/deep-sleep-stub.rst for details.
  579. */
  580. void esp_default_wake_deep_sleep(void);
  581. /**
  582. * @brief Disable logging from the ROM code after deep sleep.
  583. *
  584. * Using LSB of RTC_STORE4.
  585. */
  586. void esp_deep_sleep_disable_rom_logging(void);
  587. #ifdef SOC_PM_SUPPORT_CPU_PD
  588. #if SOC_PM_CPU_RETENTION_BY_RTCCNTL
  589. /**
  590. * @brief CPU Power down low-level initialize, enable CPU power down during light sleep
  591. * @return
  592. * - ESP_OK on success
  593. * - ESP_ERR_NO_MEM not enough retention memory
  594. */
  595. esp_err_t esp_sleep_cpu_pd_low_init(void);
  596. /**
  597. * @brief CPU Power down low-level deinitialize, disable CPU power down during light sleep
  598. * @return
  599. * - ESP_OK on success
  600. * - ESP_ERR_NO_MEM not enough retention memory
  601. */
  602. esp_err_t esp_sleep_cpu_pd_low_deinit(void);
  603. #endif
  604. /**
  605. * @brief CPU Power down initialize
  606. *
  607. * @return
  608. * - ESP_OK on success
  609. * - ESP_ERR_NO_MEM not enough retention memory
  610. */
  611. esp_err_t esp_sleep_cpu_retention_init(void);
  612. /**
  613. * @brief CPU Power down de-initialize
  614. *
  615. * @return
  616. * - ESP_OK on success
  617. *
  618. * Release system retention memory.
  619. */
  620. esp_err_t esp_sleep_cpu_retention_deinit(void);
  621. #endif
  622. /**
  623. * @brief Configure to isolate all GPIO pins in sleep state
  624. */
  625. void esp_sleep_config_gpio_isolate(void);
  626. /**
  627. * @brief Enable or disable GPIO pins status switching between slept status and waked status.
  628. * @param enable decide whether to switch status or not
  629. */
  630. void esp_sleep_enable_gpio_switch(bool enable);
  631. #if CONFIG_MAC_BB_PD
  632. /**
  633. * @brief Function type for stub to run mac bb power down.
  634. */
  635. typedef void (* mac_bb_power_down_cb_t)(void);
  636. /**
  637. * @brief Function type for stub to run mac bb power up.
  638. */
  639. typedef void (* mac_bb_power_up_cb_t)(void);
  640. /**
  641. * @brief Registet mac bb power down callback.
  642. * @param cb mac bb power down callback.
  643. * @return
  644. * - ESP_OK on success
  645. */
  646. esp_err_t esp_register_mac_bb_pd_callback(mac_bb_power_down_cb_t cb);
  647. /**
  648. * @brief Unregistet mac bb power down callback.
  649. * @param cb mac bb power down callback.
  650. * @return
  651. * - ESP_OK on success
  652. */
  653. esp_err_t esp_unregister_mac_bb_pd_callback(mac_bb_power_down_cb_t cb);
  654. /**
  655. * @brief Registet mac bb power up callback.
  656. * @param cb mac bb power up callback.
  657. * @return
  658. * - ESP_OK on success
  659. */
  660. esp_err_t esp_register_mac_bb_pu_callback(mac_bb_power_up_cb_t cb);
  661. /**
  662. * @brief Unregistet mac bb power up callback.
  663. * @param cb mac bb power up callback.
  664. * @return
  665. * - ESP_OK on success
  666. */
  667. esp_err_t esp_unregister_mac_bb_pu_callback(mac_bb_power_up_cb_t cb);
  668. #endif
  669. #ifdef __cplusplus
  670. }
  671. #endif