为了运用SPI 驱动,必须在装备Linux 编译选项时,敞开相应的SPI 选项,如下所示
-> Device Drivers
-> SPI support
SPI support
*** SPI Master Controller Drivers ***
-*- Bitbanging SPI master
<*> Samsung S3C24XX series SPI
< > Samsung S3C24XX series SPI by GPIO
*** SPI Protocol Masters ***
< > SPI EEPROMs from most vendors
<*> User mode SPI device driver support
< > Infineon TLE62X0 (for power switching)
挑选SPI support 是使Linux内核供给SPI 支撑,挑选该选项会默许挑选Bitbanging SPImaster,还需挑选SamsungS3C24XX series SPI,告知内核运用S3C2440A的SPI 控制器驱动。
首先在 devs.c中,添加板子信息
- /* 2009/06/30 jwpan add by spi begin */
- static struct spi_board_info s3c2410_spi0_board[] = {
- [0] = {
- .modalias = “spidev”, /*跟spidev.ko驱动名要相同,否则挂载不上*/
- .platform_data = NULL,
- .bus_num = 0, /*运用的第一条BUS线,2410支撑0,1两根*/
- .chip_select = 1, /*片选信号,和上面结合,在/dev 里边能够看到spidev0.1*/
- .max_speed_hz = 500*1000, /CLK频率*/
- },
- };
- static struct s3c2410_spi_info s3c2410_spi0_platdata = {
- .pin_cs = S3C2410_GPB1,
- .board_info = s3c2410_spi0_board,
- .board_size = ARRAY_SIZE(s3c2410_spi0_board),
- };
- /* 2009/06/30 jwpan add by spi end */
仿制代码
- struct platform_device s3c_device_spi0 = {
- .name = “s3c2410-spi”,
- .id = 0,
- .num_resources = ARRAY_SIZE(s3c_spi0_resource),
- .resource = s3c_spi0_resource,
- .dev = {
- .dma_mask = &s3c_device_spi0_dmamask,
- /* 2009/06/30 jwpan add by spi begin */
- //.coherent_dma_mask = 0xffffffffUL
- .coherent_dma_mask = 0xffffffffUL,
- .platform_data = &s3c2410_spi0_platdata,
- /* 2009/06/30 jwpan add by spi end */
- }
- };
仿制代码添加初始化代码,也能够加到mach-smdk2410.c里边
- static struct platform_device __initdata *smdk_devs[] = {
- &s3c_device_nand,
- &smdk_led4,
- &smdk_led5,
- &smdk_led6,
- &smdk_led7,
- /* 2009/06/30 jwpan add by dm9000 spi begin */
- &s3c_device_spi0,
- /* 2009/06/30 jwpan add by dm9000 spi end */
- };
仿制代码在spi.c的spi_register_master函数里边,有这么一个判别:
- if(master->num_chipselect == 0)
- return -EINVAL;
仿制代码本认为master->num_chipselect这个值能够经过硬件检测拿到,或许驱动有这样的设置.
万万没有想到,这个值便是没有初始化.
需要在spi_s3c24xx.c里边添加一句代码:
- hw->bitbang.txrx_bufs = s3c24xx_spi_txrx;
- hw->bitbang.master->num_chipselect = 2;/* add */
- dev_dbg(hw->dev,”bitbang at %p\n”,hw->bitbang);
仿制代码别的,在 *spi_new_device函数里边,还有一个判别的bug,num_chipselect也是从0开端的,>=的话就不对了.
- //if (chip->chip_select >= master->num_chipselect) {
- if (chip->chip_select > master->num_chipselect) {
仿制代码编译内核,发动后能够在/dev/下面看到spidev.0.1
之后,就能够运用spidev_test ,spidev_fdx程序进行测试了,这两个文件在linux2.6.30的Documentation/Spi目录下面有。
最终,提示一下:
I2C/SPI支撑“线与”,答应多个设备互连。所以协议规则:在无数据传输(开端前、完毕后)时,时钟和数据线都处于“开释”状况,也便是“高阻”状况。
在“开释”状况,假如有上拉电阻(内部或外部的),用外用表测会是高电平(1);假如没有上拉电阻,用万用表测是低电平(0)。
简略一点便是说:SPI的CLK只要在有数据通信时,才会测得出波形,千万别认为SPI出问题了。