esp_deep_sleep.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Copyright 2015-2016 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_ext1_wakeup_mode_t;
  29. /**
  30. * @brief Power domains which can be powered down in deep sleep
  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_MAX //!< Number of domains
  37. } esp_deep_sleep_pd_domain_t;
  38. /**
  39. * @brief Power down options
  40. */
  41. typedef enum {
  42. ESP_PD_OPTION_OFF, //!< Power down the power domain in deep sleep
  43. ESP_PD_OPTION_ON, //!< Keep power domain enabled during deep sleep
  44. ESP_PD_OPTION_AUTO //!< Keep power domain enabled in deep sleep, if it is needed by one of the wakeup options. Otherwise power it down.
  45. } esp_deep_sleep_pd_option_t;
  46. /**
  47. * @brief Deep sleep wakeup cause
  48. */
  49. typedef enum {
  50. ESP_DEEP_SLEEP_WAKEUP_UNDEFINED, //! Wakeup was not caused by deep sleep
  51. ESP_DEEP_SLEEP_WAKEUP_EXT0, //! Wakeup caused by external signal using RTC_IO
  52. ESP_DEEP_SLEEP_WAKEUP_EXT1, //! Wakeup caused by external signal using RTC_CNTL
  53. ESP_DEEP_SLEEP_WAKEUP_TIMER, //! Wakeup caused by timer
  54. ESP_DEEP_SLEEP_WAKEUP_TOUCHPAD, //! Wakeup caused by touchpad
  55. ESP_DEEP_SLEEP_WAKEUP_ULP, //! Wakeup caused by ULP program
  56. } esp_deep_sleep_wakeup_cause_t;
  57. /**
  58. * @brief Enable wakeup by ULP coprocessor
  59. * @note In revisions 0 and 1 of the ESP32, ULP wakeup source
  60. * can not be used when RTC_PERIPH power domain is forced
  61. * to be powered on (ESP_PD_OPTION_ON) or when ext0 wakeup
  62. * source is used.
  63. * @return
  64. * - ESP_OK on success
  65. * - ESP_ERR_INVALID_STATE if ULP co-processor is not enabled or if wakeup triggers conflict
  66. */
  67. esp_err_t esp_deep_sleep_enable_ulp_wakeup();
  68. /**
  69. * @brief Enable wakeup by timer
  70. * @param time_in_us time before wakeup, in microseconds
  71. * @return
  72. * - ESP_OK on success
  73. * - ESP_ERR_INVALID_ARG if value is out of range (TBD)
  74. */
  75. esp_err_t esp_deep_sleep_enable_timer_wakeup(uint64_t time_in_us);
  76. /**
  77. * @brief Enable wakeup by touch sensor
  78. *
  79. * @note In revisions 0 and 1 of the ESP32, touch wakeup source
  80. * can not be used when RTC_PERIPH power domain is forced
  81. * to be powered on (ESP_PD_OPTION_ON) or when ext0 wakeup
  82. * source is used.
  83. *
  84. * @return
  85. * - ESP_OK on success
  86. * - ESP_ERR_INVALID_STATE if wakeup triggers conflict
  87. */
  88. esp_err_t esp_deep_sleep_enable_touchpad_wakeup();
  89. /**
  90. * @brief Get the touch pad which caused wakeup
  91. *
  92. * If wakeup was caused by another source, this function will return TOUCH_PAD_MAX;
  93. *
  94. * @return touch pad which caused wakeup
  95. */
  96. touch_pad_t esp_deep_sleep_get_touchpad_wakeup_status();
  97. /**
  98. * @brief Enable wakeup using a pin
  99. *
  100. * This function uses external wakeup feature of RTC_IO peripheral.
  101. * It will work only if RTC peripherals are kept on during deep sleep.
  102. *
  103. * This feature can monitor any pin which is an RTC IO. Once the pin transitions
  104. * into the state given by level argument, the chip will be woken up.
  105. *
  106. * @note This function does not modify pin configuration. The pin is
  107. * configured in esp_deep_sleep_start, immediately before
  108. * entering deep sleep.
  109. *
  110. * @note In revisions 0 and 1 of the ESP32, ext0 wakeup source
  111. * can not be used together with touch or ULP wakeup sources.
  112. *
  113. * @param gpio_num GPIO number used as wakeup source. Only GPIOs which are have RTC
  114. * functionality can be used: 0,2,4,12-15,25-27,32-39.
  115. * @param level input level which will trigger wakeup (0=low, 1=high)
  116. * @return
  117. * - ESP_OK on success
  118. * - ESP_ERR_INVALID_ARG if the selected GPIO is not an RTC GPIO,
  119. * or the mode is invalid
  120. * - ESP_ERR_INVALID_STATE if wakeup triggers conflict
  121. */
  122. esp_err_t esp_deep_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level);
  123. /**
  124. * @brief Enable wakeup using multiple pins
  125. *
  126. * This function uses external wakeup feature of RTC controller.
  127. * It will work even if RTC peripherals are shut down during deep sleep.
  128. *
  129. * This feature can monitor any number of pins which are in RTC IOs.
  130. * Once any of the selected pins goes into the state given by mode argument,
  131. * the chip will be woken up.
  132. *
  133. * @note This function does not modify pin configuration. The pins are
  134. * configured in esp_deep_sleep_start, immediately before
  135. * entering deep sleep.
  136. *
  137. * @note internal pullups and pulldowns don't work when RTC peripherals are
  138. * shut down. In this case, external resistors need to be added.
  139. * Alternatively, RTC peripherals (and pullups/pulldowns) may be
  140. * kept enabled using esp_deep_sleep_pd_config function.
  141. *
  142. * @param mask bit mask of GPIO numbers which will cause wakeup. Only GPIOs
  143. * which are have RTC functionality can be used in this bit map:
  144. * 0,2,4,12-15,25-27,32-39.
  145. * @param mode select logic function used to determine wakeup condition:
  146. * - ESP_EXT1_WAKEUP_ALL_LOW: wake up when all selected GPIOs are low
  147. * - ESP_EXT1_WAKEUP_ANY_HIGH: wake up when any of the selected GPIOs is high
  148. * @return
  149. * - ESP_OK on success
  150. * - ESP_ERR_INVALID_ARG if any of the selected GPIOs is not an RTC GPIO,
  151. * or mode is invalid
  152. */
  153. esp_err_t esp_deep_sleep_enable_ext1_wakeup(uint64_t mask, esp_ext1_wakeup_mode_t mode);
  154. /**
  155. * @brief Get the bit mask of GPIOs which caused wakeup (ext1)
  156. *
  157. * If wakeup was caused by another source, this function will return 0.
  158. *
  159. * @return bit mask, if GPIOn caused wakeup, BIT(n) will be set
  160. */
  161. uint64_t esp_deep_sleep_get_ext1_wakeup_status();
  162. /**
  163. * @brief Set power down mode for an RTC power domain in deep sleep
  164. *
  165. * If not set set using this API, all power domains default to ESP_PD_OPTION_AUTO.
  166. *
  167. * @param domain power domain to configure
  168. * @param option power down option (ESP_PD_OPTION_OFF, ESP_PD_OPTION_ON, or ESP_PD_OPTION_AUTO)
  169. * @return
  170. * - ESP_OK on success
  171. * - ESP_ERR_INVALID_ARG if either of the arguments is out of range
  172. */
  173. esp_err_t esp_deep_sleep_pd_config(esp_deep_sleep_pd_domain_t domain,
  174. esp_deep_sleep_pd_option_t option);
  175. /**
  176. * @brief Enter deep sleep with the configured wakeup options
  177. *
  178. * This function does not return.
  179. */
  180. void esp_deep_sleep_start() __attribute__((noreturn));
  181. /**
  182. * @brief Enter deep-sleep mode
  183. *
  184. * The device will automatically wake up after the deep-sleep time
  185. * Upon waking up, the device calls deep sleep wake stub, and then proceeds
  186. * to load application.
  187. *
  188. * Call to this function is equivalent to a call to esp_deep_sleep_enable_timer_wakeup
  189. * followed by a call to esp_deep_sleep_start.
  190. *
  191. * esp_deep_sleep does not shut down WiFi, BT, and higher level protocol
  192. * connections gracefully.
  193. * Make sure relevant WiFi and BT stack functions are called to close any
  194. * connections and deinitialize the peripherals. These include:
  195. * - esp_bluedroid_disable
  196. * - esp_bt_controller_disable
  197. * - esp_wifi_stop
  198. *
  199. * This function does not return.
  200. *
  201. * @param time_in_us deep-sleep time, unit: microsecond
  202. */
  203. void esp_deep_sleep(uint64_t time_in_us) __attribute__((noreturn));
  204. /**
  205. * @brief Enter deep-sleep mode
  206. *
  207. * Function has been renamed to esp_deep_sleep.
  208. * This name is deprecated and will be removed in a future version.
  209. *
  210. * @param time_in_us deep-sleep time, unit: microsecond
  211. */
  212. void system_deep_sleep(uint64_t time_in_us) __attribute__((noreturn, deprecated));
  213. /**
  214. * @brief Get the source which caused deep sleep wakeup
  215. *
  216. * @return wakeup cause, or ESP_DEEP_SLEEP_WAKEUP_UNDEFINED if reset reason is other than deep sleep reset.
  217. */
  218. esp_deep_sleep_wakeup_cause_t esp_deep_sleep_get_wakeup_cause();
  219. /**
  220. * @brief Default stub to run on wake from deep sleep.
  221. *
  222. * Allows for executing code immediately on wake from sleep, before
  223. * the software bootloader or ESP-IDF app has started up.
  224. *
  225. * This function is weak-linked, so you can implement your own version
  226. * to run code immediately when the chip wakes from
  227. * sleep.
  228. *
  229. * See docs/deep-sleep-stub.rst for details.
  230. */
  231. void esp_wake_deep_sleep(void);
  232. /**
  233. * @brief Function type for stub to run on wake from sleep.
  234. *
  235. */
  236. typedef void (*esp_deep_sleep_wake_stub_fn_t)(void);
  237. /**
  238. * @brief Install a new stub at runtime to run on wake from deep sleep
  239. *
  240. * If implementing esp_wake_deep_sleep() then it is not necessary to
  241. * call this function.
  242. *
  243. * However, it is possible to call this function to substitute a
  244. * different deep sleep stub. Any function used as a deep sleep stub
  245. * must be marked RTC_IRAM_ATTR, and must obey the same rules given
  246. * for esp_wake_deep_sleep().
  247. */
  248. void esp_set_deep_sleep_wake_stub(esp_deep_sleep_wake_stub_fn_t new_stub);
  249. /**
  250. * @brief Get current wake from deep sleep stub
  251. * @return Return current wake from deep sleep stub, or NULL if
  252. * no stub is installed.
  253. */
  254. esp_deep_sleep_wake_stub_fn_t esp_get_deep_sleep_wake_stub(void);
  255. /**
  256. * @brief The default esp-idf-provided esp_wake_deep_sleep() stub.
  257. *
  258. * See docs/deep-sleep-stub.rst for details.
  259. */
  260. void esp_default_wake_deep_sleep(void);
  261. #ifdef __cplusplus
  262. }
  263. #endif