Skip to content

FlexIO SPI: track active transfer state for accurate wire-drain timeout #3433

Open

Description

@zackees

Background

CodeRabbit flagged this on PR #3431 (#3431 (comment)) as a Major / Heavy-lift Stability concern; deferring to a dedicated follow-up so it can land with its own coverage.

Problem

In src/platforms/arm/teensy/teensy4_common/drivers/flexio/flexio_spi_mode.cpp.hpp:

  • flexio_spi_wait() currently returns as soon as sSpiDmaComplete is set (DMA major-loop ISR has fired), then does a bounded 5 ms shifter-drain wait. Fast short transfers can still have the final byte clocking on the wire when the caller continues.
  • The fixed 50 ms DMA timeout is sized for the fastest SCLK. At the effective ~234 kHz floor (when baud_div_field is clamped to 0xFF), a 4096-byte frame takes ~140 ms and would be force-recovered mid-transfer.

Fix sketch

  1. In flexio_spi_show() store:
    • sSpiActiveBytes = num_bytes;
    • sSpiActiveBitClockHz = <effective clock derived from baud_div_field>;
  2. In flexio_spi_wait():
    • Always drain the shifter/timer after DMA completion when a transfer was launched.
    • Derive the DMA + drain timeout from sSpiActiveBytes * 8 * 1000 / sSpiActiveBitClockHz plus a slack factor (e.g. 2x), with a sane floor and ceiling, instead of a hard-coded 50 ms.
    • Keep the existing force-recovery behavior, but trigger it only after the derived timeout, not the fixed one.

Test plan

  • Extend ci/autoresearch/test_flexio_spi_smoke.py with a slow-clock case (e.g. 250 kHz x 1024 B, expected ~33 ms) to confirm no force-recovery is triggered.
  • Add a fast-clock case (12 MHz x 64 B, expected <0.1 ms) to confirm the wire-drain catches the tail byte before deinit.
  • Hardware-verify on Teensy 4.1 (the same setup used for feat(flexio): unified clockless+SPI engine scaffolding (#3428) #3431's 3/3 smoke-test pass).

Refs

Activity

  1. zackees commented on Aug 20, 2026

    @zackees
    MemberAuthor

    Deferred under the active bug-burn-down policy after reproducing the defect in current source and checking the attached hardware inventory.

    Current source evidence:

    • flexio_spi_wait() still begins with if (sSpiDmaComplete) return;, so it can skip the timer/shifter drain once the major-loop ISR has already fired.
    • The DMA wait still uses a fixed timeout_ms = 50, independent of bytes launched and effective SCLK.
    • flexio_spi_show() records neither active byte count nor effective clock, so a derived deadline cannot currently be computed.
    • At the clamped divider floor (~234 kHz), the issue's 4,096-byte example needs about 140 ms on the wire and can still be force-recovered mid-transfer.
    • The AutoResearch smoke matrix still contains only 4 B @ 1 MHz, 16 B @ 6 MHz, and 64 B @ 12 MHz; it has no slow 1,024/4,096-byte regression case.

    Hardware gate:

    • A fresh fbuild port scan shows no selectable PJRC device. COM20/COM19/COM21/COM7 are phantom Windows records (present=no); only ESP32-C6 COM9 and RP-class COM18 are healthy.
    • This is a Teensy 4.x FlexIO/eDMA/timer correctness issue. Per the hardware-validation gate it cannot be closed from host analysis alone; acceptance requires Teensy 4.1 wire/drain evidence.

    Restart criteria:

    1. Attach a selectable Teensy 4.1 with the FlexIO SPI loopback/scope setup.
    2. Track active-transfer state: launched bytes, effective clock after divider clamp, and whether wire drain is pending.
    3. Compute an overflow-safe deadline from ceil(bytes * 8 / effective_hz) plus bounded slack; keep a sane floor/ceiling.
    4. Drain timer/shifter state whenever a transfer was launched, even when DMA completion was observed before entering wait().
    5. Add slow-clock/long-frame and fast-clock/tail-drain AutoResearch cases plus host-testable timeout arithmetic.
    6. Verify the emitted wire duration and final byte on Teensy 4.1, then remove deferred and land the focused PR.

    The issue remains open and is excluded from the actionable queue while labeled deferred.

  2. added
    deferred
    on 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

    deferred

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions