Skip to content

bug(esp32-s3): LCD_SPI and LCD_CLOCKLESS share LcdSpiPeripheralEsp singleton with fragile re-init #2270

Closed

Description

@zackees

Summary

On ESP32-S3, both LCD_SPI (APA102/SK9822) and LCD_CLOCKLESS (WS2812/SK6812) drivers wrap the same fl::detail::LcdSpiPeripheralEsp::instance() singleton. Switching between the two driver types at runtime exercises a re-initialisation path that does not fully tear down the I80 bus / GDMA / ISR / semaphore state, producing intermittent passed:false results on the second switch even when the captured LED bytes match (mismatchedLeds:0).

Evidence

Running alternating runSingleTest calls against a freshly-booted ESP32-S3 (5 LEDs, WS2812B-V5 timing):

# Driver Result
1 LCD_SPI passed:true
2 LCD_CLOCKLESS passed:true
3 LCD_SPI passed:false (mismatchedLeds:0)
4 LCD_CLOCKLESS passed:false (mismatchedLeds:0)

First switch (SPI → CLOCKLESS) works. Second switch (CLOCKLESS → SPI → CLOCKLESS) fails despite the captured LED data matching — the passed flag drops for a reason other than data integrity, which points at the shared-peripheral re-init path.

Root cause (shared-singleton architecture)

Both factory functions in src/platforms/esp/32/drivers/lcd_spi/ register an EspWrapper that forwards every method to LcdSpiPeripheralEsp::instance():

  • createLcdSpiEngine() — channel_driver_lcd_spi.cpp.hpp:392
  • createLcdClocklessEngine() — channel_driver_lcd_clockless.cpp.hpp:419

The two drivers hold different mInitialized flags locally, but the singleton has only one state. Each driver's beginTransmission() calls mPeripheral->initialize(config) with its own config; if the config differs (lanes, clock_hz, transfer size) the singleton calls deinitialize() then re-initialises the I80 bus:

// lcd_spi_peripheral_esp.cpp.hpp:102
bool LcdSpiPeripheralEsp::initialize(const LcdSpiConfig &config) {
    if (mInitialized) {
        if (mConfig.num_lanes == config.num_lanes &&
            mConfig.clock_hz == config.clock_hz &&
            mConfig.max_transfer_bytes >= config.max_transfer_bytes) {
            return true;   // fast-path: same config, no teardown
        }
        deinitialize();    // slow-path: tear down + recreate
    }
    ...
}

Fragility in the re-init path

  1. Panel IO is created lazily in transmit() (lcd_spi_peripheral_esp.cpp.hpp:273-308). The code already acknowledges stale GDMA descriptor state that causes the second transmit to produce no output (ESP_OK but no DMA activity) — the panel_io recreate addresses the same-driver case but not the cross-driver case where encoding, clock, and ISR callback all differ.
  2. ISR callback stays registered across driver switches — there's no registerTransmitCallback(nullptr, nullptr) between drivers, so the callback from the previous driver type can fire once against the new driver's ring buffers (each driver has its own isrChunkDone function + context type).
  3. mCompleteSem binary semaphore is reused across re-inits — deinitialize() only deletes it on full tear-down, but the fast-path re-init leaves it in whatever state the previous driver left it (given, taken, or in-flight from an unacked DMA completion).
  4. Ring buffer ownership — each driver owns its own ring buffers via its own allocateBuffer()/freeBuffer() calls on the singleton. If a driver aborts mid-stream, the singleton may still hold an ISR pointer into the other driver's freed buffers.

Other bulk drivers

Audited for the same pattern:

Family SPI variant Clockless variant Share peripheral? Status
I2S I2S_SPI (ESP32dev, direct I2S register + lldesc_t DMA) I2S (ESP32-S3 misnomer — actually LCD_CAM I80 via I2sLcdCamPeripheralEsp) No — distinct HW subsystems, distinct singletons OK
PARLIO none (ChannelDriverPARLIOImpl::canHandle() returns !isSpi()) PARLIO N/A OK
LCD_CAM (I80) LCD_SPI LCD_CLOCKLESS Yes — shared LcdSpiPeripheralEsp singleton Problem

LCD_RGB (ESP32-P4 only) uses esp_lcd_panel_rgb via a separate LcdRgbPeripheralEsp singleton and does not overlap with the I80 path — no concern.

Suggested fixes (in order of increasing invasiveness)

  1. Force full deinitialize() on driver-type change. Track the previous caller identity (SPI vs clockless) on the singleton; if it changes, skip the fast-path same-config return and always tear down. Cheapest fix.
  2. Null out the ISR callback + flush semaphore on every deinitialize(). Even in the fast path, make beginTransmission() re-register the ISR and reset the semaphore to the primed state.
  3. Split into two separate singletons — LcdSpiPeripheralEsp and LcdClocklessPeripheralEsp — guarded by a mutex so only one can hold the I80 bus at a time, with explicit acquire() / release() that fully tears the bus between holders. Cleanest but touches more code.

The issue does not block #2260 (now merged) or its follow-up (#2269) because each driver passes when used alone after a fresh boot — it only bites when both drivers are exercised in the same session, which is an unusual production pattern but common for autoresearch / --all sweep testing.

Related

Activity

  1. zackees commented on Apr 18, 2026

    @zackees
    MemberAuthor

    Follow up review requested

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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions