Skip to content

fix(nrf52): forced software FastPin hi()/lo()/toggle() use OUTSET/OUTCLR (#5396) - #5401

Merged
zackees merged 1 commit into
masterfrom
fix/5396-nrf52-atomic-hi-lo
Nov 22, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/5396-nrf52-atomic-hi-lo

Conversation

@zackees

@zackees zackees commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Closes #5396.

Problem

With FASTLED_FORCE_SOFTWARE_PINS on nRF52, FastPin<PIN> is the runtime-port template in src/fl/system/fastpin_base.h. Its hi()/lo() did *sPort |= mask / *sPort &= ~mask, which loads and stores the whole OUT latch. If an interrupt changes another pin on the same port between the load and the store, the store reverts that change. toggle() had the same read-modify-write.

Fix

The writes now go through a SoftwarePinWriter type that the preprocessor selects once:

  • nRF52: hi()/lo() (and the hi(port)/lo(port) overloads) do a single store to OUTSET/OUTCLR. toggle() reads OUT and then writes only OUTSET or OUTCLR, the same approach fix(nrf51): make FastPin toggle() race-free via OUTSET/OUTCLR (#5047) #5158 took for nRF51. The Arduino map returns &NRF_GPIO_Type::OUT, and the vendor CMSIS NRF_GPIO_Type is used to find OUTSET/OUTCLR from it (FL_OFFSETOF). No offsets are hand-written.
  • Every other platform: no behaviour change. Writes are still a read-modify-write of the output latch, and AVR still toggles through its PIN register.

Tests

New test tests/fl/system/fastpin_nrf52_set_clr.cpp. It sits on the non-PCH list next to fastpin_mapped_toggle/fastpin_avr_toggle because it picks a different FastPin provider before including the header. It models a GPIO block in which OUTSET/OUTCLR do not fold into OUT, so any store to OUT shows up in the result.

  • RED on master: 3/3 cases fail (OUTSET stays 0 and OUT gets rewritten from 0x81 to 0x89).
  • GREEN: bash test fastpin_nrf52_set_clr passes, and so does --debug (ASAN/UBSAN).
  • fastpin_mapped_toggle, fastpin_avr_toggle, fastpin_forced_no_pinmap and nrf52_pin still pass.
  • bash lint --cpp passes.

Limitation

No nRF52 board was attached, so this has not run on a device. The nRF52 changes were checked by host tests and by compiling with fbuild only: bash compile adafruit_feather_nrf52840_sense --examples Blink --defines FASTLED_FORCE_SOFTWARE_PINS succeeds.

Summary by CodeRabbit

  • Bug Fixes
    • Software-driven pin writes on nRF52 now set or clear the requested pin without changing the output state of other pins.
    • Pin toggling and writes continue to use platform-appropriate behavior across supported ports.

…OUTSET/OUTCLR (#5396)

The runtime-port FastPin used by FASTLED_FORCE_SOFTWARE_PINS did a
read-modify-write of the whole OUT latch, so an interrupt that changed
another pin on the same port between the load and the store was undone.
On nRF52 the pin writes now go through the vendor NRF_GPIO_Type's
OUTSET/OUTCLR, one store each; other platforms keep their existing writes.
A host test with a modelled GPIO block fails on the old code.
@zackees
zackees enabled auto-merge November 22, 2026 03:24
@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Explain Change →

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

No actionable comments were generated in the recent review.

Walkthrough

With FASTLED_FORCE_SOFTWARE_PINS, runtime-port FastPin writes now use a platform-specific writer. On nRF52, the writer uses GPIO set and clear registers. Tests cover these writes and compile without the shared PCH.

Changes: Forced-software pin writes

Layer / File(s) Summary
Platform-specific writes and validation
src/fl/system/fastpin_base.h, tests/fl/system/fastpin_nrf52_set_clr.cpp, tests/meson.build
SoftwarePinWriter uses OUTSET and OUTCLR for nRF52 writes. Other platforms retain output-latch updates, with platform-specific toggle operations. Runtime-port FastPin methods delegate to the writer. Tests check nRF52 writes and confirm OUT is unchanged. Meson compiles the new test without the shared PCH.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • FastLED/FastLED#5149: It selects the shared software FastPin template on nRF52, the same forced-mode path updated here.
Merge Risk: Minimal · up to cf4aa

This change makes forced-software pin writes on nRF52 use the GPIO set and clear registers, so an interrupt's update to another pin on the same port is no longer overwritten. Other platforms keep their existing behavior. No merge-blocking issues remain. The change is covered by a host test and has no on-device run.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: forced software FastPin hi()/lo()/toggle() writes on nRF52 now use OUTSET/OUTCLR. The raw summary confirms this is the core of the change. The title is specific and ma…
Linked Issues check Passed [#5396] The reviewed head implements the required fix in src/fl/system/fastpin_base.h. Under FASTLED_FORCE_SOFTWARE_PINS on FL_IS_NRF52, SoftwarePinWriter::hi()/lo() perform one store to OUTSET/OUTCLR…
Out of Scope Changes check Passed All changes support the objective of #5396. The fastpin_base.h changes add the SoftwarePinWriter selection and route the FastPin writes through it. The added includes (fl/stl/cstddef.h, fl/stl/int.h) …
Full details: Title check

Explanation

The title names the main change: forced software FastPin hi()/lo()/toggle() writes on nRF52 now use OUTSET/OUTCLR. The raw summary confirms this is the core of the change. The title is specific and matches the changeset. The fix(nrf52) prefix and the issue number #5396 are conventional and do not reduce relevance. The title is 80 characters, which is at the upper end of a concise title but still readable.

Full details: Linked Issues check

Explanation

[#5396] The reviewed head implements the required fix in src/fl/system/fastpin_base.h. Under FASTLED_FORCE_SOFTWARE_PINS on FL_IS_NRF52, SoftwarePinWriter::hi()/lo() perform one store to OUTSET/OUTCLR. The hi(port)/lo(port) overloads use the same writer. The OUTSET/OUTCLR offsets come from FL_OFFSETOF(NRF_GPIO_Type, OUT) and are not hand-written. toggle() reads OUT and writes only OUTSET or OUTCLR, so no store to OUT occurs for a single-pin write. The non-nRF52 branch keeps the prior read-modify-write and the AVR PIN-register toggle, so other platforms keep their behaviour. The new test tests/fl/system/fastpin_nrf52_set_clr.cpp models OUT, OUTSET and OUTCLR as separate registers. It asserts OUT is unchanged after hi(), lo(), hi(port), lo(port) and toggle(). This covers the host-test acceptance criterion. The author reports that the test fails 3/3 on master, that fastpin_mapped_toggle, fastpin_avr_toggle, fastpin_forced_no_pinmap and nrf52_pin pass, and that the forced nRF52 build succeeds. I did not re-run these results. No device run was done, so the real Nordic GPIO layout is not verified on hardware.

Full details: Out of Scope Changes check

Explanation

All changes support the objective of #5396. The fastpin_base.h changes add the SoftwarePinWriter selection and route the FastPin writes through it. The added includes (fl/stl/cstddef.h, fl/stl/int.h) support the new types and macros. The tests/meson.build change excludes fastpin_nrf52_set_clr from the shared PCH, as the other FastPin provider tests are excluded, so the test can select its own provider. The new test file covers the changed behaviour. No unrelated functional change was found in the reviewed diff.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • 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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 0df0973 and cf4aad0.

Files selected for processing: 3

  • src/fl/system/fastpin_base.h
  • tests/fl/system/fastpin_nrf52_set_clr.cpp
  • tests/meson.build

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.

@github-actions github-actions Bot added the sanitized Title, body and conversation screened; review text requires separate screening. label Nov 22, 2026
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Nov 22, 2026
@zackees
zackees added this pull request to the merge queue Nov 22, 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 22, 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 22, 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 22, 2026
Merged via the queue into master with commit 6cb9b2f Nov 22, 2026
133 of 134 checks passed
@zackees
zackees deleted the fix/5396-nrf52-atomic-hi-lo branch November 22, 2026 03:37
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sanitized Title, body and conversation screened; review text requires separate screening.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

fix(nrf52): forced software FastPin hi()/lo() are non-atomic read-modify-writes of OUT (toggle() already fixed in #5237)

1 participant