Skip to content

fix(teensy): tier-1 defensive hardening from 6-agent #3406 audit - #3407

Merged
zackees merged 1 commit into
masterfrom
fix/teensy-rx-cache-align-and-captcompa-clear
Jun 26, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/teensy-rx-cache-align-and-captcompa-clear

Conversation

@zackees

@zackees zackees commented Jun 26, 2026 •

Copy link
Copy Markdown
Member

Tier-1 defensive correctness from the #3406 multi-agent audit

Two undefined-behavior hazards the audit identified — does NOT measurably move the 0.6 % byte corruption (verified empirically) but worth landing because both are genuine correctness fixes.

1. FlexPWM RX D-cache invalidate rounded to cache-line boundaries

arm_dcache_delete(mCaptureBuffer.data(), captures_written * sizeof(u16)) was passed an arbitrary byte count. Cortex-M7 cache maintenance requires 32-byte-aligned address + size; a partial-line invalidate leaves 1-31 stale bytes adjacent. New code rounds start down + end up to cache-line boundaries.

2. ObjectFLED::bitmaskLocal is alignas(32)

The 16-byte arm_dcache_flush_delete in begin() now operates on a single whole cache line rather than potentially straddling two partially.

3. CAPTCOMPA / CAPTCOMPB write-1-to-clear semantics

Per i.MX RT1062 RM eFlexPWM chapter, CAPTCOMPA[CFA, CAE] (bits 0:1) are write-1-to-clear capture-occurred flags. Driver was writing 0 which has no effect — a stale flag from a previous capture session could produce a mismatched CVAL2/CVAL3 pair on the first read of the next session. Now writes 0x3 to actually clear.

Hardware result

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

Run 1: byte corruption 0.4 % (4/900)
Run 2: byte corruption 0.7 % (8/1200)
Run 3: byte corruption 0.5 % (6/1200)

Unchanged from baseline ~0.5-0.7 %. Root cause is in Tier 2 (decoder dual-phase validation) or Tier 3 (ISR memset scope / DMA priority) — to follow.

Test plan

  • bash test --unit → 254/254 passed
  • bash lint --cpp → clean
  • Hardware: 3 consecutive autoresearch runs, no regression.

Refs: #3406 #3359

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved Teensy 4.x capture reliability by preventing stale input-capture state from affecting new measurements.
    • Fixed occasional incorrect edge timing results caused by cache-related stale data during DMA reads.
    • Reduced random bit flips and mismatched rising/falling capture pairs in RX capture sessions.

Two correctness fixes that the multi-agent audit flagged as plausible
root causes for the residual 0.5-0.7 % byte corruption in #3406. Bench
re-run showed Tier-1 alone does NOT measurably move the corruption
rate, so the root cause must be elsewhere (Tier 2+ to follow). These
fixes are still worth landing because they remove undefined-behavior
hazards.

1. FlexPWM RX D-cache invalidate is now cache-line aligned
   (rx_flexpwm_channel.cpp.hpp::buildEdgeTimesFromCaptures). Previous
   call passed `captures_written * sizeof(u16)` -- arbitrary byte
   count -- to arm_dcache_delete. Cortex-M7 cache maintenance is only
   safe with 32-byte-aligned address + size; partial-line invalidate
   leaves 1-31 stale bytes adjacent to the region. New code rounds
   start address down and end address up to cache-line boundaries
   before calling arm_dcache_delete.

2. ObjectFLED::bitmaskLocal is now alignas(32) so its 16-byte
   arm_dcache_flush_delete in begin() operates on a single whole
   cache line rather than potentially straddling two partially.

3. FlexPWM capture-occurred flags (CAPTCOMPA/B bits 0:1 = CFA/CAE,
   CFB/CBE) are write-one-to-clear per i.MX RT1062 RM eFlexPWM
   chapter. Driver was writing 0 (no effect). Now writes 0x3 to
   actually clear them so a stale flag from a previous capture
   session can't produce a mismatched CVAL2/CVAL3 pair on the first
   read of the next session.

Validation:
- bash test --unit -> 254/254 passed
- bash lint --cpp -> clean (PlatformIO-internal-usage warn-only)
- bash autoresearch teensy41 --object-fled --tx-pin 22 --rx-pin 8
  --timeout 60 --skip-lint, 3 consecutive runs: 0.4 %, 0.7 %, 0.5 %
  byte corruption (no regression from baseline ~0.5-0.7 %).

Refs: #3406 #3359
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@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: a7b2b793-e46b-4236-9ccd-cf2a1dad2007

📥 Commits

Reviewing files that changed from the base of the PR and between 72184c0 and 65fd577.

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

📝 Walkthrough

Walkthrough

Teensy 4.x RX capture now clears stale FlexPWM capture flags before starting and invalidates DMA capture buffers on 32-byte cache-line boundaries. The related ObjectFLED buffer is explicitly 32-byte aligned.

Changes

Teensy RX capture cache correctness

Layer / File(s) Summary
Clear stale capture flags
src/platforms/arm/teensy/teensy4_common/rx_flexpwm_channel.cpp.hpp
CAPTCOMPA and CAPTCOMPB are cleared with write-1-to-clear values before capture sessions start.
Align DMA cache invalidation
src/platforms/arm/teensy/teensy4_common/rx_flexpwm_channel.cpp.hpp, src/third_party/object_fled/src/ObjectFLED.h
DMA capture buffer invalidation expands to 32-byte cache-line boundaries, and bitmaskLocal[4] is explicitly 32-byte aligned.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐇 I hopped through flags and cache-line glow,
Cleared stale bits so captures flow.
Thirty-two bytes keep the bytes in tow,
Now every bunny beat lands just so.

✨ 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 fix/teensy-rx-cache-align-and-captcompa-clear

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.

@zackees
zackees merged commit 96a9608 into master Jun 26, 2026
6 of 10 checks passed
@zackees
zackees deleted the fix/teensy-rx-cache-align-and-captcompa-clear branch June 26, 2026 02:37
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 26, 2026
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