feat(bluetooth/controller): adopting new HCI layer code on ESP32-C5

This commit is contained in:
zwl 2024-07-04 21:15:18 +08:00
parent e154fa0af2
commit 113c8923da
5 changed files with 55 additions and 187 deletions

View File

@ -2,15 +2,15 @@
menu "HCI Config" menu "HCI Config"
choice BT_LE_HCI_INTERFACE choice BT_LE_HCI_INTERFACE
prompt "Select HCI interface" prompt "HCI mode"
default BT_LE_HCI_INTERFACE_USE_RAM default BT_LE_HCI_INTERFACE_USE_RAM
config BT_LE_HCI_INTERFACE_USE_RAM config BT_LE_HCI_INTERFACE_USE_RAM
bool "ram" bool "VHCI"
help help
Use RAM as HCI interface Use RAM as HCI interface
config BT_LE_HCI_INTERFACE_USE_UART config BT_LE_HCI_INTERFACE_USE_UART
bool "uart" bool "UART(H4)"
help help
Use UART as HCI interface Use UART as HCI interface
endchoice endchoice
@ -73,12 +73,26 @@ menu "HCI Config"
UART_PARITY_ODD UART_PARITY_ODD
endchoice endchoice
config BT_LE_HCI_UART_TASK_STACK_SIZE config BT_LE_HCI_UART_RX_BUFFER_SIZE
int "HCI uart task stack size" int "The size of rx ring buffer memory"
depends on BT_LE_HCI_INTERFACE_USE_UART depends on !BT_LE_HCI_INTERFACE_USE_RAM
default 1000 default 512
help help
Set the size of uart task stack The size of rx ring buffer memory
config BT_LE_HCI_UART_TX_BUFFER_SIZE
int "The size of tx ring buffer memory"
depends on !BT_LE_HCI_INTERFACE_USE_RAM
default 256
help
The size of tx ring buffer memory
config BT_LE_HCI_TRANS_TASK_STACK_SIZE
int "HCI transport task stack size"
depends on !BT_LE_HCI_INTERFACE_USE_RAM
default 1024
help
This configures stack size of hci transport task
endmenu endmenu
config BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT config BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT

View File

