Skip to content

Gradient palettes get stored in RAM on ESP8266 #743

Closed

Description

@Aircoookie

Hello!
I've noticed that if I use the following syntax to define a palette:

DEFINE_GRADIENT_PALETTE( heatmap_gp ) {
  0,     0,  0,  0,   //black
128,   255,  0,  0,   //red
224,   255,255,  0,   //bright yellow
255,   255,255,255 }; //full white

the palette gets stored in RAM, not PROGMEM, even though the docs say so.

In platforms/esp/8266/led_sysdefs_esp8266.h I found this definition:

// Default to NOT using PROGMEM here
#ifndef FASTLED_USE_PROGMEM
# define FASTLED_USE_PROGMEM 0
#endif

I have some ~2kB worth of palette definitions in my application and would like to free up some memory.
Can someone explain the issue of enabling PROGMEM on ESP8266?

Activity

  1. kriegsman commented on Feb 22, 2019

    @kriegsman
    Member

    Thank you for pointing this out; I think it’s probably just an oversight and something we can change easily. I’ll take a look later today if I can.

  2. Aircoookie commented on Feb 22, 2019

    @Aircoookie
    Author

    Thanks for your quick response! I just tried # define FASTLED_USE_PROGMEM 1 in my sketch.
    Unfortunately, it seems like the default of disabling it for esp8266 was in fact intentional.
    It compiles just fine and reduces the memory usage as expected, but as soon as a palette stored in PROGMEN is selected, the application crashes (using latest 2.5.0 esp8266 core and FastLED 2.3.6):

    Exception 9: LoadStoreAlignmentCause: Load or store to an unaligned address
    Decoding 10 results
    0x402080b6: CRGBPalette16::operator=(unsigned char const*) at C:\Users\cschwinne\Documents\Arduino\libraries\FastLED/colorutils.h line 836
    0x40269a7d: lwip_init at ?? line ?
    0x4020966a: WS2812FX::handle_palette() at C:\Users\CSCHWI~1\AppData\Local\Temp\arduino_build_5212\sketch/WS2812FX_fcn.cpp line 699
    0x40100370: millis at C:\Users\cschwinne\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266/core_esp8266_wiring.c line 183
    0x40100370: millis at C:\Users\cschwinne\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266/core_esp8266_wiring.c line 183
    0x40100370: millis at C:\Users\cschwinne\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266/core_esp8266_wiring.c line 183
    0x402096e3: WS2812FX::service() at C:\Users\CSCHWI~1\AppData\Local\Temp\arduino_build_5212\sketch/WS2812FX_fcn.cpp line 66
    0x40219c26: loop at C:\Users\cschwinne\git\Arduino\WLED02\wled00/wled00.ino line 560
    0x4022f524: loop_wrapper() at C:\Users\cschwinne\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266/core_esp8266_main.cpp line 125
    0x40100ea5: cont_wrapper at C:\Users\cschwinne\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266/cont.S line 81
    

    Maybe I can try it again later with a simple palette example sketch to verify it's not an issue with my rather complex application.

    Edit: With just pasting the gradient palette definition in the example ColorPalette.ino and referencing it in one of the if statements in ChangePalettePeriodically() , the issue doesn't occur. So it's likely there is some interference with EEPROM, WiFi or other libs...

  3. Aircoookie commented on Feb 22, 2019

    @Aircoookie
    Author

    Alright, didn't know why ColorPalette.ino worked just then. Here is a minimal example where the issue occurs, without using WiFi libs or anything.
    PaletteProgmemTest.zip

    By the way, I don't need a solution quickly, so take your time, just making you aware of it :)

  4. kriegsman commented on Feb 22, 2019

    @kriegsman
    Member

    ESP microcontrollers requires 4-byte-aligned reads from PROGMEM; AVR do not. I'm pretty sure that when we first ported to ESP, I said "oh well, I'll just stick it in RAM for now, and come back to deal with the specifics of ESP's needs about PROGMEM later." Now, it appears, is 'later'.

  5. self-assigned this
    on Feb 22, 2019
  6. added
    platform: esp8266
    area: core
    status: stale
    status: triage
    on Apr 16, 2026
  7. zackees commented on Aug 1, 2026

    @zackees
    Member

    Settled, with evidence — but the premise turns out to be inverted.

    The palettes are already in flash. objdump on a freshly linked ESP8266 image (built today from master):

    40239f70 g  O .irom0.text  00000040 PartyColors_p
    4023a030 g  O .irom0.text  00000040 CloudColors_p
    

    .irom0.text is flash. 0x40 = 64 bytes = 16 × u32, the correct size.

    Why the setting says otherwise: it is never read on ESP8266. src/fastled_progmem.h:8 dispatches on defined(ESP8266) before it ever tests FASTLED_USE_PROGMEM:

    #if defined(__EMSCRIPTEN__) || defined(FASTLED_TESTING) || defined(FASTLED_STUB_IMPL)
    #include "platforms/null_progmem.h"
    #elif defined(ESP8266)
    #include "platforms/esp/8266/progmem_esp8266.h"

    progmem_esp8266.h then maps FL_PROGMEM → PROGMEM and FL_PGM_READ_* → the real pgm_read_* accessors unconditionally — there is not one #if FASTLED_USE_PROGMEM in the file. The #if (FASTLED_USE_PROGMEM == 1) block lives in the #else arm and is never compiled here. So the value moves nothing, in either direction.

    You read # define FASTLED_USE_PROGMEM 0 and concluded the tables were in RAM. That was the only reasonable reading; the define is simply wrong. It has said 0 since the 2013 initial ESP8266 port (55690d6832) with no recorded rationale — every later touch was whitespace.

    Your 2019 crash was real and has since been fixed, separately. Exception 9: LoadStoreAlignmentCause in CRGBPalette16::operator= came from pgm_read_dword — which, unlike pgm_read_byte/pgm_read_word, has no unaligned-access helper on ESP8266. DEFINE_GRADIENT_PALETTE then emitted a bare const uint8_t[] the linker could place at any byte offset. Today it emits FL_ALIGN_PROGMEM(4) (src/colorutils.h:123), which on ESP8266 is aligned(4), and the union it is read through is 4 bytes wide, so every stride stays aligned.

    So: nothing to reclaim — the ~2 kB you wanted freed was never in RAM to begin with.

    What I did not change. Correcting the define to 1 requires editing led_sysdefs_esp8266.h, which sits behind this repo's protect_platform_headers guardrail. The change would be behaviorally inert, but overriding a deliberate guardrail for a documentation-grade fix wants a maintainer's call rather than mine. Flagging it here so the decision is visible: the define is misleading and worth correcting, and doing so cannot alter generated code.

    Incidental: ESP8266 did not compile at all while I was checking this (fatal error: SPI.h). Fixed in #3815.

  8. zackees commented on Aug 1, 2026

    @zackees
    Member

    Researched this properly. The premise is no longer true — gradient palettes are already in flash on ESP8266. But the reason is not that anyone fixed FASTLED_USE_PROGMEM; it's that the flag stopped being consulted on this platform, and the comment you found is now actively misleading. Details, with evidence.

    1. Measured: palettes are in flash today

    Built examples/Fire2023 (which uses a real DEFINE_GRADIENT_PALETTE(hot_gp)) and examples/ColorPalette for esp8266, then read the symbols out of the ELF:

    40239ef0  00000040  T  myRedWhiteBluePalette_p     (ColorPalette, TProgmemPalette16)
    4023ac68  00000018  T  hot_gp                      (Fire2023, DEFINE_GRADIENT_PALETTE)
    

    Section map from the same binary:

    section VMA region
    .data 0x3ffe8000 DRAM
    .rodata 0x3ffe8620 DRAM — 940 bytes total
    .bss 0x3ffe89d0 DRAM
    .irom0.text 0x40201010, 242 KB flash (XIP)

    0x402xxxxx is the flash-mapped region. Both palettes are in .irom0.text. Note also that RAM-resident .rodata is only 940 bytes in the whole image — essentially nothing const is in RAM.

    So your ~2 KB of palette definitions are not costing you RAM on a current FastLED.

    2. Why — the flag is bypassed on ESP8266

    src/fastled_progmem.h:6-12 dispatches on platform, before the flag is ever tested:

    #if defined(__EMSCRIPTEN__) || defined(FASTLED_TESTING) || defined(FASTLED_STUB_IMPL)
    #include "platforms/null_progmem.h"
    #elif defined(ESP8266)
    #include "platforms/esp/8266/progmem_esp8266.h"   // <-- ESP8266 exits here
    #elif defined(FL_IS_STM32)
    ...
    #else
        ...
        #if (FASTLED_USE_PROGMEM == 1)                // <-- line 45, never reached on ESP8266

    progmem_esp8266.h:13-17 then defines FL_PROGMEM as real PROGMEM and FL_PGM_READ_* as real pgm_read_*. So ESP8266 gets genuine flash residency regardless of FASTLED_USE_PROGMEM being 0.

    That dispatch was added in e8ae4f8aeb / ba65d939d8 (Aug 2025), six years after you filed this.

    3. So what's actually still wrong

    The comment at src/platforms/esp/8266/led_sysdefs_esp8266.h:22-25:

    // Default to NOT using PROGMEM here
    #ifndef FASTLED_USE_PROGMEM
    # define FASTLED_USE_PROGMEM 0
    #endif

    is stale. It documents behavior the platform no longer has, and it's what sent you (reasonably) down this path. That's the remaining defect here — a documentation bug, not a memory one.

    There's a second wrinkle: setting the flag to 1 is now a hard compile error, which directly answers your original question ("Can someone explain the issue of enabling PROGMEM on ESP8266?"). I tried it:

    $ bash compile esp8266 --examples ColorPalette --defines FASTLED_USE_PROGMEM=1
    platforms/esp/compile_test.hpp:13:2: error: #error "FASTLED_USE_PROGMEM should be 0 for ESP8266"
    

    So the library now blocks a flag that does nothing. ESP32 carries the identical assert (compile_test.hpp:59-60).

    4. Your 2019 crash

    LoadStoreAlignmentCause inside CRGBPalette16::operator= was an alignment fault: gradient palettes are read with FL_PGM_READ_DWORD_NEAR (32-bit loads), and unaligned flash dword reads fault on Xtensa.

    That's handled now. DEFINE_GRADIENT_PALETTE emits FL_ALIGN_PROGMEM(4) before the array (src/colorutils.h:122-124), and ESP8266 caps FL_ALIGN_PROGMEM at 4 bytes (progmem_esp8266.h:19-23) because the platform lacks memalign.

    Your instinct that it was environment-specific was also right — you noted it reproduced in your full app but not in a trimmed ColorPalette.ino. Alignment bugs behave exactly like that, since whether a given array lands aligned depends on what the linker placed before it.

    5. The ISR hazard people usually assume — checked, doesn't apply

    The obvious reason to keep constants out of flash on ESP8266 is that flash reads fault when the cache is disabled. I checked whether FastLED's output path is exposed:

    • clockless_esp8266.h:109 and clockless_block_esp8266.h:132 are both FL_IRAM showRGBInternal, and the os_intr_lock() regions (:122-125, :142) touch only loadAndScale* / stepDithering / FastPin register writes.
    • Those primitives are pure RAM arithmetic — pixel_controller.h:418 reads the RAM pixel buffer, :431 is qadd8, :445 is scale8. pixel_controller.h contains zero FL_PGM_* uses.
    • Gamma/five_bit_hd_gamma LUT reads exist but are on the SPI chipset paths (APA102/HD108), outside any interrupt lock. Gamma8Impl even copies its LUT into RAM at construction (ease.cpp.hpp:411-415).

    So no PROGMEM read happens during clockless output. The hazard isn't why the flag was 0.

    6. Why it was 0 in the first place

    No commit ever explained it. It arrives fully formed in 55690d6832 (2016-01-23, Daniel Garcia, "ESP8266 first light") — the file is created already containing that exact comment and value. Most plausibly the 2016-era core's .irom/pgm_read_* handling was unreliable and the porter simply left constants in RAM. Whatever the reason, it was superseded in 2025.

    7. ESP8266 isn't unusual

    Only AVR and the Teensy family default to 1. ESP32, RP2040, Apollo3, SAMD21/51, SAM, nRF51/52, MGM240, LPC, WASM and stub all default to 0 — every memory-mapped-flash platform does. What's unusual about ESP8266 is only that its actual behavior now contradicts its own flag.


    Recommendation

    Leaving this open, retitled in spirit as a docs/cleanup item rather than a memory bug. The tractable work is:

    1. Replace the misleading comment at led_sysdefs_esp8266.h:22 with a note that ESP8266 uses PROGMEM unconditionally via the platform dispatch in fastled_progmem.h:8-9, and that the flag is vestigial there.
    2. Decide what compile_test.hpp:12-14 should assert now that the flag it guards is inert.
    3. src/platforms/esp/8266/README.md:19,23 needs the same correction. (src/platforms/readme.md:365-366 already says "ESP8266: PROGMEM supported", which is the accurate statement.)

    @Aircoookie — six years late, but: your reading of the code was correct at the time, the crash you hit was a real alignment bug, and the behavior you wanted is what ships today.

    Unrelated find worth its own issue: the five console fonts (src/fl/font/console_font_*.h:6-8, ~12 KB) are declared FL_PROGMEM but have no FL_PGM_* reader and no consumer anywhere. On ESP8266 — where FL_PROGMEM is genuine flash — dereferencing them directly would fault.

  9. moved this from Triage to Done in FastLED Trackeron Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: coreplatform: esp8266status: stalestatus: triage

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions