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

added I2C TwoWire::busIdle to be checked by writeReadAsync #2798

Conversation

veecle-stefan
Copy link
Contributor

Problem
When using writeReadAsync in a multi-threaded environment (e.g. with FreeRTOS), the DMA interrupt fires immediately after the TX FIFO is loaded—not when the I2C peripheral has actually finished clocking out those bytes. As a result, a new writeReadAsync call might start too early and interrupt the ongoing hardware transfer. This issue only affects write-only transactions (rbytes == 0), because read transactions already use a different interrupt that waits for RX completion.

Solution
This PR adds a bool busIdle() function that checks if the I2C hardware is truly idle (i.e., no master activity and the transmit FIFO is empty). Inside writeReadAsync, if the bus is still busy, we return false—just as we already do if a DMA transfer is in progress. This allows the caller to detect the “bus still busy” case and retry the operation once the bus becomes free.

Benefits

  • Prevents partial writes when a new transaction starts too soon.
  • Allows cleaner multi-thread or asynchronous usage of writeReadAsync by giving callers a straightforward way to avoid interrupting a still-active write.
  • In testing, this improved stability and reduced CPU load significantly when accessing complex sensors (e.g. Adafruit BNO085) at higher rates.

Copy link
Owner

@earlephilhower earlephilhower left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition, looks good to me!

@earlephilhower earlephilhower merged commit 96a4059 into earlephilhower:master Feb 9, 2025
26 checks passed
@veecle-stefan veecle-stefan deleted the added-I2C-TwoWire--busIdle-to-be-checked-by-writeReadyAsync branch February 10, 2025 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants