spidev测试方法 /how to test spidev

在设备树中配置spidev

https://elixir.bootlin.com/linux/v5.2.2/source/Documentation/spi/spidev

首先需要在设备树配置好spi设备。可参考如下配置

?
&ecspi3 {
        pinctrl-names = "default";
        pinctrl-0 = <&pinctrl_ecspi3>;
        cs-gpios = <&gpio1 20 GPIO_ACTIVE_LOW>;
        dmas = <&sdma 7 8 0>,
               <&sdma 8 8 0>;
        dma-names = "rx", "tx";
        status = "okay";
        spi_dev0: spi@0 {
                compatible = "spidev";
                spi-max-frequency = <57600000>;
                reg = <0>;
                status = "okay";
        };
};

在配置好后,会出现 /dev/spidevX.X 设备。

编译并传输spidev测试程序到目标板

初始化SDK环境,进入内核的源码目录,然后进入tools/spi目录,make.

https://elixir.bootlin.com/linux/latest/source/tools/spi

得到 spidev_test 和 spidev_fdx两个程序。

可以使用优盘,也可以使用tftp将两个程序下载过去

?
tftp -g -r spidev_test ip_addr

执行测试

spidev_test的帮助:

?
root@ceyear-imx6ul-2:~# ./spidev_test -help
./spidev_test: invalid option -- 'h'
Usage: ./spidev_test [-DsbdlHOLC3vpNR24SI]
  -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

如,在/dev/spidev2.0上发送"string_to_send"字符串,显示发送和接收的数据。

?
root@phyboard-segin-imx6ul-2:~# ./spidev_test -D /dev/spidev2.0 -v -p string_to_send
spi mode: 0x0
bits per word: 8
max speed: 500000 Hz (500 KHz)
TX | 73 74 72 69 6E 67 5F 74 6F 5F 73 65 6E 64 __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __  | string_to_send
RX | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __  | ..............

如果要发送32位/16位的数据,则需要先生成二进制文件,如生成32字节的随机数据:

?
dd if=/dev/urandom of=test_data bs=16 count=2

用hexdump来查看这个二进制文件:

?
root@phyboard-segin-imx6ul-2:~# hexdump -v test_data -C
00000000  74 6a 59 3e 1e 81 73 fb  5a 3f 94 c7 d8 20 ca e9  |tjY>..s.Z?... ..|
00000010  24 2e a5 68 75 ab f7 12  af e6 c1 3d e2 d8 9a ba  |$..hu......=....|
00000020

发送:

?
root@phyboard-segin-imx6ul-2:~# ./spidev_test -D /dev/spidev2.0 -b 32 -v -i test_data
spi mode: 0x0
bits per word: 32
max speed: 500000 Hz (500 KHz)
TX | 74 6A 59 3E 1E 81 73 FB 5A 3F 94 C7 D8 20 CA E9 24 2E A5 68 75 AB F7 12 AF E6 C1 3D E2 D8 9A BA  | tjY>.s鸝?秦.书$.u.?庳?
RX | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  | ................................
(0)

相关推荐