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

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

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

2.5 spi_transfer

该数据结构是对一次完整的数据传输的描述。

  1.  /*
  2.  * I/O INTERFACE between SPI controller and protocol drivers 
  3.  * 
  4.  * Protocol drivers use a queue of spi_messages, each transferring data 
  5.  * between the controller and memory buffers. 
  6.  * 
  7.  * The spi_messages themselves consist of a series of read+write transfer 
  8.  * segments.  Those segments always read the same number of bits as they 
  9.  * write; but one or the other is easily ignored by passing a null buffer 
  10.  * pointer.  (This is unlike most types of I/O API, because SPI hardware 
  11.  * is full duplex.) 
  12.  * 
  13.  * NOTE:  Allocation of spi_transfer and spi_message memory is entirely 
  14.  * up to the protocol driver, which guarantees the integrity of both (as 
  15.  * well as the data buffers) for as long as the message is queued. 
  16.  */  
  17.   
  18. /** 
  19.  * struct spi_transfer - a read/write buffer pair 
  20.  * @tx_buf: data to be written (dma-safe memory), or NULL 
  21.  * @rx_buf: data to be read (dma-safe memory), or NULL 
  22.  * @tx_dma: DMA address of tx_buf, if @spi_message.is_dma_mapped 
  23.  * @rx_dma: DMA address of rx_buf, if @spi_message.is_dma_mapped 
  24.  * @len: size of rx and tx buffers (in bytes) 
  25.  * @speed_hz: Select a speed other than the device default for this 
  26.  *      transfer. If 0 the default (from @spi_device) is used. 
  27.  * @bits_per_word: select a bits_per_word other than the device default 
  28.  *      for this transfer. If 0 the default (from @spi_device) is used. 
  29.  * @cs_change: affects chipselect after this transfer completes 
  30.  * @delay_usecs: microseconds to delay after this transfer before 
  31.  *  (optionally) changing the chipselect status, then starting 
  32.  *  the next transfer or completing this @spi_message. 
  33.  * @transfer_list: transfers are sequenced through @spi_message.transfers 
  34.  * 
  35.  * SPI transfers always write the same number of bytes as they read. 
  36.  * Protocol drivers should always provide @rx_buf and/or @tx_buf. 
  37.  * In some cases, they may also want to provide DMA addresses for 
  38.  * the data being transferred; that may reduce overhead, when the 
  39.  * underlying driver uses dma. 
  40.  * 
  41.  * If the transmit buffer is null, zeroes will be shifted out 
  42.  * while filling @rx_buf.  If the receive buffer is null, the data 
  43.  * shifted in will be discarded.  Only "len" bytes shift out (or in). 
  44.  * It's an error to try to shift out a partial word.  (For example, by 
  45.  * shifting out three bytes with word size of sixteen or twenty bits; 
  46.  * the former uses two bytes per word, the latter uses four bytes.) 
  47.  * 
  48.  * In-memory data values are always in native CPU byte order, translated 
  49.  * from the wire byte order (big-endian except with SPI_LSB_FIRST).  So 
  50.  * for example when bits_per_word is sixteen, buffers are 2N bytes long 
  51.  * (@len = 2N) and hold N sixteen bit words in CPU byte order. 
  52.  * 
  53.  * When the word size of the SPI transfer is not a power-of-two multiple 
  54.  * of eight bits, those in-memory words include extra bits.  In-memory 
  55.  * words are always seen by protocol drivers as right-justified, so the 
  56.  * undefined (rx) or unused (tx) bits are always the most significant bits. 
  57.  * 
  58.  * All SPI transfers start with the relevant chipselect active.  Normally 
  59.  * it stays selected until after the last transfer in a message.  Drivers 
  60.  * can affect the chipselect signal using cs_change. 
  61.  * 
  62.  * (i) If the transfer isn't the last one in the message, this flag is 
  63.  * used to make the chipselect briefly go inactive in the middle of the 
  64.  * message.  Toggling chipselect in this way may be needed to terminate 
  65.  * a chip command, letting a single spi_message perform all of group of 
  66.  * chip transactions together. 
  67.  * 
  68.  * (ii) When the transfer is the last one in the message, the chip may 
  69.  * stay selected until the next transfer.  On multi-device SPI busses 
  70.  * with nothing blocking messages going to other devices, this is just 
  71.  * a performance hint; starting a message to another device deselects 
  72.  * this one.  But in other cases, this can be used to ensure correctness. 
  73.  * Some devices need protocol transactions to be built from a series of 
  74.  * spi_message submissions, where the content of one message is determined 
  75.  * by the results of previous messages and where the whole transaction 
  76.  * ends when the chipselect goes intactive. 
  77.  * 
  78.  * The code that submits an spi_message (and its spi_transfers) 
  79.  * to the lower layers is responsible for managing its memory. 
  80.  * Zero-initialize every field you don't set up explicitly, to 
  81.  * insulate against future API updates.  After you submit a message 
  82.  * and its transfers, ignore them until its completion callback. 
  83.  */  
  84. struct spi_transfer {  
  85.     /* it's ok if tx_buf == rx_buf (right?) 
  86.      * for MicroWire, one buffer must be null 
  87.      * buffers must work with dma_*map_single() calls, unless 
  88.      *   spi_message.is_dma_mapped reports a pre-existing mapping 
  89.      */  
  90.     const void  *tx_buf;  
  91.     void        *rx_buf;  
  92.     unsigned    len;  
  93.   
  94.     dma_addr_t  tx_dma;  
  95.     dma_addr_t  rx_dma;  
  96.   
  97.     unsigned    cs_change:1;  
  98.     u8      bits_per_word;  
  99.     u16     delay_usecs;  
  100.     u32     speed_hz;  
  101.   
  102.     struct list_head transfer_list;  
  103. };

