Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible bug with i2cdevWrite - sending a single byte of data, sends two bytes #949

Closed
amitga opened this issue Feb 22, 2022 · 9 comments
Closed
Assignees
Labels
Milestone

Comments

@amitga
Copy link

amitga commented Feb 22, 2022

Hi.
I recently ran into a problem while sending a single byte of data using i2cdevWrite:
My goal is to send/write a single byte of data to an I2C slave (no internal address/register).
When I send like this: i2cdevWrite(I2C_DeckBus, devAddr, 1, &myByte); actually my byte (uint8_t) value is sent as one byte, but there is also another empty (zero) byte sent immediately after that byte (within the same data transfer it seems).

I have confirmed this with various MCUs acting as an I2C slave (Arduino, PIC16f...).
This also happend if the data is a buffer/array of data, but we send 1 as the requested length.
If we choose to send more than 1 from a buffer, all works as expected.

I have looked into the code, and it seems related to the ISR handling the I2C events.
Specifically here:

if(i2c->messageIndex == i2c->txMessage.messageLength)

I think that the problem may be that the messageIndex is not initialised/reset when that single byte is sent.
I added i2c->messageIndex = 0; right after:

I2C_ITConfig(i2c->def->i2cPort, I2C_IT_BUF, DISABLE);

and it seems to solve the problem: working with sending a single and multiple bytes, as stated in data length when calling i2cdevWrite function.

But, I'm still not sure why this would happen in the first place, or if my quick fix is correct or can cause some other side-affects.

I would like to hear your thoughts about this.
BTW, I'm running a CF Bolt with a bit dated firmware, but its seems that the ISR logic hasn't changed much in master branch compared to mine (they are basically identical). I cannot confirm if this bug happens with newer/latest firmware (I would have to re-write my custom code).

Thanks!

@knmcguire
Copy link
Member

Hi Thanks for notifying us on this! It seem that this should not happen, but we will discuss it internally why this happens. Probably we can give you an update on that next week.

@amitga
Copy link
Author

amitga commented Mar 3, 2022

Thanks!

@knmcguire
Copy link
Member

Hi, sorry we were not able to handle this issue during this week's triage meeting so it will be the next, so that is 3 weeks (29th of March).

Hope this will not block your development too much, but if it does, please let us know.

@amitga
Copy link
Author

amitga commented Apr 10, 2022

Thanks.
Just an update: I ended up using i2cdevReadReg8 instead, which seems to work correctly (doesn't send an additional byte).

@knmcguire
Copy link
Member

thanks for the update! good that this works for you and this will be useful information for us to figure out what is wrong

@knmcguire knmcguire added the bug label Apr 12, 2022
@whoenig
Copy link
Contributor

whoenig commented Apr 12, 2022

Not sure if it's related, but https://github.com/bitcraze/crazyflie-firmware/tree/dev-systemId had some fixes(?) for I2C that we never ended up merging back to master, due to limited testing.

@tobbeanton tobbeanton self-assigned this Jun 8, 2022
@tobbeanton
Copy link
Member

I tested and could replicate this today. The strange thing is that any instruction after I2C_ITConfig(i2c->def->i2cPort, I2C_IT_BUF, DISABLE); fixes the problem. The STM32F405 I2C peripheral is bugged and this might be one of them. My guess is that the TXE interrupt isn't cleared fast enough or similar and gets called again which send another byte on the buss.

@tobbeanton
Copy link
Member

Fixing this with an added instruction is as good as it gets right now.

@amitga
Copy link
Author

amitga commented Jul 12, 2022

Thanks for the updates!

@krichardsson krichardsson added this to the 2022.09 milestone Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants