Skip to content

Commit

Permalink
samples/bluetooth: Move hci_spi to new SPI API
Browse files Browse the repository at this point in the history
Removing old API usage.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
  • Loading branch information
Tomasz Bursztyka authored and carlescufi committed Apr 4, 2018
1 parent d5e6874 commit e7de85b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
1 change: 0 additions & 1 deletion samples/bluetooth/hci_spi/prj.conf
@@ -1,6 +1,5 @@
CONFIG_GPIO=y
CONFIG_SPI=y
CONFIG_SPI_LEGACY_API=y
CONFIG_MAIN_STACK_SIZE=512
CONFIG_BT=y
CONFIG_BT_HCI_RAW=y
Expand Down
69 changes: 47 additions & 22 deletions samples/bluetooth/hci_spi/src/main.c
Expand Up @@ -55,7 +55,18 @@
#define SPI_MAX_MSG_LEN 255

static u8_t rxmsg[SPI_MAX_MSG_LEN];
static struct spi_buf rx;
const static struct spi_buf_set rx_bufs = {
.buffers = &rx,
.count = 1,
};

static u8_t txmsg[SPI_MAX_MSG_LEN];
static struct spi_buf tx;
const static struct spi_buf_set tx_bufs = {
.buffers = &tx,
.count = 1,
};

/* HCI buffer pools */
#define CMD_BUF_SIZE BT_BUF_RX_SIZE
Expand Down Expand Up @@ -83,6 +94,9 @@ NET_BUF_POOL_DEFINE(acl_tx_pool, TX_BUF_COUNT, BT_BUF_ACL_SIZE,
BT_BUF_USER_DATA_MIN, NULL);

static struct device *spi_hci_dev;
static struct spi_config spi_cfg = {
.operation = SPI_WORD_SET(8),
};
static struct device *gpio_dev;
static BT_STACK_NOINIT(bt_tx_thread_stack, CONFIG_BT_HCI_TX_STACK_SIZE);
static struct k_thread bt_tx_thread_data;
Expand All @@ -92,11 +106,12 @@ static K_SEM_DEFINE(sem_spi_tx, 0, 1);

static inline int spi_send(struct net_buf *buf)
{
u8_t header_master[5] = { 0 };
u8_t header_slave[5] = { READY_NOW, SANITY_CHECK,
0x00, 0x00, 0x00 };
int ret;

txmsg[0] = READY_NOW;
txmsg[1] = SANITY_CHECK;
txmsg[2] = 0x00;

SYS_LOG_DBG("buf %p type %u len %u", buf, bt_buf_get_type(buf),
buf->len);

Expand All @@ -118,22 +133,30 @@ static inline int spi_send(struct net_buf *buf)
net_buf_unref(buf);
return -EINVAL;
}
header_slave[STATUS_HEADER_TOREAD] = buf->len;

txmsg[STATUS_HEADER_TOREAD] = buf->len;
txmsg[4] = 0x00;

tx.buf = txmsg;
tx.len = 5;
rx.buf = rxmsg;
rx.len = 5;

gpio_pin_write(gpio_dev, GPIO_IRQ_PIN, 1);

/* Coordinate transfer lock with the spi rx thread */
k_sem_take(&sem_spi_tx, K_FOREVER);
do {
ret = spi_transceive(spi_hci_dev, header_slave, 5,
header_master, 5);
ret = spi_transceive(spi_hci_dev, &spi_cfg, &tx_bufs, &rx_bufs);
if (ret < 0) {
SYS_LOG_ERR("SPI transceive error: %d", ret);
}
} while (header_master[STATUS_HEADER_READY] != SPI_READ);
} while (rxmsg[STATUS_HEADER_READY] != SPI_READ);

tx.buf = buf->data;
tx.len = buf->len;

ret = spi_transceive(spi_hci_dev, buf->data, buf->len,
&rxmsg, buf->len);
ret = spi_write(spi_hci_dev, &spi_cfg, &tx_bufs);
if (ret < 0) {
SYS_LOG_ERR("SPI transceive error: %d", ret);
}
Expand All @@ -159,12 +182,17 @@ static void bt_tx_thread(void *p1, void *p2, void *p3)
ARG_UNUSED(p2);
ARG_UNUSED(p3);

memset(&txmsg, 0xFF, SPI_MAX_MSG_LEN);
memset(txmsg, 0xFF, SPI_MAX_MSG_LEN);

while (1) {
tx.buf = header_slave;
tx.len = 5;
rx.buf = header_master;
rx.len = 5;

do {
ret = spi_transceive(spi_hci_dev, header_slave, 5,
header_master, 5);
ret = spi_transceive(spi_hci_dev, &spi_cfg,
&tx_bufs, &rx_bufs);
if (ret < 0) {
SYS_LOG_ERR("SPI transceive error: %d", ret);
}
Expand All @@ -178,9 +206,14 @@ static void bt_tx_thread(void *p1, void *p2, void *p3)
continue;
}

tx.buf = txmsg;
tx.len = SPI_MAX_MSG_LEN;
rx.buf = rxmsg;
rx.len = SPI_MAX_MSG_LEN;

/* Receiving data from the SPI Host */
ret = spi_transceive(spi_hci_dev, &txmsg, SPI_MAX_MSG_LEN,
&rxmsg, SPI_MAX_MSG_LEN);
ret = spi_transceive(spi_hci_dev, &spi_cfg,
&tx_bufs, &rx_bufs);
if (ret < 0) {
SYS_LOG_ERR("SPI transceive error: %d", ret);
continue;
Expand Down Expand Up @@ -240,10 +273,6 @@ static void bt_tx_thread(void *p1, void *p2, void *p3)

static int hci_spi_init(struct device *unused)
{
static struct spi_config btspi_config = {
.config = SPI_WORD(8),
};

ARG_UNUSED(unused);

SYS_LOG_DBG("");
Expand All @@ -253,10 +282,6 @@ static int hci_spi_init(struct device *unused)
return -EINVAL;
}

if (spi_configure(spi_hci_dev, &btspi_config) < 0) {
return -EINVAL;
}

gpio_dev = device_get_binding(GPIO_IRQ_DEV_NAME);
if (!gpio_dev) {
return -EINVAL;
Expand Down

0 comments on commit e7de85b

Please sign in to comment.