Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_current_issue_after_ble_init_5.1' into 'rele…
Browse files Browse the repository at this point in the history
…ase/v5.1'

ble: move phy enabled/disable to controller enable/disable

See merge request espressif/esp-idf!25081
  • Loading branch information
Isl2017 committed Aug 25, 2023
2 parents 9332a31 + fa806d7 commit 48052e4
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 347 deletions.
15 changes: 0 additions & 15 deletions components/bt/controller/esp32c2/Kconfig.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
122 changes: 26 additions & 96 deletions components/bt/controller/esp32c2/bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -565,26 +495,13 @@ esp_err_t controller_sleep_init(void)
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
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -798,6 +713,13 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode)
#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;
}

Expand All @@ -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
Expand Down
15 changes: 0 additions & 15 deletions components/bt/controller/esp32c6/Kconfig.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 48052e4

Please sign in to comment.