Code: Select all
int write_register(unsigned char reg, unsigned char value, int *p_fd)
{
unsigned char buf[2] = { 0 };
int fd = *p_fd;
if(ioctl(fd, I2C_SLAVE, MPL3115A2_ADDR) < 0)
{
printf("Unable to get bus access to talk to slave\n");
close(fd);
return 1;
}
buf[0]=reg;
buf[1]=value;
if((write(fd, buf, 2)) != 2)
{
printf("Error writing to i2c slave\n");
close(fd);
return 1;
}
return 0;
}Code: Select all
int write_register(unsigned char reg, unsigned char value, int *p_fd)
{
int result = 0;
unsigned char buf[2] = { 0 };
int fd = *p_fd;
struct i2c_msg rdwr_msg[1];
struct i2c_rdwr_ioctl_data rdwr_data1;
rdwr_data1.msgs = rdwr_msg;
rdwr_data1.nmsgs = 1;
if(ioctl(fd, I2C_SLAVE, MPL3115A2_ADDR) < 0)
{
printf("Unable to get bus access to talk to slave\n");
close(fd);
return 1;
}
buf[0]=reg;
buf[1]=value;
rdwr_data1.msgs[0].addr = MPL3115A2_ADDR;
rdwr_data1.msgs[0].flags = 0;
rdwr_data1.msgs[0].len = 2;
rdwr_data1.msgs[0].buf = buf;
result = ioctl( fd, I2C_RDWR, &rdwr_data1 );
if ( result < 0 )
{
printf( "rdwr ioctl error: %d\n", errno );
perror( "reason" );
}
return 0;
}