Skip to content

LCD_SPI driver: buffer growth path fails on ESP32-S3 hardware #2249

Closed

Description

@zackees

Summary

The new LCD_SPI channel driver (ESP32-S3, via LCD_CAM I80 bus) fails when the buffer size needs to grow between successive show() calls, even when the peripheral configuration (lane count, clock) is unchanged. Investigation during hardware validation reveals several related issues.

Environment

  • Platform: ESP32-S3 (Seeed XIAO ESP32-S3)
  • Framework: Arduino / ESP-IDF 5.5
  • Branch: master
  • Chipset: APA102 (SPI clocked)
  • Validation: bash autoresearch --lcd-spi with RMT RX loopback (GPIO1 → GPIO2)

Issue 1 — Buffer growth path silently fails

Repro: Run two consecutive FastLED.show() calls with the same lane count but different LED strip sizes.

bash autoresearch --lcd-spi --lanes 1 --strip-sizes 100,250 --skip-lint --no-fbuild

Expected: Both tests pass.
Actual:

  • Test 1 (1 lane × 100 LEDs): PASS
  • Test 2 (1 lane × 250 LEDs): FAIL — RMT RX receives 0 symbols or incomplete data

When 250 LEDs is run standalone as the first test, it passes. The failure only occurs when a smaller transmit preceded it. The device stays responsive (not a crash) — esp_lcd_panel_io_tx_color() appears to return ESP_OK but DMA does not produce output on the expected GPIO.

What was tried (did not fix the issue)

  1. Over-provisioned max_transfer_bytes — set to max(bufferBytes, 256*1024) on first init so subsequent transmits stay within the DMA descriptor chain.
  2. Avoid deinitialize() + initialize() on buffer growth — reinit only when num_lanes actually changes. This fixed the lane-change path but not the buffer-growth path.
  3. Drain stale semaphore — xSemaphoreTake(mCompleteSem, 0) at the start of transmit() to consume completion signal from the previous transfer.
  4. Pre-allocate buffer at max size on first use so no free/realloc happens between transmits. The failure still occurs, ruling out a cache coherence issue from reallocation.

Current state (partial fix in working tree)

Applied in src/platforms/esp/32/drivers/lcd_spi/channel_driver_lcd_spi.cpp.hpp:

  • beginTransmission() only reinitializes the peripheral when numLanes != mNumLanes
  • Buffer allocated once at max(bufferBytes, 256KB) on first use
  • mNumLanes tracked separately from mInitialized

Applied in src/platforms/esp/32/drivers/lcd_spi/lcd_spi_peripheral_esp.cpp.hpp:

  • transmit() drains stale semaphore before queueing new transaction
  • I80 bus created with GPIO 0 as dummy for unused lanes (was -1, which the I80 API rejects)

Result: Lane count changes work (tests with different lane counts all pass at 100 LEDs). Same-lane buffer growth still fails.

Issue 2 — Lane count reinit path was broken

Original bug (before any fix): Any second runSingleTest call would fail, regardless of whether the lane count or buffer size changed. Only the very first transmit after boot would succeed.

Root cause: beginTransmission() called mPeripheral->deinitialize() + mPeripheral->initialize() whenever mBufferSize < bufferBytes. On ESP32-S3, this deinit/reinit cycle breaks GPIO matrix routing — the LCD_CAM peripheral loses its connection to the output pin silently.

Partial fix: Separate peripheral lifecycle from buffer lifecycle. Only call deinitialize() + initialize() when hardware config (lane count) actually changes. This allows lane-count changes to work correctly because the reinit is rarer.

Issue 3 — RMT RX capture buffer overflow for large SPI payloads

During 300 LED LCD_SPI testing, the validation test emits:

[RAW EDGE TIMING] WARNING: No edge data captured at offset 14348
[Run 1/1] FAIL | Errors: 1/300 (99.67%)
First error at LED[299]: expected RGB(0,0,255) got RGB(0,0,0)

The RMT RX capture buffer runs out at ~14348 bytes, losing the last LED's data. The SPI decoder successfully reconstructs the first 299 LEDs correctly.

Threshold: 299 LEDs passes, 300 LEDs fails (standalone test).

This is a validation test limitation, not an LCD_SPI driver bug, but it prevents end-to-end validation of larger strip sizes.

Fix location: examples/AutoResearch/AutoResearchTest.cpp and src/platforms/esp/32/drivers/rmt_rx/rmt_rx_channel.cpp.hpp — RX buffer sizing for SPI capture mode.

Issue 4 — I2S LCD_CAM clockless driver works correctly

For reference, the I2S driver (bash autoresearch --lcd) passes all 24 test configurations across 1-3 lanes × 100/300 LEDs. The LCD_SPI driver has the same I80 bus lifecycle management but fails on the buffer growth path, suggesting the bug is specific to esp_lcd_panel_io_tx_color() or the I80 bus reconfiguration path.

