Repository navigation
Description
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
Ezekiel-DA commentedon Oct 10, 2022
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 codezackees commentedon Oct 10, 2022
try
Ezekiel-DA commentedon Oct 11, 2022
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.
Ezekiel-DA commentedon Oct 12, 2022
Update: crossposted this to the reddit community here for more input, and found what appears to be a better workaround:
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.
PureTek-Innovations commentedon Oct 12, 2022
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.
zackees commentedon Oct 13, 2022
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.
samguyer commentedon Nov 25, 2022
@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.
TriviumGames commentedon Nov 27, 2022
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:
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:
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.
Ezekiel-DA commentedon Nov 29, 2022
@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?
TriviumGames commentedon Nov 30, 2022
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.
samguyer commentedon Nov 30, 2022
TriviumGames commentedon Nov 30, 2022
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.
dotorg commentedon Nov 30, 2022
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.
samguyer commentedon Nov 30, 2022
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.
Ezekiel-DA commentedon Dec 1, 2022
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.44 remaining items
davidlmorris commentedon Aug 1, 2024
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!
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.davidlmorris commentedon Aug 1, 2024
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.)
davidlmorris commentedon Aug 1, 2024
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.
davidlmorris commentedon Aug 2, 2024
Some Googling later...
This stackoverflow discussion pointed to Esp32 Docs Memory Types which says:
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.
niiiksh commentedon Aug 2, 2024
@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.
davidlmorris commentedon Aug 2, 2024
@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 1using 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);toxSemaphoreTake(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.cppline 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!
niiiksh commentedon Aug 2, 2024
@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.
davidlmorris commentedon Aug 2, 2024
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.
davidlmorris commentedon Aug 3, 2024
@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?
davidlmorris commentedon Aug 4, 2024
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.cpptoFastLED_Hang_Fix_Demo.inoit should work in any Arduino environment.ping @samguyer.
zackees commentedon Aug 8, 2024
This fix has been merged
ledereyes commentedon Oct 28, 2024
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.
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
zackees commentedon Oct 28, 2024
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.
zackees commentedon Oct 28, 2024
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: