• 大小: 199KB
    文件类型: .gz
    金币: 1
    下载: 0 次
    发布日期: 2021-05-09
  • 语言: 其他
  • 标签: IIC  linux  

资源简介

一种是使用read and write的方式读写。一种是使用构造i2c_msg结构体的方式并利用ioctl的方式读写

资源截图

代码片段和文件信息

        #include 
        #include 
        #include 
        #include 
        #include 
        #include 
        #include 
        #include 
        #define I2C_RETRIES 0x0701
        #define I2C_TIMEOUT 0x0702
        #define I2C_RDWR 0x0707 
        /*********定义struct i2c_rdwr_ioctl_data和struct i2c_msg,要和内核一致*******/

struct i2c_msg
        {
                unsigned short addr;
                unsigned short flags;
        #define I2C_M_TEN 0x0010
        #define I2C_M_RD 0x0001
                unsigned short len;
                unsigned char *buf;
        };

struct i2c_rdwr_ioctl_data
        {
                struct i2c_msg *msgs;
                int nmsgs; 
        /* nmsgs这个数量决定了有多少开始信号,对于“单开始时序”,取1*/
        };

/***********主程序***********/
        int main()
        {
                int fdretk;
                struct i2c_rdwr_ioctl_data e2prom_data;
                fd=open(“/dev/i2c-0“O_RDWR);
        /*
        dev/i2c-0是在注册i2c-dev.c后产生的,代表一个可操作的适配器。如果不使用i2c-dev.c
        的方式,就没有,也不需要这个节点。
        */
                if(fd<0)
                {
                        perror(“open error“);
                }
                e2prom_data.nmsgs=2; 
        /*
        *因为操作时序中,最多是用到2个开始信号(字节读操作中),所以此将
        *e2prom_data.nmsgs配置为2
        */
                e2prom_data.msgs=(struct i2c_msg*)malloc(e2prom_data.nmsgs*sizeof(struct i2c_msg));
                if(!e2prom_data.msgs)
                {
                        perror(“malloc error“);
                        exit(1);
                }
                ioctl(fdI2C_TIMEOUT2);/*超时时间*/
                ioctl(fdI2C_RETRIES2);/*重复次数*/
                /***write data to e2prom**/

                e2prom_data.nmsgs=1;
                (e2prom_data.msgs[0]).len=27; //1个 e2prom 写入目标的地址和1个数据 
                (e2prom_data.msgs[0]).addr=0x51;//e2prom 设备地址
                (e2prom_data.msgs[0]).flags=0; //write
                (e2prom_data.msgs[0]).buf=(unsigned char*)malloc(27);
                (e2prom_data.msgs[0]).buf[0]=0x00;// e2prom 写入目标的地址
for (k=1;k<27;k++)
                (e2prom_data.msgs[0]).buf[k]=(unsigned char)k;//the data to write 

         ret=ioctl(fdI2C_RDWR(unsigned long)&e2prom_data);
                if(ret<0)
                {
                        perror(“write error“);
                }

free((e2prom_data.msgs[0]).buf);
                sleep(1);


        /******read data from e2prom*******/
               e2prom_data.nmsgs=2;
                (e2prom_data.msgs[0]).len=1; //e2prom 目标数据的地址
                (e2prom_data.msgs[0]).addr=0x51; // e2prom 设备地址
                (e2prom_data.msgs[0]).flags=0;//write
 (e2prom_data.msgs[0]).buf=(unsigned char*)malloc(1);
                (e2prom_data.msgs[0]).buf[0]=0x00;//e2prom数据地址
                (e2prom_data.msgs[1]).len=26;//读出的数据
                (e2prom_data.msgs[1]).addr=0x51;// e2prom 设备地址 
                (e2prom_dat

评论

共有 条评论