Skip to content

Inefficient SPI Data Transmission for APA102 / SK9822 LEDs on ESP32 #1609

Closed

Description

@coryking

Summary

When using FastLED with ESP32 to drive APA102 and SK9822 LED strips, significant inefficiencies have been observed in SPI data transmission. The implementation appears to conduct separate DMA transactions for each byte of data, introducing noticeable gaps between 8-bit bursts. This behavior considerably diminishes the effective data transmission rate, closely mirroring the performance limitations characteristic of older one-wire LED models like the WS2812b.

Environment

  • Microcontroller: ESP32
  • LED Types: APA102 and SK9822
  • LED Count: Tested with 30 LEDs, but the issue is applicable to any strip length
  • Data Rate: Configured for 5MHz and 20MHz
  • Library Version: FastLED (latest as of writing)

Observations

  • Oscilloscope Measurements:
    • With a data rate set to 5MHz, updating a 30-LED strip takes about 422 microseconds.
    • Setting the data rate to 20MHz reduces the update sequence to approximately 244 microseconds, which, despite showing improvement, falls short of expected performance levels.
    • Gaps between each 8-bit data burst are measured to be roughly 1.7 microseconds, aligning with the setup overhead for DMA transactions described in ESP32 documentation.

Expected vs. Actual Behavior

Expected: For high-speed SPI communication, especially at higher data rates, FastLED should utilize continuous DMA transfers for the entire data packet of LED strip data, minimizing CPU overhead and maximizing throughput for quick and efficient updates.

SDS00004
This is what I'm calling the "inner byte gap". It's roughly 1.7 or so microseconds. It transmits 8 bits, takes a break, transmits 8 more bits... etc.

Actual: The implementation seems to trigger individual DMA transactions for every byte sent to the APA102 and SK9822 LEDs, leading to unnecessary overhead, reduced transmission rate, and increased update times, particularly for longer LED strips.

Investigation and Documentation

This issue aims to document and investigate the SPI data transmission inefficiency for APA102 and SK9822 LEDs on the ESP32 using FastLED. The goal is to explore this issue further, propose optimizations, and prevent duplication of efforts across multiple issues or pull requests.

SDS00002
The above image shows how long it took to transmit a 30 LED strip. Roughly 230uS at a "20MHz" setting in fastLED.

Additionally, this concern echoes a comment from @SCarter858 on Dec 27, 2023, which detailed similar inefficiencies and questioned the actual utilization of DMA for SPI communication within FastLED's ESP32 HW SPI support.

Additional Context

Note that this being caused by separate DMA transactions is only hypothetical. It might not be using DMA at all!

This inefficiency not only affects APA102 LEDs but also their SK9822 counterparts, which are often used interchangeably in projects. The potential lack of full DMA utilization for SPI transmission significantly throttles the performance capabilities of these chips, making high-density LED installations as slow as older one-wire LED protocols like the WS2801. An optimized implementation would greatly enhance performance, leveraging the ESP32's capabilities for LED projects using FastLED.

Activity

  1. coryking commented on Mar 11, 2024

    @coryking
    ContributorAuthor

    Here is some results when I used a completely different APA102 driver I found online: DMA Findings.md

    As you can see from the oscilloscope readings in the linked document, when you use DMA it looks nothing like the ones in this ticket. Plus the speeds are significantly faster (though they appear to be "rounded" for reasons I don't fully understand.

  2. coryking commented on Mar 17, 2024

    @coryking
    ContributorAuthor

    So I need everybody's opinion on how to provide the "SPIOutput" class the number of LEDs / bytes required for a DMA buffer.

    In order to get DMA working we need to allocate a buffer on the heap but to do so we need the size of the buffer. I think the best place for this is in the init() function. The problem is these "SPIOutput" classes do not know how many LEDs they are supposed to be driving, thus I have no clue how big of a buffer to allocate. So I need to get the number of LED's plumbed in and probably the number of "extra" bytes needed for start and end frames.

    The problem is these SPI handlers are all supposed to look like "SPIOutput" even though they are all completely different classes. So I suspect if I make a change to init() to pass in the number of leds (or perhaps just bytes) I'll have to change all the other SPIOutput classes as well (which may not be a bad idea because any of the other SPI classes that do DMA need the same info).

    So, in short:

    1. Is SPIOutput the correct place to manage the DMA buffer?
    2. What is the best way to provide SPIOutput with the size of said DMA buffer?
    3. If it is the SPIOutput::init() function like I plan, are we cool with updating all the other SPIOutputs to have the same signature?
    4. Should it get the number of LEDs or the number of bytes? I'm leaning toward the number of bytes, as I suspect that SPIOutput is a "bit pusher" and isn't supposed to care about the various LED protocols.

    Pinging @samguyer and @kriegsman for their thoughts as well as @Jueff & @sheaivey who looks to have done some DMA stuff for a different platform.

    To avoid getting blocked, if I don't hear anything in the next few days I'm gonna proceed with doing this in the ESP32SPIOutput class and passing the number of bytes / leds through the init() function.

  3. coryking commented on Mar 17, 2024

    @coryking
    ContributorAuthor

    Also I should add I'm going to borrow quite liberally from the led_strip_spi component from the UncleRus esp-if-lib component library. I'll make sure to adhere to its MIT License when pushing my changes.

  4. zackees commented on Aug 17, 2024

    @zackees
    Member

    Any update on this?

  5. zackees commented on Nov 14, 2024

    @zackees
    Member

    Make sure that you have #define FASTLED_ALL_PINS_HARDWARE_SPI before the #include "FastLED.h" part to make sure you are not doing software bit banging.

    Okay so i took at look at src/platforms/esp/32/fastspi_esp32.h and indeed we are using the 8-bit spi write and just iterating through the pixels. From the documentation of ESP32, these spi transactions need to grab a lock once per byte. So if true, that would explain the pattern of inefficient transmission you are seeing - it's basically blocked on a lock.

    Say you have 1k leds, that's going to translate into 3k lock/unlocks to write all that data out.

    Looking through the docs I see that esp32 now provides void spiWritePixelsNL(spi_t *spi, const void *data_in, uint32_t len); as a new experimental API. Apparently, an entire buffer can now be sent to the SPI device in one shot and the lock will hold until the pixels have been flushed out through the spi device. I'd have to look more closely to see if 2 or more SPI devices can operate in parallel in FastLED.

    Looking at src/platforms/esp/32/fastspi_esp32.h at the function template <uint8_t FLAGS, class D, EOrder RGB_ORDER> __attribute__((noinline)) void writePixels(PixelController<RGB_ORDER> pixels, void* context);

    I see that we are writing each r,g,b byte out, then we are doing our advancedData() and stepDithering() operations after each pixel write. So sending an undefined block of data out to the spi in it's entirely won't work unless we supply a side buffer. However, we do do this for the WS2812 and other clockless data types exactly for this reason.

    So the solution here is either create a side buffer that lives for the duration of the esp32 spi implementation... OR allocate a giant block of leds and send K batches for N leds. For example the batch size could be 256 pixels. This would mean that the inefficiency that you see would be cut to 1/256 of what it is now, which would almost be identical to full performance mode where everything is sent all at once.

  6. zackees commented on Nov 14, 2024

    @zackees
    Member

    I'm adding a new feature for bulk transfer through the SPI device on ESP32. You can enable it via #define FASTLED_ESP32_SPI_BULK_TRANSFER 1 before the inclusion of FastLED.

    It will go out with 3.9.4. Right now it's just in master. I've compile tested, but haven't run it yet. If anyone tests it, let me know the results.

  7. coryking commented on Nov 14, 2024

    @coryking
    ContributorAuthor
  8. SCarter858 commented on Dec 31, 2024

    @SCarter858
    Contributor

    Thanks, Zackees! I finally got back to this project and turning on your new bulk transfer flag doesn't seem to change the speed/operation of the SPI output. But I haven't dug into things very far yet...

  9. zackees commented on Jan 1, 2025

    @zackees
    Member

    Thanks, Zackees! I finally got back to this project and turning on your new bulk transfer flag doesn't seem to change the speed/operation of the SPI output. But I haven't dug into things very far yet...

    You are using a build define and not an include define right?

  10. SCarter858 commented on Jan 1, 2025

    @SCarter858
    Contributor

    Using this at the start of my main.cpp:

    #define FASTLED_ESP32_SPI_BULK_TRANSFER 1
    #define FASTLED_ALL_PINS_HARDWARE_SPI
    #include <FastLED.h>

    Without debugger and scope hooked up, I see that it does indeed seem to invoke your changes, since I can console print that FASTLED_ESP32_SPI_BULK_TRANSFER_SIZE is set to 64 (your default).

    But the refresh rate that I'm seeing (a single strand of 128 APA102s can refresh only 7 frames/sec) is exactly the same with or without your bulk transfer invoked.

    As a quick debug, I put a couple Serial.print lines in the writePixels template where you implemented your fix, and it looks like that template is never being called?

  11. zackees commented on Jan 1, 2025

    @zackees
    Member

    I don't know why it wouldn't be called. I just did an audit.

    Massively increase the size of FASTLED_ESP32_SPI_BULK_TRANSFER_SIZE and see if that helps.

    Try to put some print statements to confirm what is happening. It should be calling my new api.

  12. SCarter858 commented on Jan 1, 2025

    @SCarter858
    Contributor

    Part of the problem might be in the initial conditional compile setup: (all this in file fastspi_esp32.h)

    Putting some "#pragma message" statements in, it looks like (when compiling for an ESP32S3), it correctly goes into the #if of lines 70-75 to set the bus to FSPI (though I'm not sure what that term is - it's not standard ESP nomenclature?). But just a few lines later, it compiles lines 83-87, as if the bus was set to VSPI. It does not compile lines 93-97 as expected. Later, in the early part of the main.cpp program, when I print the value of FASTLED_ESP32_SPI_BUS to the console, it shows as "0".

    Perhaps VSPI is a reserved word, while FSPI is not? (color coding of them in VSCode is different).

    Putting in some Serial.print lines in the main program as pixel writes are happening shows that the "select()" method in the file we're talking about is called frequently, but I can't find any of the other writeByte, writePixel, etc, functions ever being called. But this is all downstream of the initial setup discussed above...

  13. added
    platform: esp32
    area: drivers
    status: stale
    status: triage
    on Apr 16, 2026
  14. github-actions commented on Sep 1, 2026

    @github-actions
    Contributor

    This issue was closed after 30 additional days without activity. If it is still relevant, please comment with current reproduction details or other actionable information and it can be reopened.

  15. zackees commented on Sep 3, 2026

    @zackees
    Member

    Reopening: this issue was closed automatically by a stale-bot run, not by a maintainer decision. That auto-close behavior was a mistake and has been disabled — FastLED issues will no longer be closed for inactivity. Apologies for the noise.

  16. reopened this on Sep 3, 2026
  17. removed
    status: stale
    on Sep 3, 2026
  18. SCarter858 commented on Oct 26, 2026

    @SCarter858
    Contributor

    Circling back on this years later — finally dug into it properly.

    Root cause: ESP32SPIOutput's direct select()/writeByte()/writeWord()/release() path (used by APA102/SK9822/WS2801-style clocked chipsets) issues one spi_device_polling_transmit() per byte, each of which blocks on the SPI peripheral lock as @zackees suspected back in Nov 2024. FASTLED_ESP32_SPI_BULK_TRANSFER doesn't help because it's wired into a different code path (writePixels) that this chipset class never calls — matches what I found testing it back in Jan 2025 in the thread above.

    Filed with a working fix (ring-buffered async DMA via spi_device_queue_trans(), confirmed on real hardware): #4918 / #4919.

  19. SCarter858 commented on Oct 26, 2026

    @SCarter858
    Contributor

    Closing - fixed by #4919, merged 2026-10-26. Thanks @zackees for the fix path and the hardware verification on the PR.

  20. SCarter858 commented on Oct 26, 2026

    @SCarter858
    Contributor

    Looks like neither the reporter nor I have permission to close this ourselves - @zackees, could you close this out now that #4919's merged? Thanks again for the hardware verification.

  21. zackees commented on Oct 26, 2026

    @zackees
    Member

    Thank you!!!

  22. moved this from Triage to Done in FastLED Trackeron Oct 26, 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: driversplatform: esp32status: triage

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions