Skip to content

fix(teensy-rx): eliminate intermittent whole-frame bit-shift via ARMA re-arm + single-chunk DMA - #3409

Merged
zackees merged 1 commit into
masterfrom
experiment/bytes-per-dma-150-no-chunk
Jun 26, 2026
Merged

zackees merged 1 commit into
masterfrom
experiment/bytes-per-dma-150-no-chunk

Conversation

@zackees

@zackees zackees commented Jun 26, 2026 •

Copy link
Copy Markdown
Member

🎯 Eliminated catastrophic whole-frame 1-bit-shift corruption

Round-4 investigation on #3406 uncovered a distinct second bug hiding behind the residual byte corruption: ~14% of runs had a catastrophic whole-frame 1-bit shift on ONE pattern, where the IOMUXC pad-mux switch in configureFlexPwm latched a phantom capture pair before the real TX started — and the existing phantom-leading-pair skip missed it because the phantom was followed by a short bit gap (< 5 µs) instead of an inter-frame reset gap.

Signature of the failure:

expected (0xF0, 0x0F, 0xAA) decoded as (0xE0, 0x1F, 0x55) for ALL 100 LEDs in the failing pattern
-> ~23% aggregate byte corruption per affected run, ~1 in 7 runs

Two coupled fixes

1. ARMA re-arm in configureDma() — wait 50 µs after the FlexPWM submodule is armed, then toggle the capture circuit's ARMA bit off/on, then enable DMA. The settle delay lets any pad-mux spike pass through the FlexPWM input, and the ARMA toggle flushes whatever was latched into CVAL2/CVAL3 so DMA's first read is the real first WS2812 bit.

2. BYTES_PER_DMA 120 → 150 — makes the canonical 100-LED test (300 bytes) fit in a single major loop (numbytes ≤ BYTES_PER_DMA*2 = 300), bypassing the ESG/ISR refill chain entirely. Reduces both the refill-race attack surface and the absolute corruption floor.

Hardware result (10 consecutive runs)

bash autoresearch teensy41 --object-fled --tx-pin 22 --rx-pin 8 --timeout 60 --skip-lint

Run 1: 0.5%   Run 2: 0.5%   Run 3: 0.5%   Run 4: 0.6%   Run 5: 0.8%
Run 6: 0.3%   Run 7: 0.3%   Run 8: 0.4%   Run 9: 0.3%   Run 10: 1.0%

Max:     1.0% (was 22-26% with frame-shift events)
Mean:    0.52%
Catastrophic frame-shift events: 0 (was ~14% incidence)

Driver is now reliably in the 0.3-1.0% per-frame byte-corruption band. The previous catastrophic 22-26% events are eliminated.

Test plan

  • bash test --unit → 254/254 passed
  • bash lint --cpp → clean
  • 10× canonical autoresearch → no frame-shift events, max 1.0%, avg 0.52%

Refs: #3406

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved DMA capture reliability on supported hardware by reducing the chance of an initial spurious reading.
    • Adjusted DMA chunk handling to help stabilize LED data transfers and reduce corruption during updates.

…MA re-arm + DMA single-chunk path

Two changes that together eliminate the ~14% incidence of CATASTROPHIC
whole-frame 1-bit-shift corruption observed in #3406 Round-4:

1. configureDma() now waits 50 us after FlexPWM submodule is armed
   (configureFlexPwm sets MCTRL_RUN), then toggles ARMA off/on, then
   enables DMA. The IOMUXC pad-mux switch in configureFlexPwm can
   latch a spurious capture pair into CVAL2/CVAL3 BEFORE the real TX
   starts. The settle delay + ARMA re-arm flushes the spike so the
   first DMA capture is the real first WS2812 bit, not the spike.
   Without this, ~1 in 7 frames had its phantom-leading-pair skip
   logic miss (because the spike was followed by a short LOW < 5 us,
   not the expected inter-frame reset gap), shifting every decoded
   byte by 1 bit. Signature: expected (0xF0, 0x0F, 0xAA) decoded as
   (0xE0, 0x1F, 0x55) for ALL 100 LEDs in the failing pattern, ~23%
   aggregate byte corruption per affected run.

2. ObjectFLEDDmaManager BYTES_PER_DMA bumped 120 -> 150 so the
   canonical 100-LED test (300 bytes) fits in a single major loop
   (numbytes=300 <= BYTES_PER_DMA*2=300), bypassing the ESG/ISR
   refill chain entirely. Reduces both the refill-race attack
   surface and the absolute corruption floor (avg ~0.5% with
   single-chunk vs ~0.6% with chunked).

Validation:
- bash test --unit -> 254/254 passed
- bash lint --cpp -> clean
- bash autoresearch teensy41 --object-fled --tx-pin 22 --rx-pin 8
  --timeout 60 --skip-lint, 10 consecutive runs: 0.5%, 0.5%, 0.5%,
  0.6%, 0.8%, 0.3%, 0.3%, 0.4%, 0.3%, 1.0%. Max 1.0%, average 0.52%.
  ZERO catastrophic frame-shift events (vs ~14% baseline incidence
  with ~23% byte corruption when they hit).

Driver is now reliably in the 0.3-1.0% byte-corruption band per
frame; the previous 22-26% catastrophic shift events are eliminated.

Refs: #3406
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Jun 26, 2026
@zackees
zackees merged commit e420987 into master Jun 26, 2026
6 of 10 checks passed
@zackees
zackees deleted the experiment/bytes-per-dma-150-no-chunk branch June 26, 2026 04:24
@coderabbitai

coderabbitai Bot commented Jun 26, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6e53a974-e598-4a9f-ab82-245094e1088c

📥 Commits

Reviewing files that changed from the base of the PR and between b63652a and bcb97f2.

📒 Files selected for processing (2)
  • src/platforms/arm/teensy/teensy4_common/rx_flexpwm_channel.cpp.hpp
  • src/third_party/object_fled/src/ObjectFLEDDmaManager.h

📝 Walkthrough

Walkthrough

This PR changes Teensy 4 DMA behavior in two places: ObjectFLED now uses a 150-byte refill chunk, and FlexPWM RX now waits 50 µs, re-arms the capture circuit, and then enables DMA.

Changes

Teensy DMA timing tweaks

Layer / File(s) Summary
DMA refill chunk size
src/third_party/object_fled/src/ObjectFLEDDmaManager.h
BYTES_PER_DMA changes from 120 to 150, and nearby comments describe the Teensy 4.x experiment.
FlexPWM DMA re-arm
src/platforms/arm/teensy/teensy4_common/rx_flexpwm_channel.cpp.hpp
configureDma() adds a 50 µs delay, re-arms the FlexPWM capture circuit for channel A or B, and then enables DMA.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

  • FastLED/FastLED#3341 — Also adjusts Teensy 4 FlexPWM RX arm timing and capture correctness in rx_flexpwm_channel.cpp.hpp.
  • FastLED/FastLED#3407 — Also changes Teensy 4.x FlexPWM RX capture/DMA initialization, including capture flag handling and cache-related setup.

Poem

I hopped past the moon with a whisker of glee,
nudging DMA timings so spikes run free.
One tiny pause, then a capture re-arm,
and buffers stay cozy, unspoiled from harm. 🐇

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch experiment/bytes-per-dma-150-no-chunk

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant