Skip to content
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

drivers: bluetooth: hci: Simplify SPI driver #64453

Merged
merged 5 commits into from
Nov 3, 2023
Merged
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
2 changes: 2 additions & 0 deletions boards/arm/b_l4s5i_iot01a/b_l4s5i_iot01a.dts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@
reset-gpios = <&gpioa 8 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
irq-gpios = <&gpioe 6 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>;
spi-max-frequency = <2000000>;
controller-data-delay-us = <0>; /* No need for extra delay for BlueNRG-MS */
spi-hold-cs;
};

wifi0: ism43362@1 {
Expand Down
2 changes: 2 additions & 0 deletions boards/arm/disco_l475_iot1/disco_l475_iot1.dts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@
reset-gpios = <&gpioa 8 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
irq-gpios = <&gpioe 6 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>;
spi-max-frequency = <2000000>;
controller-data-delay-us = <0>; /* No need for extra delay for BlueNRG-MS */
spi-hold-cs;
};

wifi0: ism43362@1 {
Expand Down
1 change: 1 addition & 0 deletions boards/arm/stm32l562e_dk/stm32l562e_dk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ supported:
- dac
- adc
- spi
- ble
- dma
- usart
- arduino_spi
Expand Down
4 changes: 3 additions & 1 deletion boards/arm/stm32l562e_dk/stm32l562e_dk_common.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
&spi1 {
pinctrl-0 = <&spi1_sck_pg2 &spi1_miso_pg3 &spi1_mosi_pg4>;
pinctrl-names = "default";
cs-gpios = <&gpiog 5 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>;
cs-gpios = <&gpiog 5 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
erwango marked this conversation as resolved.
Show resolved Hide resolved
status = "okay";

spbtle-rf@0 {
Expand All @@ -120,6 +120,8 @@
irq-gpios = <&gpiog 6 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>;
reset-gpios = <&gpiog 8 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
spi-max-frequency = <2000000>;
controller-data-delay-us = <0>; /* No need for extra delay for BlueNRG-MS */
erwango marked this conversation as resolved.
Show resolved Hide resolved
spi-hold-cs;
};
};

Expand Down
1 change: 1 addition & 0 deletions boards/shields/x_nucleo_idb05a1/x_nucleo_idb05a1.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
irq-gpios = <&arduino_header 0 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>; /* A0 */
spi-max-frequency = <2000000>;
controller-data-delay-us = <0>; /* No need for extra delay for BlueNRG-MS */
spi-hold-cs;
};
};
53 changes: 5 additions & 48 deletions drivers/bluetooth/hci/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ LOG_MODULE_REGISTER(bt_driver);
#define EVT_HEADER_TYPE 0
#define EVT_HEADER_EVENT 1
#define EVT_HEADER_SIZE 2
#define EVT_LE_META_SUBEVENT 3
#define EVT_VENDOR_CODE_LSB 3
#define EVT_VENDOR_CODE_MSB 4

Expand Down Expand Up @@ -84,30 +85,6 @@ static K_SEM_DEFINE(sem_busy, 1, 1);
static K_KERNEL_STACK_DEFINE(spi_rx_stack, CONFIG_BT_DRV_RX_STACK_SIZE);
static struct k_thread spi_rx_thread_data;

#if defined(CONFIG_BT_HCI_DRIVER_LOG_LEVEL_DBG)
#include <zephyr/sys/printk.h>
static inline void spi_dump_message(const uint8_t *pre, uint8_t *buf,
uint8_t size)
{
uint8_t i, c;

printk("%s (%d): ", pre, size);
for (i = 0U; i < size; i++) {
c = buf[i];
printk("%x ", c);
if (c >= 31U && c <= 126U) {
printk("[%c] ", c);
} else {
printk("[.] ");
}
}
printk("\n");
}
#else
static inline
void spi_dump_message(const uint8_t *pre, uint8_t *buf, uint8_t size) {}
#endif

#if defined(CONFIG_BT_SPI_BLUENRG)
/* Define a limit when reading IRQ high */
/* It can be required to be increased for */
Expand All @@ -129,17 +106,8 @@ struct bluenrg_aci_cmd_ll_param {
static int bt_spi_send_aci_config_data_controller_mode(void);
#endif /* CONFIG_BT_BLUENRG_ACI */

#if defined(CONFIG_BT_SPI_BLUENRG)
/* In case of BlueNRG-MS, it is necessary to prevent SPI driver to release CS,
* and instead, let current driver manage CS release. see kick_cs()/release_cs()
* So, add SPI_HOLD_ON_CS to operation field.
*/
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_HOLD_ON_CS, 0);
#else
static const struct spi_dt_spec bus = SPI_DT_SPEC_INST_GET(
0, SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(8), 0);
#endif

static struct spi_buf spi_tx_buf;
static struct spi_buf spi_rx_buf;
Expand Down Expand Up @@ -208,12 +176,6 @@ static bool bt_spi_handle_vendor_evt(uint8_t *msg)
* know the amount of byte to read.
* (See section 5.2 of BlueNRG-MS datasheet)
*/
static int configure_cs(void)
{
/* Configure pin as output and set to inactive */
return gpio_pin_configure_dt(&bus.config.cs.gpio, GPIO_OUTPUT_INACTIVE);
}

static void kick_cs(void)
{
gpio_pin_set_dt(&bus.config.cs.gpio, 0);
Expand Down Expand Up @@ -253,7 +215,6 @@ static bool exit_irq_high_loop(void)

#else

#define configure_cs(...) 0
#define kick_cs(...)
#define release_cs(...)
#define irq_pin_high(...) 0
Expand Down Expand Up @@ -304,8 +265,8 @@ static struct net_buf *bt_spi_rx_buf_construct(uint8_t *msg)
/* Event has not yet been handled */
__fallthrough;
default:
if (msg[1] == BT_HCI_EVT_LE_META_EVENT &&
(msg[3] == BT_HCI_EVT_LE_ADVERTISING_REPORT)) {
if (msg[EVT_HEADER_EVENT] == BT_HCI_EVT_LE_META_EVENT &&
(msg[EVT_LE_META_SUBEVENT] == BT_HCI_EVT_LE_ADVERTISING_REPORT)) {
discardable = true;
timeout = K_NO_WAIT;
}
Expand Down Expand Up @@ -400,7 +361,7 @@ static void bt_spi_rx_thread(void)
continue;
}

spi_dump_message("RX:ed", rxmsg, size);
LOG_HEXDUMP_DBG(rxmsg, size, "SPI RX");

/* Construct net_buf from SPI data */
buf = bt_spi_rx_buf_construct(rxmsg);
Expand Down Expand Up @@ -476,7 +437,7 @@ static int bt_spi_send(struct net_buf *buf)
goto out;
}

spi_dump_message("TX:ed", buf->data, buf->len);
LOG_HEXDUMP_DBG(buf->data, buf->len, "SPI TX");

#if defined(CONFIG_BT_SPI_BLUENRG)
/*
Expand Down Expand Up @@ -556,10 +517,6 @@ static int bt_spi_init(void)
return -ENODEV;
}

if (configure_cs()) {
return -EIO;
}

if (!gpio_is_ready_dt(&irq_gpio)) {
LOG_ERR("IRQ GPIO device not ready");
return -ENODEV;
Expand Down
2 changes: 1 addition & 1 deletion samples/bluetooth/hci_usb/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tests:
- usb
- bluetooth
# FIXME: exclude due to build error
platform_exclude: 96b_carbon
platform_exclude: 96b_carbon stm32l562e_dk
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this because of USB?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Since the platform is running a 2-chip configuration where the controller part is running on BlueNRG-MS using an SPI interface, and the STM32L4xxxx is running the host part connected via SPI to the controller, therefore, the hci_usb protocol does not apply to this platform.

sample.bluetooth.hci_usb.device_next:
harness: bluetooth
depends_on:
Expand Down
2 changes: 1 addition & 1 deletion samples/bluetooth/hci_usb_h4/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ tests:
- usb
- bluetooth
# FIXME: exclude due to build error
platform_exclude: 96b_carbon
platform_exclude: 96b_carbon stm32l562e_dk
Loading