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. 【jQuery基础学习】03 jQuery中的事件与动画

    关于jQuery中的事件 js与HTML之间的交互是通过用户和浏览器操作页面时引发的事件来处理的. jQuery增加并扩展了基本的事件处理机制,jQuery不仅提供了更加优雅的事件处理方法,而且极大地 ...

  2. SqlServer 2008 R2定时备份数据库,并且发送邮件通知

    先配置数据库的邮件设置,这样才可以发送邮件. 2. 3. 4. 5. 6. 7. 8. 9. 10. 总的预览图,如图 执行这一段(先发送备份邮件,然后进行数据备份,将昨天的发送数据插入到另一张表中, ...

  3. [moka同学笔记]yii2.0数据库操作以及分页

    1.model中models/article.php 1 <?php 2 3 namespace app\models; 4 5 use Yii; 6 7 /** 8 * This is the ...

  4. 开发机多用户 xdebug 远程调试 PhpStorm

    在公司都用的远程开发机开发,每次有错误调试就得dd(xxx)然后保存真是,让我在本地开发用惯xdebug的情何以堪,所以有了下文. 1.安装配置xdebug 直接使用pecl安装即可 # pecl i ...

  5. 基于java的socket编程

    #开头的废话#学习java已经半个月了,原本在抠教材里面的字眼时,觉得教材好厚,要看完不知道要到猴年马月去了.突然在网上看到一个教程,里面老师说学编程语言书不用太细看,看个大概,知道里面讲些什么就好, ...

  6. [翻译]:SQL死锁-锁与事务级别

    其实这一篇呢与解决我项目中遇到的问题也是必不可少的.上一篇讲到了各种锁之间的兼容性,里面有一项就是共享锁会引起死锁,如何避免呢,将我们的查询都设置中read uncommitted是否可行呢?其结果显 ...

  7. 写给java程序员的c++与java实现的一些重要细微差别

    0.其实常规的逻辑判断结构.工具类.文件读写.控制台读写这些的关系都不大,熟悉之后,这些都是灵活运用的问题. 学习c/c++需要预先知道的一个前提就是,虽然有ANSI C标准,但是每个c/c++编译器 ...

  8. jquery常用选择器

    1.数字性过滤 $("tr:first")               //选择所有tr元素的第一个 $("tr:last")                / ...

  9. HTML5原生拖放实例分析

    HTML5提供了原生拖放功能的JavaScript API,使用起来很方便. 兼容性: 对于PC端浏览器,Firefox.Chrome.Safari支持良好,而IE和Edge浏览器有些特性不支持,如I ...

  10. SVG基础图形与参数

    SVG是什么 SVG 指可伸缩矢量图形 (Scalable Vector Graphics) SVG 用来定义WEB上使用的矢量图 SVG 使用 XML 格式定义图形 SVG 图像在缩放时其图形质量不 ...