Skip to content

Commit c3139c5

Browse files
JordanYateskartben
authored andcommitted
bluetooth: hci: SPI backend boot timeout
Add a timeout to the SPI boot process to ensure that `bt_enable` does not block forever but instead returns an error after an appropriately large delay. While 1 second would be sufficient under normal operation, we also want to give the controller time to perform any application upgrades via a bootloader. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent ce4240f commit c3139c5

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

drivers/bluetooth/hci/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ config BT_SPI_INIT_PRIORITY
8181
depends on BT_SPI
8282
default 75
8383

84+
config BT_SPI_BOOT_TIMEOUT_SEC
85+
int "Seconds to wait for SPI device to report ready"
86+
depends on BT_SPI_ZEPHYR || BT_SPI_BLUENRG
87+
default 30
88+
help
89+
Maximum duration for a HCI SPI Controller to report ready through the
90+
Zephyr Project defined `EVT_BLUE_INITIALIZED` HCI vendor event.
91+
Default is 30 seconds to support a bootloader image swap on the
92+
Controller.
93+
8494
config BT_SPI_ZEPHYR
8595
bool
8696
default y

drivers/bluetooth/hci/hci_spi_st.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,9 @@ static int bt_spi_send(const struct device *dev, struct net_buf *buf)
631631
* EVT_BLUE_INITIALIZED as an indication that it is safe to proceed.
632632
*/
633633
if (bt_spi_get_cmd(buf->data) == BT_HCI_OP_RESET) {
634-
k_sem_take(&sem_initialised, K_FOREVER);
634+
if (k_sem_take(&sem_initialised, K_SECONDS(CONFIG_BT_SPI_BOOT_TIMEOUT_SEC)) < 0) {
635+
ret = -EIO;
636+
}
635637
}
636638
#endif /* DT_HAS_COMPAT_STATUS_OKAY(st_hci_spi_v1) */
637639
net_buf_unref(buf);
@@ -682,7 +684,9 @@ static int bt_spi_open(const struct device *dev, bt_hci_recv_t recv)
682684
0, K_NO_WAIT);
683685

684686
/* Device will let us know when it's ready */
685-
k_sem_take(&sem_initialised, K_FOREVER);
687+
if (k_sem_take(&sem_initialised, K_SECONDS(CONFIG_BT_SPI_BOOT_TIMEOUT_SEC)) < 0) {
688+
return -EIO;
689+
}
686690

687691
#if defined(CONFIG_BT_HCI_RAW) && defined(CONFIG_BT_BLUENRG_ACI)
688692
/* force BlueNRG to be on controller mode */

drivers/bluetooth/hci/spi.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,9 @@ static int bt_spi_open(const struct device *dev, bt_hci_recv_t recv)
416416
k_thread_name_set(&spi_rx_thread_data, "bt_spi_rx_thread");
417417

418418
/* Device will let us know when it's ready */
419-
k_sem_take(&sem_initialised, K_FOREVER);
419+
if (k_sem_take(&sem_initialised, K_SECONDS(CONFIG_BT_SPI_BOOT_TIMEOUT_SEC)) < 0) {
420+
return -EIO;
421+
}
420422

421423
return 0;
422424
}

0 commit comments

Comments
 (0)