Skip to content

Commit 1e9bb5b

Browse files
committed
esp32: Deactivate wakeup trigger after first wakeup
The files updated according to code review discussions. In the sleep_modes.c removed immidiate disable of ULP mode and leave just trigger deactivation. The esp_sleep.h is updated to have the same defines for source as esp_sleep_wakeup_cause_t. Updated documentation in sleep_modes.rst file to include cross references. Some minor changes in test_sleep.c unit test. (TW#18952) Closes #1677
1 parent ce09cfd commit 1e9bb5b

File tree

5 files changed

+32
-61
lines changed

5 files changed

+32
-61
lines changed

components/esp32/include/esp_deep_sleep.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ typedef esp_sleep_pd_option_t esp_deep_sleep_pd_option_t;
3737
typedef esp_sleep_ext1_wakeup_mode_t esp_ext1_wakeup_mode_t;
3838
typedef esp_sleep_wakeup_cause_t esp_deep_sleep_wakeup_cause_t;
3939

40-
inline static esp_err_t esp_deep_sleep_disable_wakeup_source(esp_sleep_source_t source)
41-
{
42-
return esp_sleep_disable_wakeup_source(source);
43-
}
44-
4540
inline static esp_err_t esp_deep_sleep_enable_ulp_wakeup(void)
4641
{
4742
return esp_sleep_enable_ulp_wakeup();

components/esp32/include/esp_sleep.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "esp_err.h"
1919
#include "driver/gpio.h"
2020
#include "driver/touch_pad.h"
21-
#include "soc/rtc.h"
2221

2322
#ifdef __cplusplus
2423
extern "C" {
@@ -61,20 +60,11 @@ typedef enum {
6160
ESP_SLEEP_WAKEUP_TIMER, //!< Wakeup caused by timer
6261
ESP_SLEEP_WAKEUP_TOUCHPAD, //!< Wakeup caused by touchpad
6362
ESP_SLEEP_WAKEUP_ULP, //!< Wakeup caused by ULP program
64-
} esp_sleep_wakeup_cause_t;
65-
66-
/**
67-
* @brief Sleep wakeup sources for esp_sleep_disable_wakeup_source function
68-
*/
69-
typedef enum {
70-
ESP_SLEEP_SOURCE_UNDEFINED, //!< Wakeup source is not defined
71-
ESP_SLEEP_SOURCE_EXT0, //!< Wakeup source for external signal using RTC_IO
72-
ESP_SLEEP_SOURCE_EXT1, //!< Wakeup source for external signal using RTC_CNTL
73-
ESP_SLEEP_SOURCE_TIMER, //!< Wakeup source for timer
74-
ESP_SLEEP_SOURCE_TOUCHPAD, //!< Wakeup source for touchpad
75-
ESP_SLEEP_SOURCE_ULP, //!< Wakeup source for ULP program
7663
} esp_sleep_source_t;
7764

65+
/* Leave this type define for compatibility */
66+
typedef esp_sleep_source_t esp_sleep_wakeup_cause_t;
67+
7868
/**
7969
* @brief Disable wakeup source
8070
*

components/esp32/sleep_modes.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,28 +290,25 @@ esp_err_t esp_sleep_disable_wakeup_source(esp_sleep_source_t source)
290290
// For most of sources it is enough to set trigger mask in local
291291
// configuration structure. The actual RTC wake up options
292292
// will be updated by esp_sleep_start().
293-
if (CHECK_SOURCE(source, ESP_SLEEP_SOURCE_TIMER, RTC_TIMER_TRIG_EN)) {
293+
if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_TIMER, RTC_TIMER_TRIG_EN)) {
294294
s_config.wakeup_triggers &= ~RTC_TIMER_TRIG_EN;
295295
s_config.sleep_duration = 0;
296296
}
297-
else if (CHECK_SOURCE(source, ESP_SLEEP_SOURCE_EXT0, RTC_EXT0_TRIG_EN)) {
297+
else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_EXT0, RTC_EXT0_TRIG_EN)) {
298298
s_config.ext0_rtc_gpio_num = 0;
299299
s_config.ext0_trigger_level = 0;
300300
s_config.wakeup_triggers &= ~RTC_EXT0_TRIG_EN;
301301
}
302-
else if (CHECK_SOURCE(source, ESP_SLEEP_SOURCE_EXT1, RTC_EXT1_TRIG_EN)) {
302+
else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_EXT1, RTC_EXT1_TRIG_EN)) {
303303
s_config.ext1_rtc_gpio_mask = 0;
304304
s_config.ext1_trigger_mode = 0;
305305
s_config.wakeup_triggers &= ~RTC_EXT1_TRIG_EN;
306306
}
307-
else if (CHECK_SOURCE(source, ESP_SLEEP_SOURCE_TOUCHPAD, RTC_TOUCH_TRIG_EN)) {
307+
else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_TOUCHPAD, RTC_TOUCH_TRIG_EN)) {
308308
s_config.wakeup_triggers &= ~RTC_TOUCH_TRIG_EN;
309309
}
310310
#ifdef CONFIG_ULP_COPROC_ENABLED
311-
else if (CHECK_SOURCE(source, ESP_SLEEP_SOURCE_ULP, RTC_ULP_TRIG_EN)) {
312-
// The ulp wake up option is disabled immediately
313-
CLEAR_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_WAKEUP_FORCE_EN);
314-
CLEAR_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
311+
else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_ULP, RTC_ULP_TRIG_EN)) {
315312
s_config.wakeup_triggers &= ~RTC_ULP_TRIG_EN;
316313
}
317314
#endif

components/esp32/test/test_sleep.c

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,13 @@
55
#include "freertos/FreeRTOS.h"
66
#include "freertos/task.h"
77

8+
#include "soc/rtc.h" // for wakeup trigger defines
89
#include "soc/rtc_cntl_reg.h" // for read rtc registers directly (cause)
9-
#include "soc/soc.h"
10+
#include "soc/soc.h" // for direct register read macros
1011

1112
#define ESP_EXT0_WAKEUP_LEVEL_LOW 0
1213
#define ESP_EXT0_WAKEUP_LEVEL_HIGH 1
1314

14-
// These are flags for wakeup cause to get it directly from RTC
15-
#define RTC_EXT0_TRIG_EN BIT(0) //!< EXT0 GPIO wakeup
16-
#define RTC_EXT1_TRIG_EN BIT(1) //!< EXT1 GPIO wakeup
17-
#define RTC_GPIO_TRIG_EN BIT(2) //!< GPIO wakeup (light sleep only)
18-
#define RTC_TIMER_TRIG_EN BIT(3) //!< Timer wakeup
19-
#define RTC_SDIO_TRIG_EN BIT(4) //!< SDIO wakeup (light sleep only)
20-
#define RTC_MAC_TRIG_EN BIT(5) //!< MAC wakeup (light sleep only)
21-
#define RTC_UART0_TRIG_EN BIT(6) //!< UART0 wakeup (light sleep only)
22-
#define RTC_UART1_TRIG_EN BIT(7) //!< UART1 wakeup (light sleep only)
23-
#define RTC_TOUCH_TRIG_EN BIT(8) //!< Touch wakeup
24-
#define RTC_ULP_TRIG_EN BIT(9) //!< ULP wakeup
25-
#define RTC_BT_TRIG_EN BIT(10) //!< BT wakeup (light sleep only)
26-
2715
static struct timeval tv_start, tv_stop;
2816

2917
TEST_CASE("esp_deepsleep works", "[deepsleep][reset=DEEPSLEEP_RESET]")
@@ -129,7 +117,7 @@ TEST_CASE("wake up using ext1 when RTC_PERIPH is on (13 low)", "[deepsleep][igno
129117
esp_deep_sleep_start();
130118
}
131119

132-
static float get_time(void)
120+
static float get_time_ms(void)
133121
{
134122
gettimeofday(&tv_stop, NULL);
135123

@@ -146,7 +134,7 @@ static uint32_t get_cause()
146134
}
147135

148136
// This test case verifies deactivation of trigger for wake up sources
149-
TEST_CASE("disable source behavior", "[deepsleep][ignore]")
137+
TEST_CASE("disable source trigger behavior", "[deepsleep]")
150138
{
151139
float dt = 0;
152140

@@ -166,23 +154,23 @@ TEST_CASE("disable source behavior", "[deepsleep][ignore]")
166154
gettimeofday(&tv_start, NULL);
167155
esp_light_sleep_start();
168156

169-
dt = get_time();
157+
dt = get_time_ms();
170158
printf("Ext0 sleep time = %d \n", (int) dt);
171159

172160
// Check wakeup from Ext0 using time measurement because wakeup cause is
173161
// not available in light sleep mode
174-
TEST_ASSERT_INT32_WITHIN(297, 300, (int) dt);
162+
TEST_ASSERT_INT32_WITHIN(299, 300, (int) dt);
175163

176164
TEST_ASSERT((get_cause() & RTC_EXT0_TRIG_EN) != 0);
177165

178166
// Disable Ext0 source. Timer source should be triggered
179-
ESP_ERROR_CHECK(esp_sleep_disable_wakeup_source(ESP_SLEEP_SOURCE_EXT0));
180-
printf("Disable ext0 source leave timer active.\n");
167+
ESP_ERROR_CHECK(esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_EXT0));
168+
printf("Disable ext0 trigger and leave timer active.\n");
181169

182170
gettimeofday(&tv_start, NULL);
183171
esp_light_sleep_start();
184172

185-
dt = get_time();
173+
dt = get_time_ms();
186174
printf("Timer sleep time = %d \n", (int) dt);
187175

188176
TEST_ASSERT_INT32_WITHIN(500, 2000, (int) dt);
@@ -191,27 +179,28 @@ TEST_CASE("disable source behavior", "[deepsleep][ignore]")
191179
TEST_ASSERT((get_cause() & RTC_TIMER_TRIG_EN) != 0);
192180

193181
// Disable timer source.
194-
ESP_ERROR_CHECK(esp_sleep_disable_wakeup_source(ESP_SLEEP_SOURCE_TIMER));
182+
ESP_ERROR_CHECK(esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER));
195183

196184
// Setup ext0 configuration to wake up immediately
197185
ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
198186
ESP_ERROR_CHECK(gpio_pullup_en(GPIO_NUM_13));
199187
ESP_ERROR_CHECK(gpio_pulldown_dis(GPIO_NUM_13));
200188
ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, ESP_EXT0_WAKEUP_LEVEL_HIGH));
201189

202-
printf("Disable timer and wake up from ext0 source.\n");
190+
printf("Disable timer trigger to wake up from ext0 source.\n");
203191

204192
gettimeofday(&tv_start, NULL);
205193
esp_light_sleep_start();
206194

207-
dt = get_time();
195+
dt = get_time_ms();
208196
printf("Ext0 sleep time = %d \n", (int) dt);
209197

210-
TEST_ASSERT_INT32_WITHIN(198, 200, (int) dt);
198+
TEST_ASSERT_INT32_WITHIN(199, 200, (int) dt);
211199
TEST_ASSERT((get_cause() & RTC_EXT0_TRIG_EN) != 0);
212200

213201
// Check error message when source is already disabled
214-
esp_err_t err_code = esp_sleep_disable_wakeup_source(ESP_SLEEP_SOURCE_TIMER);
202+
esp_err_t err_code = esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
215203
TEST_ASSERT(err_code == ESP_ERR_INVALID_STATE);
204+
printf("Test case completed successfully.");
216205
}
217206

docs/api-reference/system/sleep_modes.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ In light sleep mode, digital peripherals, most of the RAM, and CPUs are clock-ga
1010

1111
In 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: RTC controller, RTC peripherals (including ULP coprocessor), and RTC memories (slow and fast).
1212

13-
Wakeup 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 ``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.
13+
Wakeup 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.
1414

15-
Additionally, the application can force specific powerdown modes for the RTC peripherals and RTC memories using ``esp_sleep_pd_config`` API.
15+
Additionally, the application can force specific powerdown modes for the RTC peripherals and RTC memories using :cpp:func:`esp_sleep_pd_config` API.
1616

17-
Once wakeup sources are configured, application can enter sleep mode using ``esp_light_sleep_start`` or ``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.
17+
Once 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.
1818

1919
WiFi/BT and sleep modes
2020
-----------------------
2121

22-
In deep sleep mode, wireless peripherals are powered down. Before entering sleep mode, applications must disable WiFi and BT using appropriate calls ( ``esp_bluedroid_disable``, ``esp_bt_controller_disable``, ``esp_wifi_stop``).
22+
In deep sleep mode, wireless peripherals are powered down. Before entering sleep mode, 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`).
2323

24-
WiFi can coexist with light sleep mode, allowing the chip to go into light sleep mode when there is no network activity, and waking up the chip from light sleep mode when required. However **APIs described in this section can not be used for that purpose**. ``esp_light_sleep_start`` forces the chip to enter light sleep mode, regardless of whether WiFi is active or not. Automatic entry into light sleep mode, coordinated with WiFi driver, will be supported using a separate set of APIs.
24+
WiFi can coexist with light sleep mode, allowing the chip to go into light sleep mode when there is no network activity, and waking up the chip from light sleep mode when required. However **APIs described in this section can not be used for that purpose**. :cpp:func:`esp_light_sleep_start` forces the chip to enter light sleep mode, regardless of whether WiFi is active or not. Automatic entry into light sleep mode, coordinated with WiFi driver, will be supported using a separate set of APIs.
2525

2626
Wakeup sources
2727
--------------
@@ -52,7 +52,7 @@ External wakeup (ext0)
5252

5353
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.
5454

55-
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 ``rtc_gpio_pullup_en`` and ``rtc_gpio_pulldown_en`` functions, before calling ``esp_sleep_start``.
55+
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`.
5656

5757
In revisions 0 and 1 of the ESP32, this wakeup source is incompatible with ULP and touch wakeup sources.
5858

@@ -97,11 +97,11 @@ The following function can be used to enable this wakeup mode:
9797
Power-down of RTC peripherals and memories
9898
------------------------------------------
9999

100-
By default, ``esp_deep_sleep_start`` and ``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, ``esp_sleep_pd_config`` function is provided.
100+
By 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.
101101

102102
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 overriden, if the application doesn't need clean reset behaviour after deep sleep.
103103

104-
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 overriden using ``esp_sleep_pd_config`` function, if desired.
104+
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 overriden using :cpp:func:`esp_sleep_pd_config` function, if desired.
105105

106106
.. doxygenfunction:: esp_sleep_pd_config
107107
.. doxygenenum:: esp_sleep_pd_domain_t
@@ -149,7 +149,7 @@ The following function can be used to check which wakeup source has triggered wa
149149
Disable sleep wakeup source
150150
---------------------------
151151

152-
Previously configured wakeup source can be disabled later using ``esp_sleep_disable_wakeup_source`` API. This function deactivates trigger for source defined as input parameter if it should not be used to wake up from sleep.
152+
Previously configured wakeup source can be disabled later using :cpp:func:`esp_sleep_disable_wakeup_source` API. This function deactivates trigger for source defined as input parameter if it should not be used to wake up from sleep.
153153

154154
.. doxygenenum:: esp_sleep_source_t
155155
.. doxygenfunction:: esp_sleep_disable_wakeup_source

0 commit comments

Comments
 (0)