Skip to content

bench(teensy4): hardware-validate the SdFat LPSPI transport on Teensy 4.0 and 4.1 #3983

Open

Description

@zackees

Hardware bench validation for the SdFat SPI transport work merged in #3971 / #3972 (parent #3970).

Why this is needed

The whole of #3970 shipped compile- and link-verified only. No Teensy hardware was available at any point. Specifically, on Teensy 4:

  • LpspiBus::transfer(fl::u8) has never returned a byte from a real card
  • LpspiBus::transfer(const void* tx, void* rx, fl::size count) is a new loop that has never moved a real block
  • The RX/TX FIFO flush added to beginTransaction() was written to fix a bug reasoned about from the register spec, never observed
  • SD init timing — the 74-clock burst, CMD0 entry, the 400 kHz → run-speed step — is entirely from the reference manual

A green build proves the symbols resolve. It proves nothing about whether a card mounts.

Scope is Teensy 4.0 and 4.1 only. Teensy 3.5/3.6 and LC are out of production; #3972's DSPI path stays as-is and we take user bug reports if it turns out broken there.

Setup

  • Teensy 4.1 (built-in SD socket is SDIO, not this path — use an SPI SD breakout) and Teensy 4.0
  • SPI SD card breakout on LPSPI4: MOSI 11, MISO 12, SCK 13, CS on any free GPIO
  • A FAT32 card with a .fled or .rgb + screenmap.json payload, per examples/Fx/FxSdCard/
  • Per CLAUDE.md, drive this through examples/AutoResearch/AutoResearch.ino rather than adding a new sketch — bash autoresearch <board>

Test 1 — does a card mount and read at all

Baseline that the transport works in isolation.

  • sd.begin(cs, SPI_HALF_SPEED) succeeds (fs_sdfat_teensy.h:115)
  • Directory enumeration returns real entries
  • A file read returns correct bytes — verify against a known checksum, do not eyeball it
  • FxSdCard renders actual frames

Failure here most likely means the transfer() polling loop or the CCR clock divisor is wrong.

Test 2 — the shared-bus case, which is the one I most expect to break

This is the scenario the FIFO flush was written for, and it is the highest-risk untested path.

Drive APA102/HD107 LEDs on the same LPSPI4 bus (pins 11/13) and stream frames off the SD card in the same sketch.

  • Call FastLED.show() at least once, then read from the card
  • Repeat interleaved for several minutes

The predicted failure without the flush: FastLED's LED driver writes TDR in a loop and never reads RDR, while CFGR1 leaves RX capture enabled, so a strip write leaves up to 16 stale bytes in the RX FIFO. The next SD transfer() returns a queued LED byte instead of the card's reply. That is silent data corruption, not an error — corrupt frames or CMD timeouts, no failure path triggered.

beginTransaction() now issues CR = LPSPI_CR_RRF | LPSPI_CR_RTF before re-enabling. This test is what confirms that fix actually works. If corruption appears only after the first show(), the flush is not doing its job.

Test 3 — clock correctness

fs_sdfat_teensy.h:115 passes SPI_HALF_SPEED = SD_SCK_MHZ(4).

  • Scope SCK during init: must be within the SD spec's 100–400 kHz window
  • Scope SCK during data transfer: should be ≤ 4 MHz and must never exceed it
  • Confirm the init → run-speed transition actually happens rather than staying slow

Test 4 — elision did not regress

A sketch that never calls beginSd() should emit no SdFat or LPSPI storage. Compare bash bloat teensy41 against a build with SD in use; the argument for this is currently structural only, never measured.

Pass criteria

  • Card mounts and reads verified by checksum on both 4.0 and 4.1
  • FxSdCard renders correctly from SD
  • Shared APA102 + SD sketch runs clean across many interleaved show()/read cycles with no corruption
  • SCK measured in spec for both init and run phases
  • No flash regression for non-SD sketches

If it fails

The most likely culprits, in order:

  1. The FIFO flush (Test 2) — newest code, written against a bug never observed
  2. LpspiBus::transfer(tx, rx, count) null-buffer handling — tx == nullptr clocks the fill byte, rx == nullptr discards but still pops RDR to keep the FIFO drained
  3. CCR divisor / clock configuration
  4. LpspiBus::get(0) resolving to the wrong peripheral instance — reviewed as correct (LPSPI4 on 11/12/13) but never confirmed on silicon

Related: #3980 proposes deleting FL_SDFAT_USE_ARDUINO_SPI. Worth resolving after this bench test, not before — if something here fails, that hatch is the first thing someone will reach for, even though it currently cannot link without a sketch-level #include <SPI.h>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions