Fixed the RP2040 interrupt problem #8505
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.
I noticed a bug in the pulseio module for the RP2040 when using it:
Under specific circumstances, an item getting pushed into the queue during the PIO interrupt handler can be overwritten.
This happens specifically when the "len--" of popleft() is being executed: len is loaded into a register, then the register is decremented, then written back to memory. If the interrupt happens after len is loaded from memory into a register, or after it's decremented - but before it's written back to memory - then the value added in that interrupt will be overwritten (since the register state is restored when the interrupt ends, and the old decremented value of len is written back in, even though len was incremented in the handler).
The fix is not particularly elegant, but it requires no spinlocks or interrupt disabling, and in testing has not failed.