esp_sleep.h 15 KB

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