Skip to content

fix(avr): drain native SPI bytes and release peripheral pins for GPIO writeBit #5055

Closed

Description

@zackees

Context

Two-round follow-up audit of baseline 94dab9c for #5024 verifies a separate defect from #5052: correct bit selection alone cannot make native AVR GPIO writeBit pulses visible when the peripheral still owns the pins, and pending bytes must drain before ownership changes.

src/platforms/avr/fastspi_avr.h has five independent GPIO methods:

  • USART1 at 92-102: init sets UCSR1B RXEN1/TXEN1 (47), writeBit never releases the transmitter; enable_pins/disable_pins (105-106) are empty.
  • USART0 at 248-258: same TXEN0/RXEN0 ownership from init (204), empty hooks (261-262).
  • Modern SPI0_CTRLA at 427-437: init enables SPI (386); writeBit never clears SPI_ENABLE_bm, and hooks (440-441) are empty. stop (402) clears it but is not called by select/release/writeBit.
  • Classic SPSR at 615-629 and SPSR0 at 803-817 do clear SPE before GPIO and restore it after, but neither first drains a pending byte; both unconditionally restore enabled state and clear shouldWait.

USART waitFully (83,239) aliases wait, which checks UDRE only (80,236): a free data buffer does not prove the shifter finished. Modern waitFully (418) checks completion via IF when shouldWait is true, but writeBit never calls it. Classic writeBit likewise does not call its completion wait.

Verified peripheral rules

ATmega4808/4809 datasheet §24.2.2, printed p312 establishes that configured output values belong to enabled SPI; being idle does not return them to PORT GPIO. §24.3.2.1 states IF is set after the byte shifts out.
Microchip USART MSPIM data transfer establishes TXEN ownership of TxD and XCK transfer-clock function.
USART control B explains delayed transmitter disable until pending transmissions finish.
USART control A distinguishes buffer readiness from complete transmission. Use the actual target vendor headers/docs for the handoff; do not invent register shims or assume clearing TXEN alone releases every clock override.

Actual callers

src/fl/chipsets/sm16716.h:62-66 invokes shared writePixelsToSPI<FLAG_START_BIT>. src/platforms/shared/spi_pixel_writer.h:43-49 emits start bit then three writeByte calls, and loops straight into the next writeBit; the final byte can still be pending. Its release at 57 does not pre-drain either. SM16716 writeHeader (29-44) also interleaves byte/bit methods. The native class writePixels methods use post-waits on flagged bytes, but USART post-wait only proves UDRE. USART classes expose direct public APIs but have no current internal instantiation found. Modern/classic native SPIOutput is production-selected by src/platforms/avr/spi_output_template.h:22-24.

Proposal

Define a safe byte-to-GPIO-to-byte handoff for all five implementations: complete pending transfer, yield actual mux ownership of data/clock, emit the requested bit, and restore relevant peripheral/control/wait state. Audit release/endTransaction only as needed for this ordering. Keep #5052 focused on selected-bit predicates; this independently fails even for correct constant-one start bits.

Acceptance criteria

  • RED first on actual production methods using a mux-aware peripheral/register fixture: PORT writes while peripheral-owned must not count as observable pin edges. Current USART0/1 and modern SPI paths must fail the start-bit waveform check even after predicates are corrected. GREEN requires a visible requested data level and one high/low clock pulse while GPIO actually owns the pins.
  • Model buffered/shift-register busy state separately. Write a byte followed immediately by writeBit and another byte: all prior byte edges complete before takeover, the inserted bit occurs exactly once, and the following byte transfers normally. UDRE=true/TXC=false must not pass the UART drain gate; pending modern/classic SPI transfers must not be aborted.
  • Exercise actual shared SM16716 pixel and header call paths for multiple pixels, not only native writePixels or isolated writeBit. Include idle/no-previous-byte calls so a stale/never-set completion flag cannot hang.
  • Cover already-enabled and already-disabled peripheral state, unrelated control bits, mode/rate, wait bookkeeping, flags with write-one-to-clear semantics and back-to-back handoffs. Mutants omitting each drain/release/restore stage must fail that stage's check. Verify all five guards separately.
  • Compile representative USART0/1, SPI0_CTRLA, SPSR and SPSR0 board/configuration variants via repository wrappers (uno/mega, leonardo, available ATmega4809 or ATtiny1604/1616, ATmega328PB or explicit real guarded fixture if inventory lacks it). Use target vendor headers; no new workflow.

Decisions

P2 protocol correctness, separate from #5052 logical operator and #5025 generic policy adoption. This issue covers ownership and ordering across independently implemented native handoffs because the shared public bit/byte contract needs one consistent acceptance suite. No claim of observed device failure or throughput improvement: evidence is source plus documented mux behavior, without physical measurement.

Reproduction

rg -n 'writeBit|TXEN|SPI_ENABLE|UDRE|waitFully|enable_pins|disable_pins' src/platforms/avr/fastspi_avr.h
rg -n 'writeBit|writeByte|writePixelsToSPI' src/fl/chipsets/sm16716.h src/platforms/shared/spi_pixel_writer.h
Metadata-only all-state duplicate searches AVR SPI GPIO ownership and USART writeBit found no same issue; #5052 is the related but distinct selected-bit defect. No source edits/builds/tests during discovery.

Activity

  1. added
    sanitized
    on Nov 5, 2026
  2. zackees commented on Nov 22, 2026

    @zackees
    MemberAuthor

    Closing with an accepted limitation. The planned split for release() drain and the surviving waitFully mutant is not needed: #5168 (23c8ccc, closes #5133) already fixed both.

    Fix: #5080 (bc6f9b9), plus test #5134 (552633e) and #5168. All five native AVR writeBit methods drain the pending byte, release the peripheral, emit the bit and restore control. USART arms and drains on TXC rather than UDRE (src/platforms/avr/fastspi_avr.h:89-107, :286-304), and SPI0_CTRLA releases and restores SPI_ENABLE_bm (:509-514). release() and select() call waitFully() first on all five classes (:154-155, :351-352, :539-540).

    Proven by: tests/platforms/avr/fastspi_avr.cpp, a mux-aware pin model with a busy shifter. It runs the real writePixelsToSPI<FLAG_START_BIT> on all five classes, and :423-434 asserts no byte is still shifting at select()/release() with UDRE held set. From reading the code, that assertion should now also catch a USART waitFully() without the TXC drain; the test was not run here. The board builds named in #5080 and #5134 passed: uno, leonardo, mega, nano_every and attiny1604/1616.

    Accepted limitations:

    • No SM16716 strip has run on a real AVR: no AVR board is attached here.
    • SPSR0 and ATmega328PB have no board target and are covered only by the host fixture.

    The #5031 binding is replaced by a rescoped host-side gate, tracked in #5031.

  3. moved this from Triage to Done in FastLED Trackeron Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    sanitized

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions