|
| 1 | +/* |
| 2 | + * Copyright 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/kernel.h> |
| 8 | +#include <zephyr/device.h> |
| 9 | +#include <zephyr/drivers/spi.h> |
| 10 | +#include <zephyr/drivers/gpio.h> |
| 11 | + |
| 12 | +#define SPI_DEV_NODE DT_PROP(DT_PATH(zephyr_user), spi) |
| 13 | + |
| 14 | +static const struct device *const spi_dev = DEVICE_DT_GET(SPI_DEV_NODE); |
| 15 | +static const struct gpio_dt_spec cs_pin = GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), cs_gpios); |
| 16 | + |
| 17 | +static uint8_t tx_buf[8]; |
| 18 | + |
| 19 | +static const struct spi_buf tx_buf_pool[1] = { |
| 20 | + { |
| 21 | + .buf = tx_buf, |
| 22 | + .len = sizeof(tx_buf), |
| 23 | + }, |
| 24 | +}; |
| 25 | + |
| 26 | +static const struct spi_buf_set spi_tx_buf = { |
| 27 | + .buffers = tx_buf_pool, |
| 28 | + .count = ARRAY_SIZE(tx_buf_pool), |
| 29 | +}; |
| 30 | + |
| 31 | +static const struct spi_config config = { |
| 32 | + .frequency = DT_PROP_OR(SPI_DEV_NODE, max_frequency, 1000000), |
| 33 | + .operation = SPI_OP_MODE_MASTER | SPI_HOLD_ON_CS | SPI_WORD_SET(8), |
| 34 | + .slave = 0, |
| 35 | + .cs = { |
| 36 | + .gpio = GPIO_DT_SPEC_GET_OR(SPI_DEV_NODE, cs_gpios, {0}), |
| 37 | + .delay = 0, |
| 38 | + }, |
| 39 | +}; |
| 40 | + |
| 41 | +static int control_cs_pin_with_gpio(void) |
| 42 | +{ |
| 43 | + int ret; |
| 44 | + |
| 45 | + ret = gpio_pin_configure_dt(&cs_pin, GPIO_OUTPUT_ACTIVE); |
| 46 | + if (ret) { |
| 47 | + printk("failed to configure CS pin\n"); |
| 48 | + return 0; |
| 49 | + } |
| 50 | + |
| 51 | + k_msleep(1000); |
| 52 | + |
| 53 | + ret = gpio_pin_configure_dt(&cs_pin, GPIO_INPUT); |
| 54 | + if (ret) { |
| 55 | + printk("failed to configure CS pin\n"); |
| 56 | + return 0; |
| 57 | + } |
| 58 | + |
| 59 | + k_msleep(1000); |
| 60 | + return 0; |
| 61 | +} |
| 62 | + |
| 63 | +static int control_cs_pin_with_spi(void) |
| 64 | +{ |
| 65 | + int ret; |
| 66 | + |
| 67 | + /* |
| 68 | + * Perform SPI transaction and keep CS pin asserted after |
| 69 | + * after transaction (see config.operation). |
| 70 | + */ |
| 71 | + ret = spi_transceive(spi_dev, &config, &spi_tx_buf, NULL); |
| 72 | + if (ret) { |
| 73 | + printk("failed to perform SPI transaction (ret %d)\n", ret); |
| 74 | + return 0; |
| 75 | + } |
| 76 | + |
| 77 | + k_msleep(1000); |
| 78 | + |
| 79 | + /* Release SPI device and thus CS pin shall be deasserted */ |
| 80 | + ret = spi_release(spi_dev, &config); |
| 81 | + if (ret) { |
| 82 | + printk("failed to release SPI device (ret %d)\n", ret); |
| 83 | + return 0; |
| 84 | + } |
| 85 | + |
| 86 | + k_msleep(1000); |
| 87 | + return 0; |
| 88 | +} |
| 89 | + |
| 90 | +int main(void) |
| 91 | +{ |
| 92 | + int ret; |
| 93 | + |
| 94 | + /* |
| 95 | + * As we have specified deferred init for the SPI device in the devicetree, the driver |
| 96 | + * will not be initialized at this stage. We should have full control of the CS GPIO, |
| 97 | + * let's test that. |
| 98 | + */ |
| 99 | + ret = control_cs_pin_with_gpio(); |
| 100 | + if (ret) { |
| 101 | + printk("failed to control CS pin with GPIO device driver (ret %d)\n", ret); |
| 102 | + return 0; |
| 103 | + } |
| 104 | + |
| 105 | + /* |
| 106 | + * Now lets initialize the SPI device driver. |
| 107 | + */ |
| 108 | + ret = device_init(spi_dev); |
| 109 | + if (ret) { |
| 110 | + printk("failed to init SPI device driver (ret %d)\n", ret); |
| 111 | + return 0; |
| 112 | + } |
| 113 | + |
| 114 | + /* |
| 115 | + * To control the CS pin in "human speed", lets perform a mock |
| 116 | + * transaction, holding the CS pin until we release the SPI device. |
| 117 | + */ |
| 118 | + ret = control_cs_pin_with_spi(); |
| 119 | + if (ret) { |
| 120 | + printk("failed to control CS pin with SPI device driver (ret %d)\n", ret); |
| 121 | + return 0; |
| 122 | + } |
| 123 | + |
| 124 | + /* |
| 125 | + * Lets deinit the SPI device driver an manually control the |
| 126 | + * CS pin again. |
| 127 | + */ |
| 128 | + ret = device_deinit(spi_dev); |
| 129 | + if (ret) { |
| 130 | + printk("failed to deinit SPI device driver (ret %d)\n", ret); |
| 131 | + return 0; |
| 132 | + } |
| 133 | + |
| 134 | + ret = control_cs_pin_with_gpio(); |
| 135 | + if (ret) { |
| 136 | + printk("failed to control CS pin with GPIO device driver (ret %d)\n", ret); |
| 137 | + return 0; |
| 138 | + } |
| 139 | + |
| 140 | + /* |
| 141 | + * Lastly, lets check if we can bring back the SPI device driver. |
| 142 | + */ |
| 143 | + ret = device_init(spi_dev); |
| 144 | + if (ret) { |
| 145 | + printk("failed to init SPI device driver (ret %d)\n", ret); |
| 146 | + return 0; |
| 147 | + } |
| 148 | + |
| 149 | + /* One final blink */ |
| 150 | + ret = control_cs_pin_with_spi(); |
| 151 | + if (ret) { |
| 152 | + printk("failed to control CS pin with SPI device driver (ret %d)\n", ret); |
| 153 | + return 0; |
| 154 | + } |
| 155 | + |
| 156 | + printk("sample complete\n"); |
| 157 | + return 0; |
| 158 | +} |
0 commit comments