esp_sleep.h 13 KB

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