码迷,mamicode.com
首页 > 系统相关 > 详细

Linux下spi驱动开发

时间:2015-10-14 23:24:00      阅读:452      评论:0      收藏:0      [点我收藏+]

标签:

转载至:http://www.embedu.org/Column/Column367.htm

作者:刘洪涛,华清远见嵌入式学院讲师。

一、概述

基于子系统去开发驱动程序已经是linux内核中普遍的做法了。前面写过基于I2C子系 统的驱动开发。本文介绍另外一种常用总线SPI的开发方法。SPI子系统的开发和I2C有很多的相似性,大家可以对比学习。本主题分为两个部分叙述,第一 部分介绍基于SPI子系统开发的理论框架;第二部分以华清远见教学平台FS_S5PC100上的M25P10芯片为例(内核版本2.6.29),编写一个 SPI驱动程序实例。

二、SPI总线协议简介

介绍驱动开发前,需要先熟悉下SPI通讯协议中的几个关键的地方,后面在编写驱动时,需要考虑相关因素。

SPI总线由MISO(串行数据输入)、MOSI(串行数据输出)、SCK(串行移位时钟)、CS(使能信号)4个信号线组成。如FS_S5PC100上的M25P10芯片接线为:

技术分享

 

上图中M25P10的D脚为它的数据输入脚,Q为数据输出脚,C为时钟脚。

SPI常用四种数据传输模式,主要差别在于:输出串行同步时钟极性(CPOL)和相位 (CPHA)可以进行配置。如果CPOL= 0,串行同步时钟的空闲状态为低电平;如果CPOL= 1,串行同步时钟的空闲状态为高电平。如果CPHA= 0,在串行同步时钟的前沿(上升或下降)数据被采样;如果CPHA = 1,在串行同步时钟的后沿(上升或下降)数据被采样。

 

技术分享技术分享

 

这四种模式中究竟选择哪种模式取决于设备。如M25P10的手册中明确它可以支持的两种模式为:CPOL=0 CPHA=0  和 CPOL=1 CPHA=1

技术分享

 

三、linux下SPI驱动开发

首先明确SPI驱动层次,如下图:       

技术分享

 

我们以上面的这个图为思路

1、 Platform bus

Platform bus对应的结构是platform_bus_type,这个内核开始就定义好的。我们不需要定义。

2、Platform_device

SPI控制器对应platform_device的定义方式,同样以S5PC100中的SPI控制器为例,参看arch/arm/plat-s5pc1xx/dev-spi.c文件

