From 85eb5d4374cb47fd8a3dcfcc314743471b88b851 Mon Sep 17 00:00:00 2001 From: Tian Hao Date: Fri, 20 Oct 2017 17:09:03 +0800 Subject: [PATCH] component/bt : fix bug of sw interrupt cause to run btdm to different cpu core. 1. add sw interrupt cause osi to controller. 2. modify the kconfig to improve the option view. 3. add option of the cpu core which bluedroid run. 4. add option of the cpu core which bluetooth controller run. --- components/bt/Kconfig | 134 ++++++++++++------ components/bt/bluedroid/btc/core/btc_task.c | 4 +- .../btc/profile/std/a2dp/btc_media_task.c | 10 +- components/bt/bluedroid/hci/hci_hal_h4.c | 4 +- components/bt/bluedroid/hci/hci_layer.c | 4 +- components/bt/bluedroid/osi/include/thread.h | 21 ++- components/bt/bluedroid/stack/btu/btu_init.c | 4 +- components/bt/bt.c | 28 +++- components/bt/lib | 2 +- 9 files changed, 145 insertions(+), 66 deletions(-) diff --git a/components/bt/Kconfig b/components/bt/Kconfig index 174b2deef..8a4f5248f 100644 --- a/components/bt/Kconfig +++ b/components/bt/Kconfig @@ -1,14 +1,96 @@ -menuconfig BT_ENABLED +menu Bluetooth + + +config BT_ENABLED bool "Bluetooth" help Select this option to enable Bluetooth and show the submenu with Bluetooth configuration choices. -menuconfig BLUEDROID_ENABLED - bool "Bluedroid Bluetooth stack enabled" +choice BTDM_CONTROLLER_PINNED_TO_CORE_CHOICE + prompt "The cpu core which bluetooth controller run" + depends on BT_ENABLED && !FREERTOS_UNICORE + help + Specify the cpu core to run bluetooth controller. + Can not specify no-affinity. + +config BTDM_CONTROLLER_PINNED_TO_CORE_0 + bool "Core 0 (PRO CPU)" +config BTDM_CONTROLLER_PINNED_TO_CORE_1 + bool "Core 1 (APP CPU)" + depends on !FREERTOS_UNICORE +endchoice + +config BTDM_CONTROLLER_PINNED_TO_CORE + int + default 0 if BTDM_CONTROLLER_PINNED_TO_CORE_0 + default 1 if BTDM_CONTROLLER_PINNED_TO_CORE_1 + default 0 + +choice BTDM_CONTROLLER_HCI_MODE_CHOICE + prompt "HCI mode" depends on BT_ENABLED - default y - help - This enables the default Bluedroid Bluetooth stack + help + Speicify HCI mode as VHCI or UART(H4) + +config BTDM_CONTROLLER_HCI_MODE_VHCI + bool "VHCI" + help + Normal option. Mostly, choose this VHCI when bluetooth host run on ESP32, too. + +config BTDM_CONTROLLER_HCI_MODE_UART_H4 + bool "UART(H4)" + help + If use external bluetooth host which run on other hardware and use UART as the HCI interface, + choose this option. +endchoice + +menu "HCI UART(H4) Options" + visible if BTDM_CONTROLLER_HCI_MODE_UART_H4 + +config BT_HCI_UART_NO + int "UART Number for HCI" + depends on BTDM_CONTROLLER_HCI_MODE_UART_H4 + range 1 2 + default 1 + help + Uart number for HCI. The available uart is UART1 and UART2. + +config BT_HCI_UART_BAUDRATE + int "UART Baudrate for HCI" + depends on BTDM_CONTROLLER_HCI_MODE_UART_H4 + range 115200 921600 + default 921600 + help + UART Baudrate for HCI. Please use standard baudrate. +endmenu + +menuconfig BLUEDROID_ENABLED + bool "Bluedroid Enable" + depends on BTDM_CONTROLLER_HCI_MODE_VHCI + default y + help + This enables the default Bluedroid Bluetooth stack + +choice BLUEDROID_PINNED_TO_CORE_CHOICE + prompt "The cpu core which Bluedroid run" + depends on BLUEDROID_ENABLED && !FREERTOS_UNICORE + help + Which the cpu core to run Bluedroid. Can choose core0 and core1. + Can not specify no-affinity. + +config BLUEDROID_PINNED_TO_CORE_0 + bool "Core 0 (PRO CPU)" +config BLUEDROID_PINNED_TO_CORE_1 + bool "Core 1 (APP CPU)" + depends on !FREERTOS_UNICORE +endchoice + +config BLUEDROID_PINNED_TO_CORE + int + depends on BLUEDROID_ENABLED + default 0 if BLUEDROID_PINNED_TO_CORE_0 + default 1 if BLUEDROID_PINNED_TO_CORE_1 + default 0 config BTC_TASK_STACK_SIZE int "Bluetooth event (callback to application) task stack size" @@ -67,44 +149,6 @@ config BT_ACL_CONNECTIONS help Maximum BT/BLE connection count -#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 - default n - help - Run controller on APP CPU. - -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 && !BLUEDROID_ENABLED - default n - help - 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 BT_HCI_UART_NO - int "UART Number for HCI" - depends on BT_HCI_UART - range 1 2 - default 1 - help - Uart number for HCI. - -config BT_HCI_UART_BAUDRATE - int "UART Baudrate for HCI" - depends on BT_HCI_UART - range 115200 921600 - default 921600 - help - UART Baudrate for HCI. Please use standard baudrate. - config SMP_ENABLE bool depends on BLUEDROID_ENABLED @@ -115,3 +159,5 @@ config BT_RESERVE_DRAM hex default 0x10000 if BT_ENABLED default 0 + +endmenu diff --git a/components/bt/bluedroid/btc/core/btc_task.c b/components/bt/bluedroid/btc/core/btc_task.c index 52cbf6dcf..bed68bbb7 100644 --- a/components/bt/bluedroid/btc/core/btc_task.c +++ b/components/bt/bluedroid/btc/core/btc_task.c @@ -139,8 +139,8 @@ bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg int btc_init(void) { - xBtcQueue = xQueueCreate(BTC_TASK_QUEUE_NUM, sizeof(btc_msg_t)); - xTaskCreatePinnedToCore(btc_task, "Btc_task", BTC_TASK_STACK_SIZE, NULL, BTC_TASK_PRIO, &xBtcTaskHandle, 0); + xBtcQueue = xQueueCreate(BTC_TASK_QUEUE_LEN, sizeof(btc_msg_t)); + xTaskCreatePinnedToCore(btc_task, "Btc_task", BTC_TASK_STACK_SIZE, NULL, BTC_TASK_PRIO, &xBtcTaskHandle, BTC_TASK_PINNED_TO_CORE); btc_gap_callback_init(); /* TODO: initial the profile_tab */ diff --git a/components/bt/bluedroid/btc/profile/std/a2dp/btc_media_task.c b/components/bt/bluedroid/btc/profile/std/a2dp/btc_media_task.c index 6444392a4..1b6db63c4 100644 --- a/components/bt/bluedroid/btc/profile/std/a2dp/btc_media_task.c +++ b/components/bt/bluedroid/btc/profile/std/a2dp/btc_media_task.c @@ -112,10 +112,6 @@ enum { /* 5 frames is equivalent to 6.89*5*2.9 ~= 100 ms @ 44.1 khz, 20 ms mediatick */ #define MAX_OUTPUT_A2DP_FRAME_QUEUE_SZ (5) -#define MEDIA_DATA_Q_LEN (1) -#define MEDIA_CTRL_Q_LEN (5) -#define COMBINED_MEDIA_Q_LEN (MEDIA_DATA_Q_LEN + MEDIA_CTRL_Q_LEN) - typedef struct { UINT16 num_frames_to_be_processed; UINT16 len; @@ -276,13 +272,13 @@ bool btc_a2dp_start_media_task(void) APPL_TRACE_EVENT("## A2DP START MEDIA THREAD ##"); - xBtcMediaQueueSet = xQueueCreateSet(COMBINED_MEDIA_Q_LEN); + xBtcMediaQueueSet = xQueueCreateSet(BTC_MEDIA_TASK_QUEUE_SET_LEN); configASSERT(xBtcMediaQueueSet); - xBtcMediaDataQueue = xQueueCreate(MEDIA_DATA_Q_LEN, sizeof(void *)); + xBtcMediaDataQueue = xQueueCreate(BTC_MEDIA_DATA_QUEUE_LEN, sizeof(void *)); configASSERT(xBtcMediaDataQueue); xQueueAddToSet(xBtcMediaDataQueue, xBtcMediaQueueSet); - xBtcMediaCtrlQueue = xQueueCreate(MEDIA_CTRL_Q_LEN, sizeof(void *)); + xBtcMediaCtrlQueue = xQueueCreate(BTC_MEDIA_CTRL_QUEUE_LEN, sizeof(void *)); configASSERT(xBtcMediaCtrlQueue); xQueueAddToSet(xBtcMediaCtrlQueue, xBtcMediaQueueSet); diff --git a/components/bt/bluedroid/hci/hci_hal_h4.c b/components/bt/bluedroid/hci/hci_hal_h4.c index 0cc5db676..6d823a58f 100644 --- a/components/bt/bluedroid/hci/hci_hal_h4.c +++ b/components/bt/bluedroid/hci/hci_hal_h4.c @@ -101,8 +101,8 @@ static bool hal_open(const hci_hal_callbacks_t *upper_callbacks) hci_hal_env_init(HCI_HAL_SERIAL_BUFFER_SIZE, SIZE_MAX); - xHciH4Queue = xQueueCreate(HCI_H4_QUEUE_NUM, sizeof(BtTaskEvt_t)); - xTaskCreatePinnedToCore(hci_hal_h4_rx_handler, HCI_H4_TASK_NAME, HCI_H4_TASK_STACK_SIZE, NULL, HCI_H4_TASK_PRIO, &xHciH4TaskHandle, 0); + xHciH4Queue = xQueueCreate(HCI_H4_QUEUE_LEN, sizeof(BtTaskEvt_t)); + xTaskCreatePinnedToCore(hci_hal_h4_rx_handler, HCI_H4_TASK_NAME, HCI_H4_TASK_STACK_SIZE, NULL, HCI_H4_TASK_PRIO, &xHciH4TaskHandle, HCI_H4_TASK_PINNED_TO_CORE); //register vhci host cb esp_vhci_host_register_callback(&vhci_host_cb); diff --git a/components/bt/bluedroid/hci/hci_layer.c b/components/bt/bluedroid/hci/hci_layer.c index 6651fb6f2..cbf180f7e 100644 --- a/components/bt/bluedroid/hci/hci_layer.c +++ b/components/bt/bluedroid/hci/hci_layer.c @@ -107,8 +107,8 @@ int hci_start_up(void) goto error; } - xHciHostQueue = xQueueCreate(HCI_HOST_QUEUE_NUM, sizeof(BtTaskEvt_t)); - xTaskCreatePinnedToCore(hci_host_thread_handler, HCI_HOST_TASK_NAME, HCI_HOST_TASK_STACK_SIZE, NULL, HCI_HOST_TASK_PRIO, &xHciHostTaskHandle, 0); + xHciHostQueue = xQueueCreate(HCI_HOST_QUEUE_LEN, sizeof(BtTaskEvt_t)); + xTaskCreatePinnedToCore(hci_host_thread_handler, HCI_HOST_TASK_NAME, HCI_HOST_TASK_STACK_SIZE, NULL, HCI_HOST_TASK_PRIO, &xHciHostTaskHandle, HCI_HOST_TASK_PINNED_TO_CORE); packet_fragmenter->init(&packet_fragmenter_callbacks); hal->open(&hal_callbacks); diff --git a/components/bt/bluedroid/osi/include/thread.h b/components/bt/bluedroid/osi/include/thread.h index 5f023c657..9ac5da6bd 100644 --- a/components/bt/bluedroid/osi/include/thread.h +++ b/components/bt/bluedroid/osi/include/thread.h @@ -57,26 +57,39 @@ typedef enum { SIG_BTU_NUM, } SIG_BTU_t; +#define TASK_PINNED_TO_CORE (CONFIG_BLUEDROID_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BLUEDROID_PINNED_TO_CORE : tskNO_AFFINITY) + +#define HCI_HOST_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE) #define HCI_HOST_TASK_STACK_SIZE (2048 + BT_TASK_EXTRA_STACK_SIZE) #define HCI_HOST_TASK_PRIO (configMAX_PRIORITIES - 3) #define HCI_HOST_TASK_NAME "hciHostT" -#define HCI_HOST_QUEUE_NUM 40 +#define HCI_HOST_QUEUE_LEN 40 +#define HCI_H4_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE) #define HCI_H4_TASK_STACK_SIZE (2048 + BT_TASK_EXTRA_STACK_SIZE) #define HCI_H4_TASK_PRIO (configMAX_PRIORITIES - 4) #define HCI_H4_TASK_NAME "hciH4T" -#define HCI_H4_QUEUE_NUM 60 +#define HCI_H4_QUEUE_LEN 60 +#define BTU_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE) #define BTU_TASK_STACK_SIZE (4096 + BT_TASK_EXTRA_STACK_SIZE) #define BTU_TASK_PRIO (configMAX_PRIORITIES - 5) #define BTU_TASK_NAME "btuT" -#define BTU_QUEUE_NUM 50 +#define BTU_QUEUE_LEN 50 +#define BTC_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE) #define BTC_TASK_STACK_SIZE (CONFIG_BTC_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE) //by menuconfig #define BTC_TASK_NAME "btcT" #define BTC_TASK_PRIO (configMAX_PRIORITIES - 6) -#define BTC_TASK_QUEUE_NUM 60 +#define BTC_TASK_QUEUE_LEN 60 +#define BTC_MEDIA_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE) +#define BTC_MEDIA_TASK_STACK_SIZE (CONFIG_BTC_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE) +#define BTC_MEDIA_TASK_NAME "BtcMediaT" +#define BTC_MEDIA_TASK_PRIO (configMAX_PRIORITIES - 3) +#define BTC_MEDIA_DATA_QUEUE_LEN (1) +#define BTC_MEDIA_CTRL_QUEUE_LEN (5) +#define BTC_MEDIA_TASK_QUEUE_SET_LEN (BTC_MEDIA_DATA_QUEUE_LEN + BTC_MEDIA_CTRL_QUEUE_LEN) #define TASK_POST_NON_BLOCKING (0) #define TASK_POST_BLOCKING (portMAX_DELAY) diff --git a/components/bt/bluedroid/stack/btu/btu_init.c b/components/bt/bluedroid/stack/btu/btu_init.c index 9776139ec..a3ffd0e70 100644 --- a/components/bt/bluedroid/stack/btu/btu_init.c +++ b/components/bt/bluedroid/stack/btu/btu_init.c @@ -168,8 +168,8 @@ void BTU_StartUp(void) osi_mutex_new(&btu_l2cap_alarm_lock); - xBtuQueue = xQueueCreate(BTU_QUEUE_NUM, sizeof(BtTaskEvt_t)); - xTaskCreatePinnedToCore(btu_task_thread_handler, BTU_TASK_NAME, BTU_TASK_STACK_SIZE, NULL, BTU_TASK_PRIO, &xBtuTaskHandle, 0); + xBtuQueue = xQueueCreate(BTU_QUEUE_LEN, sizeof(BtTaskEvt_t)); + xTaskCreatePinnedToCore(btu_task_thread_handler, BTU_TASK_NAME, BTU_TASK_STACK_SIZE, NULL, BTU_TASK_PRIO, &xBtuTaskHandle, BTU_TASK_PINNED_TO_CORE); btu_task_post(SIG_BTU_START_UP, NULL, TASK_POST_BLOCKING); diff --git a/components/bt/bt.c b/components/bt/bt.c index a0539a19a..6f351cad1 100644 --- a/components/bt/bt.c +++ b/components/bt/bt.c @@ -24,6 +24,7 @@ #include "freertos/semphr.h" #include "freertos/xtensa_api.h" #include "freertos/portmacro.h" +#include "xtensa/core-macros.h" #include "esp_types.h" #include "esp_system.h" #include "esp_task.h" @@ -34,6 +35,7 @@ #include "esp_err.h" #include "esp_log.h" #include "esp_pm.h" +#include "esp_ipc.h" #if CONFIG_BT_ENABLED @@ -135,6 +137,7 @@ struct osi_funcs_t { int32_t (* _task_create)(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id); void (* _task_delete)(void *task_handle); bool (* _is_in_isr)(void); + int (* _cause_sw_intr_to_core)(int core_id, int intr_no); void *(* _malloc)(uint32_t size); void (* _free)(void *p); int32_t (* _read_efuse_mac)(uint8_t mac[6]); @@ -274,6 +277,26 @@ static bool IRAM_ATTR is_in_isr_wrapper(void) return (bool)xPortInIsrContext(); } +static void IRAM_ATTR cause_sw_intr(void *arg) +{ + /* just convert void * to int, because the width is the same */ + uint32_t intr_no = (uint32_t)arg; + XTHAL_SET_INTSET((1<