Skip to content

sdfat(teensy): route the vendored SdFat SPI transport onto FastLED's own LPSPI/DSPI instead of Arduino SPIClass #3970

Closed
2 of 2 issues completed

Description

@zackees

Follow-up to #3777 / #3802, and the remaining half of #3838.

Summary

#3802 vendored the LPSPI bring-up so FastLED's LED-side SPI drivers stopped needing Arduino <SPI.h>. The SdFat transport was not part of that work and still reaches for SPIClass:

src/platforms/arm/teensy/sdfat/SpiDriver/SdSpiArduinoDriver.h:90   SPIClass *mSpi;
src/platforms/arm/teensy/sdfat/SpiDriver/SdSpiDriver.h:35          #include "SPI.h"
src/platforms/arm/teensy/sdfat/SpiDriver/SdSpiDriver.h:99          typedef SPIClass SpiPort_t;

These are now the only files under src/platforms/arm/teensy/ that use SPIClass outside of comments. Everything else was cleaned by #3802.

Impact

Fx/FxSdCard fails to link on every Teensy board that compiles it:

undefined reference to `SPIClass::begin()'
undefined reference to `SPI'
undefined reference to `SPIClass::transfer(void const*, void*, unsigned int)'
undefined reference to `SPIClass::end()'
SPISettings::ctar_clock_table / ctar_div_table     (3.5/3.6 only)

The affected set is exactly teensy35, teensy36, teensy40, teensy41 — FxSdCard.ino:1 is tagged // @filter: (memory is large), so the smaller Teensys never build it. After #3966 these are the last four red teensy boards; 30/31/32/LC are green.

Why the dependency cannot simply be declared

Measured, not assumed:

approach result
#if FL_HAS_INCLUDE(<SPI.h>) guard in the sketch library not selected — the finder skips conditional includes even when the condition is true
lib_deps=["SPI"] on the board reaches the generated ini, link errors clear, then build error: package error: library 'SPI' not found in registry — fbuild resolves names against the PlatformIO registry, and SPI ships inside the framework
unconditional #include <SPI.h> in FxSdCard.ino links on all four boards, but breaks the host build: example tests linux/windows fail with 'SPI.h' file not found (the stub platform has no SPI.h). Tried in #3969, closed.

The root cause is the same trap #3838 documents: framework-arduinoteensy bundles 92 libraries (Adafruit_NeoPixel, Audio, SPI, even FastLED), so the header always resolves while its sources are only compiled if the finder selects the library — and it only selects from unconditional sketch includes (FastLED/fbuild#1214, a deliberate tested invariant).

Why we cannot vendor Arduino's SPI

libraries/SPI/SPI.{h,cpp} in the Teensy framework is:

 * Copyright (c) 2010 by Cristian Maglie
 * Copyright (c) 2014 by Paul Stoffregen (Transaction API)
 * ... under the terms of either the GNU General Public License version 2
 * or the GNU Lesser General Public License version 2.1

FastLED is MIT. Copying it in would make FastLED effectively GPL (viral to every sketch) or LGPL (relinking rights + source-distribution obligations — a problem for closed firmware). This is why SPI differs from the trees already vendored successfully: SdFat 2.1.2 is MIT, and the PJRC Audio subset is permissive.

It is also 3,461 lines for the ~12 methods SdFat needs, and pulls in DMAChannel.h + EventResponder.h, so the dependency chain does not terminate.

So the fix is our own implementation against the registers, not a copy — exactly what #3802 did for the LED side.

The work

Teensy 4.0 / 4.1 — mostly wiring

teensy4_common/lpspi/lpspi_bus.h already provides most of the surface, including a genuine full-duplex byte exchange, and lpspi_hardware.h already carries the MISO pin/mux/select tables:

SdFat needs LpspiBus today
setMOSI / setMISO / setSCK present
begin() / end() present
beginTransaction() / endTransaction() present
transfer(u8) present — writes TDR, spins on FSR, returns RDR
port() raw register access present
transfer(buf, count) missing
transfer(tx, rx, count) missing
setTransferWriteFill(u8) missing

Tasks:

  • Add the three bulk-transfer methods to LpspiBus (naive loops over transfer(u8) first; FIFO-batch later if profiling wants it)
  • Introduce SdSpiFlDriver implementing SdFat's driver contract on LpspiBus, selected through the existing SdSpiDriver.h typedef seam
  • Default Teensy 4 to it; keep SdSpiArduinoDriver reachable behind a define as an escape hatch

Teensy 3.5 / 3.6 (K64/K66) — a real port

teensy36/fastspi_arm_k66.h already has the DSPI machinery — CTAR0/CTAR1 programming (update_ctars, update_ctar0), PUSHR/POPR, pin config. That is the same domain as the missing SPISettings::ctar_clock_table / ctar_div_table symbols. But it is written write-only for LED output: templated on _DATA_PIN/_CLOCK_PIN, never configures MISO, never reads POPR.

Tasks:

  • MISO pin mux configuration
  • POPR-based read path / full-duplex byte exchange
  • Frequency to CTAR computation (what Arduino ships as the two lookup tables)
  • SD initialisation clock ramp: 400 kHz for card init, then high speed
  • Validate on real 3.5/3.6 hardware — a green compile proves nothing here; SD init timing is unforgiving

