這部分准備分幾個部分進行分析總結
- 主芯片的i2c的驅動
- 從芯片的i2c的驅動
編寫方法:
一般都不會使用i2c-dev.c的read()、write()方法。最常用的是ioctl()方法。ioctl()方法可以實現上面所有的情況(兩種數據格式、以及I2C算法和smbus算法)。
針對i2c的算法,需要熟悉struct i2c_rdwr_ioctl_data 、struct i2c_msg。使用的命令是I2C_RDWR。
struct i2c_rdwr_ioctl_data
{
struct i2c_msg __user *msgs; /* pointers to i2c_msgs */
__u32 nmsgs; /* number of i2c_msgs */
};
struct i2c_msg {
_ _u16 addr; /* slave address */
_ _u16 flags; /* 標志(讀、寫) */
_ _u16 len; /* msg length */
_ _u8 *buf; /* pointer to msg data */
};
針對smbus算法,需要熟悉struct i2c_smbus_ioctl_data。使用的命令是I2C_SMBUS。對於smbus算法,不需要考慮“多開始信號時序”問題。
struct i2c_smbus_ioctl_data {
__u8 read_write; //讀、寫
__u8 command; //命令
__u32 size; //數據長度標識
union i2c_smbus_data __user *data; //數據
};
首先在內核中已經包含了對s3c2410 中的i2c控制器(總線驅動)驅動的支持。提供了i2c算法(非smbus類型的,所以后面的ioctl的命令是I2C_RDWR)
static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
.master_xfer = s3c24xx_i2c_xfer,
.functionality = s3c24xx_i2c_func,
};
另外一方面需要確定為了實現對AT24C02 e2prom的操作,需要確定從機芯片的地址及讀寫訪問時序。
在網上找了個例子:
具體分析如下:
#include <stdio.h> #include <linux/types.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/ioctl.h> #include <errno.h> #define I2C_RETRIES 0x0701 #define I2C_TIMEOUT 0x0702 #define I2C_RDWR 0x0707 /*********定義struct i2c_rdwr_ioctl_data和struct i2c_msg,要和內核一致。兩個重要的結構體*******/ struct i2c_msg { unsigned short addr; unsigned short flags; unsigned short len; unsigned char *buf; }; struct i2c_rdwr_ioctl_data { struct i2c_msg *msgs; int nmsgs; /* nmsgs這個數量決定了有多少開始信號,對於“單開始時序”,取1*/ }; int main() { int fd,ret; struct i2c_rdwr_ioctl_data e2prom_data; fd=open("/dev/i2c-0",O_RDWR);
/*
為什么是i2c-0呢???那就要到內核里看啦,等會再說 open底層調用了i2c_get_adapter(int id)函數,這個函數很重要,他可以識別占用了哪個i2c總線 使用地0個i2c控制器 /dev/i2c-0是在注冊i2c-dev.c后產生的,代表一個可操作的適配器。如果不使用i2c-dev.c(這里說啦上面的為什么) 的方式,就沒有,也不需要這個節,i2c_driver結構體中有attach_adapter方法:里面用device_create(i2c_dev_class, &adap->dev,MKDEV(I2C_MAJOR, adap->nr), NULL,"i2c-%d",adap->nr);I2C_MAJOR=89,即i2c-dev.c針對每個i2c適配器生成一個主設備號位89的設備文件,次設備要自己定義 */ if(fd<0) { perror("open error"); } e2prom_data.nmsgs=2; /* *因為操作時序中,最多是用到2個開始信號(字節讀操作中),所以此將 *e2prom_data.nmsgs配置為2 */ e2prom_data.msgs=(struct i2c_msg*)malloc(e2prom_data.nmsgs*sizeof(struct i2c_msg)); if(!e2prom_data.msgs) { perror("malloc error"); exit(1); } ioctl(fd,I2C_TIMEOUT,1);/*超時時間*/ ioctl(fd,I2C_RETRIES,2);/*重復次數*/ /***write data to e2prom**/ /**/ e2prom_data.nmsgs=1; (e2prom_data.msgs[0]).len=2; //1個 e2prom 寫入目標的地址和1個數據 (e2prom_data.msgs[0]).addr=0x50;//e2prom 設備地址 (e2prom_data.msgs[0]).flags=0; //write (e2prom_data.msgs[0]).buf=(unsigned char*)malloc(2); (e2prom_data.msgs[0]).buf[0]=0x10;// e2prom 寫入目標的地址 (e2prom_data.msgs[0]).buf[1]=0x58;//the data to write ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data); if(ret<0) { perror("ioctl error1"); } sleep(1); /******read data from e2prom*******/ e2prom_data.nmsgs=2; (e2prom_data.msgs[0]).len=1; //e2prom 目標數據的地址 (e2prom_data.msgs[0]).addr=0x50; // e2prom 設備地址 (e2prom_data.msgs[0]).flags=0;//write (e2prom_data.msgs[0]).buf[0]=0x10;//e2prom數據地址 (e2prom_data.msgs[1]).len=1;//讀出的數據 (e2prom_data.msgs[1]).addr=0x50;// e2prom 設備地址 (e2prom_data.msgs[1]).flags=I2C_M_RD;//read (e2prom_data.msgs[1]).buf=(unsigned char*)malloc(1);//存放返回值的地址。 (e2prom_data.msgs[1]).buf[0]=0;//初始化讀緩沖 ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data); if(ret<0) { perror("ioctl error2"); } printf("buff[0]=%x/n",(e2prom_data.msgs[1]).buf[0]); close(fd); i2c_put_adapter(client->adapter);釋放i2c總線 return 0; }
以上講述了一種比較常用的利用i2c-dev.c操作i2c設備的方法,這種方法可以說是在應用層完成了對具體i2c設備的驅動工作。
接下來准備具體分析如何寫第一部分!