From 4625335d935bece2bf06d085669da4fa8f041692 Mon Sep 17 00:00:00 2001 From: JiangGuangMing Date: Thu, 28 May 2026 11:34:36 +0800 Subject: [PATCH] feat(sleep/usb): support usb as wakeup source from light sleep Co-authored-by: Cursor --- components/esp_hw_support/include/esp_sleep.h | 18 +++++++++++- components/esp_hw_support/sleep_modes.c | 29 +++++++++++++++++++ .../esp32p4/include/soc/Kconfig.soc_caps.in | 4 +++ components/soc/esp32p4/include/soc/soc_caps.h | 1 + .../esp32s31/include/soc/Kconfig.soc_caps.in | 4 +++ .../soc/esp32s31/include/soc/soc_caps.h | 1 + 6 files changed, 56 insertions(+), 1 deletion(-) diff --git a/components/esp_hw_support/include/esp_sleep.h b/components/esp_hw_support/include/esp_sleep.h index 8d0941435d7..723774eeb88 100644 --- a/components/esp_hw_support/include/esp_sleep.h +++ b/components/esp_hw_support/include/esp_sleep.h @@ -137,6 +137,7 @@ typedef enum { ESP_SLEEP_WAKEUP_BT, //!< Wakeup caused by BT (light sleep only) ESP_SLEEP_WAKEUP_VAD, //!< Wakeup caused by VAD ESP_SLEEP_WAKEUP_VBAT_UNDER_VOLT, //!< Wakeup caused by VDD_BAT under voltage. + ESP_SLEEP_WAKEUP_USB, //!< Wakeup caused by USB HS (light sleep only) } esp_sleep_source_t; /** @@ -149,7 +150,6 @@ typedef enum { /* Leave this type define for compatibility */ typedef esp_sleep_source_t esp_sleep_wakeup_cause_t; - enum { ESP_ERR_SLEEP_REJECT = ESP_ERR_INVALID_STATE, ESP_ERR_SLEEP_TOO_SHORT_SLEEP_DURATION = ESP_ERR_INVALID_ARG, @@ -565,6 +565,22 @@ esp_err_t esp_sleep_enable_bt_wakeup(void); */ esp_err_t esp_sleep_disable_bt_wakeup(void); +/** + * @brief Enable wakeup by High-Speed USB-OTG + * @return + * - ESP_OK on success + * - ESP_ERR_NOT_SUPPORTED if wakeup from USB is not supported + */ +esp_err_t esp_sleep_enable_usb_wakeup(void); + +/** + * @brief Disable wakeup by High-Speed USB-OTG + * @return + * - ESP_OK on success + * - ESP_ERR_NOT_SUPPORTED if wakeup from USB is not supported + */ +esp_err_t esp_sleep_disable_usb_wakeup(void); + /** * @brief Enable wakeup by WiFi MAC * @return diff --git a/components/esp_hw_support/sleep_modes.c b/components/esp_hw_support/sleep_modes.c index 4242bcd2ea9..d0ab9b87584 100644 --- a/components/esp_hw_support/sleep_modes.c +++ b/components/esp_hw_support/sleep_modes.c @@ -1830,6 +1830,10 @@ esp_err_t esp_sleep_disable_wakeup_source(esp_sleep_source_t source) #if SOC_VBAT_SUPPORTED } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_VBAT_UNDER_VOLT, RTC_VBAT_UNDER_VOLT_TRIG_EN)) { s_config.wakeup_triggers &= ~RTC_VBAT_UNDER_VOLT_TRIG_EN; +#endif +#if SOC_PM_SUPPORT_USB_WAKEUP + } else if (CHECK_SOURCE(source, ESP_SLEEP_WAKEUP_USB, RTC_USB_TRIG_EN)) { + s_config.wakeup_triggers &= ~RTC_USB_TRIG_EN; #endif } else { ESP_EARLY_LOGE(TAG, "Incorrect wakeup source (%d) to disable.", (int) source); @@ -2456,6 +2460,26 @@ esp_err_t esp_sleep_disable_bt_wakeup(void) #endif } +esp_err_t esp_sleep_enable_usb_wakeup(void) +{ +#if SOC_PM_SUPPORT_USB_WAKEUP + s_config.wakeup_triggers |= RTC_USB_TRIG_EN; + return ESP_OK; +#else + return ESP_ERR_NOT_SUPPORTED; +#endif +} + +esp_err_t esp_sleep_disable_usb_wakeup(void) +{ +#if SOC_PM_SUPPORT_USB_WAKEUP + s_config.wakeup_triggers &= (~RTC_USB_TRIG_EN); + return ESP_OK; +#else + return ESP_ERR_NOT_SUPPORTED; +#endif +} + esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause(void) { if (esp_rom_get_reset_reason(0) != RESET_REASON_CORE_DEEP_SLEEP && !s_light_sleep_wakeup) { @@ -2640,6 +2664,11 @@ uint32_t esp_sleep_get_wakeup_causes(void) if (wakeup_cause_raw & RTC_VBAT_UNDER_VOLT_TRIG_EN) { wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_VBAT_UNDER_VOLT); } +#endif +#if SOC_PM_SUPPORT_USB_WAKEUP + if (wakeup_cause_raw & RTC_USB_TRIG_EN) { + wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_USB); + } #endif if (wakeup_cause == 0) { wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_UNDEFINED); diff --git a/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in b/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in index 0b093bc7395..42c3a7b6505 100644 --- a/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in +++ b/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in @@ -1187,6 +1187,10 @@ config SOC_USB_UTMI_PHY_NUM int default 1 +config SOC_PM_SUPPORT_USB_WAKEUP + int + default 1 + config SOC_PARLIO_TX_UNIT_MAX_DATA_WIDTH int default 16 diff --git a/components/soc/esp32p4/include/soc/soc_caps.h b/components/soc/esp32p4/include/soc/soc_caps.h index e6e699b9b02..04059777e09 100644 --- a/components/soc/esp32p4/include/soc/soc_caps.h +++ b/components/soc/esp32p4/include/soc/soc_caps.h @@ -440,6 +440,7 @@ // USB PHY Caps #define SOC_USB_UTMI_PHY_NUM (1U) +#define SOC_PM_SUPPORT_USB_WAKEUP (1U) /*-------------------------- PARLIO CAPS --------------------------------------*/ #define SOC_PARLIO_TX_UNIT_MAX_DATA_WIDTH 16 /*!< Number of data lines of the TX unit */ diff --git a/components/soc/esp32s31/include/soc/Kconfig.soc_caps.in b/components/soc/esp32s31/include/soc/Kconfig.soc_caps.in index 44d2f9a7492..85831ea5f48 100644 --- a/components/soc/esp32s31/include/soc/Kconfig.soc_caps.in +++ b/components/soc/esp32s31/include/soc/Kconfig.soc_caps.in @@ -387,6 +387,10 @@ config SOC_USB_UTMI_PHY_NUM int default 1 +config SOC_PM_SUPPORT_USB_WAKEUP + int + default 1 + config SOC_PHY_SUPPORTED bool default y diff --git a/components/soc/esp32s31/include/soc/soc_caps.h b/components/soc/esp32s31/include/soc/soc_caps.h index f7f25426e87..595c370ad9f 100644 --- a/components/soc/esp32s31/include/soc/soc_caps.h +++ b/components/soc/esp32s31/include/soc/soc_caps.h @@ -128,6 +128,7 @@ #define SOC_USB_OTG_PERIPH_NUM (1U) #define SOC_USB_FSLS_PHY_NUM (0U) #define SOC_USB_UTMI_PHY_NUM (1U) +#define SOC_PM_SUPPORT_USB_WAKEUP (1U) #define SOC_PHY_SUPPORTED 1 #define SOC_WIFI_SUPPORTED 1