Skip to content

test(spi): pin SPI chipset instantiation across the data-rate range - #3826

Merged
zackees merged 1 commit into
masterfrom
test/974-976-spi-data-rate
Aug 1, 2026
Merged

zackees merged 1 commit into
masterfrom
test/974-976-spi-data-rate

Conversation

@zackees

@zackees zackees commented Aug 1, 2026 •

Copy link
Copy Markdown
Member

Addresses #974 and #976 — both template instantiation depth exceeds maximum when a software-SPI chipset is given an explicit data rate, on Teensy and ESP32 respectively.

Both are fixed

delaycycles<N> used to recurse linearly (N -> N-1), which is exactly the chain in #974's log:

void delaycycles() [with int CYCLES = 2147482745]
  recursively required from 'void delaycycles() [with int CYCLES = 2147483640]'

It now splits binary — delaycycles<N/2>() + delaycycles<N - N/2>() — with <0>/<1>/<2> base cases present in every translation unit, so depth is logarithmic. src/platforms/shared/delay_cycles_generic.h:40-52 carries a good comment on why those base cases must be per-TU.

Verified on a software-SPI platform, not assumed. I temporarily gave examples/Apa102 the DATA_RATE_KHZ(1) from the reports and ran bash compile uno --examples Apa102 — succeeds in 31s. Reverted after.

Why they lasted six years

Not a hard bug — a coverage hole:

  • No example anywhere passes a data rate at all. grep -rn "DATA_RATE_KHZ\|DATA_RATE_MHZ" examples/ returns nothing.
  • The only tests that did used a single fast rate (DATA_RATE_MHZ(12)).

So the 83-example compile matrix, across every board, never once instantiated the path that broke.

What this adds

SK9822 and APA102 across DATA_RATE_KHZ(1) — the exact rate from #976 — through DATA_RATE_MHZ(24).

Scoped deliberately, and the test says so

This is the part I want reviewed rather than skimmed. FASTLED_STUB_IMPL is in the hardware-SPI branch of fastspi.h:78:

#elif defined(FASTLED_TEENSY4) || defined(ESP32) || ... || defined(FASTLED_STUB_IMPL)
#define FL_DATA_RATE_KHZ(X) (1000 * (X))

so natively DATA_RATE_KHZ expands to a frequency, not to the cycles-per-bit divider that fed delaycycles. These cases pin the template surface — that every rate, including the pathological ones, instantiates — but they cannot reach the software-SPI path that actually broke.

I caught that after writing a native test and nearly reporting it as proof. It isn't. The genuine guard needs a platform compile, and the natural home for one (examples/CompileTest/) is itself dead — see #3816. The test comment states this so nobody later reads it as coverage it doesn't provide.

Related finding, not fixed here

delaycycles is loop-based on AVR (_delaycycles_avr<CYCLES/3, CYCLES%3>) but the generic path force-inlines N NOPs via the binary split. On a non-AVR software-SPI platform a slow rate therefore emits tens of thousands of inlined NOPs — it compiles, but the code-size cost is large and silent. That's Teensy 3.x, exactly #974's board. Worth its own look; I haven't measured it on hardware-class flash so I'm not asserting a number.

check result
bash test --cpp 277/277 unit, 83/83 examples
bash lint no blocking violations
bash compile uno --examples Apa102 with DATA_RATE_KHZ(1) succeeds

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Tests
    • Added compile-time coverage for SPI chipset configurations across data rates from 1 kHz to 24 MHz.
    • Included rates that help validate compatibility with deep template instantiations.

Addresses #974 and #976, both "template instantiation depth exceeds
maximum" when a software-SPI chipset is given an explicit data rate.

Both are fixed. delaycycles<N> used to recurse linearly (N -> N-1), which
is what produced the reported chain from CYCLES=2147483640 downwards. It
now splits binary with <0>/<1>/<2> base cases present in every translation
unit, so depth is logarithmic.

Verified on a software-SPI platform rather than assumed: temporarily giving
examples/Apa102 the DATA_RATE_KHZ(1) from the reports and running
`bash compile uno --examples Apa102` succeeds. Reverted after.

The reason these lasted six years is a coverage hole, not a hard bug. No
example anywhere passes a data rate at all, and the only tests that did
used a single fast rate. So the 83-example compile matrix never once
instantiated the path that broke.

This closes the natively-closable part of that hole: SK9822 and APA102
across DATA_RATE_KHZ(1) -- the exact rate from #976 -- through
DATA_RATE_MHZ(24).

Deliberately scoped, and the test says so rather than implying more:
FASTLED_STUB_IMPL sits in the *hardware* SPI branch of fastspi.h, so
DATA_RATE_KHZ expands to a frequency here, not to the cycles-per-bit
divider that fed delaycycles. These cases pin the template surface; they
cannot reach the software-SPI path. That one needs a platform compile, and
the natural home for such a test (examples/CompileTest/) is itself dead --
see #3816.

bash test --cpp: 277/277 unit, 83/83 examples. bash lint: clean.

Co-Authored-By: Claude <[email protected]>
@zackees
zackees merged commit d4eb976 into master Aug 1, 2026
4 checks passed
@coderabbitai

coderabbitai Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: fd544a8a-c838-4970-b42a-50634d0bd94c

📥 Commits

Reviewing files that changed from the base of the PR and between af434d5 and 8138b57.

📒 Files selected for processing (1)
  • tests/fl/channels/bus_traits.cpp

📝 Walkthrough

Walkthrough

The pull request adds compile-time tests for SK9822 and APA102 SPI configurations at four data rates from 1 kHz to 24 MHz.

Changes

SPI instantiation coverage

Layer / File(s) Summary
Compile-time data-rate coverage
tests/fl/channels/bus_traits.cpp
The test instantiates SK9822 and APA102 configurations at 1 kHz, 500 kHz, 1 MHz, and 24 MHz. It covers software-SPI template-instantiation-depth cases and documents hardware-SPI coverage.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • FastLED/FastLED#3668: Introduced the BusTraits and SPI bus support validated by this test.
  • FastLED/FastLED#3705: Also modified compile-time SPI coverage in bus_traits.cpp, but tested public SK9822 API calls.
  • FastLED/FastLED#3709: Introduced FLEX_IO SPI support covered by these chipset instantiation tests.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/974-976-spi-data-rate

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

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant