Skip to content

fix(teensy4): send pending writeBit bits at transaction boundaries - #5147

Merged
zackees merged 1 commit into
masterfrom
fix/issue-5064
Nov 7, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/issue-5064

Conversation

@zackees

@zackees zackees commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Part of #5064 (does not close it; see "Not met"). Part of #5024.

On Teensy 4, Teensy4HardwareSPIOutput::writeBit only transmitted on every eighth call, and waitFully, finalizeTransmission and release never finished a partial group. So select(); writeBit<0>(1); endTransaction(); sent nothing, and the bit leaked into the next transaction's first byte. writeBytesValueRaw also skipped bits still pending.

Fix

  • Bits followed by a byte: they go out in front of it as one 9-15 bit LPSPI frame, as before. The pending state is now cleared after the send.
  • Leftover 1-7 bits: LPSPI frames are at least 8 bits, so these cannot go as a frame. The driver hands both pads to GPIO, clocks the bits out in mode 0 at half-period fl::delayNanoseconds steps, then restores both pads' mux and pad-control values. Every LPSPI write here blocks until it is done, so the shifter is idle at the handoff.
  • Flush points: release, waitFully, finalizeTransmission and writeBytesValueRaw send pending bits first. Pending bits can no longer reach a later transaction or be overtaken by raw bytes, and chip select is released only after they are out.
  • TCR: after a merged frame it is reset to 8-bit frames, rather than restored from a read-back that can lag the command FIFO.
  • Bus index: values outside 0-2 are now a compile error instead of falling off the end of port().
  • Header layout: namespace fl and the warning push/pop now sit inside the FL_IS_TEENSY_4X block they open in.
  • Legacy copy: src/platforms/arm/mxrt1062/fastspi_arm_mxrt1062.h shared the live header's include guard, so whichever was included first silently replaced the other. It is now a one-line shim, like its siblings in that directory.

Evidence

  • Test: tests/platforms/arm/teensy/teensy4_common/fastspi_arm_mxrt1062.cpp runs the real class, its real LpspiBus and the real SM16716Controller on three simulated LPSPI peripherals. The simulator records frame lengths (frames under 8 bits count as errors), chip select, pad ownership, the receive FIFO, and GPIO latches on pins 11/13.
  • Cases:
    • every BIT 0-7 with all 256 values;
    • bit counts 1-9, 15, 16 and 17;
    • bit-byte-bit, bit-word, and raw repeated bytes;
    • waitFully, finalizeTransmission, endTransaction and release;
    • reselect with no carry-over;
    • byte-only transfers unchanged;
    • CFGR1/CCR/TCR and pad state preserved;
    • indices 0, 1 and 2 each driving only their own peripheral;
    • three SM16716 pixels plus the 50-bit header, all as 8- and 9-bit frames.
  • RED: against the original header, the test fails to compile: the bare port().RDR; read is an unused-value error on the simulated register. With only that line made an explicit read, it fails at its first check: endTransaction sent 0 bits, not 1.
  • Mutations, each failing the test at an assertion, then restored:
    • no flush in release;
    • waitFully or finalizeTransmission as no-ops;
    • raw bytes skipping pending bits;
    • the pending count kept;
    • lone bits sent as a short frame;
    • TCR left at the merged size;
    • a frame one bit short;
    • the data mux not restored;
    • the clock pad not restored;
    • the clock not parked low after each bit.
  • Real Teensy SDK builds: bash compile teensy41|teensy40 --examples Apa102 pass, with the sketch temporarily given calls through both header paths: Teensy4HardwareSPIOutput on indices 0, 1 and 2, and every changed method. The sketch was restored and not committed.
  • Tests and lint: bash test fastspi_arm_mxrt1062 --debug passes with sanitizers. bash test --cpp passes: 359/359 unit tests, but only 1/1 example was re-run, the rest being cached. bash lint exits 0.

