Skip to content

Allow interrupts on WS2811 etc on AVR without glitches - #1268

Closed
ben-xo wants to merge 3 commits into
FastLED:masterfrom
ben-xo:feature/avr-clockless-trinket-interrupts
Closed

ben-xo wants to merge 3 commits into
FastLED:masterfrom
ben-xo:feature/avr-clockless-trinket-interrupts

Conversation

@ben-xo

@ben-xo ben-xo commented Jul 29, 2021

Copy link
Copy Markdown
Contributor

For many WS2811 strips, we can relax the clearing of interrupts down to protecting the timing of the shortest pulse. Your interrupts must still be very very fast in order for the strip not to latch part-way through.

@ben-xo

ben-xo commented Jul 29, 2021

Copy link
Copy Markdown
Contributor Author

Part three of the changes from my previous PR (#1265) which I'm separating out into separate pull requests, following from #1267

@ben-xo

ben-xo commented Jul 29, 2021

Copy link
Copy Markdown
Contributor Author

@kriegsman at your request the new behaviour is only activated if FASTLED_ALLOW_INTERRUPTS is set to 1.

ben-xo added 3 commits January 3, 2022 02:20
For many WS2811 strips, we can relax the clearing of interrupts down to protecting the timing of the shortest pulse. Your interrupts must still be very very fast in order for the strip not to latch part-way through.
@ben-xo
ben-xo force-pushed the feature/avr-clockless-trinket-interrupts branch from 2094bd9 to 65da7cc Compare January 3, 2022 02:33
@ben-xo

ben-xo commented Jan 3, 2022 •

Copy link
Copy Markdown
Contributor Author

I'll add a bit more detail / background here.

Even with this PR, the default platform behaviour for AVR Clockless remains unchanged (because FASTLED_ALLOW_INTERRUPTS is not set by default).

To make use of this at all, you must #define FASTLED_ALLOW_INTERRUPTS 1 yourself, as if to assert that you really do want this, and that hopefully you understand the various caveats, consequences and shortcomings, which are all listed in the source code (where you will bump into them if you found that you had to dig and figure out why interrupts weren't working, like I did).

The caveats are various. In principal, you are sacrificing specification conformance. In practice, different makes, models or manufacturers of LEDs may have different tolerances to the out-of-spec behaviour, although I have not so far met a strip on which this hasn't worked.

The principle being applied, inspired by this blog post, is that there are only 2 timings which matter for ws2812, even though 5 are defined in the spec.

The rationale for why the other timings in the spec are irrelevant is discussed in detail in the blog post, and if you study it for a while it becomes increasingly hard to imagine that there would be any pixel models which would obey any other timing directly; it would seem more costly and unnecessarily complicated.

The two timings in question constrain the maximum width of a HIGH control pulse that will still be interpreted as a 0 bit, and the maximum width of a LOW control pulse before the pixel strip will latch. These two constraints imply:

  • if an interrupt fires within the first "i'm still 0" part of the control pulse, it all but guarantees it will flip to a 1 bit. This happens often enough in practise that it results in "speckles" appearing constantly along the strip in random places with random colours, due to the timer0 interrupt used by Arduino for the clock. To fix this, the first part of the pulse (which guarantees the 0-ness or 1-ness of a data bit) must be in a critical section - and this is the main contribution of this PR.

  • if your interrupt handler takes longer than the maximum width of LOW before latching, you will see corruptions on the strip, although they're quite different to the speckles from the bit-flipping.

Which sort of corruptions you may see depends mainly on the frequency of your long interrupts, and your target frame rate, although there are some interesting variations.

If your long interrupts are infrequent and your frame rate high, you may see the occasional bright flash across the picture, starting at 0 but going up some random amount.

If the frame rate is too low, those flickers may stay displayed for a long time. The "flicker" is actually a partially corrupted frame with random colours on some proportion of the lowest pixels. This is because if a latch event happens partway through a byte, the remaining bits from the frame will effectively start a new frame on the strip at pixel 0, and 7/8ths of the time the color bits will be offset with bits from adjacent colors leading to corruption that's very bright and obnoxious unless all your LEDs are set to black.

Alternatively, if the interrupt rate is so high that it interrupts every frame, perhaps more than once, then you may end up with a "cutoff" pixel beyond which your picture can never seem to reach. The higher the interrupt frequency, the closer to 0 that cutoff will get - and so all the pixels will be colour gibberish all the time.

Nevertheless, there are some things you can control.

One thing to consider is that your interrupt may be short enough if it fires alone, but may be too long if it fires simultaneously with another interrupt. This is especially relevant for timer interrupts, remembering that the the Arduino Core clock timer is a thing.

It's often possible to refactor ISRs to just buffer work for when back in the main loop (e.g. "button_was_pushed = 1; return etc). Timers at related frequencies can also be initialised at an offset from each other so they don't coincide with timer0 or each other.

I have no idea if other popular Arduino libraries have ISRs which are too long and run at a high frequency. I have not tried the "servo" library that seems to come up in support requests a lot.

@ben-xo

ben-xo commented Jan 13, 2023

Copy link
Copy Markdown
Contributor Author

Any feedback which would get this merged?

@zackees zackees closed this Sep 2, 2024
zackees added a commit that referenced this pull request Aug 7, 2026
fbuild 2.5.15 was published to PyPI on 2026-08-07 05:34:16Z (tag
v2.5.15). It carries three fixes this repo needs, all confirmed
ancestors of the tag:

  2976d863  fix(rp): honor Arduino Bluetooth menu (#1263)
  0dd68b24  fix(teensy): run the link in the build dir (#1268)
  1324d6c6  fix(arm): run the link in the build dir for every ARM linker (#1270)

Why it matters here:

- #1263 is what makes `board_build.ipbtstack = ipv4btcble` take effect,
  so `libipv4-bt.a` is linked instead of `libipv4.a` and BTstackLib
  compiles. Without it the rp2350w build dies at `_needsbt.h`'s
  `static_assert(ENABLE_CLASSIC, ...)`, which blocked every RP2350W HIL
  flow in #3864.
- #1268/#1270 stop the ARM link running in the daemon's inherited cwd,
  which was dropping a stray 0-byte `-r` file into the repo root on every
  clean build (#3867).

Verified against the published wheel (registry install, not a local
build — the previously reported rp2350w validation used a local fbuild
development executable and was not reproducible from a clean checkout):

  bash compile rp2350w --examples AutoResearch
  -> build succeeded in 253.7s (flash: 1177936 bytes, ram: 132048 bytes)
  -> SUCCESS: AutoResearch
  -> working tree clean, no stray `-r`

Byte-identical to the local-build result, so the published artifact
behaves the same.

`bash lint` clean. C++ tests fingerprint-skipped (no C++ changed in this
commit); the cached result is 362/362.

Refs #3864, #3867, FastLED/fbuild#1269

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants