Skip to content

fix(avr): finish the pending SPI byte before select() and release() (#5133) - #5168

Merged
zackees merged 1 commit into
masterfrom
fix/avr-spi-release-drain-5133
Nov 8, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/avr-spi-release-drain-5133

Conversation

@zackees

@zackees zackees commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Closes #5133.

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.

@zackees
zackees enabled auto-merge November 8, 2026 18:50
@github-actions github-actions Bot added the sanitized Title, body and conversation screened; review text requires separate screening. label Nov 8, 2026
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Nov 8, 2026
@coderabbitai

coderabbitai Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

Walkthrough

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.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • 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.

Share

@zackees
zackees added this pull request to the merge queue Nov 8, 2026
@github-actions github-actions Bot added security: unscreened-content and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 8, 2026
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Memory usage (this PR vs. its merge base)

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).

Target FLASH [bytes] RAM [bytes]
attiny85 2,460 (0) 136 (0)

Merged via the queue into master with commit 23c8ccc Nov 8, 2026
131 of 132 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Nov 8, 2026
@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed security: unscreened-content labels Nov 8, 2026
@zackees
zackees deleted the fix/avr-spi-release-drain-5133 branch November 17, 2026 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sanitized Title, body and conversation screened; review text requires separate screening.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

AVR USART and SPI0_CTRLA release() can deselect before the last byte finishes

1 participant