Not met from #5064

  • No production caller: on Teensy 4, SPIOutput resolves to SPIDeviceProxy, which has no writeBit, so SM16716 cannot reach this class through the normal dispatch. The test routes the real controller onto this class directly. This PR fixes the public class; it does not add a producer.
  • Host simulation only: the simulator's LPSPI model (instant frames, an 8-bit minimum, the RX word per frame, TCR as plain state) and its GPIO model were written from the driver and the reference-manual rules, not checked against silicon.
  • No timing: the GPIO pulse width, the pacing and the frame-rate cost of the pad handoff are not measured. The host test cannot see the fl::delayNanoseconds calls.
  • test: gate software SPI changes on exhaustive production-path and mutation evidence #5031 binding: not done.

Limits

  • Pad handoff: lone bits use FastPin::setOutput, which calls Teensy's pinMode and writes the pads. Both values are saved and restored. The GPIO direction bits stay as outputs; the mux decides what reaches the pad.
  • RX-capture assumption: sendFrame still spins on RX-not-empty, as before. It assumes RX capture is on, which beginTransaction sets.

Summary by CodeRabbit

  • Bug Fixes
    • Improved SPI output reliability on Teensy 4, including transmissions that mix individual bits and bytes.
    • Ensured incomplete bit sequences are sent when a transmission finishes or the bus is released.
  • Tests
    • Added coverage for bit and byte ordering, transmission boundaries, and pixel-controller output.

…5064)

Bits ahead of a byte still ride in one 9-15 bit LPSPI frame. A 1-7 bit
remainder, which no LPSPI frame can carry, goes out by GPIO with the pads
saved and restored. release, waitFully, finalizeTransmission and
writeBytesValueRaw flush first, so pending bits never reach the next
transaction. The legacy mxrt1062 copy becomes a shim.
@zackees
zackees enabled auto-merge November 7, 2026 12:12
@github-actions github-actions Bot added the sanitized Title, body and conversation screened; review text requires separate screening. label Nov 7, 2026
@coderabbitai

coderabbitai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

Walkthrough

The Teensy 4 SPI implementation now handles pending bits across bit, byte, and transmission operations. A simulated hardware test harness checks output ordering, transaction boundaries, bus indices, and SM16716 controller data.

Changes: Teensy 4 SPI output

Layer / File(s) Summary
Pending-bit transmission and shared header
src/platforms/arm/teensy/teensy4_common/fastspi_arm_mxrt1062.h, src/platforms/arm/mxrt1062/fastspi_arm_mxrt1062.h
The shared implementation adds pending-bit handling for LPSPI frames and GPIO clocking. It flushes pending bits during release, waits, raw byte writes, and finalization. The MXRT1062 header now includes the shared implementation.
Simulated peripheral and output validation
tests/platforms/arm/teensy/teensy4_common/fastspi_arm_mxrt1062.cpp
The test harness simulates LPSPI and GPIO output. Tests cover bit and byte ordering, transaction boundaries, all three output indices, and SM16716 pixel data and header bits.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Possibly related issues

  • Issue 5064 describes Teensy 4 pending-bit loss and ordering defects at transaction boundaries. This PR adds flushing and ordering behavior for those pending bits.

Possibly related PRs

  • FastLED/FastLED#5142: Both PRs address lost writeBit output for SM16716 on Teensy hardware SPI and test pixel start bits and the 50-bit header.

Suggested labels: ci-platforms-unit-test

Merge Risk: Moderate · up to f0694

Resolve the production integration requirement before merging. Direct users also face broken class-qualified calls and lost tail bits with alternate pin routing; the simulated tests need targeted coverage improvements.

- [ ] 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

@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Nov 7, 2026
@zackees
zackees added this pull request to the merge queue Nov 7, 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 7, 2026
@github-actions

github-actions Bot commented Nov 7, 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)
teensy41 47,104 (0) 21,738 (0)

@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed security: unscreened-content labels Nov 7, 2026
Merged via the queue into master with commit e3ee4b9 Nov 7, 2026
132 of 133 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Nov 7, 2026
@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 7, 2026
@zackees
zackees deleted the fix/issue-5064 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.

1 participant