I2C(TCA9545APWR+eeprom(AT24C04YI)

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<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
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####\n");
        return(-1);
    }
    //写操作
    lseek(fd,0x40,SEEK_SET); //定位地址,地址是0x40
    if((size=write(fd,bufw, len))<0)//写入数据
    {
        printf("write error\n");
        return 1;
    }
    printf("writeok\n");
    //读操作
    lseek(fd,0x40, SEEK_SET);//准备读,首先定位地址,因为前面写入的时候更新了当前文件偏移量,所以这边需要重新定位到0x40.
    if((size=read(fd,buf,len))<0)//读数据
    {
        printf("readerror\n");
        return 1;
    }
    printf("readok\n");
    for(i=0; i< len; i++)
    printf("buff[%d]=%c\n",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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值