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核心、总线驱动与设备驱动的更多相关文章

  1. 字符设备驱动、平台设备驱动、设备驱动模型、sysfs的比较和关联

    转载自:http://www.kancloud.cn/yueqian_scut/emlinux/106829 学习Linux设备驱动开发的过程中自然会遇到字符设备驱动.平台设备驱动.设备驱动模型和sy ...

  2. [kernel]字符设备驱动、平台设备驱动、设备驱动模型、sysfs几者之间的比较和关联

    转自:http://www.2cto.com/kf/201510/444943.html Linux驱动开发经验总结,绝对干货! 学习Linux设备驱动开发的过程中自然会遇到字符设备驱动.平台设备驱动 ...

  3. 【驱动】linux设备驱动·字符设备驱动开发

    Preface 前面对linux设备驱动的相应知识点进行了总结,现在进入实践阶段! <linux设备驱动入门篇>:http://infohacker.blog.51cto.com/6751 ...

  4. spi驱动框架全面分析,从master驱动到设备驱动

    内核版本:linux2.6.32.2  硬件资源:s3c2440 参考:  韦东山SPI视频教程 内容概括:     1.I2C 驱动框架回顾     2.SPI 框架简单介绍     3.maste ...

  5. fl2440 platform总线led字符设备驱动

    首先需要知道的是,设备跟驱动是分开的.设备通过struct device来定义,也可以自己将结构体封装到自己定义的device结构体中: 例如:struct platform_device: 在inc ...

  6. fl2440 platform总线button字符设备驱动

    驱动程序: #include "s3c_driver.h" #define DRV_DESC "S3C24XX button driver" /* Driver ...

  7. Linux字符设备驱动--Led设备驱动

    ①驱动源码 #include <linux/module.h> #include <linux/init.h> #include <linux/cdev.h> #i ...

  8. 乾坤合一~Linux设备驱动之I2C核心、总线以及设备驱动

    我思念的城市已是黄昏 为何我总对你一往情深 曾经给我快乐 也给我创伤 曾经给我希望 也给我绝望 我在遥远的城市 陌生的人群 感觉着你遥远的忧伤 我的幻想 你的忧伤,像我的的绝望,那样漫长,,,,,这是 ...

  9. Linux I2C核心、总线和设备驱动

    目录 更新记录 一.Linux I2C 体系结构 1.1 Linux I2C 体系结构的组成部分 1.2 内核源码文件 1.3 重要的数据结构 二.Linux I2C 核心 2.1 流程 2.2 主要 ...

随机推荐

  1. 利用Aspose.Cell控件导入Excel非强类型的数据

    导入Excel的操作是非常常见的操作,可以使用Aspose.Cell.APOI.MyXls.OLEDB.Excel VBA等操作Excel文件,从而实现数据的导入,在导入数据的时候,如果是强类型的数据 ...

  2. macbook装双系统多分区其实很简单,你只要把macbook当作一台普通pc就可以了!

    macbook装双系统多分区其实很简单,你只要把macbook当作一台普通pc就可以了! 不用理会苹果官网的警告,苹果官网警告你只能用bootcamp安装且不能多分区,把人吓得不轻.其实不用过多担心, ...

  3. 利用mciSendString播放音频

    最近在写音频播放器,不过有点懒散,开发进度很慢,一天只做了一点点东西.其实就是让程序能播放音频.这个在我大二学winform程序开发时书上有说,那是书上教的是用media player的COM组件,而 ...

  4. Linux里如何查找文件内容

    Linux查找文件内容的常用命令方法. 从文件内容查找匹配指定字符串的行: $ grep "被查找的字符串" 文件名例子:在当前目录里第一级文件夹中寻找包含指定字符串的.in文件g ...

  5. [Tool] 使用StyleCop验证命名规则

    [Tool] 使用StyleCop验证命名规则 前言 微软的MSDN上,有提供了一份微软的命名方针,指引开发人员去建立风格一致的程序代码. http://msdn.microsoft.com/zh-t ...

  6. jQuery eislideshow 图片轮播

    在线实例 基础演示 自动播放 使用方法 <div id="ei-slider" class="ei-slider"> <ul class=&q ...

  7. English Training Material - 05

    Could I leave a message? Language Checklist Telephoning (1) Introducing yourself Good morning, Arist ...

  8. 安卓问题集-Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED

    错误出现原因: 1.没有 AndroidManifest.xml file文件(出现几率较小) 2. 是你在外面修改了包名而在 AndroidManifest.xml file.文件中没有同步过去导致 ...

  9. iOS 学习 - 6.Objective-C中的各种遍历(迭代)方式

    说明:转自文顶顶 一.使用 for 循环 要遍历字典.数组或者是集合,for 循环是最简单也用的比较多的方法 -(void)iteratorWithFor { //////////处理数组////// ...

  10. 【原创+译文】官方文档中声明的如何创建抽屉导航栏(Navigation Drawer)

    如需转载请注明出处:http://www.cnblogs.com/ghylzwsb/p/5831759.html 创建一个抽屉导航栏 抽屉式导航栏是显示在屏幕的左边缘,它是应用程序的主导航选项面板.它 ...