Skip to content

ESP32: FastLED.show hangs when core 1 is under load #1438

Closed

Description

@Ezekiel-DA

Hi all,

I am working on a project that uses FastLED to drive a significant number of WS2812B LEDs while performing quite a bit of other work, namely audio and BLE.

For a little bit of context (and, I will admit, a shameless attempt at trying to get more help ASAP before Halloween!), the actual "finished" project is a costume for a kiddo in a wheelchair, as part of a charity project I and several coworkers donate time to via the awesome non profit Magic Wheelchair.

You can see some shots of the costume in action and the eye-searing glory of close to 1500 LEDs, all on battery power, at an early Halloween parade in Salem, MA, in this news report.

Now for the issue I am attempting to figure out: as the code stands right now, when mp3 playback starts, FastLED essentially "crashes": FastLED.show() no longer returns, which blocks the main task (using the Arduino framework in Platform.io's ESP32 Platform support).

If I move the call to FastLED.show() to a separate task, that task stops running, while the main loop() task continues to run, predictably.

Hunting down a minimal reproduction for this has been a little tricky:

  • without mp3 playback, FastLED behaves normally. I attempted to simulate load on Core 1 with something simpler than mp3 playback but was unsuccessful
  • with fewer than 3 FastLED controllers (only 1 or 2 calls to addLeds()), the problem also stops occurring
  • the number of LEDs added also seems to play a role

This is the most minimal reproduction I was able to create: https://github.com/Ezekiel-DA/FastLED_audio_hang_repro

It still relies on bringing in a whole external I2S / mp3 decoding library, but it is otherwise fairly simple, does not require any of the external hardware that the actual costume uses, etc. You do not need to connect any LEDs or connect anything that would allow actual sound output to see the problem occur.

To reproduce:

  • clone this repo and open the project with Platform.io
  • run the Upload Filesystem Image command in Platform.io to upload the sample mp3 file to flash
  • upload the actual code
  • observe that, once music playback starts (automatically after a set number of loops through loop()), looping through the task that runs FastLED.show() stops

Thanks in advance for any help!

Activity

  1. Ezekiel-DA commented on Oct 10, 2022

    @Ezekiel-DA
    Author

    More information:

    I found a "fix" / workaround: increasing the priority of the task dedicated to calling FastLED.show() to above the priority of the mp3 decoding task fixes the issue.

    But: I still believe there is a hang bug in FastLED. My expertise as a software engineer is all desktop / web and very little embedded, so apologies if I am incorrect, but here is my logic:

    • loop(), the Arduino style main task, does not hang, despite being priority 1, i.e. lower than the mp3 decoding and lower than I had the FastLED task set to in the repro code
    • Adding simple serial print debugging to the repro code that traces entry and return from FastLED, I get this:
    before FastLED show
    Loop #100001
    after FastLED show
    before FastLED show
    after FastLED show
    <snip: a whole bunch more before / after, obviously>
    FastLED Task Loop #101   // <== emitted by my FastLED.show() task
    before FastLED show
    after FastLED show
    before FastLED show
    after FastLED show
    <... more of the same predictable output>
    before FastLED show
    after FastLED show
    Music starts; FastLED.show() task will stop responding.
    Loop #300001  // <== emitted by loop()
    before FastLED show
    after FastLED show
    before FastLED show  // <== note that we will never hit the after trace!
    Loop #350001 // main loop, at priority 1, is still getting ticks, while the show() loop at priority 3 isn't 
    Loop #400001
    Loop #450001
    Loop #500001
    <... this, endlessly, even when mp3 decoding ends>
    
  2. zackees commented on Oct 10, 2022

    @zackees
    Member

    try

    #define NO_HARDWARE_PIN_SUPPORT
    #include <FastLED.h>
  3. Ezekiel-DA commented on Oct 11, 2022

    @Ezekiel-DA
    Author
    #define NO_HARDWARE_PIN_SUPPORT

    Thanks for the suggestion! Unfortunately this makes no difference in either the full firmware or the minimal reproduction I posted. As far as I can tell from a quick look through the FastLED, this flag does not appear to be in use for the ESP32 platform? I can only find references to it for AVR, ARM, etc.

  4. Ezekiel-DA commented on Oct 12, 2022

    @Ezekiel-DA
    Author

    Update: crossposted this to the reddit community here for more input, and found what appears to be a better workaround:

    #define FASTLED_RMT_MAX_CHANNELS 1
    

    This obviously kills most of the advantage of using the RMT driver (as far as I understand), but it does seem to fully solve my issue right now.

    If anyone with expertise with the ESP32's RMT APIs wants to take a crack at this, I am more than willing to try to provide more information or any support in investigating.

  5. PureTek-Innovations commented on Oct 12, 2022

    @PureTek-Innovations

    I've had a similar issue with the RMT driver while streaming led data to an ESP32 using this library to receive the data and output it to 9 led strips: https://github.com/hpwit/artnetESP32

    I did manage to get an error message from the ESP32:

    14:43:21.208 -> Guru Meditation Error: Core 0 panic'ed (Interrupt wdt timeout on CPU0).
    14:43:21.208 ->
    14:43:21.208 -> Core 0 register dump:
    14:43:21.208 -> PC : 0x400814de PS : 0x00050035 A0 : 0x40084da4 A1 : 0x3ffbea9c
    14:43:21.208 -> A2 : 0x00000007 A3 : 0x3ff56000 A4 : 0x00000001 A5 : 0x3ffca680
    14:43:21.208 -> A6 : 0x0000001e A7 : 0x00000000 A8 : 0x0000000c A9 : 0x00000012
    14:43:21.208 -> A10 : 0x3ffc2e40 A11 : 0x00000000 A12 : 0x800813b2 A13 : 0x3ffde780
    14:43:21.208 -> A14 : 0x3ffc2e40 A15 : 0x55000001 SAR : 0x0000001e EXCCAUSE: 0x00000005
    14:43:21.208 -> EXCVADDR: 0x00000000 LBEG : 0x40084739 LEND : 0x40084741 LCOUNT : 0x00000027
    14:43:21.208 -> Core 0 was running in ISR context:
    14:43:21.208 -> EPC1 : 0x400df7a7 EPC2 : 0x00000000 EPC3 : 0x400813d8 EPC4 : 0x00000000
    14:43:21.208 ->
    14:43:21.208 ->
    14:43:21.208 -> Backtrace:0x400814db:0x3ffbea9c |<-CORRUPTED
    14:43:21.208 ->
    14:43:21.208 ->
    14:43:21.208 ->
    14:43:21.208 ->
    14:43:21.208 -> ELF file SHA256: 0000000000000000

    Which decodes to this:

    PC: 0x400814de
    EXCVADDR: 0x00000000

    Decoding stack results
    0x400814db: ESP32RMTController::interruptHandler(void*) at C:\Users\Jem\Documents\Arduino\libraries\FastLED\src\platforms\esp\32\clockless_rmt_esp32.cpp line 44

    I can't be sure that it's related, but it might help.

  6. zackees commented on Oct 13, 2022

    @zackees
    Member

    A debugging suggestion is to look at FastLED.addLeds<WS2812B, 12, GRB>(strip1Leds, 3) and see that the template argument is being used to select a particular CPixelController class for WS2812, which is then used to call another function with the constructed pixel controller.

    You could create a dummy class for the pixel controller that does nothing, and then pass that dummy class into FastLED.addLeds().

    If the program crashes, then it's something in the framekwork of FastLED. If it does not crash with the dummy CPixelController, then it's something inside the pixel controller. It's probably something to do with the pixel controller in my opinion and in this scenario, you could then copy the real CPixelController class and then start commenting functions out until it does not crash, then find the exact function / line where the issue is occuring.

  7. samguyer commented on Nov 25, 2022

    @samguyer
    Contributor

    @Ezekiel-DA Apologies for getting to this issue so far past your deadline! How did it go in the end? Is there more to do here?

    In general, I suspect that when the CPU is overloaded, some of the interrupts drop on the floor. As a result, what probably happens is that the semaphore that signals show() is done never gets triggered. What I should probably do is set a reasonable timeout.

  8. TriviumGames commented on Nov 27, 2022

    @TriviumGames

    I believe I have been seeing this ... for many months. I'm using this in an escape room with Olimex ESP32-POE's.

    I have 15 LEDs in 3 strips (i.e., 3 calls to addLeds)

    My loop() I think is pretty normal:

    • Poll some sensors as fast as possible, send a UDP packet if stuff changes.
    • Poll a UDP socket for some relatively infrequent network messages to update some CRGB values
    • If it's been more than 30ms since the last time I did it, call FastLED.show()
    • I guess I'm also polling for OTA updates.

    Notably I am not doing anything funny with scheduling my own tasks. Because I'm polling as fast as I can I guess I would say the CPU is fully busy? IDK. It does seem to be the case that the network stuff uses some tasks to run things on other cores. At least, after loop() hangs, the device still responds to ICMP ping requests.

    Things I have noticed:

    • With no application code changes, this went from never happening, to rarely happening (~ once a week) to happening much more often (~once every couple hours). I believe it is toolchain "upgrades" that affected this frequency.
    • With the latest everything, this is happening an average of once every 2.5 hours, which was enough to make me dig in and add enough logging to even narrow this down to FastLED. A little under a quarter of the time it gets stuck, the ESP32 watchdog timer notices and reboots the device for me.

    I will try some of the workarounds and suggested experiments here on Monday (weekends are our busy time), and even if I find something that works around this, I'm happy to do anything I can to help find a proper fix for this.

  9. Ezekiel-DA commented on Nov 29, 2022

    @Ezekiel-DA
    Author

    @samguyer no worries!

    As we had discussed in the reddit thread and here, I found a workaround for this particular project by setting #define FASTLED_RMT_MAX_CHANNELS 1, which seems to "fix" the problem (or at least make it so infrequent that I haven't seen it again).

    That being said, I'm sure we'll do another Magic Wheelchair build at some point next year, and I'd like to stay with this ESP32 + FastLED combo. Knowing my teammates, they'll want even more hardware and software features, so I would definitely be interested in a real fix that allows multiple RMT channels to be used, for better performance with many channels.

    Is there anything I can do to help diagnose this?

  10. TriviumGames commented on Nov 30, 2022

    @TriviumGames

    Well I switched it over to use i2s, and so far it seems to be working 100% (I am assuming the several hour multi-block power outage this afternoon was not related to this change 😺 ). I'd be happy to help troubleshoot RMT stuff, though. I'll see if I can get this reproducing reliably on a spare ESP32 that's not part of anything customer-facing.

  11. samguyer commented on Nov 30, 2022

    @samguyer
    Contributor
  12. TriviumGames commented on Nov 30, 2022

    @TriviumGames

    Great. I'll try to pare my code down to make a test case that doesn't require it to be plugged in to an escape room to do anything.

  13. dotorg commented on Nov 30, 2022

    @dotorg

    Just to add another raised hand on this issue -- after quite a bit of time trying to diagnose a very infrequent hang on two of my devices, I finally tracked it down to FastLED.show() not returning. Sometimes after ten minutes, sometimes after a few days of calling it every 50ms. As a datapoint that may, or may not, be useful -- this is on an ESP32S2, not a full ESP32. (Primarily, the difference is being single core, but software-based USB stack on them also means generally never getting any WDT output, etc)

    So I don't have anything specific to help, but if it helps narrow it down, it seems to be happening on the S2 as well.

  14. samguyer commented on Nov 30, 2022

    @samguyer
    Contributor

    OK, that's useful information. I just ordered myself a few of these newer ESP32 variations, so I can try things out myself. I suspect that the code is getting stuck at the semaphore "take" at the end, which is waiting for the interrupt handler to do a "give" telling it that all the data is sent. If that doesn't happen for some reason, the "take" will just sit there. BUT I think I can set a timeout, so maybe that's a stopgap until I figure out why it's failing.

  15. Ezekiel-DA commented on Dec 1, 2022

    @Ezekiel-DA
    Author

    Just in case this got missed and might be helpful: there is a minimal-ish reproduction case linked in the original issue above.

    It's not quite as minimal as I'd like because I couldn't generate the right load on Core 1 without the MP3 decoder, but it should compile, run and reproduce the issue with no external code or additional hardware needed.

    For me, this test cases consistently reproduces the hang in FastLED.show() on a standard ESP32 dev board.

  16. 44 remaining items

  17. davidlmorris commented on Aug 1, 2024

    @davidlmorris
    Contributor

    Funnily enough, I was thinking along those lines from a hint you gave above. (Memory issues. However, my original thoughts were it was it wasn't correctly finding the end of the buffer because I had multiple controllers not using contiguous memory... but buffer guards didn't stop the crash).

    So, there are 4 strands of LEDs, 2 x 256 matrix, and 2 x strands of 'sticks' each totaling 470 for a total of 1452 pixels. Moreover, my code is doing a lot... DMX lights and '6 x Analogue' strands (with a sort of PWM), displaying around 30 different patterns, plus SPI receiving audio data, and a button, rotary encoder and (non-FastLED) SPI LED output. (And still getting 1000 loops a second.)

    This says I still have some capacity!

    RAM:   [=         ]  11.2% (used 36840 bytes from 327680 bytes)
    Flash: [===       ]  31.4% (used 411557 bytes from 1310720 bytes)
    

    If I get a chance today I'll put an assert after the calloc in void ESP32RMTController::initPulseBuffer(int size_in_bytes) (line 539) as my next suspect.

  18. davidlmorris commented on Aug 1, 2024

    @davidlmorris
    Contributor

    Well now...
    (I'm not sure I understand this since 3 x 1452 x uint32_t = 17.4k, right? - in any case after adding a FAST_LED debug (pixels.size() * 3) = 1410 which would be the first controller with a strand with 470 pixels.)

    FASTLED: initPulseBuffer size_in_bytes: 1410
    
    assert failed: void ESP32RMTController::initPulseBuffer(int) clockless_rmt_esp32.cpp:541 (mBuffer != 0)
    
    
    Backtrace: 0x40084551:0x3ffb4ab0 0x400894b1:0x3ffb4ad0 0x4008f145:0x3ffb4af0 0x400d2dbb:0x3ffb4c20 0x400d4eda:0x3ffb4c40 0x400d5951:0x3ffb4c60 0x400d2a45:0x3ffb4cb0 0x4008177a:0x3ffb4cf0
      #0  0x40084551 in panic_abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_system/panic.c:408
      #1  0x400894b1 in esp_system_abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_system/esp_system.c:137
      #2  0x4008f145 in __assert_func at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/newlib/assert.c:85
      #3  0x400d2dbb in ESP32RMTController::initPulseBuffer(int) at .pio/libdeps/esp32doit-devkit-v1/FastLED/src/platforms/esp/32/clockless_rmt_esp32.cpp:541 (discriminator 1)
      #4  0x400d4eda in ClocklessController<32, 60, 150, 90, (EOrder)66, 0, false, 5>::convertAllPixelData(PixelController<(EOrder)66, 1, 4294967295u>&) at .pio/libdeps/esp32doit-devkit-v1/FastLED/src/platforms/esp/32/clockless_rmt_esp32.h:416
          (inlined by) ClocklessController<32, 60, 150, 90, (EOrder)66, 0, false, 5>::showPixels(PixelController<(EOrder)66, 1, 4294967295u>&) at .pio/libdeps/esp32doit-devkit-v1/FastLED/src/platforms/esp/32/clockless_rmt_esp32.h:400
      #5  0x400d5951 in CPixelLEDController<(EOrder)66, 1, 4294967295u>::show(CRGB const*, int, CRGB) at .pio/libdeps/esp32doit-devkit-v1/FastLED/src/controller.h:641
      #6  0x400d2a45 in CLEDController::showLeds(unsigned char) at .pio/libdeps/esp32doit-devkit-v1/FastLED/src/controller.h:127
          (inlined by) CFastLED::show(unsigned char) at .pio/libdeps/esp32doit-devkit-v1/FastLED/src/FastLED.cpp:68
      #7  0x4008177a in fastLedShowHandlerTask(void*) at src/displayFastLedCommon.cpp:273
    
    
  19. davidlmorris commented on Aug 1, 2024

    @davidlmorris
    Contributor

    Oops missed: mBufferSize = size_in_bytes * 8 * 4;
    So we should be allocating: [470 (pixels) * 3]bytes * 8 * 4 = 45120 (uint32_t) or 180,480 bytes.

  20. davidlmorris commented on Aug 2, 2024

    @davidlmorris
    Contributor

    Some Googling later...

    This stackoverflow discussion pointed to Esp32 Docs Memory Types which says:

    Note

    There is 520 KB of available SRAM (320 KB of DRAM and 200 KB of IRAM) on the ESP32. However, due to a technical limitation, the maximum statically allocated DRAM usage is 160 KB. The remaining 160 KB (for a total of 320 KB of DRAM) can only be allocated at runtime as heap.

    And 180,480 > 160 KB, so I guess that is the answer. This means either the code following needs to be broken down more, or we are limited to around ~415 pixels in a controller.

  21. niiiksh commented on Aug 2, 2024

    @niiiksh

    @davidlmorris I have only 364 pixels
    [364(pixels) * 3]bytes * 8 * 4 = 34944 (uint32_t) (139,776 bytes)
    and experiencing something like described problem with show freeze with running standard example of FastLED library called Pacific. I have ESP32S3.
    Workaround with #define FASTLED_RMT_MAX_CHANNELS 1 seems like fixed problem but one time it appeared again, it is rare case now with this workaround. I also experience something like white flicker once in a period of time (like 1 time in a hour or less) just for milliseconds and then returning to normal. Maybe it is related to this issue somehow too.

  22. davidlmorris commented on Aug 2, 2024

    @davidlmorris
    Contributor

    @niiiksh The issue above (the memory crash) only manifests with over a maximum (depending on other allocations in your code) of about 415 pixels when setting #define FASTLED_RMT_BUILTIN_DRIVER 1 using the clockless_rmt_esp32 driver.

    The issue you are having might be related to the original issue which not been resolved (though I have a workaround) which is the freeze or lockup with FastLED.show() which can happen minutes or more likely hours into a run (making it hard to track down).

    I think it is caused by an ESp32 edge case with interrupts, and the fix is to change xSemaphoreTake(gTX_sem, portMAX_DELAY); to xSemaphoreTake(gTX_sem, 2000 / portTICK_PERIOD_MS); for a wait of 2 seconds rather than indefinitely for the semaphore to be freed. See the discussion above. You need to dive deep into the FastLED lib to \FastLED\src\platforms\esp\32\clockless_rmt_esp32.cpp line 204 for version 3.6.0 and line 223 for version 3.70.

    The two seconds is pretty arbitrary, 5 seconds would do, or longer if you want to notice when it happens.

    Try that and see if it goes away. (I'll confess I was hoping that this would have made it into 3.7.0... perhaps next time?).

    The flicker might be something quite different. I would guess at some hardware issues (maybe put some capacitors if you haven't already between the 5V and GND on the LED strip, an electrolytic (10uf or bigger) for power smoothing, and a small 100nf ceramic for electrical noise. I put the ceramic close to the first pixel.) But this is just a guess!

  23. niiiksh commented on Aug 2, 2024

    @niiiksh

    @davidlmorris Thank you! I will try to modify the xSemaphoreTake call within the library to see if it resolves the freezing issue. Regarding the flickering, I plan to replace the capacitors on the board once I complete the integration of the Matter protocol into the code. This requires using Wi-Fi functions directly from the ESP libraries instead of the Arduino WiFi.h. Additionally, I need to implement MQTT, but cannot utilize the Arduino functions for this purpose and need some time for this work to be done. Currently, I have a 1uF capacitor between the 5V and GND right before the level shifter, not on the LED strip itself. The LED strip operates at 12V and uses WS2818B LEDs.
    My guess that WiFi library of Arduino leads to flickering because of its interrupts interferring FastLED or ESP32-S3 chip takes a lot of power when using WiFi which leads to flicker in a moment when power consumption is high.

  24. davidlmorris commented on Aug 2, 2024

    @davidlmorris
    Contributor

    Sounds interesting, I was going to look at ESP-NOW for something similar. Assuming that it is Wifi, and that it is power draw and not EMI (or something internal like interrupts et al which is probably more likely), one more guess/try which you have probably thought of already, is a large electrolytic across the power rails going into the Esp32 and a 100nf ceramic very close to the Esp32 power in for the same reasons as above.

  25. davidlmorris commented on Aug 3, 2024

    @davidlmorris
    Contributor

    @samguyer I propose to fork FastLED and do a pull request to modify the lib. I also propose to create a simple(ish) demo to repeatably show this issue (though this may be fraught with difficulties).

    Are you in a position to facilitate an update if I get all this working?

  26. davidlmorris commented on Aug 4, 2024

    @davidlmorris
    Contributor

    Ok. I've done it (hopefully correctly as this is my first pull request on Github). #1651

    I've also published the demo code to replicate the problem (no need for any hardware except for a bare-bones Esp32 - I've used esp32doit-devkit-v1). The code was designed for Platformio, but if you change FastLED_Hang_Fix_Demo.cpp to FastLED_Hang_Fix_Demo.ino it should work in any Arduino environment.

    ping @samguyer.

  27. zackees commented on Aug 8, 2024

    @zackees
    Member

    This fix has been merged

  28. ledereyes commented on Oct 28, 2024

    @ledereyes

    I believe I am also experiencing this issue on an ESP32 S3. As soon as there is lots of WiFi traffic, FastLED.show() does not return after a few seconds.

    I tried FastLED 3.9.0 with and without the define before including FastLED.h but doesn't seem to make a big difference. Maybe with the define it takes a bit longer until it hangs, but that could be coincidence also.

    #define FASTLED_RMT_MAX_TICKS_FOR_GTX_SEM (2000/portTICK_PERIOD_MS)
    

    What I noticed is that I am still getting WiFi events via the AsyncTCP library on Core 1 (the same Core where loop() is calling FastLED.show() ). At least that is the Core ID I am getting with

     xPortGetCoreID()
    
  29. zackees commented on Oct 28, 2024

    @zackees
    Member

    Well that's not good news.

    The issue is that we no longer stream encode for the RMT driver.

    Espressif does that now with their led_strip component which we then use.

    As far as I know, #define FASTLED_RMT_MAX_TICKS_FOR_GTX_SEM (2000/portTICK_PERIOD_MS) is now a no-op for RMT5 driver.

    So it got to be something else... I missed the repro case but I'll look at it now and see if i can reproduce.

  30. zackees commented on Oct 28, 2024

    @zackees
    Member

    The next step for this bug is to force a core dump while it's hanging.

    I recommend putting in a watchdog timer that will fire whenever loop() hasn't been called in over a 1 second.

    When the watchdog fires it should immediately force a crash so that the stack dump can be read out.

    Here's a platformio with the necessary changes to get stack dumps working:

    [env:dev]
    ; This is the espressif32 platform which is the 4.1 toolchain as of 2024-Aug-23rd
    ; platform = espressif32
    ; The following platform enables the espressif32 platform to use the 5.1 toolchain, simulating
    ; the new Arduino 2.3.1+ toolchain.
    
    # Developement branch of the open source espressif32 platform
    platform = https://github.com/pioarduino/platform-espressif32.git#develop
    
    framework = arduino
    board = esp32-s3-devkitc-1
    
    upload_protocol = esptool
    
    monitor_filters = 
    	default
    	esp32_exception_decoder  ; Decode exceptions so that they are human readable.
    
    ; Symlink in the FastLED library so that changes to the library are reflected in the project
    ; build immediatly.
    lib_deps =
      FastLED=symlink://./
    
    build_type = debug
    
    build_flags =
    	-DDEBUG
        -g
        -Og
        -DCORE_DEBUG_LEVEL=5
        -DLOG_LOCAL_LEVEL=ESP_LOG_VERBOSE
        -DFASTLED_RMT5=1
        ```
    
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions