I'm trying to interface to an I2C device and have uncovered what I think is a major bug in the Azure Sphere I2C libraries.
I have opened the I2CMaster, set the speed and try to read from the device:
const uint8_t readLength = 8; const uint8_t address = 0xD0; uint8_t b1[20]; uint8_t b2[20]; uint8_t b3[20]; uint8_t b4[20]; uint8_t b5[20]; uint8_t b6[20]; I2CMaster_WriteThenRead(i2cFd, i2cAddress, &address, 1, b1, readLength); I2CMaster_WriteThenRead(i2cFd, i2cAddress, &address, 1, b2, readLength); I2CMaster_WriteThenRead(i2cFd, i2cAddress, &address, 1, b3, readLength); I2CMaster_WriteThenRead(i2cFd, i2cAddress, &address, 1, b4, readLength); I2CMaster_WriteThenRead(i2cFd, i2cAddress, &address, 1, b5, readLength); I2CMaster_WriteThenRead(i2cFd, i2cAddress, &address, 1, b6, readLength); Log_Debug("\nb1[0] = %x, b2[0] = %x, b3[0] = %x, b4[0] = %x, b5[0] = %x, b6[0] = %x\n", b1[0], b2[0], b3[0], b4[0], b5[0], b6[0]);
With a readLength of 8 or less, then the content of the output buffers is as expected, and the following is printed to the console:
b1[0] = 61, b2[0] = 61, b3[0] = 61, b4[0] = 61, b5[0] = 61, b6[0] = 61
However if I make my readLength greater than 8, I get the following console output:
b1[0] = 80, b2[0] = 0, b3[0] = 0, b4[0] = 0, b5[0] = 0, b6[0] = 0
and my output buffers contain an extra byte before the expected data.
In other words, buffer[0] is rubbish, and my read data is from buffer[1] onwards.
If I mix up the readLengths with a combination of values <=8 and > 8, then I discover that the first read of 9 or more bytes is good but subsequent reads of 9 or more bytes are bad with the additional byte at the start, until the next read of 8 or less bytes which is good.
Can anyone else reproduce this? I'm using an external I2C device with address 0x77 on the click socket #2.