Skip to content

feat(spi,esp32): implement back-to-back DMA chunk streaming for strips > ~680 LEDs #2304

Open

Description

@zackees

Summary

Follow-up to #2254 / PR #2300. The merged fix bumped the SPI channel staging buffer from 4 KB to 16 KB so a single spi_device_queue_trans() call covers up to ~680 WS2812B LEDs in one continuous DMA transaction. This resolves the "black frame between displays" bug for most real-world strips, but strips longer than ~680 LEDs (24 bytes/LED × wave8) still split into multiple SPI transactions advanced by the poll() loop. Between transactions, the SPI line idles for the time it takes the host task to notice completion and queue the next chunk — which can exceed WS2812B's ~50 µs reset threshold under RTOS contention.

This issue tracks a proper fix: back-to-back DMA chunk streaming where ESP-IDF's SPI ISR transitions directly from one queued transaction to the next without any host-task involvement, making the gap independent of poll() cadence.

Current behavior

src/platforms/esp/32/drivers/spi/channel_driver_spi.cpp.hpp:

  • dev_config.queue_size = 4 — already allows 4 transactions queued at once.
  • startFirstDma() queues one transaction via spiStart() and sets mDmaInFlight = true.
  • advancePipeline() (called from poll()) waits for completion, encodes the next chunk into the alternate buffer, queues it.

Sequence:

[encode A] → [queue A] → [wait for A] → [encode B] → [queue B] → [wait for B] → ...
                          ↑ inter-chunk gap scales with poll() latency

Proposed behavior

Pre-queue both buffers so the ESP-IDF SPI ISR chains directly from transA→transB (or B→A) the moment one completes, without any host involvement:

[encode A] → [queue A] → [encode B] → [queue B]      ← both in flight from start
  poll(): check what completed → refill → re-queue      ← refill-on-completion
  ESP-IDF ISR auto-starts next queued txn, ~µs gap, not tied to poll()

Implementation sketch

  • startFirstDma(): encode both stagingA and stagingB and queue both transactions before returning.
  • advancePipeline() becomes a refiller:
    • On completion of transA with data remaining: encode next chunk into stagingA, re-queue transA.
    • On completion of transB with data remaining: encode next chunk into stagingB, re-queue transB.
    • Termination: track ledBytesRemaining and only re-queue when data remains; at end-of-data, wait for both outstanding transactions to drain before marking channel complete.
  • Teardown/release path (releaseChannel, ~ChannelEngineSpi): ensure both transactions drained before freeing staging buffers.

Maybe 50-100 lines modified in the state machine. Data structures (stagingA/B, transA/B, queue_size=4) are already in place.

Expected benefits

  • Supports arbitrarily long strips (1500+ LEDs) on SPI without the poll()-scheduling dependency.
  • Can revert staging_size back to 4 KB if desired — total RAM footprint returns to 2×4 KB per channel instead of 2×16 KB.
  • Removes a whole class of RTOS-contention-related LED artifacts reported in MoonLight-style workloads.

Risks / things to validate

  • ESP-IDF SPI master inter-transaction gap measurement on ESP32-S3, ESP32-C3, ESP32-C6. Should be < 1 µs in DMA mode but worth confirming with an oscilloscope or RMT RX timing dump.
  • Does spi_device_queue_trans() from a polling-mode chip (ESP32-C6 uses spi_device_polling_start per channel_driver_spi.cpp.hpp:192) behave differently? May need a separate code path for C6.
  • Teardown correctness — need to drain both outstanding transactions before spi_bus_remove_device.
  • Interaction with transAInFlight / transBInFlight bookkeeping — already present but only one is true at a time today.

Test plan

On ESP32-S3 via bash autoresearch:

Related

Activity

  1. added
    platform: esp32
    area: drivers
    on Apr 18, 2026
  2. zackees commented on Aug 20, 2026

    @zackees
    MemberAuthor

    Marking deferred after implementation/history and attached-hardware audit.

    Merged PR #2810 implements the ESP32 A/B prequeue/refill/drain state machine, and follow-up regressions were repaired in #2843/#2869. The remaining acceptance evidence is hardware-specific: >683-LED byte equality plus inter-chunk timing under WiFi/HTTP contention on ESP32-S3/C3, and an explicit decision for ESP32-C6's single-in-flight polling path.

    Current attached hardware has only an ESP32-C6 (COM9); no S3/C3 target is present. A diagnostic bash autoresearch esp32c6 --spi --strip-sizes 1000 --frames 2 spent the bounded five-minute window compiling the full C6 framework and never reached deploy, so its exact process tree was terminated. C6 cannot supply the missing S3/C3 DMA-chaining proof in any case.

    Resume when an ESP32-S3/C3 loopback target is attached. Run a 1000-LED/two-frame SPI test under network contention, capture full byte equality and the inter-chunk gap, then document C6 as unsupported for gap-free chaining or land a separately tested C6 implementation.

  3. added
    deferred
    on Aug 20, 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: driversdeferredplatform: esp32

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions