Skip to content

Bluetooth: Host: Introduce a Bluetooth workqueue #93033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions samples/bluetooth/central_hr/prj_minimal.conf
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,29 @@ CONFIG_ARM_MPU=n
# CONFIG_THREAD_ANALYZER_RUN_UNLOCKED=y
# CONFIG_THREAD_ANALYZER_USE_PRINTK=y
# CONFIG_THREAD_ANALYZER_AUTO_INTERVAL=20
# CONFIG_CONSOLE=y
# CONFIG_UART_CONSOLE=y
# CONFIG_SERIAL=y
# CONFIG_PRINTK=y
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y
CONFIG_SERIAL=y
CONFIG_PRINTK=y

# Example output of thread analyzer
# BT RX : STACK: unused 576 usage 448 / 1024 (43 %); CPU: 0 %
# BT RX pri : STACK: unused 260 usage 188 / 448 (41 %); CPU: 0 %
# BT ECC : STACK: unused 256 usage 888 / 1144 (77 %); CPU: 1 %
# BT TX : STACK: unused 296 usage 344 / 640 (53 %); CPU: 0 %
# thread_analyzer : STACK: unused 128 usage 384 / 512 (75 %); CPU: 1 %
# sysworkq : STACK: unused 856 usage 168 / 1024 (16 %); CPU: 0 %
# logging : STACK: unused 232 usage 536 / 768 (69 %); CPU: 0 %
# idle 00 : STACK: unused 208 usage 48 / 256 (18 %); CPU: 97 %
# main : STACK: unused 576 usage 448 / 1024 (43 %); CPU: 0 %
# BT CTLR RX : STACK: unused 16 usage 288 / 304 (94 %); CPU: 0 %
# BT CTLR RX pri : STACK: unused 24 usage 344 / 368 (93 %); CPU: 0 %
# BT WQ : STACK: unused 48 usage 976 / 1024 (95 %); CPU: 1 %
# thread_analyzer : STACK: unused 464 usage 560 / 1024 (54 %); CPU: 0 %
# BT LW WQ : STACK: unused 24 usage 1080 / 1104 (97 %); CPU: 19 %
# sysworkq : STACK: unused 96 usage 504 / 600 (84 %); CPU: 0 %
# idle : STACK: unused 40 usage 88 / 128 (68 %); CPU: 78 %
# main : STACK: unused 168 usage 432 / 600 (72 %); CPU: 0 %
# ISR0 : STACK: unused 200 usage 600 / 800 (75 %)
CONFIG_BT_RX_STACK_SIZE=1024
CONFIG_BT_HCI_TX_STACK_SIZE_WITH_PROMPT=y
CONFIG_BT_HCI_TX_STACK_SIZE=640
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=1100
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=600
CONFIG_BT_LONG_WQ_STACK_SIZE=1100
CONFIG_IDLE_STACK_SIZE=128
CONFIG_MAIN_STACK_SIZE=640
CONFIG_ISR_STACK_SIZE=1024
CONFIG_MAIN_STACK_SIZE=600
CONFIG_ISR_STACK_SIZE=700

# Disable features not needed
CONFIG_TIMESLICING=n
Expand Down
11 changes: 7 additions & 4 deletions samples/bluetooth/mesh_demo/boards/bbc_microbit.conf
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
CONFIG_ISR_STACK_SIZE=768
CONFIG_ISR_STACK_SIZE=700
CONFIG_MAIN_STACK_SIZE=512
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=1200
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=256
CONFIG_GPIO=y
CONFIG_DISPLAY=y
CONFIG_MICROBIT_DISPLAY=y
CONFIG_PWM=y

CONFIG_BT_RX_STACK_SIZE=1280
CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT=1
CONFIG_BT_MESH_ADV_STACK_SIZE=200

CONFIG_BT_RX_STACK_SIZE=2000
CONFIG_BT_CTLR_DUP_FILTER_LEN=0
CONFIG_BT_PHY_UPDATE=n
CONFIG_BT_DATA_LEN_UPDATE=n
CONFIG_BT_EXT_ADV=n
CONFIG_BT_BUF_EVT_DISCARDABLE_COUNT=15
CONFIG_BT_BUF_EVT_DISCARDABLE_COUNT=1

CONFIG_BT_MESH_SEG_BUFS=6
CONFIG_BT_MESH_ADV_BUF_COUNT=3
Expand Down
30 changes: 14 additions & 16 deletions samples/bluetooth/mesh_demo/src/microbit.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,14 @@

#define SCROLL_SPEED 300

#define BUZZER_PWM_CHANNEL 0
#define BEEP_DURATION K_MSEC(60)

#define SEQ_PER_BIT 976
#define SEQ_PAGE (NRF_FICR->CODEPAGESIZE * (NRF_FICR->CODESIZE - 1))
#define SEQ_MAX (NRF_FICR->CODEPAGESIZE * 8 * SEQ_PER_BIT)

static const struct gpio_dt_spec button_a =
GPIO_DT_SPEC_GET(DT_NODELABEL(buttona), gpios);
GPIO_DT_SPEC_GET(DT_ALIAS(sw0), gpios);
static const struct gpio_dt_spec button_b =
GPIO_DT_SPEC_GET(DT_NODELABEL(buttonb), gpios);
static const struct device *const nvm =
DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));
static const struct device *const pwm =
DEVICE_DT_GET_ANY(nordic_nrf_sw_pwm);
GPIO_DT_SPEC_GET(DT_ALIAS(sw1), gpios);

static struct k_work button_work;

Expand Down Expand Up @@ -64,6 +57,13 @@ static void button_pressed(const struct device *dev, struct gpio_callback *cb,
}
}

#if defined(CONFIG_PWM)
#define BUZZER_PWM_CHANNEL 0
#define BEEP_DURATION K_MSEC(60)

static const struct device *const pwm =
DEVICE_DT_GET_ANY(nordic_nrf_sw_pwm);

static const struct {
char note;
uint32_t period;
Expand Down Expand Up @@ -139,6 +139,11 @@ void board_play_tune(const char *str)
pwm_set(pwm, BUZZER_PWM_CHANNEL, 0, 0, 0);
}
}
#else
void board_play_tune(const char *str)
{
}
#endif /* CONFIG_PWM */

