From bf7e9f091405e641138572425412731119b0b253 Mon Sep 17 00:00:00 2001 From: cjin Date: Fri, 19 Sep 2025 16:47:18 +0800 Subject: [PATCH 1/4] feat(ble): get wakeup cause when wake up on ESP32-C2 --- components/bt/controller/esp32c2/bt.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/components/bt/controller/esp32c2/bt.c b/components/bt/controller/esp32c2/bt.c index e5eb940d34..7b335387ab 100644 --- a/components/bt/controller/esp32c2/bt.c +++ b/components/bt/controller/esp32c2/bt.c @@ -130,6 +130,15 @@ enum { }; #endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +typedef union { + struct { + uint32_t rtc_freq:20; + uint32_t rsv:11; + uint32_t bt_wakeup:1; + }; + uint32_t val; +} bt_wakeup_params_t; + /* External functions or variables ************************************************************************ */ @@ -220,6 +229,9 @@ static void esp_bt_ctrl_log_partition_get_and_erase_first_block(void); #endif // CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE #endif /* !CONFIG_BT_LE_CONTROLLER_LOG_MODE_BLE_LOG_V2 */ #endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE +static bool esp_bt_check_wakeup_by_bt(void); +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE /* Local variable definition *************************************************************************** */ @@ -712,6 +724,7 @@ void controller_sleep_cb(uint32_t enable_tick, void *arg) void controller_wakeup_cb(void *arg) { + bt_wakeup_params_t *params; if (s_ble_active) { return; } @@ -721,15 +734,25 @@ void controller_wakeup_cb(void *arg) esp_pm_get_configuration(&pm_config); assert(esp_rom_get_cpu_ticks_per_us() == pm_config.max_freq_mhz); #endif //CONFIG_PM_ENABLE + params = (bt_wakeup_params_t *)arg; esp_phy_enable(PHY_MODEM_BT); if (s_bt_lpclk_src == MODEM_CLOCK_LPCLK_SRC_RC_SLOW) { - uint32_t *clk_freq = (uint32_t *)arg; - *clk_freq = esp_clk_tree_lp_slow_get_freq_hz(ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED) / 5; + params->rtc_freq = esp_clk_tree_lp_slow_get_freq_hz(ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED) / 5; } +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE + params->bt_wakeup = esp_bt_check_wakeup_by_bt(); +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE // need to check if need to call pm lock here s_ble_active = true; } +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE +static bool esp_bt_check_wakeup_by_bt(void) +{ + return (esp_sleep_get_wakeup_causes() & ESP_SLEEP_WAKEUP_BT); +} +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE + esp_err_t controller_sleep_init(modem_clock_lpclk_src_t slow_clk_src) { esp_err_t rc = 0; From bc09833dbf2eba032a099f65b2a1b79dd7f2751f Mon Sep 17 00:00:00 2001 From: cjin Date: Fri, 19 Sep 2025 16:47:35 +0800 Subject: [PATCH 2/4] feat(ble): get wakeup cause when wake up on ESP32-C6 --- components/bt/controller/esp32c6/bt.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/components/bt/controller/esp32c6/bt.c b/components/bt/controller/esp32c6/bt.c index fdb38fa979..351474d6dc 100644 --- a/components/bt/controller/esp32c6/bt.c +++ b/components/bt/controller/esp32c6/bt.c @@ -125,6 +125,15 @@ enum { }; #endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +typedef union { + struct { + uint32_t rtc_freq:20; + uint32_t rsv:11; + uint32_t bt_wakeup:1; + }; + uint32_t val; +} bt_wakeup_params_t; + /* External functions or variables ************************************************************************ */ @@ -221,6 +230,9 @@ static void esp_bt_ctrl_log_partition_get_and_erase_first_block(void); #endif // CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE #endif /* !CONFIG_BT_LE_CONTROLLER_LOG_MODE_BLE_LOG_V2 */ #endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE +static bool esp_bt_check_wakeup_by_bt(void); +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE /* Local variable definition *************************************************************************** */ @@ -759,6 +771,7 @@ IRAM_ATTR void controller_sleep_cb(uint32_t enable_tick, void *arg) IRAM_ATTR void controller_wakeup_cb(void *arg) { + bt_wakeup_params_t *params; if (s_ble_active) { return; } @@ -769,15 +782,23 @@ IRAM_ATTR void controller_wakeup_cb(void *arg) assert(esp_rom_get_cpu_ticks_per_us() == pm_config.max_freq_mhz); r_ble_rtc_wake_up_state_clr(); #endif //CONFIG_PM_ENABLE + params = (bt_wakeup_params_t *)arg; esp_phy_enable(PHY_MODEM_BT); if (s_bt_lpclk_src == MODEM_CLOCK_LPCLK_SRC_RC_SLOW) { - uint32_t *clk_freq = (uint32_t *)arg; - *clk_freq = esp_clk_tree_lp_slow_get_freq_hz(ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED) / 5; + params->rtc_freq = esp_clk_tree_lp_slow_get_freq_hz(ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED) / 5; } +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE + params->bt_wakeup = esp_bt_check_wakeup_by_bt(); +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE s_ble_active = true; } #if CONFIG_FREERTOS_USE_TICKLESS_IDLE +static bool esp_bt_check_wakeup_by_bt(void) +{ + return (esp_sleep_get_wakeup_causes() & ESP_SLEEP_WAKEUP_BT); +} + static esp_err_t sleep_modem_ble_mac_retention_init(void *arg) { uint8_t size; From f8dc8242ba70ba4e64f690ac23a9881c8dc9e4b5 Mon Sep 17 00:00:00 2001 From: cjin Date: Fri, 19 Sep 2025 16:47:45 +0800 Subject: [PATCH 3/4] feat(ble): get wakeup cause when wake up on ESP32-C5 --- components/bt/controller/esp32c5/bt.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/components/bt/controller/esp32c5/bt.c b/components/bt/controller/esp32c5/bt.c index e6070f520c..90d2db2ec0 100644 --- a/components/bt/controller/esp32c5/bt.c +++ b/components/bt/controller/esp32c5/bt.c @@ -109,6 +109,16 @@ enum { BLE_LOG_INTERFACE_FLAG_END, }; #endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED + +typedef union { + struct { + uint32_t rtc_freq:20; + uint32_t rsv:11; + uint32_t bt_wakeup:1; + }; + uint32_t val; +} bt_wakeup_params_t; + /* External functions or variables ************************************************************************ */ @@ -196,6 +206,9 @@ static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, u static void esp_bt_ctrl_log_partition_get_and_erase_first_block(void); #endif // CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE #endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE +static bool esp_bt_check_wakeup_by_bt(void); +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE /* Local variable definition *************************************************************************** */ @@ -628,6 +641,7 @@ IRAM_ATTR void controller_sleep_cb(uint32_t enable_tick, void *arg) IRAM_ATTR void controller_wakeup_cb(void *arg) { + bt_wakeup_params_t *params; if (s_ble_active) { return; } @@ -638,15 +652,23 @@ IRAM_ATTR void controller_wakeup_cb(void *arg) assert(esp_rom_get_cpu_ticks_per_us() == pm_config.max_freq_mhz); r_ble_rtc_wake_up_state_clr(); #endif //CONFIG_PM_ENABLE + params = (bt_wakeup_params_t *)arg; esp_phy_enable(PHY_MODEM_BT); if (s_bt_lpclk_src == MODEM_CLOCK_LPCLK_SRC_RC_SLOW) { - uint32_t *clk_freq = (uint32_t *)arg; - *clk_freq = esp_clk_tree_lp_slow_get_freq_hz(ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED) / 5; + params->rtc_freq = esp_clk_tree_lp_slow_get_freq_hz(ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED) / 5; } +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE + params->bt_wakeup = esp_bt_check_wakeup_by_bt(); +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE s_ble_active = true; } #if CONFIG_FREERTOS_USE_TICKLESS_IDLE +static bool esp_bt_check_wakeup_by_bt(void) +{ + return (esp_sleep_get_wakeup_causes() & ESP_SLEEP_WAKEUP_BT); +} + static esp_err_t sleep_modem_ble_mac_retention_init(void *arg) { uint8_t size; From 32b71fa3943f62862d391ae4adfba928e33a6598 Mon Sep 17 00:00:00 2001 From: cjin Date: Fri, 19 Sep 2025 16:47:57 +0800 Subject: [PATCH 4/4] feat(ble): get wakeup cause when wake up on ESP32-H2 --- components/bt/controller/esp32h2/bt.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/components/bt/controller/esp32h2/bt.c b/components/bt/controller/esp32h2/bt.c index 306587b9e5..8533a1aa99 100644 --- a/components/bt/controller/esp32h2/bt.c +++ b/components/bt/controller/esp32h2/bt.c @@ -117,6 +117,15 @@ enum { BLE_LOG_INTERFACE_FLAG_END, }; #endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED + +typedef union { + struct { + uint32_t rtc_freq:20; + uint32_t rsv:11; + uint32_t bt_wakeup:1; + }; + uint32_t val; +} bt_wakeup_params_t; /* External functions or variables ************************************************************************ */ @@ -214,6 +223,9 @@ static void esp_bt_ctrl_log_partition_get_and_erase_first_block(void); #endif // CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE #endif /* !CONFIG_BT_LE_CONTROLLER_LOG_MODE_BLE_LOG_V2 */ #endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE +static bool esp_bt_check_wakeup_by_bt(void); +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE /* Local variable definition *************************************************************************** */ @@ -727,6 +739,7 @@ IRAM_ATTR void controller_sleep_cb(uint32_t enable_tick, void *arg) IRAM_ATTR void controller_wakeup_cb(void *arg) { + bt_wakeup_params_t *params; if (s_ble_active) { return; } @@ -737,15 +750,23 @@ IRAM_ATTR void controller_wakeup_cb(void *arg) assert(esp_rom_get_cpu_ticks_per_us() == pm_config.max_freq_mhz); r_ble_rtc_wake_up_state_clr(); #endif //CONFIG_PM_ENABLE + params = (bt_wakeup_params_t *)arg; esp_phy_enable(PHY_MODEM_BT); if (s_bt_lpclk_src == MODEM_CLOCK_LPCLK_SRC_RC_SLOW) { - uint32_t *clk_freq = (uint32_t *)arg; - *clk_freq = esp_clk_tree_lp_slow_get_freq_hz(ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED) / 5; + params->rtc_freq = esp_clk_tree_lp_slow_get_freq_hz(ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED) / 5; } +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE + params->bt_wakeup = esp_bt_check_wakeup_by_bt(); +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE s_ble_active = true; } #ifdef CONFIG_FREERTOS_USE_TICKLESS_IDLE +static bool esp_bt_check_wakeup_by_bt(void) +{ + return (esp_sleep_get_wakeup_causes() & ESP_SLEEP_WAKEUP_BT); +} + static esp_err_t sleep_modem_ble_mac_retention_init(void *arg) { uint8_t size;