点击(此处)折叠或打开

  1. struct platform_device s3c_device_spi0 = {
  2.                           .name = "s3c64xx-spi", //名称,要和Platform_driver匹配
  3.                 .id = 0, //第0个控制器,S5PC100中有3个控制器
  4.                 .num_resources = ARRAY_SIZE(s5pc1xx_spi0_resource), //占用资源的种类
  5.                 .resource = s5pc1xx_spi0_resource, //指向资源结构数组的指针
  6.                 .dev = {
  7.                                   .dma_mask = &spi_dmamask, //dma寻址范围
  8.                         .coherent_dma_mask = DMA_BIT_MASK(32), //可以通过关闭cache等措施保证一致性的dma寻址范围
  9.                         .platform_data = &s5pc1xx_spi0_pdata, //特殊的平台数据,参看后文
  10.                 },
  11.                 };

  12. static struct s3c64xx_spi_cntrlr_info s5pc1xx_spi0_pdata = {
  13.                   .cfg_gpio = s5pc1xx_spi_cfg_gpio, //用于控制器管脚的IO配置
  14.         .fifo_lvl_mask = 0x7f,
  15.                   .rx_lvl_offset = 13,
  16.                 };

  17. static int s5pc1xx_spi_cfg_gpio(struct platform_device *pdev)
  18.                   {
  19.                   switch (pdev->id) {
  20.                   case 0:
  21.                           s3c_gpio_cfgpin(S5PC1XX_GPB(0), S5PC1XX_GPB0_SPI_MISO0);
  22.                           s3c_gpio_cfgpin(S5PC1XX_GPB(1), S5PC1XX_GPB1_SPI_CLK0);
  23.                           s3c_gpio_cfgpin(S5PC1XX_GPB(2), S5PC1XX_GPB2_SPI_MOSI0);
  24.                           s3c_gpio_setpull(S5PC1XX_GPB(0), S3C_GPIO_PULL_UP);
  25.                           s3c_gpio_setpull(S5PC1XX_GPB(1), S3C_GPIO_PULL_UP);
  26.                           s3c_gpio_setpull(S5PC1XX_GPB(2), S3C_GPIO_PULL_UP);
  27.                         break;

  28. case 1:
  29.                           s3c_gpio_cfgpin(S5PC1XX_GPB(4), S5PC1XX_GPB4_SPI_MISO1);
  30.                           s3c_gpio_cfgpin(S5PC1XX_GPB(5), S5PC1XX_GPB5_SPI_CLK1);
  31.                           s3c_gpio_cfgpin(S5PC1XX_GPB(6), S5PC1XX_GPB6_SPI_MOSI1);
  32.                           s3c_gpio_setpull(S5PC1XX_GPB(4), S3C_GPIO_PULL_UP);
  33.                           s3c_gpio_setpull(S5PC1XX_GPB(5), S3C_GPIO_PULL_UP);
  34.                           s3c_gpio_setpull(S5PC1XX_GPB(6), S3C_GPIO_PULL_UP);
  35.                         break;

  36. case 2:
  37.                           s3c_gpio_cfgpin(S5PC1XX_GPG3(0), S5PC1XX_GPG3_0_SPI_CLK2);
  38.                           s3c_gpio_cfgpin(S5PC1XX_GPG3(2), S5PC1XX_GPG3_2_SPI_MISO2);
  39.                           s3c_gpio_cfgpin(S5PC1XX_GPG3(3), S5PC1XX_GPG3_3_SPI_MOSI2);
  40.                           s3c_gpio_setpull(S5PC1XX_GPG3(0), S3C_GPIO_PULL_UP);
  41.                           s3c_gpio_setpull(S5PC1XX_GPG3(2), S3C_GPIO_PULL_UP);
  42.                           s3c_gpio_setpull(S5PC1XX_GPG3(3), S3C_GPIO_PULL_UP);
  43.                         break;

  44. default:
  45.                           dev_err(&pdev->dev, "Invalid SPI Controller number!");
  46.                           return -EINVAL;
  47.                   }

3、Platform_driver

再看platform_driver,参看drivers/spi/spi_s3c64xx.c文件

 

点击(此处)折叠或打开

  1. static struct platform_driver s3c64xx_spi_driver = {
  2.                           .driver = {
  3.                                   .name = "s3c64xx-spi", //名称,和platform_device对应
  4.                         .owner = THIS_MODULE,
  5.                           },
  6.                           .remove = s3c64xx_spi_remove,
  7.                           .suspend = s3c64xx_spi_suspend,
  8.                           .resume = s3c64xx_spi_resume,
  9.                 };

  10. platform_driver_probe(&s3c64xx_spi_driver, s3c64xx_spi_probe);//注册s3c64xx_spi_driver

和平台中注册的platform_device匹配后,调用 s3c64xx_spi_probe。然后根据传入的platform_device参数,构建一个用于描述SPI控制器的结构体spi_master, 并注册。spi_register_master(master)。后续注册的spi_device需要选定自己的spi_master,并利用 spi_master提供的传输功能传输spi数据。

和I2C类似,SPI也有一个描述控制器的对象叫spi_master。其主要成员是主机控制器的序号(系统中可能存在多个SPI主机控制器)、片选数量、SPI模式和时钟设置用到的函数、数据传输用到的函数等。

 

点击(此处)折叠或打开

  1. struct spi_master {
  2.                           struct device dev;
  3.                           s16 bus_num; //表示是SPI主机控制器的编号。由平台代码决定
  4.                 u16 num_chipselect; //控制器支持的片选数量,即能支持多少个spi设备
  5.                 int (*setup)(struct spi_device *spi); //针对设备设置SPI的工作时钟及数据传输模式等。在spi_add_device函数中调用。
  6.                 int (*transfer)(struct spi_device *spi,
  7.                           struct spi_message *mesg); //实现数据的双向传输,可能会睡眠
  8.                 void (*cleanup)(struct spi_device *spi); //注销时调用
  9.         };

4、Spi bus

Spi总线对应的总线类型为spi_bus_type,在内核的drivers/spi/spi.c中定义

 

点击(此处)折叠或打开

  1. struct bus_type spi_bus_type = {
  2.                           .name = "spi",
  3.                           .dev_attrs = spi_dev_attrs,
  4.                           .match = spi_match_device,
  5.                           .uevent = spi_uevent,
  6.                           .suspend = spi_suspend,
  7.                           .resume = spi_resume,
  8.                 };

对应的匹配规则是(高版本中的匹配规则会稍有变化,引入了id_table,可以匹配多个spi设备名称):

 

点击(此处)折叠或打开

  1. static int spi_match_device(struct device *dev, struct device_driver *drv)
  2.                   {
  3.                           const struct spi_device *spi = to_spi_device(dev);
  4.                           return strcmp(spi->modalias, drv->name) == 0;
  5.                 }

5、spi_device

下面该讲到spi_device的构建与注册了。spi_device对应的含义是挂接在spi总线上的一个设备,所以描述它的时候应该明确它自身的设备特性、传输要求、及挂接在哪个总线上。

 

点击(此处)折叠或打开

  1. static struct spi_board_info s3c_spi_devs[] __initdata = {
  2.                           {
  3.                                   .modalias = "m25p10",
  4.                                   .mode = SPI_MODE_0, //CPOL=0, CPHA=0 此处选择具体数据传输模式
  5.                         .max_speed_hz = 10000000, //最大的spi时钟频率
  6.                         /* Connected to SPI-0 as 1st Slave */
  7.                                   .bus_num = 0, //设备连接在spi控制器0上
  8.                         .chip_select = 0, //片选线号,在S5PC100的控制器驱动中没有使用它作为片选的依据,而是选择了下文controller_data里的方法。
  9.                         .controller_data = &smdk_spi0_csi[0],
  10.                           },
  11.                   };
  12.                   static struct s3c64xx_spi_csinfo smdk_spi0_csi[] = {
  13.                           [0] = {
  14.                                   .set_level = smdk_m25p10_cs_set_level,
  15.                                   .fb_delay = 0x3,
  16.                           },
  17.                   };
  18.                   static void smdk_m25p10_cs_set_level(int high) //spi控制器会用这个方法设置cs
  19.                   {
  20.                           u32 val;
  21.                           val = readl(S5PC1XX_GPBDAT);
  22.                           if (high)
  23.                                   val |= (1<<3);
  24.                           else
  25.                                   val &= ~(1<<3);
  26.                           writel(val, S5PC1XX_GPBDAT);
  27.                 }

  28. spi_register_board_info(s3c_spi_devs, ARRAY_SIZE(s3c_spi_devs));//注册spi_board_info。这个代码会把spi_board_info注册要链表board_list上。

事实上上文提到的spi_master的注册会在spi_register_board_info之后,spi_master注册的过程中会调用scan_boardinfo扫描board_list,找到挂接在它上面的spi设备,然后创建并注册spi_device。

 

点击(此处)折叠或打开

  1. static void scan_boardinfo(struct spi_master *master)
  2.                   {
  3.                           struct boardinfo *bi;
  4.                           mutex_lock(&board_lock);
  5.                           list_for_each_entry(bi, &board_list, list) {
  6.                                   struct spi_board_info *chip = bi->board_info;
  7.                                   unsigned n;
  8.                                   for (n = bi->n_board_info; n > 0; n--, chip++) {
  9.                                           if (chip->bus_num != master->bus_num)
  10.                                           continue;
  11.                                           /* NOTE: this relies on spi_new_device to
  12.                                           * issue diagnostics when given bogus inputs
  13.                                           */
  14.                                           (void) spi_new_device(master, chip); //创建并注册了spi_device
  15.                                   }
  16.                           }
  17.                           mutex_unlock(&board_lock);
  18.                 }

6、spi_driver

本文先以linux内核中的/driver/mtd/devices/m25p80.c驱动为参考。

 

点击(此处)折叠或打开

  1. static struct spi_driver m25p80_driver = { //spi_driver的构建
  2.                 .driver = {
  3.                                   .name = "m25p80",
  4.                                   .bus = &spi_bus_type,
  5.                                   .owner = THIS_MODULE,
  6.                           },
  7.                           .probe = m25p_probe,
  8.                           .remove = __devexit_p(m25p_remove),
  9.                           */
  10.                 };

  11. spi_register_driver(&m25p80_driver);//spi driver的注册

  12. 在有匹配的spi device时,会调用m25p_probe

  13. static int __devinit m25p_probe(struct spi_device *spi)
  14.                   {
  15.                   ……
  16.         }

根据传入的spi_device参数,可以找到对应的spi_master。接下来就可 以利用spi子系统为我们完成数据交互了。可以参看m25p80_read函数。要完成传输,先理解下面几个结构的含义:(这两个结构的定义及详细注释参 见include/linux/spi/spi.h)

spi_message:描述一次完整的传输,即cs信号从高->底->高的传输
        spi_transfer:多个spi_transfer够成一个spi_message
                举例说明:m25p80的读过程如下图

技术分享

 

可以分解为两个spi_ transfer一个是写命令,另一个是读数据。具体实现参见m25p80.c中的m25p80_read函数。下面内容摘取之此函数。

 

点击(此处)折叠或打开

  1. struct spi_transfer t[2]; //定义了两个spi_transfer
  2.                   struct spi_message m; //定义了两个spi_message
  3.                 spi_message_init(&m); //初始化其transfers链表

  4. t[0].tx_buf = flash->command;
  5.                   t[0].len = CMD_SIZE + FAST_READ_DUMMY_BYTE; //定义第一个transfer的写指针和长度
  6.         spi_message_add_tail(&t[0], &m); //添加到spi_message
  7.                   t[1].rx_buf = buf;
  8.                 t[1].len = len; //定义第二个transfer的读指针和长度

  9. spi_message_add_tail(&t[1], &m); //添加到spi_message
  10.                   flash->command[0] = OPCODE_READ;
  11.                   flash->command[1] = from >> 16;
  12.                   flash->command[2] = from >> 8;
  13.                 flash->command[3] = from; //初始化前面写buf的内容

  14. spi_sync(flash->spi, &m); //调用spi_master发送spi_message

  15. // spi_sync为同步方式发送,还可以用spi_async异步方式,那样的话,需要设置回调完成函数。

  16. 另外你也可以选择一些封装好的更容易使用的函数,这些函数可以在include/linux/spi/spi.h文件中找到,如:

  17. extern int spi_write_then_read(struct spi_device *spi,
  18.                           const u8 *txbuf, unsigned n_tx,
  19.                           u8 *rxbuf, unsigned n_rx);

这篇博文就到这了,下篇给出一个针对m25p10完整的驱动程序。


Linux下spi驱动开发之m25p10驱动测试

目标:在华清远见的FS_S5PC100平台上 编写一个简单的spi驱动模块,在probe阶段实现对m25p10的ID号探测、flash擦除、flash状态读取、flash写入、flash读取 等操作。代码已经经过测试,运行于2.6.35内核。理解下面代码需要参照m25p10的芯片手册。其实下面的代码和处理器没有太大关系,这也是spi子 系统的分层特点。

 

点击(此处)折叠或打开

  1. #include <linux/platform_device.h>
  2.         #include <linux/spi/spi.h>
  3.         #include <linux/init.h>
  4.         #include <linux/module.h>
  5.         #include <linux/device.h>
  6.         #include <linux/interrupt.h>
  7.         #include <linux/mutex.h>
  8.         #include <linux/slab.h> // kzalloc
  9.         #include <linux/delay.h>

  10. #define FLASH_PAGE_SIZE 256

  11. /* Flash Operating Commands */
  12.         #define CMD_READ_ID 0x9f
  13.         #define CMD_WRITE_ENABLE 0x06
  14.         #define CMD_BULK_ERASE 0xc7
  15.         #define CMD_READ_BYTES 0x03
  16.         #define CMD_PAGE_PROGRAM 0x02
  17.         #define CMD_RDSR 0x05

  18. /* Status Register bits. */
  19.         #define SR_WIP 1 /* Write in progress */
  20.         #define SR_WEL 2 /* Write enable latch */

  21. /* ID Numbers */
  22.         #define MANUFACTURER_ID 0x20
  23.         #define DEVICE_ID 0x1120

  24. /* Define max times to check status register before we give up. */
  25.         #define MAX_READY_WAIT_COUNT 100000
  26.         #define CMD_SZ 4

  27. struct m25p10a {
  28.                 struct spi_device *spi;
  29.                 struct mutex lock;
  30.                 char erase_opcode;
  31.                 char cmd[ CMD_SZ ];
  32.         };

  33. /*
  34.         * Internal Helper functions
  35.         */

  36. /*
  37.         * Read the status register, returning its value in the location
  38.         * Return the status register value.
  39.         * Returns negative if error occurred.
  40.         */
  41.         static int read_sr(struct m25p10a *flash)
  42.         {
  43.                 ssize_t retval;
  44.                 u8 code = CMD_RDSR;
  45.                 u8 val;

  46.         retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);

  47.         if (retval < 0) {
  48.                        dev_err(&flash->spi->dev, "error %d reading SR\n", (int) retval);
  49.                        return retval;
  50.                  }

  51.         return val;
  52.         }

  53. /*
  54.         * Service routine to read status register until ready, or timeout occurs.
  55.         * Returns non-zero if error.
  56.         */
  57.         static int wait_till_ready(struct m25p10a *flash)
  58.         {
  59.                 int count;
  60.                 int sr;

  61.         /* one chip guarantees max 5 msec wait here after page writes,
  62.                 * but potentially three seconds (!) after page erase.
  63.                 */
  64.                 for (count = 0; count < MAX_READY_WAIT_COUNT; count++) {
  65.                         if ((sr = read_sr(flash)) < 0)
  66.                                 break;
  67.                         else if (!(sr & SR_WIP))
  68.                                 return 0;

  69.                 /* REVISIT sometimes sleeping would be best */
  70.                 }
  71.                  printk( "in (%s): count = %d\n", count );

  72.         return 1;
  73.         }

  74. /*
  75.         * Set write enable latch with Write Enable command.
  76.         * Returns negative if error occurred.
  77.         */
  78.         static inline int write_enable( struct m25p10a *flash )
  79.         {
  80.                 flash->cmd[0] = CMD_WRITE_ENABLE;
  81.                 return spi_write( flash->spi, flash->cmd, 1 );
  82.         }

  83. /*
  84.         * Erase the whole flash memory
  85.         *
  86.         * Returns 0 if successful, non-zero otherwise.
  87.         */
  88.         static int erase_chip( struct m25p10a *flash )
  89.         {
  90.                 /* Wait until finished previous write command. */
  91.                 if (wait_till_ready(flash))
  92.                         return -1;

  93.         /* Send write enable, then erase commands. */
  94.                 write_enable( flash );
  95.                 flash->cmd[0] = CMD_BULK_ERASE;
  96.                 return spi_write( flash->spi, flash->cmd, 1 );
  97.         }

  98. /*
  99.         * Read an address range from the flash chip. The address range
  100.         * may be any size provided it is within the physical boundaries.
  101.         */
  102.         static int m25p10a_read( struct m25p10a *flash, loff_t from, size_t len, char *buf )
  103.         {
  104.                 int r_count = 0, i;

  105.         flash->cmd[0] = CMD_READ_BYTES;
  106.                 flash->cmd[1] = from >> 16;
  107.                 flash->cmd[2] = from >> 8;
  108.                 flash->cmd[3] = from;
  109.     
  110.         #if 1
  111.                 struct spi_transfer st[2];
  112.                 struct spi_message msg;
  113.     
  114.                 spi_message_init( &msg );
  115.                 memset( st, 0, sizeof(st) );

  116.         flash->cmd[0] = CMD_READ_BYTES;
  117.                 flash->cmd[1] = from >> 16;
  118.                 flash->cmd[2] = from >> 8;
  119.                 flash->cmd[3] = from;

  120.         st[ 0 ].tx_buf = flash->cmd;
  121.                 st[ 0 ].len = CMD_SZ;
  122.                 spi_message_add_tail( &st[0], &msg );

  123.         st[ 1 ].rx_buf = buf;
  124.                 st[ 1 ].len = len;
  125.                 spi_message_add_tail( &st[1], &msg );

  126.         mutex_lock( &flash->lock );
  127.     
  128.                 /* Wait until finished previous write command. */
  129.                 if (wait_till_ready(flash)) {
  130.                         mutex_unlock( &flash->lock );
  131.                         return -1;
  132.                 }

  133.         spi_sync( flash->spi, &msg );
  134.                 r_count = msg.actual_length - CMD_SZ;
  135.                 printk( "in (%s): read %d bytes\n", __func__, r_count );
  136.                 for( i = 0; i < r_count; i++ ) {
  137.                         printk( "0x%02x\n", buf[ i ] );
  138.                 }

  139.         mutex_unlock( &flash->lock );
  140.         #endif

  141.         return 0;
  142.         }

  143. /*
  144.         * Write an address range to the flash chip. Data must be written in
  145.         * FLASH_PAGE_SIZE chunks. The address range may be any size provided
  146.         * it is within the physical boundaries.
  147.         */
  148.         static int m25p10a_write( struct m25p10a *flash, loff_t to, size_t len, const char *buf )
  149.         {
  150.                 int w_count = 0, i, page_offset;
  151.                 struct spi_transfer st[2];
  152.                 struct spi_message msg;
  153.         #if 1
  154.                 if (wait_till_ready(flash)) { //读状态,等待ready
  155.                 mutex_unlock( &flash->lock );
  156.                 return -1;
  157.                 }
  158.         #endif
  159.                 write_enable( flash ); //写使能
  160.     
  161.                 spi_message_init( &msg );
  162.                 memset( st, 0, sizeof(st) );

  163.         flash->cmd[0] = CMD_PAGE_PROGRAM;
  164.                 flash->cmd[1] = to >> 16;
  165.                 flash->cmd[2] = to >> 8;
  166.                 flash->cmd[3] = to;

  167.         st[ 0 ].tx_buf = flash->cmd;
  168.                 st[ 0 ].len = CMD_SZ;
  169.                 spi_message_add_tail( &st[0], &msg );

  170.         st[ 1 ].tx_buf = buf;
  171.                 st[ 1 ].len = len;
  172.                 spi_message_add_tail( &st[1], &msg );

  173.         mutex_lock( &flash->lock );

  174.         /* get offset address inside a page */
  175.                 page_offset = to % FLASH_PAGE_SIZE;

  176.         /* do all the bytes fit onto one page? */
  177.                 if( page_offset + len <= FLASH_PAGE_SIZE ) { // yes
  178.                         st[ 1 ].len = len;
  179.                         printk("%d, cmd = %d\n", st[ 1 ].len, *(char *)st[0].tx_buf);
  180.                         //while(1)
  181.                         {
  182.                         spi_sync( flash->spi, &msg );
  183.                         }
  184.                         w_count = msg.actual_length - CMD_SZ;
  185.                 }
  186.                 else { // no
  187.                 }
  188.                 printk( "in (%s): write %d bytes to flash in total\n", __func__, w_count );
  189.                 mutex_unlock( &flash->lock );
  190.                 return 0;
  191.         }

  192. static int check_id( struct m25p10a *flash )
  193.         {
  194.                 char buf[10] = {0};
  195.                 flash->cmd[0] = CMD_READ_ID;
  196.                 spi_write_then_read( flash->spi, flash->cmd, 1, buf, 3 );
  197.                 printk( "Manufacture ID: 0x%x\n", buf[0] );
  198.                 printk( "Device ID: 0x%x\n", buf[1] | buf[2] << 8 );
  199.                 return buf[2] << 16 | buf[1] << 8 | buf[0];
  200.         }

  201. static int m25p10a_probe(struct spi_device *spi)
  202.         {
  203.                 int ret = 0;
  204.                 struct m25p10a *flash;
  205.                 char buf[ 256 ];
  206.                 printk( "%s was called\n", __func__ );
  207.                 flash = kzalloc( sizeof(struct m25p10a), GFP_KERNEL );
  208.                 if( !flash ) {
  209.                         return -ENOMEM;
  210.                 }
  211.                 flash->spi = spi;
  212.                 mutex_init( &flash->lock );
  213.                 /* save flash as driver‘s private data */
  214.                 spi_set_drvdata( spi, flash );
  215.     
  216.                 check_id( flash ); //读取ID
  217.         #if 1
  218.                 ret = erase_chip( flash ); //擦除
  219.                 if( ret < 0 ) {
  220.                         printk( "erase the entirely chip failed\n" );
  221.                 }
  222.                 printk( "erase the whole chip done\n" );
  223.                 memset( buf, 0x7, 256 );
  224.                 m25p10a_write( flash, 0, 20, buf); //0地址写入20个7
  225.                 memset( buf, 0, 256 );
  226.                 m25p10a_read( flash, 0, 25, buf ); //0地址读出25个数
  227.         #endif
  228.                 return 0;
  229.         }

  230. static int m25p10a_remove(struct spi_device *spi)
  231.         {
  232.                 return 0;
  233.         }

  234. static struct spi_driver m25p10a_driver = {
  235.                 .probe = m25p10a_probe,
  236.                 .remove = m25p10a_remove,
  237.                 .driver = {
  238.                         .name = "m25p10a",
  239.                 },
  240.         };

  241. static int __init m25p10a_init(void)
  242.         {
  243.                 return spi_register_driver(&m25p10a_driver);
  244.         }

  245. static void __exit m25p10a_exit(void)
  246.         {
  247.                 spi_unregister_driver(&m25p10a_driver);
  248.         }

  249. module_init(m25p10a_init);
  250.         module_exit(m25p10a_exit);

  251. MODULE_DESCRIPTION("m25p10a driver for FS_S5PC100");

  252. MODULE_LICENSE("GPL");

 

 

===========================

感谢作者的奉献, 要是能早一点看到这篇文章, 我也不用在茫茫的网页中四处寻迷, 搞得焦头烂额也不得其中真谛.

看到这样的文章, 如同拔得云雾见清天,柳岸花明又一村; 其实I2C原来是搞懂了的, SPI跟I2C简直

一模一样, 但就是最初的不了解, 让网上那些分析SPI如何实现的文章带到了迷宫一样. 要说的话那些文章就

完全是在装B, 只是展示了自己理解得怎样怎样, 完全没有阅读的价值. 我要是读了这篇文章, 网上那些东西

还不是多跟踪一下代码就可以搞明白的.只能说怎一个艹字了得.最后再次感谢文章作者的无私奉献, 希望能看到更多

像这样的文章.

 

Linux下spi驱动开发

标签:

原文地址:http://www.cnblogs.com/ganrui/p/spi.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!