su - root
cd /opt
tar -xzvf Ascend310B-source.tar.gz
cd Ascend310B-source
bash build.sh kernelSource
cd /opt/Ascend310B-source/driver/kernel/linux-4.19/tools/spi
make CROSS_COMPILE=aarch64-target-linux-gnu-
mkdir -p include/linux/spi 2>&1 || true ln -sf /opt/Ascend310B-sdk/Ascend310B-source/driver/kernel/linux-4.19/tools/spi/../../include/uapi/linux/spi/spidev.h include/linux/spi/spidev.h make -f /opt/Ascend310B-sdk/Ascend310B-source/driver/kernel/linux-4.19/tools/build/Makefile.build dir=. obj=spidev_test make[1]: Entering directory '/opt/Ascend310B-sdk/Ascend310B-source/driver/kernel/linux-4.19/tools/spi' CC spidev_test.o LD spidev_test-in.o make[1]: Leaving directory '/opt/Ascend310B-sdk/Ascend310B-source/driver/kernel/linux-4.19/tools/spi' LINK spidev_test make -f /opt/Ascend310B-sdk/Ascend310B-source/driver/kernel/linux-4.19/tools/build/Makefile.build dir=. obj=spidev_fdx make[1]: Entering directory '/opt/Ascend310B-sdk/Ascend310B-source/driver/kernel/linux-4.19/tools/spi' CC spidev_fdx.o LD spidev_fdx-in.o make[1]: Leaving directory '/opt/Ascend310B-sdk/Ascend310B-source/driver/kernel/linux-4.19/tools/spi' LINK spidev_fdx
total 300 drwxr-xr-x 3 root root 4096 Sep 16 14:54 ./ drwxr-xr-x 36 root root 4096 Sep 16 14:50 ../ -rw-r--r-- 1 root root 60 Sep 11 10:16 Build drwxr-xr-x 3 root root 4096 Sep 16 14:54 include/ -rw-r--r-- 1 root root 1745 Sep 11 10:16 Makefile -rwxr-xr-x 1 root root 22568 Sep 16 14:54 spidev_fdx* -rw-r--r-- 1 root root 2798 Sep 11 10:16 spidev_fdx.c -rw-r--r-- 1 root root 22336 Sep 16 14:54 spidev_fdx-in.o -rw-r--r-- 1 root root 88 Sep 16 14:54 .spidev_fdx-in.o.cmd -rw-r--r-- 1 root root 22328 Sep 16 14:54 spidev_fdx.o -rw-r--r-- 1 root root 6816 Sep 16 14:54 .spidev_fdx.o.cmd -rw-r--r-- 1 root root 6479 Sep 16 14:54 .spidev_fdx.o.d -rwxr-xr-x 1 root root 37120 Sep 16 14:54 spidev_test* -rw-r--r-- 1 root root 10529 Sep 11 10:16 spidev_test.c -rw-r--r-- 1 root root 54312 Sep 16 14:54 spidev_test-in.o -rw-r--r-- 1 root root 91 Sep 16 14:54 .spidev_test-in.o.cmd -rw-r--r-- 1 root root 54312 Sep 16 14:54 spidev_test.o -rw-r--r-- 1 root root 7975 Sep 16 14:54 .spidev_test.o.cmd -rw-r--r-- 1 root root 7633 Sep 16 14:54 .spidev_test.o.d
具体管脚定义请参见《Atlas 200I A2 加速模块 硬件开发指南》的 章节。
chmod +x spidev_test
spidev_test支持的参数可执行./spidev_test --help命令查看,如下所示。
-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 -8 --octal octal transfer -S --size transfer size -I --iter iterations
./spidev_test -D /dev/spidev0.0 -p "\x04\x05\x20\x01" -v
回显信息如下,表示RX和TX数据和所发数据一致,收发功能正常。
spi mode: 0x0 bits per word: 8 max speed: 500000 Hz (500 kHz) TX | 04 05 20 01 __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ |.. .| RX | 04 05 20 01 __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ |.. .