@ -30,7 +30,7 @@
#endif // CONFIG_SW_COEXIST_ENABLE #endif // CONFIG_SW_COEXIST_ENABLE
#include "nimble/nimble_npl_os.h" #include "nimble/nimble_npl_os.h"
#include "ble_hci_trans.h" #include "esp_hci_transport.h"
#include "os/endian.h" #include "os/endian.h"
#include "esp_bt.h" #include "esp_bt.h"
@ -39,7 +39,6 @@
#include "esp_pm.h" #include "esp_pm.h"
#include "esp_phy_init.h" #include "esp_phy_init.h"
#include "esp_private/periph_ctrl.h" #include "esp_private/periph_ctrl.h"
#include "hci_uart.h"
#include "bt_osi_mem.h" #include "bt_osi_mem.h"
#if SOC_PM_RETENTION_HAS_CLOCK_BUG #if SOC_PM_RETENTION_HAS_CLOCK_BUG
@ -50,10 +49,6 @@
#include "esp_private/sleep_modem.h" #include "esp_private/sleep_modem.h"
#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE #endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE
#ifdef CONFIG_BT_BLUEDROID_ENABLED
#include "hci/hci_hal.h"
#endif // CONFIG_BT_BLUEDROID_ENABLED
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
@ -69,16 +64,11 @@
#define OSI_COEX_VERSION 0x00010006 #define OSI_COEX_VERSION 0x00010006
#define OSI_COEX_MAGIC_VALUE 0xFADEBEAD #define OSI_COEX_MAGIC_VALUE 0xFADEBEAD
#define EXT_FUNC_VERSION 0x20221122 #define EXT_FUNC_VERSION 0x20240422
#define EXT_FUNC_MAGIC_VALUE 0xA5A5A5A5 #define EXT_FUNC_MAGIC_VALUE 0xA5A5A5A5
#define BT_ASSERT_PRINT ets_printf #define BT_ASSERT_PRINT ets_printf
#ifdef CONFIG_BT_BLUEDROID_ENABLED
/* ACL_DATA_MBUF_LEADINGSPCAE: The leadingspace in user info header for ACL data */
#define ACL_DATA_MBUF_LEADINGSPCAE 4
#endif // CONFIG_BT_BLUEDROID_ENABLED
/* Types definition /* Types definition
************************************************************************ ************************************************************************
*/ */
@ -97,12 +87,6 @@ struct ext_funcs_t {
int (*_esp_intr_free)(void **ret_handle); int (*_esp_intr_free)(void **ret_handle);
void *(* _malloc)(size_t size); void *(* _malloc)(size_t size);
void (*_free)(void *p); void (*_free)(void *p);
void (*_hal_uart_start_tx)(int);
int (*_hal_uart_init_cbs)(int, hci_uart_tx_char, hci_uart_tx_done, hci_uart_rx_char, void *);
int (*_hal_uart_config)(int, int32_t, uint8_t, uint8_t, uart_parity_t, uart_hw_flowcontrol_t);
int (*_hal_uart_close)(int);
void (*_hal_uart_blocking_tx)(int, uint8_t);
int (*_hal_uart_init)(int, void *);
int (* _task_create)(void *task_func, const char *name, uint32_t stack_depth, void *param, int (* _task_create)(void *task_func, const char *name, uint32_t stack_depth, void *param,
uint32_t prio, void *task_handle, uint32_t core_id); uint32_t prio, void *task_handle, uint32_t core_id);
void (* _task_delete)(void *task_handle); void (* _task_delete)(void *task_handle);
@ -179,16 +163,6 @@ static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status);
static int task_create_wrapper(void *task_func, const char *name, uint32_t stack_depth, static int task_create_wrapper(void *task_func, const char *name, uint32_t stack_depth,
void *param, uint32_t prio, void *task_handle, uint32_t core_id); void *param, uint32_t prio, void *task_handle, uint32_t core_id);
static void task_delete_wrapper(void *task_handle); static void task_delete_wrapper(void *task_handle);
#if CONFIG_BT_LE_HCI_INTERFACE_USE_UART
static void hci_uart_start_tx_wrapper(int uart_no);
static int hci_uart_init_cbs_wrapper(int uart_no, hci_uart_tx_char tx_func,
hci_uart_tx_done tx_done, hci_uart_rx_char rx_func, void *arg);
static int hci_uart_config_wrapper(int uart_no, int32_t speed, uint8_t databits, uint8_t stopbits,
uart_parity_t parity, uart_hw_flowcontrol_t flow_ctl);
static int hci_uart_close_wrapper(int uart_no);
static void hci_uart_blocking_tx_wrapper(int port, uint8_t data);
static int hci_uart_init_wrapper(int uart_no, void *cfg);
#endif // CONFIG_BT_LE_HCI_INTERFACE_USE_UART
static int esp_intr_alloc_wrapper(int source, int flags, intr_handler_t handler, static int esp_intr_alloc_wrapper(int source, int flags, intr_handler_t handler,
void *arg, void **ret_handle_in); void *arg, void **ret_handle_in);
static int esp_intr_free_wrapper(void **ret_handle); static int esp_intr_free_wrapper(void **ret_handle);
@ -206,7 +180,6 @@ static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, b
*/ */
/* Static variable declare */ /* Static variable declare */
static DRAM_ATTR esp_bt_controller_status_t ble_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE; static DRAM_ATTR esp_bt_controller_status_t ble_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED #if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
const static uint32_t log_bufs_size[] = {CONFIG_BT_LE_LOG_CTRL_BUF1_SIZE, CONFIG_BT_LE_LOG_HCI_BUF_SIZE, CONFIG_BT_LE_LOG_CTRL_BUF2_SIZE}; const static uint32_t log_bufs_size[] = {CONFIG_BT_LE_LOG_CTRL_BUF1_SIZE, CONFIG_BT_LE_LOG_HCI_BUF_SIZE, CONFIG_BT_LE_LOG_CTRL_BUF2_SIZE};
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED #endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
@ -236,14 +209,6 @@ struct ext_funcs_t ext_funcs_ro = {
._esp_intr_free = esp_intr_free_wrapper, ._esp_intr_free = esp_intr_free_wrapper,
._malloc = bt_osi_mem_malloc_internal, ._malloc = bt_osi_mem_malloc_internal,
._free = bt_osi_mem_free, ._free = bt_osi_mem_free,
#if CONFIG_BT_LE_HCI_INTERFACE_USE_UART
._hal_uart_start_tx = hci_uart_start_tx_wrapper,
._hal_uart_init_cbs = hci_uart_init_cbs_wrapper,
._hal_uart_config = hci_uart_config_wrapper,
._hal_uart_close = hci_uart_close_wrapper,
._hal_uart_blocking_tx = hci_uart_blocking_tx_wrapper,
._hal_uart_init = hci_uart_init_wrapper,
#endif //CONFIG_BT_LE_HCI_INTERFACE_USE_UART
._task_create = task_create_wrapper, ._task_create = task_create_wrapper,
._task_delete = task_delete_wrapper, ._task_delete = task_delete_wrapper,
._osi_assert = osi_assert_wrapper, ._osi_assert = osi_assert_wrapper,
@ -288,75 +253,6 @@ static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status)
#endif // CONFIG_SW_COEXIST_ENABLE #endif // CONFIG_SW_COEXIST_ENABLE
} }
#ifdef CONFIG_BT_BLUEDROID_ENABLED
bool esp_vhci_host_check_send_available(void)
{
if (ble_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
return false;
}
return true;
}
static struct os_mbuf *ble_hs_mbuf_gen_pkt(uint16_t leading_space)
{
struct os_mbuf *om;
int rc;
om = os_msys_get_pkthdr(0, 0);
if (om == NULL) {
return NULL;
}
if (om->om_omp->omp_databuf_len < leading_space) {
rc = os_mbuf_free_chain(om);
assert(rc == 0);
return NULL;
}
om->om_data += leading_space;
return om;
}
struct os_mbuf *ble_hs_mbuf_acl_pkt(void)
{
return ble_hs_mbuf_gen_pkt(4 + 1);
}
void esp_vhci_host_send_packet(uint8_t *data, uint16_t len)
{
if (ble_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
return;
}
if (*(data) == DATA_TYPE_COMMAND) {
struct ble_hci_cmd *cmd = NULL;
cmd = (struct ble_hci_cmd *) ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_CMD);
assert(cmd);
memcpy((uint8_t *)cmd, data + 1, len - 1);
ble_hci_trans_hs_cmd_tx((uint8_t *)cmd);
}
if (*(data) == DATA_TYPE_ACL) {
struct os_mbuf *om = os_msys_get_pkthdr(len, ACL_DATA_MBUF_LEADINGSPCAE);
assert(om);
assert(os_mbuf_append(om, &data[1], len - 1) == 0);
ble_hci_trans_hs_acl_tx(om);
}
}
esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback)
{
if (ble_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) {
return ESP_FAIL;
}
ble_hci_trans_cfg_hs(ble_hs_hci_rx_evt, NULL, ble_hs_rx_data, NULL);
return ESP_OK;
}
#endif // CONFIG_BT_BLUEDROID_ENABLED
static int task_create_wrapper(void *task_func, const char *name, uint32_t stack_depth, static int task_create_wrapper(void *task_func, const char *name, uint32_t stack_depth,
void *param, uint32_t prio, void *task_handle, uint32_t core_id) void *param, uint32_t prio, void *task_handle, uint32_t core_id)
{ {
@ -388,56 +284,6 @@ static int esp_ecc_gen_dh_key(const uint8_t *peer_pub_key_x, const uint8_t *peer
return rc; return rc;
} }
#ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART
static void hci_uart_start_tx_wrapper(int uart_no)
{
hci_uart_start_tx(uart_no);
}
static int hci_uart_init_cbs_wrapper(int uart_no, hci_uart_tx_char tx_func,
hci_uart_tx_done tx_done, hci_uart_rx_char rx_func, void *arg)
{
int rc = -1;
rc = hci_uart_init_cbs(uart_no, tx_func, tx_done, rx_func, arg);
return rc;
}
static int hci_uart_config_wrapper(int port_num, int32_t baud_rate, uint8_t data_bits,
uint8_t stop_bits, uart_parity_t parity,
uart_hw_flowcontrol_t flow_ctl)
{
int rc = -1;
rc = hci_uart_config(port_num, baud_rate, data_bits, stop_bits, parity, flow_ctl);
return rc;
}
static int hci_uart_close_wrapper(int uart_no)
{
int rc = -1;
rc = hci_uart_close(uart_no);
return rc;
}
static void hci_uart_blocking_tx_wrapper(int port, uint8_t data)
{
//This function is nowhere to use.
}
static int hci_uart_init_wrapper(int uart_no, void *cfg)
{
//This function is nowhere to use.
return 0;
}
#endif //CONFIG_BT_LE_HCI_INTERFACE_USE_UART
static int ble_hci_unregistered_hook(void*, void*)
{
ESP_LOGD(NIMBLE_PORT_LOG_TAG,"%s ble hci rx_evt is not registered.",__func__);
return 0;
}
static int esp_intr_alloc_wrapper(int source, int flags, intr_handler_t handler, static int esp_intr_alloc_wrapper(int source, int flags, intr_handler_t handler,
void *arg, void **ret_handle_in) void *arg, void **ret_handle_in)
{ {
@ -694,6 +540,7 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
ble_npl_count_info_t npl_info; ble_npl_count_info_t npl_info;
uint32_t slow_clk_freq = 0; uint32_t slow_clk_freq = 0;
uint8_t hci_transport_mode;
memset(&npl_info, 0, sizeof(ble_npl_count_info_t)); memset(&npl_info, 0, sizeof(ble_npl_count_info_t));
@ -838,11 +685,20 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
ble_controller_status = ESP_BT_CONTROLLER_STATUS_INITED; ble_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
ble_hci_trans_cfg_hs((ble_hci_trans_rx_cmd_fn *)ble_hci_unregistered_hook,NULL, #if CONFIG_BT_LE_HCI_INTERFACE_USE_RAM
(ble_hci_trans_rx_acl_fn *)ble_hci_unregistered_hook,NULL); hci_transport_mode = HCI_TRANSPORT_VHCI;
return ESP_OK; #elif CONFIG_BT_LE_HCI_INTERFACE_USE_UART
hci_transport_mode = HCI_TRANSPORT_UART_NO_DMA;
#endif // CONFIG_BT_LE_HCI_INTERFACE_USE_RAM
ret = hci_transport_init(hci_transport_mode);
if (ret) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "hci transport init failed %d", ret);
goto free_controller;
}
return ESP_OK;
free_controller: free_controller:
hci_transport_deinit();
controller_sleep_deinit(); controller_sleep_deinit();
os_msys_deinit(); os_msys_deinit();
r_ble_controller_deinit(); r_ble_controller_deinit();
@ -873,6 +729,7 @@ esp_err_t esp_bt_controller_deinit(void)
return ESP_FAIL; return ESP_FAIL;
} }
hci_transport_deinit();
controller_sleep_deinit(); controller_sleep_deinit();
os_msys_deinit(); os_msys_deinit();

