diff --git a/components/bt/Kconfig b/components/bt/Kconfig index 5490ba99b3..5b2cc3e6aa 100644 --- a/components/bt/Kconfig +++ b/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 diff --git a/components/bt/bt.c b/components/bt/bt.c index f1676107c5..34b29b9c0c 100644 --- a/components/bt/bt.c +++ b/components/bt/bt.c @@ -15,6 +15,7 @@ #include #include #include +#include #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", + 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, 1); -#else - 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; } diff --git a/components/bt/include/bt.h b/components/bt/include/bt.h index e3bd7f0840..5a244e0c80 100644 --- a/components/bt/include/bt.h +++ b/components/bt/include/bt.h @@ -18,11 +18,44 @@ #include #include #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. diff --git a/docs/api/bluetooth/controller_vhci.rst b/docs/api/bluetooth/controller_vhci.rst index 9fc5910c25..6241adfbd1 100644 --- a/docs/api/bluetooth/controller_vhci.rst +++ b/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: diff --git a/examples/bluetooth/ble_adv/main/app_bt.c b/examples/bluetooth/ble_adv/main/app_bt.c index 67ab2c8fe0..31e8f2fa00 100644 --- a/examples/bluetooth/ble_adv/main/app_bt.c +++ b/examples/bluetooth/ble_adv/main/app_bt.c @@ -13,10 +13,13 @@ // limitations under the License. #include +#include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "bt.h" -#include +#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; } diff --git a/examples/bluetooth/blufi/main/blufi_example_main.c b/examples/bluetooth/blufi/main/blufi_example_main.c index 05f39c4859..2a3f2ce629 100644 --- a/examples/bluetooth/blufi/main/blufi_example_main.c +++ b/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) { diff --git a/examples/bluetooth/controller_hci_uart/main/main.c b/examples/bluetooth/controller_hci_uart/main/controller_hci_uart_demo.c similarity index 61% rename from examples/bluetooth/controller_hci_uart/main/main.c rename to examples/bluetooth/controller_hci_uart/main/controller_hci_uart_demo.c index 3f6de6f475..6270596d92 100644 --- a/examples/bluetooth/controller_hci_uart/main/main.c +++ b/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; } } diff --git a/examples/bluetooth/controller_hci_uart/sdkconfig.defaults b/examples/bluetooth/controller_hci_uart/sdkconfig.defaults index 4e1001ba4a..32a86537f6 100644 --- a/examples/bluetooth/controller_hci_uart/sdkconfig.defaults +++ b/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 diff --git a/examples/bluetooth/gatt_client/main/gattc_demo.c b/examples/bluetooth/gatt_client/main/gattc_demo.c index 2904484913..9985ccdc3b 100644 --- a/examples/bluetooth/gatt_client/main/gattc_demo.c +++ b/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(); diff --git a/examples/bluetooth/gatt_server/main/gatts_demo.c b/examples/bluetooth/gatt_server/main/gatts_demo.c index 39d33286a0..934078aa73 100644 --- a/examples/bluetooth/gatt_server/main/gatts_demo.c +++ b/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) { 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 9bb03e7e89..291d9f5941 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 @@ -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) {