LinuxI2C核心、总线驱动与设备驱动
I2C体系结构分为三个部分:I2C核心、总线驱动、设备驱动
I2C核心:
I2C核心提供了一组不依赖硬件的接口函数,I2C总线驱动和设备驱动之间依赖于I2C核心作为纽带
(1)增加/删除i2c_adapter
int i2c_add_adapter(struct i2c_adapter *adap);
int i2c_del_adapter(struct i2c_adapter *adap);
(2)增加/删除i2c_driver
int i2c_register_driver(struct module *owner, struct i2c_driver *drever);
int i2c_del_driver(struct i2c_driver *drever);
inline int i2c_add_driver(struct i2c_driver *drever);
(3)i2c_client依附/脱离
int i2c_attach_client(struct i2c_client *client);
int i2c_detach_client(struct i2c_client *client);
(4)I2C传输、发送和接收
int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);/*用于I2C适配器和I2C设备之间通信*/
int i2c_master_send(struct i2c_client *client, const char *buf, int count);//调用i2c_transfer()函数
int i2c_master_recv(struct i2c_client *client, char *buf, int count);
I2C总线驱动:
I2C总线驱动是对I2C硬件体系中适配器的实现。主要包含适配器数据结构i2c_adapter和适配器的algorithm结构体i2c_algorithm
i2c_adapter
struct i2c_adapter
{
struct module *owner;/*所属模块*/
unsigned int id; /*algorithm的类型,定义于i2c-id.h,以I2C_ALGO_开始*/
unsigned int class;
struct i2c_algorithm *algo;/*总线通信方法结构体指针 */
void *algo_data; /* algorithm数据 */
int (*client_register)(struct i2c_client *); /*client注册时调用*/
int (*client_unregister)(struct i2c_client *); /*client注销时调用*/
struct semaphore bus_lock; /*控制并发访问的自旋锁*/
struct semaphore clist_lock;
int timeout;
int retries; /*重试次数*/
struct device dev; /* 适配器设备 */
struct class_device class_dev; /* 类设备 */
int nr;
struct list_head clients; /* client链表头*/
struct list_head list;
char name[I2C_NAME_SIZE]; /*适配器名称*/
struct completion dev_released; /*用于同步*/
struct completion class_dev_released;
};
i2c_algorithm
struct i2c_algorithm
{
int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);
int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr, unsigned short flags, char read_write, u8 command, int size, union i2c_smbus_data *data);
u32 (*functionality) (struct i2c_adapter *);
};
i2c_algorithm结构体主要实现了master_xfer()函数和functionalityh()函数,其中master_xfer()函数定义了适配器和设备之间的通信方式,functionalityh()函数用于返回algorithm支持的通信协议。
i2c_adapter对应物理上的一个适配器,i2c_algorithm对应一套通信方法。
I2C设备驱动:
I2C设备驱动包含i2c_driver和i2c_client两个数据结构。
i2c_driver
struct i2c_driver
{
int id;
unsigned int class;
int (*attach_adapter)(struct i2c_adapter *); /*依附i2c_adapter函数指针 */
int (*detach_adapter)(struct i2c_adapter *); /*脱离i2c_adapter函数指针*/
int (*detach_client)(struct i2c_client *); /*i2c client脱离函数指针*/
int (*probe)(struct i2c_client *, const struct i2c_device_id *); //现行通用的与对应设备进行绑定的接口函数
int (*remove)(struct i2c_client *); //现行通用与对应设备进行解绑的接口函数
void (*shutdown)(struct i2c_client *); //关闭设备
int (*suspend)(struct i2c_client *, pm_message_t mesg); //挂起设备,与电源管理有关,为省电
int (*resume)(struct i2c_client *); //从挂起状态恢复
void (*alert)(struct i2c_client *, unsigned int data);
int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
struct device_driver driver; //I2C设备的驱动模型
const struct i2c_device_id *id_table; //匹配设备列表
int (*detect)(struct i2c_client *, struct i2c_board_info *);
const unsigned short *address_list;
struct list_head clients;
};
i2c_client
struct i2c_client
{
unsigned short flags; //I2C_CLIENT_TEN表示设备使用10bit从地址,I2C_CLIENT_PEC表示设备使用SMBus检错
unsigned short addr; //设备从地址,7bit。这里说一下为什么是7位,因为最后以为0表示写,1表示读,通过对这个7bit地址移位处理即可。addr<<1 & 0x0即写,addr<<1 | 0x01即读。
char name[I2C_NAME_SIZE]; //从设备名称
struct i2c_adapter *adapter; //此从设备依附于哪个adapter上
struct i2c_driver *driver; // 此设备对应的I2C驱动指针
struct device dev; // 设备模型
int irq; // 设备使用的中断号
struct list_head detected; //用于链表操作
};
i2c_driver 对应一套驱动方法,i2c_client对应真实的物理设备,每个i2c设备都需要一个i2c_client来描述。
LinuxI2C核心、总线驱动与设备驱动的更多相关文章
- 字符设备驱动、平台设备驱动、设备驱动模型、sysfs的比较和关联
转载自:http://www.kancloud.cn/yueqian_scut/emlinux/106829 学习Linux设备驱动开发的过程中自然会遇到字符设备驱动.平台设备驱动.设备驱动模型和sy ...
- [kernel]字符设备驱动、平台设备驱动、设备驱动模型、sysfs几者之间的比较和关联
转自:http://www.2cto.com/kf/201510/444943.html Linux驱动开发经验总结,绝对干货! 学习Linux设备驱动开发的过程中自然会遇到字符设备驱动.平台设备驱动 ...
- 【驱动】linux设备驱动·字符设备驱动开发
Preface 前面对linux设备驱动的相应知识点进行了总结,现在进入实践阶段! <linux设备驱动入门篇>:http://infohacker.blog.51cto.com/6751 ...
- spi驱动框架全面分析,从master驱动到设备驱动
内核版本:linux2.6.32.2 硬件资源:s3c2440 参考: 韦东山SPI视频教程 内容概括: 1.I2C 驱动框架回顾 2.SPI 框架简单介绍 3.maste ...
- fl2440 platform总线led字符设备驱动
首先需要知道的是,设备跟驱动是分开的.设备通过struct device来定义,也可以自己将结构体封装到自己定义的device结构体中: 例如:struct platform_device: 在inc ...
- fl2440 platform总线button字符设备驱动
驱动程序: #include "s3c_driver.h" #define DRV_DESC "S3C24XX button driver" /* Driver ...
- Linux字符设备驱动--Led设备驱动
①驱动源码 #include <linux/module.h> #include <linux/init.h> #include <linux/cdev.h> #i ...
- 乾坤合一~Linux设备驱动之I2C核心、总线以及设备驱动
我思念的城市已是黄昏 为何我总对你一往情深 曾经给我快乐 也给我创伤 曾经给我希望 也给我绝望 我在遥远的城市 陌生的人群 感觉着你遥远的忧伤 我的幻想 你的忧伤,像我的的绝望,那样漫长,,,,,这是 ...
- Linux I2C核心、总线和设备驱动
目录 更新记录 一.Linux I2C 体系结构 1.1 Linux I2C 体系结构的组成部分 1.2 内核源码文件 1.3 重要的数据结构 二.Linux I2C 核心 2.1 流程 2.2 主要 ...
随机推荐
- .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点
除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...
- jQuery的事件one
如果你只想你设计的对象只能让用户执行一次的话,可以使用jQuery的事件one()来实现. 演示一个,创建视图, 看看效果:
- 新的Visual C++代码优化器
微软在 5 月 4 日发布了新的高级代码优化器,服务于 Visual C++ 的后端编译器.提高了代码性能,可以压缩代码体积,将编译器带入了一个新的境界. Visual C++ 的团队在博客上称,这将 ...
- C#代码反编译 得到项目可运行源码
C#代码反编译 得到项目可运行源码 摘自:http://www.cnblogs.com/know/archive/2011/03/15/1985026.html 谈到"C#代码反编译&quo ...
- asp.net学习之Repeater控件
asp.net学习之Repeater控件 文章摘自:http://www.cnblogs.com/shipfi/archive/2009/10/19/1585703.html Repeater控件和D ...
- MySQL更新优化
通常情况下,当访问某张表的时候,读取者首先必须获取该表的锁,如果有写入操作到达,那么写入者一直等待读取者完成操作(查询开始之后就不能中断,因此允许读取者完成操作).当读取者完成对表的操作的时候,锁就会 ...
- PHP学习笔记:万能随机字符串生成函数(已经封装好)
做验证码用到的,然后就把这个函数封装起来,使用时候要设置2个参数: $str设置里要被采集的字符串,比如: $str='efasfgzsrhftjxjxjhsrth'; 则在函数里面生成的字符串就回从 ...
- 【GOF23设计模式】模板方法模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_模板方法模式.钩子函数.方法回调.好莱坞原则 package com.test.templateMethod; publi ...
- 【GOF23设计模式】享元模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_享元模式.享元池.内部状态.外部状态.线程池.连接池 package com.test.flyweight; /** * ...
- Play常用代码片段 http://www.anool.net/?p=625
持续更新中: (1)按照降序查询: List<Entity> entities= Entity.find("order by id desc").fetch(2); ...