Answered by:
I2C read bug

Question
-
I think I have discovered a major bug in the Azure Sphere I2C libraries.
I'm trying to interface to an I2C device from my Avnet Azure Sphere.
i2cFd = I2CMaster_Open(AVNET_MT3620_SK_ISU2_I2C); int result = I2CMaster_SetBusSpeed(i2cFd, I2C_BUS_SPEED_STANDARD); result = I2CMaster_SetTimeout(i2cFd, 100); result = I2CMaster_SetDefaultTargetAddress(i2cFd, i2cAddress);
const uint8_t readLength = 9;
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]);
When readLength is <= 8, the code works as expected, the content of the output buffers is good, and the following is printed to the console (register d0 is a chip ID which is read-only and should always be 0x61):
b1[0] = 61, b2[0] = 61, b3[0] = 61, b4[0] = 61, b5[0] = 61, b6[0] = 61
However if readLength > 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.
Im using Azure Sphere 19.07 on an Avnet Starter Kit board with an external I2C device with address 0x77.
My current workaround is to read larger lengths in blocks of 8 (or less) bytes.
- Edited by Jeremy_K Sunday, September 29, 2019 1:28 PM
- Changed type Jeremy_K Sunday, September 29, 2019 1:28 PM
- Changed type António Sérgio Azevedo - MSFTMicrosoft employee, Moderator Wednesday, October 2, 2019 11:07 AM
Answers
-
Hello Jeremy, all.
We have fixed this issue and it will be deployed on Retail Eval Feed 19.11. Please wait for that release and let us know if you stop seeing the additional byte at the start when readLength > 8.
Thank you again for reporting this issue!
FYI: Here are the instructions to set-up Retail Eval feed:
- Marked as answer by António Sérgio Azevedo - MSFTMicrosoft employee, Moderator Friday, November 15, 2019 8:08 PM
All replies
-
I can confirm the same behaviour with a different I2C device. I put an oscilloscope on the I2C signal and it the data appears to be correct, but the MT3620 is returning the duplicated first byte.
- Edited by FredMurphy Wednesday, October 2, 2019 10:24 AM
-
-
For completeness, here's my workaround code for reading more than 8 bytes:
int32_t retVal = I2CMaster_Write(i2cFd, dev_id, ®_addr, 1); if (retVal < 0) { return -1; } uint16_t i = 0; /* there seems to be a bug where bad data is returned when reading more than 8 bytes at a time so read in blocks of 8 bytes for now */ for (i = 0; i < len; i += 8) { uint8_t readLength = len - i; if (readLength > 8) { readLength = 8; } retVal = I2CMaster_Read(i2cFd, dev_id, ®_data[i], readLength); if (retVal < 0) { return -1; } }
Also in my case, I don't think I was receiving a duplicate of the first expected byte in position 0.
- Edited by Jeremy_K Wednesday, October 2, 2019 11:15 AM
-
Can you reproduce this when using I2CMaster_Read Function ?
-
The same issue occurs whether using I2CMaster_Write followed by I2CMaster_Read or a single I2CMaster_WriteThenRead
I do not know of any problems with the I2C write functionality, only the reading.
- Edited by Jeremy_K Wednesday, October 2, 2019 11:30 AM
-
Thanks for checking it Jeremy. I am at the moment trying to repro your issue. Can I also kindly as you to test ISU0, ISU1, ISU3 and ISU4 to check if the issue is persistent?
See : https://docs.microsoft.com/en-us/azure-sphere/app-development/app-manifest
- I2cMaster: BETA feature List of I2C master interfaces that are used by the application. For the MT3620 development board, the possible values are "ISU0" through "ISU4".
I guess you only tested ISU2, that's why I ask.
Thanks!
- Edited by António Sérgio Azevedo - MSFTMicrosoft employee, Moderator Monday, October 7, 2019 1:51 PM
-
You are correct, I have only tested ISU2 so far.
I am testing with an Avnet Azure Sphere starter kit, which does not expose ISU3 or ISU4. In order to use I2C on ISU0 or ISU1, I'll need to add some jiggery-pokery between my Click Board and the starter kit, I'm not sure I have time at present to run those tests.
Have you been able to reproduce my issue? Other users have confirmed the bug exists with other I2C devices on the same ISU2 peripheral.
-
-
-
-
:)
This is not a bug, but incomplete documentation ...
Mediatek: Hardware SPI for this chipset ( and other )
support 8 bytes for one time transaction...example:
look members description:
VMUINT32 data_length; The data length. Please note: this length should not exceed 8.
-
Sorry I don't agree that this should solely be fixed in documentation.
The API should not necessarily be restricted by hardware limitations. If a new Azure Sphere device is released, for example, you want the API to be as compatible as possible.
In my view, if there is a hardware limit of 8 bytes then the appropriate fix is for the Azure Sphere libraries to abstract that away and make multiple reads under the hood.
-
-
according to Microsoft, you're right :)
https://docs.microsoft.com/en-us/azure-sphere/reference/applibs-reference/applibs-i2c/function-i2cmaster-write
ssize_t I2CMaster_Write(int fd, I2C_DeviceAddress address, const uint8_t *data, size_t length);
... This function provides the same functionality as the POSIX write() function ... -
-
Hello Jeremy, all.
We have fixed this issue and it will be deployed on Retail Eval Feed 19.11. Please wait for that release and let us know if you stop seeing the additional byte at the start when readLength > 8.
Thank you again for reporting this issue!
FYI: Here are the instructions to set-up Retail Eval feed:
- Marked as answer by António Sérgio Azevedo - MSFTMicrosoft employee, Moderator Friday, November 15, 2019 8:08 PM