# SPI操作 ## 预备配置 ### 主线内核 在DTS中使能SPI功能: spi是平台驱动,dts中写的设备的compatible字段即是需要匹配的驱动名。这里使用用户空间驱动SPIDEV。 /arch/arm/boot/dts/sun8i-v3s-licheepi-zero.dts ~~~ &spi0 { pinctrl-names = "default"; pinctrl-0 = <&spi0_pins>; status = "okay"; #address-cells=<1>; #size-cells=<0>; spidev@0x00 { compatible = "spidev"; spi-max-frequency = <100000000>; reg = <0>; }; }; ~~~ 在内核配置中使能spidev驱动: ### BSP内核 在fex中修改: ~~~ [spi_devices] spi_dev_num = 1 [spi_board0] modalias = "spidev" max_speed_hz = 12000000 bus_num = 0 chip_select = 0 mode = 0 ~~~ ~~~ #define SPI_MODE_0 (0|0) #define SPI_MODE_1 (0|SPI_CPHA) //相位 #define SPI_MODE_2 (SPI_CPOL|0) //极性 #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) ~~~ bsp驱动中限制最高SPI速率80M 以上操作完成后,开启系统,会看到/dev/spidev0.0设备 ## C语言操作 linux-3.4/Documentation/spi/spidev_test.c 该例程可以设置spi参数,并发送了一堆测试数据。 ~~~ " -D --device device to use (default /dev/spidev1.1)\n" " -s --speed max speed (Hz)\n" " -d --delay delay (usec)\n" " -b --bpw bits per word \n" " -l --loop loopback\n" " -H --cpha clock phase\n" " -O --cpol clock polarity\n" " -L --lsb least significant bit first\n" " -C --cs-high chip select active high\n" " -3 --3wire SI/SO signals shared\n"); ~~~ ~~~ # ./spidev_test -D /dev/spidev0.0 -s 50000000 spi mode: 0 bits per word: 8 max speed: 50000000 Hz (50000 KHz) 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 DE AD BE EF BA AD F0 0D ~~~ 发送接收示例 linux-3.4/Documentation/spi/spidev_fdx.c usage: %s [-h] [-m N] [-r N] /dev/spidevB.D\n" ./spidev_fdx -m 发送长度 -r 接收长度 ## python操作 ## 参考资料 http://blog.csdn.net/DroidPhone/article/details/23367051 http://blog.csdn.net/DroidPhone/article/details/23932447 http://blog.csdn.net/droidphone/article/details/24353293 http://blog.csdn.net/DroidPhone/article/details/24663659 http://www.embedu.org/column/column367.htm