#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
//#include </linux/i2c-dev.h>
#define I2C_SLAVE 0x0703 /* Use this slave address */
int main(int argc ,char **argv)
{
#define IIC_Path "/dev/i2c/0"
int fd;
int addr = 0x50; /* The I2C address */
int res;
char i;
char length;
char register_addr = 0x00;
char write_buf[10];
char read_buf[10];
//snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
fd = open(IIC_Path, O_RDWR);
if (fd < 0)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf("failed open %s \n",IIC_Path);
exit(1);
}
if (ioctl(fd, I2C_SLAVE, addr) < 0)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf("failed ioctl set addr\n");
exit(1);
}
/* Using I2C Write, equivalent of
i2c_smbus_write_word_data(file, register, 0x6543) */
write_buf[0] = register_addr;
write_buf[1] = 0x43;
write_buf[2] = 0x65;
write_buf[3] = 0x11;
write_buf[4] = 0x22;
length=5;
if (write(fd, &write_buf[0], length) !=length)
{
/* ERROR HANDLING: i2c transaction failed */
printf("failed to write data to EEPROM \n");
}
//char buf[10];
//char sendbuffer1[2];
write_buf[0]=register_addr;
//write_buf[1]=register_addr;
write(fd,&write_buf[0],1);
//res=read(fd,buff,count);
/* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */
if (read(fd, &read_buf[0], length) != length)
{
/* ERROR HANDLING: i2c transaction failed */
printf("failed to read data from EEPROM \n");
}
else
{
/* buf[0] contains the read byte */
for(i=0;i<length;i++)
printf("read read_buf[%d]=%x\n",i,read_buf[i]);
}
close(fd);
return 0;
}