diff --git a/examples/system/deep_sleep/README.md b/examples/system/deep_sleep/README.md index 296cf07c926..6d347bc4873 100644 --- a/examples/system/deep_sleep/README.md +++ b/examples/system/deep_sleep/README.md @@ -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. -- **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 @@ -51,8 +51,8 @@ idf.py menuconfig * **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)` * **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` - Trigger level can be selected via `Example configuration > GPIO wakeup configuration > Enable GPIO high-level wakeup` + The pin is set under `Example configuration > GPIO wakeup configuration > Enable wakeup from GPIO`. + 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. diff --git a/examples/system/deep_sleep/main/Kconfig.projbuild b/examples/system/deep_sleep/main/Kconfig.projbuild index 2657efb2b88..5a843a76e16 100644 --- a/examples/system/deep_sleep/main/Kconfig.projbuild +++ b/examples/system/deep_sleep/main/Kconfig.projbuild @@ -275,12 +275,38 @@ menu "Example Configuration" 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. - config EXAMPLE_GPIO_WAKEUP_HIGH_LEVEL - bool "Enable GPIO high-level wakeup" - default y + choice EXAMPLE_GPIO_WAKEUP_TRIGGER + prompt "GPIO wakeup trigger type" + default EXAMPLE_GPIO_WAKEUP_TRIGGER_HIGH help - This option set the gpio wake-up trigger signal, In deep sleep, only high or low level wake-up is - supported. If this option is enabled, it is a high level wake up, otherwise it is a low level wake up. + Selects esp_sleep_gpio_wake_up_mode_t for + 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 diff --git a/examples/system/deep_sleep/main/gpio_wakeup.c b/examples/system/deep_sleep/main/gpio_wakeup.c index 31ddeead431..aec0e08ebda 100644 --- a/examples/system/deep_sleep/main/gpio_wakeup.c +++ b/examples/system/deep_sleep/main/gpio_wakeup.c @@ -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 */ #include #include "driver/gpio.h" +#include "hal/gpio_types.h" #include "esp_sleep.h" +#include "soc/soc_caps.h" #include "sdkconfig.h" #if CONFIG_EXAMPLE_GPIO_WAKEUP -#define DEFAULT_WAKEUP_PIN CONFIG_EXAMPLE_GPIO_WAKEUP_PIN -#ifdef CONFIG_EXAMPLE_GPIO_WAKEUP_HIGH_LEVEL -#define DEFAULT_WAKEUP_LEVEL ESP_GPIO_WAKEUP_GPIO_HIGH -#else -#define DEFAULT_WAKEUP_LEVEL ESP_GPIO_WAKEUP_GPIO_LOW +#define WAKEUP_PIN CONFIG_EXAMPLE_GPIO_WAKEUP_PIN +#define WAKEUP_MODE CONFIG_EXAMPLE_GPIO_WAKEUP_MODE + +/* Index by esp_sleep_gpio_wake_up_mode_t */ +static const char *const s_wakeup_mode_str[] = { + "LOW_LEVEL", + "HIGH_LEVEL", +#if SOC_RTC_GPIO_EDGE_WAKEUP_SUPPORTED + "POSEDGE", + "NEGEDGE", + "ANYEDGE", #endif +}; 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 = { - .pin_bit_mask = BIT64(DEFAULT_WAKEUP_PIN), + .pin_bit_mask = BIT64(WAKEUP_PIN), .mode = GPIO_MODE_INPUT, - .pull_up_en = !DEFAULT_WAKEUP_LEVEL, - .pull_down_en = DEFAULT_WAKEUP_LEVEL + .pull_up_en = pull_up_en, + .pull_down_en = pull_down_en, + .intr_type = GPIO_INTR_DISABLE, }; 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