1 /*
2  * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <stdint.h>
10 #include "esp_err.h"
11 
12 #include "hal/touch_sensor_types.h"
13 #include "hal/gpio_types.h"
14 
15 #include "soc/soc_caps.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 /*
22  Definitions for the deepsleep prepare callbacks
23 */
24 typedef void (*esp_deep_sleep_cb_t)(void);
25 
26 /**
27  * @brief Logic function used for EXT1 wakeup mode.
28  */
29 #if SOC_PM_SUPPORT_EXT1_WAKEUP
30 #if CONFIG_IDF_TARGET_ESP32
31 typedef enum {
32     ESP_EXT1_WAKEUP_ALL_LOW = 0,    //!< Wake the chip when all selected GPIOs go low
33     ESP_EXT1_WAKEUP_ANY_HIGH = 1    //!< Wake the chip when any of the selected GPIOs go high
34 } esp_sleep_ext1_wakeup_mode_t;
35 #else
36 typedef enum {
37     ESP_EXT1_WAKEUP_ANY_LOW = 0,    //!< Wake the chip when any of the selected GPIOs go low
38     ESP_EXT1_WAKEUP_ANY_HIGH = 1,    //!< Wake the chip when any of the selected GPIOs go high
39     ESP_EXT1_WAKEUP_ALL_LOW __attribute__((deprecated("wakeup mode \"ALL_LOW\" is no longer supported after ESP32, \
40     please use ESP_EXT1_WAKEUP_ANY_LOW instead"))) = ESP_EXT1_WAKEUP_ANY_LOW
41 } esp_sleep_ext1_wakeup_mode_t;
42 #endif
43 #endif
44 
45 #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
46 typedef enum {
47     ESP_GPIO_WAKEUP_GPIO_LOW = 0,
48     ESP_GPIO_WAKEUP_GPIO_HIGH = 1
49 } esp_deepsleep_gpio_wake_up_mode_t;
50 #endif
51 
52 /**
53  * @brief Power domains which can be powered down in sleep mode
54  */
55 typedef enum {
56 #if SOC_PM_SUPPORT_RTC_PERIPH_PD
57     ESP_PD_DOMAIN_RTC_PERIPH,      //!< RTC IO, sensors and ULP co-processor
58 #endif
59 #if SOC_PM_SUPPORT_RTC_SLOW_MEM_PD
60     ESP_PD_DOMAIN_RTC_SLOW_MEM,    //!< RTC slow memory
61 #endif
62 #if SOC_PM_SUPPORT_RTC_FAST_MEM_PD
63     ESP_PD_DOMAIN_RTC_FAST_MEM,    //!< RTC fast memory
64 #endif
65     ESP_PD_DOMAIN_XTAL,            //!< XTAL oscillator
66 #if SOC_PM_SUPPORT_XTAL32K_PD
67     ESP_PD_DOMAIN_XTAL32K,         //!< External 32 kHz XTAL oscillator
68 #endif
69 #if SOC_PM_SUPPORT_RC32K_PD
70     ESP_PD_DOMAIN_RC32K,           //!< Internal 32 kHz RC oscillator
71 #endif
72 #if SOC_PM_SUPPORT_RC_FAST_PD
73     ESP_PD_DOMAIN_RC_FAST,         //!< Internal Fast oscillator
74 #endif
75 #if SOC_PM_SUPPORT_CPU_PD
76     ESP_PD_DOMAIN_CPU,             //!< CPU core
77 #endif
78 #if SOC_PM_SUPPORT_VDDSDIO_PD
79     ESP_PD_DOMAIN_VDDSDIO,         //!< VDD_SDIO
80 #endif
81 #if SOC_PM_SUPPORT_MODEM_PD
82     ESP_PD_DOMAIN_MODEM,           //!< MODEM, includes WiFi, Bluetooth and IEEE802.15.4
83 #endif
84 #if SOC_PM_SUPPORT_TOP_PD
85     ESP_PD_DOMAIN_TOP,             //!< SoC TOP
86 #endif
87     ESP_PD_DOMAIN_MAX              //!< Number of domains
88 } esp_sleep_pd_domain_t;
89 
90 #define ESP_PD_DOMAIN_RTC8M _Pragma("GCC warning \"'ESP_PD_DOMAIN_RTC8M' enum is deprecated\"") ESP_PD_DOMAIN_RC_FAST
91 
92 /**
93  * @brief Power down options
94  */
95 typedef enum {
96     ESP_PD_OPTION_OFF,      //!< Power down the power domain in sleep mode
97     ESP_PD_OPTION_ON,       //!< Keep power domain enabled during sleep mode
98     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.
99 } esp_sleep_pd_option_t;
100 
101 /**
102  * @brief Sleep wakeup cause
103  */
104 typedef enum {
105     ESP_SLEEP_WAKEUP_UNDEFINED,    //!< In case of deep sleep, reset was not caused by exit from deep sleep
106     ESP_SLEEP_WAKEUP_ALL,          //!< Not a wakeup cause, used to disable all wakeup sources with esp_sleep_disable_wakeup_source
107     ESP_SLEEP_WAKEUP_EXT0,         //!< Wakeup caused by external signal using RTC_IO
108     ESP_SLEEP_WAKEUP_EXT1,         //!< Wakeup caused by external signal using RTC_CNTL
109     ESP_SLEEP_WAKEUP_TIMER,        //!< Wakeup caused by timer
110     ESP_SLEEP_WAKEUP_TOUCHPAD,     //!< Wakeup caused by touchpad
111     ESP_SLEEP_WAKEUP_ULP,          //!< Wakeup caused by ULP program
112     ESP_SLEEP_WAKEUP_GPIO,         //!< Wakeup caused by GPIO (light sleep only on ESP32, S2 and S3)
113     ESP_SLEEP_WAKEUP_UART,         //!< Wakeup caused by UART (light sleep only)
114     ESP_SLEEP_WAKEUP_WIFI,              //!< Wakeup caused by WIFI (light sleep only)
115     ESP_SLEEP_WAKEUP_COCPU,             //!< Wakeup caused by COCPU int
116     ESP_SLEEP_WAKEUP_COCPU_TRAP_TRIG,   //!< Wakeup caused by COCPU crash
117     ESP_SLEEP_WAKEUP_BT,           //!< Wakeup caused by BT (light sleep only)
118 } esp_sleep_source_t;
119 
120 /**
121  * @brief Sleep mode
122  */
123 typedef enum {
124     ESP_SLEEP_MODE_LIGHT_SLEEP,   //!< light sleep mode
125     ESP_SLEEP_MODE_DEEP_SLEEP     //!< deep sleep mode
126 } esp_sleep_mode_t;
127 
128 /* Leave this type define for compatibility */
129 typedef esp_sleep_source_t esp_sleep_wakeup_cause_t;
130 
131 enum {
132     ESP_ERR_SLEEP_REJECT = ESP_ERR_INVALID_STATE,
133     ESP_ERR_SLEEP_TOO_SHORT_SLEEP_DURATION = ESP_ERR_INVALID_ARG,
134 };
135 
136 /**
137  * @brief Disable wakeup source
138  *
139  * This function is used to deactivate wake up trigger for source
140  * defined as parameter of the function.
141  *
142  * @note This function does not modify wake up configuration in RTC.
143  *       It will be performed in esp_deep_sleep_start/esp_light_sleep_start function.
144  *
145  * See docs/sleep-modes.rst for details.
146  *
147  * @param source - number of source to disable of type esp_sleep_source_t
148  * @return
149  *      - ESP_OK on success
150  *      - ESP_ERR_INVALID_STATE if trigger was not active
151  */
152 esp_err_t esp_sleep_disable_wakeup_source(esp_sleep_source_t source);
153 
154 #if SOC_ULP_SUPPORTED
155 /**
156  * @brief Enable wakeup by ULP coprocessor
157  * @note On ESP32, ULP wakeup source cannot be used when RTC_PERIPH power domain is forced,
158  *       to be powered on (ESP_PD_OPTION_ON) or when ext0 wakeup source is used.
159  * @return
160  *      - ESP_OK on success
161  *      - ESP_ERR_NOT_SUPPORTED if additional current by touch (CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT) is enabled.
162  *      - ESP_ERR_INVALID_STATE if ULP co-processor is not enabled or if wakeup triggers conflict
163  */
164 esp_err_t esp_sleep_enable_ulp_wakeup(void);
165 
166 #endif // SOC_ULP_SUPPORTED
167 
168 /**
169  * @brief Enable wakeup by timer
170  * @param time_in_us  time before wakeup, in microseconds
171  * @return
172  *      - ESP_OK on success
173  *      - ESP_ERR_INVALID_ARG if value is out of range (TBD)
174  */
175 esp_err_t esp_sleep_enable_timer_wakeup(uint64_t time_in_us);
176 
177 #if SOC_TOUCH_SENSOR_SUPPORTED
178 /**
179  * @brief Enable wakeup by touch sensor
180  *
181  * @note On ESP32, touch wakeup source can not be used when RTC_PERIPH power domain is forced
182  *       to be powered on (ESP_PD_OPTION_ON) or when ext0 wakeup source is used.
183  *
184  * @note The FSM mode of the touch button should be configured
185  *       as the timer trigger mode.
186  *
187  * @return
188  *      - ESP_OK on success
189  *      - ESP_ERR_NOT_SUPPORTED if additional current by touch (CONFIG_RTC_EXT_CRYST_ADDIT_CURRENT) is enabled.
190  *      - ESP_ERR_INVALID_STATE if wakeup triggers conflict
191  */
192 esp_err_t esp_sleep_enable_touchpad_wakeup(void);
193 
194 /**
195  * @brief Get the touch pad which caused wakeup
196  *
197  * If wakeup was caused by another source, this function will return TOUCH_PAD_MAX;
198  *
199  * @return touch pad which caused wakeup
200  */
201 touch_pad_t esp_sleep_get_touchpad_wakeup_status(void);
202 #endif // SOC_TOUCH_SENSOR_SUPPORTED
203 
204 /**
205  * @brief Returns true if a GPIO number is valid for use as wakeup source.
206  *
207  * @note For SoCs with RTC IO capability, this can be any valid RTC IO input pin.
208  *
209  * @param gpio_num Number of the GPIO to test for wakeup source capability
210  *
211  * @return True if this GPIO number will be accepted as a sleep wakeup source.
212  */
213 bool esp_sleep_is_valid_wakeup_gpio(gpio_num_t gpio_num);
214 
215 #if SOC_PM_SUPPORT_EXT0_WAKEUP
216 /**
217  * @brief Enable wakeup using a pin
218  *
219  * This function uses external wakeup feature of RTC_IO peripheral.
220  * It will work only if RTC peripherals are kept on during sleep.
221  *
222  * This feature can monitor any pin which is an RTC IO. Once the pin transitions
223  * into the state given by level argument, the chip will be woken up.
224  *
225  * @note This function does not modify pin configuration. The pin is
226  *       configured in esp_deep_sleep_start/esp_light_sleep_start,
227  *       immediately before entering sleep mode.
228  *
229  * @note ESP32: ext0 wakeup source can not be used together with touch or ULP wakeup sources.
230  *
231  * @param gpio_num  GPIO number used as wakeup source. Only GPIOs with the RTC
232  *        functionality can be used. For different SoCs, the related GPIOs are:
233  *          - ESP32: 0, 2, 4, 12-15, 25-27, 32-39;
234  *          - ESP32-S2: 0-21;
235  *          - ESP32-S3: 0-21.
236  * @param level  input level which will trigger wakeup (0=low, 1=high)
237  * @return
238  *      - ESP_OK on success
239  *      - ESP_ERR_INVALID_ARG if the selected GPIO is not an RTC GPIO,
240  *        or the mode is invalid
241  *      - ESP_ERR_INVALID_STATE if wakeup triggers conflict
242  */
243 esp_err_t esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level);
244 #endif // SOC_PM_SUPPORT_EXT0_WAKEUP
245 
246 #if SOC_PM_SUPPORT_EXT1_WAKEUP
247 /**
248  * @brief Enable wakeup using multiple pins
249  *
250  * This function uses external wakeup feature of RTC controller.
251  * It will work even if RTC peripherals are shut down during sleep.
252  *
253  * This feature can monitor any number of pins which are in RTC IOs.
254  * Once selected pins go into the state given by level_mode argument,
255  * the chip will be woken up.
256  *
257  * @note This function does not modify pin configuration. The pins are
258  *       configured in esp_deep_sleep_start/esp_light_sleep_start,
259  *       immediately before entering sleep mode.
260  *
261  * @note Internal pullups and pulldowns don't work when RTC peripherals are
262  *       shut down. In this case, external resistors need to be added.
263  *       Alternatively, RTC peripherals (and pullups/pulldowns) may be
264  *       kept enabled using esp_sleep_pd_config function. If we turn off the
265  *       ``RTC_PERIPH`` domain or certain chips lack the ``RTC_PERIPH`` domain,
266  *       we will use the HOLD feature to maintain the pull-up and pull-down on
267  *       the pins during sleep. HOLD feature will be acted on the pin internally
268  *       before the system entering sleep, and this can further reduce power consumption.
269  *
270  * @note Call this func will reset the previous ext1 configuration.
271  *
272  * @note This function will be deprecated in release/v6.0. Please switch to use `esp_sleep_enable_ext1_wakeup_io` and `esp_sleep_disable_ext1_wakeup_io`
273  *
274  * @param io_mask  Bit mask of GPIO numbers which will cause wakeup. Only GPIOs
275  *                 which have RTC functionality can be used in this bit map.
276  *                 For different SoCs, the related GPIOs are:
277  *                    - ESP32: 0, 2, 4, 12-15, 25-27, 32-39
278  *                    - ESP32-S2: 0-21
279  *                    - ESP32-S3: 0-21
280  *                    - ESP32-C6: 0-7
281  *                    - ESP32-H2: 7-14
282  * @param level_mode Select logic function used to determine wakeup condition:
283  *                   When target chip is ESP32:
284  *                      - ESP_EXT1_WAKEUP_ALL_LOW: wake up when all selected GPIOs are low
285  *                      - ESP_EXT1_WAKEUP_ANY_HIGH: wake up when any of the selected GPIOs is high
286  *                   When target chip is ESP32-S2, ESP32-S3, ESP32-C6 or ESP32-H2:
287  *                      - ESP_EXT1_WAKEUP_ANY_LOW: wake up when any of the selected GPIOs is low
288  *                      - ESP_EXT1_WAKEUP_ANY_HIGH: wake up when any of the selected GPIOs is high
289  * @return
290  *      - ESP_OK on success
291  *      - ESP_ERR_INVALID_ARG if io_mask is zero,
292  *        or mode is invalid
293  */
294 esp_err_t esp_sleep_enable_ext1_wakeup(uint64_t io_mask, esp_sleep_ext1_wakeup_mode_t level_mode);
295 
296 /**
297  * @brief Enable ext1 wakeup pins with IO masks.
298  *
299  * This will append selected IOs to the wakeup IOs, it will not reset previously enabled IOs.
300  * To reset specific previously enabled IOs, call esp_sleep_disable_ext1_wakeup_io with the io_mask.
301  * To reset all the enabled IOs, call esp_sleep_disable_ext1_wakeup_io(0).
302  *
303  * This function uses external wakeup feature of RTC controller.
304  * It will work even if RTC peripherals are shut down during sleep.
305  *
306  * This feature can monitor any number of pins which are in RTC IOs.
307  * Once selected pins go into the state given by level_mode argument,
308  * the chip will be woken up.
309  *
310  * @note This function does not modify pin configuration. The pins are
311  *       configured in esp_deep_sleep_start/esp_light_sleep_start,
312  *       immediately before entering sleep mode.
313  *
314  * @note Internal pullups and pulldowns don't work when RTC peripherals are
315  *       shut down. In this case, external resistors need to be added.
316  *       Alternatively, RTC peripherals (and pullups/pulldowns) may be
317  *       kept enabled using esp_sleep_pd_config function. If we turn off the
318  *       ``RTC_PERIPH`` domain or certain chips lack the ``RTC_PERIPH`` domain,
319  *       we will use the HOLD feature to maintain the pull-up and pull-down on
320  *       the pins during sleep. HOLD feature will be acted on the pin internally
321  *       before the system entering sleep, and this can further reduce power consumption.
322  *
323  * @param io_mask  Bit mask of GPIO numbers which will cause wakeup. Only GPIOs
324  *                 which have RTC functionality can be used in this bit map.
325  *                 For different SoCs, the related GPIOs are:
326  *                    - ESP32: 0, 2, 4, 12-15, 25-27, 32-39
327  *                    - ESP32-S2: 0-21
328  *                    - ESP32-S3: 0-21
329  *                    - ESP32-C6: 0-7
330  *                    - ESP32-H2: 7-14
331  * @param level_mode Select logic function used to determine wakeup condition:
332  *                   When target chip is ESP32:
333  *                      - ESP_EXT1_WAKEUP_ALL_LOW: wake up when all selected GPIOs are low
334  *                      - ESP_EXT1_WAKEUP_ANY_HIGH: wake up when any of the selected GPIOs is high
335  *                   When target chip is ESP32-S2, ESP32-S3, ESP32-C6 or ESP32-H2:
336  *                      - ESP_EXT1_WAKEUP_ANY_LOW: wake up when any of the selected GPIOs is low
337  *                      - ESP_EXT1_WAKEUP_ANY_HIGH: wake up when any of the selected GPIOs is high
338  * @return
339  *      - ESP_OK on success
340  *      - ESP_ERR_INVALID_ARG if any of the selected GPIOs is not an RTC GPIO,
341  *        or mode is invalid
342  *      - ESP_ERR_NOT_SUPPORTED when wakeup level will become different between
343  *        ext1 IOs if !SOC_PM_SUPPORT_EXT1_WAKEUP_MODE_PER_PIN
344  */
345 esp_err_t esp_sleep_enable_ext1_wakeup_io(uint64_t io_mask, esp_sleep_ext1_wakeup_mode_t level_mode);
346 
347 /**
348  * @brief Disable ext1 wakeup pins with IO masks. This will remove selected IOs from the wakeup IOs.
349  * @param io_mask  Bit mask of GPIO numbers which will cause wakeup. Only GPIOs
350  *                 which have RTC functionality can be used in this bit map.
351  *                 If value is zero, this func will remove all previous ext1 configuration.
352  *                 For different SoCs, the related GPIOs are:
353  *                    - ESP32: 0, 2, 4, 12-15, 25-27, 32-39
354  *                    - ESP32-S2: 0-21
355  *                    - ESP32-S3: 0-21
356  *                    - ESP32-C6: 0-7
357  *                    - ESP32-H2: 7-14
358  *
359  * @return
360  *      - ESP_OK on success
361  *      - ESP_ERR_INVALID_ARG if any of the selected GPIOs is not an RTC GPIO.
362  */
363 esp_err_t esp_sleep_disable_ext1_wakeup_io(uint64_t io_mask);
364 
365 #if SOC_PM_SUPPORT_EXT1_WAKEUP_MODE_PER_PIN
366 /**
367  * @brief Enable wakeup using multiple pins, allows different trigger mode per pin
368  *
369  * This function uses external wakeup feature of RTC controller.
370  * It will work even if RTC peripherals are shut down during sleep.
371  *
372  * This feature can monitor any number of pins which are in RTC IOs.
373  * Once selected pins go into the state given by level_mode argument,
374  * the chip will be woken up.
375  *
376  * @note This function does not modify pin configuration. The pins are
377  *       configured in esp_deep_sleep_start/esp_light_sleep_start,
378  *       immediately before entering sleep mode.
379  *
380  * @note Internal pullups and pulldowns don't work when RTC peripherals are
381  *       shut down. In this case, external resistors need to be added.
382  *       Alternatively, RTC peripherals (and pullups/pulldowns) may be
383  *       kept enabled using esp_sleep_pd_config function. If we turn off the
384  *       ``RTC_PERIPH`` domain or certain chips lack the ``RTC_PERIPH`` domain,
385  *       we will use the HOLD feature to maintain the pull-up and pull-down on
386  *       the pins during sleep. HOLD feature will be acted on the pin internally
387  *       before the system entering sleep, and this can further reduce power consumption.
388  *
389  * @param io_mask  Bit mask of GPIO numbers which will cause wakeup. Only GPIOs
390  *                 which have RTC functionality can be used in this bit map.
391  *                 For different SoCs, the related GPIOs are:
392  *                    - ESP32-C6: 0-7.
393  *                    - ESP32-H2: 7-14.
394  * @param level_mask Select logic function used to determine wakeup condition per pin.
395  *                   Each bit of the level_mask corresponds to the respective GPIO. Each bit's corresponding
396  *                   position is set to 0, the wakeup level will be low, on the contrary,
397  *                   each bit's corresponding position is set to 1, the wakeup level will be high.
398  * @return
399  *      - ESP_OK on success
400  *      - ESP_ERR_INVALID_ARG if any of the selected GPIOs is not an RTC GPIO,
401  *        or mode is invalid
402  */
403 esp_err_t esp_sleep_enable_ext1_wakeup_with_level_mask(uint64_t io_mask, uint64_t level_mask);
404 
405 #endif // SOC_PM_SUPPORT_EXT1_WAKEUP_MODE_PER_PIN
406 #endif // SOC_PM_SUPPORT_EXT1_WAKEUP
407 
408 #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
409 /**
410  * @brief Enable wakeup using specific gpio pins
411  *
412  * This function enables an IO pin to wake up the chip from deep sleep.
413  *
414  * @note This function does not modify pin configuration. The pins are
415  *       configured inside esp_deep_sleep_start, immediately before entering sleep mode.
416  *
417  * @note You don't need to worry about pull-up or pull-down resistors before
418  *       using this function because the ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS
419  *       option is enabled by default. It will automatically set pull-up or pull-down
420  *       resistors internally in esp_deep_sleep_start based on the wakeup mode. However,
421  *       when using external pull-up or pull-down resistors, please be sure to disable
422  *       the ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS option, as the combination of internal
423  *       and external resistors may cause interference. BTW, when you use low level to wake up the
424  *       chip, we strongly recommend you to add external resistors (pull-up).
425  *
426  * @param gpio_pin_mask  Bit mask of GPIO numbers which will cause wakeup. Only GPIOs
427  *              which have RTC functionality (pads that powered by VDD3P3_RTC) can be used in this bit map.
428  * @param mode Select logic function used to determine wakeup condition:
429  *            - ESP_GPIO_WAKEUP_GPIO_LOW: wake up when the gpio turn to low.
430  *            - ESP_GPIO_WAKEUP_GPIO_HIGH: wake up when the gpio turn to high.
431  * @return
432  *      - ESP_OK on success
433  *      - ESP_ERR_INVALID_ARG if the mask contains any invalid deep sleep wakeup pin or wakeup mode is invalid
434  */
435 esp_err_t esp_deep_sleep_enable_gpio_wakeup(uint64_t gpio_pin_mask, esp_deepsleep_gpio_wake_up_mode_t mode);
436 #endif
437 
438 /**
439  * @brief Enable wakeup from light sleep using GPIOs
440  *
441  * Each GPIO supports wakeup function, which can be triggered on either low level
442  * or high level. Unlike EXT0 and EXT1 wakeup sources, this method can be used
443  * both for all IOs: RTC IOs and digital IOs. It can only be used to wakeup from
444  * light sleep though.
445  *
446  * To enable wakeup, first call gpio_wakeup_enable, specifying gpio number and
447  * wakeup level, for each GPIO which is used for wakeup.
448  * Then call this function to enable wakeup feature.
449  *
450  * @note On ESP32, GPIO wakeup source can not be used together with touch or ULP wakeup sources.
451  *
452  * @return
453  *      - ESP_OK on success
454  *      - ESP_ERR_INVALID_STATE if wakeup triggers conflict
455  */
456 esp_err_t esp_sleep_enable_gpio_wakeup(void);
457 
458 /**
459  * @brief Enable wakeup from light sleep using UART
460  *
461  * Use uart_set_wakeup_threshold function to configure UART wakeup threshold.
462  *
463  * Wakeup from light sleep takes some time, so not every character sent
464  * to the UART can be received by the application.
465  *
466  * @note ESP32 does not support wakeup from UART2.
467  *
468  * @param uart_num  UART port to wake up from
469  * @return
470  *      - ESP_OK on success
471  *      - ESP_ERR_INVALID_ARG if wakeup from given UART is not supported
472  */
473 esp_err_t esp_sleep_enable_uart_wakeup(int uart_num);
474 
475 /**
476  * @brief Enable wakeup by bluetooth
477  * @return
478  *      - ESP_OK on success
479  *      - ESP_ERR_NOT_SUPPORTED if wakeup from bluetooth is not supported
480  */
481 esp_err_t esp_sleep_enable_bt_wakeup(void);
482 
483 /**
484  * @brief Disable wakeup by bluetooth
485  * @return
486  *      - ESP_OK on success
487  *      - ESP_ERR_NOT_SUPPORTED if wakeup from bluetooth is not supported
488  */
489 esp_err_t esp_sleep_disable_bt_wakeup(void);
490 
491 /**
492  * @brief Enable wakeup by WiFi MAC
493  * @return
494  *      - ESP_OK on success
495  */
496 esp_err_t esp_sleep_enable_wifi_wakeup(void);
497 
498 /**
499  * @brief Disable wakeup by WiFi MAC
500  * @return
501  *      - ESP_OK on success
502  */
503 esp_err_t esp_sleep_disable_wifi_wakeup(void);
504 
505 /**
506  * @brief Enable beacon wakeup by WiFi MAC, it will wake up the system into modem state
507  * @return
508  *      - ESP_OK on success
509  */
510 esp_err_t esp_sleep_enable_wifi_beacon_wakeup(void);
511 
512 /**
513  * @brief Disable beacon wakeup by WiFi MAC
514  * @return
515  *      - ESP_OK on success
516  */
517 esp_err_t esp_sleep_disable_wifi_beacon_wakeup(void);
518 
519 /**
520  * @brief Get the bit mask of GPIOs which caused wakeup (ext1)
521  *
522  * If wakeup was caused by another source, this function will return 0.
523  *
524  * @return bit mask, if GPIOn caused wakeup, BIT(n) will be set
525  */
526 uint64_t esp_sleep_get_ext1_wakeup_status(void);
527 
528 #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
529 /**
530  * @brief Get the bit mask of GPIOs which caused wakeup (gpio)
531  *
532  * If wakeup was caused by another source, this function will return 0.
533  *
534  * @return bit mask, if GPIOn caused wakeup, BIT(n) will be set
535  */
536 uint64_t esp_sleep_get_gpio_wakeup_status(void);
537 #endif //SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
538 
539 /**
540  * @brief Set power down mode for an RTC power domain in sleep mode
541  *
542  * If not set set using this API, all power domains default to ESP_PD_OPTION_AUTO.
543  *
544  * @param domain  power domain to configure
545  * @param option  power down option (ESP_PD_OPTION_OFF, ESP_PD_OPTION_ON, or ESP_PD_OPTION_AUTO)
546  * @return
547  *      - ESP_OK on success
548  *      - ESP_ERR_INVALID_ARG if either of the arguments is out of range
549  */
550 esp_err_t esp_sleep_pd_config(esp_sleep_pd_domain_t domain,
551                               esp_sleep_pd_option_t option);
552 
553 /**
554  * @brief Enter deep sleep with the configured wakeup options
555  *
556  * @note In general, the function does not return, but if the sleep is rejected,
557  * then it returns from it.
558  *
559  * The reason for the rejection can be such as a short sleep time.
560  *
561  * @return
562  *  - No return - If the sleep is not rejected.
563  *  - ESP_ERR_SLEEP_REJECT sleep request is rejected(wakeup source set before the sleep request)
564  */
565 esp_err_t esp_deep_sleep_try_to_start(void);
566 
567 /**
568  * @brief Enter deep sleep with the configured wakeup options
569  *
570  * @note The function does not do a return (no rejection). Even if wakeup source set before the sleep request
571  * it goes to deep sleep anyway.
572  */
573 void esp_deep_sleep_start(void) __attribute__((__noreturn__));
574 
575 /**
576  * @brief Enter light sleep with the configured wakeup options
577  *
578  * @return
579  *  - ESP_OK on success (returned after wakeup)
580  *  - ESP_ERR_SLEEP_REJECT sleep request is rejected(wakeup source set before the sleep request)
581  *  - ESP_ERR_SLEEP_TOO_SHORT_SLEEP_DURATION after deducting the sleep flow overhead, the final sleep duration
582  *                                           is too short to cover the minimum sleep duration of the chip, when
583  *                                           rtc timer wakeup source enabled
584  */
585 esp_err_t esp_light_sleep_start(void);
586 
587 /**
588  * @brief Enter deep-sleep mode
589  *
590  * The device will automatically wake up after the deep-sleep time
591  * Upon waking up, the device calls deep sleep wake stub, and then proceeds
592  * to load application.
593  *
594  * Call to this function is equivalent to a call to esp_deep_sleep_enable_timer_wakeup
595  * followed by a call to esp_deep_sleep_start.
596  *
597  * @param time_in_us  deep-sleep time, unit: microsecond
598  *
599  * @return
600  *  - No return - If the sleep is not rejected.
601  *  - ESP_ERR_SLEEP_REJECT sleep request is rejected(wakeup source set before the sleep request)
602  */
603 esp_err_t esp_deep_sleep_try(uint64_t time_in_us);
604 
605 /**
606  * @brief Enter deep-sleep mode
607  *
608  * The device will automatically wake up after the deep-sleep time
609  * Upon waking up, the device calls deep sleep wake stub, and then proceeds
610  * to load application.
611  *
612  * Call to this function is equivalent to a call to esp_deep_sleep_enable_timer_wakeup
613  * followed by a call to esp_deep_sleep_start.
614  *
615  * @note The function does not do a return (no rejection).. Even if wakeup source set before the sleep request
616  * it goes to deep sleep anyway.
617  *
618  * @param time_in_us  deep-sleep time, unit: microsecond
619  */
620 void esp_deep_sleep(uint64_t time_in_us) __attribute__((__noreturn__));
621 
622 /**
623   * @brief Register a callback to be called from the deep sleep prepare
624   *
625   * @warning deepsleep callbacks should without parameters, and MUST NOT,
626   *          UNDER ANY CIRCUMSTANCES, CALL A FUNCTION THAT MIGHT BLOCK.
627   *
628   * @param new_dslp_cb     Callback to be called
629   *
630   * @return
631   *     - ESP_OK:         Callback registered to the deepsleep misc_modules_sleep_prepare
632   *     - ESP_ERR_NO_MEM: No more hook space for register the callback
633   */
634 esp_err_t esp_deep_sleep_register_hook(esp_deep_sleep_cb_t new_dslp_cb);
635 
636 /**
637   * @brief Unregister an deepsleep callback
638   *
639   * @param old_dslp_cb     Callback to be unregistered
640   */
641 void esp_deep_sleep_deregister_hook(esp_deep_sleep_cb_t old_dslp_cb);
642 
643 /**
644  * @brief Get the wakeup source which caused wakeup from sleep
645  *
646  * @return cause of wake up from last sleep (deep sleep or light sleep)
647  */
648 esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause(void);
649 
650 
651 /**
652  * @brief Default stub to run on wake from deep sleep.
653  *
654  * Allows for executing code immediately on wake from sleep, before
655  * the software bootloader or ESP-IDF app has started up.
656  *
657  * This function is weak-linked, so you can implement your own version
658  * to run code immediately when the chip wakes from
659  * sleep.
660  *
661  * See docs/deep-sleep-stub.rst for details.
662  */
663 void esp_wake_deep_sleep(void);
664 
665 /**
666  * @brief Function type for stub to run on wake from sleep.
667  *
668  */
669 typedef void (*esp_deep_sleep_wake_stub_fn_t)(void);
670 
671 /**
672  * @brief Install a new stub at runtime to run on wake from deep sleep
673  *
674  * If implementing esp_wake_deep_sleep() then it is not necessary to
675  * call this function.
676  *
677  * However, it is possible to call this function to substitute a
678  * different deep sleep stub. Any function used as a deep sleep stub
679  * must be marked RTC_IRAM_ATTR, and must obey the same rules given
680  * for esp_wake_deep_sleep().
681  */
682 void esp_set_deep_sleep_wake_stub(esp_deep_sleep_wake_stub_fn_t new_stub);
683 
684 /**
685  * @brief Set wake stub entry to default `esp_wake_stub_entry`
686  */
687 void esp_set_deep_sleep_wake_stub_default_entry(void);
688 
689 /**
690  * @brief Get current wake from deep sleep stub
691  * @return Return current wake from deep sleep stub, or NULL if
692  *         no stub is installed.
693  */
694 esp_deep_sleep_wake_stub_fn_t esp_get_deep_sleep_wake_stub(void);
695 
696 /**
697  *  @brief The default esp-idf-provided esp_wake_deep_sleep() stub.
698  *
699  *  See docs/deep-sleep-stub.rst for details.
700  */
701 void esp_default_wake_deep_sleep(void);
702 
703 /**
704  * @brief Disable logging from the ROM code after deep sleep.
705  *
706  * Using LSB of RTC_STORE4.
707  */
708 void esp_deep_sleep_disable_rom_logging(void);
709 
710 #ifdef SOC_PM_SUPPORT_CPU_PD
711 
712 #if SOC_PM_CPU_RETENTION_BY_RTCCNTL
713 /**
714  * @brief CPU Power down low-level initialize, enable CPU power down during light sleep
715  * @return
716  *      - ESP_OK on success
717  *      - ESP_ERR_NO_MEM not enough retention memory
718  */
719 esp_err_t esp_sleep_cpu_pd_low_init(void);
720 
721 /**
722  * @brief CPU Power down low-level deinitialize, disable CPU power down during light sleep
723  * @return
724  *      - ESP_OK on success
725  *      - ESP_ERR_NO_MEM not enough retention memory
726  */
727 esp_err_t esp_sleep_cpu_pd_low_deinit(void);
728 
729 #endif
730 
731 /**
732  * @brief CPU Power down initialize
733  *
734  * @return
735  *      - ESP_OK on success
736  *      - ESP_ERR_NO_MEM not enough retention memory
737  */
738 esp_err_t esp_sleep_cpu_retention_init(void);
739 
740 /**
741  * @brief CPU Power down de-initialize
742  *
743  * @return
744  *      - ESP_OK on success
745  *
746  * Release system retention memory.
747  */
748 esp_err_t esp_sleep_cpu_retention_deinit(void);
749 #endif
750 
751 /**
752  * @brief Configure to isolate all GPIO pins in sleep state
753  */
754 void esp_sleep_config_gpio_isolate(void);
755 
756 /**
757  * @brief Enable or disable GPIO pins status switching between slept status and waked status.
758  * @param enable decide whether to switch status or not
759  */
760 void esp_sleep_enable_gpio_switch(bool enable);
761 
762 #if CONFIG_MAC_BB_PD
763 /**
764  * @brief Function type for stub to run mac bb power down.
765  */
766 typedef void (* mac_bb_power_down_cb_t)(void);
767 
768 /**
769  * @brief Function type for stub to run mac bb power up.
770  */
771 typedef void (* mac_bb_power_up_cb_t)(void);
772 
773 /**
774  * @brief  Registet mac bb power down callback.
775  * @param  cb mac bb power down callback.
776  * @return
777  *  - ESP_OK on success
778  */
779 esp_err_t esp_register_mac_bb_pd_callback(mac_bb_power_down_cb_t cb);
780 
781 /**
782  * @brief  Unregistet mac bb power down callback.
783  * @param  cb mac bb power down callback.
784  * @return
785  *  - ESP_OK on success
786  */
787 esp_err_t esp_unregister_mac_bb_pd_callback(mac_bb_power_down_cb_t cb);
788 
789 /**
790  * @brief  Registet mac bb power up callback.
791  * @param  cb mac bb power up callback.
792  * @return
793  *  - ESP_OK on success
794  */
795 esp_err_t esp_register_mac_bb_pu_callback(mac_bb_power_up_cb_t cb);
796 
797 /**
798  * @brief  Unregistet mac bb power up callback.
799  * @param  cb mac bb power up callback.
800  * @return
801  *  - ESP_OK on success
802  */
803 esp_err_t esp_unregister_mac_bb_pu_callback(mac_bb_power_up_cb_t cb);
804 #endif
805 
806 #ifdef __cplusplus
807 }
808 #endif
809