一.spi设备
struct spi_device {
struct device dev; //设备文件
struct spi_master *master; //spi主机
u32 max_speed_hz; //最大速率
u8 chip_select; //片选
u8 mode; //模式
u8 bits_per_word; //一个字有多少位
int irq; //中断号
void *controller_state; //控制器状态
void *controller_data; //控制器数据
char modalias[SPI_NAME_SIZE];//名字
};
2.spi传输模式:
#define SPI_CPHA 0x01 //时钟相位
#define SPI_CPOL 0x02 //时钟继续
#define SPI_MODE_0 (0|0) //模式0
#define SPI_MODE_1 (0|SPI_CPHA) //模式1
#define SPI_MODE_2 (SPI_CPOL|0) //模式2
#define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) //模式3
#define SPI_CS_HIGH 0x04 //片选高电平
#define SPI_LSB_FIRST 0x08 //LSB
#define SPI_3WIRE 0x10 //3线模式 SI和SO同一根线
#define SPI_LOOP 0x20 //回送模式
#define SPI_NO_CS 0x40 //单个设备占用一根SPI总线,所以没片选
#define SPI_READY 0x80 //从机拉低电平停止数据传输
3.spi设备的添加spi_new_device
struct spi_device *spi_new_device(struct spi_master *master,struct spi_board_info *chip)
{
struct spi_device *proxy;
int status;
proxy = spi_alloc_device(master); //3.1 spi设备初始化
if (!proxy)
return NULL;
WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
proxy->chip_select = chip->chip_select; //片选
proxy->max_speed_hz = chip->max_speed_hz; //最大速率
proxy->mode = chip->mode; //模式
proxy->irq = chip->irq; //中断号
strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
proxy->dev.platform_data = (void *) chip->platform_data;
proxy->controller_data = chip->controller_data;
proxy->controller_state = NULL;
status = spi_add_device(proxy); //3.2 添加spi设备
if (status < 0) {
spi_dev_put(proxy); //增加spi设备引用计数
return NULL;
}
return proxy;
}
EXPORT_SYMBOL_GPL(spi_new_device);
3.1.分配spi设备
struct spi_device *spi_alloc_device(struct spi_master *master)
{
struct spi_device *spi;
struct device *dev = master->dev.parent;
if (!spi_master_get(master)) //判断spi主机是否存在
return NULL;
spi = kzalloc(sizeof *spi, GFP_KERNEL); //分配内存
if (!spi) {
dev_err(dev, "cannot alloc spi_device\n");
spi_master_put(master); //增加主机引用计数
return NULL;
}
spi->master = master; //设置spi主机
spi->dev.parent = dev; //spi设备文件的父设备为spi主机设备文件的父设备
spi->dev.bus = &spi_bus_type; //总线类型
spi->dev.release = spidev_release; //释放方法
device_initialize(&spi->dev); //设备初始化
return spi;
}
EXPORT_SYMBOL_GPL(spi_alloc_device);
3.2 添加spi设备
int spi_add_device(struct spi_device *spi)
{
static DEFINE_MUTEX(spi_add_lock);
struct device *dev = spi->master->dev.parent;
struct device *d;
int status;
if (spi->chip_select >= spi->master->num_chipselect) {
dev_err(dev, "cs%d >= max %d\n",spi->chip_select,spi->master->num_chipselect);
return -EINVAL;
}
dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),spi->chip_select);
mutex_lock(&spi_add_lock);
d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev)); //查找总线上的spi设备
if (d != NULL) { //判断是否已经在使用了
dev_err(dev, "chipselect %d already in use\n",spi->chip_select);
put_device(d);
status = -EBUSY;
goto done;
}
status = spi_setup(spi); //调用spi主机 setup方法
if (status < 0) {
dev_err(dev, "can't setup %s, status %d\n",dev_name(&spi->dev), status);
goto done;
}
status = device_add(&spi->dev); //添加设备
if (status < 0)
dev_err(dev, "can't add %s, status %d\n",dev_name(&spi->dev), status);
else
dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
done:
mutex_unlock(&spi_add_lock);
return status;
}
EXPORT_SYMBOL_GPL(spi_add_device);
3.2.1 spi setup方法
int spi_setup(struct spi_device *spi)
{
unsigned bad_bits;
int status;
bad_bits = spi->mode & ~spi->master->mode_bits; //比较spi设备的模式和spi主机支持的模式
if (bad_bits) { //存在不支持的模式
dev_err(&spi->dev, "setup: unsupported mode bits %x\n",bad_bits);
return -EINVAL;
}
if (!spi->bits_per_word) //若没设置设备的每个字含多少位
spi->bits_per_word = 8; //则默认设置为8
status = spi->master->setup(spi); //调用spi主机的setup方法
dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s""%u bits/w, %u Hz max --> %d\n",
(int) (spi->mode & (SPI_CPOL | SPI_CPHA)),(spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
(spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",(spi->mode & SPI_3WIRE) ? "3wire, " : "",
(spi->mode & SPI_LOOP) ? "loopback, " : "",spi->bits_per_word, spi->max_speed_hz,status);
return status;
}
EXPORT_SYMBOL_GPL(spi_setup);
二.spi板级设备
1.板级设备结构体
struct spi_board_info {
char modalias[SPI_NAME_SIZE]; //名字
const void *platform_data; //平台数据
void *controller_data; //控制器数据
int irq; //中断号
u32 max_speed_hz; //最大速率
u16 bus_num; //spi总线编号
u16 chip_select; //片选
u8 mode; //模式
};
2.板级设备注册(静态注册,一般在板级初始化函数中调用)
int __init spi_register_board_info(struct spi_board_info const *info, unsigned n)
{
struct boardinfo *bi;
int i;
bi = kzalloc(n * sizeof(*bi), GFP_KERNEL); //分配内存
if (!bi)
return -ENOMEM;
for (i = 0; i < n; i++, bi++, info++) {
struct spi_master *master;
memcpy(&bi->board_info, info, sizeof(*info)); //设置bi的板级信息
mutex_lock(&board_lock);
list_add_tail(&bi->list, &board_list); //添加bi->list到全局board_list链表
list_for_each_entry(master, &spi_master_list, list) //遍历spi主机链表
spi_match_master_to_boardinfo(master, &bi->board_info);
mutex_unlock(&board_lock);
}
return 0;
}
2.1.spi板级设备与spi主机匹配
static void spi_match_master_to_boardinfo(struct spi_master *master,struct spi_board_info *bi)
{
struct spi_device *dev;
if (master->bus_num != bi->bus_num)
return;
dev = spi_new_device(master, bi);
if (!dev)
dev_err(master->dev.parent, "can't create new device for %s\n",bi->modalias);
}
三.spi设备驱动
1.spi设备驱动结构体
struct spi_driver {
const struct spi_device_id *id_table; //spi设备id表
int (*probe)(struct spi_device *spi); //probe方法(探测到设备)
int (*remove)(struct spi_device *spi); //remove方法(设备移除)
void (*shutdown)(struct spi_device *spi); //shutdown方法(关闭设备)
int (*suspend)(struct spi_device *spi, pm_message_t mesg); //suspend方法(挂起设备)
int (*resume)(struct spi_device *spi); //resume方法(唤醒设备)
struct device_driver driver; //设备驱动文件
};
2.spi设备驱动注册
int spi_register_driver(struct spi_driver *sdrv)
{
sdrv->driver.bus = &spi_bus_type; //总线类型
if (sdrv->probe) //若存在probe方法
sdrv->driver.probe = spi_drv_probe; //设置其设备驱动文件的probe方法为spi_drv_probe
if (sdrv->remove) //若存在remove方法
sdrv->driver.remove = spi_drv_remove; //设置其设备驱动文件的remove方法为spi_drv_remove
if (sdrv->shutdown) 若存在shutdown方法
sdrv->driver.shutdown = spi_drv_shutdown; //设置其设备驱动文件的shutdown方法为spi_drv_shutdown
return driver_register(&sdrv->driver); //注册设备驱动
}
EXPORT_SYMBOL_GPL(spi_register_driver);
这里的probe方法会在设备与驱动匹配的时候给调用
参看really_probe函数的部分代码
if (dev->bus->probe) { //若总线有probe方法(spi子系统的没有)
ret = dev->bus->probe(dev); //则调用总线的probe方法
if (ret)
goto probe_failed;
}
else if (drv->probe) { //若存在设备驱动的probe方法
ret = drv->probe(dev); //则调用设备驱动的probe方法
if (ret)
goto probe_failed;
}
2.1 spi_drv_probe
static int spi_drv_probe(struct device *dev)
{
const struct spi_driver *sdrv = to_spi_driver(dev->driver); //根据设备文件的设备驱动找到spi设备驱动
return sdrv->probe(to_spi_device(dev)); //调用spi设备驱动的probe方法
}
3.spi设备驱动注销
static inline void spi_unregister_driver(struct spi_driver *sdrv)
{
if (sdrv)
driver_unregister(&sdrv->driver); //注销设备驱动
}
四.spi主机
1.spi主机结构体
struct spi_master {
struct device dev; //spi主机设备文件
struct list_head list;
s16 bus_num; //spi总线号
u16 num_chipselect; //片选号
u16 dma_alignment; //dma算法
u16 mode_bits; //模式位
u16 flags; //传输类型标志
spinlock_t bus_lock_spinlock; //spi总线自旋锁
struct mutex bus_lock_mutex; //spi总线互斥锁
bool bus_lock_flag; //上锁标志
int (*setup)(struct spi_device *spi); //setup方法
int (*transfer)(struct spi_device *spi,struct spi_message *mesg); //传输方法
void (*cleanup)(struct spi_device *spi); //cleanup方法
};
1.2.flags标志
#define SPI_MASTER_HALF_DUPLEX BIT(0) //半双工
#define SPI_MASTER_NO_RX BIT(1) //不读
#define SPI_MASTER_NO_TX BIT(2) //不写
2.spi主机初始化spi_alloc_master
struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
{
struct spi_master *master;
if (!dev)
return NULL;
master = kzalloc(size + sizeof *master, GFP_KERNEL); //分配内存
if (!master)
return NULL;
device_initialize(&master->dev); //初始化主机设备文件
master->dev.class = &spi_master_class; //指定设备类spi_master_class
master->dev.parent = get_device(dev); //设置spi主机设备的父设备
spi_master_set_devdata(master, &master[1]); //设置设备数据
return master;
}
EXPORT_SYMBOL_GPL(spi_alloc_master);
3.注册spi主机
int spi_register_master(struct spi_master *master)
{
static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
struct device *dev = master->dev.parent; //获得spi主机设备的父设备
struct boardinfo *bi;
int status = -ENODEV;
int dynamic = 0;
if (!dev)
return -ENODEV;
if (master->num_chipselect == 0) //判断片选个数
return -EINVAL;
if (master->bus_num < 0) { //验证spi总线编号
master->bus_num = atomic_dec_return(&dyn_bus_id);
dynamic = 1;
}
spin_lock_init(&master->bus_lock_spinlock);
mutex_init(&master->bus_lock_mutex);
master->bus_lock_flag = 0;
dev_set_name(&master->dev, "spi%u", master->bus_num); //设置spi主机设备名
status = device_add(&master->dev); //添加spi主机设备
if (status < 0)
goto done;
dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),dynamic ? " (dynamic)" : "");
mutex_lock(&board_lock);
list_add_tail(&master->list, &spi_master_list); //spi主机list链表添加进全局spi_master_list链表
list_for_each_entry(bi, &board_list, list) //遍历全局board_list查找bi结构体
spi_match_master_to_boardinfo(master, &bi->board_info); //找到匹配的板级spi设备
mutex_unlock(&board_lock);
status = 0;
of_register_spi_devices(master);
done:
return status;
}
EXPORT_SYMBOL_GPL(spi_register_master);
3.1 匹配spi主机和板级spi设备
static void spi_match_master_to_boardinfo(struct spi_master *master,struct spi_board_info *bi)
{
struct spi_device *dev;
if (master->bus_num != bi->bus_num) //判断是否所属的spi总线
return;
dev = spi_new_device(master, bi); //添加新的spi设备
if (!dev)
dev_err(master->dev.parent, "can't create new device for %s\n",bi->modalias);
}
在注册板级设备或主机设备的时候都会添加
spi板级设备添加进board_list链表,spi主机设备添加进spi_master_list链表
不管是先注册spi板级设备还是先注册spi主机设备
都会调用list_for_each_entry遍历对应的要匹配的设备的链表,查找是否有匹配的例子
若找到都会调用spi_match_master_to_boardinfo函数添加spi设备
4.注销spi主机
void spi_unregister_master(struct spi_master *master)
{
int dummy;
mutex_lock(&board_lock);
list_del(&master->list); //删除链表
mutex_unlock(&board_lock);
dummy = device_for_each_child(&master->dev, NULL, __unregister); //调用__unregister函数注销子设备
device_unregister(&master->dev); //注销设备
}
EXPORT_SYMBOL_GPL(spi_unregister_master);
4.1 注销挂载该总线上的spi子设备
static int __unregister(struct device *dev, void *null)
{
spi_unregister_device(to_spi_device(dev));
return 0;
}
5.spi主机设备类
static struct class spi_master_class = {
.name = "spi_master",
.owner = THIS_MODULE,
.dev_release = spi_master_release,
};
五.spi总线
1.spi总线结构体
struct bus_type spi_bus_type = {
.name = "spi",
.dev_attrs = spi_dev_attrs,
.match = spi_match_device, //匹配方法
.uevent = spi_uevent,
.suspend = spi_suspend,
.resume = spi_resume,
};
EXPORT_SYMBOL_GPL(spi_bus_type);
2.设备匹配方法spi_match_device
前面的匹配方法是spi板级设备与spi主机设备的匹配方法,匹配的结果是添加新spi设备spi_new_device
这里的匹配是spi设备和spi驱动的匹配,匹配的结果是会调用spi驱动的设备驱动文件probe方法,既spi_drv_probe
static int spi_match_device(struct device *dev, struct device_driver *drv)
{
const struct spi_device *spi = to_spi_device(dev);
const struct spi_driver *sdrv = to_spi_driver(drv);
if (of_driver_match_device(dev, drv)) //设备文件驱动表的匹配
return 1;
if (sdrv->id_table) //spi设备驱动存在支持id表
return !!spi_match_id(sdrv->id_table, spi); //spi设备驱动表的匹配
return strcmp(spi->modalias, drv->name) == 0; //比较spi设备的名字和spi设备驱动的名字
}
2.1 of_driver_match_device
static inline int of_driver_match_device(const struct device *dev,const struct device_driver *drv)
{
return of_match_device(drv->of_match_table, dev) != NULL; //调用of_match_device函数
}
2.1.1 of_match_device
const struct of_device_id *of_match_device(const struct of_device_id *matches,const struct device *dev)
{
if ((!matches) || (!dev->of_node)) //id表和设备节点都不存在
return NULL; //则返回
return of_match_node(matches, dev->of_node); //调用of_match_node函数
}
EXPORT_SYMBOL(of_match_device);
2.1.1.1 of_match_node //drv->of_match_table,dev->of_node
const struct of_device_id *of_match_node(const struct of_device_id *matches,const struct device_node *node)
{
while (matches->name[0] || matches->type[0] || matches->compatible[0]) { //名字,类型,兼容方法有一个存在
int match = 1;
if (matches->name[0]) //判断名字
match &= node->name && !strcmp(matches->name, node->name);
if (matches->type[0]) //判断类型
match &= node->type && !strcmp(matches->type, node->type);
if (matches->compatible[0]) //兼容方法
match &= of_device_is_compatible(node,matches->compatible);
if (match) //匹配
return matches; //返回匹配的id
matches++; //matches指针++,指向下一个id
}
return NULL;
}
EXPORT_SYMBOL(of_match_node);
2.2 spi_match_id
static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,const struct spi_device *sdev)
{
while (id->name[0]) { //id表的成员的名字域不为空
if (!strcmp(sdev->modalias, id->name)) //则判断其名字是否与spi设备的名字一样
return id; //一样则返回该id
id++; //id表指针++,指向下一个id
}
return NULL;
}
六 spi消息和spi传输
1.spi消息结构体
struct spi_message {
struct list_head transfers; //spi传输事务链表头
struct spi_device *spi; //所属spi设备
unsigned is_dma_mapped:1;
void (*complete)(void *context);
void *context;
unsigned actual_length;
int status; //传输状态
struct list_head queue;
void *state;
};
2.初始化spi消息
static inline void spi_message_init(struct spi_message *m)
{
memset(m, 0, sizeof *m);
INIT_LIST_HEAD(&m->transfers); //初始化spi消息的事务链表头
}
3.添加传输事务到spi传输链表
static inline void spi_message_add_tail(struct spi_transfer *t, struct spi_message *m)
{
list_add_tail(&t->transfer_list, &m->transfers);
}
4.spi传输结构体
struct spi_transfer {
const void *tx_buf; //发送缓冲区指针
void *rx_buf; //接收缓冲区指针
unsigned len; //消息长度
dma_addr_t tx_dma; //DMA发送地址
dma_addr_t rx_dma; //DMA接收地址
unsigned cs_change:1;
u8 bits_per_word; //一个字多少位
u16 delay_usecs; //毫秒级延时
u32 speed_hz; //速率
struct list_head transfer_list; //传输链表头
};
七.spi子系统的初始化spi_init
static int __init spi_init(void)
{
int status;
buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); //分配数据收发缓冲区
if (!buf) {
status = -ENOMEM;
goto err0;
}
status = bus_register(&spi_bus_type); //注册spi总线
if (status < 0)
goto err1;
status = class_register(&spi_master_class); //注册spi主机类 "/sys/class/spi_master"
if (status < 0)
goto err2;
return 0;
err2:
bus_unregister(&spi_bus_type);
err1:
kfree(buf);
buf = NULL;
err0:
return status;
}
postcore_initcall(spi_init); //入口声明 #define postcore_initcall(fn) __define_initcall("2",fn,2)
八.spi子系统的API
1.spi读 spi_read
static inline int spi_read(struct spi_device *spi, u8 *buf, size_t len)
{
struct spi_transfer t = {
.rx_buf = buf,
.len = len,
};
struct spi_message m;
spi_message_init(&m); //spi消息初始化(初始化传输事务链表头)
spi_message_add_tail(&t, &m); //添加spi传输到spi消息传输链表
return spi_sync(spi, &m); //spi同步传输
}
2.spi写 spi_write
static inline int spi_write(struct spi_device *spi, const u8 *buf, size_t len)
{
struct spi_transfer t = {
.tx_buf = buf,
.len = len,
};
struct spi_message m;
spi_message_init(&m); //spi消息初始化(初始化传输事务链表头)
spi_message_add_tail(&t, &m); //添加spi传输到spi消息传输链表
return spi_sync(spi, &m); //spi同步传输
}
spi的读写操作都是初始化一个spi_transfer传输结构体,并将其添加进spi消息传输事务链表中
然后通过spi_sync来同步读写操作,接着看下spi_sync的具体代码
2.1 spi_sync
int spi_sync(struct spi_device *spi, struct spi_message *message)
{
return __spi_sync(spi, message, 0); //调用__spi_sync函数
}
EXPORT_SYMBOL_GPL(spi_sync);
2.1.1 __spi_sync函数
static int __spi_sync(struct spi_device *spi, struct spi_message *message,int bus_locked)
{
DECLARE_COMPLETION_ONSTACK(done); //声明一个工作队列
int status;
struct spi_master *master = spi->master; //获取spi主机
message->complete = spi_complete; //设置spi消息的complete方法为spi_complete
message->context = &done; //设置spi消息的context
if (!bus_locked)
mutex_lock(&master->bus_lock_mutex); //上锁
status = spi_async_locked(spi, message);
if (!bus_locked)
mutex_unlock(&master->bus_lock_mutex); //解锁
if (status == 0) {
wait_for_completion(&done); //等待完成
status = message->status; //设置spi消息传输状态
}
message->context = NULL;
return status;
}
2.1.1.1 spi_async_locked 函数
int spi_async_locked(struct spi_device *spi, struct spi_message *message)
{
struct spi_master *master = spi->master; //获取spi主机
int ret;
unsigned long flags;
spin_lock_irqsave(&master->bus_lock_spinlock, flags); //上自旋锁
ret = __spi_async(spi, message); //调用了spi异步同步方法
spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); //解自旋锁
return ret;
}
EXPORT_SYMBOL_GPL(spi_async_locked);
2.1.1.1.1 __spi_async函数(异步)
static int __spi_async(struct spi_device *spi, struct spi_message *message)
{
struct spi_master *master = spi->master;
//主机为半双工或spi设备为3线设备
if ((master->flags & SPI_MASTER_HALF_DUPLEX)|| (spi->mode & SPI_3WIRE)) {
struct spi_transfer *xfer;
unsigned flags = master->flags;
list_for_each_entry(xfer, &message->transfers, transfer_list) { //遍历spi消息的传输事务链表
if (xfer->rx_buf && xfer->tx_buf) //判断接收或发送缓冲区是否为空
return -EINVAL;
if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf) //检验无spi数据发送的情况
return -EINVAL;
if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf) //检验无spi数据接收的情况
return -EINVAL;
}
}
message->spi = spi; //设置spi消息所属的spi设备
message->status = -EINPROGRESS; //设置spi消息的传输状态
return master->transfer(spi, message); //调用spi主机的transfer方法,收发spi信息给spi设备
}
3.spi先写后读 spi_write_then_read
int spi_write_then_read(struct spi_device *spi,const u8 *txbuf, unsigned n_tx,u8 *rxbuf, unsigned n_rx)
{
static DEFINE_MUTEX(lock);
int status;
struct spi_message message;
struct spi_transfer x[2]; //声明两个spi传输结构体
u8 *local_buf;
if ((n_tx + n_rx) > SPI_BUFSIZ) //验证发送和接收的数据总和是否溢出
return -EINVAL;
spi_message_init(&message); //spi消息初始化(初始化传输事务链表头)
memset(x, 0, sizeof x); //
if (n_tx) {
x[0].len = n_tx;
spi_message_add_tail(&x[0], &message); //添加spi传输到spi消息传输链表
}
if (n_rx) {
x[1].len = n_rx;
spi_message_add_tail(&x[1], &message); //添加spi传输到spi消息传输链表
}
if (!mutex_trylock(&lock)) { //尝试上锁 失败
local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); //则分配local_buf内存
if (!local_buf)
return -ENOMEM;
} else
local_buf = buf; //指向默认分配好内存的缓冲区(spi_init中初始化的)
memcpy(local_buf, txbuf, n_tx); //发送缓冲区的内容复制到local_buf中
x[0].tx_buf = local_buf; //发送的spi传输结构体
x[1].rx_buf = local_buf + n_tx; //接收的spi传输结构体
status = spi_sync(spi, &message); //spi同步传输--发送数据
if (status == 0)
memcpy(rxbuf, x[1].rx_buf, n_rx); //接收返回的数据复制到rxbuf中
if (x[0].tx_buf == buf)
mutex_unlock(&lock); //解锁
else
kfree(local_buf); //释放内存
return status;
}
EXPORT_SYMBOL_GPL(spi_write_then_read);
4.spi写8位后读8位数据 spi_w8r8
static inline ssize_t spi_w8r8(struct spi_device *spi, u8 cmd)
{
ssize_t status;
u8 result;
status = spi_write_then_read(spi, &cmd, 1, &result, 1); //写8位读8位
return (status < 0) ? status : result;
}
5.spi写8位数据后读16位数据 spi_w8r16
static inline ssize_t spi_w8r16(struct spi_device *spi, u8 cmd)
{
ssize_t status;
u16 result;
status = spi_write_then_read(spi, &cmd, 1, (u8 *) &result, 2); //写8位读16位
return (status < 0) ? status : result;
}
这里的API是内核空间使用的接口,设备驱动程序调用这些API直接操作spi的读写操作,来完成任务
spi用户空间的接口由spidev.c中的spi驱动提供,见
-->二 spi 子系统(spidev.c)
一.spidev.c文件
看一个设备驱动的方法:
概览下重要的结构体spidev_data及全局变量device_list,bufsiz,SPIDEV_MAJOR...
module_init标识的入口初始化函数spidev_init,(module_exit标识的出口函数)
设备与设备驱动匹配时候调用的probe方法spidev_probe
设备驱动的操作函数集file_operations--->spidev_fops
先看open方法spidev_open
接着看读写方法spidev_read & spidev_write
再接着看ioctl方法-->spidev_ioctl
再看其他剩余的方法
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SPIDEV_MAJOR 153 //spidev主设备号
#define N_SPI_MINORS 32 /* ... up to 256 */
static DECLARE_BITMAP(minors, N_SPI_MINORS); //声明次设备位图
#define SPI_MODE_MASK (SPI_CPHA|SPI_CPOL|SPI_CS_HIGH|SPI_LSB_FIRST|SPI_3WIRE|SPI_LOOP|SPI_NO_CS|SPI_READY)
struct spidev_data {
dev_t devt; //设备号
spinlock_t spi_lock; //自旋锁
struct spi_device *spi; //spi设备结构体
struct list_head device_entry;
struct mutex buf_lock; //互斥锁
unsigned users; //使用者计数
u8 *buffer; //缓冲区
};
static LIST_HEAD(device_list); //声明spi设备链表
static DEFINE_MUTEX(device_list_lock); //定义互斥锁
static unsigned bufsiz = 4096; //最大传输缓冲区大小
module_param(bufsiz, uint, S_IRUGO);
MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");
static void spidev_complete(void *arg)
{
complete(arg); //调用complete
}
static ssize_t spidev_sync(struct spidev_data *spidev, struct spi_message *message)
{
DECLARE_COMPLETION_ONSTACK(done);
int status;
message->complete = spidev_complete; //设置spi消息的complete方法 回调函数
message->context = &done;
spin_lock_irq(&spidev->spi_lock);
if (spidev->spi == NULL) //判断是否有指定对应的spi设备
status = -ESHUTDOWN;
else
status = spi_async(spidev->spi, message); //spi异步同步
spin_unlock_irq(&spidev->spi_lock);
if (status == 0) {
wait_for_completion(&done); //等待传输完成
status = message->status; //获取spi消息传输事务状态
if (status == 0)
status = message->actual_length; //status等于传输的实际长度
}
return status; //返回实际传输长度
}
static inline ssize_t spidev_sync_write(struct spidev_data *spidev, size_t len)
{
struct spi_transfer t = {
.tx_buf = spidev->buffer, //发送缓冲区
.len = len, //发送数据长度
};
struct spi_message m;
spi_message_init(&m); //初始化spi消息(初始化spi传递事务队列)
spi_message_add_tail(&t, &m); //添加spr传递到该队列
return spidev_sync(spidev, &m); //同步读写
}
static inline ssize_t spidev_sync_read(struct spidev_data *spidev, size_t len)
{
struct spi_transfer t = {
.rx_buf = spidev->buffer, //接收缓冲区
.len = len, //接收数据长度
};
struct spi_message m;
spi_message_init(&m); //初始化spi消息(初始化spi传递事务队列)
spi_message_add_tail(&t, &m); //添加spr传递到该队列
return spidev_sync(spidev, &m); //同步读写
}
static ssize_t spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
struct spidev_data *spidev;
ssize_t status = 0;
if (count > bufsiz) //传输数据大于缓冲区容量
return -EMSGSIZE;
spidev = filp->private_data; //从文件私有数据指针获取spidev_data
mutex_lock(&spidev->buf_lock); //上互斥锁
status = spidev_sync_read(spidev, count); //同步读,返回传输数据长度
if (status > 0) {
unsigned long missing; //丢失的数据个数
missing = copy_to_user(buf, spidev->buffer, status); //内核空间复制到用户空间
if (missing == status) //丢失的数据个数等于要传输的数据个数
status = -EFAULT;
else
status = status - missing; //传输成功的数据个数
}
mutex_unlock(&spidev->buf_lock);//解互斥锁
return status; //返回读取成功的数据个数
}
static ssize_t spidev_write(struct file *filp, const char __user *buf,size_t count, loff_t *f_pos)
{
struct spidev_data *spidev;
ssize_t status = 0;
unsigned long missing;
if (count > bufsiz) //传输数据大于缓冲区容量
return -EMSGSIZE;
spidev = filp->private_data; //从文件私有数据指针获取spidev_data
mutex_lock(&spidev->buf_lock); //上互斥锁
missing = copy_from_user(spidev->buffer, buf, count); //用户空间复制到内核空间
if (missing == 0) { //传输失败个数为0
status = spidev_sync_write(spidev, count); //同步写,返回传输数据长度
}
else
status = -EFAULT;
mutex_unlock(&spidev->buf_lock);//解互斥锁
return status; //返回写数据的实际个数
}
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); //初始化spi消息(初始化spi传递事务队列)
k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL); //分配spi传输指针内存
if (k_xfers == NULL)
return -ENOMEM;
buf = spidev->buffer; //获取spidev_data的缓冲区
total = 0;
//n=xfers为spi_ioc_transfer个数,u_tmp = u_xfers为要处理的spi_ioc_transfer指针
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) { //信息量超过bufsiz缓冲区最大容量
status = -EMSGSIZE;
goto done;
}
if (u_tmp->rx_buf) { //接收缓冲区指针不为空
k_tmp->rx_buf = 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; //缓冲区指针指向buf
if (copy_from_user(buf, (const u8 __user *)(uintptr_t) u_tmp->tx_buf,u_tmp->len)) //用户空间复制数据到buf
goto done;
}
buf += k_tmp->len; //缓冲区指针移动一个传输信息的长度
k_tmp->cs_change = !!u_tmp->cs_change; //设置cs_change
k_tmp->bits_per_word = u_tmp->bits_per_word; //设置bits_per_word 一个字多少位
k_tmp->delay_usecs = u_tmp->delay_usecs; //设置delay_usecs 毫秒级延时
k_tmp->speed_hz = u_tmp->speed_hz; //设置speed_hz 速率
#ifdef VERBOSE
dev_dbg(&spidev->spi->dev," xfer len %zd %s%s%s%dbits %u usec %uHz\n",
u_tmp->len,u_tmp->rx_buf ? "rx " : "",u_tmp->tx_buf ? "tx " : "",u_tmp->cs_change ? "cs " : "",
u_tmp->bits_per_word ? : spidev->spi->bits_per_word,u_tmp->delay_usecs,u_tmp->speed_hz ? : spidev->spi->max_speed_hz);
#endif
spi_message_add_tail(k_tmp, &msg); //添加spr传递到该队列
}
//for循环的作用是将spi_ioc_transfer批量转换为spi传递结构体spi_transfer,然后添加进spi传递事务队列
status = spidev_sync(spidev, &msg); //同步读写
if (status < 0)
goto done;
buf = spidev->buffer; //获取spidev_data缓冲区指针
for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) { //批量从内核空间复制spi_ioc_transfer到用户空间
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; //buf指针位置调整指向下一个spi_ioc_transfer
}
status = total; //status等于实际传输的数据长度
done:
kfree(k_xfers); //释放k_xfers
return status; //返回实际传输的数据长度
}
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) //判断控制命令的方向是否为读read
err = !access_ok(VERIFY_WRITE,(void __user *)arg, _IOC_SIZE(cmd)); //判断传输数据大小
if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE) //判断控制命令的方向是否为写write
err = !access_ok(VERIFY_READ,(void __user *)arg, _IOC_SIZE(cmd)); //判断传输数据大小
if (err)
return -EFAULT;
spidev = filp->private_data; //从文件私有数据中获取spidev_data
spin_lock_irq(&spidev->spi_lock); //上自旋锁
spi = spi_dev_get(spidev->spi); //获取spi设备
spin_unlock_irq(&spidev->spi_lock); //解自旋锁
if (spi == NULL) //获取spi设备失败
return -ESHUTDOWN; //则返回错误
mutex_lock(&spidev->buf_lock); //上互斥锁
switch (cmd) {
case SPI_IOC_RD_MODE: //设置spi读模式
retval = __put_user(spi->mode & SPI_MODE_MASK,(__u8 __user *)arg);
break;
case SPI_IOC_RD_LSB_FIRST: //设置spi读最低有效位
retval = __put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,(__u8 __user *)arg);
break;
case SPI_IOC_RD_BITS_PER_WORD: //设置spi读每个字含多个个位
retval = __put_user(spi->bits_per_word, (__u8 __user *)arg);
break;
case SPI_IOC_RD_MAX_SPEED_HZ: //设置spi读最大速率
retval = __put_user(spi->max_speed_hz, (__u32 __user *)arg);
break;
case SPI_IOC_WR_MODE: //设置spi写模式
retval = __get_user(tmp, (u8 __user *)arg);
if (retval == 0) {
u8 save = spi->mode; //获取spi设备模式
if (tmp & ~SPI_MODE_MASK) {
retval = -EINVAL;
break;
}
tmp |= spi->mode & ~SPI_MODE_MASK;
spi->mode = (u8)tmp;
retval = spi_setup(spi); //配置spi设备
if (retval < 0)
spi->mode = save;
else
dev_dbg(&spi->dev, "spi mode %02x\n", tmp);
}
break;
case SPI_IOC_WR_LSB_FIRST: //设置spi写最低有效位
retval = __get_user(tmp, (__u8 __user *)arg);
if (retval == 0) {
u8 save = spi->mode; //获取spi设备模式
if (tmp)
spi->mode |= SPI_LSB_FIRST;
else
spi->mode &= ~SPI_LSB_FIRST;
retval = spi_setup(spi); //配置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: //设置spi写每个字含多个个位
retval = __get_user(tmp, (__u8 __user *)arg); //用户空间获取数据
if (retval == 0) {
u8 save = spi->bits_per_word; //获取spi设备 每个字含多少位
spi->bits_per_word = tmp; //更新新的spi设备 每个字含多少位
retval = spi_setup(spi); //配置spi设备
if (retval < 0) //配置失败
spi->bits_per_word = save; //还原spi设备 每个字含多少位
else
dev_dbg(&spi->dev, "%d bits per word\n", tmp);
}
break;
case SPI_IOC_WR_MAX_SPEED_HZ: //设置spi写最大速率
retval = __get_user(tmp, (__u32 __user *)arg); //用户空间获取数据
if (retval == 0) {
u32 save = spi->max_speed_hz; //获取spi设备最大速率
spi->max_speed_hz = tmp; //更新新的spi设备最大速率
retval = spi_setup(spi); //配置spi设备
if (retval < 0) //配置失败
spi->max_speed_hz = save; //还原spi设备最大速率
else
dev_dbg(&spi->dev, "%d Hz (max)\n", tmp);
}
break;
default:
//命令必须为写方向的命令,且传输数据必须是SPI_IOC_MESSAGE()修饰的命令
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) { //判断是否为spi_ioc_transfer对齐
retval = -EINVAL;
break;
}
n_ioc = tmp / sizeof(struct spi_ioc_transfer); //计算出spi_ioc_transfer数据的个数
if (n_ioc == 0)
break;
ioc = kmalloc(tmp, GFP_KERNEL); //分配spi_ioc_transfer指针ioc内存
if (!ioc) {
retval = -ENOMEM;
break;
}
if (__copy_from_user(ioc, (void __user *)arg, tmp)) { //从用户空间复制到内核空间
kfree(ioc); //复制失败则释放ioc内存
retval = -EFAULT;
break;
}
retval = spidev_message(spidev, ioc, n_ioc); //spidev消息处理
kfree(ioc); //释放ioc内存
break;
}
mutex_unlock(&spidev->buf_lock); //解互斥锁
spi_dev_put(spi); //增加spi设备的引用计数
return retval;
}
static int spidev_open(struct inode *inode, struct file *filp)
{
struct spidev_data *spidev;
int status = -ENXIO;
mutex_lock(&device_list_lock); //上互斥锁
list_for_each_entry(spidev, &device_list, device_entry) { //遍历device_list
if (spidev->devt == inode->i_rdev) { //判断设备号找到对应的设备
status = 0; //设置状态为0
break;
}
}
if (status == 0) { //找得到对应的设备
if (!spidev->buffer) { //spidev_data缓冲区为空
spidev->buffer = kmalloc(bufsiz, GFP_KERNEL); //则分配内存
if (!spidev->buffer) { //还空
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n"); //调试了
status = -ENOMEM;
}
}
if (status == 0) { //找得到对应的设备
spidev->users++; //spidev_data使用者计数++
filp->private_data = spidev; //spidev_data放在文件的私有数据里
nonseekable_open(inode, filp); //设置文件的打开模式(文件读写指针不会跟随读写操作移动)
}
}
else
pr_debug("spidev: nothing for minor %d\n", iminor(inode));
mutex_unlock(&device_list_lock); //接互斥锁
return status;
}
static int spidev_release(struct inode *inode, struct file *filp)
{
struct spidev_data *spidev;
int status = 0;
mutex_lock(&device_list_lock);
spidev = filp->private_data; //获取spidev_data
filp->private_data = NULL; //清除文件的私有数据指针
spidev->users--; //使用者个数--
if (!spidev->users) { //如果使用者个数为0
int dofree;
kfree(spidev->buffer); //释放spidev_data的缓冲区内存
spidev->buffer = NULL; //清除spidev_data缓冲区指针
spin_lock_irq(&spidev->spi_lock); //上自旋锁
dofree = (spidev->spi == NULL); //判断spi设备是否与spidev_data解绑了
spin_unlock_irq(&spidev->spi_lock); //解自旋锁
if (dofree) //没有捆绑的spi设备
kfree(spidev); //则是否spidev_data内存
}
mutex_unlock(&device_list_lock);
return status;
}
static const struct file_operations spidev_fops = { //文件操作函数集
.owner = THIS_MODULE,
.write = spidev_write, //写write
.read = spidev_read, //读read
.unlocked_ioctl = spidev_ioctl, //控制ioctl
.open = spidev_open, //打开open
.release = spidev_release, //释放release
.llseek = no_llseek, //文件指针移动 no_llseek表示没有移动
};
static struct class *spidev_class;
static int __devinit spidev_probe(struct spi_device *spi)
{
struct spidev_data *spidev;
int status;
unsigned long minor;
spidev = kzalloc(sizeof(*spidev), GFP_KERNEL); //分配spidev_data内存
if (!spidev)
return -ENOMEM;
spidev->spi = spi; //设置spidev_data->spi(spi设备)
spin_lock_init(&spidev->spi_lock);
mutex_init(&spidev->buf_lock);
INIT_LIST_HEAD(&spidev->device_entry); //初始化spidev_data入口链表
mutex_lock(&device_list_lock);
minor = find_first_zero_bit(minors, N_SPI_MINORS); //查找次设备位图分配次设备号
if (minor < N_SPI_MINORS) {
struct device *dev;
spidev->devt = MKDEV(SPIDEV_MAJOR, minor); //计算出设备号
//创建设备/dev/spidev%d.%d(spidev总线号.片选号)
dev = device_create(spidev_class, &spi->dev, spidev->devt,spidev, "spidev%d.%d",spi->master->bus_num, spi->chip_select);
status = IS_ERR(dev) ? PTR_ERR(dev) : 0;
}
else {
dev_dbg(&spi->dev, "no minor number available!\n");
status = -ENODEV;
}
if (status == 0) { //分配设备号成功
set_bit(minor, minors); //更新次设备位图
list_add(&spidev->device_entry, &device_list); //添加进设备链表
}
mutex_unlock(&device_list_lock);
if (status == 0)
spi_set_drvdata(spi, spidev); //spi->dev->p->driver_data=spidev
else
kfree(spidev);
return status;
}
static int __devexit spidev_remove(struct spi_device *spi)
{
struct spidev_data *spidev = spi_get_drvdata(spi); //根据spi设备获取spidev_data
spin_lock_irq(&spidev->spi_lock); //上自旋锁
spidev->spi = NULL; //清空spidev_data->spi指针
spi_set_drvdata(spi, NULL); //spi->dev->p->driver_data=NULL
spin_unlock_irq(&spidev->spi_lock); //解自旋锁
mutex_lock(&device_list_lock); //上互斥锁
list_del(&spidev->device_entry); //删除spidev_data入口链表
device_destroy(spidev_class, spidev->devt); //销毁/dev/spidev%d.%d
clear_bit(MINOR(spidev->devt), minors); //清除次设备位图对应位
if (spidev->users == 0) //使用者个数为0
kfree(spidev); //释放spidev_data内存
mutex_unlock(&device_list_lock); //解互斥锁
return 0;
}
static struct spi_driver spidev_spi_driver = { //spi设备驱动
.driver = {
.name = "spidev",
.owner = THIS_MODULE,
},
.probe = spidev_probe, //spidev的probe方法(当注册了modalias域为"spidev"的spi设备或板级设备,则会调用probe方法)
.remove = __devexit_p(spidev_remove), //spidev的remove方法
};
static int __init spidev_init(void) //spidev接口初始化
{
int status;
BUILD_BUG_ON(N_SPI_MINORS > 256);
//注册字符设备,主设备号SPIDEV_MAJOR=153,捆绑的设备操作函数集为spidev_fops
status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
if (status < 0)
return status;
spidev_class = class_create(THIS_MODULE, "spidev"); //创建设备类spidev_class
if (IS_ERR(spidev_class)) {
unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
return PTR_ERR(spidev_class);
}
status = spi_register_driver(&spidev_spi_driver); //注册spi设备驱动spidev_spi_driver
if (status < 0) {
class_destroy(spidev_class);
unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
}
return status;
}
module_init(spidev_init); //声明初始化入口
static void __exit spidev_exit(void) //spidev接口销毁
{
spi_unregister_driver(&spidev_spi_driver); //注销spi设备驱动spidev_spi_driver
class_destroy(spidev_class); //注销设备类spidev_class
unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); //注销字符设备
}
module_exit(spidev_exit); //声明初始化出口
MODULE_AUTHOR("Andrea Paterniani, ");
MODULE_DESCRIPTION("User mode SPI device interface");
MODULE_LICENSE("GPL");
MODULE_ALIAS("spi:spidev");
二.用户空间例子(spidev_test.c)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
static void pabort(const char *s)
{
perror(s);
abort();
}
static const char *device = "/dev/spidev1.1";
static uint8_t mode;
static uint8_t bits = 8;
static uint32_t speed = 500000;
static uint16_t delay;
static void transfer(int fd)
{
int ret;
uint8_t tx[] = { //要发送的数据数组
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,
0xF0, 0x0D,
};
uint8_t rx[ARRAY_SIZE(tx)] = {0, }; //接收的数据数据
struct spi_ioc_transfer tr = { //声明并初始化spi_ioc_transfer结构体
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
};
//SPI_IOC_MESSAGE(1)的1表示spi_ioc_transfer的数量
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); //ioctl默认操作,传输数据
if (ret < 1)
pabort("can't send spi message");
for (ret = 0; ret < ARRAY_SIZE(tx); ret++) { //打印接收缓冲区
if (!(ret % 6)) //6个数据为一簇打印
puts("");
printf("%.2X ", rx[ret]);
}
puts("");
}
static void print_usage(const char *prog) //参数错误则打印帮助信息
{
printf("Usage: %s [-DsbdlHOLC3]\n", prog);
puts(" -D --device device to use (default /dev/spidev1.1)\n"
" -s --speed max speed (Hz)\n"
" -d --delay delay (usec)\n"
" -b --bpw bits per word \n"
" -l --loop loopback\n"
" -H --cpha clock phase\n"
" -O --cpol clock polarity\n"
" -L --lsb least significant bit first\n"
" -C --cs-high chip select active high\n"
" -3 --3wire SI/SO signals shared\n");
exit(1);
}
static void parse_opts(int argc, char *argv[])
{
while (1) {
static const struct option lopts[] = { //参数命令表
{ "device", 1, 0, 'D' },
{ "speed", 1, 0, 's' },
{ "delay", 1, 0, 'd' },
{ "bpw", 1, 0, 'b' },
{ "loop", 0, 0, 'l' },
{ "cpha", 0, 0, 'H' },
{ "cpol", 0, 0, 'O' },
{ "lsb", 0, 0, 'L' },
{ "cs-high", 0, 0, 'C' },
{ "3wire", 0, 0, '3' },
{ "no-cs", 0, 0, 'N' },
{ "ready", 0, 0, 'R' },
{ NULL, 0, 0, 0 },
};
int c;
c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR", lopts, NULL);
if (c == -1)
break;
switch (c) {
case 'D': //设备名
device = optarg;
break;
case 's': //速率
speed = atoi(optarg);
break;
case 'd': //延时时间
delay = atoi(optarg);
break;
case 'b': //每字含多少位
bits = atoi(optarg);
break;
case 'l': //回送模式
mode |= SPI_LOOP;
break;
case 'H': //时钟相位
mode |= SPI_CPHA;
break;
case 'O': //时钟极性
mode |= SPI_CPOL;
break;
case 'L': //lsb 最低有效位
mode |= SPI_LSB_FIRST;
break;
case 'C': //片选高电平
mode |= SPI_CS_HIGH;
break;
case '3': //3线传输模式
mode |= SPI_3WIRE;
break;
case 'N': //没片选
mode |= SPI_NO_CS;
break;
case 'R': //从机拉低电平停止数据传输
mode |= SPI_READY;
break;
default: //错误的参数
print_usage(argv[0]);
break;
}
}
}
int main(int argc, char *argv[])
{
int ret = 0;
int fd;
parse_opts(argc, argv); //解析传递进来的参数
fd = open(device, O_RDWR); //打开设备文件
if (fd < 0)
pabort("can't open device");
/*
* spi mode //设置spi设备模式
*/
ret = ioctl(fd, SPI_IOC_WR_MODE, &mode); //写模式
if (ret == -1)
pabort("can't set spi mode");
ret = ioctl(fd, SPI_IOC_RD_MODE, &mode); //读模式
if (ret == -1)
pabort("can't get spi mode");
/*
* bits per word //设置每个字含多少位
*/
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); //写 每个字含多少位
if (ret == -1)
pabort("can't set bits per word");
ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); //读 每个字含多少位
if (ret == -1)
pabort("can't get bits per word");
/*
* max speed hz //设置速率
*/
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); //写速率
if (ret == -1)
pabort("can't set max speed hz");
ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); //读速率
if (ret == -1)
pabort("can't get max speed hz");
//打印模式,每字多少位和速率信息
printf("spi mode: %d\n", mode);
printf("bits per word: %d\n", bits);
printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
transfer(fd); //传输测试
close(fd); //关闭设备
return ret;
}
这里整理下ioctl的命令:
SPI_IOC_RD_MODE //读 模式
SPI_IOC_RD_LSB_FIRST //读 LSB
SPI_IOC_RD_BITS_PER_WORD //读 每字多少位
SPI_IOC_RD_MAX_SPEED_HZ //读 最大速率
SPI_IOC_WR_MODE //写 模式
SPI_IOC_WR_LSB_FIRST //写 LSB
SPI_IOC_WR_BITS_PER_WORD //写 每字多少位
SPI_IOC_WR_MAX_SPEED_HZ //写 最大速率
SPI_IOC_MESSAGE(n) //传输n个数据包