How to use SPI from Linux userland with spidev

Revision as of 11:19, 20 August 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]

{{{W1931: ADE: Maybe add few lines here to explain the interest of a SPI Loop}}} 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]

2.4.1. spidev_test compilation[edit source]

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

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

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.2. List the spidev options[edit source]

spidev_test doesn't include an help option. Nevertheless if an invalid option is used, a default option case will display the list of supported options

 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.3. 32 data in full-duplex transfer with spidev_test[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  | ......@.... .................. .

{{{W1931: ELR: add a section about kernel spaces tests to be discussed with ADE drivers/spi/spi-loopback-test.c /drivers/spi/spi-test.h

Commit message: spi: add loopback test driver to allow for spi_master regression tests

This driver is submitting lots of distinct spi-messages messages with all kinds of alignments and length pattern. Also distinct kinds of transfer pattern tests are implemented (rx, tx, rx/tx, tx+tx, tx+rx,...)

Right now on a raspberry pi 752 distinct spi_messages are executed in 13 different scenarios.

Configuration of additional test-pattern is easy, so that when new bugs in drivers get detected the relevant transfer pattern can also get added to the test framework, so that such situations are detected in other drivers as well.

The idea behind this driver is to make it possible to also detect regressions in spi_master implementations when changes occur. Potentially these tests could get executed automatically in a test-server-farm. }}}

3. References[edit source]

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


Template:ArticleMainWriter