1. fix bluedroid task post to blocking.
2. use semphore protect btdm controller task when be called from higher priority task
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_MAIN_INIT;
msg.act = BTC_MAIN_ACT_ENABLE;
- btc_transfer_context(&msg, NULL, 0, NULL);
+
+ if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
+ LOG_ERROR("Bluedroid enable failed\n");
+ return ESP_FAIL;
+ }
if (future_await(*future_p) == FUTURE_FAIL) {
LOG_ERROR("Bluedroid enable failed\n");
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_MAIN_INIT;
msg.act = BTC_MAIN_ACT_DISABLE;
- btc_transfer_context(&msg, NULL, 0, NULL);
+
+ if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
+ LOG_ERROR("Bluedroid disable failed\n");
+ return ESP_FAIL;
+ }
if (future_await(*future_p) == FUTURE_FAIL) {
LOG_ERROR("Bluedroid disable failed\n");
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_MAIN_INIT;
msg.act = BTC_MAIN_ACT_INIT;
- btc_transfer_context(&msg, NULL, 0, NULL);
+
+ if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
+ LOG_ERROR("Bluedroid initialise failed\n");
+ return ESP_FAIL;
+ }
if (future_await(*future_p) == FUTURE_FAIL) {
LOG_ERROR("Bluedroid initialise failed\n");
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_MAIN_INIT;
msg.act = BTC_MAIN_ACT_DEINIT;
- btc_transfer_context(&msg, NULL, 0, NULL);
+
+ if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) {
+ LOG_ERROR("Bluedroid de-initialise failed\n");
+ return ESP_FAIL;
+ }
if (future_await(*future_p) == FUTURE_FAIL) {
LOG_ERROR("Bluedroid de-initialise failed\n");
if (btu_bta_msg_queue) {
fixed_queue_enqueue(btu_bta_msg_queue, p_msg);
//ke_event_set(KE_EVENT_BTU_TASK_THREAD);
- btu_task_post(SIG_BTU_WORK);
+ btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING);
}
}
fixed_queue_enqueue(btu_bta_alarm_queue, p_tle);
- btu_task_post(SIG_BTU_WORK);
+ btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING);
}
void bta_sys_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, INT32 timeout_ms)
}
}
-static bt_status_t btc_task_post(btc_msg_t *msg)
+static bt_status_t btc_task_post(btc_msg_t *msg, task_post_t timeout)
{
if (msg == NULL) {
return BT_STATUS_PARM_INVALID;
}
- if (xQueueSend(xBtcQueue, msg, 10 / portTICK_PERIOD_MS) != pdTRUE) {
+ if (xQueueSend(xBtcQueue, msg, timeout) != pdTRUE) {
LOG_ERROR("Btc Post failed\n");
return BT_STATUS_BUSY;
}
lmsg.arg = NULL;
}
- return btc_task_post(&lmsg);
+ return btc_task_post(&lmsg, TASK_POST_BLOCKING);
}
}
}
-void hci_hal_h4_task_post(void)
+void hci_hal_h4_task_post(task_post_t timeout)
{
BtTaskEvt_t evt;
evt.sig = 0xff;
evt.par = 0;
- if (xQueueSend(xHciH4Queue, &evt, 10 / portTICK_PERIOD_MS) != pdTRUE) {
+ if (xQueueSend(xHciH4Queue, &evt, timeout) != pdTRUE) {
LOG_ERROR("xHciH4Queue failed\n");
}
}
{
//Controller rx cache buffer is ready for receiving new host packet
//Just Call Host main thread task to process pending packets.
- hci_host_task_post();
+ hci_host_task_post(TASK_POST_BLOCKING);
}
static int host_recv_pkt_cb(uint8_t *data, uint16_t len)
pkt->layer_specific = 0;
memcpy(pkt->data, data, len);
fixed_queue_enqueue(hci_hal_env.rx_q, pkt);
- hci_hal_h4_task_post();
+ hci_hal_h4_task_post(TASK_POST_BLOCKING);
BTTRC_DUMP_BUFFER("Recv Pkt", pkt->data, len);
}
-void hci_host_task_post(void)
+void hci_host_task_post(task_post_t timeout)
{
BtTaskEvt_t evt;
evt.sig = 0xff;
evt.par = 0;
- if (xQueueSend(xHciHostQueue, &evt, 10 / portTICK_PERIOD_MS) != pdTRUE) {
+ if (xQueueSend(xHciHostQueue, &evt, timeout) != pdTRUE) {
LOG_ERROR("xHciHostQueue failed\n");
}
}
BTTRC_DUMP_BUFFER(NULL, command->data + command->offset, command->len);
fixed_queue_enqueue(hci_host_env.command_queue, wait_entry);
- hci_host_task_post();
+ hci_host_task_post(TASK_POST_BLOCKING);
}
static future_t *transmit_command_futured(BT_HDR *command)
command->event = MSG_STACK_TO_HC_HCI_CMD;
fixed_queue_enqueue(hci_host_env.command_queue, wait_entry);
- hci_host_task_post();
+ hci_host_task_post(TASK_POST_BLOCKING);
return future;
}
fixed_queue_enqueue(hci_host_env.packet_queue, data);
}
//ke_event_set(KE_EVENT_HCI_HOST_THREAD);
- hci_host_task_post();
+ hci_host_task_post(TASK_POST_BLOCKING);
}
/*Tell HCI Host Task to continue TX Pending commands*/
if (hci_host_env.command_credits &&
!fixed_queue_is_empty(hci_host_env.command_queue)) {
- hci_host_task_post();
+ hci_host_task_post(TASK_POST_BLOCKING);
}
//ke_event_set(KE_EVENT_HCI_HOST_THREAD);
if (hci_host_env.upwards_data_queue) {
fixed_queue_enqueue(hci_host_env.upwards_data_queue, packet);
- btu_task_post(SIG_BTU_WORK);
+ btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING);
//Tell Up-layer received packet.
} else {
LOG_DEBUG("%s had no queue to place upwards data packet in. Dropping it on the floor.", __func__);
#define BTC_TASK_PRIO (configMAX_PRIORITIES - 6)
#define BTC_TASK_QUEUE_NUM 60
-void btu_task_post(uint32_t sig);
-void hci_host_task_post(void);
-void hci_hal_h4_task_post(void);
-void hci_drv_task_post(void);
-void bt_alarm_task_post(void);
+
+#define TASK_POST_NON_BLOCKING (0)
+#define TASK_POST_BLOCKING (portMAX_DELAY)
+typedef uint32_t task_post_t; /* Timeout of task post return, unit TICK */
+
+void btu_task_post(uint32_t sig, task_post_t timeout);
+void hci_host_task_post(task_post_t timeout);
+void hci_hal_h4_task_post(task_post_t timeout);
#endif /* __THREAD_H__ */
fixed_queue_enqueue(btu_hci_msg_queue, event);
// ke_event_set(KE_EVENT_BTU_TASK_THREAD);
- btu_task_post(SIG_BTU_WORK);
+ btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING);
}
fixed_queue_enqueue(btu_hci_msg_queue, event);
//ke_event_set(KE_EVENT_BTU_TASK_THREAD);
- btu_task_post(SIG_BTU_WORK);
+ btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING);
}
/*******************************************************************************
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);
- btu_task_post(SIG_BTU_START_UP);
+ btu_task_post(SIG_BTU_START_UP, TASK_POST_BLOCKING);
/*
// Continue startup on bt workqueue thread.
thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
}
-void btu_task_post(uint32_t sig)
+void btu_task_post(uint32_t sig, task_post_t timeout)
{
BtTaskEvt_t evt;
evt.sig = sig;
evt.par = 0;
- if (xQueueSend(xBtuQueue, &evt, 10 / portTICK_PERIOD_MS) != pdTRUE) {
+ if (xQueueSend(xBtuQueue, &evt, timeout) != pdTRUE) {
LOG_ERROR("xBtuQueue failed\n");
}
}
fixed_queue_enqueue(btu_general_alarm_queue, p_tle);
//ke_event_set(KE_EVENT_BTU_TASK_THREAD);
- btu_task_post(SIG_BTU_WORK);
+ btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING);
}
void btu_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec)
fixed_queue_enqueue(btu_l2cap_alarm_queue, p_tle);
//ke_event_set(KE_EVENT_BTU_TASK_THREAD);
- btu_task_post(SIG_BTU_WORK);
+ btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING);
}
void btu_start_quick_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_ticks)
fixed_queue_enqueue(btu_oneshot_alarm_queue, p_tle);
//ke_event_set(KE_EVENT_BTU_TASK_THREAD);
- btu_task_post(SIG_BTU_WORK);
+ btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING);
}
/*
#if CONFIG_BT_ENABLED
+#define BTDM_INIT_PERIOD (5000) /* ms */
+
/* Bluetooth system and controller config */
#define BTDM_CFG_BT_EM_RELEASE (1<<0)
#define BTDM_CFG_BT_DATA_RELEASE (1<<1)
/* Static variable declare */
static bool btdm_bb_init_flag = false;
+static xSemaphoreHandle btdm_init_sem;
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;
btdm_controller_init(btdm_cfg_mask, &btdm_cfg_opts);
btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
+
+ xSemaphoreGive(btdm_init_sem);
+
/* Loop */
btdm_controller_schedule();
+
+ /* never run here */
}
esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
return ESP_ERR_INVALID_ARG;
}
+ btdm_init_sem = xSemaphoreCreateBinary();
+ if (btdm_init_sem == NULL) {
+ return ESP_ERR_NO_MEM;
+ }
+
memcpy(&btdm_cfg_opts, cfg, sizeof(esp_bt_controller_config_t));
ret = xTaskCreatePinnedToCore(bt_controller_task, "btController",
if (ret != pdPASS) {
memset(&btdm_cfg_opts, 0x0, sizeof(esp_bt_controller_config_t));
+ vSemaphoreDelete(btdm_init_sem);
return ESP_ERR_NO_MEM;
}
+ xSemaphoreTake(btdm_init_sem, BTDM_INIT_PERIOD/portTICK_PERIOD_MS);
+ vSemaphoreDelete(btdm_init_sem);
+
return ESP_OK;
}