]> granicus.if.org Git - esp-idf/commitdiff
component/bt : add bt enable/disable for power save
authorTian Hao <tianhao@espressif.com>
Fri, 17 Feb 2017 11:24:58 +0000 (19:24 +0800)
committerTian Hao <tianhao@espressif.com>
Fri, 17 Feb 2017 11:24:58 +0000 (19:24 +0800)
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
components/bt/bt.c
components/bt/include/bt.h
components/bt/lib
components/esp32/phy_init.c
docs/api/bluetooth/controller_vhci.rst
examples/bluetooth/ble_adv/main/app_bt.c
examples/bluetooth/blufi/main/blufi_main.c
examples/bluetooth/gatt_client/main/gattc_demo.c
examples/bluetooth/gatt_server/main/gatts_demo.c
examples/bluetooth/gatt_server_service_table/main/gatts_table_creat_demo.c

index 612a9e020a29cfbe7a88238773f1b6fe49d7905c..c9fe4fc060e97a96c99526a9d9ad572825d33835 100644 (file)
@@ -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;
 }
 
index 2390456afb2ae2a6249c058bf1242ce886a58e88..440c960e649a0948373fc58786ee2c4ef5e2e44e 100644 (file)
 /* 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
index 926ecfadcdde5163ad3d2519c5f8a339abf06117..91617a28f4e79a9b4f36f7afcefd3a9047ecc3f6 100644 (file)
 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
  */
index 9c1eea6bb03adc3b3847fff79c3f017652840a46..69616af7653f4de6e3b78f475dc10e73f0a20ece 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 9c1eea6bb03adc3b3847fff79c3f017652840a46
+Subproject commit 69616af7653f4de6e3b78f475dc10e73f0a20ece
index 1c67a404abb1802f2f6824199110b539fbd7c5bc..340bd7ee72d263ebd2a60c37d4d4f06c070ba25b 100644 (file)
@@ -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);
index 35248cbb1e0a9f6ca844fece2deae467cd27a4d6..108a9f678d9d41415813098e0b322f14a81dca04 100644 (file)
@@ -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
index f0780c950c9e83de7c4b0d718a7c875eda960439..67ab2c8fe04ed3e3203bdf11de81b24e125a3d25 100644 (file)
@@ -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);
 }
 
index a95db66eb59df67f2074c2ac2306f7755786b86c..b46ffb46484dba0170e60b747deaf52eadf2284b 100644 (file)
@@ -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__);
index e6ee8672937822b70d614932f2b32cf41074d6b7..4a2fa0051dcdc4a6170cf9c95e3f5c95dd32b510 100644 (file)
@@ -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();
 }
 
index 0afa1630ba045c7ed6f61868c180caae47dfef0a..acbf9195a0090ce6d7d36dbaed618d61171d5b01 100644 (file)
@@ -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__);
index 024362c5b7af85f81dc54a292eebac2c5f4b5c35..71baf1842c7e82bb183ffe4084c4ba410667db46 100644 (file)
@@ -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;
     }