Page 1 of 1

Problem w/C communication i2c

Posted: Sat Mar 05, 2016 10:38 pm
by itsagoodquestion
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;
}

Re: Problem w/C communication i2c

Posted: Sun Mar 06, 2016 4:08 pm
by Goraxium
Can I suggest that you try using the WiringPi library? I had all sorts of issues trying to get my own code to work with no real success (reads seemed to work, but writes failed), until I started using WiringPi. Here's a link to the I2C reference:
http://wiringpi.com/reference/i2c-library/

The functions you'd be interested in are:

Code: Select all

int deviceAddr = 0x68;//as seen in "i2cdetect -y 1"
int wakeupReg = 0x68;
int wakeupCmd = 0x6b;
int device = wiringPiI2CSetup(deviceAddr);
int check = wiringPiI2CWriteReg8(device, wakeupReg, wakeupCmd);
close(device);
Also, don't be afraid to hit that code button and dump code between the two code blocks in forums (otherwise you lose all of your formatting).

Re: Problem w/C communication i2c

Posted: Sun Mar 06, 2016 7:57 pm
by itsagoodquestion
Hey friend, thanks for your response.

Unfortunately I don't like using libraries like that because I can't understand what's happening beneath it so it will eventually create problems for me elsewhere.

I solved the problem, all I did was change 0x68 to 0x6b and it worked fine. I ended up having to loose a lot of sleep writing a library specifically to hook all the functions in smbus.o to see exactly what gets sent / received from the python script, but now it works great : ) : ): ) :D :D :D :D :D :D :D :lol: :lol: :lol: :lol:

if it would be useful to someone else I can share my hooking library.

Re: Problem w/C communication i2c

Posted: Sun Mar 06, 2016 8:46 pm
by danjperron