How to use SPI from Linux userland with spidev

Revision as of 11:19, 17 September 2019 by Registered User


1. Purpose[edit source]

This article describes how to use SPI. The SPI overview is described in SPI overview article.


2. SPI Loop between MOSI and MISO[edit source]

Short-circuit the MISO and MOSI lines of SPI bus to create a loopback allows the bus to receive the same data it is sending. This is interesting to execute quickly basic tests and to execute performance tests.

2.1. Board connections[edit source]

A wire has to be added to connect MISO and MOSI pins (D12 et D11 pins on STM32MP157X-DKX ARDUINO connector)

2.2. DT configuration (board level)[edit source]

Board DT configuration has to be updated to:

  • configure cs-gpios and activate spi4 node
  • add spidev configuration to spi4 node



&spi4 {
	pinctrl-names = "default", "sleep";
	pinctrl-0 = <&spi4_pins_a>;
	pinctrl-1 = <&spi4_sleep_pins_a>;
	status = "okay";
	cs-gpios = <&gpioe 11 0>;

	spidev@0{
		compatible = "spidev";
		reg = <0>;
		spi-max-frequency = <4000000>;
		#address-cells = <1>;
		#size-cells = <0>;
	};
};

2.3. Simple data transfer[edit source]

 echo -ne "\xAA\xBB" > /dev/spidev0.0
 more /dev/spidev0.0

2.4. Data transfer with spidev_test[edit source]

The spidev_test tool, from the Linux® kernel, provides a test tool for spidev.

2.4.1. Source code location[edit source]

The Linux® kernel spidev_test tool source code can be found under tools/spi[1].

2.4.2. spidev_test compilation[edit source]

This tool is not compiled by default when compiling the Linux kernel for the target board. It can be compiled independently: How to build Linux kernel user space tools and then installed on the target.

2.4.3. List the spidev options[edit source]

The spidev_test tool options can be listed:

 spidev_test -h
Usage: spidev_test [-DsbdlHOLC3vpNR24SItx]
  -D --device   device to use (default /dev/spidev1.1)
  -s --speed    max speed (Hz)
  -d --delay    delay (usec)
  -b --bpw      bits per word
  -i --input    input data from a file (e.g. "test.bin")
  -o --output   output data to a file (e.g. "results.bin")
  -l --loop     loopback
  -H --cpha     clock phase
  -O --cpol     clock polarity
  -L --lsb      least significant bit first
  -C --cs-high  chip select active high
  -3 --3wire    SI/SO signals shared
  -v --verbose  Verbose (show tx buffer)
  -p            Send data (e.g. "1234\xde\xad")
  -N --no-cs    no chip select
  -R --ready    slave pulls low to pause
  -2 --dual     dual transfer
  -4 --quad     quad transfer
  -S --size     transfer size
  -I --iter     iterations
  -t --txonly   simplex tx transfer
  -r --rxonly   simplex rx transfer

2.4.4. Example to transfer 32 bytes in full-duplex[edit source]

 spidev_test -D /dev/spidev0.0 -v
			spi mode: 0x0
			bits per word: 8
			max speed: 500000 Hz (500 KHz)
			TX | FF FF FF FF FF FF 40 00 00 00 00 95 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F0 0D  | ......@.... .................. .
			RX | FF FF FF FF FF FF 40 00 00 00 00 95 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F0 0D  | ......@.... .................. .

3. References[edit source]

  1. tools/spi , Linux® spi source code directory


Template:ArticleMainWriter