Repository navigation
Description
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 overtransfer(u8)first; FIFO-batch later if profiling wants it) - Introduce
SdSpiFlDriverimplementing SdFat's driver contract onLpspiBus, selected through the existingSdSpiDriver.htypedef seam - Default Teensy 4 to it; keep
SdSpiArduinoDriverreachable 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/FxSdCardlinks with no Arduino SPI symbols - No
SPIClassreferences remain undersrc/platforms/arm/teensy/sdfat/outside comments -
bash test --cppstill 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_depsdependency 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
Collapse Sub-issuesSub-issues
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
zackees commentedon Aug 22, 2026
Done. Both sub-issues are closed and the SdFat SPI transport no longer touches Arduino's
SPIClasson any Teensy.LpspiBus)54107f0bDspiBus)3de8d4f8Verified on merged master (
3de8d4f8),Fx/FxSdCard:Plus
bash test --cpp --clean283/283 unit + 83/83 examples andbash lintclean.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 —
LpspiBusgained the two methods SdFat needed, andDspiBusis 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:
#if FL_HAS_INCLUDE(<SPI.h>)guard in the sketchlib_deps=["SPI"]on the boardpackage error: library 'SPI' not found in registry— fbuild resolves against the PlatformIO registry and SPI ships inside the framework#include <SPI.h>inFxSdCard.ino'SPI.h' file not foundon 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":
LpspiBus::get(0), writesTDRwithout ever readingRDR, andCFGR1leaves RX capture on, so a strip write left up to 16 stale bytes queued for SdFat's nexttransfer(). Fixed by flushing both FIFOs inbeginTransaction.begin(), but the LED path'sdisable_pins()returns pins 11/13 to GPIO at the end of everyshow(). SD worked until the firstFastLED.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_RXFwere raised alongsideMDIS, 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()norDspiBus::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_SPIhatch, which now selects a path that cannot link, and which is the one remainingSPIClassreference undersdfat/).Also worth a separate issue if anyone wants the throughput:
fs_sdfat_teensy.h:115passesSPI_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.