来自:http://blog.csdn.net/woshixingaaa/article/details/6574224
这篇文档主要介绍spi数据传输过程。
当应用层要向设备传输数据的时候,会通过ioctl向设备驱动发送传输数据的命令。如图,向SPI从设备发送读写命令,实际的读写操作还是调用了主机控制
器驱动的数据传输函数。transfer函数用于spi的IO传输。但是,transfer函数一般不会执行真正的传输操作,而是把要传输的内容放到一个
队列里,然后调用一种类似底半部的机制进行真正的传输。这是因为,spi总线一般会连多个spi设备,而spi设备间的访问可能会并发。如果直接在
transfer函数中实现传输,那么会产生竞态,spi设备互相间会干扰。所以,真正的spi传输与具体的spi控制器的实现有关,spi的框架代码中
没有涉及。像spi设备的片选,根据具体设备进行时钟调整等等都在实现传输的代码中被调用。spi的传输命令都是通过结构spi_message定义,设
备程序调用transfer函数将spi_message交给spi总线驱动,总线驱动再将message传到底半部排队,实现串行化传输。
在spidev.c中实现了file_operations:
-
"font-size:18px;">static struct file_operations spidev_fops = {
-
.owner = THIS_MODULE,
-
.write = spidev_write,
-
.read = spidev_read,
-
.unlocked_ioctl = spidev_ioctl,
-
.open = spidev_open,
-
.release = spidev_release,
-
};
这里看spidev_ioctl的实现:
-
"font-size:18px;">static long
-
spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
-
{
-
int err = 0;
-
int retval = 0;
-
struct spidev_data *spidev;
-
struct spi_device *spi;
-
u32 tmp;
-
unsigned n_ioc;
-
struct spi_ioc_transfer *ioc;
-
-
-
if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
-
return -ENOTTY;
-
-
-
if (_IOC_DIR(cmd) & _IOC_READ)
-
err = !access_ok(VERIFY_WRITE,
-
(void __user *)arg, _IOC_SIZE(cmd));
-
-
if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE)
-
err = !access_ok(VERIFY_READ,
-
(void __user *)arg, _IOC_SIZE(cmd));
-
if (err)
-
return -EFAULT;
-
-
-
-
-
spidev = filp->private_data;
-
spin_lock_irq(&spidev->spi_lock);
-
spi = spi_dev_get(spidev->spi);
-
spin_unlock_irq(&spidev->spi_lock);
-
-
if (spi == NULL)
-
return -ESHUTDOWN;
-
-
mutex_lock(&spidev->buf_lock);
-
-
switch (cmd) {
-
-
case SPI_IOC_RD_MODE:
-
-
retval = __put_user(spi->mode & SPI_MODE_MASK,
-
(__u8 __user *)arg);
-
break;
-
case SPI_IOC_RD_LSB_FIRST:
-
retval = __put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,
-
(__u8 __user *)arg);
-
break;
-
case SPI_IOC_RD_BITS_PER_WORD:
-
retval = __put_user(spi->bits_per_word, (__u8 __user *)arg);
-
break;
-
case SPI_IOC_RD_MAX_SPEED_HZ:
-
retval = __put_user(spi->max_speed_hz, (__u32 __user *)arg);
-
break;
-
-
-
case SPI_IOC_WR_MODE:
-
retval = __get_user(tmp, (u8 __user *)arg);
-
if (retval == 0) {
-
-
u8 save = spi->mode;
-
-
if (tmp & ~SPI_MODE_MASK) {
-
retval = -EINVAL;
-
break;
-
}
-
-
tmp |= spi->mode & ~SPI_MODE_MASK;
-
spi->mode = (u8)tmp;
-
retval = spi_setup(spi);
-
if (retval < 0)
-
spi->mode = save;
-
else
-
dev_dbg(&spi->dev, "spi mode %02x\n", tmp);
-
}
-
break;
-
case SPI_IOC_WR_LSB_FIRST:
-
retval = __get_user(tmp, (__u8 __user *)arg);
-
if (retval == 0) {
-
u8 save = spi->mode;
-
-
if (tmp)
-
spi->mode |= SPI_LSB_FIRST;
-
else
-
spi->mode &= ~SPI_LSB_FIRST;
-
retval = spi_setup(spi);
-
if (retval < 0)
-
spi->mode = save;
-
else
-
dev_dbg(&spi->dev, "%csb first\n",
-
tmp ? 'l' : 'm');
-
}
-
break;
-
case SPI_IOC_WR_BITS_PER_WORD:
-
retval = __get_user(tmp, (__u8 __user *)arg);
-
if (retval == 0) {
-
u8 save = spi->bits_per_word;
-
-
spi->bits_per_word = tmp;
-
retval = spi_setup(spi);
-
if (retval < 0)
-
spi->bits_per_word = save;
-
else
-
dev_dbg(&spi->dev, "%d bits per word\n", tmp);
-
}
-
break;
-
case SPI_IOC_WR_MAX_SPEED_HZ:
-
retval = __get_user(tmp, (__u32 __user *)arg);
-
if (retval == 0) {
-
u32 save = spi->max_speed_hz;
-
-
spi->max_speed_hz = tmp;
-
retval = spi_setup(spi);
-
if (retval < 0)
-
spi->max_speed_hz = save;
-
else
-
dev_dbg(&spi->dev, "%d Hz (max)\n", tmp);
-
}
-
break;
-
-
default:
-
-
if (_IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
-
|| _IOC_DIR(cmd) != _IOC_WRITE) {
-
retval = -ENOTTY;
-
break;
-
}
-
-
tmp = _IOC_SIZE(cmd);
-
-
if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) {
-
retval = -EINVAL;
-
break;
-
}
-
-
n_ioc = tmp / sizeof(struct spi_ioc_transfer);
-
if (n_ioc == 0)
-
break;
-
-
-
ioc = kmalloc(tmp, GFP_KERNEL);
-
if (!ioc) {
-
retval = -ENOMEM;
-
break;
-
}
-
-
if (__copy_from_user(ioc, (void __user *)arg, tmp)) {
-
kfree(ioc);
-
retval = -EFAULT;
-
break;
-
}
-
-
-
"color:#ff0000;">retval = spidev_message(spidev, ioc, n_ioc);
-
kfree(ioc);
-
break;
-
}
-
-
mutex_unlock(&spidev->buf_lock);
-
spi_dev_put(spi);
-
return retval;
-
}
-
下面跟踪spidev_message看看:
-
"font-size:18px;">static int spidev_message(struct spidev_data *spidev,
-
struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
-
{
-
struct spi_message msg;
-
struct spi_transfer *k_xfers;
-
struct spi_transfer *k_tmp;
-
struct spi_ioc_transfer *u_tmp;
-
unsigned n, total;
-
u8 *buf;
-
int status = -EFAULT;
-
-
spi_message_init(&msg);
-
-
k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);
-
if (k_xfers == NULL)
-
return -ENOMEM;
-
-
buf = spidev->buffer;
-
total = 0;
-
-
for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
-
n;
-
n--, k_tmp++, u_tmp++) {
-
-
k_tmp->len = u_tmp->len;
-
-
total += k_tmp->len;
-
if (total > bufsiz) {
-
status = -EMSGSIZE;
-
goto done;
-
}
-
-
if (u_tmp->rx_buf) {
-
k_tmp->rx_buf = buf;
-
if (!access_ok(VERIFY_WRITE, (u8 __user *)
-
(uintptr_t) u_tmp->rx_buf,
-
u_tmp->len))
-
goto done;
-
}
-
-
if (u_tmp->tx_buf) {
-
k_tmp->tx_buf = buf;
-
if (copy_from_user(buf, (const u8 __user *)
-
(uintptr_t) u_tmp->tx_buf,
-
u_tmp->len))
-
goto done;
-
}
-
-
buf += k_tmp->len;
-
-
k_tmp->cs_change = !!u_tmp->cs_change;
-
-
k_tmp->bits_per_word = u_tmp->bits_per_word;
-
-
k_tmp->delay_usecs = u_tmp->delay_usecs;
-
-
k_tmp->speed_hz = u_tmp->speed_hz;
-
-
spi_message_add_tail(k_tmp, &msg);
-
}
-
-
"color:#ff0000;">status = spidev_sync(spidev, &msg);
-
if (status < 0)
-
goto done;
-
-
-
buf = spidev->buffer;
-
-
for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) {
-
if (u_tmp->rx_buf) {
-
if (__copy_to_user((u8 __user *)
-
(uintptr_t) u_tmp->rx_buf, buf,
-
u_tmp->len)) {
-
status = -EFAULT;
-
goto done;
-
}
-
}
-
buf += u_tmp->len;
-
}
-
status = total;
-
-
done:
-
kfree(k_xfers);
-
return status;
-
}
-
看spidev_sync的实现:
-
"font-size:18px;">static ssize_t
-
spidev_sync(struct spidev_data *spidev, struct spi_message *message)
-
{
-
-
DECLARE_COMPLETION_ONSTACK(done);
-
int status;
-
-
message->complete = spidev_complete;
-
message->context = &done;
-
-
spin_lock_irq(&spidev->spi_lock);
-
if (spidev->spi == NULL)
-
status = -ESHUTDOWN;
-
else
-
-
"color:#ff0000;"> status = spi_async(spidev->spi, message);
-
spin_unlock_irq(&spidev->spi_lock);
-
-
if (status == 0) {
-
-
wait_for_completion(&done);
-
status = message->status;
-
if (status == 0)
-
status = message->actual_length;
-
}
-
return status;
-
}
-
spi_async在spi.h中定义的:
-
"font-size:18px;">static inline int
-
spi_async(struct spi_device *spi, struct spi_message *message)
-
{
-
message->spi = spi;
-
return spi->master->transfer(spi, message);
-
}
-
这里的master->transfer是在spi_bitbang_start中进行赋值的:
bitbang->master->transfer=
spi_bitbang_transfer;
看spi_bitbang_transfer的实现:
-
"font-size:18px;">int spi_bitbang_transfer(struct spi_device *spi, struct spi_message *m)
-
{
-
struct spi_bitbang *bitbang;
-
unsigned long flags;
-
int status = 0;
-
-
m->actual_length = 0;
-
m->status = -EINPROGRESS;
-
-
bitbang = spi_master_get_devdata(spi->master);
-
-
spin_lock_irqsave(&bitbang->lock, flags);
-
if (!spi->max_speed_hz)
-
status = -ENETDOWN;
-
else {
-
-
list_add_tail(&m->queue, &bitbang->queue);
-
-
queue_work(bitbang->workqueue, &bitbang->work);
-
}
-
spin_unlock_irqrestore(&bitbang->lock, flags);
-
-
return status;
-
}
-
EXPORT_SYMBOL_GPL(spi_bitbang_transfer);
-
分析工作队列的处理函数:
-
"font-size:18px;">static void bitbang_work(struct work_struct *work)
-
{
-
struct spi_bitbang *bitbang =
-
container_of(work, struct spi_bitbang, work);
-
unsigned long flags;
-
-
spin_lock_irqsave(&bitbang->lock, flags);
-
-
bitbang->busy = 1;
-
-
while (!list_empty(&bitbang->queue)) {
-
struct spi_message *m;
-
struct spi_device *spi;
-
unsigned nsecs;
-
struct spi_transfer *t = NULL;
-
unsigned tmp;
-
unsigned cs_change;
-
int status;
-
int (*setup_transfer)(struct spi_device *,
-
struct spi_transfer *);
-
-
m = container_of(bitbang->queue.next, struct spi_message,
-
queue);
-
-
list_del_init(&m->queue);
-
spin_unlock_irqrestore(&bitbang->lock, flags);
-
-
nsecs = 100;
-
-
spi = m->spi;
-
tmp = 0;
-
cs_change = 1;
-
status = 0;
-
setup_transfer = NULL;
-
-
list_for_each_entry (t, &m->transfers, transfer_list) {
-
。。。。。。。。。。。。。。。。。
-
if (t->len) {
-
if (!m->is_dma_mapped)
-
t->rx_dma = t->tx_dma = 0;
-
-
"color:#ff0000;">status = bitbang->txrx_bufs(spi, t);
-
}
-
。。。。。。。。。。。。。。。。
-
-
m->status = status;
-
-
m->complete(m->context);
-
-
-
if (setup_transfer)
-
setup_transfer(spi, NULL);
-
if (!(status == 0 && cs_change)) {
-
ndelay(nsecs);
-
bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
-
ndelay(nsecs);
-
}
-
-
spin_lock_irqsave(&bitbang->lock, flags);
-
}
-
bitbang->busy = 0;
-
spin_unlock_irqrestore(&bitbang->lock, flags);
-
}
-
这个工作队列的处理函数中调用了spi controller driver中的传输函数:
-
"font-size:18px;">static int s3c24xx_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
-
{
-
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);
-
-
hw->tx = t->tx_buf;
-
hw->rx = t->rx_buf;
-
hw->len = t->len;
-
hw->count = 0;
-
-
init_completion(&hw->done);
-
-
-
-
-
-
writeb(hw_txbyte(hw, 0), hw->regs + S3C2410_SPTDAT);
-
-
wait_for_completion(&hw->done);
-
return hw->count;
-
}
-
static inline unsigned int hw_txbyte(struct s3c24xx_spi *hw, int count)
-
{
-
return hw->tx ? hw->tx[count] : 0xff;
-
-
}
-
下面来分析中断函数:
-
"font-size:18px;">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);
-
goto irq_done;
-
}
-
-
if (!(spsta & S3C2410_SPSTA_READY)) {
-
dev_dbg(hw->dev, "spi not ready for tx?\n");
-
-
complete(&hw->done);
-
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);
-
-
irq_done:
-
return IRQ_HANDLED;
-
}
-
至
此spi数据传输过程完成,如果不想为自己的SPI设备写驱动,那么可以用Linux自带的spidev.c提供的驱动程序,只要在登记时,把设备名设置
成spidev就可以了。spidev.c会在device目录下自动为每一个匹配的SPI设备创建设备节点,节点名"spi%d"。之后,用户程序可以
通过字符型设备的通用接口控制SPI设备。需要注意的是,spidev创建的设备在设备模型中属于虚拟设备,他的class是spidev_class,
他的父设备是在boardinfo中定义的spi设备。
阅读(1958) | 评论(0) | 转发(0) |