手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

基于S3C2440的嵌入式Linux驱动——SPI子系统解读(四)

[日期:2012-08-16] 来源:Linux社区  作者:yj4231 [字体:大 中 小]

8.8 complete方法

在8.6节的bitbang_work中,当一个message的所有数据发送完成以后,将会调用complete函数。该函数如下:

  1. /* 
  2.  * We can't use the standard synchronous wrappers for file I/O; we 
  3.  * need to protect against async removal of the underlying spi_device. 
  4.  */  
  5. static void spidev_complete(void *arg)  
  6. {  
  7.     complete(arg);  
  8. }  

该函数将使在spidev_sync函数中的wait_for_completion得以返回,从而完成了同步IO。

至此,整个write系统调用的流程均以讲解完毕,在这其中也对在master和protocol中未曾给出的函数做出了一一讲解,最后,对第8章进行小结。

8.9 小结

从示意图中,我们可以很清除看到函数的调用过程:先调用spi设备驱动层,随后调用bitbang中间层,最后调用了master驱动层来完成数据的传输。

9. read方法

read方法和write方法基本差不多,关键的区别在于其发送的数据为0,而在s3c24xx_spi_txrx中断服务程序中将读取数据寄存器。下面仅仅给出函数调用示意图。

在这里给出spidev_read和spidev_sync_read,方便读者进行对比。

  1. /* Read-only message with current device setup */  
  2. static ssize_t  
  3. spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)  
  4. {  
  5.     struct spidev_data  *spidev;  
  6.     ssize_t         status = 0;  
  7.   
  8.     /* chipselect only toggles at start or end of operation */  
  9.     if (count > bufsiz)  /*如果读取的字节数大于缓冲区的大小,则报错*/  
  10.         return -EMSGSIZE;  
  11.   
  12.     spidev = filp->private_data; /*获取spidev*/  
  13.   
  14.     mutex_lock(&spidev->buf_lock);   /*加锁,对buffer进行互斥房屋内*/  
  15.     status = spidev_sync_read(spidev, count);  
  16.     if (status > 0) {  
  17.         unsigned long   missing;  
  18.   
  19.         missing = copy_to_user(buf, spidev->buffer, status);  
  20.         if (missing == status)  
  21.             status = -EFAULT;  
  22.         else  
  23.             status = status - missing;  
  24.     }  
  25.     mutex_unlock(&spidev->buf_lock);  
  26.   
  27.     return status;  
  28. }  
  29.   
  30. static inline ssize_t  
  31. spidev_sync_read(struct spidev_data *spidev, size_t len)  
  32. {  
  33.     struct spi_transfer    t = {  
  34.             .rx_buf        = spidev->buffer,  
  35.             .len        = len,  
  36.         };  
  37.     struct spi_message    m;  
  38.   
  39.     spi_message_init(&m);    /*初始化message*/  
  40.     spi_message_add_tail(&t, &m);    /*添加transfer*/  
  41.     return spidev_sync(spidev, &m);  
  42. }  

10. ioctl方法

这一章节中,我们将看一下SPI子系统是如何使用ioctl系统调用来实现全双工读写。

10.1 spi_ioc_transfer

在使用ioctl时,用户空间要使用一个数据结构来封装需要传输的数据,该结构为spi_ioc_transfe。而在write系统调用时,只是简单的从用户空间复制数据过来。该结构中的很多字段将被复制到spi_transfer结构中相应的字段。也就是说一个spi_ioc_transfer表示一个spi_transfer,用户空间可以定义多个spi_ioc_transfe,最后以数组形式传递给ioctl。

下面同时给出ioctl中cmd的值。其中SPI_IOC_MASSAGE用于实现全双工IO,而其他的用于设置或者读取某个特定值。

