设为首页 加入收藏

TOP

底层之旅 —— Linux下的I2c体系结构(一)
2014-11-24 11:20:26 来源: 作者: 【 】 浏览:2
Tags:底层 之旅 Linux I2c 体系结构

I2C总线是Philips公司推出的芯片间品德传输总线,它采用两线制,由串行时钟线SCL和串行数据线SDA构成。在电路设计方面,由于I2C总线接口为开漏或开集电极输出,需要加上上拉电阻。


I2C总线通信方式:


(1)I2C采用主/从方式进行双向通信。


(2)I2C总线的时钟线SCL和数据线SDA都是双向传输线。


(3)在标准I2C模式下,数据传输速率可达100Kbps、400Kbps,在高速(HS)模式下可达3.4Mbit/s。


(4)在时钟线SCL保持高电平期间,数据线SDA出现由高电平向低电平变化时为起始信号S,启动I2C工作。若在时钟线SCL保持高电平期间,数据线SDA上出现由低到高的电平变化时为停止信号P,终止I2C总线的数据传送。




(5)I2C总线传送的格式为:开始位以后,主设备送出8位控制字节,以选择从设备并控制总线传送的方向,其后传送数据。



Linux的I2C体系结构分为3个部分:


(1)I2C核心


文件路径:linux / driver / i2c / i2c-core.c 这个文件实现了I2C核心的功能以及 /proc/bus/i2c-xxx 接口。


(2)I2C总线驱动


文件路径:linux / driver / i2c / busses / i2c-xxx.c 这个文件包含了各种arm处理器的I2C总线驱动。I2C总线驱动主要包含了I2C适配器数据结构i2c_adapter、I2C适配器的algorithm数据结构i2c_algorithm和控制I2C适配器产生通信信号的函数。


(3)I2C设备驱动


I2C设备驱动主要包含了数据结构i2c_driver 和 i2c_client 我们需要根据具体设备实现其中的成员函数。



I2C总线驱动中重要的四个数据结构。


i2c适配器结构体:


i2c_adapter对应于物理上的一个适配器。


/* i2c_adapter is the structure used to identify a physical i2c bus along with the access algorithms necessary to access it. */
struct i2c_adapter {


};


I2C通信方法结构体:


i2c_algorithm对应于一套通信方法。i2c——algorithm中的关键函数master_xfer()用于产生I2C访问周期需要的信号。


struct i2c_algorithm {
/* If an adapter algorithm can't do I2C-level access, set master_xfer
to NULL. If an adapter algorithm can do SMBus access, set
smbus_xfer. If set to NULL, the SMBus protocol is simulated
using common I2C messages */
/* master_xfer should return the number of messages successfully
processed, or a negative value on error */

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);


/* To determine what the adapter supports */
u32 (*functionality) (struct i2c_adapter *);
};


I2C设备驱动结构体:


i2c_driver对应一套驱动方法。


/* The i2c_client structure which is handed to the @detect callback is
* not a real i2c_client. It is initialized just enough so that you can
* call i2c_smbus_read_byte_data and friends on it. Don't do anything
* else with it. In particular, calling dev_dbg and friends on it is
* not allowed.
*/

struct i2c_driver {
int id;
unsigned int class;

/* Notifies the driver that a new bus has appeared or is about to be
* removed. You should avoid using this if you can, it will probably
* be removed in a near future.
*/

int (*attach_adapter)(struct i2c_adapter *);
int (*detach_adapter)(struct i2c_adapter *);


/* Standard driver model interfaces */
int (*probe)(struct i2c_client *, const struct i2c_device_id *);
int (*remove)(struct i2c_client *);


/* driver model interfaces that don't relate to enumeration */
void (*shutdown)(struct i2c_client *);
int (*suspend)(struct i2c_client *, pm_message_t mesg);
int (*resume)(struct i2c_client *);


/* a ioctl like command that can be used to perform specific functions
* with the device.
*/

int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);


struct device_driver driver;
const struct i2c_device_id *id_table;


/* Device detection callback for automatic device creation */
int (*detect)(struct i2c_client *, int kind, struct i2c_board_info *);
const struct i2c_client_address_data *address_data;
struct list_head clients;
};


I2C客户端结构体:


i2c_client对应于真实的物理设备,每个I2C设备都需要一个i2c_client来描述。


struct i2c_client {
unsigned short flags;/* div., see below*/

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux C 线程同步实例分析 下一篇Android中的service 实现之 利用o..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)
·使用华为开发者空间 (2025-12-27 04:19:24)
·Getting Started wit (2025-12-27 03:49:24)
·Ubuntu 上最好用的中 (2025-12-27 03:49:20)