View File

@ -129,6 +129,21 @@ extern "C" {
#else #else
#define DEFAULT_BT_LE_50_FEATURE_SUPPORT (0) #define DEFAULT_BT_LE_50_FEATURE_SUPPORT (0)
#endif #endif
#if defined (CONFIG_BT_LE_HCI_UART_FLOWCTRL)
#define DEFAULT_BT_LE_HCI_UART_FLOW_CTRL (CONFIG_BT_LE_HCI_UART_FLOWCTRL)
#if DEFAULT_BT_LE_HCI_UART_FLOW_CTRL
#define DEFAULT_BT_LE_HCI_UART_CTS_PIN (CONFIG_BT_LE_HCI_UART_CTS_PIN)
#define DEFAULT_BT_LE_HCI_UART_RTS_PIN (CONFIG_BT_LE_HCI_UART_RTS_PIN)
#else
#define DEFAULT_BT_LE_HCI_UART_CTS_PIN (-1)
#define DEFAULT_BT_LE_HCI_UART_RTS_PIN (-1)
#endif
#else
#define DEFAULT_BT_LE_HCI_UART_FLOW_CTRL (0)
#define DEFAULT_BT_LE_HCI_UART_CTS_PIN (-1)
#define DEFAULT_BT_LE_HCI_UART_RTS_PIN (-1)
#endif
#endif #endif
#define DEFAULT_BT_LE_COEX_PHY_CODED_TX_RX_TLIM_EFF CONFIG_BT_LE_COEX_PHY_CODED_TX_RX_TLIM_EFF #define DEFAULT_BT_LE_COEX_PHY_CODED_TX_RX_TLIM_EFF CONFIG_BT_LE_COEX_PHY_CODED_TX_RX_TLIM_EFF
@ -169,8 +184,6 @@ extern "C" {
#define DEFAULT_BT_LE_HCI_UART_DATA_BITS (UART_DATA_8_BITS) #define DEFAULT_BT_LE_HCI_UART_DATA_BITS (UART_DATA_8_BITS)
#define DEFAULT_BT_LE_HCI_UART_STOP_BITS (UART_STOP_BITS_1) #define DEFAULT_BT_LE_HCI_UART_STOP_BITS (UART_STOP_BITS_1)
#define DEFAULT_BT_LE_HCI_UART_PARITY (0) #define DEFAULT_BT_LE_HCI_UART_PARITY (0)
#define DEFAULT_BT_LE_HCI_UART_TASK_STACK_SIZE (CONFIG_BT_LE_HCI_UART_TASK_STACK_SIZE)
#define DEFAULT_BT_LE_HCI_UART_FLOW_CTRL (0)
#else #else
#define DEFAULT_BT_LE_HCI_UART_TX_PIN (0) #define DEFAULT_BT_LE_HCI_UART_TX_PIN (0)
#define DEFAULT_BT_LE_HCI_UART_RX_PIN (0) #define DEFAULT_BT_LE_HCI_UART_RX_PIN (0)
@ -179,8 +192,6 @@ extern "C" {
#define DEFAULT_BT_LE_HCI_UART_DATA_BITS (0) #define DEFAULT_BT_LE_HCI_UART_DATA_BITS (0)
#define DEFAULT_BT_LE_HCI_UART_STOP_BITS (0) #define DEFAULT_BT_LE_HCI_UART_STOP_BITS (0)
#define DEFAULT_BT_LE_HCI_UART_PARITY (0) #define DEFAULT_BT_LE_HCI_UART_PARITY (0)
#define DEFAULT_BT_LE_HCI_UART_TASK_STACK_SIZE (0)
#define DEFAULT_BT_LE_HCI_UART_FLOW_CTRL (0)
#endif #endif
/* Unchanged configuration */ /* Unchanged configuration */

@ -1 +1 @@
Subproject commit 2e05c001042650bca426b672febd23c9ff45754e Subproject commit 77c67a4d06c95113a2d1766892cde59f5af744af

View File

@ -155,7 +155,7 @@ esp_err_t esp_ble_tx_power_set_enhanced(esp_ble_enhanced_power_type_t power_type
*/ */
esp_power_level_t esp_ble_tx_power_get_enhanced(esp_ble_enhanced_power_type_t power_type, uint16_t handle); esp_power_level_t esp_ble_tx_power_get_enhanced(esp_ble_enhanced_power_type_t power_type, uint16_t handle);
#define CONFIG_VERSION 0x20231124 #define CONFIG_VERSION 0x20240422
#define CONFIG_MAGIC 0x5A5AA5A5 #define CONFIG_MAGIC 0x5A5AA5A5
/** /**
@ -196,13 +196,6 @@ typedef struct {
uint8_t controller_run_cpu; /*!< CPU core on which the controller runs */ uint8_t controller_run_cpu; /*!< CPU core on which the controller runs */
uint8_t enable_qa_test; /*!< Enable quality assurance (QA) testing */ uint8_t enable_qa_test; /*!< Enable quality assurance (QA) testing */
uint8_t enable_bqb_test; /*!< Enable Bluetooth Qualification Test (BQB) testing */ uint8_t enable_bqb_test; /*!< Enable Bluetooth Qualification Test (BQB) testing */
uint8_t enable_uart_hci; /*!< Enable UART HCI (Host Controller Interface) */
uint8_t ble_hci_uart_port; /*!< UART port number for Bluetooth HCI */
uint32_t ble_hci_uart_baud; /*!< Baud rate for Bluetooth HCI UART */
uint8_t ble_hci_uart_data_bits; /*!< Number of data bits for Bluetooth HCI UART */
uint8_t ble_hci_uart_stop_bits; /*!< Number of stop bits for Bluetooth HCI UART */
uint8_t ble_hci_uart_flow_ctrl; /*!< Flow control settings for Bluetooth HCI UART */
uint8_t ble_hci_uart_uart_parity; /*!< Parity settings for Bluetooth HCI UART */
uint8_t enable_tx_cca; /*!< Enable Transmit Clear Channel Assessment (TX CCA) */ uint8_t enable_tx_cca; /*!< Enable Transmit Clear Channel Assessment (TX CCA) */
uint8_t cca_rssi_thresh; /*!< RSSI threshold for Transmit Clear Channel Assessment (CCA) */ uint8_t cca_rssi_thresh; /*!< RSSI threshold for Transmit Clear Channel Assessment (CCA) */
uint8_t sleep_en; /*!< Enable sleep mode */ uint8_t sleep_en; /*!< Enable sleep mode */
@ -252,13 +245,6 @@ typedef struct {
.controller_run_cpu = 0, \ .controller_run_cpu = 0, \
.enable_qa_test = RUN_QA_TEST, \ .enable_qa_test = RUN_QA_TEST, \
.enable_bqb_test = RUN_BQB_TEST, \ .enable_bqb_test = RUN_BQB_TEST, \
.enable_uart_hci = HCI_UART_EN, \
.ble_hci_uart_port = DEFAULT_BT_LE_HCI_UART_PORT, \
.ble_hci_uart_baud = DEFAULT_BT_LE_HCI_UART_BAUD, \
.ble_hci_uart_data_bits = DEFAULT_BT_LE_HCI_UART_DATA_BITS, \
.ble_hci_uart_stop_bits = DEFAULT_BT_LE_HCI_UART_STOP_BITS, \
.ble_hci_uart_flow_ctrl = DEFAULT_BT_LE_HCI_UART_FLOW_CTRL, \
.ble_hci_uart_uart_parity = DEFAULT_BT_LE_HCI_UART_PARITY, \
.enable_tx_cca = DEFAULT_BT_LE_TX_CCA_ENABLED, \ .enable_tx_cca = DEFAULT_BT_LE_TX_CCA_ENABLED, \
.cca_rssi_thresh = 256 - DEFAULT_BT_LE_CCA_RSSI_THRESH, \ .cca_rssi_thresh = 256 - DEFAULT_BT_LE_CCA_RSSI_THRESH, \
.sleep_en = NIMBLE_SLEEP_ENABLE, \ .sleep_en = NIMBLE_SLEEP_ENABLE, \