How To Use SPI From Linux Userland With Spidev - Stm32mpu
How To Use SPI From Linux Userland With Spidev - Stm32mpu
Contents
• 1 Article purpose
• 2 SPI hardware tests (loopback MOSI / MISO)
• 3 DT and kernel configuration
• 4 SPI unitary tests using spidev_test
• 4.1 Source code
• 4.2 Installation on your target
• 4.3 List of spidev options
• 4.4 Example of 32-byte transfer in Full-duplex with loopback
• 5 References
1 Article purpose
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.
Before going further in this document, the reader might be interested in having a look at the SPI overview article
that describes how to use an SPI when the peripheral is assigned to Linux®:
• How to configure an SPI device through the board device tree (example using "spidev")
• How to perform data transfers in userland
1 of 4 11/10/2022, 1:09 PM
How to use SPI from Linux userland with spidev - stm32mpu https://wiki.st.com/stm32mpu/wiki/How_to_use_SPI_from_Linux_user...
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.
On the STM32MP157X-DKX discovery board, MOSI and MISO signals are accessible via the D12 and D11 pins of
the STM32MP157x-DKx_-_hardware_description#Arduino_Uno_connector.
In our example of MOSI/MISO loopback on the STM32MP157X-DKX Discovery board, the stm32mp157a-dk1.dts[1]
device tree, which already includes the board skeleton for spi4, must be customized as follows:
&spi4 {
pinctrl-names = "default", "sleep";
pinctrl-0 = <&spi4_pins_a>;
pinctrl-1 = <&spi4_sleep_pins_a>;
status = "okay";
spidev@0{
compatible = "spidev";
reg = <0>;
spi-max-frequency = <4000000>;
};
};
2 of 4 11/10/2022, 1:09 PM
How to use SPI from Linux userland with spidev - stm32mpu https://wiki.st.com/stm32mpu/wiki/How_to_use_SPI_from_Linux_user...
The Linux® kernel spidev_test tool source code can be found under tools/spi[2] directory:
• tools/spi/spidev_test.c
The Linux® kernel SPI tools are not embedded by default in OpenSTLinux distribution. They can be compiled
independently and then installed on the target (see How to build Linux kernel user space tools).
5 References
3 of 4 11/10/2022, 1:09 PM
How to use SPI from Linux userland with spidev - stm32mpu https://wiki.st.com/stm32mpu/wiki/How_to_use_SPI_from_Linux_user...
1. arch/arm/boot/dts/stm32mp157a-dk1.dts
2. tools/spi , Linux® SPI tools source code
Device tree
Transmit
Receive
4 of 4 11/10/2022, 1:09 PM