Repository navigation
Description
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.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
Naverritus commentedon Mar 3, 2020
I tried setting the -ftemplate-depth flag to something much larger (32768 for example) but it still produces the same issue.
Naverritus commentedon Mar 3, 2020
Issue does not arise in version 3.3.2
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.zackees commentedon Aug 2, 2026
Fixed. Verified by compiling your exact configuration on current master.
Reproduction
I built
SK9822 + BGR + DATA_RATE_KHZ(1)on explicit DATA/CLOCK pins foresp32dev— the same chipset, colour order, data rate and pin style as your sketch:No template-depth error, no recursion.
Why it failed in 2020, and what changed
Your trace is unusually informative:
SPI_SPEED = 240000uandCYCLES = 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 viadelaycycles<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.hdefines it unconditionally:There is no
#if, no fallback branch, and the file does not even includegeneric_software_spi.h. Every ESP32 clocked chipset goes through the hardware SPI peripheral via the GPIO matrix, so any pin pair works anddelaycycles<>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_KHZmeans something different on ESP32 now. It returns Hz, not a clock divider (fastspi.h:82:#define FL_DATA_RATE_KHZ(X) (1000 * (X))). SoDATA_RATE_KHZ(1)is now the value1000, handed to the hardware peripheral as a frequency — not240000handed to a NOP loop. The 240000 in your trace cannot arise on ESP32 any more.3.
delaycycles<>binary-splits instead of decrementing. It is nowdelaycycles<N>() → delaycycles<N/2>() + delaycycles<N-N/2>(), so depth is logarithmic. EvenN = 120000would 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 = 240000uwithCYCLES = 119991is the entire diagnosis in two numbers, and it is why this was still answerable six years later.