Просмотр исходного кода

component/bt : modify bluetooth config style

Tian Hao 8 лет назад
Родитель
Сommit
d90a35af19

+ 15 - 8
components/bt/Kconfig

@@ -29,10 +29,11 @@ config BT_DRAM_RELEASE
     depends on BT_ENABLED
     default n
     help
-	This option should only be used when BLE only.
-	Open this option will release about 30K DRAM from Classic BT.
-	The released DRAM will be used as system heap memory.
+        This option should only be used when BLE only.
+        Open this option will release about 30K DRAM from Classic BT.
+        The released DRAM will be used as system heap memory.
 
+#disable now for app cpu due to a known issue
 config BTDM_CONTROLLER_RUN_APP_CPU
     bool "Run controller on APP CPU"
     depends on BT_ENABLED && !FREERTOS_UNICORE && 0
@@ -40,7 +41,13 @@ config BTDM_CONTROLLER_RUN_APP_CPU
     help
         Run controller on APP CPU.
 
-menuconfig HCI_UART
+config BTDM_CONTROLLER_RUN_CPU
+    int
+    depends on BT_ENABLED
+    default 1 if BTDM_CONTROLLER_RUN_APP_CPU
+    default 0
+
+menuconfig BT_HCI_UART
     bool "HCI use UART as IO"
     depends on BT_ENABLED
     default n
@@ -48,17 +55,17 @@ menuconfig HCI_UART
         Default HCI use VHCI, if this option choose, HCI will use UART(0/1/2) as IO.
         Besides, it can set uart number and uart baudrate.
 
-config HCI_UART_NO
+config BT_HCI_UART_NO
     int "UART Number for HCI"
-    depends on HCI_UART
+    depends on BT_HCI_UART
     range 1 2
     default 1
     help
         Uart number for HCI.
 
-config HCI_UART_BAUDRATE
+config BT_HCI_UART_BAUDRATE
     int "UART Baudrate for HCI"
-    depends on HCI_UART
+    depends on BT_HCI_UART
     range 115200 921600
     default 921600
     help

+ 26 - 35
components/bt/bt.c

@@ -15,6 +15,7 @@
 #include <stddef.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
@@ -39,15 +40,9 @@
 #define BTDM_CFG_CONTROLLER_RUN_APP_CPU     (1<<3)
 /* Other reserved for future */
 
-/* Controller config options, depend on config mask */
-struct btdm_config_options {
-    uint8_t hci_uart_no;
-    uint32_t hci_uart_baudrate;
-};
-
 /* 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(uint32_t config_mask, struct btdm_config_options *opts);
+extern void btdm_controller_init(uint32_t config_mask, esp_bt_controller_config_t *config_opts);
 extern void btdm_controller_schedule(void);
 extern void btdm_controller_deinit(void);
 extern int btdm_controller_enable(esp_bt_mode_t mode);
@@ -94,7 +89,7 @@ struct osi_funcs_t {
 /* Static variable declare */
 static bool btdm_bb_init_flag = false;
 static esp_bt_controller_status_t btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
-
+static esp_bt_controller_config_t btdm_cfg_opts;
 static xTaskHandle btControllerTaskHandle;
 
 static portMUX_TYPE global_int_mux = portMUX_INITIALIZER_UNLOCKED;
@@ -193,7 +188,7 @@ static uint32_t btdm_config_mask_load(void)
 #ifdef CONFIG_BT_DRAM_RELEASE
     mask |= (BTDM_CFG_BT_EM_RELEASE | BTDM_CFG_BT_DATA_RELEASE);
 #endif
-#ifdef CONFIG_HCI_UART
+#ifdef CONFIG_BT_HCI_UART
     mask |= BTDM_CFG_HCI_UART;
 #endif
 #ifdef CONFIG_BTDM_CONTROLLER_RUN_APP_CPU
@@ -202,54 +197,50 @@ static uint32_t btdm_config_mask_load(void)
     return mask;
 }
 
-static void btdm_config_opts_load(struct btdm_config_options *opts)
-{
-    if (opts == NULL) {
-        return;
-    }
-#ifdef CONFIG_HCI_UART
-    opts->hci_uart_no = CONFIG_HCI_UART_NO;
-    opts->hci_uart_baudrate = CONFIG_HCI_UART_BAUDRATE;
-#endif
-}
-
 static void bt_controller_task(void *pvParam)
 {
-    struct btdm_config_options btdm_cfg_opts;
     uint32_t btdm_cfg_mask = 0;
 
-    btdm_osi_funcs_register(&osi_funcs);
-
     btdm_cfg_mask = btdm_config_mask_load();
-    btdm_config_opts_load(&btdm_cfg_opts);
+
+    btdm_osi_funcs_register(&osi_funcs);
 
     btdm_controller_init(btdm_cfg_mask, &btdm_cfg_opts);
 
     btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
-
     /* Loop */
     btdm_controller_schedule();
 }
 
-void esp_bt_controller_init()
+esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
 {
+    BaseType_t ret;
+
     if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) {
-        return;
+        return ESP_ERR_INVALID_STATE;
     }
 
-#ifdef CONFIG_BTDM_CONTROLLER_RUN_APP_CPU
-    xTaskCreatePinnedToCore(bt_controller_task, "btController",
-                            ESP_TASK_BT_CONTROLLER_STACK, NULL,
-                            ESP_TASK_BT_CONTROLLER_PRIO, &btControllerTaskHandle, 1);
-#else
-    xTaskCreatePinnedToCore(bt_controller_task, "btController",
+    if (cfg == NULL) {
+        return ESP_ERR_INVALID_ARG;
+    }
+
+    memcpy(&btdm_cfg_opts, cfg, sizeof(esp_bt_controller_config_t));
+
+    ret = xTaskCreatePinnedToCore(bt_controller_task, "btController",
                             ESP_TASK_BT_CONTROLLER_STACK, NULL,
-                            ESP_TASK_BT_CONTROLLER_PRIO, &btControllerTaskHandle, 0);
-#endif
+                            ESP_TASK_BT_CONTROLLER_PRIO, &btControllerTaskHandle, CONFIG_BTDM_CONTROLLER_RUN_CPU);
+
+    if (ret != pdPASS) {
+        memset(&btdm_cfg_opts, 0x0, sizeof(esp_bt_controller_config_t));
+        return ESP_ERR_NO_MEM;
+    }
+
+    return ESP_OK;
 }
 
 void esp_bt_controller_deinit(void)
 {
+    memset(&btdm_cfg_opts, 0x0, sizeof(esp_bt_controller_config_t));
     vTaskDelete(btControllerTaskHandle);
     btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
 }

+ 36 - 2
components/bt/include/bt.h

@@ -18,11 +18,44 @@
 #include <stdint.h>
 #include <stdbool.h>
 #include "esp_err.h"
+#include "sdkconfig.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+/**
+ * @brief Controller config options, depend on config mask.
+ *        Config mask indicate which functions enabled, this means 
+ *        some options or parameters of some functions enabled by config mask.
+ */
+typedef struct {
+    uint8_t hci_uart_no;            /*!< If use UART1/2 as HCI IO interface, indicate UART number */
+    uint32_t hci_uart_baudrate;     /*!< If use UART1/2 as HCI IO interface, indicate UART baudrate */
+} esp_bt_controller_config_t;
+
+#ifdef CONFIG_BT_ENABLED
+
+#ifdef CONFIG_BT_HCI_UART_NO
+#define BT_HCI_UART_NO_DEFAULT CONFIG_BT_HCI_UART_NO
+#else
+#define BT_HCI_UART_NO_DEFAULT 1
+#endif /* BT_HCI_UART_NO_DEFAULT */
+
+#ifdef CONFIG_BT_HCI_UART_BAUDRATE
+#define BT_HCI_UART_BAUDRATE_DEFAULT CONFIG_BT_HCI_UART_BAUDRATE
+#else
+#define BT_HCI_UART_BAUDRATE_DEFAULT 921600
+#endif /* BT_HCI_UART_BAUDRATE_DEFAULT */
+
+#define BT_CONTROLLER_INIT_CONFIG_DEFAULT() { \
+    .hci_uart_no = BT_HCI_UART_NO_DEFAULT,\
+    .hci_uart_baudrate = BT_HCI_UART_BAUDRATE_DEFAULT,\
+};
+#else
+#define BT_CONTROLLER_INIT_CONFIG_DEFAULT() {0}; _Static_assert(0, "please enable bluetooth in menuconfig to use bt.h");
+#endif
+
 /**
  * @brief Bluetooth mode for controller enable/disable
  */
@@ -45,10 +78,11 @@ typedef enum {
 
 /**
  * @brief  Initialize BT controller to allocate task and other resource.
- *
+ * @param  cfg: Initial configuration of BT controller.
  * This function should be called only once, before any other BT functions are called.
+ * @return       ESP_OK - success, other - failed
  */
-void esp_bt_controller_init(void);
+esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg);
 
 /**
  * @brief  De-initialize BT controller to free resource and delete task.

+ 3 - 0
docs/api/bluetooth/controller_vhci.rst

@@ -38,6 +38,9 @@ Enumerations
 Structures
 ^^^^^^^^^^
 
+.. doxygenstruct:: esp_bt_controller_config_t
+    :members:
+
 .. doxygenstruct:: esp_vhci_host_callback
     :members:
 

+ 11 - 2
examples/bluetooth/ble_adv/main/app_bt.c

@@ -13,10 +13,13 @@
 // limitations under the License.
 
 #include <stdio.h>
+#include <string.h>
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
 #include "bt.h"
-#include <string.h>
+#include "esp_log.h"
+
+static const char *tag = "BLE_ADV";
 
 #define HCI_H4_CMD_PREAMBLE_SIZE           (4)
 
@@ -214,9 +217,15 @@ void bleAdvtTask(void *pvParameters)
 
 void app_main()
 {
-    esp_bt_controller_init();
+    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
+    
+    if (esp_bt_controller_init(&bt_cfg) != ESP_OK) {
+        ESP_LOGI(tag, "Bluetooth controller initialize failed");
+        return;
+    }
 
     if (esp_bt_controller_enable(ESP_BT_MODE_BTDM) != ESP_OK) {
+        ESP_LOGI(tag, "Bluetooth controller enable failed");
         return;
     }
 

+ 5 - 1
examples/bluetooth/blufi/main/blufi_example_main.c

@@ -315,7 +315,11 @@ void app_main()
     ESP_ERROR_CHECK( nvs_flash_init() );
     initialise_wifi();
 
-    esp_bt_controller_init();
+    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
+    ret = esp_bt_controller_init(&bt_cfg);
+    if (ret) {
+        BLUFI_ERROR("%s initialize bt controller failed\n", __func__);
+    }
 
     ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
     if (ret) {

+ 15 - 6
examples/bluetooth/controller_hci_uart/main/main.c → examples/bluetooth/controller_hci_uart/main/controller_hci_uart_demo.c

@@ -23,21 +23,30 @@ static const char *tag = "CONTROLLER_UART_HCI";
 
 static void uart_gpio_reset(void)
 {
-    ESP_LOGI(tag, "HCI UART Pin select: TX 5, RX, 18, CTS 23, RTS 19\n");
+#if CONFIG_BT_HCI_UART_NO
+    ESP_LOGI(tag, "HCI UART%d Pin select: TX 5, RX, 18, CTS 23, RTS 19", CONFIG_BT_HCI_UART_NO);
 
-#if CONFIG_HCI_UART_NO
-    uart_set_pin(CONFIG_HCI_UART_NO, 5, 18, 19, 23);
+    uart_set_pin(CONFIG_BT_HCI_UART_NO, 5, 18, 19, 23);
 #endif
 }
 
 void app_main()
 { 
+    esp_err_t ret;
+
     /* As the UART1/2 pin conflict with flash pin, so do matrix of the signal and pin */
     uart_gpio_reset();
- 
-    esp_bt_controller_init();
 
-    if (esp_bt_controller_enable(ESP_BT_MODE_BTDM) != ESP_OK) {
+    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
+    ret = esp_bt_controller_init(&bt_cfg);
+    if (ret != ESP_OK) {
+        ESP_LOGE(tag, "Bluetooth Controller initialize failed, ret %d", ret);
+        return;
+    }
+
+    ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
+    if (ret != ESP_OK) {
+        ESP_LOGE(tag, "Bluetooth Controller initialize failed, ret %d", ret);
         return;
     }
 }

+ 3 - 3
examples/bluetooth/controller_hci_uart/sdkconfig.defaults

@@ -5,6 +5,6 @@
 # BT config
 #
 CONFIG_BT_ENABLED=y
-CONFIG_HCI_UART=y
-CONFIG_HCI_UART_NO=1
-CONFIG_HCI_UART_BAUDRATE=921600
+CONFIG_BT_HCI_UART=y
+CONFIG_BT_HCI_UART_NO_DEFAULT=1
+CONFIG_BT_HCI_UART_BAUDRATE_DEFAULT=921600

+ 2 - 1
examples/bluetooth/gatt_client/main/gattc_demo.c

@@ -402,7 +402,8 @@ void gattc_client_test(void)
 
 void app_main()
 {
-    esp_bt_controller_init();
+    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
+    esp_bt_controller_init(&bt_cfg);
     esp_bt_controller_enable(ESP_BT_MODE_BTDM);
 
     gattc_client_test();

+ 6 - 1
examples/bluetooth/gatt_server/main/gatts_demo.c

@@ -398,7 +398,12 @@ void app_main()
 {
     esp_err_t ret;
 
-    esp_bt_controller_init();
+    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
+    ret = esp_bt_controller_init(&bt_cfg);
+    if (ret) {
+        ESP_LOGE(GATTS_TAG, "%s initialize controller failed\n", __func__);
+        return;
+    }
 
     ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
     if (ret) {

+ 6 - 1
examples/bluetooth/gatt_server_service_table/main/gatts_table_creat_demo.c

@@ -313,7 +313,12 @@ void app_main()
 {
     esp_err_t ret;
 
-    esp_bt_controller_init();
+    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
+    ret = esp_bt_controller_init(&bt_cfg);
+    if (ret) {
+        ESP_LOGE(GATTS_TABLE_TAG, "%s enable controller failed\n", __func__);
+        return;
+    }
 
     ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
     if (ret) {