Repository navigation
Description
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:392createLcdClocklessEngine()—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
- Panel IO is created lazily in
transmit()(lcd_spi_peripheral_esp.cpp.hpp:273-308). The code already acknowledgesstale 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. - 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 ownisrChunkDonefunction + context type). mCompleteSembinary 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).- 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)
- 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. - Null out the ISR callback + flush semaphore on every
deinitialize(). Even in the fast path, makebeginTransmission()re-register the ISR and reset the semaphore to the primed state. - Split into two separate singletons —
LcdSpiPeripheralEspandLcdClocklessPeripheralEsp— guarded by a mutex so only one can hold the I80 bus at a time, with explicitacquire()/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
- feat(esp32-s3): ISR-driven chunked DMA for LCD_CAM SPI and clockless drivers #2260 (merged) — original LCD_SPI + LCD_CLOCKLESS ISR-driven DMA
- fix(esp32-s3): LCD_CLOCKLESS GPIO crash fix + autoresearch --lcd/--lcd-spi #2269 (open) — GPIO default crash fix + autoresearch CLI wiring (hit this issue during back-to-back verification)
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsTriage
Activity
zackees commentedon Apr 18, 2026
Follow up review requested
fix(esp32s3,lcd): force full teardown on cross-driver switch (#2270)