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. IOS 之 PJSIP 笔记(一) 编译多平台支持的静态库

    好久没有写博客了,这也算是我步入新工作后的第一篇技术博文吧.在进入新公司前,早就有了技术层进入下一个迭代的准备,但很多事情是意想不到的,就像我以 C# 程序员的身份面试入职的,而今却是一个全职的 IO ...

  2. AccessHelper

    代码: using System; using System.Data; using System.Configuration; using System.Data.OleDb; using ahwi ...

  3. Web开发者选择的最佳HTML5/CSS3代码生成器

    原文地址:http://codecloud.net/css3-code-generators-for-web-programmers-6672.htmlHTML5 和CSS3是一入门就能用的最好的语言 ...

  4. .net 读书笔记

    好书不能只读一遍,这两天又翻看了一遍<你必须知道的.NET>,重温了下基础,重温了下经典,简单记录了下来. 内存分配:CLR 管理内存的区域,主要有三块,分别为: 线程的堆栈,用于分配值类 ...

  5. SqlDataReader、SqlDataAdapter與SqlCommand的 区别

    1.SqlDataReader,在线应用,需要conn.open(),使用完之后要关闭. SqlConnection conn = new SqlConnection(connStr); //conn ...

  6. CSS代码重构

    CSS代码重构的目的 我们写CSS代码时,不仅仅只是完成页面设计的效果,还应该让CSS代码易于管理,维护.我们对CSS代码重构主要有两个目的:1.提高代码性能2.提高代码的可维护性 提高代码性能 提高 ...

  7. Linux Shell系列教程之(十三)Shell分支语句case … esac教程

    本文是Linux Shell系列教程的第(十三)篇,更多Linux Shell教程请看:Linux Shell系列教程 分支语句非常实用,基本上高级语言都支持分支语句(python 没有),大多数都使 ...

  8. 人人都应该学习Markdown

    Markdown是一门新兴的标记语言,已经有12年历史了.随着它在全球范围内的流行,很多人已经听说.熟识或者开始使用了. 首先,Markdown既不是工具,也不是程序语言,而是一种十分轻量级的标记语言 ...

  9. 照着别人的demo自己试着做了个放大镜效果

    原理: A:放大镜   B:小图片 C:大图片可视区域 D:大图片 鼠标的位置应该在放大镜的中央,所以鼠标位置为:clientX=A.offsetLeft()+B.offsetLeft+1/2*A.o ...

  10. 关于SAP的视图类型

    1 sap的视图的类型sap的视图的类型有五种 Database views (数据库视图):和数据库的视图形同,连接条件是必须自定义. Projection views(投影视图): 用于屏蔽一些字 ...