“How to use SPI”的版本间的差异

来自百问网嵌入式Linux wiki
第1行: 第1行:
 
__TOC__
 
__TOC__
 
<br />
 
<br />
{{ReviewsComments|W1938 FGA: Maybe rename the article:
+
Maybe rename the article:
 
* precise it's for Linux ?
 
* precise it's for Linux ?
 
* How to use SPI from userland? (Uses "spidev": SPI devices userspace API)
 
* How to use SPI from userland? (Uses "spidev": SPI devices userspace API)
Do we need a summary?}}
+
Do we need a summary?
 
==Purpose==
 
==Purpose==
 
This article describes how to use SPI. The SPI overview is described in [[SPI overview]] article. <br/>
 
This article describes how to use SPI. The SPI overview is described in [[SPI overview]] article. <br/>

2020年11月8日 (日) 15:28的版本


Maybe rename the article:

  • precise it's for Linux ?
  • How to use SPI from userland? (Uses "spidev": SPI devices userspace API)

Do we need a summary?

Purpose

This article describes how to use SPI. The SPI overview is described in SPI overview article.
<securetransclude src="ProtectedTemplate:ReviewsComments" params="W1938 FGA: This article describes how to use the SPI when the peripheral is assigned to Linux<sup>&reg;</sup>

  • How to configure a SPI device via the board device tree (example using "spidev")
  • How to perform data transfers in userland

Maybe refine to TOC:

  • 1 Purpose
  • 2 Configuration (DT, kernel, board connection
  • 3 Data transfert in user land"></securetransclude>{{#set:Has reviews comments=true}}

SPI Loop between MOSI and MISO

<securetransclude src="ProtectedTemplate:ReviewsComments" params="W1931: ADE: Maybe add few lines here to explain the interest of a SPI Loop"></securetransclude>{{#set:Has reviews comments=true}} <securetransclude src="ProtectedTemplate:ReviewsComments" params="W1938: FGA: Agreed, looks like that's for the example here, but valid for custom SPI user land uage? To elaborate...<br/> W1938: FGA: Also may be moved before #Simple data transfer"></securetransclude>{{#set:Has reviews comments=true}} 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.

Board connections

<securetransclude src="ProtectedTemplate:ReviewsComments" params="W1938: FGA: Merge this into previsous description? Redundant with above Connect the MISO and MOSI lines..."></securetransclude>{{#set:Has reviews comments=true}} A wire has to be added to connect MISO and MOSI pins (D12 et D11 pins on STM32MP157X-DKX ARDUINO connector) <securetransclude src="ProtectedTemplate:ReviewsComments" params="W1938: FGA: Maybe add level2 "Configuration" chapter as pre-requisite:

Device Drivers --->
   SPI support --->
       <*>   User mode SPI device driver support"></securetransclude>{{#set:Has reviews comments=true}}

DT configuration (board level)

Board DT configuration has to be updated to:

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

<securetransclude src="ProtectedTemplate:ReviewsComments" params="W1935: ABO: The properties #addess-cells and #size-cells are not required in the spi device. They are mandatory in the spi controller node, and in fact they are correctly present in stm32mp157c.dtsi, but not in the spi device. This is described in Documentation/devicetree/bindings/spi/spi-bus.txt"></securetransclude>{{#set:Has reviews comments=true}} <securetransclude src="ProtectedTemplate:ReviewsComments" params="W1938 FGA: Don't understand why you copy/paste the example already provided in SPI_device_tree_configuration#DT_configuration_-28board_level-29. There's no additional information here... Or maybe need to add better explanation on why it has to be updated for loopback testing ?

  • Configure the PE11 GPIO as the chip select by using cs-gpios ...
  • Configure the SPI MOSI/MISO/... pin as alternate function via pinctrl through pinctrl-0, pinctrl-1 and pinctrl-names
  • Activate SPI controller by setting status "okay"
  • Add a spidev child node
    • Enable spidev by adding compatible "spidev";
    • Configure speed by setting ...
    • Also a note on unit-address (@0) / reg <0> / cs-gpio index/number?;"></securetransclude>{{#set:Has reviews comments=true}}
&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>;
	};
};

Simple data transfer

Board $> echo -ne "\xAA\xBB" > /dev/spidev0.0
Board $> more /dev/spidev0.0
<securetransclude src="ProtectedTemplate:ReviewsComments" params="W1938 FGA: command result ? How to check it matches spi4 instance ?"></securetransclude>{{#set:Has reviews comments=true}}

Data transfer with spidev_test

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

Source code location

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

spidev_test compilation

This tool is not compiled by default when compiling the Linux kernel for the target board. It can be compiled independently: 如何构建Linux内核用户空间工具 and then installed on the target.

List the spidev options

The spidev_test tool options can be listed:

Board $> 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

Example to transfer 32 bytes in full-duplex

Board $> 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  | ......@.... .................. .

References

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