首页天道酬勤linux,fpga实现arm软核

linux,fpga实现arm软核

张世龙 05-13 00:54 88次浏览

1 .配置

首先是spidev。 要在/dev/下生成设备文件,需要spidev支持

CONFIG_SPI_SPIDEV=y

正在使用gpio模拟spi。 gpio模拟spi的计时原理是在bitbang文件中实现的,因此也必须打开。 在openwrt中动态加载时,有以下两种配置

config _ package _ kmod-SPI-bit bang=y

CONFIG_PACKAGE_kmod-spi-gpio=y

如果是直接内核,则为以下两个

CONFIG_SPI_BITBANG=y

CONFIG_SPI_GPIO=y

2 .将设备和board_info添加到2.arch层

与I2C的arch层一样,主要是添加设备和添加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 ',以匹配驱动器中的spi_gpio和probe。

这需要一个num_chipselect,因为SPI可以在一条总线上放置多个,以便在芯片选择CS上进行硬件切换。 如果有两个,则设置为2,如果是一个设备,则设置为1。 设定此处后,还需要与后面的board_info对应的个数。 另外,芯片选择端子必须不同。

I2C对每个设备具有不同的地址,通过地址进行软件切换。

#包含

#包含

#define SPI_GPIO_BUS_NUM 1

# defines pi _ gpio _ sck3/* set gpio2ass CK * /

# defines pi _ gpio _ MoSi1/* set gpio3as MoSi * /

# defines pi _ gpio _ miso0/* set gpio4asmiso * /

# defines pi _ gpio _ num _ chipselect1///2

struct SPI _ gpio _ platform _ dataspi _ gpio _ info={

. sck=SPI_GPIO_SCK,

. mosi=SPI_GPIO_MOSI,

. miso=SPI_GPIO_MISO,

. num _ chipselect=SPI _ gpio _ num _ chipselect,

(;

结构平台_设备bsp _ SPI _ gpio _设备={

. name='spi_gpio ',

. id=SPI_GPIO_BUS_NUM,

. dev={

. platform_data=spi_gpio_info,

}

(;

staticstructplatform _ device * bsp _ SPI _ gpio _ devs [ ]={

bsp_spi_gpio_device,

(;

board_info使用spidev、驱动程序/SPI/spidev.c文件。 此文件的内容是注册spidev驱动程序。 此驱动程序是字符设备的驱动程序。

如果设备与驱动程序匹配,则执行spidev_probe ()的内容。 在spidev_probe ()函数中成功调用device_create ) )时,将在/dev目录下生成与spidev相关的设备节点。

这里有几个参数。

bus_num是总线,如果为0,则生成/dev/spidev0.x设备;如果为1,则生成/dev/spidev1.x设备。

chip_select是第几个设备,如果配置为有两个设备,则生成两个设备:/dev/spidev1.0、/dev/spidev1.1,每两个设备0

controller_data是与芯片选择CS对应的硬件GPIO引脚。

# defines pi _ gpio _ chip _ select _ MFR c522

//# defines pi _ gpio _ chip _ select _ spide v1

# defines pi _ gpio _ controller _ data _ MFR c522 (void * )2 /* set gpio 2 as CS for spidev */

//# defines pi _ gpio _ controller _ dat

A_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

plc只读和写入的区别,nandflash读写错误 ,