linux内核I2C子系统学习(一)
這部分準(zhǔn)備分幾個(gè)部分進(jìn)行分析總結(jié)
因?yàn)镮2C的通信肯定至少要有2個(gè)芯片完成,所以它的驅(qū)動(dòng)是由2大部分組成:編寫方法:
第一.要有i2c總線驅(qū)動(dòng)(首先要查查內(nèi)核i2c文件是否支持這種總線驅(qū)動(dòng),一般都有支持,如果沒有只好自己倒霉自己寫了) 第二.i2c設(shè)備驅(qū)動(dòng)(主控芯片的地址等等信息) 這個(gè)過(guò)程都是差不多的,以后在分析。 一般的主控芯片的i2c控制器linux內(nèi)核基本上支持的很好,如:2410的i2c驅(qū)動(dòng)器的支持 (二).從芯片的I2C的驅(qū)動(dòng): 下面主要分析從芯片的I2C驅(qū)動(dòng)(也有2種方式,第一個(gè)是利用內(nèi)核提供的i2c-dev.c來(lái)構(gòu)建,另一個(gè)是自己寫) 主要分析第一種方式: 利用系統(tǒng)給我們提供的i2c-dev.c來(lái)實(shí)現(xiàn)一個(gè)i2c適配器的設(shè)備文件。然后通過(guò)在應(yīng)用層操作i2c適配器來(lái)控制i2c設(shè)備。 i2c-dev.c并沒有針對(duì)特定的設(shè)備而設(shè)計(jì),只是提供了通用的read()、write()和ioctl()等接口,應(yīng)用層可以借用這些接口訪問(wèn)掛接在適配器上的i2c設(shè)備的存儲(chǔ)空間或寄存器,并控制I2C設(shè)備的工作方式。但是read和write方法適用性有限。 所以用ioctl方法來(lái)操作:一般都不會(huì)使用i2c-dev.c的read()、write()方法。最常用的是ioctl()方法。ioctl()方法可以實(shí)現(xiàn)上面所有的情況(兩種數(shù)據(jù)格式、以及I2C算法和smbus算法)。
?針對(duì)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; /* 標(biāo)志(讀、寫) */?
????????????_ _u16 len; /* msg length */
????????????_ _u8 *buf; /* pointer to msg data */
????????};
針對(duì)smbus算法,需要熟悉struct i2c_smbus_ioctl_data。使用的命令是I2C_SMBUS。對(duì)于smbus算法,不需要考慮“多開始信號(hào)時(shí)序”問(wèn)題。
????????struct i2c_smbus_ioctl_data {
????????????__u8 read_write; //讀、寫
????????????__u8 command; //命令
????????????__u32 size; //數(shù)據(jù)長(zhǎng)度標(biāo)識(shí)
????????????union i2c_smbus_data __user *data; //數(shù)據(jù)
????????};
首先在內(nèi)核中已經(jīng)包含了對(duì)s3c2410 中的i2c控制器(總線驅(qū)動(dòng))驅(qū)動(dòng)的支持。提供了i2c算法(非smbus類型的,所以后面的ioctl的命令是I2C_RDWR)
????????static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
????????????.master_xfer = s3c24xx_i2c_xfer,
????????????.functionality = s3c24xx_i2c_func,
????????};
?
另外一方面需要確定為了實(shí)現(xiàn)對(duì)AT24C02 e2prom的操作,需要確定從機(jī)芯片的地址及讀寫訪問(wèn)時(shí)序。
在網(wǎng)上找了個(gè)例子:
具體分析如下:
#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,要和內(nèi)核一致。兩個(gè)重要的結(jié)構(gòu)體*******/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這個(gè)數(shù)量決定了有多少開始信號(hào),對(duì)于“單開始時(shí)序”,取1*/};int main(){int fd,ret;struct i2c_rdwr_ioctl_data e2prom_data;fd=open("/dev/i2c-0",O_RDWR); /*為什么是i2c-0呢???那就要到內(nèi)核里看啦,等會(huì)再說(shuō)open底層調(diào)用了i2c_get_adapter(int id)函數(shù),這個(gè)函數(shù)很重要,他可以識(shí)別占用了哪個(gè)i2c總線使用地0個(gè)i2c控制器/dev/i2c-0是在注冊(cè)i2c-dev.c后產(chǎn)生的,代表一個(gè)可操作的適配器。如果不使用i2c-dev.c(這里說(shuō)啦上面的為什么) 的方式,就沒有,也不需要這個(gè)節(jié),i2c_driver結(jié)構(gòu)體中有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針對(duì)每個(gè)i2c適配器生成一個(gè)主設(shè)備號(hào)位89的設(shè)備文件,次設(shè)備要自己定義*/if(fd<0){perror("open error");}e2prom_data.nmsgs=2; /**因?yàn)椴僮鲿r(shí)序中,最多是用到2個(gè)開始信號(hào)(字節(jié)讀操作中),所以此將*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);/*超時(shí)時(shí)間*/ioctl(fd,I2C_RETRIES,2);/*重復(fù)次數(shù)*//***write data to e2prom**/ /**/e2prom_data.nmsgs=1;(e2prom_data.msgs[0]).len=2; //1個(gè) e2prom 寫入目標(biāo)的地址和1個(gè)數(shù)據(jù) (e2prom_data.msgs[0]).addr=0x50;//e2prom 設(shè)備地址(e2prom_data.msgs[0]).flags=0; //write(e2prom_data.msgs[0]).buf=(unsigned char*)malloc(2);(e2prom_data.msgs[0]).buf[0]=0x10;// e2prom 寫入目標(biāo)的地址(e2prom_data.msgs[0]).buf[1]=0x58;//the data to writeret=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 目標(biāo)數(shù)據(jù)的地址(e2prom_data.msgs[0]).addr=0x50; // e2prom 設(shè)備地址(e2prom_data.msgs[0]).flags=0;//write(e2prom_data.msgs[0]).buf[0]=0x10;//e2prom數(shù)據(jù)地址(e2prom_data.msgs[1]).len=1;//讀出的數(shù)據(jù)(e2prom_data.msgs[1]).addr=0x50;// e2prom 設(shè)備地址 (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設(shè)備的方法,這種方法可以說(shuō)是在應(yīng)用層完成了對(duì)具體i2c設(shè)備的驅(qū)動(dòng)工作。
接下來(lái)準(zhǔn)備具體分析如何寫第一部分!
總結(jié)
以上是生活随笔為你收集整理的linux内核I2C子系统学习(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: asterisk 支持 VP8 vide
- 下一篇: 招行信用卡自动还款设置和到账时间