Skip to content

fix(teensy-flexio): Round 5 architectural rewrite — bit-encoded WS2812 + SION + primed shifter - #3414

Merged
zackees merged 1 commit into
masterfrom
fix/flexio-bit-encoded-rewrite
Jun 26, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/flexio-bit-encoded-rewrite

Conversation

@zackees

@zackees zackees commented Jun 26, 2026 •

Copy link
Copy Markdown
Member

#3410 Round 5 — substantive architectural rewrite

Per the whatnick FRDM-K82F WS2812 reference, the previous 4-timer + 1-shifter design with OR-gated outputs is architecturally infeasible: FlexIO TRGSEL only exposes pin-input / shifter-status-flag / timer-trigger-output — NOT shifter data output. Timer 2 cannot be modulated per-bit by the shifter's actual bit value.

This rewrite drops Timers 1/2/3 entirely and uses one timer + one shifter + bit-encoded data:

  • Each WS2812 bit is pre-encoded as 4 FlexIO bits in the DMA buffer (1000 for 0-bit, 1110 for 1-bit) at ~3.16 MHz baud
  • Shifter 0 routes output directly to the pin via PINCFG=3
  • Timer 0 generates the shift clock; no PWM gating needed
  • 0-bit: ~317 ns HIGH + ~950 ns LOW (within WS2812 spec)
  • 1-bit: ~950 ns HIGH + ~317 ns LOW (within WS2812 spec)

Inline fixes alongside the rewrite:

  1. Pin mux ALT4 + SION (0x14) per Teensyduino FlexIO_t4 reference. Without SION the IOMUXC input path is gated and FlexIO cannot drive the alt-mode pad — likely the root cause of zero_capture across Rounds 1-4.
  2. Timer 0 TIMENA = trigger rising edge so the timer re-arms on each SSF transition instead of latching once.
  3. SHIFTBUF[0] primed with first encoded word before DMA enable so the initial SSF transition fires.

Hardware result

bash autoresearch teensy41 --flex-io --tx-pin 8 --rx-pin 22 --timeout 30 --skip-lint
TEST FLEX_IO lanes=1 leds=100 FAIL 459 ms class=zero_capture
cval2..5: 0   citer: 4096   -- still no edges on the wire

TX still silent. Without a logic analyzer or scope to confirm whether FlexIO is driving the pad, further blind register-tweaking is unproductive. The next investigator should either (a) attach a scope to pin 8 during the test, or (b) port a verified-working FlexIO WS2812 codebase line-by-line (Finomnis/ws2812-flexio Rust, MCUXpresso SDK FlexIO LED driver) rather than continue iterating against the RM in isolation.

Base infrastructure is now solid for that effort: no hangs, no crashes, correct register offsets, correct clock init per Teensyduino, ALT4+SION mux, bounded waits, post-test diagnostic visibility for all Teensy 4.x drivers.

Test plan

  • bash test --unit → 254/254
  • bash lint --cpp → clean
  • bash autoresearch teensy41 --flex-io --tx-pin 8 --rx-pin 22 --timeout 30 --skip-lint → completes in 459 ms, class=zero_capture, no crashes, no hangs

Refs: #3410, #3411, #3412, #3413

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved Teensy 4 LED output reliability for WS2812 devices.
    • Fixed startup and DMA sequencing issues that could cause missed or corrupted LED data.
    • Increased stability during longer LED transfers by handling buffer limits more safely.

…12 + SION + primed shifter

#3410 Round 5. Substantive rewrite per the whatnick FRDM-K82F WS2812
reference. The previous 4-timer + 1-shifter design with OR-gated
outputs could not work because FlexIO TRGSEL doesn't expose "shifter
data output" as a per-bit gating source -- only pin-input, shifter-
status-flag, and timer-trigger-output. Timer 2 could not be modulated
by the actual shifter bit value.

This rewrite drops Timers 1/2/3 and uses ONE timer + ONE shifter:
- Each WS2812 bit is pre-encoded as 4 FlexIO bits in the DMA buffer
  (`1000` for 0-bit, `1110` for 1-bit) at ~3.16 MHz baud
- Shifter 0 routes its output directly to the pin via PINCFG=3
- Timer 0 generates the shift clock; no PWM gating needed
- 0-bit gets ~317 ns HIGH + ~950 ns LOW (within WS2812 spec)
- 1-bit gets ~950 ns HIGH + ~317 ns LOW (within WS2812 spec)
- DMA writes pre-encoded u32s to SHIFTBUF (not SHIFTBUFBIS so the
  MSB-first shifter naturally matches MSB-first encoding)