下列数据结构位于:include/linux/spi/spidev.h。

  1. /** 
  2.  * struct spi_ioc_transfer - describes a single SPI transfer 
  3.  * @tx_buf: Holds pointer to userspace buffer with transmit data, or null. 
  4.  *  If no data is provided, zeroes are shifted out. 
  5.  * @rx_buf: Holds pointer to userspace buffer for receive data, or null. 
  6.  * @len: Length of tx and rx buffers, in bytes. 
  7.  * @speed_hz: Temporary override of the device's bitrate. 
  8.  * @bits_per_word: Temporary override of the device's wordsize. 
  9.  * @delay_usecs: If nonzero, how long to delay after the last bit transfer 
  10.  *  before optionally deselecting the device before the next transfer. 
  11.  * @cs_change: True to deselect device before starting the next transfer. 
  12.  * 
  13.  * This structure is mapped directly to the kernel spi_transfer structure; 
  14.  * the fields have the same meanings, except of course that the pointers 
  15.  * are in a different address space (and may be of different sizes in some 
  16.  * cases, such as 32-bit i386 userspace over a 64-bit x86_64 kernel). 
  17.  * Zero-initialize the structure, including currently unused fields, to 
  18.  * accomodate potential future updates. 
  19.  * 
  20.  * SPI_IOC_MESSAGE gives userspace the equivalent of kernel spi_sync(). 
  21.  * Pass it an array of related transfers, they'll execute together. 
  22.  * Each transfer may be half duplex (either direction) or full duplex. 
  23.  * 
  24.  *  struct spi_ioc_transfer mesg[4]; 
  25.  *  ... 
  26.  *  status = ioctl(fd, SPI_IOC_MESSAGE(4), mesg); 
  27.  * 
  28.  * So for example one transfer might send a nine bit command (right aligned 
  29.  * in a 16-bit word), the next could read a block of 8-bit data before 
  30.  * terminating that command by temporarily deselecting the chip; the next 
  31.  * could send a different nine bit command (re-selecting the chip), and the 
  32.  * last transfer might write some register values. 
  33.  */  
  34. struct spi_ioc_transfer {  
  35.     __u64       tx_buf;  
  36.     __u64       rx_buf;  
  37.   
  38.     __u32       len;  
  39.     __u32       speed_hz;  
  40.   
  41.     __u16       delay_usecs;  
  42.     __u8        bits_per_word;  
  43.     __u8        cs_change;  
  44.     __u32       pad;  
  45.   
  46.     /* If the contents of 'struct spi_ioc_transfer' ever change 
  47.      * incompatibly, then the ioctl number (currently 0) must change; 
  48.      * ioctls with constant size fields get a bit more in the way of 
  49.      * error checking than ones (like this) where that field varies. 
  50.      * 
  51.      * NOTE: struct layout is the same in 64bit and 32bit userspace. 
  52.      */  
  53. };  
  54.   
  55. /* not all platforms use <asm-generic/ioctl.h> or _IOC_TYPECHECK() ... */  
  56. #define SPI_MSGSIZE(N) \                /*SPI_MSGSIZE不能大于4KB*/   
  57.     ((((N)*(sizeof (struct spi_ioc_transfer))) < (1 << _IOC_SIZEBITS)) \  
  58.         ? ((N)*(sizeof (struct spi_ioc_transfer))) : 0)  
  59. #define SPI_IOC_MESSAGE(N) _IOW(SPI_IOC_MAGIC, 0, char[SPI_MSGSIZE(N)])   
  60.   
  61.   
  62. /* Read / Write of SPI mode (SPI_MODE_0..SPI_MODE_3) */  
  63. #define SPI_IOC_RD_MODE            _IOR(SPI_IOC_MAGIC, 1, __u8)   
  64. #define SPI_IOC_WR_MODE            _IOW(SPI_IOC_MAGIC, 1, __u8)   
  65.   
  66. /* Read / Write SPI bit justification */  
  67. #define SPI_IOC_RD_LSB_FIRST        _IOR(SPI_IOC_MAGIC, 2, __u8)   
  68. #define SPI_IOC_WR_LSB_FIRST        _IOW(SPI_IOC_MAGIC, 2, __u8)   
  69.   
  70. /* Read / Write SPI device word length (1..N) */  
  71. #define SPI_IOC_RD_BITS_PER_WORD    _IOR(SPI_IOC_MAGIC, 3, __u8)   
  72. #define SPI_IOC_WR_BITS_PER_WORD    _IOW(SPI_IOC_MAGIC, 3, __u8)   
  73.   
  74. /* Read / Write SPI device default max speed hz */  
  75. #define SPI_IOC_RD_MAX_SPEED_HZ        _IOR(SPI_IOC_MAGIC, 4, __u32)   
  76. #define SPI_IOC_WR_MAX_SPEED_HZ        _IOW(SPI_IOC_MAGIC, 4, __u32)  
linux
相关资讯       嵌入式Linux  S3C2440 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款