Linux gpio模拟spi
1.config配置
首先是spidev,要在/dev/下面产生设备文件,需要spidev的支持
CONFIG_SPI_SPIDEV=y
使用的是gpio模拟spi,gpio模拟spi的时序原理是bitbang文件实现的,所以这个也需要打开,如果是在openwrt下动态加载的话就是如下两个配置
CONFIG_PACKAGE_kmod-spi-bitbang=y
+CONFIG_PACKAGE_kmod-spi-gpio=y
如果是直接内核的话是如下两个
CONFIG_SPI_BITBANG=y
CONFIG_SPI_GPIO=y
2.arch层添加device和board_info
跟I2C的arch层一样,主要是devices的添加和board_info的添加,如下
//nfc mfrc522
platform_add_devices(bsp_spi_gpio_devs, ARRAY_SIZE(bsp_spi_gpio_devs));
spi_register_board_info(nfc_spi_gpio_board_info,
ARRAY_SIZE(nfc_spi_gpio_board_info));
对于platform_add_devices,因为是使用spi_gpio,所以name是"spi_gpio"这样才可以与driver里面的spi_gpio相互匹配probe到。
因为SPI是可以一个总线上面挂多个,然后通过片选脚CS进行硬件切换,所以这变有个num_chipselect需要设置,如果有2个设置就设置2,一个设备就设置1,这边设置好之后,后面board_info也要有对应的个数,而且片选引脚需要不同。
I2C是通过每个设备有自己不同的地址,通过地址来进行软件切换。
#include <linux/spi/spi.h>
#include <linux/spi/spi_gpio.h
#define SPI_GPIO_BUS_NUM 1
#define SPI_GPIO_SCK 3 /* set gpio 2 as sck */
#define SPI_GPIO_MOSI 1 /* set gpio 3 as MOSI */
#define SPI_GPIO_MISO 0 /* set gpio 4 as MISO */
#define SPI_GPIO_NUM_CHIPSELECT 1 //2
struct spi_gpio_platform_data spi_gpio_info = {
.sck = SPI_GPIO_SCK,
.mosi = SPI_GPIO_MOSI,
.miso = SPI_GPIO_MISO,
.num_chipselect = SPI_GPIO_NUM_CHIPSELECT,
};
struct platform_device bsp_spi_gpio_device = {
.name = "spi_gpio",
.id = SPI_GPIO_BUS_NUM,
.dev = {
.platform_data = &spi_gpio_info,
}
};
static struct platform_device *bsp_spi_gpio_devs[] = {
&bsp_spi_gpio_device,
};
对于board_info使用的是spidev,drivers/spi/spidev.c文件,该文件的内容是注册一个spidev驱动。该驱动是一个字符设备驱动。
如果设备与驱动匹配,那么就会执行spidev_probe()的内容。在spidev_probe()函数中会调用device_create()成功后在 /dev 目录下就会生成 spidev 相关的设备节点。
这边有几个参数要注意:
- bus_num说的是总线,如果为0,生成的就是/dev/spidev0.x,如果为1,生成的就是/dev/spidev1.x的设备。
- chip_select,说的是第几个设备,如果配置了有两个设备,则就两个一个0一个1,对应生成/dev/spidev1.0,/dev/spidev1.1两个设备。
- controller_data就是片选CS对应的硬件GPIO引脚。
#define SPI_GPIO_CHIP_SELECT_MFRC522 0
//#define SPI_GPIO_CHIP_SELECT_SPIDEV 1
#define SPI_GPIO_CONTROLLER_DATA_MFRC522 (void *)2 /* set gpio 2 as CS for spidev */
//#define SPI_GPIO_CONTROLLER_DATA_SPIDEV (void *)4 /* set gpio 4 as CS for spidev */
static struct spi_board_info nfc_spi_gpio_board_info[] __initdata = {
{
.modalias = "spidev",
.mode = SPI_MODE_0, // CPOL=0, CPHA=0
.bus_num = SPI_GPIO_BUS_NUM,
.chip_select = SPI_GPIO_CHIP_SELECT_MFRC522,
.controller_data = SPI_GPIO_CONTROLLER_DATA_MFRC522,
.max_speed_hz = 25000000,
},
/*{
.modalias = "spidev",
.mode = SPI_MODE_0, // CPOL=0, CPHA=0
.bus_num = SPI_GPIO_BUS_NUM,
.chip_select = SPI_GPIO_CHIP_SELECT_SPIDEV,
.controller_data = SPI_GPIO_CONTROLLER_DATA_SPIDEV,
.max_speed_hz = 10000000,
},*/
};
3.打开内核debug
调试过程想看一些细节的debug信息可以打开内核的动态debug信息,这个在以前的print system里面有
CONFIG_DYNAMIC_DEBUG=y
CONFIG_DEBUG_DRIVER=y
CONFIG_DEBUG_DEVRES=y
CONFIG_SPI_DEBUG=y
printk的等级设置成8.
4.调试过程
开始
13.650000] bus: 'platform': add driver spi_gpio
[ 13.660000] bus: 'platform': driver_probe_device: matched device spi_gpio.0 with driver spi_gpio
[ 13.680000] bus: 'platform': really_probe: probing driver spi_gpio with device spi_gpio.0
[ 13.690000] spi_gpio spi_gpio.0: 11111-spi_gpio_probe
[ 13.700000] spi_gpio spi_gpio.0: 4444-spi_gpio_request
[ 13.710000] bus:5555
[ 13.720000] spi_gpio: 2222-probe of spi_gpio.0 failed with error -16
定位到是spi_gpio_request
的时候报错
后面就将zkernel/3.10.49/arch/mips/mtk/ziroom/zrmt7628.c里面GPIO的信息调整下,因为SPI的引脚和LED的引脚号一样,内核不知道哪里会检测到。
修改后打印如下:
[ 13.610000] bus: 'platform': add driver spi_gpio
[ 13.620000] bus: 'platform': driver_probe_device: matched device spi_gpio.1 with driver spi_gpio
[ 13.640000] bus: 'platform': really_probe: probing driver spi_gpio with device spi_gpio.1
[ 13.660000] spi_gpio spi_gpio.1: 11111-spi_gpio_probe
[ 13.680000] device: 'spi1': device_add
[ 13.680000] spi_gpio spi_gpio.1: registered master spi1
[ 13.690000] spi_gpio spi_gpio.1: master is unqueued, this is deprecated
[ 13.710000] spi spi1.0: spi_bitbang_setup, 100 nsec/bit
[ 13.720000] spi spi1.0: setup mode 0, 8 bits/w, 10000000 Hz max --> 0
[ 13.730000] device: 'spi1.0': device_add
[ 13.740000] bus: 'spi': add device spi1.0
[ 13.760000] bus: 'spi': driver_probe_device: matched device spi1.0 with driver spidev
[ 13.780000] bus: 'spi': really_probe: probing driver spidev with device spi1.0
[ 13.790000] device: 'spidev1.0': device_add
[ 13.840000] driver: 'spi1.0': driver_bound: bound to device 'spidev'
[ 13.860000] bus: 'spi': really_probe: bound device spi1.0 to driver spidev
[ 13.870000] spi_gpio spi_gpio.1: registered child spi1.0
[ 13.880000] spi spi1.1: spi_bitbang_setup, 40 nsec/bit
[ 13.890000] spi spi1.1: setup mode 0, 8 bits/w, 25000000 Hz max --> 0
[ 13.900000] device: 'spi1.1': device_add
[ 13.910000] bus: 'spi': add device spi1.1
[ 13.940000] spi_gpio spi_gpio.1: registered child spi1.1
[ 13.950000] driver: 'spi_gpio.1': driver_bound: bound to device 'spi_gpio'
[ 13.960000] bus: 'platform': really_probe: bound device spi_gpio.1 to driver spi_gpio
[ 14.110000] xt_time: kernel timezone is -0000
之后在/dev/下面就生成了spidev1.0的设备
5.应用层测试
有了/dev/spidev1.0设备之后,就可以在应用成操作改设备收发数据。
在drivers/spi/spidev.c里面已经封装好了ioctl的对应接口,根据这些接口就可以测试使用。
在Documentation/spi/spidev_test.c下面有个应用层的实例,打开看下就清除了。
$(cc) spidev_test.c -o spidev_test生成可执行文件spidev_test
然后拷贝到板子上,将MOSI和MISO短接就可以测试回环数据是否正常。
root@zihome:/tmp# ./spidev_test -D /dev/spidev1.0
[ 3142.770000] spidev spi1.0: spi_bitbang_setup, 2000 nsec/bit
[ 3142.780000] spidev spi1.0: setup mode 0, 8 bits/w, 500000 Hz max --> 0
[ 3142.800000] spidev spi1.0: spi mode 00
[ 3142.800000] spidev spi1.0: spi_bitbang_setup, 2000 nsec/bit
[ 3142.820000] spidev spi1.0: setup mode 0, 8 bits/w, 500000 Hz max --> 0
[ 3142.830000] spidev spi1.0: 8 bits per word
[ 3142.840000] spidev spi1.0: spi_bitbang_setup, 2000 nsec/bit
[ 3142.850000] spidev spi1.0: setup mode 0, 8 bits/w, 500000 Hz max --> 0
[ 3142.860000] spidev spi1.0: 500000 Hz (max)
spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 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
有逻辑分析仪的接上logic看波形就更加直观。
gpio模拟SPI:
https://blog.csdn.net/luckywang1103/article/details/70145870
在ARM Linux下使用GPIO模拟SPI时序详解:
https://blog.csdn.net/yangzheng_yz/article/details/50470577
linux SPI驱动:
https://www.cnblogs.com/xuyh/category/903809.html
本文地址:https://blog.csdn.net/Creator_Ly/article/details/109640572
上一篇: JDBC的典型用法
推荐阅读
-
使用 linux 模拟广域网延迟 - Emulating wide area network delays with Linux
-
Linux中gpio接口的使用方法示例
-
Linux下模拟http的get/post请求(curl or wget)详解
-
Linux模拟实现sleep函数
-
浅谈linux模拟多线程崩溃和多进程崩溃
-
[大数据学习研究]2.利用VirtualBox模拟Linux集群
-
linux debug -- dosemu (模拟DOS下debug工具)
-
基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(二)之cdev与read、write
-
嵌入式Linux-JZ2440-02课 GPIO口实验
-
python之模拟linux系统中的命令行