Skip to content

fix(my9221): enforce DDR setup/phase and latch/stop timing (#5066) - #5371

Merged
zackees merged 1 commit into
masterfrom
fix/5066-my9221-timing
Nov 21, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/5066-my9221-timing

Conversation

@zackees

@zackees zackees commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Fixes #5066

Problem

MY9221Controller (src/fl/chipsets/my9221.h) drives DI/DCKI through FastPin. Until now it relied on incidental GPIO and compiler overhead for every datasheet interval. Nothing enforced DI setup/hold, DCKI phase width, the four latch-pulse widths, or the cascade stop interval.

Fix

This adds explicit minimum waits using the existing fl::delayNanoseconds primitives. They are the same ones clockless_blocking.h uses on every platform, AVR included. The values follow the MY-Semi MY9221 datasheet v1.0, pp 6-7 and 15:

Interval Datasheet Chosen
DI setup before each DCKI edge >= 10 ns delayNanoseconds<10>
DCKI pulse duration (each DDR phase); also covers DI hold >= 10 ns >= 50 ns delayNanoseconds<50> after each toggle
DCKI quiet before latch > 220 us unchanged 230 us
Latch DI pulse high > 70 ns 80 ns
Latch DI pulse low > 230 ns 240 ns
Cascade stop after latch > 200 ns + N*10 ns delayNanoseconds(210 + 10*N), where N = chips shifted this frame

Dual-edge operation, command/payload order, RGB mapping, zero padding of a partial chip and the latch sequence are unchanged. The hot path adds two constant waits per bit.

The stub delayMicroseconds now also reports the requested wait, in ns, to the existing fl::stub::setNanosecondDelayCallback observer. Host tests can then see the 230 us latch quiet time. This is a host-only change with no production effect.

Test (TDD)

New case in tests/fl/channels/spi_legacy_golden.cpp: MY9221 controller enforces DDR setup/hold, clock phase and latch timing. FastPin<92>/<93> specializations, the same pattern as the existing #4636 MY9221 wire capture, record each DI/DCKI write on a single timeline. Model time advances only through requested waits, observed with the existing nanosecond-delay callback. GPIO writes cost 0 ns, so incidental latency can never satisfy a datasheet interval. The case drives the production controller from FastLED.addLedsSpiChannel<MY9221,92,93,RGB> with 17 LEDs (N = 5 chips, stop bound 250 ns). It runs two frames and checks the following:

  • 1040 DCKI edges; setup >= 10, hold >= 10 and phase >= 50 at every edge
  • DCKI stays parked low through the latch, with > 220 us between the last DCKI edge and the first latch rise
  • exactly 4 latch pulses, each high > 70 ns and low > 230 ns
  • the last latch fall to the next DI/DCKI transition > 200 + 10*N ns

RED (unchanged my9221.h; bash test spi_legacy_golden, then again with --debug: same failures, no sanitizer reports):

spi_legacy_golden.cpp:868: 0 >= 10    (setup)
spi_legacy_golden.cpp:869: 0 >= 10    (hold)
spi_legacy_golden.cpp:870: 0 >= 50    (DCKI phase)
spi_legacy_golden.cpp:871: 0 > 70     (latch high)
spi_legacy_golden.cpp:872: 0 > 230    (latch low)
spi_legacy_golden.cpp:873: 0 > 250    (cascade stop, N=5)

GREEN (with the fix, and again after rebasing on origin/master): All tests passed (1/1).

Mutation: deleting only the cascade-stop delayNanoseconds(...) line fails the focused test with spi_legacy_golden.cpp:873: 250 > 250. With N = 5, the 240 ns latch-low wait alone cannot meet the chain-dependent bound. Restoring the header byte-for-byte (cmp) passes again.

bash lint: all stages pass.

Relationship to #5058

This PR supersedes the MY9221 slice of #5058 (src/fl/chipsets/my9221.h) as a small, independent change. It does not include #5058's platform emitters, frame pre-staging or IRQ masking.

Not claimed / out of scope

  • No physical or electrical measurement was made; no MY9221 strip was available. The waits are minimums: each platform's delayNanoseconds rounds up to whole cycles.
  • The datasheet's 7200 ns maximum DCKI phase is not enforced. Pixel scaling between chips, or interrupts, can stretch a phase on slow MCUs. Bounding that would need frame pre-staging and IRQ masking, which Optimize generic software SPI and audit platform coverage #5058 explores.
  • No in-tree example instantiates MY9221; Blink.ino only mentions it in a comment. A board compile would not exercise this controller, so none was run.

Summary by CodeRabbit

  • Bug Fixes
    • Improved MY9221 LED controller signal timing, including data setup, clock phases, latch pulses, and cascade-stop intervals. This helps ensure more reliable updates when driving cascaded LED chips.

Add explicit fl::delayNanoseconds waits for DI setup (10 ns), DCKI phase
(50 ns, also covering DI hold), the four latch DI pulses (80 ns high,
240 ns low) and the cascade stop interval (210 ns + N*10 ns) per the
MY9221 datasheet. The stub delayMicroseconds now reports to the existing
nanosecond-delay observer so host tests see the 230 us latch quiet time.
Adds a host timing-oracle test in spi_legacy_golden.
@zackees
zackees enabled auto-merge November 21, 2026 23:02
@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

Note

.coderabbit.yaml has unrecognized properties

CodeRabbit is using all valid settings from your configuration. Unrecognized properties (listed below) have been ignored and may indicate typos or deprecated fields that can be removed.

Parsing warnings: 1

Validation error: Unrecognized key: "enabled" at "reviews.pre_merge_checks"
Configuration instructions

  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
Walkthrough

The MY9221 controller now enforces data, clock, and latch timing with explicit delays. It counts transmitted chips to calculate the final cascade-stop interval. A host timing test records GPIO transitions and checks protocol intervals across two frames.

Changes: MY9221 timing enforcement

Layer / File(s) Summary
Controller data and latch timing
src/fl/chipsets/my9221.h
writeBit adds data setup, hold, and clock phase delays. latch adds timed DI pulses and a stop interval based on the number of transmitted chips. showPixels counts chips and passes the count to latch.
Host timing recorder and checks
src/platforms/stub/platform_time.cpp.hpp, tests/fl/channels/spi_legacy_golden.cpp
The stub records microsecond delays as nanoseconds. The MY9221 test records DI and DCKI transitions and checks timing intervals, latch pulses, cascade stop time, and clock-edge counts across two frames.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related issues

  • Issue 5024 — Umbrella issue for the software SPI audit work that includes the MY9221 timing slice.

Possibly related PRs

Suggested reviewers: gitbruno

Merge Risk: Minimal · up to ef1df

The change adds minimum timing waits and host timeline checks without an established merge-blocking defect. It is ready for normal checks, but physical timing compliance remains unverified.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@github-actions github-actions Bot added the sanitized Title, body and conversation screened; review text requires separate screening. label Nov 21, 2026
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Nov 21, 2026
@zackees
zackees added this pull request to the merge queue Nov 21, 2026
@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Memory usage (this PR vs. its merge base)

Change in examples/Blink per target. The left number is this PR's image flash / attributed RAM in bytes; the colored delta is the change from the merge base (red ↑ is larger, green ↓ is smaller, Same is unchanged).

Target FLASH [bytes] RAM [bytes]
attiny85 2,460 (Same) 136 (Same)

@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 21, 2026
Merged via the queue into master with commit 839f11c Nov 21, 2026
131 of 133 checks passed
@zackees
zackees deleted the fix/5066-my9221-timing branch November 21, 2026 23:13
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Nov 21, 2026
@github-actions github-actions Bot added security: unscreened-content and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

MY9221: enforce DDR and latch timing on the direct FastPin path

1 participant