Fix I2C hang when power removed from pull-ups #8827
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes #2253 and fixes #8093.
Explanation
This works by creating the nrfx_twim device with a callback (
twim_event_handler
).When the callback is triggered it marks that the transfer has finished and records any error/success code in the twim_peripheral data structure.
We replace the nrfx_twim_xfer function with our
_twim_xfer_with_timeout
which make a note of the start time, and waits until either the callback has triggered or 1s has elapsed.If the timeout is triggered it generates an
OSError: Timed out
Trying to use the i2c bus again after power restoration fails (generates an OSError), but it can be deinited and reinited and it then works
Sample Code
The following code now works (adapted from example in #8093: