1 在内核中配置I2C 驱动
Linux-2.6.32.2 对S2C2440 的I2C 接口提供了完善的驱动,因此我们只需在内核中配置一下即可使用。
在内核源代码目录执行:make menuconfig,进入内核配置主菜单,依次选择进入如下子菜单:
Device Drivers --->
<*> I2C support --->
I2C Hardware Bus support --->
如图,我们看到这里已经选择好了“<*> S3C2410 I2C Driver”,这里的S3C2410 也可以适用于S3C2440,因为它们的I2C 端口及寄存器定义都是完全相同的。
以上配置所对应的驱动源代码为:linux-2.6.32.2/drivers/i2c/busses/i2c-s3c2410.c
2 、I2C-EEPROM测试源码
gedit eeprog.c
#include <stdio.h> #include <fcntl.h> #include <getopt.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include "24cXX.h" #define usage_if(a) do { do_usage_if( a , __LINE__); } while(0); void do_usage_if(int b, int line) { const static char *eeprog_usage = "I2C-24C08(256 bytes) Read/Write Program, ONLY FOR TEST!\n"; if(!b) return; fprintf(stderr, "%s\n[line %d]\n", eeprog_usage, line); exit(1); } #define die_if(a, msg) do { do_die_if( a , msg, __LINE__); } while(0); void do_die_if(int b, char* msg, int line) { if(!b) return; fprintf(stderr, "Error at line %d: %s\n", line, msg); fprintf(stderr, " sysmsg: %s\n", strerror(errno)); exit(1); } static int read_from_eeprom(struct eeprom *e, int addr, int size) { int ch, i; for(i = 0; i < size; ++i, ++addr) { die_if((ch = eeprom_read_byte(e, addr)) < 0, "read error"); if( (i % 16) == 0 ) printf("\n %.4x| ", addr); else if( (i % 8) == 0 ) printf(" "); printf("%.2x ", ch); fflush(stdout); } fprintf(stderr, "\n\n"); return 0; } static int write_to_eeprom(struct eeprom *e, int addr) { int i; for(i=0, addr=0; i<256; i++, addr++) { if( (i % 16) == 0 ) printf("\n %.4x| ", addr); else if( (i % 8) == 0 ) printf(" "); printf("%.2x ", i); fflush(stdout); die_if(eeprom_write_byte(e, addr, i), "write error"); } fprintf(stderr, "\n\n"); return 0; } int main(int argc, char** argv) { struct eeprom e; int op; op = 0; usage_if(argc != 2 || argv[1][0] != '-' || argv[1][2] != '\0'); op = argv[1][1]; fprintf(stderr, "Open /dev/i2c/0 with 8bit mode\n"); die_if(eeprom_open("/dev/i2c/0", 0x50, EEPROM_TYPE_8BIT_ADDR, &e) < 0, "unable to open eeprom device file " "(check that the file exists and that it's readable)"); switch(op) { case 'r': fprintf(stderr, " Reading 256 bytes from 0x0\n"); read_from_eeprom(&e, 0, 256); break; case 'w': fprintf(stderr, " Writing 0x00-0xff into 24C08 \n"); write_to_eeprom(&e, 0); break; default: usage_if(1); exit(1); } eeprom_close(&e); return 0; }
gedit 24cXX.h
#ifndef _24CXX_H_ #define _24CXX_H_ #include <linux/i2c-dev.h> #include <linux/i2c.h> #define EEPROM_TYPE_UNKNOWN 0 #define EEPROM_TYPE_8BIT_ADDR 1 #define EEPROM_TYPE_16BIT_ADDR 2 struct eeprom { char *dev; // device file i.e. /dev/i2c-N int addr; // i2c address int fd; // file descriptor int type; // eeprom type }; /* * opens the eeprom device at [dev_fqn] (i.e. /dev/i2c-N) whose address is * [addr] and set the eeprom_24c32 [e] */ int eeprom_open(char *dev_fqn, int addr, int type, struct eeprom*); /* * closees the eeprom device [e] */ int eeprom_close(struct eeprom *e); /* * read and returns the eeprom byte at memory address [mem_addr] * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address) */ int eeprom_read_byte(struct eeprom* e, __u16 mem_addr); /* * read the current byte * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address) */ int eeprom_read_current_byte(struct eeprom *e); /* * writes [data] at memory address [mem_addr] * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address) */ int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data); #endif
gedit 24cXX.c
/*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <linux/fs.h> #include <sys/types.h> #include <sys/ioctl.h> #include <errno.h> #include <assert.h> #include <string.h> #include "24cXX.h" static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command, int size, union i2c_smbus_data *data) { struct i2c_smbus_ioctl_data args; args.read_write = read_write; args.command = command; args.size = size; args.data = data; return ioctl(file,I2C_SMBUS,&args); } static inline __s32 i2c_smbus_write_quick(int file, __u8 value) { return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL); } static inline __s32 i2c_smbus_read_byte(int file) { union i2c_smbus_data data; if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data)) return -1; else return 0x0FF & data.byte; } static inline __s32 i2c_smbus_write_byte(int file, __u8 value) { return i2c_smbus_access(file,I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,NULL); } static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command) { union i2c_smbus_data data; if (i2c_smbus_access(file,I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data)) return -1; else return 0x0FF & data.byte; } static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value) { union i2c_smbus_data data; data.byte = value; return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, I2C_SMBUS_BYTE_DATA, &data); } static inline __s32 i2c_smbus_read_word_data(int file, __u8 command) { union i2c_smbus_data data; if (i2c_smbus_access(file,I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA,&data)) return -1; else return 0x0FFFF & data.word; } static inline __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value) { union i2c_smbus_data data; data.word = value; return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, I2C_SMBUS_WORD_DATA, &data); } static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value) { union i2c_smbus_data data; data.word = value; if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command, I2C_SMBUS_PROC_CALL,&data)) return -1; else return 0x0FFFF & data.word; } /* Returns the number of read bytes */ static inline __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values) { union i2c_smbus_data data; int i; if (i2c_smbus_access(file,I2C_SMBUS_READ,command, I2C_SMBUS_BLOCK_DATA,&data)) return -1; else { for (i = 1; i <= data.block[0]; i++) values[i-1] = data.block[i]; return data.block[0]; } } static inline __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, __u8 *values) { union i2c_smbus_data data; int i; if (length > 32) length = 32; for (i = 1; i <= length; i++) data.block[i] = values[i-1]; data.block[0] = length; return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, I2C_SMBUS_BLOCK_DATA, &data); } /* Returns the number of read bytes */ static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command, __u8 *values) { union i2c_smbus_data data; int i; if (i2c_smbus_access(file,I2C_SMBUS_READ,command, I2C_SMBUS_I2C_BLOCK_DATA,&data)) return -1; else { for (i = 1; i <= data.block[0]; i++) values[i-1] = data.block[i]; return data.block[0]; } } static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command, __u8 length, __u8 *values) { union i2c_smbus_data data; int i; if (length > 32) length = 32; for (i = 1; i <= length; i++) data.block[i] = values[i-1]; data.block[0] = length; return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, I2C_SMBUS_I2C_BLOCK_DATA, &data); } /* Returns the number of read bytes */ static inline __s32 i2c_smbus_block_process_call(int file, __u8 command, __u8 length, __u8 *values) { union i2c_smbus_data data; int i; if (length > 32) length = 32; for (i = 1; i <= length; i++) data.block[i] = values[i-1]; data.block[0] = length; if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command, I2C_SMBUS_BLOCK_PROC_CALL,&data)) return -1; else { for (i = 1; i <= data.block[0]; i++) values[i-1] = data.block[i]; return data.block[0]; } } static int i2c_write_1b(struct eeprom *e, __u8 buf) { int r; // we must simulate a plain I2C byte write with SMBus functions r = i2c_smbus_write_byte(e->fd, buf); if(r < 0) fprintf(stderr, "Error i2c_write_1b: %s\n", strerror(errno)); usleep(10); return r; } static int i2c_write_2b(struct eeprom *e, __u8 buf[2]) { int r; // we must simulate a plain I2C byte write with SMBus functions r = i2c_smbus_write_byte_data(e->fd, buf[0], buf[1]); if(r < 0) fprintf(stderr, "Error i2c_write_2b: %s\n", strerror(errno)); usleep(10); return r; } static int i2c_write_3b(struct eeprom *e, __u8 buf[3]) { int r; // we must simulate a plain I2C byte write with SMBus functions // the __u16 data field will be byte swapped by the SMBus protocol r = i2c_smbus_write_word_data(e->fd, buf[0], buf[2] << 8 | buf[1]); if(r < 0) fprintf(stderr, "Error i2c_write_3b: %s\n", strerror(errno)); usleep(10); return r; } #define CHECK_I2C_FUNC( var, label ) \ do { if(0 == (var & label)) { \ fprintf(stderr, "\nError: " \ #label " function is required. Program halted.\n\n"); \ exit(1); } \ } while(0); int eeprom_open(char *dev_fqn, int addr, int type, struct eeprom* e) { int funcs, fd, r; e->fd = e->addr = 0; e->dev = 0; fd = open(dev_fqn, O_RDWR); if(fd <= 0) { fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno)); return -1; } // get funcs list if((r = ioctl(fd, I2C_FUNCS, &funcs) < 0)) { fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno)); return -1; } // check for req funcs CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE ); CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE ); CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA ); CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE_DATA ); CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_WORD_DATA ); CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_WORD_DATA ); // set working device if( ( r = ioctl(fd, I2C_SLAVE, addr)) < 0) { fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno)); return -1; } e->fd = fd; e->addr = addr; e->dev = dev_fqn; e->type = type; return 0; } int eeprom_close(struct eeprom *e) { close(e->fd); e->fd = -1; e->dev = 0; e->type = EEPROM_TYPE_UNKNOWN; return 0; } #if 0 int eeprom_24c32_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data) { __u8 buf[3] = { (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data }; return i2c_write_3b(e, buf); } int eeprom_24c32_read_current_byte(struct eeprom* e) { ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer return i2c_smbus_read_byte(e->fd); } int eeprom_24c32_read_byte(struct eeprom* e, __u16 mem_addr) { int r; ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer __u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff }; r = i2c_write_2b(e, buf); if (r < 0) return r; r = i2c_smbus_read_byte(e->fd); return r; } #endif int eeprom_read_current_byte(struct eeprom* e) { ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer return i2c_smbus_read_byte(e->fd); } int eeprom_read_byte(struct eeprom* e, __u16 mem_addr) { int r; ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer if(e->type == EEPROM_TYPE_8BIT_ADDR) { __u8 buf = mem_addr & 0x0ff; r = i2c_write_1b(e, buf); } else if(e->type == EEPROM_TYPE_16BIT_ADDR) { __u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff }; r = i2c_write_2b(e, buf); } else { fprintf(stderr, "ERR: unknown eeprom type\n"); return -1; } if (r < 0) return r; r = i2c_smbus_read_byte(e->fd); return r; } int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data) { if(e->type == EEPROM_TYPE_8BIT_ADDR) { __u8 buf[2] = { mem_addr & 0x00ff, data }; return i2c_write_2b(e, buf); } else if(e->type == EEPROM_TYPE_16BIT_ADDR) { __u8 buf[3] = { (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data }; return i2c_write_3b(e, buf); } fprintf(stderr, "ERR: unknown eeprom type\n"); return -1; }
gedit Makefile
CFLAGS= -Wall -O2 CC=arm-linux-gcc i2c: eeprog.o 24cXX.o $(CC) $(CFLAGS) -o i2c eeprog.o 24cXX.o clean: rm -f i2c 24cXX.o eeprog.o
3、编译部署
# make
生成可执行文件 i2c,上传到开发板
4、查看设备号
[root@MrBenjamin]# cat /proc/devices | grep i2c 89 i2c
89 为i2c设备号
5、生成设备文件(如果设备文件已存在,跳过此步)
cd /dev mkdir i2c mknod -m 666 /dev/i2c/0 c 89 0
6、测试,看截图
读测试
写测试