You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(avr): finish the pending SPI byte before select() and release() (#5133) - #5168
The AVR USART1, USART0 and SPI0_CTRLA SPI outputs called mPSelect->release() while the last byte could still be shifting. writePixelsToSPI calls release() straight after its final writeByte, so a device behind a Selectable could be deselected mid-byte. On all five AVR classes, select() also reprogrammed the SPI rate without first finishing a pending byte.
Fix
release(): USART1, USART0 and SPI0_CTRLA now call waitFully() first, as the classic classes already did.
select(): all five classes call waitFully() before touching the Selectable or the rate. When nothing is pending, waitFully() returns at once.
Evidence
RED: a new check in tests/platforms/avr/fastspi_avr.cpp, run against the existing fake-register harness and the real production classes, failed on master at fastspi_avr.cpp:431 (busyAtRelease 1 == 0). The --debug re-run failed the same way, with no sanitizer reports. The check uses a Selectable that records whether a byte is still shifting when select() or release() reaches it.
GREEN:bash test fastspi_avr passes for all five classes.
Builds:bash compile uno, nano_every and leonardo build Apa102 and Blink.
Lint:bash lint exits 0.
Limits
Not run on hardware.
No in-tree chipset was found that sets a Selectable on these classes, so the fix is defensive for external users. The select() drain also covers the rate reprogramming that every user hits.
Summary by CodeRabbit
Bug Fixes
Improved AVR SPI transfer handoffs so devices are selected and deselected only after the shifter is idle. This helps ensure transfers complete cleanly when switching between selectable devices and prevents the status-register hang that could occur during handoff.
The AVR USART and hardware SPI implementations now wait for transmission completion before changing SPI rate or deselecting. The AVR handoff test checks that release and subsequent select observe an idle shifter.
Changes: AVR SPI handoff
Layer / File(s)
Summary
Drain the shifter at handoff src/platforms/avr/fastspi_avr.h, tests/platforms/avr/fastspi_avr.cpp
USART and hardware SPI select() methods call waitFully() before configuring the SPI rate. USART1, USART0, and megaAVR hardware SPI release() methods call it before deselecting. The test probe checks that release and the next select observe an idle shifter. It also checks for status-register hangs and restores the control register.
Issue 5055 concerns pending AVR bytes during ownership changes. This PR adds waits around select and release but does not change the issue’s writeBit GPIO handoff behavior.
Possibly related PRs
FastLED/FastLED#5080: Both PRs change AVR SPI drain behavior in fastspi_avr.h, including around release().
Merge Risk: Moderate · up to 3069e
USART release can hang when callers enable transmit-complete interrupts, preventing device deselection. Resolve or explicitly accept this compatibility risk before merging.
- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Change in examples/Blink per target. The left number is this PR's image flash / attributed RAM in bytes; the colored delta is the change from the merge base (red ↑ is larger, green ↓ is smaller, 0 is unchanged).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sanitizedTitle, body and conversation screened; review text requires separate screening.
1 participant
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.
Closes #5133.
The AVR USART1, USART0 and SPI0_CTRLA SPI outputs called
mPSelect->release()while the last byte could still be shifting.writePixelsToSPIcallsrelease()straight after its finalwriteByte, so a device behind aSelectablecould be deselected mid-byte. On all five AVR classes,select()also reprogrammed the SPI rate without first finishing a pending byte.Fix
release(): USART1, USART0 and SPI0_CTRLA now callwaitFully()first, as the classic classes already did.select(): all five classes callwaitFully()before touching theSelectableor the rate. When nothing is pending,waitFully()returns at once.Evidence
tests/platforms/avr/fastspi_avr.cpp, run against the existing fake-register harness and the real production classes, failed on master atfastspi_avr.cpp:431(busyAtRelease1 == 0). The--debugre-run failed the same way, with no sanitizer reports. The check uses aSelectablethat records whether a byte is still shifting whenselect()orrelease()reaches it.bash test fastspi_avrpasses for all five classes.bash compile uno,nano_everyandleonardobuild Apa102 and Blink.bash lintexits 0.Limits
Selectableon these classes, so the fix is defensive for external users. Theselect()drain also covers the rate reprogramming that every user hits.Summary by CodeRabbit