一、前言 本文主要分為三個(gè)部分,第一部分,介紹i2c字符設(shè)備驅(qū)動(dòng)應(yīng)用的背景以及本文測(cè)試需要的開發(fā)環(huán)境;第二部分,介紹主要的字符驅(qū)動(dòng)源碼及測(cè)試程序;第三部分,測(cè)試方法以及測(cè)試結(jié)果,i2c從設(shè)備的器件地址可以在該器件的datasheet查找。文章的最后會(huì)給大家分享本文的所有源碼。 二、開發(fā)背景和環(huán)境 我已經(jīng)講解過利用i2c總線的去配置i2c從設(shè)備的方法,本文采用i2c設(shè)備驅(qū)動(dòng)的方式完成同樣的功能,在此完善工作記錄。 優(yōu)點(diǎn):(1)當(dāng)從設(shè)備需要多種功能操作時(shí)(比如修改攝像頭的亮度、放大、曝光、分辨率等配置),把每個(gè)功能包裝成子模塊,相對(duì)總線方式的配置層次清晰,而且方便管理維護(hù),而且在擴(kuò)展時(shí)還能夠在設(shè)備驅(qū)動(dòng)中添加對(duì)系統(tǒng)內(nèi)核資源的訪問(操作時(shí)請(qǐng)注意安全); (2)設(shè)備初始化順序可以隨意控制,想i2c從設(shè)備啟動(dòng)快點(diǎn)就把設(shè)備初始化添加到內(nèi)核啟動(dòng),想它啟動(dòng)慢一點(diǎn),就以.ko的方式加載,等文件系統(tǒng)加載完畢了再初始化; 缺點(diǎn) :(1)相比總線操作的方式編寫代碼較復(fù)雜,因?yàn)槭紫纫煜ぷ址?qū)動(dòng)架構(gòu),而且還需要編寫一個(gè)操作設(shè)備驅(qū)動(dòng)的應(yīng)用程序; 運(yùn)行環(huán)境:ARM S3C6410平臺(tái) 交叉編譯器:ARM-LINUX-GCC 內(nèi)核版本:2.6.28.6 三、源碼的講解 源碼的講解分為兩個(gè)部分,第一個(gè)部分初略地介紹下i2c字符設(shè)備初始化過程,具體的字符驅(qū)動(dòng)架構(gòu)不再本文講解的范圍內(nèi),第二部分,講解利用i2c設(shè)備驅(qū)動(dòng)對(duì)i2c從設(shè)備的寄存器進(jìn)行讀寫操作; 驅(qū)動(dòng)源碼初始化執(zhí)行步驟, 首先執(zhí)行ch7026_init函數(shù) static __init int ch7026_init(void) printk(DEVICE_NAME ' start init...\n'); p_bank = kmalloc(sizeof(struct ch7026_bank), GFP_KERNEL); memset(p_bank, 0, sizeof(struct ch7026_bank)); devno = MKDEV(CH7026_MAJOR,0); ret = register_chrdev_region(devno,1,DEVICE_NAME); printk(KERN_NOTICE 'can not register ch7026 device'); cdev_init(&cdev_ch7026,&ch7026_fops); cdev_ch7026.owner = THIS_MODULE; ret =cdev_add(&cdev_ch7026,devno,1); printk(KERN_NOTICE 'can not add ch7026 device'); /*在/sys/class/下創(chuàng)建相對(duì)應(yīng)的類目錄*/ my_class = class_create(THIS_MODULE,'ch7026'); printk('Err: Failed in creating class\n'); /*完成設(shè)備節(jié)點(diǎn)的自動(dòng)創(chuàng)建,當(dāng)加載模塊時(shí),就會(huì)在/dev下自動(dòng)創(chuàng)建設(shè)備文件*/ device_create(my_class,NULL,MKDEV(CH7026_MAJOR,0),NULL,DEVICE_NAME); printk(DEVICE_NAME ' initialized\n'); i2c_add_driver(&ch7026_driver);
執(zhí)行回調(diào)函數(shù) ch7026_probe()函數(shù) static struct i2c_driver ch7026_driver = { .id = I2C_DRIVERID_CH7026, .attach_adapter = ch7026_probe, .detach_client = ch7026_detach,
在執(zhí)行ch7026_attach()函數(shù) static int ch7026_probe(struct i2c_adapter *adap) ret = i2c_probe(adap, &addr_data, ch7026_attach); printk('failed to attach ch7026 driver\n');
執(zhí)行ch7026_attach函數(shù),執(zhí)行用戶配置i2c從設(shè)備ch7026_config()函數(shù) static int ch7026_attach(struct i2c_adapter *adap, int addr, int flags ) strcpy(p_bank->c.name, 'ch7026'); p_bank->c.adapter = adap; p_bank->c.driver = &ch7026_driver; ret = i2c_attach_client(&p_bank->c); ch7026_config(&p_bank->c); printk('CH7026 attached successfully\n');
第二部分下面講解下應(yīng)用層write、read、ioctl系統(tǒng)調(diào)用到設(shè)備驅(qū)動(dòng)的讀寫函數(shù),即i2c設(shè)備驅(qū)動(dòng)的讀寫操作,熟悉設(shè)備驅(qū)動(dòng)的人就知道,分別實(shí)現(xiàn)對(duì)應(yīng)的ch7026_write、 ch7026_read、 ch7026_ioctl函數(shù)即可, static struct file_operations ch7026_fops = {
ch7026_write函數(shù)的作用是實(shí)現(xiàn)用戶空間的數(shù)據(jù)到內(nèi)核空間的拷貝,然后再調(diào)用ch7026_i2c_write函數(shù) static ssize_t ch7026_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) unsigned char addr = *ppos; if (copy_from_user(&buffer, buf, count)) { ret = ch7026_i2c_write(&p_bank->c, addr, buffer); printk('i2c transfer error\n');
ch7026_read函數(shù)讀取地址的數(shù)據(jù),再從內(nèi)核空間拷貝該數(shù)據(jù)到用戶空間,本文所講的i2從設(shè)備寄存器地址是8bit(一個(gè)字節(jié)),如果遇到i2從設(shè)備寄存器地址是16bit(兩個(gè)字節(jié)),對(duì)應(yīng)修改下面buffer為unsigned short型,i2c_master_send(&p_bank->c, &buffer, 2),讀操作也同理,改下數(shù)據(jù)接口就得,根據(jù)手冊(cè)多測(cè)試就知道怎么改了。 static ssize_t ch7026_read(struct file *file, char *buf, size_t count, loff_t *ppos) if (i2c_master_send(&p_bank->c, &buffer, 1) != 1) { if (i2c_master_recv(&p_bank->c, &Rdval, 1) != 1) { if (copy_to_user(buf, &Rdval, sizeof(Rdval))) {
要在ioctl實(shí)現(xiàn)從設(shè)備各種子功能的實(shí)現(xiàn),就自定義實(shí)現(xiàn)一個(gè)設(shè)備驅(qū)動(dòng)里能讀寫的函數(shù)接口,開機(jī)配置i2c從設(shè)備多個(gè)寄存器的接口函數(shù)作用如下,就用到了ch7026_i2c_write函數(shù), void inline ch7026_config(struct i2c_client *client) for (i = 0; i < CH7026_INIT_REGS; i++) { ret = ch7026_i2c_write(client, ch7026_reg[i].subaddr, ch7026_reg[i].value); if(ret != 0) printk('ch7026:write faild!\n');
以下為i2c設(shè)備驅(qū)動(dòng)讀寫i2c從設(shè)備寄存器的接口, static unsigned char ch7026_i2c_read(struct i2c_client *client, u8 subaddr) if (i2c_master_send(client, Regbuf, 1) != 1) {//發(fā)送要讀取從設(shè)備的寄存器地址 if (i2c_master_recv(client, &Rdval, 1) != 1) {//把讀取到寄存器的值保存在Rdval并返回 static int ch7026_i2c_write(struct i2c_client *client, unsigned char subaddr, unsigned char val) struct i2c_msg msg = { client->addr, 0, 2, buf }; buf[0] = subaddr;//所寫寄存器的地址 printk('Kernel Reg: 0x%x Value: 0x%x\n',buf[0], buf[1]); ret = i2c_transfer(client->adapter, &msg, 1) == 1 ? 0 : -EIO; printk('ch7026_i2c_write error!\n');
以下為i2c讀寫接口函數(shù)的延展,展示了i2c_master_send、i2c_master_recv、i2c_transfer的函數(shù)關(guān)系,其中i2c_msg 數(shù)據(jù)結(jié)構(gòu)非常關(guān)鍵 /usr/local/arm/4.2.2-eabi/usr/include/linux/i2c.h //i2c_msg 數(shù)據(jù)定義的頭文件 * I2C Message - used for pure i2c transaction, also from /dev interface __u16 addr; /* slave address---從設(shè)備地址*/ __u16 flags; /*以下的宏定義為可以對(duì)flags操作的位運(yùn)算*/ #define I2C_M_TEN 0x10 /* we have a ten bit chip address */ #define I2C_M_NOSTART 0x4000 #define I2C_M_REV_DIR_ADDR 0x2000 #define I2C_M_IGNORE_NAK 0x1000 #define I2C_M_NO_RD_ACK 0x0800 #define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */ __u16 len; /* msg length---數(shù)據(jù)長度,字節(jié)為單位*/ __u8 *buf; /* pointer to msg data---存在數(shù)據(jù)的指針*/
/opt/htx-linux-2.6.28-d300-20170531/drivers/i2c/i2c-core.c //i2c_master_send、 i2c_master_recv、i2c_transfer函數(shù)定義的文件
* i2c_master_send - issue a single I2C message in master transmit mode * @client: Handle to slave device * @buf: Data that will be written to the slave * @count: How many bytes to write * Returns negative errno, or else the number of bytes written. int i2c_master_send(struct i2c_client *client,const char *buf ,int count) struct i2c_adapter *adap=client->adapter; msg.flags = client->flags & I2C_M_TEN; ret = i2c_transfer(adap, &msg, 1); /* If everything went ok (i.e. 1 msg transmitted), return #bytes transmitted, else error code. */ return (ret == 1) ? count : ret; EXPORT_SYMBOL(i2c_master_send); * i2c_master_recv - issue a single I2C message in master receive mode * @client: Handle to slave device * @buf: Where to store data read from slave * @count: How many bytes to read * Returns negative errno, or else the number of bytes read. int i2c_master_recv(struct i2c_client *client, char *buf ,int count) struct i2c_adapter *adap=client->adapter; msg.flags = client->flags & I2C_M_TEN; ret = i2c_transfer(adap, &msg, 1); /* If everything went ok (i.e. 1 msg transmitted), return #bytes transmitted, else error code. */ return (ret == 1) ? count : ret; EXPORT_SYMBOL(i2c_master_recv);
從上可以看到,i2c_master_send和i2c_master_recv傳輸數(shù)據(jù),最終調(diào)用的函數(shù)接口都是i2c_transfer,唯一的區(qū)別就在數(shù)據(jù)包中的msg.flags標(biāo)志位,接收消息時(shí)多了個(gè)I2C_M_RD標(biāo)志的或運(yùn)算。 /* ---------------------------------------------------- * the functional interface to the i2c busses. * ---------------------------------------------------- * i2c_transfer - execute a single or combined I2C message * @adap: Handle to I2C bus * @msgs: One or more messages to execute before STOP is issued to * terminate the operation; each message begins with a START. * @num: Number of messages to be executed. * Returns negative errno, else the number of messages executed. * Note that there is no requirement that each message be sent to * the same slave address, although that is the most common model. int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num) /* REVISIT the fault reporting model here is weak: * - When we get an error after receiving N bytes from a slave, * there is no way to report 'N'. * - When we get a NAK after transmitting N bytes to a slave, * there is no way to report 'N' ... or to let the master * continue executing the rest of this combined message, if * that's the appropriate response. * - When for example 'num' is two and we successfully complete * the first message but get an error part way through the * second, it's unclear whether that should be reported as * one (discarding status on the second message) or errno * (discarding status on the first one). if (adap->algo->master_xfer) { for (ret = 0; ret < num; ret++) { dev_dbg(&adap->dev, 'master_xfer[%d] %c, addr=0x%02x, ' 'len=%d%s\n', ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W', msgs[ret].addr, msgs[ret].len, (msgs[ret].flags & I2C_M_RECV_LEN) ? '+' : ''); if (in_atomic() || irqs_disabled()) { ret = mutex_trylock(&adap->bus_lock); /* I2C activity is ongoing. */ mutex_lock_nested(&adap->bus_lock, adap->level); ret = adap->algo->master_xfer(adap,msgs,num); mutex_unlock(&adap->bus_lock); dev_dbg(&adap->dev, 'I2C level transfers not supported\n'); EXPORT_SYMBOL(i2c_transfer);
它們同i2c總線操作一樣,都要回歸于i2c標(biāo)準(zhǔn)的子系統(tǒng)中去,最終接口為i2c_transfer()。ioctl的應(yīng)用也是一樣,應(yīng)用層調(diào)用的參考代碼如下, m_imagerFd = open('/dev/ch7026', O_RDWR); perror('open device ch7026:'); ret = ioctl(m_imagerFd, POWER_ON, NULL); ret = ioctl(m_imagerFd, POWER_OFF, NULL); perror('ioctl:device ch7026:');
設(shè)備驅(qū)動(dòng)ioctl部分實(shí)現(xiàn)的部分,每個(gè)宏定義都可以配置成一個(gè)子功能塊,每個(gè)子模塊可以配置多個(gè)寄存器; #define SET_BRIGHTNESS 0x02 typedef struct samsung_t{ static int ch7026_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) unsigned char contrat = 0x30; unsigned char brightness = 0x31; ret = ch7026_i2c_write(&p_bank->c, contrat, value); printk('i2c transfer error\n'); printk('vga_contrat = 0x:%x\n', value); ret = ch7026_i2c_write(&p_bank->c, brightness, value); printk('i2c transfer error\n'); printk('vga_brightness = 0x:%x\n', value); ch7026_config(&p_bank->c); printk('ch7026 power up!\n'); for (i = 0; i < CH7026_POW_OFF_REGS; i++) { ret = ch7026_i2c_write(&p_bank->c, ch7026_reg_pow_off[i].subaddr, ch7026_reg_pow_off[i].value); if(ret != 0) printk('ch7026:write faild!\n'); printk('ch7026 power down!\n'); printk('unexpect command\n');
四、程序測(cè)試應(yīng)用層測(cè)試main函數(shù): static void print_usage(void); int main(int argc, char *argv[]) fd = open('/dev/ch7026', O_RDWR); perror('open device ch7026'); if( (argc == 4)&& (strcmp(argv[1], 'write') == 0) ) { if((argv[2][0]=='0') && (argv[2][1]=='x')) sscanf(argv[2], '0x%x', &addr); if((argv[3][0]=='0') && (argv[3][1]=='x')) sscanf(argv[3], '0x%x', &tmp); printf('Write: addr[0x%02x] [0x%02x] \n', addr, val); lseek(fd, addr, SEEK_SET); write(fd, &val, sizeof(unsigned char)); } else if( (argc == 3) && (strcmp(argv[1], 'read') == 0)) { if((argv[2][0]=='0') && (argv[2][1]=='x')) sscanf(argv[2], '0x%x', &addr); lseek(fd, addr, SEEK_SET); read(fd, &val, sizeof(val)); printf('Read: addr[0x%02x] [0x%02x] \n', addr, val); static void print_usage(void) printf('usage:./ch7026_test [commad]\n'); printf('./ch7026_test write [address] [value]\n'); printf('./ch7026_test read [address]\n'); printf('For example:\n'); printf('./ch7026_test write 0x03 0x01\n'); printf('./ch7026_test read 0x03\n');
在PC Linux上用交叉編譯器編譯設(shè)備驅(qū)動(dòng)模塊(make)和應(yīng)用程序(make test), 測(cè)試結(jié)果如下,可以看到應(yīng)用層讀寫i2c從設(shè)備的0x06寄存器成功: 在編譯設(shè)備驅(qū)動(dòng)模塊的時(shí)候,首先要先編譯內(nèi)核,因?yàn)樵O(shè)備驅(qū)動(dòng)中的許多函數(shù)定義都來自內(nèi)核,如i2c子系統(tǒng),'.o'后綴的就是內(nèi)核已經(jīng)編譯好的。當(dāng)然,Makefile還要加入編譯好的內(nèi)核路徑。 Makefile的內(nèi)容,更換自己的內(nèi)核路徑和交叉編譯器 KERNEL=/opt/kernel-s3c6410/htx-linux-2.6.28-g96p-***** #更換成自己的內(nèi)核路徑 CC=/usr/local/arm/4.2.2-eabi/usr/bin/arm-linux-gcc #更換成自己平臺(tái)的交叉編譯器 @make -C $(KERNEL) M=`pwd` modules @make -C /lib/modules/`uname -r`/build M=`pwd` modules_install @make -C /lib/modules/`uname -r`/build M=`pwd` clean arm-linux-gcc ch7026_test.c -o ch7026_test cp ch7026_test /mnt/hgfs/upload/
最后我把i2c總線方式和i2c設(shè)備驅(qū)動(dòng)方式配置從設(shè)備的源碼整理在一起上傳到資源區(qū)(https://download.csdn.net/download/psy6653/11014339), 寫了這么多本想設(shè)置2個(gè)下載積分安慰下自己得,但不知道怎么的系統(tǒng)默認(rèn)設(shè)置為5分(但修改不了),實(shí)在抱歉。不過沒有關(guān)系,有多的積分的朋友就贊助下哈,沒積分的朋友可以給我留言,我會(huì)用wan盤單獨(dú)分享給你。
|