void board_heartbeat(uint8_t hops, uint16_t feat)
{
Expand Down Expand Up @@ -267,13 +272,6 @@ int board_init(uint16_t *addr)
{
struct mb_display *disp = mb_display_get();

if (!(device_is_ready(nvm) && device_is_ready(pwm) &&
gpio_is_ready_dt(&button_a) &&
gpio_is_ready_dt(&button_b))) {
printk("One or more devices are not ready\n");
return -ENODEV;
}

*addr = NRF_UICR->CUSTOMER[0];
if (!*addr || *addr == 0xffff) {
#if defined(NODE_ADDR)
Expand Down
35 changes: 17 additions & 18 deletions samples/bluetooth/peripheral_hr/prj_minimal.conf
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,28 @@ CONFIG_ARM_MPU=n
# CONFIG_THREAD_ANALYZER_RUN_UNLOCKED=y
# CONFIG_THREAD_ANALYZER_USE_PRINTK=y
# CONFIG_THREAD_ANALYZER_AUTO_INTERVAL=20
# CONFIG_CONSOLE=y
# CONFIG_UART_CONSOLE=y
# CONFIG_SERIAL=y
# CONFIG_PRINTK=y
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y
CONFIG_SERIAL=y
CONFIG_PRINTK=y

# Example output of thread analyzer
# BT RX : STACK: unused 576 usage 448 / 1024 (43 %); CPU: 0 %
# BT RX pri : STACK: unused 260 usage 188 / 448 (41 %); CPU: 0 %
# BT ECC : STACK: unused 256 usage 888 / 1144 (77 %); CPU: 1 %
# BT TX : STACK: unused 296 usage 344 / 640 (53 %); CPU: 0 %
# thread_analyzer : STACK: unused 128 usage 384 / 512 (75 %); CPU: 1 %
# sysworkq : STACK: unused 856 usage 168 / 1024 (16 %); CPU: 0 %
# logging : STACK: unused 232 usage 536 / 768 (69 %); CPU: 0 %
# idle 00 : STACK: unused 208 usage 48 / 256 (18 %); CPU: 97 %
# main : STACK: unused 576 usage 448 / 1024 (43 %); CPU: 0 %
CONFIG_BT_RX_STACK_SIZE=1024
# BT CTLR RX : STACK: unused 32 usage 288 / 320 (90 %); CPU: 0 %
# BT CTLR RX pri : STACK: unused 32 usage 344 / 376 (91 %); CPU: 0 %
# BT WQ : STACK: unused 32 usage 944 / 976 (96 %); CPU: 0 %
# BT LW WQ : STACK: unused 8 usage 1096 / 1104 (99 %); CPU: 8 %
# sysworkq : STACK: unused 40 usage 360 / 400 (90 %); CPU: 0 %
# idle : STACK: unused 40 usage 88 / 128 (68 %); CPU: 90 %
# main : STACK: unused 72 usage 608 / 680 (89 %); CPU: 0 %
# ISR0 : STACK: unused 104 usage 600 / 704 (85 %)
CONFIG_BT_RX_STACK_SIZE=976
CONFIG_BT_HCI_TX_STACK_SIZE_WITH_PROMPT=y
CONFIG_BT_HCI_TX_STACK_SIZE=640
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=1100
CONFIG_BT_LONG_WQ_STACK_SIZE=1100
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=400
CONFIG_IDLE_STACK_SIZE=128
CONFIG_MAIN_STACK_SIZE=640
CONFIG_ISR_STACK_SIZE=1024
CONFIG_MAIN_STACK_SIZE=680
CONFIG_ISR_STACK_SIZE=700
CONFIG_BT_LONG_WQ_STACK_SIZE=1120

# Disable features not needed
CONFIG_TIMESLICING=n
Expand Down
2 changes: 2 additions & 0 deletions subsys/bluetooth/controller/Kconfig.ll_sw_split
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ config BT_CTLR_RX_PRIO_STACK_SIZE
# Hidden
int
depends on !BT_HCI_RAW
default 376 if SOC_SERIES_NRF51X && !BT_EXT_ADV
default 448
help
Controller's Co-Operative high priority Rx thread stack size.
Expand All @@ -141,6 +142,7 @@ config BT_CTLR_RX_PRIO_STACK_SIZE
config BT_CTLR_RX_STACK_SIZE
# Hidden, Controller's Co-Operative Rx thread stack size.
int
default 320 if SOC_SERIES_NRF51X && !BT_EXT_ADV
default 896

config BT_CTLR_SETTINGS
Expand Down
18 changes: 16 additions & 2 deletions subsys/bluetooth/host/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ config BT_HCI_TX_PRIO

choice BT_RECV_CONTEXT
prompt "BT RX Thread Selection"
default BT_RECV_WORKQ_SYS if SOC_SERIES_NRF51X
default BT_RECV_WORKQ_BT
help
Selects in which context incoming low priority HCI packets are processed.
Expand All @@ -86,7 +85,8 @@ choice BT_RECV_CONTEXT
packets are processed.

config BT_RECV_WORKQ_SYS
bool "Process low priority HCI packets in the system work queue"
bool "Process low priority HCI packets in the system work queue [DEPRECATED]"
select DEPRECATED
help
When this option is selected, the host will process incoming low priority HCI packets
in the system work queue.
Expand Down Expand Up @@ -128,6 +128,20 @@ config BT_RX_PRIO
int
default 8

config BT_WQ
bool
default y if BT_RECV_WORKQ_BT

if BT_WQ
config BT_WQ_STACK_SIZE
int
default BT_RX_STACK_SIZE

config BT_WQ_PRIO
int
default BT_RX_PRIO
endif

config BT_DRIVER_RX_HIGH_PRIO
# Hidden option for Co-Operative HCI driver RX thread priority
int
Expand Down
4 changes: 2 additions & 2 deletions subsys/bluetooth/host/adv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1431,8 +1431,8 @@ int bt_le_adv_start(const struct bt_le_adv_param *param,

if (ad_is_limited(ad, ad_len)) {
k_work_init_delayable(&adv->lim_adv_timeout_work, adv_timeout);
k_work_reschedule(&adv->lim_adv_timeout_work,
K_SECONDS(CONFIG_BT_LIM_ADV_TIMEOUT));
bt_work_reschedule(&adv->lim_adv_timeout_work,
K_SECONDS(CONFIG_BT_LIM_ADV_TIMEOUT));
}

return err;
Expand Down
6 changes: 3 additions & 3 deletions subsys/bluetooth/host/att.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ static void chan_rebegin_att_timeout(struct bt_att_tx_meta_data *user_data)
* in-flight.
*/
if (chan->req) {
k_work_reschedule(&chan->timeout_work, BT_ATT_TIMEOUT);
bt_work_reschedule(&chan->timeout_work, BT_ATT_TIMEOUT);
}
}

Expand Down Expand Up @@ -3579,8 +3579,8 @@ static int att_schedule_eatt_connect(struct bt_conn *conn, uint8_t chans_to_conn

att->eatt.chans_to_connect = chans_to_connect;

return k_work_reschedule(&att->eatt.connection_work,
credit_based_connection_delay(conn));
return bt_work_reschedule(&att->eatt.connection_work,
credit_based_connection_delay(conn));
}

static void handle_potential_collision(struct bt_att *att)
Expand Down
4 changes: 2 additions & 2 deletions subsys/bluetooth/host/classic/br.c
Original file line number Diff line number Diff line change
Expand Up @@ -1218,8 +1218,8 @@ int bt_br_set_discoverable(bool enable, bool limited)
err = write_scan_enable(BT_BREDR_SCAN_INQUIRY | BT_BREDR_SCAN_PAGE);
if (!err && (limited == true)) {
atomic_set_bit(bt_dev.flags, BT_DEV_LIMITED_DISCOVERABLE_MODE);
k_work_reschedule(&bt_br_limited_discoverable_timeout,
K_SECONDS(CONFIG_BT_LIMITED_DISCOVERABLE_DURATION));
bt_work_reschedule(&bt_br_limited_discoverable_timeout,
K_SECONDS(CONFIG_BT_LIMITED_DISCOVERABLE_DURATION));
}
return err;
}
Expand Down
20 changes: 10 additions & 10 deletions subsys/bluetooth/host/classic/l2cap_br.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,29 +438,29 @@ static void l2cap_br_start_timer(struct bt_l2cap_br_chan *br_chan, enum l2cap_br
if (type == BT_L2CAP_BR_TIMER_RET) {
if (!atomic_test_and_set_bit(br_chan->flags, L2CAP_FLAG_RET_TIMER)) {
k_work_cancel_delayable(&br_chan->monitor_work);
k_work_schedule(&br_chan->ret_work, K_MSEC(br_chan->tx.ret_timeout));
bt_work_schedule(&br_chan->ret_work, K_MSEC(br_chan->tx.ret_timeout));
LOG_DBG("Start ret timer");
} else {
if (!restart) {
return;
}

k_work_reschedule(&br_chan->ret_work, K_MSEC(br_chan->tx.ret_timeout));
bt_work_reschedule(&br_chan->ret_work, K_MSEC(br_chan->tx.ret_timeout));
LOG_DBG("Restart ret timer");
}
} else {
if (atomic_test_and_clear_bit(br_chan->flags, L2CAP_FLAG_RET_TIMER)) {
k_work_cancel_delayable(&br_chan->ret_work);
k_work_schedule(&br_chan->monitor_work,
K_MSEC(br_chan->tx.monitor_timeout));
bt_work_schedule(&br_chan->monitor_work,
K_MSEC(br_chan->tx.monitor_timeout));
LOG_DBG("Start monitor timer");
} else {
if (!restart) {
return;
}

k_work_reschedule(&br_chan->monitor_work,
K_MSEC(br_chan->tx.monitor_timeout));
bt_work_reschedule(&br_chan->monitor_work,
K_MSEC(br_chan->tx.monitor_timeout));
LOG_DBG("Restart monitor timer");
}
}
Expand Down Expand Up @@ -736,7 +736,7 @@ static void l2cap_br_ret_timeout(struct k_work *work)

/* Restart the timer */
if (atomic_test_bit(br_chan->flags, L2CAP_FLAG_RET_TIMER)) {
k_work_schedule(&br_chan->ret_work, K_MSEC(br_chan->tx.ret_timeout));
bt_work_schedule(&br_chan->ret_work, K_MSEC(br_chan->tx.ret_timeout));
}

LOG_DBG("chan %p retransmission timeout", br_chan);
Expand Down Expand Up @@ -805,7 +805,7 @@ static void l2cap_br_monitor_timeout(struct k_work *work)

/* Restart the timer */
if (!atomic_test_bit(br_chan->flags, L2CAP_FLAG_RET_TIMER)) {
k_work_schedule(&br_chan->monitor_work, K_MSEC(br_chan->tx.monitor_timeout));
bt_work_schedule(&br_chan->monitor_work, K_MSEC(br_chan->tx.monitor_timeout));
}

LOG_DBG("chan %p monitor timeout", br_chan);
Expand Down Expand Up @@ -962,7 +962,7 @@ static void l2cap_br_chan_send_req(struct bt_l2cap_br_chan *chan,
* final expiration, when the response is received, or the physical
* link is lost.
*/
k_work_reschedule(&chan->rtx_work, timeout);
bt_work_reschedule(&chan->rtx_work, timeout);
}

#if defined(CONFIG_BT_L2CAP_RET_FC)
Expand Down Expand Up @@ -4805,7 +4805,7 @@ static void l2cap_br_conn_rsp(struct bt_l2cap_br *l2cap, uint8_t ident, struct n
break;
case BT_L2CAP_BR_PENDING:
br_chan->ident = ident;
k_work_reschedule(&br_chan->rtx_work, L2CAP_BR_CONN_TIMEOUT);
bt_work_reschedule(&br_chan->rtx_work, L2CAP_BR_CONN_TIMEOUT);
break;
default:
l2cap_br_chan_cleanup(chan);
Expand Down
Loading
Loading