From bd5c2c07b315f54c91213334b4a9eb2879279e61 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Thu, 13 Jul 2017 12:01:19 -0400 Subject: [PATCH] Bluetooth: hci: spi: abstract out legacy SPI API Some targets (e.g. STM32-based boards) only have SPI drivers using the new API. To support both these and existing, legacy SPI drivers in the SPI HCI driver, abstract out the SPI API into shim routines. There are no behavioral differences due to this patch. The next patch will add support for the new API. Signed-off-by: Marti Bolivar --- drivers/bluetooth/hci/spi.c | 48 +++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/drivers/bluetooth/hci/spi.c b/drivers/bluetooth/hci/spi.c index 3033f8eb1ff..10a1f77c55f 100644 --- a/drivers/bluetooth/hci/spi.c +++ b/drivers/bluetooth/hci/spi.c @@ -52,7 +52,7 @@ /* Max SPI buffer length for transceive operations. * * Buffer size needs to be at least the size of the larger RX/TX buffer - * required by the SPI slave, as spi_transceive requires both RX/TX + * required by the SPI slave, as the legacy spi_transceive requires both RX/TX * 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). */ @@ -61,7 +61,6 @@ static u8_t rxmsg[SPI_MAX_MSG_LEN]; static u8_t txmsg[SPI_MAX_MSG_LEN]; -static struct device *spi_dev; #if defined(CONFIG_BT_SPI_BLUENRG) static struct device *cs_dev; #endif /* CONFIG_BT_SPI_BLUENRG */ @@ -77,11 +76,6 @@ static K_SEM_DEFINE(sem_busy, 1, 1); static BT_STACK_NOINIT(rx_stack, 448); static struct k_thread rx_thread_data; -static struct spi_config spi_conf = { - .config = SPI_WORD(8), - .max_sys_freq = CONFIG_BT_SPI_MAX_CLK_FREQ, -}; - #if defined(CONFIG_BT_DEBUG_HCI_DRIVER) #include static inline void spi_dump_message(const u8_t *pre, u8_t *buf, @@ -106,6 +100,35 @@ static inline void spi_dump_message(const u8_t *pre, u8_t *buf, u8_t size) {} #endif +/* + * SPI driver shim. + * + * This can be removed and this driver further improved when the + * legacy SPI API is gone. (For example, the chip select handling done + * under CONFIG_BT_SPI_BLUENRG could be done with a struct + * spi_cs_control instead). + */ +static struct device *spi_dev; +#if defined(CONFIG_SPI_LEGACY_API) +static struct spi_config spi_conf = { + .config = SPI_WORD(8), + .max_sys_freq = CONFIG_BT_SPI_MAX_CLK_FREQ, +}; + +static inline int bt_spi_dev_configure(void) +{ + return spi_configure(spi_dev, &spi_conf); +} + +static inline int bt_spi_transceive(const void *tx, u32_t tx_len, + void *rx, u32_t rx_len) +{ + return spi_transceive(spi_dev, tx, tx_len, rx, rx_len); +} +#else +/* TODO add support for the new SPI API */ +#endif + static inline u16_t bt_spi_get_cmd(u8_t *txmsg) { return (txmsg[CMD_OCF] << 8) | txmsg[CMD_OGF]; @@ -153,14 +176,13 @@ static void bt_spi_rx_thread(void) gpio_pin_write(cs_dev, GPIO_CS_PIN, 1); gpio_pin_write(cs_dev, GPIO_CS_PIN, 0); #endif /* CONFIG_BT_SPI_BLUENRG */ - spi_transceive(spi_dev, - header_master, 5, header_slave, 5); + bt_spi_transceive(header_master, 5, header_slave, 5); } while (header_slave[STATUS_HEADER_TOREAD] == 0 || header_slave[STATUS_HEADER_TOREAD] == 0xFF); size = header_slave[STATUS_HEADER_TOREAD]; do { - spi_transceive(spi_dev, &txmsg, size, &rxmsg, size); + bt_spi_transceive(&txmsg, size, &rxmsg, size); } while (rxmsg[0] == 0); gpio_pin_enable_callback(irq_dev, GPIO_IRQ_PIN); @@ -252,7 +274,7 @@ static int bt_spi_send(struct net_buf *buf) gpio_pin_write(cs_dev, GPIO_CS_PIN, 1); gpio_pin_write(cs_dev, GPIO_CS_PIN, 0); #endif /* CONFIG_BT_SPI_BLUENRG */ - spi_transceive(spi_dev, header, 5, rxmsg, 5); + bt_spi_transceive(header, 5, rxmsg, 5); /* * RX Header (rxmsg) must contain a sanity check Byte and size @@ -264,7 +286,7 @@ static int bt_spi_send(struct net_buf *buf) /* Transmit the message */ do { - spi_transceive(spi_dev, buf->data, buf->len, rxmsg, buf->len); + bt_spi_transceive(buf->data, buf->len, rxmsg, buf->len); } while (rxmsg[0] == 0); #if defined(CONFIG_BT_SPI_BLUENRG) @@ -300,7 +322,7 @@ static int bt_spi_open(void) GPIO_DIR_OUT | GPIO_PUD_PULL_UP); gpio_pin_write(rst_dev, GPIO_RESET_PIN, 0); - spi_configure(spi_dev, &spi_conf); + bt_spi_dev_configure(); #if defined(CONFIG_BT_SPI_BLUENRG) /* Configure the CS (Chip Select) pin */