#include
#include
#include
#include
#include
#include
#define CHIP_ADDR 0x50 //24CXX
#define PAGE_SIZE 64 //根据自己的数据手册
#define I2C_DEV "/dev/i2c-0"
static int read_eeprom(int fd,char buff[],int addr,int count)
{
int res;
if (write(fd,&addr,1)!=1)
{
printf("Can't write %s's addr %d\n",I2C_DEV,addr);
return -1;
}
res = read(fd,buff,count);
printf("read %d bytes at 0x%02x\n\r",res,addr);
return res;
}
static int write_eeprom(int fd,char buff[],int addr,int count)
{
int res;
int i;
static char sendbuffer[PAGE_SIZE+1];
memcpy(sendbuffer+1,buff,count);
sendbuffer[0]=addr;
res = write(fd,&addr,count+1);
printf("write %d bytes at 0x%02x\n\r",res,addr);
return res;
}
int main(void)
{
int fd,i,res;
unsigned char buf[PAGE_SIZE];
fd=open(I2C_DEV,O_RDWR);
if(fd<0)
{
printf("Can't Open %s !!!\n\r",I2C_DEV);
return -1;
}
res = ioctl(fd,I2C_TENBIT,0);
res = ioctl(fd,I2C_SLAVE,CHIP_ADDR);
for(i=0;i
write_eeprom(fd,buf,i,1);
}
for(i=0;i
read_eeprom (fd,buf+i,i,1);
printf("buf[0x%02x] = 0x%02x\n\r",i,buf[i]);
}
close(fd);
return 0;
}