Hello everyone, it's my first time posting so please be nice.
I'm working on a project where I have a small SBC (not a raspi nor a beaglebone, but the problem is my software so let's move on to that) and the program I've written is having trouble writing to the i2c interface.
I can open the i2c file descriptor, and I can read from it just fine. BUT when I write to it, it returns the correct number of written bytes, correctly sends the first byte, but the rest are all zeros. This problem stumped me until I connected my board to an oscilloscope.
It's sending the correct i2c address, and the correct register location, but the data is all zeros.
I can't seem to post images so what I'm seeing on my scope is: 0xD0 0x7E 0x00
What I should be seeing is 0xD0 0x7E 0xB6.
Below is my code. If someone can tell me where I've gone wrong I would greatly appreciate it.
*The calls to LE_INFO are just debug statements that show up in logs*
Code:
#include <linux/i2c-dev.h>
int main(){
int i2c = open("/dev/i2c-4", O_RDWR);
if(i2c < 0){
LE_INFO("The i2c bus could not be opened, error: %d", i2c);
exit(1);
}else{
LE_INFO("The i2c bus was opened.");
}
//you have the bus, now send the address of the device you want to communicate with.
if((ioctl(i2c, I2C_SLAVE, 0x68)) < 0){
LE_INFO("Could not acquire control of i2c bus");
close(i2c);
exit(1);
}
int location[] = {0,0};
int res = 0;
location[0] = 0x7E; //command register
location[1] = 0xB6; //command
LE_INFO("register location %d, command %d", location[0], location[1]);
//setup bus to read a byte.
if((res = write(i2c, location, 2)) != 2){ //write the
LE_INFO("read register location was not written to i2c bus, %d bytes written",res);
}
close(i2c);
exit(0);
}