SPI from Linux with spidev
Revision and Notes
Date
Owner
Revision
Notes
24 Feb 2022
1.0
Initial release
Introduction
Linux® SPI framework offers several ways to access SPI peripherals. Among them, the spidev framework enables to easily control an SPI peripheral straight from Linux® user space. The following provides a guide for testing the SPI bus with spidev.
SPI hardware tests (loopback MOSI / MISO)
Short-circuit the SPI bus MISO and MOSI lines to create a loopback enables the bus to receive the same data it is sending. This is an interesting solution to quickly perform basic tests as well as performance tests.
Attached here the MikroBus schematics:

DT and kernel configuration
In our example of MOSI/MISO loopback, the DT must be customized as follows:
Activate the SPI controller by setting its status to okay.
Add a spidev child node.
Enable spidev by adding a compatible spidev.
Add a reg property, required for the SPI framework but not meaningful in this case since chip select is not defined and loopback is used.
Configure the bus speed for SPI communications by setting the spi-max-frequency property.
SPI unitary tests using spidev_test
spidev_test, available within the Linux® kernel, is a test tool enabling to perform tests via the spidev interface.
Source code
The Linux® kernel spidev_test tool source code can be found under tools/spi[2] directory:
List of spidev options
The spidev_test tool has the following options:
Example of 32-byte transfer in Full-duplex with loopback
To make sure you can detect your SPI devices, use the following:
Two available SPI devices are detected:

Last updated