Bluetooth: drivers: Convert SPI drivers to use new HCI API

Convert the spi.c and hci_spi_st.c drivers to use the new HCI driver API.
Both drivers are converted in one go since one derives from the other's
devicetree binding.

Signed-off-by: Johan Hedberg <johan.hedberg@gmail.com>
This commit is contained in:
Johan Hedberg 2024-06-01 10:46:50 +03:00 committed by Anas Nashif
commit 5a09c17746
14 changed files with 100 additions and 38 deletions

View file

@ -20,6 +20,7 @@
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,code-partition = &slot0_partition;
zephyr,bt-hci = &hci_spi;
};
leds {
@ -124,7 +125,7 @@
pinctrl-names = "default";
/* Nordic nRF51822-QFAC */
bt-hci@0 {
hci_spi: bt-hci@0 {
compatible = "zephyr,bt-hci-spi";
reg = <0>;
reset-gpios = <&gpiob 4 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;

View file

@ -4,10 +4,16 @@
* SPDX-License-Identifier: Apache-2.0
*/
/ {
chosen {
zephyr,bt-hci = &hci_spi;
};
};
&arduino_spi {
cs-gpios = <&arduino_header 1 GPIO_ACTIVE_LOW>; /* A1 */
bluenrg-2@0 {
hci_spi: bluenrg-2@0 {
compatible = "st,hci-spi-v2";
reg = <0>;
reset-gpios = <&arduino_header 13 GPIO_ACTIVE_LOW>; /* D7 */

View file

@ -4,6 +4,12 @@
* SPDX-License-Identifier: Apache-2.0
*/
/ {
chosen {
zephyr,bt-hci = &spbtle_rf_x_nucleo_idb05a1;
};
};
&arduino_spi {
cs-gpios = <&arduino_header 1 GPIO_ACTIVE_LOW>; /* A1 */

View file

@ -22,6 +22,7 @@
zephyr,code-partition = &slot0_partition;
zephyr,flash-controller = &mx25r6435f;
zephyr,bt-c2h-uart = &usart1;
zephyr,bt-hci = &hci_spi;
};
leds {
@ -151,7 +152,7 @@
cs-gpios = <&gpiod 13 GPIO_ACTIVE_LOW>,
<&gpioe 0 GPIO_ACTIVE_LOW>;
spbtle-rf@0 {
hci_spi: spbtle-rf@0 {
compatible = "st,hci-spi-v1";
reg = <0>;
reset-gpios = <&gpioa 8 GPIO_ACTIVE_LOW>;

View file

@ -22,6 +22,7 @@
zephyr,code-partition = &slot0_partition;
zephyr,flash-controller = &mx25r6435f;
zephyr,bt-c2h-uart = &usart1;
zephyr,bt-hci = &hci_spi;
};
leds {
@ -190,7 +191,7 @@
cs-gpios = <&gpiod 13 GPIO_ACTIVE_LOW>,
<&gpioe 0 GPIO_ACTIVE_LOW>;
spbtle-rf@0 {
hci_spi: spbtle-rf@0 {
compatible = "st,hci-spi-v1";
reg = <0>;
reset-gpios = <&gpioa 8 GPIO_ACTIVE_LOW>;

View file

@ -19,6 +19,7 @@
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,bt-c2h-uart = &usart1;
zephyr,bt-hci = &spbtle_1s_sensortile_box;
};
leds {

View file

@ -29,6 +29,7 @@
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,code-partition = &slot0_partition;
zephyr,bt-hci = &hci_spi;
};
leds {
@ -163,7 +164,7 @@ stm32_lp_tick_source: &lptim1 {
cs-gpios = <&gpioa 2 GPIO_ACTIVE_LOW>;
status = "okay";
bluenrg-lp@0 {
hci_spi: bluenrg-lp@0 {
compatible = "st,hci-spi-v2";
reg = <0>;
irq-gpios = <&gpiod 1 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>;

View file

@ -32,6 +32,8 @@
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,code-partition = &slot0_partition;
zephyr,bt-hci = &hci_spi;
};
leds {
@ -164,7 +166,7 @@ stm32_lp_tick_source: &lptim1 {
pinctrl-names = "default";
status = "okay";
cs-gpios = <&gpioe 1 GPIO_ACTIVE_LOW>;
bluenrg-2@0 {
hci_spi: bluenrg-2@0 {
compatible = "st,hci-spi-v2";
reg = <0>;
reset-gpios = <&gpiod 13 GPIO_ACTIVE_LOW>;

View file

@ -36,6 +36,10 @@
volt-sensor0 = &vref;
volt-sensor1 = &vbat;
};
chosen {
zephyr,bt-hci = &hci_spi;
};
};
&clk_hsi48 {
@ -113,7 +117,7 @@ stm32_lp_tick_source: &lptim1 {
cs-gpios = <&gpiog 5 GPIO_ACTIVE_LOW>;
status = "okay";
spbtle-rf@0 {
hci_spi: spbtle-rf@0 {
compatible = "st,hci-spi-v1";
reg = <0>;
irq-gpios = <&gpiog 6 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>;

View file

@ -22,7 +22,7 @@
#include <zephyr/sys/util.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/drivers/bluetooth/hci_driver.h>
#include <zephyr/drivers/bluetooth.h>
#include <zephyr/bluetooth/hci_raw.h>
#define LOG_LEVEL CONFIG_BT_HCI_DRIVER_LOG_LEVEL
@ -90,6 +90,10 @@ static struct k_thread spi_rx_thread_data;
#define BLUENRG_CONFIG_LL_ONLY_OFFSET 0x2C
#define BLUENRG_CONFIG_LL_ONLY_LEN 0x01
struct bt_spi_data {
bt_hci_recv_t recv;
};
static const struct spi_dt_spec bus = SPI_DT_SPEC_INST_GET(
0, SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(8) | SPI_LOCK_ON, 0);
@ -351,7 +355,8 @@ static int bt_spi_send_aci_config(uint8_t offset, const uint8_t *value, size_t v
}
#if !defined(CONFIG_BT_HCI_RAW)
static int bt_spi_bluenrg_setup(const struct bt_hci_setup_params *params)
static int bt_spi_bluenrg_setup(const struct device *dev,
const struct bt_hci_setup_params *params)
{
int ret;
const bt_addr_t *addr = &params->public_addr;
@ -496,7 +501,9 @@ static int bt_spi_rx_buf_construct(uint8_t *msg, struct net_buf **bufp, uint16_t
static void bt_spi_rx_thread(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
const struct device *dev = p1;
struct bt_spi_data *hci = dev->data;
ARG_UNUSED(p2);
ARG_UNUSED(p3);
@ -539,14 +546,14 @@ static void bt_spi_rx_thread(void *p1, void *p2, void *p3)
ret = bt_spi_rx_buf_construct(rxmsg, &buf, size);
if (!ret) {
/* Handle the received HCI data */
bt_recv(buf);
hci->recv(dev, buf);
buf = NULL;
}
} while (READ_CONDITION);
}
}
static int bt_spi_send(struct net_buf *buf)
static int bt_spi_send(const struct device *dev, struct net_buf *buf)
{
uint16_t size;
uint8_t rx_first[1];
@ -632,8 +639,9 @@ static int bt_spi_send(struct net_buf *buf)
return ret;
}
static int bt_spi_open(void)
static int bt_spi_open(const struct device *dev, bt_hci_recv_t recv)
{
struct bt_spi_data *hci = dev->data;
int err;
/* Configure RST pin and hold BLE in Reset */
@ -660,6 +668,8 @@ static int bt_spi_open(void)
return err;
}
hci->recv = recv;
/* Take BLE out of reset */
k_sleep(K_MSEC(DT_INST_PROP_OR(0, reset_assert_duration_ms, 0)));
gpio_pin_set_dt(&rst_gpio, 0);
@ -667,7 +677,7 @@ static int bt_spi_open(void)
/* Start RX thread */
k_thread_create(&spi_rx_thread_data, spi_rx_stack,
K_KERNEL_STACK_SIZEOF(spi_rx_stack),
bt_spi_rx_thread, NULL, NULL, NULL,
bt_spi_rx_thread, (void *)dev, NULL, NULL,
K_PRIO_COOP(CONFIG_BT_DRIVER_RX_HIGH_PRIO),
0, K_NO_WAIT);
@ -683,10 +693,7 @@ static int bt_spi_open(void)
return 0;
}
static const struct bt_hci_driver drv = {
.name = DEVICE_DT_NAME(DT_DRV_INST(0)),
.bus = BT_HCI_DRIVER_BUS_SPI,
.quirks = BT_QUIRK_NO_RESET,
static const struct bt_hci_driver_api drv = {
#if defined(CONFIG_BT_BLUENRG_ACI) && !defined(CONFIG_BT_HCI_RAW)
.setup = bt_spi_bluenrg_setup,
#endif /* CONFIG_BT_BLUENRG_ACI && !CONFIG_BT_HCI_RAW */
@ -694,7 +701,7 @@ static const struct bt_hci_driver drv = {
.send = bt_spi_send,
};
static int bt_spi_init(void)
static int bt_spi_init(const struct device *dev)
{
if (!spi_is_ready_dt(&bus)) {
@ -712,12 +719,16 @@ static int bt_spi_init(void)
return -ENODEV;
}
bt_hci_driver_register(&drv);
LOG_DBG("BT SPI initialized");
return 0;
}
SYS_INIT(bt_spi_init, POST_KERNEL, CONFIG_BT_SPI_INIT_PRIORITY);
#define HCI_DEVICE_INIT(inst) \
static struct bt_spi_data hci_data_##inst = { \
}; \
DEVICE_DT_INST_DEFINE(inst, bt_spi_init, NULL, &hci_data_##inst, NULL, \
POST_KERNEL, CONFIG_BT_SPI_INIT_PRIORITY, &drv)
/* Only one instance supported right now */
HCI_DEVICE_INIT(0)

View file

@ -15,7 +15,7 @@
#include <zephyr/sys/util.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/drivers/bluetooth/hci_driver.h>
#include <zephyr/drivers/bluetooth.h>
#define LOG_LEVEL CONFIG_BT_HCI_DRIVER_LOG_LEVEL
#include <zephyr/logging/log.h>
@ -66,6 +66,10 @@ LOG_MODULE_REGISTER(bt_driver);
be transmitted across this HCI link
#endif /* CONFIG_BT_L2CAP_TX_MTU > MAX_MTU */
struct bt_spi_data {
bt_hci_recv_t recv;
};
static uint8_t __noinit rxmsg[SPI_MAX_MSG_LEN];
static uint8_t __noinit txmsg[SPI_MAX_MSG_LEN];
@ -236,7 +240,9 @@ static struct net_buf *bt_spi_rx_buf_construct(uint8_t *msg)
static void bt_spi_rx_thread(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
const struct device *dev = p1;
struct bt_spi_data *hci = dev->data;
ARG_UNUSED(p2);
ARG_UNUSED(p3);
@ -288,17 +294,19 @@ static void bt_spi_rx_thread(void *p1, void *p2, void *p3)
buf = bt_spi_rx_buf_construct(rxmsg);
if (buf) {
/* Handle the received HCI data */
bt_recv(buf);
hci->recv(dev, buf);
}
}
}
static int bt_spi_send(struct net_buf *buf)
static int bt_spi_send(const struct device *dev, struct net_buf *buf)
{
uint16_t size;
uint8_t rx_first[1];
int ret;
ARG_UNUSED(dev);
LOG_DBG("");
/* Buffer needs an additional byte for type */
@ -364,8 +372,9 @@ out:
return ret;
}
static int bt_spi_open(void)
static int bt_spi_open(const struct device *dev, bt_hci_recv_t recv)
{
struct bt_spi_data *hci = dev->data;
int err;
/* Configure RST pin and hold BLE in Reset */
@ -392,6 +401,8 @@ static int bt_spi_open(void)
return err;
}
hci->recv = recv;
/* Take BLE out of reset */
k_sleep(K_MSEC(DT_INST_PROP_OR(0, reset_assert_duration_ms, 0)));
gpio_pin_set_dt(&rst_gpio, 0);
@ -399,7 +410,7 @@ static int bt_spi_open(void)
/* Start RX thread */
k_thread_create(&spi_rx_thread_data, spi_rx_stack,
K_KERNEL_STACK_SIZEOF(spi_rx_stack),
bt_spi_rx_thread, NULL, NULL, NULL,
bt_spi_rx_thread, (void *)dev, NULL, NULL,
K_PRIO_COOP(CONFIG_BT_DRIVER_RX_HIGH_PRIO),
0, K_NO_WAIT);
@ -409,15 +420,14 @@ static int bt_spi_open(void)
return 0;
}
static const struct bt_hci_driver drv = {
.name = DEVICE_DT_NAME(DT_DRV_INST(0)),
.bus = BT_HCI_DRIVER_BUS_SPI,
static const struct bt_hci_driver_api drv = {
.open = bt_spi_open,
.send = bt_spi_send,
};
static int bt_spi_init(void)
static int bt_spi_init(const struct device *dev)
{
ARG_UNUSED(dev);
if (!spi_is_ready_dt(&bus)) {
LOG_ERR("SPI device not ready");
@ -434,12 +444,16 @@ static int bt_spi_init(void)
return -ENODEV;
}
bt_hci_driver_register(&drv);
LOG_DBG("BT SPI initialized");
return 0;
}
SYS_INIT(bt_spi_init, POST_KERNEL, CONFIG_BT_SPI_INIT_PRIORITY);
#define HCI_DEVICE_INIT(inst) \
static struct bt_spi_data hci_data_##inst = { \
}; \
DEVICE_DT_INST_DEFINE(inst, bt_spi_init, NULL, &hci_data_##inst, NULL, \
POST_KERNEL, CONFIG_BT_SPI_INIT_PRIORITY, &drv)
/* Only one instance supported right now */
HCI_DEVICE_INIT(0)

View file

@ -6,3 +6,7 @@ description: STMicroelectronics SPI protocol V1 compatible with BlueNRG-MS devic
compatible: "st,hci-spi-v1"
include: zephyr,bt-hci-spi.yaml
properties:
bt-hci-quirks:
default: ["BT_HCI_QUIRK_NO_RESET"]

View file

@ -6,3 +6,7 @@ description: STMicroelectronics SPI protocol V2 compatible with BlueNRG-1 and su
compatible: "st,hci-spi-v2"
include: zephyr,bt-hci-spi.yaml
properties:
bt-hci-quirks:
default: ["BT_HCI_QUIRK_NO_RESET"]

View file

@ -7,7 +7,7 @@ description: |
compatible: "zephyr,bt-hci-spi"
include: spi-device.yaml
include: [spi-device.yaml, bt-hci.yaml]
properties:
irq-gpios:
@ -35,3 +35,9 @@ properties:
resulting in an ignored transaction that then needs to be performed a second time.
The default of 20uS was chosen as the lowest delay that reliably eliminated double
transactions between a nRF9160 host and a nRF52832 controller.
bt-hci-name:
default: "SPI"
bt-hci-bus:
default: "BT_HCI_BUS_SPI"