How to Use Generic SPI Devices with Zephyr
If there’s a driver built into Zephyr, controlling a part over Serial Peripheral Interface (SPI) is a snap. But there’s an ocean of parts out there and only so many built-in drivers in existence. Today I’m going to show you how to use generic SPI devices with Zephyr so you can try writing your own test code in userspace, to be used for the long term or in preparation for creating a driver.
SPI is simple, right?
In practice, communicating with SPI devices is simple…but only after you get the hardware peripheral configured for your chip. Zephyr is built for cross-compatibility, but that is sometimes its biggest usability flaw. The SPI bus and the devices themselves are abstracted so much that it’s hard to know where to begin if you need to do it all yourself.
I spent far too long searching the internet for a guide before I remembered that all Zephyr systems have tests. Don’t be me, check the test files before you head to Google.
It turns out that the Zephyr SPI driver tests tell us how to register a generic SPI device, and then how to talk to it.
Zephyr SPI Step-by-Step
For this experiment I have chosen perhaps the easiest SPI chip ever, the Microchip MCP3201 12-bit ADC. No need to send register settings to that device, you simply enable the chip-select pin and toggle the clock signal to start shifting out ADC readings. In short: all I need is to read 4-bytes. That’s it!
1. Enable SPI in KConfig
First things first, we need to tell Zephyr to build SPI support into the app. We also need the ability to control GPIO pins. Add these to your prf.conf file:
CONFIG_GPIO=y CONFIG_SPI=y
Why enable a library unless you’re actually going to use it? Let’s add these line to main.c to ensure we can access all of the Zephyr SPI goodness:
#include <drivers/gpio.h> #include <drivers/spi.h>
2. Add a SPI node in the overlay file
Next, we need to tell Zephyr where to find our SPI device. The idiomatic way of doing this is to use the DeviceTree. For this project I’m using a Sparkfun Thing Plus nrf9160 so I needed to remap some pins for this device.
&spi2 { status = "okay"; cs-gpios = <&gpio0 18 GPIO_ACTIVE_LOW>; pinctrl-0 = <&spi2_default>; pinctrl-1 = <&spi2_sleep>; pinctrl-names = "default", "sleep"; mcp3201: mcp3201@0 { compatible = "vnd,spi-device"; reg = <0>; spi-max-frequency = <1600000>; label = "mcp3201"; }; }; &pinctrl { spi2_default: spi2_default { group1 { psels = <NRF_PSEL(SPIM_SCK, 0, 19)>, <NRF_PSEL(SPIM_MOSI, 0, 21)>, <NRF_PSEL(SPIM_MISO, 0, 22)>; }; }; spi2_sleep: spi2_sleep { group1 { psels = <NRF_PSEL(SPIM_SCK, 0, 19)>, <NRF_PSEL(SPIM_MOSI, 0, 21)>, <NRF_PSEL(SPIM_MISO, 0, 22)>; low-power-enable; }; }; };
Don’t be scared away, I had to include all of this in order to remap which pins were being used for SPI on this device. Your mileage will vary based on your chip, just make sure you choose the correct SPI node (spi2
, in my case) and include the highlighted lines above as the added node.
Note that SPI requires a chip-select (CS) pin. Zephyr will handle this for you automatically. Study the example above. You will see one entry called cs-gpios
. This is a comma-separated list inside of arrow-brackets. If you have multiple SPI devices, just add each CS pin inside those brackets. It’s a zero-indexed list, and the CS for the mcp3201 is the zeroth entry in that list. I use an @0
in the path and reg = <0>
to tell Zephyr which CS pin to use.
vnd,spi-device
. Technically I should be defining my own DeviceTree binding instead of leaning on this “vendor” definition that is meant for testing purposes only. To learn the recommended way, here’s a 2-hour Zephyr driver talk from ZDS 2022. Let’s not get bogged down in the voodoo of DeviceTree; shall we move along and leave the bindings creation process for a future post?3. Set up the memory structure
The memory structure is a bit quirky for SPI reads. Generally speaking, you need to set up the memory structure to perfectly mirror how much data you want to read or write. I’m only going to be reading in this example.
I want to read four bytes so I need a four-byte array. That array needs to be a member of a spi_buf
, and that spi_buf
needs to be a member of a spi_buf_set
:
uint8_t my_buffer[4] = {0}; struct spi_buf my_spi_buffer[1]; my_spi_buffer[0].buf = my_buffer; my_spi_buffer[0].len = 4; const struct spi_buf_set rx_buff = { my_spi_buffer, 1 };
Honestly this was very confusing for me to figure out. It might be this way for DMA-based SPI peripherals. Or maybe this schema enables you to kind of pre-parse data of different lengths. I haven’t found documentation on why this this structure is the way it is. If you have any insight, please let us know on the Golioth forum thread for this post.
Basically the SPI peripheral is going to shift bits until it runs out of room in your memory structure. So format this for exactly the length of data you need to read/write and Zephyr takes care of the rest.
4. Get the device from the DeviceTree
It’s time to get the SPI device from the DeviceTree. This is a great time to include the configuration for your SPI bus. I followed the Zephyr test file examples and first defined my config values:
#define SPI_OP SPI_OP_MODE_MASTER |SPI_MODE_CPOL | SPI_MODE_CPHA | SPI_WORD_SET(8) | SPI_LINES_SINGLE
Once we get to the main function, it’s now a much simpler call to get your SPI device:
const struct spi_dt_spec mcp3201_dev = SPI_DT_SPEC_GET(DT_NODELABEL(mcp3201), SPI_OP, 0);
5. Issue read command
My standard advice for Zephyr applies here: always check the return codes. When working through this issue, the return codes and enabling debug-level logs in the SPI libraries helped me work out a problem with my memory structure.
ret = spi_read_dt(&mcp3201_dev, &rx_buff); if (ret) { LOG_INF("spi_read status: %d", ret); }
What we have here is a SPI read operation which immediately prints out the return code if there were any errors. If not, the data you wanted to get from your device is now ready and waiting inside of the buffer that was passed in during the read command.
Further reading
It was quite a task for me to figure all of this out. It would have been much easier had I first looked to the test files (as I mentioned previously). You may want to write to a SPI device, or read and write at the same time. This same process can be used! You just need to also have a memory structure for the data you want to transmit, and you need to use the write, or the “transceive” versions of the SPI functions.
I suggest you start by reading through the SPI API reference. You should also make time to work through all of the functions in the spi_loopback test app which uses SPI in just about every way possible on Zephyr.
Beyond that, if you do have any questions, hit up the Zephyr discord or start a thread on the Golioth forums.
Start the discussion at forum.golioth.io