esp_sleep.h 12 KB

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