Skip to content

singleton: migrate i2s_esp32dev driver state — 19 hits, ISR pattern (Tier 2a) #3489

Closed

Description

@zackees

Part of #3481 (Tier 2a). The motivating case.

Task

Migrate the ~19 file-scope statics in src/platforms/esp/32/drivers/i2s/i2s_esp32dev.cpp.hpp (the restored Yves Bazin I2S driver) to fl::Singleton<T>. This is the file that originally prompted the Singleton-elision investigation.

Current state

Approximately 19 namespace-scope statics including:

  • gCallback (function pointer)
  • gCntBuffer, gDoneFilling (state flags)
  • dmaBuffers[] (DMA descriptors)
  • gTX_semaphore (FreeRTOS semaphore handle)
  • gI2S_intr_handle (ESP-IDF interrupt handle)
  • i2s (peripheral pointer)
  • timing tables

Full list produced by SingletonElisionChecker warn output on this file.

The ISR problem

Some of this state is touched from an IRAM-resident ISR (i2s_intr_handler or similar). Naive Singleton<T>::instance() inside the ISR triggers __cxa_guard_acquire on first call — a mutex-guarded initialization path that is not ISR-safe and would deadlock or blow past IRAM budget.

Required pattern

struct I2SDriverState { /* all 19 fields */ };

// In .cpp.hpp — kept in IRAM
static I2SDriverState* IRAM_ATTR s_i2s_state = nullptr;

// Main-thread setup:
void I2SDriver::begin() {
    auto& s = fl::Singleton<I2SDriverState>::instance();  // guard runs here
    s_i2s_state = &s;                                     // cache pointer
    // ... init using s_i2s_state->foo
}

// ISR uses cached pointer, no guard:
IRAM_ATTR static void i2s_intr_handler(void*) {
    if (!s_i2s_state) return;
    s_i2s_state->gCntBuffer++;
    // ...
}

The cached pointer is now the only file-scope storage, and it's a single pointer — the ~19 fields all live inside Singleton<T>::instance()'s function-local storage and elide together when the I2S driver is unused.

Approach

  1. Group all 19 fields into struct I2SDriverState.
  2. Introduce the IRAM s_i2s_state cache pointer (single pointer, initialized to nullptr, set once from begin()).
  3. Rewrite begin() to call Singleton<I2SDriverState>::instance() and cache the pointer.
  4. Route every non-ISR access through Singleton<I2SDriverState>::instance().
  5. Route every ISR access through s_i2s_state->foo (raw pointer, no guard).
  6. Verify with bash autoresearch esp32dev --i2s (live device test) — this driver has hardware validation.
  7. Verify elision: bash bloat esp32dev --examples Blink before and after — Blink doesn't use I2S, so all 19 fields' worth of storage should drop.

Acceptance criteria

  • File clean of SingletonElisionChecker warnings except for s_i2s_state itself (that one is intentional — annotate with [[gnu::used]] and FL_KEEP and add to the checker allowlist with rationale).
  • I2S regression tests pass on real hardware (autoresearch).
  • Blink flash size drops by the I2S driver's data footprint.
  • No new IRAM budget breaches.

Risk

HIGH. ISR path is IRAM-critical. Test with autoresearch before merging. If the cached-pointer pattern has any window where the ISR could fire before begin() sets it, add a null-check in the ISR (already shown in the pattern above).

Activity

  1. added
    type: feature
    priority: medium
    platform: esp32
    area: drivers
    on Jul 1, 2026
  2. zackees commented on Jul 1, 2026

    @zackees
    MemberAuthor

    Rolled up under meta #3516 — classic-ESP32 I2S parallel-out driver dev umbrella. This issue will be closed automatically when #3512 Phase 6 lands (peripheral migration absorbs the singleton work).

  3. zackees commented on Jul 1, 2026

    @zackees
    MemberAuthor

    Closed by #3526 Phase 2e (PR #3564)

    The 10+ file-scope statics in i2s_esp32dev.cpp.hpp that this issue tracked (gCallback, gCntBuffer, dmaBuffers[], gTX_semaphore, gI2S_intr_handle, gPulsesPerBit, gOneBit[], gZeroBit[], gInitializedI2sInitialized, etc.) were all deleted with the Yves driver files at #3526 Phase 2e (2026-07-01):

    • src/platforms/esp/32/drivers/i2s/i2s_esp32dev.{h,cpp.hpp} — DELETED
    • src/platforms/esp/32/drivers/i2s/clockless_i2s_esp32.{h,cpp.hpp} — DELETED

    The modern I2S1 driver replaces the Yves architecture entirely — I2sPeripheralEsp32DevEsp is now a fl::Singleton<> (process-lifetime, never destroyed) with all state as private instance members. The former ownership global (g_i2s1_hardware_claimed_by_modern) was also folded into the singleton's mInitialized member.

    The FL_LINT_ALLOW_GLOBAL_FILE marker that allowlisted the Yves file for the SingletonElisionChecker is gone with the file. Grepping the current tree for the previously-flagged globals confirms zero remaining occurrences.

    The Tier 2a migration this issue tracked is complete — via file deletion rather than in-place migration, but with the same net outcome (zero file-scope statics in the classic-ESP32 I2S driver tree).

  4. moved this from Triage to Done in FastLED Trackeron Jul 1, 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: esp32priority: mediumtype: feature

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions