Skip to content

ESP32 + SK9822 SPI data rate compile error (template instantiation depth exceeds maximum) #976

Closed

Description

@Naverritus

Using the DOIT Esp32 DevKit v1
FastLED version 3.3.3
Tried compiling using Arduino 1.8.10 as well as PlatformIO 4.2.1

#include <Arduino.h>
#include <FastLED.h>

// Size definitions
#define WIDTH 	44 
#define HEIGHT 	15
#define NUM_LEDS WIDTH * HEIGHT

// PIN definitions
#define CLOCK_PIN 18
#define DATA_PIN  23

CRGB leds[NUM_LEDS];

void setup() {
  // fastled setup
  FastLED.addLeds<SK9822, DATA_PIN, CLOCK_PIN, BGR, DATA_RATE_KHZ(1)>(leds, NUM_LEDS);
  FastLED.setBrightness(255);
  FastLED.setDither(DISABLE_DITHER);
  delay(10);
}

void loop() {
  delay(0);
}

Trying to compile this code leads to the following fatal error:

C:\Users\Robert\Documents\Arduino\libraries\FastLED/fastled_delay.h: In instantiation of 'void delaycycles() [with int CYCLES = 119095]':

C:\Users\Robert\Documents\Arduino\libraries\FastLED/fastled_delay.h:101:31: recursively required from 'void delaycycles() [with int CYCLES = 119990]'

C:\Users\Robert\Documents\Arduino\libraries\FastLED/fastled_delay.h:101:31: required from 'void delaycycles() [with int CYCLES = 119991]'

C:\Users\Robert\Documents\Arduino\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 = 23u; unsigned char CLOCK_PIN = 18u; unsigned int SPI_SPEED = 240000u; uint8_t = unsigned char; AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::clock_ptr_t = volatile unsigned int*; AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::data_ptr_t = volatile unsigned int*; AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::data_t = unsigned int; AVRSoftwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>::clock_t = unsigned int]'

C:\Users\Robert\Documents\Arduino\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 = 128u; D = DATA_NOP; EOrder RGB_ORDER = (EOrder)136u; unsigned char DATA_PIN = 23u; unsigned char CLOCK_PIN = 18u; unsigned int SPI_SPEED = 240000u]'

C:\Users\Robert\Documents\Arduino\libraries\FastLED/chipsets.h:426:3: required from 'void SM16716Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_SPEED>::showPixels(PixelController<RGB_ORDER>&) [with unsigned char DATA_PIN = 23u; unsigned char CLOCK_PIN = 18u; EOrder RGB_ORDER = (EOrder)136u; unsigned int SPI_SPEED = 240000u]'

C:\Users\Robert\Desktop\sketch_feb25a\sketch_feb25a.ino:25:1: required from here

C:\Users\Robert\Documents\Arduino\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();

I assume this is somehow related to setting the SPI_DATA_RATE parameter in the addLeds function. If you change the DATA_RATE_KHZ(1) to the resulting value directly (240000 in my case) it still does not compile. However if I set it to 1000 or omit it entirely it ends up compiling correctly.

Activity

  1. Naverritus commented on Mar 3, 2020

    @Naverritus
    Author

    I tried setting the -ftemplate-depth flag to something much larger (32768 for example) but it still produces the same issue.

  2. Naverritus commented on Mar 3, 2020

    @Naverritus
    Author

    Issue does not arise in version 3.3.2

  3. added
    type: bug
    platform: esp32
    area: ci
    status: stale
    status: triage
    on Apr 16, 2026
  4. 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.

  5. zackees commented on Aug 2, 2026

    @zackees
    Member

    Fixed. Verified by compiling your exact configuration on current master.

    Reproduction

    I built SK9822 + BGR + DATA_RATE_KHZ(1) on explicit DATA/CLOCK pins for esp32dev — the same chipset, colour order, data rate and pin style as your sketch:

    build succeeded in 125.7s (flash: 391455 bytes, ram: 93133 bytes)
    ✅ Compilation succeeded
    

    No template-depth error, no recursion.

    Why it failed in 2020, and what changed

    Your trace is unusually informative: SPI_SPEED = 240000u and CYCLES = 119991. That is (240000 - 6) / 2, i.e. the bit-bang clock-half-period. Note there is no overflow here — 240000 was a genuinely correct clock divider (240 MHz ÷ 1 kHz), faithfully turned into a request for ~120,000 NOP cycles per clock edge. delaycycles<> then recursed one cycle at a time via delaycycles<CYCLES-1>(), so it needed ~120,000 levels of template depth and hit the 900 limit almost immediately.

    Three independent changes since then close it:

    1. ESP32 no longer has a software-SPI path at all. src/platforms/esp/32/drivers/spi/spi_output_template.h defines it unconditionally:

    template<fl::u8 _DATA_PIN, fl::u8 _CLOCK_PIN, fl::u32 _SPI_CLOCK_DIVIDER>
    class SPIOutput : public SPIDeviceProxy<_DATA_PIN, _CLOCK_PIN, _SPI_CLOCK_DIVIDER> {};

    There is no #if, no fallback branch, and the file does not even include generic_software_spi.h. Every ESP32 clocked chipset goes through the hardware SPI peripheral via the GPIO matrix, so any pin pair works and delaycycles<> is never reached. Contrast AVR, which still keeps a real fork (#if defined(AVR_HARDWARE_SPI) … #else GenericSoftwareSPIOutput) for parts like the ATtiny4313.

    2. DATA_RATE_KHZ means something different on ESP32 now. It returns Hz, not a clock divider (fastspi.h:82: #define FL_DATA_RATE_KHZ(X) (1000 * (X))). So DATA_RATE_KHZ(1) is now the value 1000, handed to the hardware peripheral as a frequency — not 240000 handed to a NOP loop. The 240000 in your trace cannot arise on ESP32 any more.

    3. delaycycles<> binary-splits instead of decrementing. It is now delaycycles<N>() → delaycycles<N/2>() + delaycycles<N-N/2>(), so depth is logarithmic. Even N = 120000 would be ~17 levels deep rather than 120,000. The specific "template instantiation depth exceeds maximum" diagnostic you saw can no longer occur on any platform.

    The file also moved: fastspi_bitbang.h → src/platforms/shared/spi_bitbang/generic_software_spi.h.

    Related

    While working through the neighbouring #974 (same diagnostic, different cause — an unsigned underflow rather than a legitimately large divisor) I found and fixed a surviving variant of that one in #3831. Yours needed no code change; it was already closed off from three directions.

    Worth noting the two issues are genuinely distinct despite the identical error text: #974 wrapped to a garbage value, whereas yours computed the right number for an extremely slow bit-banged clock. The 1 kHz request was never unreasonable — the old implementation just had no way to express a long delay other than unrolling it.

    Thanks for including the full instantiation backtrace. SPI_SPEED = 240000u with CYCLES = 119991 is the entire diagnosis in two numbers, and it is why this was still answerable six years later.

  6. 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: ciplatform: esp32status: stalestatus: triagetype: bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions