https://elixir.bootlin.com/linux/v5.2.2/source/Documentation/spi/spidev
首先需要在設(shè)備樹配置好spi設(shè)備??蓞⒖既缦屡渲?/p>
&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" ; }; }; |
在配置好后,會出現(xiàn) /dev/spidevX.X 設(shè)備。
初始化SDK環(huán)境,進入內(nèi)核的源碼目錄,然后進入tools/spi目錄,make.
https://elixir.bootlin.com/linux/latest/source/tools/spi
得到 spidev_test 和 spidev_fdx兩個程序。
可以使用優(yōu)盤,也可以使用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上發(fā)送"string_to_send"字符串,顯示發(fā)送和接收的數(shù)據(jù)。
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 __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ | .............. |
如果要發(fā)送32位/16位的數(shù)據(jù),則需要先生成二進制文件,如生成32字節(jié)的隨機數(shù)據(jù):
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 |
發(fā)送:
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 | ................................ |
聯(lián)系客服