From 4bd5b0c91af842a6a612e1c9e8d50edf6a7835cd Mon Sep 17 00:00:00 2001 From: Tian Hao Date: Fri, 17 Feb 2017 19:24:58 +0800 Subject: [PATCH] component/bt : add bt enable/disable for power save 1. add new APIs bt controller enable/disab/deinit 2. make bt controller work need to call two APIs of esp_bt_controller_init and enable 3. modify phy init to make mac reset once --- components/bt/bluedroid/api/esp_bt_main.c | 3 - components/bt/bt.c | 62 ++++++++++++++++++- components/bt/include/bt.h | 35 ++++++++++- components/bt/lib | 2 +- components/esp32/phy_init.c | 8 ++- docs/api/bluetooth/controller_vhci.rst | 4 ++ examples/bluetooth/ble_adv/main/app_bt.c | 5 ++ examples/bluetooth/blufi/main/blufi_main.c | 6 ++ .../bluetooth/gatt_client/main/gattc_demo.c | 2 + .../bluetooth/gatt_server/main/gatts_demo.c | 5 ++ .../main/gatts_table_creat_demo.c | 30 +++++---- 11 files changed, 141 insertions(+), 21 deletions(-) diff --git a/components/bt/bluedroid/api/esp_bt_main.c b/components/bt/bluedroid/api/esp_bt_main.c index 612a9e020a..c9fe4fc060 100644 --- a/components/bt/bluedroid/api/esp_bt_main.c +++ b/components/bt/bluedroid/api/esp_bt_main.c @@ -17,7 +17,6 @@ #include "btc_task.h" #include "btc_main.h" #include "future.h" -#include "esp_phy_init.h" static bool esp_already_enable = false; static bool esp_already_init = false; @@ -165,8 +164,6 @@ esp_err_t esp_bluedroid_deinit(void) esp_already_init = false; - esp_phy_rf_deinit(); - return ESP_OK; } diff --git a/components/bt/bt.c b/components/bt/bt.c index 2390456afb..440c960e64 100644 --- a/components/bt/bt.c +++ b/components/bt/bt.c @@ -35,6 +35,10 @@ /* not for user call, so don't put to include file */ extern void btdm_osi_funcs_register(void *osi_funcs); extern void btdm_controller_init(void); +extern void btdm_controller_deinit(void); +extern int btdm_controller_enable(esp_bt_mode_t mode); +extern int btdm_controller_disable(esp_bt_mode_t mode); +extern void btdm_rf_bb_init(void); /* VHCI function interface */ typedef struct vhci_host_callback { @@ -71,6 +75,11 @@ struct osi_funcs_t { esp_err_t (* _read_efuse_mac)(uint8_t mac[6]); }; +/* Static variable declare */ +static bool btdm_bb_init_flag = false; + +static xTaskHandle btControllerTaskHandle; + static portMUX_TYPE global_int_mux = portMUX_INITIALIZER_UNLOCKED; static void IRAM_ATTR interrupt_disable(void) @@ -147,16 +156,63 @@ static void bt_controller_task(void *pvParam) { btdm_osi_funcs_register(&osi_funcs); - esp_phy_load_cal_and_init(); - btdm_controller_init(); } +static bool bb_inited; void esp_bt_controller_init() { + bb_inited = false; xTaskCreatePinnedToCore(bt_controller_task, "btController", ESP_TASK_BT_CONTROLLER_STACK, NULL, - ESP_TASK_BT_CONTROLLER_PRIO, NULL, 0); + ESP_TASK_BT_CONTROLLER_PRIO, &btControllerTaskHandle, 0); +} + +void esp_bt_controller_deinit(void) +{ + vTaskDelete(btControllerTaskHandle); + bb_inited = false; +} + +esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode) +{ + int ret; + + if (mode != ESP_BT_MODE_BTDM) { + return ESP_ERR_INVALID_ARG; + } + + esp_phy_load_cal_and_init(); + + if (btdm_bb_init_flag == false) { + btdm_bb_init_flag = true; + btdm_rf_bb_init(); /* only initialise once */ + } + + ret = btdm_controller_enable(mode); + if (ret) { + return ESP_ERR_INVALID_STATE; + } + + return ESP_OK; +} + +esp_err_t esp_bt_controller_disable(esp_bt_mode_t mode) +{ + int ret; + + if (mode != ESP_BT_MODE_BTDM) { + return ESP_ERR_INVALID_ARG; + } + + ret = btdm_controller_disable(mode); + if (ret) { + return ESP_ERR_INVALID_STATE; + } + + esp_phy_rf_deinit(); + + return ESP_OK; } #endif diff --git a/components/bt/include/bt.h b/components/bt/include/bt.h index 926ecfadcd..91617a28f4 100644 --- a/components/bt/include/bt.h +++ b/components/bt/include/bt.h @@ -23,14 +23,47 @@ extern "C" { #endif +/** + * @brief Bluetooth mode for controller enable/disable + */ +typedef enum { + ESP_BT_MODE_ILDE = 0x00, /*!< Bluetooth is not run */ + ESP_BT_MODE_BLE = 0x01, /*!< Run BLE mode */ + ESP_BT_MODE_CLASSIC_BT = 0x02, /*!< Run Classic BT mode */ + ESP_BT_MODE_BTDM = 0x03, /*!< Run dual mode */ +} esp_bt_mode_t; /** - * @brief Initialize BT controller + * @brief Initialize BT controller to allocate task and other resource. * * This function should be called only once, before any other BT functions are called. */ void esp_bt_controller_init(void); +/** + * @brief De-initialize BT controller to free resource and delete task. + * + * This function should be called only once, after any other BT functions are called. + * This function is not whole completed, esp_bt_controller_init cannot called after this function. + */ +void esp_bt_controller_deinit(void); + +/** + * @brief Enable BT controller + * @param mode : the mode(BLE/BT/BTDM) to enable. + * Now only support BTDM. + */ +esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode); + +/** + * @brief Disable BT controller + * @param mode : the mode(BLE/BT/BTDM) to disable. + * Now only support BTDM. + */ +esp_err_t esp_bt_controller_disable(esp_bt_mode_t mode); + + + /** @brief esp_vhci_host_callback * used for vhci call host function to notify what host need to do */ diff --git a/components/bt/lib b/components/bt/lib index 9c1eea6bb0..69616af765 160000 --- a/components/bt/lib +++ b/components/bt/lib @@ -1 +1 @@ -Subproject commit 9c1eea6bb03adc3b3847fff79c3f017652840a46 +Subproject commit 69616af7653f4de6e3b78f475dc10e73f0a20ece diff --git a/components/esp32/phy_init.c b/components/esp32/phy_init.c index 1c67a404ab..340bd7ee72 100644 --- a/components/esp32/phy_init.c +++ b/components/esp32/phy_init.c @@ -40,6 +40,7 @@ static const char* TAG = "phy_init"; /* Count value to indicate if there is peripheral that has initialized PHY and RF */ static int s_phy_rf_init_count = 0; +static bool s_mac_rst_flag = false; static _lock_t s_phy_rf_init_lock; @@ -51,8 +52,11 @@ esp_err_t esp_phy_rf_init(const esp_phy_init_data_t* init_data, _lock_acquire(&s_phy_rf_init_lock); if (s_phy_rf_init_count == 0) { if (is_sleep == false) { - REG_SET_BIT(DPORT_CORE_RST_EN_REG, DPORT_MAC_RST); - REG_CLR_BIT(DPORT_CORE_RST_EN_REG, DPORT_MAC_RST); + if (s_mac_rst_flag == false) { + s_mac_rst_flag = true; + REG_SET_BIT(DPORT_CORE_RST_EN_REG, DPORT_MAC_RST); + REG_CLR_BIT(DPORT_CORE_RST_EN_REG, DPORT_MAC_RST); + } } // Enable WiFi peripheral clock SET_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, 0x87cf); diff --git a/docs/api/bluetooth/controller_vhci.rst b/docs/api/bluetooth/controller_vhci.rst index 35248cbb1e..108a9f678d 100644 --- a/docs/api/bluetooth/controller_vhci.rst +++ b/docs/api/bluetooth/controller_vhci.rst @@ -33,6 +33,7 @@ Type Definitions Enumerations ^^^^^^^^^^^^ +.. doxygenenum:: esp_bt_mode_t Structures ^^^^^^^^^^ @@ -45,6 +46,9 @@ Functions ^^^^^^^^^ .. doxygenfunction:: esp_bt_controller_init +.. doxygenfunction:: esp_bt_controller_deinit +.. doxygenfunction:: esp_bt_controller_enable +.. doxygenfunction:: esp_bt_controller_disable .. doxygenfunction:: esp_vhci_host_check_send_available .. doxygenfunction:: esp_vhci_host_send_packet .. doxygenfunction:: esp_vhci_host_register_callback diff --git a/examples/bluetooth/ble_adv/main/app_bt.c b/examples/bluetooth/ble_adv/main/app_bt.c index f0780c950c..67ab2c8fe0 100644 --- a/examples/bluetooth/ble_adv/main/app_bt.c +++ b/examples/bluetooth/ble_adv/main/app_bt.c @@ -215,6 +215,11 @@ void bleAdvtTask(void *pvParameters) void app_main() { esp_bt_controller_init(); + + if (esp_bt_controller_enable(ESP_BT_MODE_BTDM) != ESP_OK) { + return; + } + xTaskCreatePinnedToCore(&bleAdvtTask, "bleAdvtTask", 2048, NULL, 5, NULL, 0); } diff --git a/examples/bluetooth/blufi/main/blufi_main.c b/examples/bluetooth/blufi/main/blufi_main.c index a95db66eb5..b46ffb4648 100644 --- a/examples/bluetooth/blufi/main/blufi_main.c +++ b/examples/bluetooth/blufi/main/blufi_main.c @@ -317,6 +317,12 @@ void app_main() esp_bt_controller_init(); + ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM); + if (ret) { + BLUFI_ERROR("%s enable bt controller failed\n", __func__); + return; + } + ret = esp_bluedroid_init(); if (ret) { BLUFI_ERROR("%s init bluedroid failed\n", __func__); diff --git a/examples/bluetooth/gatt_client/main/gattc_demo.c b/examples/bluetooth/gatt_client/main/gattc_demo.c index e6ee867293..4a2fa0051d 100644 --- a/examples/bluetooth/gatt_client/main/gattc_demo.c +++ b/examples/bluetooth/gatt_client/main/gattc_demo.c @@ -397,6 +397,8 @@ void gattc_client_test(void) void app_main() { esp_bt_controller_init(); + esp_bt_controller_enable(ESP_BT_MODE_BTDM); + gattc_client_test(); } diff --git a/examples/bluetooth/gatt_server/main/gatts_demo.c b/examples/bluetooth/gatt_server/main/gatts_demo.c index 0afa1630ba..acbf9195a0 100644 --- a/examples/bluetooth/gatt_server/main/gatts_demo.c +++ b/examples/bluetooth/gatt_server/main/gatts_demo.c @@ -394,6 +394,11 @@ void app_main() esp_bt_controller_init(); + ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM); + if (ret) { + ESP_LOGE(GATTS_TAG, "%s enable controller failed\n", __func__); + return; + } ret = esp_bluedroid_init(); if (ret) { ESP_LOGE(GATTS_TAG, "%s init bluetooth failed\n", __func__); diff --git a/examples/bluetooth/gatt_server_service_table/main/gatts_table_creat_demo.c b/examples/bluetooth/gatt_server_service_table/main/gatts_table_creat_demo.c index 024362c5b7..71baf1842c 100644 --- a/examples/bluetooth/gatt_server_service_table/main/gatts_table_creat_demo.c +++ b/examples/bluetooth/gatt_server_service_table/main/gatts_table_creat_demo.c @@ -29,6 +29,7 @@ #include "esp_bt_main.h" #include "gatts_table_creat_demo.h" +#define GATTS_TABLE_TAG "GATTS_TABLE_DEMO" #define HEART_PROFILE_NUM 1 #define HEART_PROFILE_APP_IDX 0 @@ -196,7 +197,7 @@ static const esp_gatts_attr_db_t heart_rate_gatt_db[HRS_IDX_NB] = static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) { - LOG_ERROR("GAP_EVT, event %d\n", event); + ESP_LOGE(GATTS_TABLE_TAG, "GAP_EVT, event %d\n", event); switch (event) { case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: @@ -210,15 +211,15 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) { - LOG_ERROR("event = %x\n",event); + ESP_LOGE(GATTS_TABLE_TAG, "event = %x\n",event); switch (event) { case ESP_GATTS_REG_EVT: - LOG_INFO("%s %d\n", __func__, __LINE__); + ESP_LOGI(GATTS_TABLE_TAG, "%s %d\n", __func__, __LINE__); esp_ble_gap_set_device_name(SAMPLE_DEVICE_NAME); - LOG_INFO("%s %d\n", __func__, __LINE__); + ESP_LOGI(GATTS_TABLE_TAG, "%s %d\n", __func__, __LINE__); esp_ble_gap_config_adv_data(&heart_rate_adv_config); - LOG_INFO("%s %d\n", __func__, __LINE__); + ESP_LOGI(GATTS_TABLE_TAG, "%s %d\n", __func__, __LINE__); esp_ble_gatts_create_attr_tab(heart_rate_gatt_db, gatts_if, HRS_IDX_NB, HEART_RATE_SVC_INST_ID); break; @@ -256,7 +257,7 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, case ESP_GATTS_CONGEST_EVT: break; case ESP_GATTS_CREAT_ATTR_TAB_EVT:{ - LOG_ERROR("The number handle =%x\n",param->add_attr_tab.num_handle); + ESP_LOGE(GATTS_TABLE_TAG, "The number handle =%x\n",param->add_attr_tab.num_handle); if(param->add_attr_tab.num_handle == HRS_IDX_NB){ memcpy(heart_rate_handle_table, param->add_attr_tab.handles, sizeof(heart_rate_handle_table)); @@ -275,14 +276,14 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) { - LOG_INFO("EVT %d, gatts if %d\n", event, gatts_if); + ESP_LOGI(GATTS_TABLE_TAG, "EVT %d, gatts if %d\n", event, gatts_if); /* If event is register event, store the gatts_if for each profile */ if (event == ESP_GATTS_REG_EVT) { if (param->reg.status == ESP_GATT_OK) { heart_rate_profile_tab[HEART_PROFILE_APP_IDX].gatts_if = gatts_if; } else { - LOG_INFO("Reg app failed, app_id %04x, status %d\n", + ESP_LOGI(GATTS_TABLE_TAG, "Reg app failed, app_id %04x, status %d\n", param->reg.app_id, param->reg.status); return; @@ -307,15 +308,22 @@ void app_main() esp_err_t ret; esp_bt_controller_init(); - LOG_INFO("%s init bluetooth\n", __func__); + + ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM); + if (ret) { + ESP_LOGE(GATTS_TABLE_TAG, "%s enable controller failed\n", __func__); + return; + } + + ESP_LOGI(GATTS_TABLE_TAG, "%s init bluetooth\n", __func__); ret = esp_bluedroid_init(); if (ret) { - LOG_ERROR("%s init bluetooth failed\n", __func__); + ESP_LOGE(GATTS_TABLE_TAG, "%s init bluetooth failed\n", __func__); return; } ret = esp_bluedroid_enable(); if (ret) { - LOG_ERROR("%s enable bluetooth failed\n", __func__); + ESP_LOGE(GATTS_TABLE_TAG, "%s enable bluetooth failed\n", __func__); return; }