Skip to content

refactor(spi): wrap FlexIO/ObjectFLED internals in private namespaces - #3434

Merged
zackees merged 1 commit into
masterfrom
feat/spi-private-namespaces
Jun 28, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/spi-private-namespaces

Conversation

@zackees

@zackees zackees commented Jun 28, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #3431 / #3432. Per user directive 2026-06-27 "when in doubt use private namespaces", replace the sOfSpi* / kOfSpi* / of_spi_* prefix-rename workaround for the unity-build collision with the more idiomatic private named namespace approach.

What changes

Driver Before After
FlexIO-SPI static ... sSpiCurrentPins; sSpiDmaChannel; ... at fl:: scope (worked because it was first to land) wrapped in namespace fl::detail::flexio_spi_internal { ... }
ObjectFLED-SPI static ... sOfSpiCurrentPins; sOfSpiDmaChannel; ... at fl:: scope (prefixed to avoid collision with FlexIO in the same unity TU) wrapped in namespace fl::detail::objectfled_spi_internal { ... }, prefix dropped

Each public API function (the ones in the .h headers) opens its body with using namespace detail::xxx_spi_internal; so call-sites read naturally. The two drivers' identically-named statics now live in different named namespaces — no collision in the unity build (fl/build/platforms+.cpp).

Why not a single anonymous namespace per file

Per [C++ namespace.unnamed], multiple namespace { ... } declarations within the same TU merge into one anonymous namespace. In a unity TU that pulls in both files, anonymous-namespace contents would still collide. Named-and-distinct is the only collision-free option short of file isolation (which the project's unity-build strategy doesn't grant).

Hardware verification (Teensy 4.1, COM20)

  • flexioSpiSelfTest — 3/3 pass (wait_ms=0)
  • objectfledSpiSelfTest — 3/3 pass (wait_ms=5 = drain wait only)

Behavior is byte-equivalent before/after; this is a pure cleanup refactor.

Test plan

  • bash test --cpp — passes
  • bash lint --cpp clean
  • bash compile teensy41 --examples AutoResearch succeeds
  • Both SPI smoke tests pass on real Teensy 4.1
  • CI green

Refs #3428, #3431, #3432.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved SPI-mode stability on Teensy 4.x, reducing symbol conflicts in builds and making transfers more reliable.
    • Added stronger checks and safer handling for invalid or oversized SPI frames.
    • Improved timeout handling and cleanup so SPI hardware state is left in a predictable condition.
  • Refactor

    • Reorganized internal SPI implementation state for better isolation between related drivers.
    • Improved diagnostic reporting after failures and timeouts.

… (no prefix)

Per user directive 2026-06-27 "when in doubt use private namespaces" --
follow-up cleanup for the prefix-rename workaround that landed with
#3431 / #3432.

Both `flexio_spi_mode.cpp.hpp` and `objectfled_spi_mode.cpp.hpp` now wrap
their file-static state + private helpers in matching named namespaces:

  - fl::detail::flexio_spi_internal
  - fl::detail::objectfled_spi_internal

Each public API function (those in the .h header) opens with
`using namespace detail::xxx_spi_internal;` so the bodies reference the
private state without the namespace prefix. The two drivers' identically-
named symbols (sSpiCurrentPins, sSpiDmaChannel, ...) now live in
different namespaces, so they don't collide in the unity build
(fl/build/platforms+.cpp).

The ObjectFLED side drops the `sOfSpi*` / `kOfSpi*` / `of_spi_*` prefix
that was the previous workaround for the unity-build collision -- the
private namespace handles it cleanly now.

Hardware-verified on Teensy 4.1 (COM20) after the refactor:
  - flexioSpiSelfTest:    3/3 pass (wait_ms=0)
  - objectfledSpiSelfTest: 3/3 pass (wait_ms=5 = drain only)

Build / lint clean (bash test --cpp / bash lint / bash compile teensy41).

Refs #3428, #3431, #3432.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@zackees
zackees merged commit c5ee1ad into master Jun 28, 2026
5 of 8 checks passed
@coderabbitai

coderabbitai Bot commented Jun 28, 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: 53cb549d-dc71-4776-a18a-b36d621583b1

📥 Commits

Reviewing files that changed from the base of the PR and between 2e89cca and 6c7a528.

📒 Files selected for processing (2)
  • src/platforms/arm/teensy/teensy4_common/drivers/flexio/flexio_spi_mode.cpp.hpp
  • src/platforms/arm/teensy/teensy4_common/drivers/objectfled/objectfled_spi_mode.cpp.hpp

📝 Walkthrough

Walkthrough

Both Teensy 4.x SPI driver translation units (flexio_spi_mode.cpp.hpp and objectfled_spi_mode.cpp.hpp) wrap their internal state and helpers in dedicated private namespaces (detail::flexio_spi_internal / detail::objectfled_spi_internal) to prevent unity-build symbol collisions. The ObjectFLED driver additionally renames sOfSpi* state to sSpi*, adds stricter guards in show/wait/deinit, and improves timeout diagnostics snapshots.

Changes

FlexIO SPI: Private Namespace Isolation

Layer / File(s) Summary
Namespace wrapper and using-declarations
src/platforms/arm/teensy/teensy4_common/drivers/flexio/flexio_spi_mode.cpp.hpp
Opens detail::flexio_spi_internal around the TU's internal state block, closes it before the public API, and inserts using namespace detail::flexio_spi_internal; at the start of each of the five public functions (lookup_pins, init, show, wait, deinit).

ObjectFLED SPI: Namespace Isolation, Renaming, and Behavioral Hardening

Layer / File(s) Summary
Private namespace, constants, and state renaming
src/platforms/arm/teensy/teensy4_common/drivers/objectfled/objectfled_spi_mode.cpp.hpp
Introduces detail::objectfled_spi_internal, replaces kOfSpi* constants with kSpi*, and renames all file-static state from sOfSpi* to sSpi* (DMA channel pointer, flags, pin info, bit-pattern buffer, saved mux registers, diagnostics snapshot).
ISR rename, pin lookup, and QTimer helper rename
src/platforms/arm/teensy/teensy4_common/drivers/objectfled/objectfled_spi_mode.cpp.hpp
Renames the SPI DMA ISR to objectfled_spi_dma_isr, updates objectfled_spi_lookup_pins under the new namespace, and renames of_spi_program_qtimer to objectfled_spi_program_qtimer.
objectfled_spi_init: clock clamping, mux snapshot, DMA setup
src/platforms/arm/teensy/teensy4_common/drivers/objectfled/objectfled_spi_mode.cpp.hpp
Resets diagnostics, enforces and clamps clock_hz to kSpiClockMinHz/kSpiClockMaxHz, snapshots MOSI/SCLK mux into sSpiSaved*, calls renamed QTimer programmer, allocates/configures DMAChannel with ISR attachment, and records initialized state.
objectfled_spi_show: guards, encoding, DMA programming
src/platforms/arm/teensy/teensy4_common/drivers/objectfled/objectfled_spi_mode.cpp.hpp
Adds init/DMA/null/empty/oversize guards with explicit logging; encodes bytes into sSpiBitPattern, conditionally flushes cache, programs DMA TCD to write encoded words to &GPIO1_DR, and enables DMA before starting QTimer3.
objectfled_spi_wait, deinit, and diagnostics
src/platforms/arm/teensy/teensy4_common/drivers/objectfled/objectfled_spi_mode.cpp.hpp
wait() snapshots sSpiLastDiag on timeout, stops QTimer3, disables/clears DMA, and parks MOSI/SCLK low. deinit() tears down sSpiDmaChannel and restores saved mux ALT values (leaving GPR26 untouched). read_diagnostics() prefers the timeout snapshot when error flags are set.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • FastLED/FastLED#3431: Introduced the initial flexio_spi_mode.cpp.hpp SPI master implementation and flexio_spi_* APIs that this PR wraps in the private namespace.
  • FastLED/FastLED#3432: Introduced the ObjectFLED SPI-mode scaffolding (objectfled_spi_* APIs) that this PR refactors with namespace isolation and state renaming.

Poem

🐇 In unity builds where symbols would clash,
I tucked them in namespaces, quick as a flash.
sSpi* replaced what was sOfSpi* before,
With guards and diagnostics to handle each error.
No collisions remain—my burrow is neat!
The DMA now purrs with each LED beat. ✨

✨ 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 feat/spi-private-namespaces

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