devicetree:
i2c-switch@70 {
compatible = "nxp,pca9545"; /* I2C switch pca9545 */
#address-cells = <0x1>;
#size-cells = <0x0>;
reg = <0x70>;
i2c@0 {
reg = <0x0>;
#address-cells = <0x1>;
#size-cells = <0x0>;
eeprom@56 {
compatible = "at,24c04";
reg = <0x56>;
};
};
};
kernel:
将AT24驱动程序编进内核。
APP:
write:
echo test eeprom access write and read > /sys/bus/i2c/drivers/at24/2-0056/eeprom
read: cat /sys/bus/i2c/drivers/at24/2-0056/eeprom
APP:C
#include
#include
#include
#include
#include
#include
int access_eeprom(const char* path)
{
int fd, size, len, i;
char buf[50]= {0};
char *bufw="Hi,this is an second eepromtest!";//要写入的数据
len=strlen(bufw);//数据长度
fd= open(path,O_RDWR);//打开文件
if(fd< 0)
{
printf("####i2c test device open failed####
");
return(-1);
}
//写操作
lseek(fd,0x40,SEEK_SET); //定位地址,地址是0x40
if((size=write(fd,bufw, len))<0)//写入数据
{
printf("write error
");
return 1;
}
printf("writeok
");
//读操作
lseek(fd,0x40, SEEK_SET);//准备读,首先定位地址,因为前面写入的时候更新了当前文件偏移量,所以这边需要重新定位到0x40.
if((size=read(fd,buf,len))<0)//读数据
{
printf("readerror
");
return 1;
}
printf("readok
");
for(i=0; i< len; i++)
printf("buff[%d]=%c
",i, buf[i]);//打印数据
close(fd);
}
int main(void){
//access_eeprom("/sys/bus/i2c/drivers/at24/2-0056/eeprom");
//sleep(5);
access_eeprom("/sys/bus/i2c/drivers/at24/6-0056/eeprom");
return 0;
}