8.7 s3c24xx_spi_txrx 和s3c24xx_spi_irq
下列代码位于deivers/spi/s3c24xx.c。
- static inline unsigned int hw_txbyte(struct s3c24xx_spi *hw, int count)
- {
- return hw->tx ? hw->tx[count] : 0; /*发送缓冲区指针是否为空,空则发送0*/
- }
- static int s3c24xx_spi_txrx(struct spi_device *spi, struct spi_transfer *t)/*bitbang.txrx_bufs方法*/
- {
- struct s3c24xx_spi *hw = to_hw(spi);
- dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n",
- t->tx_buf, t->rx_buf, t->len);
- /*保存transfer相关数据到s3c24xx_sp结构中*/
- hw->tx = t->tx_buf;
- hw->rx = t->rx_buf;
- hw->len = t->len;
- hw->count = 0;
- init_completion(&hw->done); /*初始化completion*/
- /* send the first byte */ /*发送第一个数据,tx[0]*/
- writeb(hw_txbyte(hw, 0), hw->regs + S3C2410_SPTDAT);
- wait_for_completion(&hw->done);/*等待completion*/
- return hw->count; /*返回发送的字节数*/
- }
- static irqreturn_t s3c24xx_spi_irq(int irq, void *dev)
- {
- struct s3c24xx_spi *hw = dev;
- unsigned int spsta = readb(hw->regs + S3C2410_SPSTA);/*获取状态寄存器*/
- unsigned int count = hw->count;
- if (spsta & S3C2410_SPSTA_DCOL) { /*发生错误*/
- dev_dbg(hw->dev, "data-collision\n");
- complete(&hw->done); /*唤醒等待complete的进程*/
- goto irq_done;
- }
- if (!(spsta & S3C2410_SPSTA_READY)) {/*未就绪*/
- dev_dbg(hw->dev, "spi not ready for tx?\n");
- complete(&hw->done); /*唤醒等待complete的进程*/
- goto irq_done;
- }
- hw->count++;/*增加计数*/
- if (hw->rx)
- hw->rx[count] = readb(hw->regs + S3C2410_SPRDAT);/*读取数据*/
- count++; /*增加计数*/
- if (count < hw->len) /*未发送完毕,则继续发送*/
- writeb(hw_txbyte(hw, count), hw->regs + S3C2410_SPTDAT);
- else
- complete(&hw->done); /*发送完毕,唤醒等待complete的进程*/
- irq_done:
- return IRQ_HANDLED;
- }
在s3c24xx_spi_txrx函数中,首先发送了待发送数据中的第一个字节,随后就调用wait_for_completion来等待剩余的数据发送完成。
NOTE:这里的completion是master驱动层的,spi设备驱动也有一个completion,用于IO同步,不要混淆。
当第一个数据发送完成以后,SPI中断产生,开始执行中断服务程序。在中断服务程序中,将判断是否需要读取数据,如果是则从寄存器中读取数据。
NOTE:如果是使用read系统调用,那么在此发送的数据将是0。
随后发送下一个数据,直到数据发送完成。发送完成后调用complete,使在s3c24xx_spi_txrx的wait_for_completion得以返回。接着,s3c24xx_spi_txrx就将返回已发送的字节数。
NOTE:其实该中断服务子程序实现了全双工数据的传输,只不过特定于具体的系统调用,从而分为了半双工读和写。