Acceptance

  • bash compile teensy35|teensy36|teensy40|teensy41 --examples Fx/FxSdCard links with no Arduino SPI symbols
  • No SPIClass references remain under src/platforms/arm/teensy/sdfat/ outside comments
  • bash test --cpp still passes (83/83 examples on host — this is what fix(examples): declare the SPI dependency in FxSdCard — takes teensy to 8/8 green (#3838) #3969 broke)
  • No new sketch-level or lib_deps dependency is introduced
  • Singleton elision still holds: a sketch that never calls beginSd() emits no SdFat or SPI storage
  • SD read verified on hardware for at least one board per family

Secondary benefit

Arduino SPI and FastLED's own SPI bus manager cannot coordinate today, so a sketch driving APA102 LEDs and an SD card on the same bus is a latent conflict. Owning the transport removes that.

Separate question worth a look

lpspi_bus.h:99 carries the comment "Blocking single-byte exchange. Upstream SPI.h:1246-1257." If that was transcribed from the GPL/LGPL SPI.h rather than reimplemented from the reference manual, it is a pre-existing licensing question independent of this issue. A ten-line register poke is likely not protectable expression, but given the license mismatch above it deserves a deliberate decision rather than an assumption. Same for the SPI.h:1091-1122 citation in lpspi_hardware.h:54.

Sub-issues

Sub-issues

2 of 2 Issues completed
Loading 2 sub-issues

Activity

  1. zackees commented on Aug 22, 2026

    @zackees
    MemberAuthor

    Done. Both sub-issues are closed and the SdFat SPI transport no longer touches Arduino's SPIClass on any Teensy.

    sub-issue PR merged
    #3971 Teensy 4 (LpspiBus) #3973 54107f0b
    #3972 Teensy 3.x/LC (DspiBus) #3979 3de8d4f8

    Verified on merged master (3de8d4f8), Fx/FxSdCard:

    teensy35   LINKS  flash: 70056 bytes
    teensy36   LINKS  flash: 70192 bytes
    teensy40   LINKS  flash: 118784 bytes
    teensy41   LINKS  flash: 120832 bytes
    

    Plus bash test --cpp --clean 283/283 unit + 83/83 examples and bash lint clean.

    How it was solved

    Not by vendoring. Arduino's SPI library is GPLv2/LGPLv2.1 and FastLED is MIT, so copying it was never available. Instead both families got FastLED-owned bus classes, mirroring what #3802 had already done for the LED drivers — LpspiBus gained the two methods SdFat needed, and DspiBus is new for K20/K64/K66 plus the MKL26's different SPI module.

    CTAR values are computed from the K20/K64/K66 reference manual rather than transcribed from Arduino's ctar_clock_table / ctar_div_table. Copyright protects expression, not facts about silicon, so numeric agreement where it occurs is expected and fine; what matters is that nothing was copied.

    Three approaches were ruled out by measurement, not argument, and are recorded here so they are not retried:

    approach result
    #if FL_HAS_INCLUDE(<SPI.h>) guard in the sketch library not selected — the finder skips conditional includes even when the condition is true
    lib_deps=["SPI"] on the board reaches the generated ini, link errors clear, then package error: library 'SPI' not found in registry — fbuild resolves against the PlatformIO registry and SPI ships inside the framework
    unconditional #include <SPI.h> in FxSdCard.ino links on Teensy, but breaks the host build: 'SPI.h' file not found on the stub platform (tried in #3969, closed)

    Two bugs caught in review that a compile could not

    Both were on the shared-bus path and both would have surfaced as "flaky hardware":

    • Teensy 4 — the LED driver shares LpspiBus::get(0), writes TDR without ever reading RDR, and CFGR1 leaves RX capture on, so a strip write left up to 16 stale bytes queued for SdFat's next transfer(). Fixed by flushing both FIFOs in beginTransaction.
    • Teensy 3.x — pads were muxed once in begin(), but the LED path's disable_pins() returns pins 11/13 to GPIO at the end of every show(). SD worked until the first FastLED.show(), then frames still completed internally while no clock reached the card. Fixed by re-muxing per transaction. A second defect in the same area: CLR_TXF/CLR_RXF were raised alongside MDIS, which gates the clock to the FIFO counters, so the flush may never have run.

    What is NOT verified

    No Teensy hardware was available for any of this work. Everything above is compile- and link-verified only. SD init timing, the 74-clock burst, CMD0 entry, and the clock ramps are reasoned from the reference manuals and unexercised; neither LpspiBus::transfer() nor DspiBus::transfer() has returned a byte from a real card. First hardware bring-up should expect to debug, and the two shared-bus fixes above are the least-tested part.

    Follow-ups filed rather than folded in: #3980 (delete the FL_SDFAT_USE_ARDUINO_SPI hatch, which now selects a path that cannot link, and which is the one remaining SPIClass reference under sdfat/).

    Also worth a separate issue if anyone wants the throughput: fs_sdfat_teensy.h:115 passes SPI_HALF_SPEED (4 MHz), so the DSPI grid resolves to 3.75 MHz on a 60 MHz bus although it can reach 15 MHz. That is a pre-existing config choice, not a defect in the new code.

  2. moved this from Triage to Done in FastLED Trackeron Aug 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

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions