Skip to content

Teensy + APA102 software SPI problem (fatal error: template instantiation depth exceeds maximum) #974

Closed

Description

@azamat-bagatov

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.
`

Activity

  1. azamat-bagatov commented on Feb 19, 2020

    @azamat-bagatov
    Author

    Issue does not arise with FastLED version 3.002.010

  2. added
    type: bug
    platform: teensy
    area: drivers
    status: stale
    status: triage
    on Apr 16, 2026
  3. zackees commented on Aug 1, 2026

    @zackees
    Member

    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:

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

    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-52 explains why those base cases have to be per-TU rather than living in delay.cpp.

    The check. Native tests are not proof here: FASTLED_STUB_IMPL sits in the hardware-SPI branch of fastspi.h, so DATA_RATE_KHZ expands to a frequency and never produces the divider that fed delaycycles. I wrote a native test first and nearly reported it as evidence; it wasn't.

    So I gave examples/Apa102 the DATA_RATE_KHZ(1) from #976 and compiled for AVR, which is a software-SPI platform:

    bash compile uno --examples Apa102
    All compilations completed successfully in 00m:31s
    

    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) to DATA_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: delaycycles is 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.

  4. zackees commented on Aug 2, 2026

    @zackees
    Member

    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:

    1. A SPI_SPEED > 10 guard was added to CLOCK_HI_DELAY/CLOCK_LO_DELAY, routing small values away from the underflowing subtraction. Your trace shows SPI_SPEED = 1ul, which now takes the safe branch.
    2. delaycycles<> was rewritten to binary-split (N → N/2 + N-N/2) instead of recursing linearly via delaycycles<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.h is now src/platforms/shared/spi_bitbang/generic_software_spi.h, and Teensy 4.x takes a separate delayNanoseconds() path entirely.

    What was still broken

    The > 10 guard only covers the low end. A window survives where SPI_SPEED clears 10 but (SPI_SPEED-6)/2 is still below MIN_DELAY — and SPI_SPEED is unsigned, so the subtraction wraps:

    On a Teensy 3.6 at 180 MHz (MIN_DELAY == 4), an ordinary DATA_RATE_MHZ(4) yields SPI_SPEED == 12:

    (12 - 6) / 2 - 4  →  -1  →  4294967295 cycles
    

    Measured across the range on that config: SPI_SPEED 11 → 4294967294, 12 → 4294967295, 13 → 4294967295. Everything outside 11–13 was fine.

    The nastier part: this no longer looks like your error. Because cycle_t is i64 on non-AVR, the wrapped u32 converts to a large positive value, so the <=0 base-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 inlined NOPs. 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_DELAY has already been emitted at that point and is longer than the half-period being requested, so no further delay is owed — clock-high becomes max(MIN_DELAY, half_period), which is what the code always intended. It also hoists the twice-repeated (SPI_SPEED-6)/2 into a named constant so the high and low delays cannot drift apart.

    Behavior is unchanged for every SPI_SPEED outside 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] and SPI_SPEED = 1ul in that trace are the entire diagnosis.

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

    area: driversplatform: teensystatus: stalestatus: triagetype: bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions