samples: hci_spi: clean up
Move to the newer device getters and clean up the documentation.
Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
diff --git a/samples/bluetooth/hci_spi/README.rst b/samples/bluetooth/hci_spi/README.rst
index e82b3c4..5ffc339 100644
--- a/samples/bluetooth/hci_spi/README.rst
+++ b/samples/bluetooth/hci_spi/README.rst
@@ -12,22 +12,19 @@
Requirements
************
-* A board with SPI slave, GPIO and BLE support.
+A board with SPI slave, GPIO and Bluetooth Low Energy support.
Building and Running
********************
-In order to use this application, you need a board with a Bluetooth
-controller and SPI slave drivers, and a spare GPIO to use as an
-interrupt line to the SPI master.
+You then need to ensure that your :ref:`devicetree <dt-guide>` defines a node
+for the HCI SPI slave device with compatible
+:dtcompatible:`zephyr,bt-hci-spi-slave`. This node sets an interrupt line to
+the host and associates the application with a SPI bus to use.
-You then need to ensure that your :ref:`devicetree <dt-guide>`
-settings provide a definition for the slave HCI SPI device::
-
- bt-hci@0 {
- compatible = "zephyr,bt-hci-spi-slave";
- ...
- };
+See :zephyr_file:`boards/nrf51dk_nrf51422.overlay
+<samples/bluetooth/hci_spi/boards/nrf51dk_nrf51422.overlay>` in this sample
+directory for an example overlay for the :ref:`nrf51dk_nrf51422` board.
You can then build this application and flash it onto your board in
the usual way; see :ref:`boards` for board-specific building and
@@ -36,7 +33,7 @@
You will also need a separate chip acting as BT HCI SPI master. This
application is compatible with the HCI SPI master driver provided by
Zephyr's Bluetooth HCI driver core; see the help associated with the
-BT_SPI configuration option for more information.
+:option:`CONFIG_BT_SPI` configuration option for more information.
Refer to :ref:`bluetooth-samples` for general Bluetooth information, and
to :ref:`96b_carbon_nrf51_bluetooth` for instructions specific to the
diff --git a/samples/bluetooth/hci_spi/src/main.c b/samples/bluetooth/hci_spi/src/main.c
index 3525585..41100be 100644
--- a/samples/bluetooth/hci_spi/src/main.c
+++ b/samples/bluetooth/hci_spi/src/main.c
@@ -48,9 +48,6 @@
#define PACKET_TYPE 0
#define EVT_BLUE_INITIALIZED 0x01
-#define GPIO_IRQ_PIN DT_GPIO_PIN(DT_INST(0, zephyr_bt_hci_spi_slave), irq_gpios)
-#define GPIO_IRQ_FLAGS DT_GPIO_FLAGS(DT_INST(0, zephyr_bt_hci_spi_slave), irq_gpios)
-
/* Needs to be aligned with the SPI master buffer size */
#define SPI_MAX_MSG_LEN 255
@@ -71,11 +68,32 @@
/* HCI buffer pools */
#define CMD_BUF_SIZE BT_BUF_RX_SIZE
-static const struct device *spi_hci_dev;
+/*
+ * This finds an arbitrary node with compatible
+ * "zephyr,bt-hci-spi-slave". There should just be one in the
+ * devicetree.
+ *
+ * If for some reason you have more than one of these in your
+ * devicetree, replace this macro definition to pick one, e.g. using
+ * DT_NODELABEL().
+ */
+#define HCI_SPI_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(zephyr_bt_hci_spi_slave)
+
+/*
+ * This is the SPI bus controller device used to exchange data with
+ * the SPI-based BT controller.
+ */
+static const struct device *spi_hci_dev = DEVICE_DT_GET(DT_BUS(HCI_SPI_NODE));
static struct spi_config spi_cfg = {
.operation = SPI_WORD_SET(8) | SPI_OP_MODE_SLAVE,
};
-static const struct device *gpio_dev;
+
+/*
+ * The GPIO used to send interrupts to the host,
+ * configured in the 'irq-gpios' property in HCI_SPI_NODE.
+ */
+static const struct gpio_dt_spec irq = GPIO_DT_SPEC_GET(HCI_SPI_NODE, irq_gpios);
+
static K_THREAD_STACK_DEFINE(bt_tx_thread_stack, CONFIG_BT_HCI_TX_STACK_SIZE);
static struct k_thread bt_tx_thread_data;
@@ -111,7 +129,7 @@
}
header_slave[STATUS_HEADER_TOREAD] = buf->len;
- gpio_pin_set(gpio_dev, GPIO_IRQ_PIN, 1);
+ gpio_pin_set(irq.port, irq.pin, 1);
/* Coordinate transfer lock with the spi rx thread */
k_sem_take(&sem_spi_tx, K_FOREVER);
@@ -136,7 +154,7 @@
}
net_buf_unref(buf);
- gpio_pin_set(gpio_dev, GPIO_IRQ_PIN, 0);
+ gpio_pin_set(irq.port, irq.pin, 0);
k_sem_give(&sem_spi_rx);
return 0;
@@ -247,18 +265,16 @@
LOG_DBG("");
- spi_hci_dev = device_get_binding(DT_BUS_LABEL(DT_INST(0, zephyr_bt_hci_spi_slave)));
- if (!spi_hci_dev) {
+ if (!device_is_ready(spi_hci_dev)) {
+ LOG_ERR("SPI bus %s is not ready", spi_hci_dev->name);
return -EINVAL;
}
- gpio_dev = device_get_binding(
- DT_GPIO_LABEL(DT_INST(0, zephyr_bt_hci_spi_slave), irq_gpios));
- if (!gpio_dev) {
+ if (!device_is_ready(irq.port)) {
+ LOG_ERR("IRQ GPIO port %s is not ready", irq.port->name);
return -EINVAL;
}
- gpio_pin_configure(gpio_dev, GPIO_IRQ_PIN,
- GPIO_OUTPUT_INACTIVE | GPIO_IRQ_FLAGS);
+ gpio_pin_configure_dt(&irq, GPIO_OUTPUT_INACTIVE);
return 0;
}