#include <stdio.h> #include <linux/types.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/ioctl.h> #include <errno.h> #include <assert.h> #include <string.h>
#define I2C_RETRIES 0x0701 #define I2C_TIMEOUT 0x0702 #define I2C_RDWR 0x0707
struct i2c_msg { __u16 addr; /* slave address */ __u16 flags; __u16 len; __u8 *buf; /* pointer to msg data */ };
struct i2c_rdwr_ioctl_data { struct i2c_msg *msgs; /* pointers to i2c_msgs */ int nmsgs; /* number of i2c_msgs */ };
int main(int argc, char **argv) { struct i2c_rdwr_ioctl_data data; struct i2c_msg msg; unsigned int fd, i, j; unsigned char buff[9] = {0, 0x12, 0x32, 0x46, 0xf4, 0x9f, 0xa4, 0xc8, 0x78}; unsigned char buf2 = 0; unsigned char buf3[8] = {0}; int ret;
fd = open("/dev/i2c-0", O_RDWR); if (!fd) { printf("Error on opening the device file\n"); return 0; }
data.nmsgs = 1; data.msgs = &msg;
msg.addr = 0x52; msg.flags = 0; msg.len = 9; msg.buf = buff; ioctl(fd,I2C_TIMEOUT,5); /* set the timeout */ ioctl(fd,I2C_RETRIES,5); for(i=0; i<16; i++) { buff[0] = i*8; buff[8]++; //ioctl(fd, I2C_RDWR, &data);
}
msg.buf = &buf2; msg.len = 1; ioctl(fd, I2C_RDWR, &data);
msg.buf = buf3; msg.flags = 1; msg.len = 8;
for(j=0; j<16; j++) { ioctl(fd, I2C_RDWR, &data); for(i=0; i<8; i++) printf("0x%02x ", buf3[i]); printf("\n"); }
close(fd); return; }
|