Working Test Matrix

# Lanes LEDs/Lane LCD_SPI Result
1 1 100 PASS
2 1 250 FAIL (after 100 LEDs); PASS (standalone)
3 1 300 FAIL (RX buffer overflow at LED[299])
4 2 100 PASS
5 2 250 FAIL
6 3 100 PASS
7 3 250 FAIL

Reproducible Test Commands

# Passes — single lane, single size
bash autoresearch --lcd-spi --lanes 1 --strip-sizes 100 --skip-lint --no-fbuild

# Passes — multi-lane at small size
bash autoresearch --lcd-spi --lanes 1-3 --strip-sizes 100 --skip-lint --no-fbuild

# Passes — 250 LEDs standalone
bash autoresearch --lcd-spi --lanes 1 --strip-sizes 250 --skip-lint --no-fbuild

# FAILS — buffer growth path (test 2 fails after test 1)
bash autoresearch --lcd-spi --lanes 1 --strip-sizes 100,250 --skip-lint --no-fbuild

# FAILS — RMT RX buffer overflow at LED[299]
bash autoresearch --lcd-spi --lanes 1 --strip-sizes 300 --skip-lint --no-fbuild

Unit Tests Added

Two new test cases in tests/platforms/esp/32/drivers/lcd_spi/channel_driver_lcd_spi.cpp:

  • "ChannelDriverLcdSpi - buffer growth without peripheral reinit" — verifies deinitialize() is NOT called when only buffer size changes
  • "ChannelDriverLcdSpi - lane count change triggers reinit" — verifies reinit happens when lane count changes

Both pass with the mock peripheral, but the mock cannot reproduce the hardware-specific I80 bus DMA behavior. The failure is only observable on physical ESP32-S3 hardware.

Hypotheses for Remaining Bug

  1. ESP-IDF I80 bus caches transaction size internally and does not handle varying sizes on the same panel IO handle.
  2. lcd_cmd_bits = 0 combined with dc_levels.dc_data_level = 1 may have an edge case where subsequent transactions skip the DC pin toggle and DMA output.
  3. GDMA descriptor chain reuse between transactions of different sizes may leave stale descriptor state.
  4. PSRAM write-through cache — data written to the buffer may not be visible to DMA on the second transmit due to a missed esp_cache_msync() call in ESP-IDF's I80 driver.

Related Commits

  • 61898e066c — refactor(esp32): replace SpiHw16 with I2S_SPI and LCD_SPI channel drivers
  • Working tree changes to channel_driver_lcd_spi.cpp.hpp, lcd_spi_peripheral_esp.cpp.hpp, lcd_spi_peripheral_mock.cpp.hpp, channel_driver_lcd_spi.h, and test files contain the partial fix.

Activity

  1. zackees commented on Apr 11, 2026

    @zackees
    MemberAuthor

    Root Cause Found & Fix

    The buffer growth path failure was caused by the Channel constructor calling fl::pinMode(pin, InputPulldown) on the SPI data pin. This reconfigured the pin as a GPIO input, disconnecting the SPI MOSI output from the ESP32-S3 GPIO matrix.

    When FastLED.clear(ClearFlags::CHANNELS) + FastLED.add() recreated channels between strip size tests, the new Channel's constructor broke the persistent SPI driver's GPIO routing. The SPI peripheral continued to return ESP_OK from spi_device_polling_transmit() but no data reached the physical pin — hence "0 RMT symbols received".

    Diagnostic evidence

    • Same strip size (100→100) also failed, ruling out buffer size as the sole cause
    • RMT RX armed successfully (rmt_receive() returned OK with correct state)
    • SPI driver reported no transmit errors
    • fl::pinMode(getPin(), fl::PinMode::InputPulldown) found in all 3 Channel constructors

    Changes

    1. channel.cpp.hpp — Removed fl::pinMode(InputPulldown) from all Channel constructors. The driver configures the pin when it first uses it.
    2. channel_driver_spi.cpp.hpp — Added ledSourceBuffer reallocation on channel reuse for larger strips (prevents ISR-unsafe PSRAM fallback).
    3. lcd_spi_peripheral_esp.cpp.hpp — Semaphore priming, blocking wait, lazy panel IO recreation on size change, explicit esp_cache_msync() for PSRAM coherence.
    4. rmt_rx_channel.cpp.hpp — Reset mAccumulationOffset in clear() to prevent stale write offsets.

    Validation

    TEST SPI lanes=1 leds=100 PASS 92ms
    TEST SPI lanes=1 leds=250 PASS 131ms
    TEST SPI lanes=1 leds=100 PASS 90ms
    ✓ AUTORESEARCH SUCCEEDED
    
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