- 论坛徽章:
- 0
|
i2c-dev.c的代码看不出实现了一个i2c-client,而是实现了一个I2C总线上所有i2c-client的访问,这一点你可以通过Documentation/i2c/dev-interface的介绍来理解,所以i2c-dev.c可以理解为总线驱动。
根据Documentation/i2c/dev-interface,举例来说,假如一个I2C总线上有两个设备,地址为0x40和0x50。
第一步,打开总线/dev/i2c-x,此处x代表总线号,如第一根i2c总线就是i2c-0,第二根i2c总线就是i2c-1,依此类推。
int file;
int adapter_nr = 2; /* probably dynamically determined */
char filename[20];
sprintf(filename,"/dev/i2c-%d",adapter_nr);
if ((file = open(filename,O_RDWR)) < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */
exit(1);
}
第二步,对I2C总线(第一步打开的总线)进行一些ioctl操作,如
I2C_TENBIT是说明I2C总线(第一步打开的总线)上的待访问设备的地址是7bits地址还是10bits地址
第三步,指定I2C总线(第一步打开的总线)上的待访问设备的地址
int addr = 0x40; /* The I2C address */
if (ioctl(file,I2C_SLAVE,addr) < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */
exit(1);
}
第四步,对I2C总线(第一步打开的总线)进行read/write操作,也可以通过i2c-core.c中提供的API进行读写操作。如
__u8 register = 0x10; /* Device register to access */
__s32 res;
char buf[10];
/* Using SMBus commands */
res = i2c_smbus_read_word_data(file,register);
if (res < 0) {
/* ERROR HANDLING: i2c transaction failed */
} else {
/* res contains the read word */
}
/* Using I2C Write, equivalent of
i2c_smbus_write_word_data(file,register,0x6543) */
buf[0] = register;
buf[1] = 0x43;
buf[2] = 0x65;
if ( write(file,buf,3) != 3) {
/* ERROR HANDLING: i2c transaction failed */
}
第五步,关闭I2C总线(第一步打开的总线)
close(file);
另外,值得一提的是,
i2c-client的驱动放在drivers/i2c/chips,就是某个具体i2c设备的驱动
底层i2c总线的驱动放在drivers/i2c/busses,就是跟CPU相关的i2c接口
drivers/i2c/algos放着i2c上的算法,什么算法,就是如何实现i2c总线上的时序,当然,如果drivers/i2c/busses的CPU相关代码有实现的话就不会用这一部分
i2c-core.c更重要的实现管理和提供一些API接口
i2c-dev.c实现的就是总线访问接口 |
评分
-
查看全部评分
|