Skip to content

[teensy4] OBJECT_FLED 100-LED autoresearch: ~5% jitter — re-investigate post-#3342 FL_WARN audit #3343

Closed

Description

@zackees

Sub-issue of #3066 (Teensy 4 driver bringup ledger).
First identified during the FastLED #3219 session that landed PRs #3341 and #3342.

Status (updated): NEEDS FULL RE-INVESTIGATION.
Original framing called this "not solvable from software" — that was wrong.
PR #3342 (FL_WARN volume audit in AutoResearchTest.cpp) cut the device-side serial output by ~10x.
Device-side FL_WARN() BLOCKS on a full UART TX FIFO (the Teensy's hardware FIFO is ~4 bytes deep and the
Serial.write() path blocks rather than dropping). While blocked, the CPU isn't servicing other tasks —
including ObjectFLED's DMA refill. The "TX-side DMA jitter" hypothesis below was real, but the
root cause may already have been the printf flood we just fixed. This issue needs to be re-measured
on hardware against the new firmware (post-#3342) before any "bench-only" conclusion stands.

Symptom (pre-#3342 measurements)

On a Teensy 4.0 running examples/AutoResearch/AutoResearch.ino with --object-fled --strip-sizes 100, after the wrapper-side and decoder fixes:

Driver 5-LED test 100-LED test
OBJECT_FLED 4/4 patterns PASS in 13 ms ✓ 1-8 of 100 LEDs corrupt per run, RANDOM per run

Concrete per-run captures (3 consecutive runs of --object-fled --strip-sizes 100, all run BEFORE #3342 landed):

Pattern Run 1 errors Run 2 errors Run 3 errors
A (R=0xF0, G=0x0F, B=0xAA) 1 (LED 98) 1 (LED 12) 2 (LED 51, 58)
B (R=0x55, G=0xFF, B=0x00) 4 (LED 7, 43, 93, 95) 1 (LED 20) 0 ✓ PASS
C (R=0x0F, G=0xAA, B=0xF0) 0 ✓ PASS 2 (LED 73, 95) 4 (LED 8, 9, 10, 51)
D (RGB alternating) 3 (LED 28, 89, 96) 1 (LED 96) 0 ✓ PASS
Total per run 8 5 6

Different LEDs failed each run.

What changed post-#3342 that might fix this

PR #3342 cut device-side serial output volume by ~10x via three changes:

  1. AutoResearchTest.cpp per-byte capture dump: 26 FL_WARN lines/pattern → 1 collated line.
  2. dumpRawEdgeTiming() collated all edges into one FL_WARN instead of one-per-edge.
  3. Decode-failure raw-edge dump callers: EdgeRange(0, 256) → EdgeRange(0, 32).

Each FL_WARN on Teensy 4 routes through Serial.write() which fills the UART TX FIFO. When the FIFO is full, the call blocks the calling thread until space opens up. During that block, the CPU isn't running other code — including ObjectFLED's TX-DMA refill thread. Pulse widths on the wire can stretch by tens of µs while the CPU is blocked. That stretching would land bits at the midpoint between T0H and T1H, which the decoder then classifies arbitrarily.

If this is the dominant root cause, the post-#3342 firmware should pass 100-LED autoresearch reliably — or at least with dramatically fewer errors per run.

What I had originally written (kept for context)

Four candidate root causes were proposed, requiring bench instrumentation to disambiguate:

  1. TX-side ObjectFLED DMA jitter — periodic DMA refill preempted by another channel → pulse width widens by tens of ns.
  2. RX-side FlexPWM capture jitter — input-sample clock phase drifts vs pin-edge phase.
  3. Cable/signal-integrity — non-impedance-controlled jumper at 5-10 ns slew rates.
  4. TX-RX clock-domain drift — TX off 600 MHz core, RX off 150 MHz F_BUS.

The original framing said we can't tell which without a logic analyzer. That's still true for hypotheses 2-4, but hypothesis 1 has a software-domain mechanism (printf-blocking starves DMA) that we've just substantially reduced.

Updated acceptance / next steps

  1. First — re-measure on hardware against post-perf(autoresearch): collapse per-byte + per-edge FL_WARN dumps (fbuild#755) #3342 firmware.
    • Run bash autoresearch teensy40 --object-fled --strip-sizes 100 10 times.
    • Tally per-run error counts.
    • If average errors per run dropped from ~6 to <1 → root cause was the printf flood, this issue can close as fixed by perf(autoresearch): collapse per-byte + per-edge FL_WARN dumps (fbuild#755) #3342.
    • If errors persist at similar density → the printf-flood hypothesis was secondary, real bench work is needed (proceed to step 2).
  2. If hardware re-measurement still shows jitter:
    • Capture ground-truth TX waveform with a logic analyzer at GPIO 3.
    • Compare to FlexPWM-RX captures emitted by runSingleTest.
    • Classify: TX-side / RX-side / cable / clock.
    • Pick a targeted fix (raise FlexPWM RX clock, add adaptive midpoint, terminate the cable, etc.).
    • 10-run regression threshold: <1 LED error per run average.

Anti-goals (unchanged)

  • Do not widen decoder tolerances to mask the symptom. The midpoint classifier eliminates cascading errors; loosening per-bit tolerances any further makes real decode bugs invisible.
  • Do not disable patterns that fail more often. Pattern B (R=0x55, G=0xFF, B=0x00) stresses last-bit timing; killing it would hide a real failure mode.
  • Do not declare "bench-only" before re-testing against post-perf(autoresearch): collapse per-byte + per-edge FL_WARN dumps (fbuild#755) #3342 firmware. That was the original mistake.

Workaround until resolution

Cross-links

Activity

  1. changed the title [-][teensy4] OBJECT_FLED 100-LED autoresearch: ~5% per-frame jitter — needs bench characterization[/-] [+][teensy4] OBJECT_FLED 100-LED autoresearch: ~5% jitter — re-investigate post-#3342 FL_WARN audit[/+] on Jun 22, 2026
  2. zackees commented on Jun 22, 2026

    @zackees
    MemberAuthor

    Hardware research update from the ObjectFLED investigation:

    ObjectFLED on Teensy 4.x is not using FlexIO for output. The low-level path is:

    QuadTimer4 channels 0/1/2
      -> XBARA1 edge routing
      -> DMAMUX request sources XBAR1_0, XBAR1_1, XBAR1_2
      -> eDMA channels dma1, dma2, dma3
      -> GPIOx DR_SET / DR_CLEAR registers
    

    The vendored ObjectFLED source routes QTIMER4 through XBARA1 like this:

    xbar_connect(XBARA1_IN_QTIMER4_TIMER0, XBARA1_OUT_DMA_CH_MUX_REQ30);
    xbar_connect(XBARA1_IN_QTIMER4_TIMER1, XBARA1_OUT_DMA_CH_MUX_REQ31);
    xbar_connect(XBARA1_IN_QTIMER4_TIMER2, XBARA1_OUT_DMA_CH_MUX_REQ94);
    
    dma.dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_XBAR1_0);
    dma.dma2.triggerAtHardwareEvent(DMAMUX_SOURCE_XBAR1_1);
    dma.dma3.triggerAtHardwareEvent(DMAMUX_SOURCE_XBAR1_2);

    Teensy core confirms those constants in imxrt.h, xbar_connect() programs XBARA1 selector registers in pwm.c, and DMAChannel::triggerAtHardwareEvent() programs DMAMUX_CHCFGn in DMAChannel.h.

    The three DMA channels have distinct jobs:

    • dma1: start-of-bit set, writing the collective pin mask to GPIOx_DR_SET.
    • dma2: zero-bit falling edge, writing expanded per-bit clear masks from bitdata to GPIOx_DR_CLEAR. This channel owns the major-loop interrupt and scatter/gather refill path.
    • dma3: one-bit falling edge, writing the collective pin mask to GPIOx_DR_CLEAR; this is also used as the transfer-complete indicator.

    BYTES_PER_DMA=20 is not a hardware DMA capacity limit. It is the number of LED data bytes expanded per refill block, not the number of DMA bytes:

    #define BYTES_PER_DMA 20
    DMAMEM uint32_t bitdata[BYTES_PER_DMA * 64];

    So the expanded transmit buffer is:

    20 * 64 * sizeof(uint32_t) = 5120 bytes
    

    At 800 kHz WS2812 timing, one refill block covers roughly:

    20 LED bytes * 8 bits/byte * 1.25 us/bit = about 200 us
    

    show() initially fills up to BYTES_PER_DMA * 2, so with 20 there is about 400 us of initial runway. During the rest of the transfer, the ISR must refill a half-buffer about every 200 us.

    The i.MX RT1060 eDMA engine has 32 DMA channels and DMA_MUX can route up to 128 request sources; PJRC also lists Teensy 4.0/4.1 as having 32 DMA channels. So the value 20 is not because the peripheral can only handle 20-ish bytes.

    The source comment explains the actual tradeoff: larger values tolerate more interrupt latency, but grow the transmit buffer, and the buffer should stay below about half the Cortex-M7 data cache. Teensy 4.x has a 32 KB D-cache, so half is 16 KB.

    Comparison:

    ObjectFLED vendored here:       BYTES_PER_DMA=20 ->  5120 bytes -> ~200 us refill cadence
    PJRC OctoWS2811 Teensy 4 path:  BYTES_PER_DMA=40 -> 10240 bytes -> ~400 us refill cadence
    Hypothetical:                   BYTES_PER_DMA=60 -> 15360 bytes -> ~600 us refill cadence
    Hypothetical:                   BYTES_PER_DMA=80 -> 20480 bytes -> above half-cache guideline
    

    Why this matters for this issue:

    ObjectFLED is advertised upstream as non-blocking DMA output. Locally, ObjectFLED::show() starts timers/DMA, records update_begin_micros, and returns while the DMA/ISR pipeline continues. It also provides busy() and waitForDmaToFinish().

    That means code after FastLED.show() can overlap the active LED waveform unless FastLED's channel wrapper explicitly waits for ObjectFLED completion. Treating ObjectFLED as synchronous can make diagnostics after FastLED.show() run while DMA refills are still required.

    For a 100-LED strip, the wire time is about:

    100 LEDs * 24 bits * 1.25 us = about 3 ms
    

    With BYTES_PER_DMA=20, the refill ISR needs service about every 200 us during that transfer. Blocking serial writes, long diagnostics, interrupt masking, or competing DMA/cache/bus activity during that window are therefore plausible software-domain causes of occasional stretched output timing.

    This strengthens the post-#3342 re-test requirement: if reducing FL_WARN volume eliminates the failures, the likely mechanism is not generic "DMA can't do it" but CPU/ISR starvation during ObjectFLED's refill cadence.

    Source notes captured locally in this branch: src/third_party/object_fled/README.md.

    Primary references:

  3. zackees commented on Jun 27, 2026

    @zackees
    MemberAuthor

    Closing -- the FL_WARN audit conclusion landed in commit 6a46bb2 on branch feat/lpuart-ws2812-driver (PR opening as follow-up).

    Root cause confirmed: FL_WARN macro unconditionally constructs fl::sstream() << X regardless of log level. Under autoresearch's fl::ScopedLogDisable wrapper, the cumulative fl::sstream/fl::string heap alloc/free cycles across the test framework's many FL_WARN sites leave the Teensy 4 heap in a state where the function-exit destructor of a heap-backed fl::string hangs indefinitely (~80% of runs).

    Fix: AR_FL_WARN / AR_FL_ERROR macros that check fl::getLogLevel() before expanding -- when under ScopedLogDisable (level=NONE), the sstream is never constructed and the heap stays clean. Swept all 73 FL_WARN sites in AutoResearchTest.cpp and 8 in AutoResearchHelpers.cpp.

    Validation: LPUART autoresearch went from 1/5 to 5/5 success with the fix. FlexIO improved from 0/5 to 1-2/5 (residual heap pressure outside this file; tracked separately). When tests do complete, byte corruption stays at the documented 0.3-2.0% noise floor matching this issue's ~5% jitter envelope -- so the apparent jitter was the destructor-hang masquerading, not actual signal corruption.

    Marker-narrowed investigation transcript and per-marker bisection results captured in the commit message for 6a46bb2.

  4. moved this from Triage to Done in FastLED Trackeron Jun 27, 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

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions