feat(example): update gpio edge wakeup support in deepsleep example

This commit is contained in:
wuzhenghui
2026-05-20 11:04:04 +08:00
parent 0bd5331db7
commit e491855cc6
3 changed files with 65 additions and 19 deletions
+3 -3
View File
@@ -40,7 +40,7 @@ This example should be able to run on any commonly available ESP32 series develo
- **EXT1:** GPIO2 and GPIO4 should be connected to LOW to avoid floating pins. When triggering a wake up, connect one or both of the pins to HIGH. Note that floating pins may trigger a wake up. - **EXT1:** GPIO2 and GPIO4 should be connected to LOW to avoid floating pins. When triggering a wake up, connect one or both of the pins to HIGH. Note that floating pins may trigger a wake up.
- **GPIO:** If `EXAMPLE_GPIO_WAKEUP_HIGH_LEVEL` is selected in menuconfig, then connect `EXAMPLE_GPIO_WAKEUP_PIN` to HIGH to trigger a wake up; Otherwise, connect `EXAMPLE_GPIO_WAKEUP_PIN` to LOW to trigger a wake up. - **GPIO:** In `Example configuration > GPIO wakeup configuration`, set **GPIO wakeup trigger type** to match how you drive `EXAMPLE_GPIO_WAKEUP_PIN`: for **High level**, hold the pin low before sleep then drive high to wake; for **Low level**, hold high then pull low; for **Rising/Falling/Any edge** (targets with `SOC_RTC_GPIO_EDGE_WAKEUP_SUPPORTED`), use an edge after a stable idle level as required by the selected mode.
### Configure the project ### Configure the project
@@ -51,8 +51,8 @@ idf.py menuconfig
* **EXT0 wake up** can be enabled/disabled via `Example configuration > Enable wakeup from GPIO (ext0)` * **EXT0 wake up** can be enabled/disabled via `Example configuration > Enable wakeup from GPIO (ext0)`
* **EXT1 wake up** can be enabled/disabled via `Example configuration > Enable wakeup from GPIO (ext1)` * **EXT1 wake up** can be enabled/disabled via `Example configuration > Enable wakeup from GPIO (ext1)`
* **GPIO wake up** can be enabled/disabled via `Example configuration > Enable wakeup from GPIO` * **GPIO wake up** can be enabled/disabled via `Example configuration > Enable wakeup from GPIO`
Trigger pin can be chosen via `Example configuration > GPIO wakeup configuration > Enable wakeup from GPIO` The pin is set under `Example configuration > GPIO wakeup configuration > Enable wakeup from GPIO`.
Trigger level can be selected via `Example configuration > GPIO wakeup configuration > Enable GPIO high-level wakeup` The trigger is set under `Example configuration > GPIO wakeup configuration > GPIO wakeup trigger type` (level or edge where supported).
Wake up sources that are unused or unconnected should be disabled in configuration to prevent inadvertent triggering of wake up as a result of floating pins. Wake up sources that are unused or unconnected should be disabled in configuration to prevent inadvertent triggering of wake up as a result of floating pins.
@@ -275,12 +275,38 @@ menu "Example Configuration"
You need to ensure that the set IO can be configured as a GPIO wake-up source, You need to ensure that the set IO can be configured as a GPIO wake-up source,
refer SOC_GPIO_HP_PERIPH_PD_SLEEP_WAKEABLE_MASK in soc_caps.h. refer SOC_GPIO_HP_PERIPH_PD_SLEEP_WAKEABLE_MASK in soc_caps.h.
config EXAMPLE_GPIO_WAKEUP_HIGH_LEVEL choice EXAMPLE_GPIO_WAKEUP_TRIGGER
bool "Enable GPIO high-level wakeup" prompt "GPIO wakeup trigger type"
default y default EXAMPLE_GPIO_WAKEUP_TRIGGER_HIGH
help help
This option set the gpio wake-up trigger signal, In deep sleep, only high or low level wake-up is Selects esp_sleep_gpio_wake_up_mode_t for
supported. If this option is enabled, it is a high level wake up, otherwise it is a low level wake up. esp_sleep_enable_gpio_wakeup_on_hp_periph_powerdown.
Edge choices require SOC_RTC_GPIO_EDGE_WAKEUP_SUPPORTED on the target.
config EXAMPLE_GPIO_WAKEUP_TRIGGER_LOW
bool "Low level"
config EXAMPLE_GPIO_WAKEUP_TRIGGER_HIGH
bool "High level"
config EXAMPLE_GPIO_WAKEUP_TRIGGER_POSEDGE
bool "Rising edge (posedge)"
depends on SOC_RTC_GPIO_EDGE_WAKEUP_SUPPORTED
config EXAMPLE_GPIO_WAKEUP_TRIGGER_NEGEDGE
bool "Falling edge (negedge)"
depends on SOC_RTC_GPIO_EDGE_WAKEUP_SUPPORTED
config EXAMPLE_GPIO_WAKEUP_TRIGGER_ANYEDGE
bool "Any edge"
depends on SOC_RTC_GPIO_EDGE_WAKEUP_SUPPORTED
endchoice
config EXAMPLE_GPIO_WAKEUP_MODE
int
default 0 if EXAMPLE_GPIO_WAKEUP_TRIGGER_LOW
default 1 if EXAMPLE_GPIO_WAKEUP_TRIGGER_HIGH
default 2 if EXAMPLE_GPIO_WAKEUP_TRIGGER_POSEDGE
default 3 if EXAMPLE_GPIO_WAKEUP_TRIGGER_NEGEDGE
default 4 if EXAMPLE_GPIO_WAKEUP_TRIGGER_ANYEDGE
help
esp_sleep_gpio_wake_up_mode_t index (0=LOW, 1=HIGH, 2=POSEDGE, 3=NEGEDGE, 4=ANYEDGE);
endmenu endmenu
endmenu endmenu
+31 -11
View File
@@ -1,34 +1,54 @@
/* /*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2023-2026 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
#include <stdio.h> #include <stdio.h>
#include "driver/gpio.h" #include "driver/gpio.h"
#include "hal/gpio_types.h"
#include "esp_sleep.h" #include "esp_sleep.h"
#include "soc/soc_caps.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#if CONFIG_EXAMPLE_GPIO_WAKEUP #if CONFIG_EXAMPLE_GPIO_WAKEUP
#define DEFAULT_WAKEUP_PIN CONFIG_EXAMPLE_GPIO_WAKEUP_PIN #define WAKEUP_PIN CONFIG_EXAMPLE_GPIO_WAKEUP_PIN
#ifdef CONFIG_EXAMPLE_GPIO_WAKEUP_HIGH_LEVEL #define WAKEUP_MODE CONFIG_EXAMPLE_GPIO_WAKEUP_MODE
#define DEFAULT_WAKEUP_LEVEL ESP_GPIO_WAKEUP_GPIO_HIGH
#else /* Index by esp_sleep_gpio_wake_up_mode_t */
#define DEFAULT_WAKEUP_LEVEL ESP_GPIO_WAKEUP_GPIO_LOW static const char *const s_wakeup_mode_str[] = {
"LOW_LEVEL",
"HIGH_LEVEL",
#if SOC_RTC_GPIO_EDGE_WAKEUP_SUPPORTED
"POSEDGE",
"NEGEDGE",
"ANYEDGE",
#endif #endif
};
void example_deep_sleep_register_gpio_wakeup(void) void example_deep_sleep_register_gpio_wakeup(void)
{ {
const esp_sleep_gpio_wake_up_mode_t wakeup_mode = WAKEUP_MODE;
#if SOC_RTC_GPIO_EDGE_WAKEUP_SUPPORTED
const gpio_pullup_t pull_up_en =
((wakeup_mode == ESP_GPIO_WAKEUP_GPIO_LOW) || (wakeup_mode == ESP_GPIO_WAKEUP_GPIO_NEGEDGE)) ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE;
const gpio_pulldown_t pull_down_en =
((wakeup_mode == ESP_GPIO_WAKEUP_GPIO_HIGH) || (wakeup_mode == ESP_GPIO_WAKEUP_GPIO_POSEDGE)) ? GPIO_PULLDOWN_ENABLE : GPIO_PULLDOWN_DISABLE;
#else
const gpio_pullup_t pull_up_en = (wakeup_mode == ESP_GPIO_WAKEUP_GPIO_LOW) ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE;
const gpio_pulldown_t pull_down_en = (wakeup_mode == ESP_GPIO_WAKEUP_GPIO_HIGH) ? GPIO_PULLDOWN_ENABLE : GPIO_PULLDOWN_DISABLE;
#endif
const gpio_config_t config = { const gpio_config_t config = {
.pin_bit_mask = BIT64(DEFAULT_WAKEUP_PIN), .pin_bit_mask = BIT64(WAKEUP_PIN),
.mode = GPIO_MODE_INPUT, .mode = GPIO_MODE_INPUT,
.pull_up_en = !DEFAULT_WAKEUP_LEVEL, .pull_up_en = pull_up_en,
.pull_down_en = DEFAULT_WAKEUP_LEVEL .pull_down_en = pull_down_en,
.intr_type = GPIO_INTR_DISABLE,
}; };
ESP_ERROR_CHECK(gpio_config(&config)); ESP_ERROR_CHECK(gpio_config(&config));
ESP_ERROR_CHECK(esp_sleep_enable_gpio_wakeup_on_hp_periph_powerdown(BIT(DEFAULT_WAKEUP_PIN), DEFAULT_WAKEUP_LEVEL)); ESP_ERROR_CHECK(esp_sleep_enable_gpio_wakeup_on_hp_periph_powerdown(BIT64(WAKEUP_PIN), wakeup_mode));
printf("Enabling GPIO wakeup on pins GPIO%d\n", DEFAULT_WAKEUP_PIN); printf("Enabling GPIO wakeup on GPIO%d, trigger=%s\n", WAKEUP_PIN, s_wakeup_mode_str[wakeup_mode]);
} }
#endif #endif