Read / Write on I2C
Hi.
I am opening I2C like this on android:
int Init()
{
if ((mI2C = open("/dev/i2c-3", O_RDWR)) < 0)
{
printf(“Opening I2C Failed”);
return 0;
}
if (ioctl(mI2C, I2C_SLAVE, MASTER_ADDR) < 0)
{
printf("Cannot communicate with Master");
return 0;
}
return 1;
}
it works fine.
I write with this :
int Write(unsigned char* aValue, int aLength)
{
if (mI2C != 0)
{
if (write(mI2C, aValue, aLength) != aLength)
{
printf(“Failed to write to the I2C bus.\n”);
return -1;
}
}
return aLength;
}
Also works fine.
But the problem is with the Read.
int Read(unsigned char* aValue, int aLength)
{
if (mI2C != 0)
{
return read(mI2C, aValue, aLength);
}
return -1;
}
If there is nothing in the buffer, I expect it to return -1. But it don’t. It read the number of requested bytes and it’s all garbage or old data.
If I write data from a slave, the function Read is ok.
So, the problem is why Read doesn’t return -1 or blocks until data is available. Any idea ?
Thank you!
Simon