diff --git a/src/main.c b/src/main.c index e4c906e..c747226 100644 --- a/src/main.c +++ b/src/main.c @@ -64,6 +64,9 @@ static struct k_thread tx_thread_data; static K_THREAD_STACK_DEFINE(unproc_stack, 4096); static struct k_thread unproc_thread_data; +static K_THREAD_STACK_DEFINE(event_stack, 4096); +static struct k_thread event_thread_data; + static struct k_delayed_work ack_work; static struct k_delayed_work retx_work; @@ -121,7 +124,8 @@ static struct h5 { struct k_fifo controller_queue; struct k_fifo host_queue; struct k_fifo unack_queue; - struct k_fifo unprocessed_queue; + struct k_fifo unprocessed_queue; + struct k_fifo event_queue; u8_t tx_win; u8_t tx_ack; @@ -656,6 +660,19 @@ static void tx_thread(void) } } +static void event_thread(void) { + static struct net_buf *buf = NULL; + while (true) { + buf = net_buf_get(&h5.event_queue, K_MSEC(1000)); + if (!buf) { + LOG_WRN("host_queue is empty"); + continue; + } + net_buf_put(&h5.host_queue, buf); + net_buf_unref(buf); + } +} + static void h5_init(void) { LOG_DBG("h5_init"); @@ -669,7 +686,7 @@ static void h5_init(void) k_thread_create(&tx_thread_data, tx_stack, K_THREAD_STACK_SIZEOF(tx_stack), (k_thread_entry_t)tx_thread, NULL, NULL, NULL, - K_PRIO_COOP(CONFIG_BT_HCI_TX_PRIO), + K_PRIO_PREEMPT(1), 0, K_NO_WAIT); k_fifo_init(&h5.host_queue); @@ -687,9 +704,18 @@ static void h5_init(void) k_thread_create(&unproc_thread_data, unproc_stack, K_THREAD_STACK_SIZEOF(unproc_stack), (k_thread_entry_t)unproc_thread, NULL, NULL, NULL, - K_PRIO_COOP(CONFIG_BT_RX_PRIO), + K_PRIO_PREEMPT(1), 0, K_NO_WAIT); + /* Thread handles incoming events from BT Controller */ + k_fifo_init(&h5.event_queue); + k_thread_create(&event_thread_data, event_stack, + K_THREAD_STACK_SIZEOF(event_stack), + (k_thread_entry_t)event_thread, NULL, NULL, NULL, + K_PRIO_PREEMPT(2), + 0, K_NO_WAIT); + + /* Init delayed work */ k_delayed_work_init(&ack_work, ack_timeout); k_delayed_work_init(&retx_work, retx_timeout); @@ -911,7 +937,7 @@ void main(void) LOG_DBG("Start"); gpio_init(); // Adds controller output to host output queue - bt_enable_raw(&h5.host_queue); + bt_enable_raw(&h5.event_queue); struct net_buf *buf = NULL; h5_init();