Problem w/C communication i2c
Posted: Sat Mar 05, 2016 10:38 pm
I have a raspberry pi v2 b, running raspbian connected to a mpu6050 over i2c. I found many drivers in C for it but none work, but the python script works very well - http://blog.bitify.co.uk/2013/11/readin ... berry.html
I'm trying to write that in C but I'm failing. Right now, the only thing I want to do is write 0x6b to i2c address 0x68 to wake up the mpu6050 but it's not working. All my functions return success, but I then run a python script to read the registers of the mpu6050 after running the code below & all values return 0 because it's still sleeping. When I run "bus.write_byte_data(address, power_mgmt_1, 0)" from python though - it works perfectly. Please take a look at this code and let me know if you see a problem. Thanks.
int Mpu6050_i2c_Begin()
{
int fd = -1, IoctlRetVal = -1;
char *fileName = "/dev/i2c-1";
// Open port for reading and writing
if ((fd = open(fileName, O_RDWR)) < 0)
{
exit(1);
printf("[!] Could not open %s\n", fileName);
}
// Set the port options and set the address of the device
IoctlRetVal = ioctl (fd, I2C_SLAVE, 0x68);
if ( IoctlRetVal < 0)
{
printf("[!] Could not set 0x68 address on ioctl\n");
close(fd);
exit(1);
}
else
{
printf("ioctl() retval = %d\n", IoctlRetVal);
}
return fd;
}
void WriteByte(int fd, __u8 address, __u8 value)
{
int RetValWrite = i2c_smbus_write_byte_data(fd, address, value);
if (RetValWrite < 0)
{
printf("[!] i2c_smbus_write_byte_data() fail\n");
close(fd);
exit(1);
}
else
{
printf("write retval - %d\n", RetValWrite);
}
}
int main(int argc, char **argv)
{
int fd = Mpu6050_i2c_Begin();
//close(fd);
WriteByte(fd, 0x68, 0x6b);
// everything succeeds by here - but the device is not woken up
close(fd);
return 0;
}
I'm trying to write that in C but I'm failing. Right now, the only thing I want to do is write 0x6b to i2c address 0x68 to wake up the mpu6050 but it's not working. All my functions return success, but I then run a python script to read the registers of the mpu6050 after running the code below & all values return 0 because it's still sleeping. When I run "bus.write_byte_data(address, power_mgmt_1, 0)" from python though - it works perfectly. Please take a look at this code and let me know if you see a problem. Thanks.
int Mpu6050_i2c_Begin()
{
int fd = -1, IoctlRetVal = -1;
char *fileName = "/dev/i2c-1";
// Open port for reading and writing
if ((fd = open(fileName, O_RDWR)) < 0)
{
exit(1);
printf("[!] Could not open %s\n", fileName);
}
// Set the port options and set the address of the device
IoctlRetVal = ioctl (fd, I2C_SLAVE, 0x68);
if ( IoctlRetVal < 0)
{
printf("[!] Could not set 0x68 address on ioctl\n");
close(fd);
exit(1);
}
else
{
printf("ioctl() retval = %d\n", IoctlRetVal);
}
return fd;
}
void WriteByte(int fd, __u8 address, __u8 value)
{
int RetValWrite = i2c_smbus_write_byte_data(fd, address, value);
if (RetValWrite < 0)
{
printf("[!] i2c_smbus_write_byte_data() fail\n");
close(fd);
exit(1);
}
else
{
printf("write retval - %d\n", RetValWrite);
}
}
int main(int argc, char **argv)
{
int fd = Mpu6050_i2c_Begin();
//close(fd);
WriteByte(fd, 0x68, 0x6b);
// everything succeeds by here - but the device is not woken up
close(fd);
return 0;
}