esp_deep_sleep.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /**
  22. * @brief Logic function used for EXT1 wakeup mode.
  23. */
  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_ext1_wakeup_mode_t;
  28. /**
  29. * @brief Power domains which can be powered down in deep sleep
  30. */
  31. typedef enum {
  32. ESP_PD_DOMAIN_RTC_PERIPH, //!< RTC IO, sensors and ULP co-processor
  33. ESP_PD_DOMAIN_RTC_SLOW_MEM, //!< RTC slow memory
  34. ESP_PD_DOMAIN_RTC_FAST_MEM, //!< RTC fast memory
  35. ESP_PD_DOMAIN_MAX //!< Number of domains
  36. } esp_deep_sleep_pd_domain_t;
  37. /**
  38. * @brief Power down options
  39. */
  40. typedef enum {
  41. ESP_PD_OPTION_OFF, //!< Power down the power domain in deep sleep
  42. ESP_PD_OPTION_ON, //!< Keep power domain enabled during deep sleep
  43. 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.
  44. } esp_deep_sleep_pd_option_t;
  45. /**
  46. * @brief Enable wakeup by ULP coprocessor
  47. * @return
  48. * - ESP_OK on success
  49. * - ESP_ERR_INVALID_STATE if ULP co-processor is not enabled.
  50. */
  51. esp_err_t esp_deep_sleep_enable_ulp_wakeup();
  52. /**
  53. * @brief Enable wakeup by timer
  54. * @param time_in_us time before wakeup, in microseconds
  55. * @return
  56. * - ESP_OK on success
  57. * - ESP_ERR_INVALID_ARG if value is out of range (TBD)
  58. */
  59. esp_err_t esp_deep_sleep_enable_timer_wakeup(uint64_t time_in_us);
  60. /**
  61. * @brief Enable wakeup using a pin
  62. *
  63. * This function uses external wakeup feature of RTC_IO peripheral.
  64. * It will work only if RTC peripherals are kept on during deep sleep.
  65. *
  66. * This feature can monitor any pin which is an RTC IO. Once the pin transitions
  67. * into the state given by level argument, the chip will be woken up.
  68. *
  69. * @note This function does not modify pin configuration. The pin is
  70. * configured in esp_deep_sleep_start, immediately before
  71. * entering deep sleep.
  72. *
  73. * @param gpio_num GPIO number used as wakeup source. Only GPIOs which are have RTC
  74. * functionality can be used: 0,2,4,12-15,25-27,32-39.
  75. * @param level input level which will trigger wakeup (0=low, 1=high)
  76. * @return
  77. * - ESP_OK on success
  78. * - ESP_ERR_INVALID_ARG if the selected GPIO is not an RTC GPIO,
  79. * or the mode is invalid
  80. */
  81. esp_err_t esp_deep_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level);
  82. /**
  83. * @brief Enable wakeup using multiple pins
  84. *
  85. * This function uses external wakeup feature of RTC controller.
  86. * It will work even if RTC peripherals are shut down during deep sleep.
  87. *
  88. * This feature can monitor any number of pins which are in RTC IOs.
  89. * Once any of the selected pins goes into the state given by mode argument,
  90. * the chip will be woken up.
  91. *
  92. * @note This function does not modify pin configuration. The pins are
  93. * configured in esp_deep_sleep_start, immediately before
  94. * entering deep sleep.
  95. *
  96. * @note internal pullups and pulldowns don't work when RTC peripherals are
  97. * shut down. In this case, external resistors need to be added.
  98. * Alternatively, RTC peripherals (and pullups/pulldowns) may be
  99. * kept enabled using esp_deep_sleep_pd_config function.
  100. *
  101. * @param mask bit mask of GPIO numbers which will cause wakeup. Only GPIOs
  102. * which are have RTC functionality can be used in this bit map:
  103. * 0,2,4,12-15,25-27,32-39.
  104. * @param mode select logic function used to determine wakeup condition:
  105. * - ESP_EXT1_WAKEUP_ALL_LOW: wake up when all selected GPIOs are low
  106. * - ESP_EXT1_WAKEUP_ANY_HIGH: wake up when any of the selected GPIOs is high
  107. * @return
  108. * - ESP_OK on success
  109. * - ESP_ERR_INVALID_ARG if any of the selected GPIOs is not an RTC GPIO,
  110. * or mode is invalid
  111. */
  112. esp_err_t esp_deep_sleep_enable_ext1_wakeup(uint64_t mask, esp_ext1_wakeup_mode_t mode);
  113. /**
  114. * @brief Get the bit mask of GPIOs which caused wakeup (ext1)
  115. *
  116. * If wakeup was caused by another source, this function will return 0.
  117. *
  118. * @return bit mask, if GPIOn caused wakeup, BIT(n) will be set
  119. */
  120. uint64_t esp_deep_sleep_get_ext1_wakeup_status();
  121. /**
  122. * @brief Set power down mode for an RTC power domain in deep sleep
  123. *
  124. * If not set set using this API, all power domains default to ESP_PD_OPTION_AUTO.
  125. *
  126. * @param domain power domain to configure
  127. * @param option power down option (ESP_PD_OPTION_OFF, ESP_PD_OPTION_ON, or ESP_PD_OPTION_AUTO)
  128. * @return
  129. * - ESP_OK on success
  130. * - ESP_ERR_INVALID_ARG if either of the arguments is out of range
  131. */
  132. esp_err_t esp_deep_sleep_pd_config(esp_deep_sleep_pd_domain_t domain,
  133. esp_deep_sleep_pd_option_t option);
  134. /**
  135. * @brief Enter deep sleep with the configured wakeup options
  136. *
  137. * This function does not return.
  138. */
  139. void esp_deep_sleep_start() __attribute__((noreturn));
  140. /**
  141. * @brief Enter deep-sleep mode
  142. *
  143. * The device will automatically wake up after the deep-sleep time
  144. * Upon waking up, the device calls deep sleep wake stub, and then proceeds
  145. * to load application.
  146. *
  147. * Call to this function is equivalent to a call to esp_deep_sleep_enable_timer_wakeup
  148. * followed by a call to esp_deep_sleep_start.
  149. *
  150. * This function does not return.
  151. *
  152. * @param time_in_us deep-sleep time, unit: microsecond
  153. */
  154. void esp_deep_sleep(uint64_t time_in_us) __attribute__((noreturn));
  155. /**
  156. * @brief Enter deep-sleep mode
  157. *
  158. * Function has been renamed to esp_deep_sleep.
  159. * This name is deprecated and will be removed in a future version.
  160. *
  161. * @param time_in_us deep-sleep time, unit: microsecond
  162. */
  163. void system_deep_sleep(uint64_t time_in_us) __attribute__((noreturn, deprecated));
  164. /**
  165. * @brief Default stub to run on wake from deep sleep.
  166. *
  167. * Allows for executing code immediately on wake from sleep, before
  168. * the software bootloader or ESP-IDF app has started up.
  169. *
  170. * This function is weak-linked, so you can implement your own version
  171. * to run code immediately when the chip wakes from
  172. * sleep.
  173. *
  174. * See docs/deep-sleep-stub.rst for details.
  175. */
  176. void esp_wake_deep_sleep(void);
  177. /**
  178. * @brief Function type for stub to run on wake from sleep.
  179. *
  180. */
  181. typedef void (*esp_deep_sleep_wake_stub_fn_t)(void);
  182. /**
  183. * @brief Install a new stub at runtime to run on wake from deep sleep
  184. *
  185. * If implementing esp_wake_deep_sleep() then it is not necessary to
  186. * call this function.
  187. *
  188. * However, it is possible to call this function to substitute a
  189. * different deep sleep stub. Any function used as a deep sleep stub
  190. * must be marked RTC_IRAM_ATTR, and must obey the same rules given
  191. * for esp_wake_deep_sleep().
  192. */
  193. void esp_set_deep_sleep_wake_stub(esp_deep_sleep_wake_stub_fn_t new_stub);
  194. /**
  195. * @brief Get current wake from deep sleep stub
  196. * @return Return current wake from deep sleep stub, or NULL if
  197. * no stub is installed.
  198. */
  199. esp_deep_sleep_wake_stub_fn_t esp_get_deep_sleep_wake_stub(void);
  200. /**
  201. * @brief The default esp-idf-provided esp_wake_deep_sleep() stub.
  202. *
  203. * See docs/deep-sleep-stub.rst for details.
  204. */
  205. void esp_default_wake_deep_sleep(void);
  206. #ifdef __cplusplus
  207. }
  208. #endif