Repository navigation
Description
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
- Group all 19 fields into
struct I2SDriverState. - Introduce the IRAM
s_i2s_statecache pointer (single pointer, initialized tonullptr, set once frombegin()). - Rewrite
begin()to callSingleton<I2SDriverState>::instance()and cache the pointer. - Route every non-ISR access through
Singleton<I2SDriverState>::instance(). - Route every ISR access through
s_i2s_state->foo(raw pointer, no guard). - Verify with
bash autoresearch esp32dev --i2s(live device test) — this driver has hardware validation. - Verify elision:
bash bloat esp32dev --examples Blinkbefore and after — Blink doesn't use I2S, so all 19 fields' worth of storage should drop.
Acceptance criteria
- File clean of
SingletonElisionCheckerwarnings except fors_i2s_stateitself (that one is intentional — annotate with[[gnu::used]]andFL_KEEPand 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).
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
feat(lint): SingletonElisionChecker — land + drive to zero violations (…
feat(i2s-esp32dev): enable IDF 6+ via i2s_ll_* compat shim (#3511)
zackees commentedon Jul 1, 2026
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).
docs(i2s): add classic-ESP32 I2S driver README (#3512 Phase 8) (#3521)
feat(i2s): real deinitialize() on modern peripheral (#3526 Phase 2b) (#…
docs(i2s): rewrite README for modern engine (#3526 Phase 8 / #3512 Ph…
zackees commentedon Jul 1, 2026
Closed by #3526 Phase 2e (PR #3564)
The 10+ file-scope statics in
i2s_esp32dev.cpp.hppthat 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}— DELETEDsrc/platforms/esp/32/drivers/i2s/clockless_i2s_esp32.{h,cpp.hpp}— DELETEDThe modern I2S1 driver replaces the Yves architecture entirely —
I2sPeripheralEsp32DevEspis now afl::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'smInitializedmember.The
FL_LINT_ALLOW_GLOBAL_FILEmarker 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).
feat(i2s): wire wave8 encoding into engine + routeLanePin call (#3526) (
docs(i2s): document open bench debug on tx_bits_mod (#3526 / #3515) (#…
fix(i2s,rmt-rx): 32-bit sample encoding + classic-ESP32 capture fixes (…