预览加载中,请您耐心等待几秒...
1/8
2/8
3/8
4/8
5/8
6/8
7/8
8/8

在线预览结束,喜欢就下载吧,查找使用更方便

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

基于I2C实时时钟DS3231驱动说明: Linux的I2C的整体架构 Linux的i2c框架中各个部分的关系如图(1)所示: 图(1) 内核中i2c相关代码可以分为三个层次: i2c框架:i2c.h和i2c-core.c为i2c框架的主体,提供了核心数据结构的定义、i2c适配器驱动和设备驱动的注册、注销管理,i2c通信方法上层的、与具体适配器无关的代码、检测设备地址的上层代码等;i2c-dev.c用于创建i2c适配器的/dev/i2c/%d设备节点,提供i2c设备访问方法等。 i2c总线适配器驱动:定义描述具体i2c总线适配器的i2c_adapter数据结构、实现在具体i2c适配器上的i2c总线通信方法,并由i2c_algorithm数据结构进行描述。 i2c设备驱动:定义描述具体设备的i2c_client和可能的私有数据结构、借助i2c框架的i2c_probe函数实现注册设备的attach_adapter方法、提供设备可能使用的地址范围、以及设备地址检测成功后创建i2c_client数据结构的回调函数。 I2C驱动相关的数据结构 I2C传送信息的数据结构体 structi2c_msg{ __u16addr; /*slaveaddress */ __u16flags; #defineI2C_M_TEN 0x10 /*wehaveatenbitchipaddress */ #defineI2C_M_RD 0x01 #defineI2C_M_NOSTART 0x4000 #defineI2C_M_REV_DIR_ADDR 0x2000 #defineI2C_M_IGNORE_NAK 0x1000 #defineI2C_M_NO_RD_ACK 0x0800 __u16len; /*msglength */ __u8*buf; /*pointertomsgdata */ }; 其中addr成员是指向器件的地址,flags是读写标志位,len是要发送的信息长度,*buf是发送信息的数据指针,如果是读操作的buf是读到数据的指针。 I2C低层数据传输函数结构体 structi2c_algorithm{ /*Ifanadapteralgorithmcan'tdoI2C-levelaccess,setmaster_xfer toNULL.IfanadapteralgorithmcandoSMBusaccess,set smbus_xfer.IfsettoNULL,theSMBusprotocolissimulated usingcommonI2Cmessages*/ /*master_xfershouldreturnthenumberofmessagessuccessfully processed,oranegativevalueonerror*/ int(*master_xfer)(structi2c_adapter*adap,structi2c_msg*msgs, intnum); int(*smbus_xfer)(structi2c_adapter*adap,u16addr, unsignedshortflags,charread_write, u8command,intsize,unioni2c_smbus_data*data); /*---ioctllikecalltosetdiv.parameters.*/ int(*algo_control)(structi2c_adapter*,unsignedint,unsignedlong); /*Todeterminewhattheadaptersupports*/ u32(*functionality)(structi2c_adapter*); }; master_xfer函数为I2C适配器提供数据传输函数,smbu_xfer函数为subus协议函数,functionality函数I2C适配器所支持的功能函数。 I2c适配器数据结构体 structi2c_adapter{ structmodule*owner; unsignedintid; unsignedintclass; conststructi2c_algorithm*algo;/*thealgorithmtoaccessthebus*/ void*algo_data; /*---administrationstuff.*/ int(*client_register)(structi2c_client*); int(*client_unregister)(structi2c_client*); /*datafields