Skip to content

singleton: migrate flexio_driver state (Tier 2b) #3490

Closed as not planned
Closed as not planned

Description

@zackees

Part of #3481 (Tier 2b).

Task

Migrate the 7 file-scope statics in src/platforms/arm/teensy/teensy4_common/drivers/flexio/flexio_driver.cpp.hpp to fl::Singleton<T> using the same cached-pointer-in-IRAM pattern as #3489.

The FlexIO ISR problem

FlexIO on Teensy 4 uses DMA + interrupt-driven RX capture (recent PR chain: #3222 FlexPWM RX byte capture, #3431 FlexIO unified engine). The ISR reads and writes driver state at high frequency. Singleton<T>::instance()'s function-local-static guard is not ISR-safe.

Required pattern

Same as #3489:

struct FlexIODriverState { /* all 7 fields */ };

static FlexIODriverState* IRAM_ATTR s_flexio_state = nullptr;

void FlexIODriver::begin() {
    auto& s = fl::Singleton<FlexIODriverState>::instance();
    s_flexio_state = &s;
    // ...
}

IRAM_ATTR static void flexio_isr(void*) {
    if (!s_flexio_state) return;
    // access s_flexio_state->foo
}

Approach

  1. Enumerate the 7 statics from the SingletonElisionChecker warn output.
  2. Collect into FlexIODriverState.
  3. Introduce the cached IRAM pointer.
  4. Route all accesses (init, teardown, ISR, user API) through the singleton or the cached pointer.
  5. Verify with bash autoresearch teensy41 --flexio (live device test — FlexIO has autoresearch coverage from the fix(teensy): restore FlexPWM RX byte capture #3222/feat(flexio): unified clockless+SPI engine scaffolding (#3428) #3431 series).
  6. Verify elision with bash bloat teensy41 --examples Blink.

Acceptance criteria

  • File clean of SingletonElisionChecker warnings except cached pointer (annotate + allowlist).
  • FlexIO autoresearch passes.
  • Blink flash size drops by FlexIO driver's data footprint on teensy41.

Coordinate with

Activity

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

    @zackees
    MemberAuthor

    Audited against the finding in #3754 / #3486: fl::Singleton elision pays off only for objects with dynamic initialization (those register .init_array entries that pull in a ctor and resist --gc-sections). It does nothing for constant-initialized PODs — builds already pass -ffunction-sections -fdata-sections (ci/boards.py:795), so those are already dropped when unreferenced. For PODs the wrap is a net loss: storage grows by a T*, every access gains a null-check, and constant-initialization (race-free) is replaced by Singleton<T>::instance()'s hand-rolled if (!ptr), which bypasses the C++11 magic-static guard.

    All 7 hits are constant-initialized PODs, and migration here is actively risky.

    flexio_driver.cpp.hpp:139-143,149-150: DMAChannel* sDmaChannel = nullptr, volatile bool sDmaComplete = true, bool sInitialized, u8 sFlexIOPin, u32 sLatchCycles, volatile u32 sDmaErrorCount/sLastDmaEs — all constant-init.

    Specific to this issue: sDmaComplete, sDmaErrorCount and sLastDmaEs are volatile and written from flexio_dma_isr() (L152). Routing ISR-touched state through Singleton<T>::instance()'s if (!ptr) placement-new is precisely the race the issue's own "cached IRAM pointer" workaround exists to dodge — and that workaround costs an extra pointer plus an indirection to protect storage -fdata-sections already handles.

    (flexio_spi_mode.cpp.hpp adds 5 more of the same class, not listed in the issue.)

    Recommend closing as not-planned. See #3492.

  3. zackees commented on Aug 20, 2026

    @zackees
    MemberAuthor

    Closing as not planned after re-validating the singleton premise against current FlexIO state.

    The seven targeted variables (sDmaChannel, sDmaComplete, sInitialized, sFlexIOPin, sLatchCycles, sDmaErrorCount, sLastDmaEs) are constant-initialized pointer/integer/bool PODs. With -fdata-sections and linker GC, their storage already elides when the FlexIO TU is unreachable; there is no dynamic initializer to remove.

    The proposed cached-pointer pattern would instead add:

    • a new retained pointer;
    • an extra indirection on every state access;
    • manual initialization ordering;
    • a second lifetime invariant across init/deinit;
    • additional ISR risk for sDmaComplete, sDmaErrorCount, and sLastDmaEs.

    The large DMAMEM sPixelBuffer also must remain explicitly placed/aligned DMA storage and is not a Singleton candidate. The sibling SPI mode contains the same class of constant-initialized, ISR-touched state, reinforcing that this is driver-owned static storage rather than dynamic-constructor bloat.

    Parent #3481 is closed, and the checker rollout intentionally distinguishes dynamically initialized globals from constant POD state. No code or hardware run is warranted for a migration that cannot produce the acceptance criterion's claimed Blink size drop and would make ISR access more fragile.

  4. moved this from Triage to Done in FastLED Trackeron Aug 20, 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: teensypriority: mediumtype: feature

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions