2017-01-09 14:16:07 +00:00
|
|
|
/* spi.c - SPI based Bluetooth driver */
|
|
|
|
|
2020-03-25 12:10:28 -05:00
|
|
|
#define DT_DRV_COMPAT zephyr_bt_hci_spi
|
|
|
|
|
2017-01-09 14:16:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Linaro Ltd.
|
|
|
|
*
|
2017-01-19 08:02:14 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2017-01-09 14:16:07 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/drivers/gpio.h>
|
|
|
|
#include <zephyr/init.h>
|
|
|
|
#include <zephyr/drivers/spi.h>
|
|
|
|
#include <zephyr/sys/byteorder.h>
|
|
|
|
#include <zephyr/sys/util.h>
|
2017-01-09 14:16:07 +00:00
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/bluetooth/hci.h>
|
2024-06-01 10:46:50 +03:00
|
|
|
#include <zephyr/drivers/bluetooth.h>
|
2017-01-09 14:16:07 +00:00
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
#define LOG_LEVEL CONFIG_BT_HCI_DRIVER_LOG_LEVEL
|
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(bt_driver);
|
2017-05-10 16:27:16 +02:00
|
|
|
|
2017-01-09 14:16:07 +00:00
|
|
|
/* Special Values */
|
|
|
|
#define SPI_WRITE 0x0A
|
|
|
|
#define SPI_READ 0x0B
|
|
|
|
#define READY_NOW 0x02
|
|
|
|
|
|
|
|
#define EVT_BLUE_INITIALIZED 0x01
|
|
|
|
|
|
|
|
/* Offsets */
|
|
|
|
#define STATUS_HEADER_READY 0
|
|
|
|
#define STATUS_HEADER_TOREAD 3
|
2023-11-22 12:54:21 +01:00
|
|
|
#define STATUS_HEADER_TOWRITE 1
|
2017-01-09 14:16:07 +00:00
|
|
|
|
2017-01-26 00:56:04 -02:00
|
|
|
#define PACKET_TYPE 0
|
2017-01-09 14:16:07 +00:00
|
|
|
#define EVT_HEADER_TYPE 0
|
|
|
|
#define EVT_HEADER_EVENT 1
|
|
|
|
#define EVT_HEADER_SIZE 2
|
2023-10-27 14:17:34 +02:00
|
|
|
#define EVT_LE_META_SUBEVENT 3
|
2017-01-09 14:16:07 +00:00
|
|
|
#define EVT_VENDOR_CODE_LSB 3
|
|
|
|
#define EVT_VENDOR_CODE_MSB 4
|
|
|
|
|
|
|
|
#define CMD_OGF 1
|
|
|
|
#define CMD_OCF 2
|
|
|
|
|
2017-01-27 18:17:01 -02:00
|
|
|
/* Max SPI buffer length for transceive operations.
|
|
|
|
*
|
|
|
|
* Buffer size needs to be at least the size of the larger RX/TX buffer
|
2017-07-13 12:01:19 -04:00
|
|
|
* required by the SPI slave, as the legacy spi_transceive requires both RX/TX
|
2017-01-27 18:17:01 -02:00
|
|
|
* to be the same length. Size also needs to be compatible with the
|
|
|
|
* slave device used (e.g. nRF5X max buffer length for SPIS is 255).
|
|
|
|
*/
|
|
|
|
#define SPI_MAX_MSG_LEN 255 /* As defined by X-NUCLEO-IDB04A1 BSP */
|
|
|
|
|
2023-06-16 13:20:02 +10:00
|
|
|
#define DATA_DELAY_US DT_INST_PROP(0, controller_data_delay_us)
|
|
|
|
|
2023-05-09 20:57:32 +10:00
|
|
|
/* Single byte header denoting the buffer type */
|
|
|
|
#define H4_HDR_SIZE 1
|
|
|
|
|
|
|
|
/* Maximum L2CAP MTU that can fit in a single packet */
|
|
|
|
#define MAX_MTU (SPI_MAX_MSG_LEN - H4_HDR_SIZE - BT_L2CAP_HDR_SIZE - BT_HCI_ACL_HDR_SIZE)
|
|
|
|
|
|
|
|
#if CONFIG_BT_L2CAP_TX_MTU > MAX_MTU
|
|
|
|
#warning CONFIG_BT_L2CAP_TX_MTU is too large and can result in packets that cannot \
|
|
|
|
be transmitted across this HCI link
|
|
|
|
#endif /* CONFIG_BT_L2CAP_TX_MTU > MAX_MTU */
|
|
|
|
|
2024-06-01 10:46:50 +03:00
|
|
|
struct bt_spi_data {
|
|
|
|
bt_hci_recv_t recv;
|
|
|
|
};
|
|
|
|
|
2024-01-14 00:02:12 +07:00
|
|
|
static uint8_t __noinit rxmsg[SPI_MAX_MSG_LEN];
|
|
|
|
static uint8_t __noinit txmsg[SPI_MAX_MSG_LEN];
|
2017-01-09 14:16:07 +00:00
|
|
|
|
2022-01-31 10:59:05 +01:00
|
|
|
static const struct gpio_dt_spec irq_gpio = GPIO_DT_SPEC_INST_GET(0, irq_gpios);
|
|
|
|
static const struct gpio_dt_spec rst_gpio = GPIO_DT_SPEC_INST_GET(0, reset_gpios);
|
2017-01-09 14:16:07 +00:00
|
|
|
|
|
|
|
static struct gpio_callback gpio_cb;
|
|
|
|
|
|
|
|
static K_SEM_DEFINE(sem_initialised, 0, 1);
|
|
|
|
static K_SEM_DEFINE(sem_request, 0, 1);
|
|
|
|
static K_SEM_DEFINE(sem_busy, 1, 1);
|
|
|
|
|
2023-02-22 08:45:15 +01:00
|
|
|
static K_KERNEL_STACK_DEFINE(spi_rx_stack, CONFIG_BT_DRV_RX_STACK_SIZE);
|
2019-06-11 12:07:27 +02:00
|
|
|
static struct k_thread spi_rx_thread_data;
|
2017-01-09 14:16:07 +00:00
|
|
|
|
2022-01-31 10:52:09 +01:00
|
|
|
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);
|
2017-07-13 12:01:19 -04:00
|
|
|
|
2017-07-13 12:22:52 -04:00
|
|
|
static struct spi_buf spi_tx_buf;
|
|
|
|
static struct spi_buf spi_rx_buf;
|
2018-01-31 09:54:54 +01:00
|
|
|
static const struct spi_buf_set spi_tx = {
|
|
|
|
.buffers = &spi_tx_buf,
|
|
|
|
.count = 1
|
|
|
|
};
|
|
|
|
static const struct spi_buf_set spi_rx = {
|
|
|
|
.buffers = &spi_rx_buf,
|
|
|
|
.count = 1
|
|
|
|
};
|
2017-07-13 12:22:52 -04:00
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
static inline int bt_spi_transceive(void *tx, uint32_t tx_len,
|
|
|
|
void *rx, uint32_t rx_len)
|
2017-07-13 12:22:52 -04:00
|
|
|
{
|
2018-01-31 09:54:54 +01:00
|
|
|
spi_tx_buf.buf = tx;
|
2017-07-13 12:22:52 -04:00
|
|
|
spi_tx_buf.len = (size_t)tx_len;
|
|
|
|
spi_rx_buf.buf = rx;
|
|
|
|
spi_rx_buf.len = (size_t)rx_len;
|
2022-01-31 10:52:09 +01:00
|
|
|
return spi_transceive_dt(&bus, &spi_tx, &spi_rx);
|
2017-07-13 12:22:52 -04:00
|
|
|
}
|
2017-07-13 12:01:19 -04:00
|
|
|
|
2023-08-09 12:55:53 -07:00
|
|
|
static inline uint16_t bt_spi_get_cmd(uint8_t *msg)
|
2017-01-09 14:16:07 +00:00
|
|
|
{
|
2023-08-09 12:55:53 -07:00
|
|
|
return (msg[CMD_OCF] << 8) | msg[CMD_OGF];
|
2017-01-09 14:16:07 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 12:55:53 -07:00
|
|
|
static inline uint16_t bt_spi_get_evt(uint8_t *msg)
|
2017-01-09 14:16:07 +00:00
|
|
|
{
|
2023-08-09 12:55:53 -07:00
|
|
|
return (msg[EVT_VENDOR_CODE_MSB] << 8) | msg[EVT_VENDOR_CODE_LSB];
|
2017-01-09 14:16:07 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 17:22:15 +02:00
|
|
|
static void bt_spi_isr(const struct device *unused1,
|
2020-04-30 20:33:38 +02:00
|
|
|
struct gpio_callback *unused2,
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t unused3)
|
2017-01-09 14:16:07 +00:00
|
|
|
{
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("");
|
2018-02-02 08:09:34 +01:00
|
|
|
|
2017-01-09 14:16:07 +00:00
|
|
|
k_sem_give(&sem_request);
|
|
|
|
}
|
|
|
|
|
2023-08-09 12:55:53 -07:00
|
|
|
static bool bt_spi_handle_vendor_evt(uint8_t *msg)
|
2017-01-09 14:16:07 +00:00
|
|
|
{
|
2022-10-07 10:16:50 +10:00
|
|
|
bool handled = false;
|
|
|
|
|
2023-08-09 12:55:53 -07:00
|
|
|
switch (bt_spi_get_evt(msg)) {
|
2023-11-14 14:15:10 +00:00
|
|
|
case EVT_BLUE_INITIALIZED: {
|
2017-01-09 14:16:07 +00:00
|
|
|
k_sem_give(&sem_initialised);
|
2022-10-07 10:16:50 +10:00
|
|
|
handled = true;
|
2023-11-14 14:15:10 +00:00
|
|
|
}
|
2017-01-09 14:16:07 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2022-10-07 10:16:50 +10:00
|
|
|
return handled;
|
2017-01-09 14:16:07 +00:00
|
|
|
}
|
|
|
|
|
2023-11-22 13:01:32 +01:00
|
|
|
static int bt_spi_get_header(uint8_t op, uint16_t *size)
|
2023-11-21 10:23:10 +01:00
|
|
|
{
|
|
|
|
uint8_t header_master[5] = {op, 0, 0, 0, 0};
|
|
|
|
uint8_t header_slave[5];
|
|
|
|
bool reading = (op == SPI_READ);
|
|
|
|
bool loop_cond;
|
|
|
|
uint8_t size_offset;
|
|
|
|
int ret;
|
2018-06-26 13:30:26 +02:00
|
|
|
|
2024-01-02 10:39:46 +01:00
|
|
|
if (!(op == SPI_READ || op == SPI_WRITE)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2023-11-21 10:23:10 +01:00
|
|
|
if (reading) {
|
|
|
|
size_offset = STATUS_HEADER_TOREAD;
|
|
|
|
}
|
2018-06-26 13:30:26 +02:00
|
|
|
|
2023-11-21 10:23:10 +01:00
|
|
|
do {
|
|
|
|
ret = bt_spi_transceive(header_master, 5, header_slave, 5);
|
|
|
|
if (ret) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (reading) {
|
2024-02-27 10:46:16 +10:00
|
|
|
/* When reading, keep looping if there is not yet any data */
|
|
|
|
loop_cond = header_slave[STATUS_HEADER_TOREAD] == 0U;
|
2023-11-22 12:54:21 +01:00
|
|
|
} else {
|
|
|
|
/* When writing, keep looping if all bytes are zero */
|
|
|
|
loop_cond = ((header_slave[1] | header_slave[2] | header_slave[3] |
|
|
|
|
header_slave[4]) == 0U);
|
2023-11-21 10:23:10 +01:00
|
|
|
}
|
|
|
|
} while ((header_slave[STATUS_HEADER_READY] != READY_NOW) || loop_cond);
|
2019-01-30 15:17:06 +01:00
|
|
|
|
2023-11-21 10:23:10 +01:00
|
|
|
*size = (reading ? header_slave[size_offset] : SPI_MAX_MSG_LEN);
|
2019-01-30 15:17:06 +01:00
|
|
|
|
2023-11-21 10:23:10 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2018-06-26 12:07:23 +02:00
|
|
|
|
2023-10-18 14:34:45 +10:00
|
|
|
static struct net_buf *bt_spi_rx_buf_construct(uint8_t *msg)
|
2017-01-09 14:16:07 +00:00
|
|
|
{
|
2020-12-22 01:42:48 -08:00
|
|
|
bool discardable = false;
|
|
|
|
k_timeout_t timeout = K_FOREVER;
|
2023-10-18 14:34:45 +10:00
|
|
|
struct bt_hci_acl_hdr acl_hdr;
|
2017-01-09 14:16:07 +00:00
|
|
|
struct net_buf *buf;
|
2023-10-18 14:34:45 +10:00
|
|
|
int len;
|
|
|
|
|
|
|
|
switch (msg[PACKET_TYPE]) {
|
2024-01-10 01:21:42 +07:00
|
|
|
case BT_HCI_H4_EVT:
|
2023-10-18 14:34:45 +10:00
|
|
|
switch (msg[EVT_HEADER_EVENT]) {
|
|
|
|
case BT_HCI_EVT_VENDOR:
|
|
|
|
/* Run event through interface handler */
|
|
|
|
if (bt_spi_handle_vendor_evt(msg)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Event has not yet been handled */
|
|
|
|
__fallthrough;
|
|
|
|
default:
|
2023-10-27 14:17:34 +02:00
|
|
|
if (msg[EVT_HEADER_EVENT] == BT_HCI_EVT_LE_META_EVENT &&
|
|
|
|
(msg[EVT_LE_META_SUBEVENT] == BT_HCI_EVT_LE_ADVERTISING_REPORT)) {
|
2023-10-18 14:34:45 +10:00
|
|
|
discardable = true;
|
|
|
|
timeout = K_NO_WAIT;
|
|
|
|
}
|
|
|
|
buf = bt_buf_get_evt(msg[EVT_HEADER_EVENT],
|
|
|
|
discardable, timeout);
|
|
|
|
if (!buf) {
|
|
|
|
LOG_DBG("Discard adv report due to insufficient buf");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = sizeof(struct bt_hci_evt_hdr) + msg[EVT_HEADER_SIZE];
|
|
|
|
if (len > net_buf_tailroom(buf)) {
|
|
|
|
LOG_ERR("Event too long: %d", len);
|
|
|
|
net_buf_unref(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
net_buf_add_mem(buf, &msg[1], len);
|
|
|
|
break;
|
2024-01-10 01:21:42 +07:00
|
|
|
case BT_HCI_H4_ACL:
|
2023-10-18 14:34:45 +10:00
|
|
|
buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_FOREVER);
|
|
|
|
memcpy(&acl_hdr, &msg[1], sizeof(acl_hdr));
|
|
|
|
len = sizeof(acl_hdr) + sys_le16_to_cpu(acl_hdr.len);
|
|
|
|
if (len > net_buf_tailroom(buf)) {
|
|
|
|
LOG_ERR("ACL too long: %d", len);
|
|
|
|
net_buf_unref(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
net_buf_add_mem(buf, &msg[1], len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERR("Unknown BT buf type %d", msg[0]);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2023-09-21 09:29:41 +02:00
|
|
|
static void bt_spi_rx_thread(void *p1, void *p2, void *p3)
|
2023-10-18 14:34:45 +10:00
|
|
|
{
|
2024-06-01 10:46:50 +03:00
|
|
|
const struct device *dev = p1;
|
|
|
|
struct bt_spi_data *hci = dev->data;
|
|
|
|
|
2023-09-21 09:29:41 +02:00
|
|
|
ARG_UNUSED(p2);
|
|
|
|
ARG_UNUSED(p3);
|
|
|
|
|
2023-10-18 14:34:45 +10:00
|
|
|
struct net_buf *buf;
|
2023-11-22 13:01:32 +01:00
|
|
|
uint16_t size = 0U;
|
2018-02-02 08:09:34 +01:00
|
|
|
int ret;
|
2017-01-25 23:19:44 -02:00
|
|
|
|
2018-09-11 19:09:03 -07:00
|
|
|
(void)memset(&txmsg, 0xFF, SPI_MAX_MSG_LEN);
|
2017-01-09 14:16:07 +00:00
|
|
|
while (true) {
|
2023-01-19 15:22:36 +10:00
|
|
|
|
|
|
|
/* Wait for interrupt pin to be active */
|
2017-01-09 14:16:07 +00:00
|
|
|
k_sem_take(&sem_request, K_FOREVER);
|
2020-01-29 08:46:16 -06:00
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("");
|
2017-01-09 14:16:07 +00:00
|
|
|
|
2024-01-05 18:11:05 +01:00
|
|
|
/* Wait for SPI bus to be available */
|
|
|
|
k_sem_take(&sem_busy, K_FOREVER);
|
|
|
|
ret = bt_spi_get_header(SPI_READ, &size);
|
2018-02-02 08:09:34 +01:00
|
|
|
|
2024-01-05 18:11:05 +01:00
|
|
|
/* Delay here is rounded up to next tick */
|
|
|
|
k_sleep(K_USEC(DATA_DELAY_US));
|
|
|
|
/* Read data */
|
|
|
|
if (ret == 0 && size != 0) {
|
|
|
|
do {
|
|
|
|
ret = bt_spi_transceive(&txmsg, size,
|
|
|
|
&rxmsg, size);
|
|
|
|
if (rxmsg[0] == 0U) {
|
|
|
|
/* Consider increasing controller-data-delay-us
|
|
|
|
* if this message is extremely common.
|
|
|
|
*/
|
|
|
|
LOG_DBG("Controller not ready for SPI transaction "
|
|
|
|
"of %d bytes", size);
|
|
|
|
}
|
|
|
|
} while (rxmsg[0] == 0U && ret == 0);
|
|
|
|
}
|
2020-01-29 08:46:16 -06:00
|
|
|
|
2024-01-05 18:11:05 +01:00
|
|
|
k_sem_give(&sem_busy);
|
2017-01-09 14:16:07 +00:00
|
|
|
|
2024-01-05 18:11:05 +01:00
|
|
|
if (ret || size == 0) {
|
|
|
|
if (ret) {
|
|
|
|
LOG_ERR("Error %d", ret);
|
2018-06-26 13:30:26 +02:00
|
|
|
}
|
2024-01-05 18:11:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-06-26 13:30:26 +02:00
|
|
|
|
2024-01-05 18:11:05 +01:00
|
|
|
LOG_HEXDUMP_DBG(rxmsg, size, "SPI RX");
|
2018-06-26 13:30:26 +02:00
|
|
|
|
2024-01-05 18:11:05 +01:00
|
|
|
/* Construct net_buf from SPI data */
|
|
|
|
buf = bt_spi_rx_buf_construct(rxmsg);
|
|
|
|
if (buf) {
|
|
|
|
/* Handle the received HCI data */
|
2024-06-01 10:46:50 +03:00
|
|
|
hci->recv(dev, buf);
|
2024-01-05 18:11:05 +01:00
|
|
|
}
|
2017-01-09 14:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-01 10:46:50 +03:00
|
|
|
static int bt_spi_send(const struct device *dev, struct net_buf *buf)
|
2017-01-09 14:16:07 +00:00
|
|
|
{
|
2023-11-22 13:01:32 +01:00
|
|
|
uint16_t size;
|
2023-10-19 15:47:47 +10:00
|
|
|
uint8_t rx_first[1];
|
2018-02-02 08:09:34 +01:00
|
|
|
int ret;
|
|
|
|
|
2024-06-01 10:46:50 +03:00
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("");
|
2017-01-09 14:16:07 +00:00
|
|
|
|
2017-01-27 18:17:01 -02:00
|
|
|
/* Buffer needs an additional byte for type */
|
|
|
|
if (buf->len >= SPI_MAX_MSG_LEN) {
|
2023-05-09 20:57:32 +10:00
|
|
|
LOG_ERR("Message too long (%d)", buf->len);
|
2017-01-09 14:16:07 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-05-22 16:02:20 +10:00
|
|
|
/* Wait for SPI bus to be available */
|
2017-01-09 14:16:07 +00:00
|
|
|
k_sem_take(&sem_busy, K_FOREVER);
|
|
|
|
|
2023-11-22 12:54:21 +01:00
|
|
|
ret = bt_spi_get_header(SPI_WRITE, &size);
|
|
|
|
size = MIN(buf->len, size);
|
2017-01-09 14:16:07 +00:00
|
|
|
|
2023-11-22 12:54:21 +01:00
|
|
|
if (size < buf->len) {
|
|
|
|
LOG_WRN("Unable to write full data, skipping");
|
2023-11-22 13:01:32 +01:00
|
|
|
size = 0;
|
2023-11-22 12:54:21 +01:00
|
|
|
ret = -ECANCELED;
|
|
|
|
}
|
2017-01-09 14:16:07 +00:00
|
|
|
|
2018-02-02 08:09:34 +01:00
|
|
|
if (!ret) {
|
2023-10-20 14:08:49 +10:00
|
|
|
/* Delay here is rounded up to next tick */
|
|
|
|
k_sleep(K_USEC(DATA_DELAY_US));
|
2018-02-02 08:09:34 +01:00
|
|
|
/* Transmit the message */
|
2023-10-20 14:00:41 +10:00
|
|
|
while (true) {
|
2023-11-22 12:54:21 +01:00
|
|
|
ret = bt_spi_transceive(buf->data, size,
|
2023-10-19 15:47:47 +10:00
|
|
|
rx_first, 1);
|
2024-01-05 18:11:05 +01:00
|
|
|
if (rx_first[0] != 0U || ret) {
|
2023-10-20 14:00:41 +10:00
|
|
|
break;
|
|
|
|
}
|
2023-10-20 14:08:49 +10:00
|
|
|
/* Consider increasing controller-data-delay-us
|
|
|
|
* if this message is extremely common.
|
|
|
|
*/
|
2023-11-22 12:54:21 +01:00
|
|
|
LOG_DBG("Controller not ready for SPI transaction of %d bytes", size);
|
2023-10-20 14:00:41 +10:00
|
|
|
}
|
2018-02-02 08:09:34 +01:00
|
|
|
}
|
|
|
|
|
2022-07-18 11:11:26 +08:00
|
|
|
k_sem_give(&sem_busy);
|
|
|
|
|
2018-02-02 08:09:34 +01:00
|
|
|
if (ret) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Error %d", ret);
|
2018-02-02 08:09:34 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:23:34 +02:00
|
|
|
LOG_HEXDUMP_DBG(buf->data, buf->len, "SPI TX");
|
2017-01-09 14:16:07 +00:00
|
|
|
|
2018-02-02 08:09:34 +01:00
|
|
|
out:
|
2017-01-09 14:16:07 +00:00
|
|
|
net_buf_unref(buf);
|
|
|
|
|
2018-02-02 08:09:34 +01:00
|
|
|
return ret;
|
2017-01-09 14:16:07 +00:00
|
|
|
}
|
|
|
|
|
2024-06-01 10:46:50 +03:00
|
|
|
static int bt_spi_open(const struct device *dev, bt_hci_recv_t recv)
|
2017-01-09 14:16:07 +00:00
|
|
|
{
|
2024-06-01 10:46:50 +03:00
|
|
|
struct bt_spi_data *hci = dev->data;
|
2023-08-03 16:13:38 +02:00
|
|
|
int err;
|
|
|
|
|
2025-02-10 08:47:45 +01:00
|
|
|
/* Configure RST pin and hold Bluetooth LE in Reset */
|
2023-08-03 16:13:38 +02:00
|
|
|
err = gpio_pin_configure_dt(&rst_gpio, GPIO_OUTPUT_ACTIVE);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
2017-01-09 14:16:07 +00:00
|
|
|
|
|
|
|
/* Configure IRQ pin and the IRQ call-back/handler */
|
2023-08-03 16:13:38 +02:00
|
|
|
err = gpio_pin_configure_dt(&irq_gpio, GPIO_INPUT);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
2017-01-09 14:16:07 +00:00
|
|
|
|
2023-07-14 17:22:15 +02:00
|
|
|
gpio_init_callback(&gpio_cb, bt_spi_isr, BIT(irq_gpio.pin));
|
2023-08-03 16:13:38 +02:00
|
|
|
err = gpio_add_callback(irq_gpio.port, &gpio_cb);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
2017-01-09 14:16:07 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 08:31:09 +02:00
|
|
|
/* Enable the interrupt line */
|
2023-11-23 18:19:54 +10:00
|
|
|
err = gpio_pin_interrupt_configure_dt(&irq_gpio, GPIO_INT_EDGE_TO_ACTIVE);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
2023-08-30 08:31:09 +02:00
|
|
|
|
2024-06-01 10:46:50 +03:00
|
|
|
hci->recv = recv;
|
|
|
|
|
2025-02-10 08:47:45 +01:00
|
|
|
/* Take Bluetooth LE out of reset */
|
2023-01-19 15:22:36 +10:00
|
|
|
k_sleep(K_MSEC(DT_INST_PROP_OR(0, reset_assert_duration_ms, 0)));
|
|
|
|
gpio_pin_set_dt(&rst_gpio, 0);
|
|
|
|
|
2017-01-09 14:16:07 +00:00
|
|
|
/* Start RX thread */
|
2019-06-11 12:07:27 +02:00
|
|
|
k_thread_create(&spi_rx_thread_data, spi_rx_stack,
|
2020-07-31 12:29:38 -07:00
|
|
|
K_KERNEL_STACK_SIZEOF(spi_rx_stack),
|
2024-06-01 10:46:50 +03:00
|
|
|
bt_spi_rx_thread, (void *)dev, NULL, NULL,
|
2020-08-19 17:16:01 +02:00
|
|
|
K_PRIO_COOP(CONFIG_BT_DRIVER_RX_HIGH_PRIO),
|
2017-07-11 09:47:08 +02:00
|
|
|
0, K_NO_WAIT);
|
2024-12-10 21:07:24 +00:00
|
|
|
k_thread_name_set(&spi_rx_thread_data, "bt_spi_rx_thread");
|
2017-01-09 14:16:07 +00:00
|
|
|
|
|
|
|
/* Device will let us know when it's ready */
|
2024-08-16 21:55:34 +10:00
|
|
|
if (k_sem_take(&sem_initialised, K_SECONDS(CONFIG_BT_SPI_BOOT_TIMEOUT_SEC)) < 0) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
2017-01-09 14:16:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-11-27 13:03:38 +01:00
|
|
|
static DEVICE_API(bt_hci, drv) = {
|
2017-01-09 14:16:07 +00:00
|
|
|
.open = bt_spi_open,
|
|
|
|
.send = bt_spi_send,
|
|
|
|
};
|
|
|
|
|
2024-06-01 10:46:50 +03:00
|
|
|
static int bt_spi_init(const struct device *dev)
|
2017-01-09 14:16:07 +00:00
|
|
|
{
|
2024-06-01 10:46:50 +03:00
|
|
|
ARG_UNUSED(dev);
|
2017-01-09 14:16:07 +00:00
|
|
|
|
2022-12-03 14:49:05 +01:00
|
|
|
if (!spi_is_ready_dt(&bus)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("SPI device not ready");
|
2022-01-31 10:52:09 +01:00
|
|
|
return -ENODEV;
|
2017-01-09 14:16:07 +00:00
|
|
|
}
|
|
|
|
|
2023-08-26 13:43:58 +10:00
|
|
|
if (!gpio_is_ready_dt(&irq_gpio)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("IRQ GPIO device not ready");
|
2022-01-31 10:59:05 +01:00
|
|
|
return -ENODEV;
|
2017-01-09 14:16:07 +00:00
|
|
|
}
|
|
|
|
|
2023-08-26 13:43:58 +10:00
|
|
|
if (!gpio_is_ready_dt(&rst_gpio)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Reset GPIO device not ready");
|
2022-01-31 10:59:05 +01:00
|
|
|
return -ENODEV;
|
2017-01-09 14:16:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("BT SPI initialized");
|
2018-02-02 08:09:34 +01:00
|
|
|
|
2017-01-09 14:16:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-06-01 10:46:50 +03:00
|
|
|
#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)
|