Repository navigation
Description
So, the code doesn't compile for APA102 leds if I specify CLOCK and DATA pins ( run software SPI).
It throws a compilation error. It does compile when the CLOCK and DATA pins are not specified.
Arduino 1.8.11 and Arduino 1.8.10, Fastled version 3.003.001, Windows 10.
Here's an error log:
`In file included from C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\FastLED/FastLED.h:44:0,
from C:\Users\User\Desktop\Blink\Blink.ino:1:
C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\FastLED/fastled_delay.h: In instantiation of 'void delaycycles() [with int CYCLES = 2147482745]':
C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\FastLED/fastled_delay.h:101:31: recursively required from 'void delaycycles() [with int CYCLES = 2147483640]'
C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\FastLED/fastled_delay.h:101:31: required from 'void delaycycles() [with int CYCLES = 2147483641]'
C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\FastLED/fastspi_bitbang.h:176:52: required from 'static void AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::writeBit(uint8_t, AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::clock_ptr_t, AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::data_ptr_t, AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::data_t, AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::data_t, AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::clock_t, AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::clock_t) [with unsigned char BIT = 0u; unsigned char DATA_PIN = 2u; unsigned char CLOCK_PIN = 3u; long unsigned int SPI_SPEED = 1ul; uint8_t = unsigned char; AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::clock_ptr_t = volatile long unsigned int*; AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::data_ptr_t = volatile long unsigned int*; AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::data_t = long unsigned int; AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::clock_t = long unsigned int]'
C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\FastLED/fastspi_bitbang.h:345:17: required from 'void AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::writePixels(PixelController<RGB_ORDER>) [with unsigned char FLAGS = 0u; D = DATA_NOP; EOrder RGB_ORDER = (EOrder)10u; unsigned char DATA_PIN = 2u; unsigned char CLOCK_PIN = 3u; long unsigned int SPI_SPEED = 1ul]'
C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\FastLED/chipsets.h:138:3: required from 'void WS2801Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_SPEED>::showPixels(PixelController<RGB_ORDER>&) [with unsigned char DATA_PIN = 2u; unsigned char CLOCK_PIN = 3u; EOrder RGB_ORDER = (EOrder)10u; long unsigned int SPI_SPEED = 1ul]'
C:\Users\User\Desktop\Blink\Blink.ino:54:1: required from here
C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\FastLED/fastled_delay.h:101:31: fatal error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum)
FL_NOP; delaycycles();
^
compilation terminated.
`
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
azamat-bagatov commentedon Feb 19, 2020
Issue does not arise with FastLED version 3.002.010
test(spi): pin SPI chipset instantiation across the data-rate range (#…
zackees commentedon Aug 1, 2026
Fixed. Verified on a software-SPI platform rather than inferred from the source.
The cause.
delaycycles<N>used to recurse linearly,N -> N-1, which is exactly the chain in the log above:Two billion instantiations, so any explicit data rate that produced a large cycles-per-bit divider blew the depth limit.
It now splits binary —
delaycycles<N/2>() + delaycycles<N - N/2>()— with the<0>/<1>/<2>base cases present in every translation unit, so depth is logarithmic instead of linear.src/platforms/shared/delay_cycles_generic.h:40-52explains why those base cases have to be per-TU rather than living indelay.cpp.The check. Native tests are not proof here:
FASTLED_STUB_IMPLsits in the hardware-SPI branch offastspi.h, soDATA_RATE_KHZexpands to a frequency and never produces the divider that feddelaycycles. I wrote a native test first and nearly reported it as evidence; it wasn't.So I gave
examples/Apa102theDATA_RATE_KHZ(1)from #976 and compiled for AVR, which is a software-SPI platform:Reverted after.
Why this survived six years. No example in the repo passes a data rate at all —
grep -rn "DATA_RATE_KHZ\|DATA_RATE_MHZ" examples/returns nothing — and the only tests that did used one fast rate. The 83-example compile matrix across every board never instantiated the broken path.#3826 adds SK9822/APA102 coverage from
DATA_RATE_KHZ(1)toDATA_RATE_MHZ(24), with a note recording that it pins the template surface only and cannot reach the software-SPI path. A real guard for that needs a platform compile test, and the natural home for one (examples/CompileTest/) is currently dead — #3816.One adjacent thing I did not fix, worth knowing if you hit slow rates on Teensy 3.x:
delaycyclesis loop-based on AVR but the generic path force-inlines N NOPs via the binary split, so a slow rate there compiles but emits a great many inlined NOPs.fix(spi): clamp bit-bang clock-high delay instead of wrapping (#974) (#…
zackees commentedon Aug 2, 2026
Fixed. Your original case had already been resolved, but chasing it turned up a surviving variant of the same bug, so this took a fix rather than just a close.
Your case: fixed
Two independent changes since 2020 handle it:
SPI_SPEED > 10guard was added toCLOCK_HI_DELAY/CLOCK_LO_DELAY, routing small values away from the underflowing subtraction. Your trace showsSPI_SPEED = 1ul, which now takes the safe branch.delaycycles<>was rewritten to binary-split (N → N/2 + N-N/2) instead of recursing linearly viadelaycycles<CYCLES-1>. Depth is now logarithmic, so the specific "template instantiation depth exceeds maximum of 900" diagnostic you hit can no longer occur at all.The code has also moved —
fastspi_bitbang.his nowsrc/platforms/shared/spi_bitbang/generic_software_spi.h, and Teensy 4.x takes a separatedelayNanoseconds()path entirely.What was still broken
The
> 10guard only covers the low end. A window survives whereSPI_SPEEDclears 10 but(SPI_SPEED-6)/2is still belowMIN_DELAY— andSPI_SPEEDis unsigned, so the subtraction wraps:On a Teensy 3.6 at 180 MHz (
MIN_DELAY == 4), an ordinaryDATA_RATE_MHZ(4)yieldsSPI_SPEED == 12:Measured across the range on that config:
SPI_SPEED11 → 4294967294, 12 → 4294967295, 13 → 4294967295. Everything outside 11–13 was fine.The nastier part: this no longer looks like your error. Because
cycle_tisi64on non-AVR, the wrappedu32converts to a large positive value, so the<=0base-case specialisations never catch it — and with the new binary splitting the compiler stops complaining about depth and instead earnestly tries to emit ~4.29 billion inlinedNOPs. The failure mode became quieter, not safer. Your 2020 report was the loud version of a bug that had gone silent.Fix
#3831 (merged) clamps the subtraction to zero.
MIN_DELAYhas already been emitted at that point and is longer than the half-period being requested, so no further delay is owed — clock-high becomesmax(MIN_DELAY, half_period), which is what the code always intended. It also hoists the twice-repeated(SPI_SPEED-6)/2into a named constant so the high and low delays cannot drift apart.Behavior is unchanged for every
SPI_SPEEDoutside the broken window, verified by evaluating the old and new expressions side by side with the real types across 1/8/11/12/13/16/24/48.Verified:
bash test --cpp(279/279 unit, 83/83 examples),bash compile teensy31 --examples Blink,bash lint.Thanks for the report — pasting the full instantiation backtrace rather than just the last line is what made this diagnosable six years on. The
[with int CYCLES = 2147482745]andSPI_SPEED = 1ulin that trace are the entire diagnosis.