Skip to content

teensy4: drop the Arduino SPI library dependency from SpiHw2/4MXRT1062 (local fork, like PJRC audio + SdFat) — real fix for #3775 #3777

Closed

Description

@zackees

This is the correct fix for #3775. Filing separately because #3775's title/attribution ended up wrong twice and this states the actual work.

The short version

FastLED's Teensy 4.x LPSPI drivers reach for the Arduino SPI library — SPIClass, and the global SPI / SPI1 / SPI2 objects — but only for pin muxing and transaction bookkeeping. The actual data path is already direct LPSPI register access. That one thin dependency is what breaks the teensy41 link, and it should be removed rather than declared.

Why "declare the dependency" is not the answer

Two reasons, and the second is the important one.

1. There is no way to declare it under fbuild. I tested every mechanism:

attempt result
lib_deps = SPI in the generated platformio.ini no effect
lib_ldf_mode = deep no effect (never parsed by fbuild at all)
"dependencies": [{"name":"SPI","platforms":"teensy"}] in library.json no effect
#if 0 + #include <SPI.h> in platforms/arm/teensy/ldf_headers.h no effect
plain unconditional #include <SPI.h> in ldf_headers.h no effect
#include <SPI.h> in the sketch itself links, Flash 129.00 KB

Worth calling out: our src/platforms/*/ldf_headers.h mechanism is inert under fbuild. It is reachable from the sketch (FastLED.h:216) and was built precisely to hint framework deps to a dependency finder, but fbuild will not let a local library's headers select a framework library — that is a deliberate, tested invariant on their side (inactive_local_library_header_cannot_select_framework_library, FastLED/fbuild#1094). Those headers still do their job for PlatformIO; nobody should expect them to work for fbuild.

2. We do not want the dependency even if we could declare it. Pulling a framework library in by default drags its global objects' constructors into the image. SPI, SPI1, SPI2 are exactly that: three global SPIClass instances whose constructors run at startup and whose symbols the linker can then never strip — for every Teensy sketch, including clockless-only ones that touch no SPI at all. This is the same problem that was already solved for the PJRC audio stack and SdFat by forking them locally and wrapping the state in fl::Singleton<T> so the linker can strip it. Adding SPI.h to ldf_headers.h would walk that back.

What the dependency actually consists of

From src/platforms/arm/teensy/teensy4_common/spi_hw_4_mxrt1062.cpp.hpp (and its spi_hw_2_ twin), the entire surface is:

  • :150/154/158 — mSPI = &SPI / &SPI1 / &SPI2 (bus selection)
  • :191/194/200 — setSCK() / setMOSI() / setMISO() (pin mux)
  • :205 — begin()
  • :269/273/314 — beginTransaction(SPISettings(...)) / endTransaction()
  • :367 — end()

Everything else is already register-level and needs no library: getPort() at :118-129 maps the bus id straight to &IMXRT_LPSPI4_S / &IMXRT_LPSPI3_S / &IMXRT_LPSPI1_S, and the transmit loop at :303-309 polls port->SR & LPSPI_SR_TDF / LPSPI_SR_MBF directly.

So we already own the hard part. What is borrowed is pin muxing, clock/transaction setup, and begin/end.

Proposed work

Replace the SPIClass usage with local code, following the audio/pjrc_* and sdfat/ precedent:

  1. Fork the minimal LPSPI bring-up — pad/mux configuration for the SCK/MOSI/MISO pins, LPSPI clock gating and CCM setup, and CR/CFGR1/CCR programming for the transaction settings.
  2. Drive it off the existing IMXRT_LPSPI_t* from getPort(), so bus selection stops needing the global SPI objects.
  3. Keep any driver state in fl::Singleton<T> so an unused SPI backend is fully strippable.
  4. Delete #include <SPI.h> from spi_hw_2_mxrt1062.cpp.hpp:21, spi_hw_4_mxrt1062.cpp.hpp:35, mxrt1062/spi_device_proxy.h:33, and teensy4_common/fastspi_arm_mxrt1062.h:10 / mxrt1062/fastspi_arm_mxrt1062.h:10.

Verification

bash compile teensy41 --examples Apa102 is a 30-second reproduction — it currently fails at link; it must succeed. bash compile teensy41 --examples Blink already succeeds (clockless-only sketches dead-strip the SPI drivers, which is why this looked board-wide but is not) and must stay succeeding, ideally with no flash growth — that is the check that confirms the linker can still strip the backend.

Affected examples once fixed: Apa102, Apa102HD, HD107, HSVTest, FestivalStick, Spi, AutoResearch, and the Audio* set.

This needs on-hardware validation — an APA102/SK9822 strip on a Teensy 4.x, since it is a rewrite of live peripheral bring-up. Correct-looking register code that mis-muxes a pad will link fine and output nothing.

Note on #3775's attribution

#3775 blames the WS2814 commit (#3770). That is wrong and I want it recorded here so nobody re-litigates it: #3770 touches nothing Teensy or SPI. It modified src/, which busted a warm build cache and forced the first cold dependency resolution in a long time. Every firmware in that run shrank by a uniform ~27 KB with byte-identical compiler flags — the SPI library silently leaving the link set. The missing dependency was always there; #3770 only stopped hiding it.

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