1Sleep Modes
2===========
3
4{IDF_TARGET_SPI_POWER_DOMAIN:default="VDD_SPI", esp32="VDD_SDIO"}
5
6Overview
7--------
8
9{IDF_TARGET_NAME} is capable of light sleep and deep sleep power saving modes.
10
11In light sleep mode, digital peripherals, most of the RAM, and CPUs are clock-gated, and supply voltage is reduced. Upon exit from light sleep, peripherals and CPUs resume operation, their internal state is preserved.
12
13In deep sleep mode, CPUs, most of the RAM, and all the digital peripherals which are clocked from APB_CLK are powered off. The only parts of the chip which can still be powered on are:
14
15    .. list::
16
17        - RTC controller
18        - RTC peripherals
19        :SOC_ULP_SUPPORTED: - ULP coprocessor
20        - RTC fast memory
21        :SOC_RTC_SLOW_MEM_SUPPORTED: - RTC slow memory
22
23Wakeup from deep and light sleep modes can be done using several sources. These sources can be combined, in this case the chip will wake up when any one of the sources is triggered. Wakeup sources can be enabled using ``esp_sleep_enable_X_wakeup`` APIs and can be disabled using :cpp:func:`esp_sleep_disable_wakeup_source` API. Next section describes these APIs in detail. Wakeup sources can be configured at any moment before entering light or deep sleep mode.
24
25Additionally, the application can force specific powerdown modes for the RTC peripherals and RTC memories using :cpp:func:`esp_sleep_pd_config` API.
26
27Once wakeup sources are configured, application can enter sleep mode using :cpp:func:`esp_light_sleep_start` or :cpp:func:`esp_deep_sleep_start` APIs. At this point the hardware will be configured according to the requested wakeup sources, and RTC controller will either power down or power off the CPUs and digital peripherals.
28
29.. only:: SOC_BT_SUPPORTED
30
31    WiFi/BT and sleep modes
32    -----------------------
33
34    In deep sleep and light sleep modes, wireless peripherals are powered down. Before entering deep sleep or light sleep modes, applications must disable WiFi and BT using appropriate calls (:cpp:func:`esp_bluedroid_disable`, :cpp:func:`esp_bt_controller_disable`, :cpp:func:`esp_wifi_stop`). WiFi and BT connections will not be maintained in deep sleep or light sleep, even if these functions are not called.
35
36.. only:: not SOC_BT_SUPPORTED
37
38    WiFi and sleep modes
39    --------------------
40
41    In deep sleep and light sleep modes, wireless peripherals are powered down. Before entering deep sleep or light sleep modes, applications must disable WiFi using appropriate calls (:cpp:func:`esp_wifi_stop`). WiFi connection will not be maintained in deep sleep or light sleep, even if these functions are not called.
42
43If WiFi connection needs to be maintained, enable WiFi modem sleep, and enable automatic light sleep feature (see :doc:`Power Management APIs <power_management>`). This will allow the system to wake up from sleep automatically when required by WiFi driver, thereby maintaining connection to the AP.
44
45Wakeup sources
46--------------
47
48Timer
49^^^^^
50
51RTC controller has a built in timer which can be used to wake up the chip after a predefined amount of time. Time is specified at microsecond precision, but the actual resolution depends on the clock source selected for RTC SLOW_CLK.
52
53.. only:: SOC_ULP_SUPPORTED
54
55    For details on RTC clock options, see *{IDF_TARGET_NAME} Technical Reference Manual* > *ULP Coprocessor* [`PDF <{IDF_TARGET_TRM_EN_URL}#ulp>`__].
56
57This wakeup mode doesn't require RTC peripherals or RTC memories to be powered on during sleep.
58
59:cpp:func:`esp_sleep_enable_timer_wakeup` function can be used to enable deep sleep wakeup using a timer.
60
61.. only:: SOC_PM_SUPPORT_TOUCH_SENSOR_WAKEUP
62
63    Touch pad
64    ^^^^^^^^^
65
66    RTC IO module contains logic to trigger wakeup when a touch sensor interrupt occurs. You need to configure the touch pad interrupt before the chip starts deep sleep.
67
68    .. only:: esp32
69
70        Revisions 0 and 1 of the ESP32 only support this wakeup mode when RTC peripherals are not forced to be powered on (i.e. ESP_PD_DOMAIN_RTC_PERIPH should be set to ESP_PD_OPTION_AUTO).
71
72    :cpp:func:`esp_sleep_enable_touchpad_wakeup` function can be used to enable this wakeup source.
73
74.. only:: SOC_PM_SUPPORT_EXT_WAKEUP
75
76    External wakeup (ext0)
77    ^^^^^^^^^^^^^^^^^^^^^^
78
79    RTC IO module contains logic to trigger wakeup when one of RTC GPIOs is set to a predefined logic level. RTC IO is part of RTC peripherals power domain, so RTC peripherals will be kept powered on during deep sleep if this wakeup source is requested.
80
81    Because RTC IO module is enabled in this mode, internal pullup or pulldown resistors can also be used. They need to be configured by the application using :cpp:func:`rtc_gpio_pullup_en` and :cpp:func:`rtc_gpio_pulldown_en` functions, before calling :cpp:func:`esp_sleep_start`.
82
83    .. only:: esp32
84
85        In revisions 0 and 1 of the ESP32, this wakeup source is incompatible with ULP and touch wakeup sources.
86
87    :cpp:func:`esp_sleep_enable_ext0_wakeup` function can be used to enable this wakeup source.
88
89    .. warning:: After wake up from sleep, IO pad used for wakeup will be configured as RTC IO. Before using this pad as digital GPIO, reconfigure it using ``rtc_gpio_deinit(gpio_num)`` function.
90
91    External wakeup (ext1)
92    ^^^^^^^^^^^^^^^^^^^^^^
93
94    RTC controller contains logic to trigger wakeup using multiple RTC GPIOs. One of the two logic functions can be used to trigger wakeup:
95
96        - wake up if any of the selected pins is high (``ESP_EXT1_WAKEUP_ANY_HIGH``)
97        - wake up if all the selected pins are low (``ESP_EXT1_WAKEUP_ALL_LOW``)
98
99    This wakeup source is implemented by the RTC controller. As such, RTC peripherals and RTC memories can be powered down in this mode. However, if RTC peripherals are powered down, internal pullup and pulldown resistors will be disabled. To use internal pullup or pulldown resistors, request RTC peripherals power domain to be kept on during sleep, and configure pullup/pulldown resistors using ``rtc_gpio_`` functions, before entering sleep::
100
101        esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
102        gpio_pullup_dis(gpio_num);
103        gpio_pulldown_en(gpio_num);
104
105    .. warning:: After wake up from sleep, IO pad(s) used for wakeup will be configured as RTC IO. Before using these pads as digital GPIOs, reconfigure them using ``rtc_gpio_deinit(gpio_num)`` function.
106
107    :cpp:func:`esp_sleep_enable_ext1_wakeup` function can be used to enable this wakeup source.
108
109.. only:: SOC_ULP_SUPPORTED
110
111    ULP coprocessor wakeup
112    ^^^^^^^^^^^^^^^^^^^^^^
113
114    ULP coprocessor can run while the chip is in sleep mode, and may be used to poll sensors, monitor ADC or touch sensor values, and wake up the chip when a specific event is detected. ULP coprocessor is part of RTC peripherals power domain, and it runs the program stored in RTC slow memory. RTC slow memory will be powered on during sleep if this wakeup mode is requested. RTC peripherals will be automatically powered on before ULP coprocessor starts running the program; once the program stops running, RTC peripherals are automatically powered down again.
115
116    .. only:: esp32
117
118        Revisions 0 and 1 of the ESP32 only support this wakeup mode when RTC peripherals are not forced to be powered on (i.e. ESP_PD_DOMAIN_RTC_PERIPH should be set to ESP_PD_OPTION_AUTO).
119
120    :cpp:func:`esp_sleep_enable_ulp_wakeup` function can be used to enable this wakeup source.
121
122.. only:: SOC_RTCIO_WAKE_SUPPORTED
123
124    GPIO wakeup (light sleep only)
125    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
126
127    .. only:: SOC_PM_SUPPORT_EXT_WAKEUP
128
129        In addition to EXT0 and EXT1 wakeup sources described above, one more method of wakeup from external inputs is available in light sleep mode. With this wakeup source, each pin can be individually configured to trigger wakeup on high or low level using :cpp:func:`gpio_wakeup_enable` function. Unlike EXT0 and EXT1 wakeup sources, which can only be used with RTC IOs, this wakeup source can be used with any IO (RTC or digital).
130
131    .. only:: not SOC_PM_SUPPORT_EXT_WAKEUP
132
133        One more method of wakeup from external inputs is available in light sleep mode. With this wakeup source, each pin can be individually configured to trigger wakeup on high or low level using :cpp:func:`gpio_wakeup_enable` function. This wakeup source can be used with any IO (RTC or digital).
134
135    :cpp:func:`esp_sleep_enable_gpio_wakeup` function can be used to enable this wakeup source.
136
137    .. warning::
138        Before entering light sleep mode, check if any GPIO pin to be driven is part of the {IDF_TARGET_SPI_POWER_DOMAIN} power domain. If so, this power domain must be configured to remain ON during sleep.
139
140        For example, on ESP32-WROOM-32 board, GPIO16 and GPIO17 are linked to {IDF_TARGET_SPI_POWER_DOMAIN} power domain. If they are configured to remain high during
141        light sleep, the power domain should be configured to remain powered ON. This can be done with :cpp:func:`esp_sleep_pd_config()`::
142
143            esp_sleep_pd_config(ESP_PD_DOMAIN_VDDSDIO, ESP_PD_OPTION_ON);
144
145
146UART wakeup (light sleep only)
147^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
148
149When {IDF_TARGET_NAME} receives UART input from external devices, it is often required to wake up the chip when input data is available. UART peripheral contains a feature which allows waking up the chip from light sleep when a certain number of positive edges on RX pin are seen. This number of positive edges can be set using :cpp:func:`uart_set_wakeup_threshold` function. Note that the character which triggers wakeup (and any characters before it) will not be received by the UART after wakeup. This means that the external device typically needs to send an extra character to the {IDF_TARGET_NAME} to trigger wakeup, before sending the data.
150
151:cpp:func:`esp_sleep_enable_uart_wakeup` function can be used to enable this wakeup source.
152
153
154Power-down of RTC peripherals and memories
155------------------------------------------
156
157By default, :cpp:func:`esp_deep_sleep_start` and :cpp:func:`esp_light_sleep_start` functions will power down all RTC power domains which are not needed by the enabled wakeup sources. To override this behaviour, :cpp:func:`esp_sleep_pd_config` function is provided.
158
159.. only:: esp32
160
161    Note: in revision 0 of the ESP32, RTC fast memory will always be kept enabled in deep sleep, so that the deep sleep stub can run after reset. This can be overridden, if the application doesn't need clean reset behaviour after deep sleep.
162
163.. only:: SOC_RTC_SLOW_MEM_SUPPORTED
164
165    If some variables in the program are placed into RTC slow memory (for example, using ``RTC_DATA_ATTR`` attribute), RTC slow memory will be kept powered on by default. This can be overridden using :cpp:func:`esp_sleep_pd_config` function, if desired.
166
167.. only:: not SOC_RTC_SLOW_MEM_SUPPORTED
168
169    In {IDF_TARGET_NAME} there is only RTC fast memory, so if some variables in the program are marked by ``RTC_DATA_ATTR``, ``RTC_SLOW_ATTR`` or ``RTC_FAST_ATTR`` attributes, all of them go to RTC fast memory. It will be kept powered on by default. This can be overridden using :cpp:func:`esp_sleep_pd_config` function, if desired.
170
171Entering light sleep
172--------------------
173
174:cpp:func:`esp_light_sleep_start` function can be used to enter light sleep once wakeup sources are configured. It is also possible to go into light sleep with no wakeup sources configured, in this case the chip will be in light sleep mode indefinitely, until external reset is applied.
175
176Entering deep sleep
177-------------------
178
179:cpp:func:`esp_deep_sleep_start` function can be used to enter deep sleep once wakeup sources are configured. It is also possible to go into deep sleep with no wakeup sources configured, in this case the chip will be in deep sleep mode indefinitely, until external reset is applied.
180
181Configuring IOs
182---------------
183
184Some {IDF_TARGET_NAME} IOs have internal pullups or pulldowns, which are enabled by default. If an external circuit drives this pin in deep sleep mode, current consumption may increase due to current flowing through these pullups and pulldowns.
185
186To isolate a pin, preventing extra current draw, call :cpp:func:`rtc_gpio_isolate` function.
187
188For example, on ESP32-WROVER module, GPIO12 is pulled up externally. GPIO12 also has an internal pulldown in the ESP32 chip. This means that in deep sleep, some current will flow through these external and internal resistors, increasing deep sleep current above the minimal possible value.
189Add the following code before :cpp:func:`esp_deep_sleep_start` to remove this extra current::
190
191	rtc_gpio_isolate(GPIO_NUM_12);
192
193
194UART output handling
195--------------------
196
197Before entering sleep mode, :cpp:func:`esp_deep_sleep_start` will flush the contents of UART FIFOs.
198
199When entering light sleep mode using :cpp:func:`esp_light_sleep_start`, UART FIFOs will not be flushed. Instead, UART output will be suspended, and remaining characters in the FIFO will be sent out after wakeup from light sleep.
200
201Checking sleep wakeup cause
202---------------------------
203
204:cpp:func:`esp_sleep_get_wakeup_cause` function can be used to check which wakeup source has triggered wakeup from sleep mode.
205
206.. only:: SOC_TOUCH_SENSOR_NUM
207
208    For touch pad, it is possible to identify touch pad which has caused wakeup using :cpp:func:`esp_sleep_get_touchpad_wakeup_status` functions.
209
210.. only:: SOC_PM_SUPPORT_EXT_WAKEUP
211
212    For ext1 wakeup sources, it is possible to identify pin which has caused wakeup using :cpp:func:`esp_sleep_get_ext1_wakeup_status` functions.
213
214
215Disable sleep wakeup source
216---------------------------
217
218Previously configured wakeup source can be disabled later using :cpp:func:`esp_sleep_disable_wakeup_source` API. This function deactivates trigger for the given wakeup source. Additionally it can disable all triggers if the argument is ``ESP_SLEEP_WAKEUP_ALL``.
219
220Application Example
221-------------------
222
223Implementation of basic functionality of deep sleep is shown in :example:`protocols/sntp` example, where ESP module is periodically waken up to retrieve time from NTP server.
224
225
226.. only:: SOC_ULP_SUPPORTED
227
228    More extensive example in :example:`system/deep_sleep` illustrates usage of various deep sleep wakeup triggers and ULP coprocessor programming.
229
230API Reference
231-------------
232
233.. include-build-file:: inc/esp_sleep.inc
234
235
236