2.6 spi_message

该结构就是对多个spi_transfer的封装。

  1. /** 
  2.  * struct spi_message - one multi-segment SPI transaction 
  •  * @transfers: list of transfer segments in this transaction 
  •  * @spi: SPI device to which the transaction is queued 
  •  * @is_dma_mapped: if true, the caller provided both dma and cpu virtual 
  •  *  addresses for each transfer buffer 
  •  * @complete: called to report transaction completions 
  •  * @context: the argument to complete() when it's called 
  •  * @actual_length: the total number of bytes that were transferred in all 
  •  *  successful segments 
  •  * @status: zero for success, else negative errno 
  •  * @queue: for use by whichever driver currently owns the message 
  •  * @state: for use by whichever driver currently owns the message 
  •  * 
  •  * A @spi_message is used to execute an atomic sequence of data transfers, 
  •  * each represented by a struct spi_transfer.  The sequence is "atomic" 
  •  * in the sense that no other spi_message may use that SPI bus until that 
  •  * sequence completes.  On some systems, many such sequences can execute as 
  •  * as single programmed DMA transfer.  On all systems, these messages are 
  •  * queued, and might complete after transactions to other devices.  Messages 
  •  * sent to a given spi_device are alway executed in FIFO order. 
  •  * 
  •  * The code that submits an spi_message (and its spi_transfers) 
  •  * to the lower layers is responsible for managing its memory. 
  •  * Zero-initialize every field you don't set up explicitly, to 
  •  * insulate against future API updates.  After you submit a message 
  •  * and its transfers, ignore them until its completion callback. 
  •  */  
  • struct spi_message {  
  •     struct list_head    transfers;  
  •   
  •     struct spi_device   *spi;  
  •   
  •     unsigned        is_dma_mapped:1;  
  •   
  •     /* REVISIT:  we might want a flag affecting the behavior of the 
  •      * last transfer ... allowing things like "read 16 bit length L" 
  •      * immediately followed by "read L bytes".  Basically imposing 
  •      * a specific message scheduling algorithm. 
  •      * 
  •      * Some controller drivers (message-at-a-time queue processing) 
  •      * could provide that as their default scheduling algorithm.  But 
  •      * others (with multi-message pipelines) could need a flag to 
  •      * tell them about such special cases. 
  •      */  
  •   
  •     /* completion is reported through a callback */  
  •     void            (*complete)(void *context);  
  •     void            *context;  
  •     unsigned        actual_length;  
  •     int         status;  
  •   
  •     /* for optional use by whatever driver currently owns the 
  •      * spi_message ...  between calls to spi_async and then later 
  •      * complete(), that's the spi_master controller driver. 
  •      */  
  •     struct list_head    queue;  
  •     void            *state;  
  • };
  • 2.7 spi_bitbang

    1. struct spi_bitbang {  
    2.     struct workqueue_struct *workqueue;  
    3.     struct work_struct  work;  
    4.   
    5.     spinlock_t      lock;  
    6.     struct list_head    queue;  
    7.     u8          busy;  
    8.     u8          use_dma;  
    9.     u8          flags;      /* extra spi->mode support */  
    10.   
    11.     struct spi_master   *master;  
    12.   
    13.     /* setup_transfer() changes clock and/or wordsize to match settings 
    14.      * for this transfer; zeroes restore defaults from spi_device. 
    15.      */  
    16.     int (*setup_transfer)(struct spi_device *spi,  
    17.             struct spi_transfer *t);  
    18.   
    19.     void    (*chipselect)(struct spi_device *spi, int is_on);  
    20. #define BITBANG_CS_ACTIVE   1   /* normally nCS, active low */   
    21. #define BITBANG_CS_INACTIVE 0   
    22.   
    23.     /* txrx_bufs() may handle dma mapping for transfers that don't 
    24.      * already have one (transfer.{tx,rx}_dma is zero), or use PIO 
    25.      */  
    26.     int (*txrx_bufs)(struct spi_device *spi, struct spi_transfer *t);  
    27.   
    28.     /* txrx_word[SPI_MODE_*]() just looks like a shift register */  
    29.     u32 (*txrx_word[4])(struct spi_device *spi,  
    30.             unsigned nsecs,  
    31.             u32 word, u8 bits);  
    32. };
    linux
    相关资讯       嵌入式Linux  S3C2440  SPI 
    本文评论   查看全部评论 (0)
    表情: 表情 姓名: 字数

           

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