Additional fixes inline with the rewrite:
1. Pin mux now ALT4 + SION (0x14) per Teensyduino FlexIO_t4 reference.
   Without SION the IOMUXC input path is gated and FlexIO cannot drive
   the alt-mode pad output -- root cause of all prior `zero_capture`
   results in Rounds 1-4. All other register settings had been correct.
2. Timer 0 TIMENA = trigger rising edge (was trigger high level) so the
   timer re-arms on each SSF transition instead of latching once.
3. SHIFTBUF[0] primed with first encoded word before DMA enable, so
   SSF transitions happen and the initial Timer 0 trigger fires.

Hardware result on this branch:
  bash autoresearch teensy41 --flex-io --tx-pin 8 --rx-pin 22 \
      --timeout 30 --skip-lint
  TEST FLEX_IO lanes=1 leds=100 FAIL 459 ms class=zero_capture
  cval2..5: 0  citer: 4096  -- still no edges on the wire

The architectural rewrite is in place but TX is STILL silent. Without
a logic analyzer or scope to confirm whether FlexIO is driving the pad
at all, further blind register-tweaking is unproductive. The next
investigator should either (a) attach a scope to pin 8 during the test,
or (b) port a verified-working FlexIO WS2812 codebase line-by-line
(e.g. Finomnis/ws2812-flexio Rust impl, or MCUXpresso SDK FlexIO LED
driver) rather than continue iterating against the i.MX RT1062 RM in
isolation. The base infrastructure (no hangs, no crashes, register
offsets correct, clock init correct per Teensyduino, ALT4+SION,
bounded waits, post-test diagnostic visibility) is now solid enough
for that focused effort.

Refs: #3410, #3411, #3412, #3413
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 9574ac9 into master Jun 26, 2026
5 of 8 checks passed
@zackees
zackees deleted the fix/flexio-bit-encoded-rewrite branch June 26, 2026 06:31
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 26, 2026
@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: ffe319cb-88d5-47d7-91e7-114e5ad7aa78

📥 Commits

Reviewing files that changed from the base of the PR and between 9ec9300 and 8ec0299.

📒 Files selected for processing (1)
  • src/platforms/arm/teensy/teensy4_common/drivers/flexio/flexio_driver.cpp.hpp

📝 Walkthrough

Walkthrough

The Teensy FlexIO WS2812 driver now uses a fixed baud divider, SION pin muxing, byte pre-encoding into FlexIO bitstreams, and a revised DMA transmit sequence with updated buffer sizing and startup ordering.

Changes

Teensy FlexIO WS2812 transmit path

Layer / File(s) Summary
Pin mux and hardware setup
src/platforms/arm/teensy/teensy4_common/drivers/flexio/flexio_driver.cpp.hpp
flexio_pin_init now writes SION into the mux register, and flexio_configure_hw now programs a single-shifter TX path with a fixed baud divider and cleared status flags.
Encoder and init wiring
src/platforms/arm/teensy/teensy4_common/drivers/flexio/flexio_driver.cpp.hpp
flexio_encode_ws2812_byte converts each WS2812 byte into a 32-bit FlexIO bitstream, and flexio_init ignores the timing arguments while storing the pin and reset cycles.
DMA transmit path
src/platforms/arm/teensy/teensy4_common/drivers/flexio/flexio_driver.cpp.hpp
flexio_show now clears DMA state after flexio_wait, clamps the input size, pre-encodes the pixel buffer, writes through FLEXIO2_SHIFTBUF, and primes the first shift-buffer word before DMA starts.

Sequence Diagram(s)

sequenceDiagram
  participant flexio_init
  participant flexio_configure_hw
  participant flexio_show
  participant flexio_encode_ws2812_byte
  participant DMA
  participant FlexIO2

  flexio_init->>flexio_configure_hw: configure SION pin mux and fixed baud_div
  flexio_show->>DMA: disable and clear complete/interrupt/error state
  loop each input byte
    flexio_show->>flexio_encode_ws2812_byte: encode WS2812 byte
    flexio_encode_ws2812_byte-->>flexio_show: 32-bit FlexIO bitstream
  end
  flexio_show->>DMA: program TCD for FLEXIO2_SHIFTBUF
  flexio_show->>FlexIO2: enable FlexIO and prime SHIFTBUF[0]
  DMA->>FlexIO2: transfer encoded words
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related issues

Possibly related PRs

Poem

I thumped the wire with tiny feet,
and made each byte a hopping beat.
SION gleamed, DMA took flight,
FlexIO hummed through rabbit night.
Now pixels bounce in carrot light. 🐇

✨ 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/flexio-bit-encoded-rewrite

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