-
-
Notifications
You must be signed in to change notification settings - Fork 101
Add timeout handling for I2C in Wire #497
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
Conversation
Also made the default timeout shorter than the 1000 ms that was hardcoded
CLA assistant check
All committers have signed the CLA.
HanzHager
commented
Oct 8, 2025
This should solve the issue #492
HanzHager
commented
Oct 8, 2025
The actions to reset the hardware in handleTimeout() is not well tested. It would be great if someone who knows better can take a look at it to see if it makes sense and if there should be changes.
The new logic for the timeout loop in read_from() and write_to() had a bug. It is now fixed.
Adding timeout handling for I2C in Wire according to previously defined solution on other platforms Also reduced the default timeout from 1000ms to 25ms
HanzHager
commented
Oct 9, 2025
Fixed a bug in timeout handling in read_from() and write_to() and made the commit cleaner
HanzHager
commented
Oct 11, 2025
After some more testing I conclude that I need even more testing to make sure it works as intended.
I think that in the current solution, the master may not be able to correctly handle when a device on the bus is stuck in clock stretching. This should most likely be fixed before ever releasing this pull request.
per1234
commented
Oct 11, 2025
Thanks for your pull request, and for taking the time to provide an update on the situation @HanzHager!
I see that one of Arduino's developers also submitted a similar pull request: #494
It would be great if you would review that prior pull request and help us to understand whether your PR provides any advancements when compared to the other, and whether the other might also suffer from any of the potential problems you have identified in testing of this PR.
HanzHager
commented
Oct 11, 2025
Thanks for your pull request, and for taking the time to provide an update on the situation @HanzHager!
I see that one of Arduino's developers also submitted a similar pull request: #494
It would be great if you would review that prior pull request and help us to understand whether your PR provides any advancements when compared to the other, and whether the other might also suffer from any of the potential problems you have identified in testing of this PR.
I looked at this PR and it looks like it is using setTimeout() instead of setWireTimeout() for the implementation.
There is already a defined way to handle setting timeout for "Wire" and this includes a function called setWireTimeout() as well as other functions described in https://docs.arduino.cc/language-reference/en/functions/communication/wire/setWireTimeout/
I have tried to make a complete implementation that harmonizes best with this and the AVR implementation.
Unfortunately, I am not fully confident that I am handling the Renesas api properly and I have realized that there needs to be some considerations for timeout situations where some device on the bus is keeping the SCL line low as result of clock stretching or some fault. So, I guess I need more testing and potentially some help in reviewing and making additional testing my solution, before it is deployed.
I am quite new in making contributions like this, so I probably need quite some guidance to get it right.
HanzHager
commented
Oct 11, 2025
I just noticed that #494 have renamed the setTimeout() to setWireTimeout(). The function sets timeout in milliseconds, but the referred implementation https://docs.arduino.cc/language-reference/en/functions/communication/wire/setWireTimeout/ are using microseconds as well as a boolean second parameter that tells the implementation to reset the bus on timeout or not.
So, even if #494 is small and simple solution, it is not compatible with other implementations and the reference documentation.
This version has more testing and some refinements for handling of timeout It still has some printouts and comments that might be useful for others review and testing When reviewed and tested, I will remove this I have also packaged the same files as an independent library that is easy to download and install as a library This can be found at: https://github.com/HanzHager/Wire
HanzHager
commented
Oct 13, 2025
I have now updated with a new better tested version, but more testing and review would be great
I order to make testing easier, I have prepared a standalone library that can be downloaded and easily used in the Arduino IDE. It can be found at https://github.com/HanzHager/Wire (same identical files Wire.cpp and Wire.h)
Adding timeout handling for I2C in Wire according to previously defined solution on other platforms
Also reduced the default timeout from 1000ms till 25ms
Also created src directory as well as adding keywords.txt and library.properties so that the "Wire" part of the repository can be handled as a separate library