diff --git a/components/bt/controller/esp32c2/Kconfig.in b/components/bt/controller/esp32c2/Kconfig.in index fc0cdd9bb0..dbaf805fac 100644 --- a/components/bt/controller/esp32c2/Kconfig.in +++ b/components/bt/controller/esp32c2/Kconfig.in @@ -373,21 +373,6 @@ config BT_LE_SLEEP_ENABLE help Enable BLE sleep -choice BT_LE_WAKEUP_SOURCE - prompt "BLE light sleep wakeup source" - depends on BT_LE_SLEEP_ENABLE - default BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER - config BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - bool "Use ESP timer to wakeup CPU" - help - Use esp timer to wakeup CPU - - config BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER - bool "Use BLE rtc timer to wakeup CPU" - help - Use BLE rtc timer to wakeup CPU -endchoice - config BT_LE_USE_ESP_TIMER bool "Use Esp Timer for callout" depends on !BT_NIMBLE_ENABLED diff --git a/components/bt/controller/esp32c2/bt.c b/components/bt/controller/esp32c2/bt.c index 45d0628dcb..a0bd38381d 100644 --- a/components/bt/controller/esp32c2/bt.c +++ b/components/bt/controller/esp32c2/bt.c @@ -190,15 +190,7 @@ static DRAM_ATTR esp_pm_lock_handle_t s_pm_lock = NULL; #define BTDM_MIN_TIMER_UNCERTAINTY_US (200) #endif /* #ifdef CONFIG_PM_ENABLE */ -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER #define BLE_RTC_DELAY_US (1800) -#endif - -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER -#define BLE_RTC_DELAY_US (0) -static void ble_sleep_timer_callback(void *arg); -static DRAM_ATTR esp_timer_handle_t s_ble_sleep_timer = NULL; -#endif static const struct osi_coex_funcs_t s_osi_coex_funcs_ro = { @@ -442,34 +434,7 @@ IRAM_ATTR void controller_sleep_cb(uint32_t enable_tick, void *arg) return; } #ifdef CONFIG_PM_ENABLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - uint32_t delta_tick; - uint32_t us_to_sleep; - uint32_t sleep_tick; - uint32_t tick_invalid = *(uint32_t*)(arg); - assert(arg != NULL); - if (!tick_invalid) { - sleep_tick = r_os_cputime_get32(); - // start a timer to wake up and acquire the pm_lock before modem_sleep awakes - delta_tick = enable_tick - sleep_tick; - if (delta_tick & 0x80000000) { - return; - } - us_to_sleep = r_os_cputime_ticks_to_usecs(delta_tick); - if (us_to_sleep <= BTDM_MIN_TIMER_UNCERTAINTY_US) { - return; - } - esp_err_t err = esp_timer_start_once(s_ble_sleep_timer, us_to_sleep - BTDM_MIN_TIMER_UNCERTAINTY_US); - if (err != ESP_OK) { - ESP_LOGW(NIMBLE_PORT_LOG_TAG, "ESP timer start failed\n"); - return; - } - } -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER r_ble_rtc_wake_up_state_clr(); -#endif esp_pm_lock_release(s_pm_lock); #endif // CONFIG_PM_ENABLE esp_phy_disable(); @@ -489,16 +454,6 @@ IRAM_ATTR void controller_wakeup_cb(void *arg) s_ble_active = true; } -#ifdef CONFIG_PM_ENABLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER -static void ble_sleep_timer_callback(void * arg) -{ - -} - -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER -#endif // CONFIG_PM_ENABLE - esp_err_t controller_sleep_init(void) { esp_err_t rc = 0; @@ -518,45 +473,20 @@ esp_err_t controller_sleep_init(void) if (rc != ESP_OK) { goto error; } - esp_pm_lock_acquire(s_pm_lock); -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - esp_timer_create_args_t create_args = { - .callback = ble_sleep_timer_callback, - .arg = NULL, - .name = "btSlp" - }; - rc = esp_timer_create(&create_args, &s_ble_sleep_timer); - if (rc != ESP_OK) { - goto error; - } - ESP_LOGW(NIMBLE_PORT_LOG_TAG, "Enable light sleep, the wake up source is ESP timer"); -#endif //CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER esp_sleep_enable_bt_wakeup(); ESP_LOGW(NIMBLE_PORT_LOG_TAG, "Enable light sleep, the wake up source is BLE timer"); -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER return rc; error: /*lock should release first and then delete*/ if (s_pm_lock != NULL) { - esp_pm_lock_release(s_pm_lock); esp_pm_lock_delete(s_pm_lock); s_pm_lock = NULL; } -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - if (s_ble_sleep_timer != NULL) { - esp_timer_stop(s_ble_sleep_timer); - esp_timer_delete(s_ble_sleep_timer); - s_ble_sleep_timer = NULL; - } -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER esp_sleep_disable_bt_wakeup(); -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER #endif //CONFIG_PM_ENABLE return rc; @@ -565,26 +495,13 @@ error: void controller_sleep_deinit(void) { #ifdef CONFIG_PM_ENABLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER r_ble_rtc_wake_up_state_clr(); esp_sleep_disable_bt_wakeup(); -#endif //CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL, ESP_PD_OPTION_AUTO); /*lock should release first and then delete*/ - if (s_ble_active) { - esp_pm_lock_release(s_pm_lock); - } - esp_pm_lock_delete(s_pm_lock); s_pm_lock = NULL; -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - if (s_ble_sleep_timer != NULL) { - esp_timer_stop(s_ble_sleep_timer); - esp_timer_delete(s_ble_sleep_timer); - s_ble_sleep_timer = NULL; - } -#endif //CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER #endif //CONFIG_PM_ENABLE } @@ -676,13 +593,6 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) esp_phy_modem_init(); periph_module_enable(PERIPH_BT_MODULE); - // init phy - esp_phy_enable(); - s_ble_active = true; - - // init bb - bt_bb_v2_init_cmplx(1); - if (ble_osi_coex_funcs_register((struct osi_coex_funcs_t *)&s_osi_coex_funcs_ro) != 0) { ESP_LOGW(NIMBLE_PORT_LOG_TAG, "osi coex funcs reg failed"); ret = ESP_ERR_INVALID_ARG; @@ -719,7 +629,6 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) free_controller: controller_sleep_deinit(); ble_controller_deinit(); - esp_phy_disable(); esp_phy_modem_deinit(); #if CONFIG_BT_NIMBLE_ENABLED ble_npl_eventq_deinit(nimble_port_get_dflt_eventq()); @@ -742,11 +651,6 @@ esp_err_t esp_bt_controller_deinit(void) controller_sleep_deinit(); - if (s_ble_active) { - esp_phy_disable(); - s_ble_active = false; - } - ble_controller_deinit(); #if CONFIG_BT_NIMBLE_ENABLED @@ -783,6 +687,17 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode) ESP_LOGW(NIMBLE_PORT_LOG_TAG, "invalid controller state"); return ESP_FAIL; } + if (!s_ble_active) { +#if CONFIG_PM_ENABLE + esp_pm_lock_acquire(s_pm_lock); +#endif // CONFIG_PM_ENABLE + // init phy + esp_phy_enable(); + s_ble_active = true; + } + // init bb + bt_bb_v2_init_cmplx(1); + #if CONFIG_SW_COEXIST_ENABLE coex_enable(); #endif @@ -798,6 +713,13 @@ error: #if CONFIG_SW_COEXIST_ENABLE coex_disable(); #endif + if (s_ble_active) { + esp_phy_disable(); +#if CONFIG_PM_ENABLE + esp_pm_lock_release(s_pm_lock); +#endif // CONFIG_PM_ENABLE + s_ble_active = false; + } return ret; } @@ -810,6 +732,14 @@ esp_err_t esp_bt_controller_disable(void) if (ble_controller_disable() != 0) { return ESP_FAIL; } + + if (s_ble_active) { + esp_phy_disable(); +#if CONFIG_PM_ENABLE + esp_pm_lock_release(s_pm_lock); +#endif // CONFIG_PM_ENABLE + s_ble_active = false; + } #if CONFIG_SW_COEXIST_ENABLE coex_disable(); #endif diff --git a/components/bt/controller/esp32c6/Kconfig.in b/components/bt/controller/esp32c6/Kconfig.in index ad988350cb..81107fd36c 100644 --- a/components/bt/controller/esp32c6/Kconfig.in +++ b/components/bt/controller/esp32c6/Kconfig.in @@ -393,21 +393,6 @@ config BT_LE_SLEEP_ENABLE help Enable BLE sleep -choice BT_LE_WAKEUP_SOURCE - prompt "BLE light sleep wakeup source" - depends on BT_LE_SLEEP_ENABLE - default BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER - config BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - bool "Use ESP timer to wakeup CPU" - help - Use esp timer to wakeup CPU - - config BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER - bool "Use BLE rtc timer to wakeup CPU" - help - Use BLE rtc timer to wakeup CPU -endchoice - choice BT_LE_LP_CLK_SRC prompt "BLE low power clock source" default BT_LE_LP_CLK_SRC_MAIN_XTAL diff --git a/components/bt/controller/esp32c6/bt.c b/components/bt/controller/esp32c6/bt.c index 56b2a28e83..c70d4d03f4 100644 --- a/components/bt/controller/esp32c6/bt.c +++ b/components/bt/controller/esp32c6/bt.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -197,27 +197,14 @@ static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, b static DRAM_ATTR esp_bt_controller_status_t ble_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE; /* This variable tells if BLE is running */ -#if CONFIG_FREERTOS_USE_TICKLESS_IDLE -static bool s_ble_backed_up = false; -#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE - static bool s_ble_active = false; #ifdef CONFIG_PM_ENABLE static DRAM_ATTR esp_pm_lock_handle_t s_pm_lock = NULL; #define BTDM_MIN_TIMER_UNCERTAINTY_US (200) #endif // CONFIG_PM_ENABLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER #define BLE_RTC_DELAY_US_LIGHT_SLEEP (2500) #define BLE_RTC_DELAY_US_MODEM_SLEEP (500) -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER - -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER -#define BLE_RTC_DELAY_US_LIGHT_SLEEP (2000) -#define BLE_RTC_DELAY_US_MODEM_SLEEP (0) -static void ble_sleep_timer_callback(void *arg); -static DRAM_ATTR esp_timer_handle_t s_ble_sleep_timer = NULL; -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER static const struct osi_coex_funcs_t s_osi_coex_funcs_ro = { ._magic = OSI_COEX_MAGIC_VALUE, @@ -474,38 +461,9 @@ IRAM_ATTR void controller_sleep_cb(uint32_t enable_tick, void *arg) return; } #if CONFIG_FREERTOS_USE_TICKLESS_IDLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - uint32_t delta_tick; - uint32_t us_to_sleep; - uint32_t sleep_tick; - uint32_t tick_invalid = *(uint32_t*)(arg); - assert(arg != NULL); - if (!tick_invalid) { - sleep_tick = r_os_cputime_get32(); - /* start a timer to wake up and acquire the pm_lock before modem_sleep awakes */ - delta_tick = enable_tick - sleep_tick; - if (delta_tick & 0x80000000) { - return; - } - us_to_sleep = r_os_cputime_ticks_to_usecs(delta_tick); - if (us_to_sleep <= BTDM_MIN_TIMER_UNCERTAINTY_US) { - return; - } - esp_err_t err = esp_timer_start_once(s_ble_sleep_timer, - us_to_sleep - BTDM_MIN_TIMER_UNCERTAINTY_US); - if (err != ESP_OK) { - ESP_LOGW(NIMBLE_PORT_LOG_TAG, "ESP timer start failed"); - return; - } - } -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER r_ble_rtc_wake_up_state_clr(); -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER #if SOC_PM_RETENTION_HAS_CLOCK_BUG sleep_retention_do_extra_retention(true); - s_ble_backed_up = true; #endif // SOC_PM_RETENTION_HAS_CLOCK_BUG #endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */ esp_phy_disable(); @@ -521,13 +479,10 @@ IRAM_ATTR void controller_wakeup_cb(void *arg) return; } #ifdef CONFIG_PM_ENABLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER esp_pm_lock_acquire(s_pm_lock); r_ble_rtc_wake_up_state_clr(); -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER #if CONFIG_FREERTOS_USE_TICKLESS_IDLE && SOC_PM_RETENTION_HAS_CLOCK_BUG sleep_retention_do_extra_retention(false); - s_ble_backed_up = false; #endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE && SOC_PM_RETENTION_HAS_CLOCK_BUG */ #endif //CONFIG_PM_ENABLE esp_phy_enable(); @@ -535,14 +490,7 @@ IRAM_ATTR void controller_wakeup_cb(void *arg) } #if CONFIG_FREERTOS_USE_TICKLESS_IDLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER -static void ble_sleep_timer_callback(void * arg) -{ - esp_pm_lock_acquire(s_pm_lock); -} -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - -esp_err_t sleep_modem_ble_mac_modem_state_init(uint8_t extra) +static esp_err_t sleep_modem_ble_mac_modem_state_init(uint8_t extra) { uint8_t size; const sleep_retention_entries_config_t *ble_mac_modem_config = esp_ble_mac_retention_link_get(&size, extra); @@ -579,49 +527,22 @@ esp_err_t controller_sleep_init(void) if (rc != ESP_OK) { goto error; } - esp_pm_lock_acquire(s_pm_lock); #if CONFIG_FREERTOS_USE_TICKLESS_IDLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - esp_timer_create_args_t create_args = { - .callback = ble_sleep_timer_callback, - .arg = NULL, - .name = "btSlp" - }; - rc = esp_timer_create(&create_args, &s_ble_sleep_timer); - if (rc != ESP_OK) { - goto error; - } - ESP_LOGW(NIMBLE_PORT_LOG_TAG, "Enable light sleep, the wake up source is ESP timer"); -#endif //CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - /* Create a new regdma link for BLE related register restoration */ rc = sleep_modem_ble_mac_modem_state_init(1); assert(rc == 0); -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER esp_sleep_enable_bt_wakeup(); ESP_LOGW(NIMBLE_PORT_LOG_TAG, "Enable light sleep, the wake up source is BLE timer"); -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER #endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */ return rc; error: #if CONFIG_FREERTOS_USE_TICKLESS_IDLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER esp_sleep_disable_bt_wakeup(); -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER - -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - if (s_ble_sleep_timer != NULL) { - esp_timer_stop(s_ble_sleep_timer); - esp_timer_delete(s_ble_sleep_timer); - s_ble_sleep_timer = NULL; - } -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER #endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */ /*lock should release first and then delete*/ if (s_pm_lock != NULL) { - esp_pm_lock_release(s_pm_lock); esp_pm_lock_delete(s_pm_lock); s_pm_lock = NULL; } @@ -633,26 +554,12 @@ error: void controller_sleep_deinit(void) { #if CONFIG_FREERTOS_USE_TICKLESS_IDLE - if (s_ble_backed_up) { - sleep_retention_module_deinit(); - s_ble_backed_up = false; - } -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER r_ble_rtc_wake_up_state_clr(); esp_sleep_disable_bt_wakeup(); -#endif //CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER sleep_modem_ble_mac_modem_state_deinit(); -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - if (s_ble_sleep_timer != NULL) { - esp_timer_stop(s_ble_sleep_timer); - esp_timer_delete(s_ble_sleep_timer); - s_ble_sleep_timer = NULL; - } -#endif //CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER #endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */ #ifdef CONFIG_PM_ENABLE /* lock should be released first */ - esp_pm_lock_release(s_pm_lock); esp_pm_lock_delete(s_pm_lock); s_pm_lock = NULL; #endif //CONFIG_PM_ENABLE @@ -818,9 +725,6 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) #endif #endif /* CONFIG_BT_LE_LP_CLK_SRC_MAIN_XTAL */ esp_phy_modem_init(); - esp_phy_enable(); - esp_btbb_enable(); - s_ble_active = true; if (ble_osi_coex_funcs_register((struct osi_coex_funcs_t *)&s_osi_coex_funcs_ro) != 0) { ESP_LOGW(NIMBLE_PORT_LOG_TAG, "osi coex funcs reg failed"); @@ -877,8 +781,6 @@ controller_init_err: #endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED ble_controller_deinit(); modem_deint: - esp_btbb_disable(); - esp_phy_disable(); esp_phy_modem_deinit(); modem_clock_deselect_lp_clock_source(PERIPH_BT_MODULE); modem_clock_module_disable(PERIPH_BT_MODULE); @@ -904,12 +806,6 @@ esp_err_t esp_bt_controller_deinit(void) controller_sleep_deinit(); - esp_btbb_disable(); - - if (s_ble_active) { - esp_phy_disable(); - s_ble_active = false; - } esp_phy_modem_deinit(); modem_clock_deselect_lp_clock_source(PERIPH_BT_MODULE); modem_clock_module_disable(PERIPH_BT_MODULE); @@ -952,7 +848,14 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode) ESP_LOGW(NIMBLE_PORT_LOG_TAG, "invalid controller state"); return ESP_FAIL; } - + if (!s_ble_active) { +#if CONFIG_PM_ENABLE + esp_pm_lock_acquire(s_pm_lock); +#endif // CONFIG_PM_ENABLE + esp_phy_enable(); + esp_btbb_enable(); + s_ble_active = true; + } #if CONFIG_SW_COEXIST_ENABLE coex_enable(); #endif // CONFIG_SW_COEXIST_ENABLE @@ -968,6 +871,14 @@ error: #if CONFIG_SW_COEXIST_ENABLE coex_disable(); #endif + if (s_ble_active) { + esp_btbb_disable(); + esp_phy_disable(); +#if CONFIG_PM_ENABLE + esp_pm_lock_release(s_pm_lock); +#endif // CONFIG_PM_ENABLE + s_ble_active = false; + } return ret; } @@ -983,6 +894,19 @@ esp_err_t esp_bt_controller_disable(void) #if CONFIG_SW_COEXIST_ENABLE coex_disable(); #endif + if (s_ble_active) { + esp_btbb_disable(); + esp_phy_disable(); +#if CONFIG_PM_ENABLE + esp_pm_lock_release(s_pm_lock); +#endif // CONFIG_PM_ENABLE + s_ble_active = false; + } else { +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE + /* Avoid consecutive backup of register cause assertion */ + sleep_retention_module_deinit(); +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE + } ble_controller_status = ESP_BT_CONTROLLER_STATUS_INITED; return ESP_OK; } diff --git a/components/bt/controller/esp32h2/Kconfig.in b/components/bt/controller/esp32h2/Kconfig.in index 4801ba53fa..17adb6a791 100644 --- a/components/bt/controller/esp32h2/Kconfig.in +++ b/components/bt/controller/esp32h2/Kconfig.in @@ -393,21 +393,6 @@ config BT_LE_SLEEP_ENABLE help Enable BLE sleep -choice BT_LE_WAKEUP_SOURCE - prompt "BLE light sleep wakeup source" - depends on BT_LE_SLEEP_ENABLE - default BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER - config BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - bool "Use ESP timer to wakeup CPU" - help - Use esp timer to wakeup CPU - - config BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER - bool "Use BLE rtc timer to wakeup CPU" - help - Use BLE rtc timer to wakeup CPU -endchoice - choice BT_LE_LP_CLK_SRC prompt "BLE low power clock source" default BT_LE_LP_CLK_SRC_MAIN_XTAL diff --git a/components/bt/controller/esp32h2/bt.c b/components/bt/controller/esp32h2/bt.c index e49410abb5..37e250b39d 100644 --- a/components/bt/controller/esp32h2/bt.c +++ b/components/bt/controller/esp32h2/bt.c @@ -199,17 +199,8 @@ static DRAM_ATTR esp_pm_lock_handle_t s_pm_lock = NULL; #define BTDM_MIN_TIMER_UNCERTAINTY_US (200) #endif // CONFIG_PM_ENABLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER #define BLE_RTC_DELAY_US_LIGHT_SLEEP (5100) #define BLE_RTC_DELAY_US_MODEM_SLEEP (1500) -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER - -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER -#define BLE_RTC_DELAY_US_LIGHT_SLEEP (2000) -#define BLE_RTC_DELAY_US_MODEM_SLEEP (0) -static void ble_sleep_timer_callback(void *arg); -static DRAM_ATTR esp_timer_handle_t s_ble_sleep_timer = NULL; -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER static const struct osi_coex_funcs_t s_osi_coex_funcs_ro = { ._magic = OSI_COEX_MAGIC_VALUE, @@ -466,35 +457,7 @@ IRAM_ATTR void controller_sleep_cb(uint32_t enable_tick, void *arg) return; } #if CONFIG_FREERTOS_USE_TICKLESS_IDLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - uint32_t delta_tick; - uint32_t us_to_sleep; - uint32_t sleep_tick; - uint32_t tick_invalid = *(uint32_t*)(arg); - assert(arg != NULL); - if (!tick_invalid) { - sleep_tick = r_os_cputime_get32(); - /* start a timer to wake up and acquire the pm_lock before modem_sleep awakes */ - delta_tick = enable_tick - sleep_tick; - if (delta_tick & 0x80000000) { - return; - } - us_to_sleep = r_os_cputime_ticks_to_usecs(delta_tick); - if (us_to_sleep <= BTDM_MIN_TIMER_UNCERTAINTY_US) { - return; - } - esp_err_t err = esp_timer_start_once(s_ble_sleep_timer, - us_to_sleep - BTDM_MIN_TIMER_UNCERTAINTY_US); - if (err != ESP_OK) { - ESP_LOGW(NIMBLE_PORT_LOG_TAG, "ESP timer start failed\n"); - return; - } - } -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER r_ble_rtc_wake_up_state_clr(); -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER #if SOC_PM_RETENTION_HAS_CLOCK_BUG sleep_retention_do_extra_retention(true); #endif // SOC_PM_RETENTION_HAS_CLOCK_BUG @@ -512,10 +475,8 @@ IRAM_ATTR void controller_wakeup_cb(void *arg) return; } #ifdef CONFIG_PM_ENABLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER esp_pm_lock_acquire(s_pm_lock); r_ble_rtc_wake_up_state_clr(); -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER #if CONFIG_FREERTOS_USE_TICKLESS_IDLE && SOC_PM_RETENTION_HAS_CLOCK_BUG sleep_retention_do_extra_retention(false); #endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE && SOC_PM_RETENTION_HAS_CLOCK_BUG */ @@ -525,13 +486,6 @@ IRAM_ATTR void controller_wakeup_cb(void *arg) } #ifdef CONFIG_FREERTOS_USE_TICKLESS_IDLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER -static void ble_sleep_timer_callback(void * arg) -{ - esp_pm_lock_acquire(s_pm_lock); -} -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - static esp_err_t sleep_modem_ble_mac_modem_state_init(uint8_t extra) { uint8_t size; @@ -570,49 +524,22 @@ esp_err_t controller_sleep_init(void) if (rc != ESP_OK) { goto error; } - esp_pm_lock_acquire(s_pm_lock); #if CONFIG_FREERTOS_USE_TICKLESS_IDLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - esp_timer_create_args_t create_args = { - .callback = ble_sleep_timer_callback, - .arg = NULL, - .name = "btSlp" - }; - rc = esp_timer_create(&create_args, &s_ble_sleep_timer); - if (rc != ESP_OK) { - goto error; - } - ESP_LOGW(NIMBLE_PORT_LOG_TAG, "Enable light sleep, the wake up source is ESP timer"); -#endif //CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - /* Create a new regdma link for BLE related register restoration */ rc = sleep_modem_ble_mac_modem_state_init(1); assert(rc == 0); -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER esp_sleep_enable_bt_wakeup(); ESP_LOGW(NIMBLE_PORT_LOG_TAG, "Enable light sleep, the wake up source is BLE timer"); -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER #endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */ return rc; error: #if CONFIG_FREERTOS_USE_TICKLESS_IDLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER esp_sleep_disable_bt_wakeup(); -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER - -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - if (s_ble_sleep_timer != NULL) { - esp_timer_stop(s_ble_sleep_timer); - esp_timer_delete(s_ble_sleep_timer); - s_ble_sleep_timer = NULL; - } -#endif // CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER #endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */ /*lock should release first and then delete*/ if (s_pm_lock != NULL) { - esp_pm_lock_release(s_pm_lock); esp_pm_lock_delete(s_pm_lock); s_pm_lock = NULL; } @@ -624,22 +551,12 @@ error: void controller_sleep_deinit(void) { #if CONFIG_FREERTOS_USE_TICKLESS_IDLE -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER r_ble_rtc_wake_up_state_clr(); esp_sleep_disable_bt_wakeup(); -#endif //CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER sleep_modem_ble_mac_modem_state_deinit(); -#ifdef CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER - if (s_ble_sleep_timer != NULL) { - esp_timer_stop(s_ble_sleep_timer); - esp_timer_delete(s_ble_sleep_timer); - s_ble_sleep_timer = NULL; - } -#endif //CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER #endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */ #ifdef CONFIG_PM_ENABLE /* lock should be released first */ - esp_pm_lock_release(s_pm_lock); esp_pm_lock_delete(s_pm_lock); s_pm_lock = NULL; #endif //CONFIG_PM_ENABLE @@ -797,9 +714,6 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) assert(0); #endif #endif /* CONFIG_BT_LE_LP_CLK_SRC_MAIN_XTAL */ - esp_phy_enable(); - esp_btbb_enable(); - s_ble_active = true; if (ble_osi_coex_funcs_register((struct osi_coex_funcs_t *)&s_osi_coex_funcs_ro) != 0) { ESP_LOGW(NIMBLE_PORT_LOG_TAG, "osi coex funcs reg failed"); @@ -857,8 +771,6 @@ controller_init_err: #endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED ble_controller_deinit(); modem_deint: - esp_btbb_disable(); - esp_phy_disable(); modem_clock_deselect_lp_clock_source(PERIPH_BT_MODULE); modem_clock_module_disable(PERIPH_BT_MODULE); #if CONFIG_BT_NIMBLE_ENABLED @@ -883,12 +795,6 @@ esp_err_t esp_bt_controller_deinit(void) controller_sleep_deinit(); - esp_btbb_disable(); - - if (s_ble_active) { - esp_phy_disable(); - s_ble_active = false; - } modem_clock_deselect_lp_clock_source(PERIPH_BT_MODULE); modem_clock_module_disable(PERIPH_BT_MODULE); @@ -930,7 +836,14 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode) ESP_LOGW(NIMBLE_PORT_LOG_TAG, "invalid controller state"); return ESP_FAIL; } - + if (!s_ble_active) { +#if CONFIG_PM_ENABLE + esp_pm_lock_acquire(s_pm_lock); +#endif // CONFIG_PM_ENABLE + esp_phy_enable(); + s_ble_active = true; + } + esp_btbb_enable(); #if CONFIG_SW_COEXIST_ENABLE coex_enable(); #endif // CONFIG_SW_COEXIST_ENABLE @@ -946,6 +859,14 @@ error: #if CONFIG_SW_COEXIST_ENABLE coex_disable(); #endif + esp_btbb_disable(); + if (s_ble_active) { + esp_phy_disable(); +#if CONFIG_PM_ENABLE + esp_pm_lock_release(s_pm_lock); +#endif // CONFIG_PM_ENABLE + s_ble_active = false; + } return ret; } @@ -961,6 +882,19 @@ esp_err_t esp_bt_controller_disable(void) #if CONFIG_SW_COEXIST_ENABLE coex_disable(); #endif + esp_btbb_disable(); + if (s_ble_active) { + esp_phy_disable(); +#if CONFIG_PM_ENABLE + esp_pm_lock_release(s_pm_lock); +#endif // CONFIG_PM_ENABLE + s_ble_active = false; + } else { +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE + /* Avoid consecutive backup of register cause assertion */ + sleep_retention_module_deinit(); +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE + } ble_controller_status = ESP_BT_CONTROLLER_STATUS_INITED; return ESP_OK; } diff --git a/examples/bluetooth/nimble/power_save/sdkconfig.32m.esp32h2 b/examples/bluetooth/nimble/power_save/sdkconfig.32m.esp32h2 index 272c259232..aa5aebe523 100644 --- a/examples/bluetooth/nimble/power_save/sdkconfig.32m.esp32h2 +++ b/examples/bluetooth/nimble/power_save/sdkconfig.32m.esp32h2 @@ -3,7 +3,6 @@ CONFIG_IDF_TARGET="esp32h2" # Bluetooth Low Power Config CONFIG_BT_LE_SLEEP_ENABLE=y # CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER is not set -CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER=y CONFIG_BT_LE_LP_CLK_SRC_MAIN_XTAL=y # CONFIG_BT_LE_LP_CLK_SRC_DEFAULT is not set diff --git a/examples/bluetooth/nimble/power_save/sdkconfig.40m.esp32c6 b/examples/bluetooth/nimble/power_save/sdkconfig.40m.esp32c6 index cdf58be981..885ab812bc 100644 --- a/examples/bluetooth/nimble/power_save/sdkconfig.40m.esp32c6 +++ b/examples/bluetooth/nimble/power_save/sdkconfig.40m.esp32c6 @@ -3,7 +3,6 @@ CONFIG_IDF_TARGET="esp32c6" # Bluetooth Low Power Config CONFIG_BT_LE_SLEEP_ENABLE=y # CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER is not set -CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER=y CONFIG_BT_LE_LP_CLK_SRC_MAIN_XTAL=y # CONFIG_BT_LE_LP_CLK_SRC_DEFAULT is not set diff --git a/examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32c6 b/examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32c6 index 558871847a..fdf2ca28b4 100644 --- a/examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32c6 +++ b/examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32c6 @@ -3,7 +3,6 @@ CONFIG_IDF_TARGET="esp32c6" # Bluetooth Low Power Config CONFIG_BT_LE_SLEEP_ENABLE=y # CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER is not set -CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER=y # CONFIG_BT_LE_LP_CLK_SRC_MAIN_XTAL is not set CONFIG_BT_LE_LP_CLK_SRC_DEFAULT=y diff --git a/examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32h2 b/examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32h2 index d034fa5973..51e03d318e 100644 --- a/examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32h2 +++ b/examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32h2 @@ -3,7 +3,6 @@ CONFIG_IDF_TARGET="esp32h2" # Bluetooth Low Power Config CONFIG_BT_LE_SLEEP_ENABLE=y # CONFIG_BT_LE_WAKEUP_SOURCE_CPU_RTC_TIMER is not set -CONFIG_BT_LE_WAKEUP_SOURCE_BLE_RTC_TIMER=y # CONFIG_BT_LE_LP_CLK_SRC_MAIN_XTAL is not set CONFIG_BT_LE_LP_CLK_SRC_DEFAULT=y