arduino / arduino/ArduinoCore-samd

Loss of arbitration not checked during I2C master read - code can hang here forever

Open
#322 0 comments 0 reactions 0 assignees View on GitHub
bug Component: Wire
Dominant language
C
Stars
502
Forks
740
PR merge metrics
No merged PRs in 30d

Description

In function readDataWIRE (here https://github.com/arduino/ArduinoCore-samd/blob/d72f11735d3588f4128b016b346f41ef9812b060/cores/arduino/SERCOM.cpp#L635) the while loop checks only the SB bit, that will be set to 1 if a byte is correctly received. In case of bus errors, like loss of arbitration, the MB bit will be set together with the ERROR bit, but the SB bit will remain 0 resulting in the code hanging there forever.
This happens often to me with high bus load and I was able to identify where the code was getting stuck in an infinite loop.
The code is implemented correctly with this additional check for example in function startTransmissionWIRE (https://github.com/arduino/ArduinoCore-samd/blob/d72f11735d3588f4128b016b346f41ef9812b060/cores/arduino/SERCOM.cpp#L512-L521), but not in readDataWIRE. Unfortunately the readDataWIRE function returns a byte of data, so there is no easy way to inform the caller that the operation failed. The caller, in this case function requestFrom from Wire.cpp (https://github.com/arduino/ArduinoCore-samd/blob/d72f11735d3588f4128b016b346f41ef9812b060/libraries/Wire/Wire.cpp#L65-L98) would try to read following bytes, even if the communication already dropped.
I already implemented a fix that seems to be working. I will do some more testing and I will create a pull request, just to give you guys a starting point for a working solution.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in cores/arduino/SERCOM.cpp at readDataWIRE and compare its loop with startTransmissionWIRE; then trace requestFrom in libraries/Wire/Wire.cpp. The fix is done when a loss of arbitration or other bus error cannot leave the read hanging, and requestFrom handles the failed communication consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.