/*****RAS-32IO-V2 I2C Read Sample Program*****/ #include #include #include #include #include #include #include #include #include int fd; //char *i2cdev = "/dev/i2c-0"; char *i2cdev = "/dev/i2c-1"; unsigned char buf[2]; int i2cwrite(char *buf, int len) { if((write(fd, buf, len)) != len){ return(1); } return(0); } int i2cread(int offset, char *buf, int len) { if(i2cwrite(buf,1)){ printf("Err:- i2cwrite\n"); return(1); } *buf = (char)offset; if(read(fd,buf,len) != len) { printf("Err:- i2cread\n"); return(1); } return(0); } main() { unsigned char SLAVE_ADDRESS = 0x11; //SLAVE ADDRESS unsigned char rddat; printf("**** I2C READ SAMPLE ****\n"); if((fd = open(i2cdev, O_RDWR)) < 0){ printf("Can't open i2c\n"); exit(1); } if(ioctl(fd,I2C_SLAVE, SLAVE_ADDRESS) < 0){ printf("Unable to get bus access to talk to slave\n"); exit(1); } //PORT 0 READ// buf[0] = 0x02; if(i2cread(fd,buf,1)){ exit(1); } rddat = buf[0]; printf("Read Data ---- %02x\n",rddat); //PORT 1 READ// buf[0] = 0x03; if(i2cread(fd,buf,1)){ exit(1); } rddat = buf[0]; printf("Read Data ---- %02x\n",rddat); close(fd); return 0; }