Skip to content

feat(rmt4): port RmtMemoryManager — WiFi-resistant TX + TX/RX ledger (#3469) - #3470

Merged
zackees merged 1 commit into
masterfrom
feat/3469-rmt4-memory-manager
Jul 1, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/3469-rmt4-memory-manager

Conversation

@zackees

@zackees zackees commented Jul 1, 2026 •

Copy link
Copy Markdown
Member

Summary

Ports the RMT5 RmtMemoryManager accounting ledger + adaptive buffer-block strategy to the classic-ESP32 / IDF 4.x RMT driver. Follows the rmt_4 / rmt_5 split precedent from #3461 and #3467.

The manager:

  • tracks the global word-pool (512 words on classic ESP32, 256 on S2)
  • records per-channel allocations (TX vs RX) so TX can see RX presence via hasActiveRxChannels()
  • exposes calculateMemoryBlocks(network_active) returning 3 (triple-buffer) when the network is active AND the pool has room for the extra 64-word block, else 2 (RMT4 historic default)

Closes #3469.

What's out (deliberate)

  • No DMA accounting — RMT4 hardware has no DMA channel. The RMT5 manager's DMA slot ledger is not ported.
  • No dedicated TX/RX pools — RMT4 is always a global-pool platform.
  • Real network detection is a stub — NetworkStateTracker4::isActive() returns false in v1. That means today's behaviour is bit-for-bit identical to pre-Port RmtMemoryManager to RMT4 — WiFi-resistant TX + central TX/RX ledger for classic ESP32 #3469 — the manager plumbing is in place, but TX still gets 2 mem blocks per channel. Follow-up PR flips the stub to poll a shared network detector.
  • No ISR-side refactor — PULSES_PER_FILL_RMT4 stays fixed. Higher mem_block_num values in rmt_config() give the ISR more headroom before it misses its deadline; the reactive checkTime truncation in fillNextBuffer stays as a belt-and-braces safety net.

Wiring

  • ChannelEngineRMT4Impl::configureChannel — free-then-tryAllocateTx idiom. Failure returns false; the caller (acquireChannel) treats this the same as a HW-channel-count miss and defers the strip to the pending queue. Uses calculateMemoryBlocks(network_active) to fill Rmt4ChannelConfig::mMemBlocks.
  • ChannelEngineRMT4Impl destructor — releases every in-use channel's allocation.
  • rmt_rx_4/RmtRxChannelImpl constructor — pre-registers a 64-symbol RX reservation with channel-id 128 + (pin & 0x7F) so TX (raw 0..N-1 IDs) never collides.
  • rmt_rx_4/RmtRxChannelImpl destructor — releases the RX reservation.

Tests

19 host FL_TEST_CASEs in tests/platforms/esp/32/drivers/rmt/rmt_4/rmt_memory_manager_4.cpp cover:

  • pool starts empty
  • TX idle = 128 words, network-active = 192
  • graceful demotion to 128 when 192 doesn't fit
  • pool-exhausted refusals
  • duplicate-alloc refusals
  • RX symbol-count recording, zero-symbol rejection
  • free() releases correctly; wrong is_tx and unknown-channel misses
  • free-then-realloc reconfigure idiom
  • hasActiveRxChannels() tracks RX lifetime, ignores TX
  • calculateMemoryBlocks() 2/3 selection + fallback when the extra block doesn't fit
  • reset() clears the ledger
  • 4 concurrent TX up to pool limit, 5th refused

Test plan

  • bash test rmt_memory_manager_4 --cpp --clean — 19 / 19 pass on a fresh build.
  • bash test --cpp — full host suite 342 / 342 pass (no regression).
  • clang-format --dry-run --Werror clean on every touched file.
  • bash compile esp32dev --examples AutoResearch — blocked on the pre-existing fbuild Windows spec-file backslash regression that also reproduces on plain master. Not introduced by this PR. CI on Linux exercises the cross-compile.
  • WROOM live validation of the network-active bump — rides on the same fbuild blocker.

The manager class body is header-inline on purpose so host tests build it directly without needing the ESP-IDF headers. Only the platform-default instance() singleton lives in the .cpp.hpp behind the usual FL_IS_ESP32 && !FASTLED_RMT5 guard.

Follow-ups called out in the file docblocks

  • Wire NetworkStateTracker4::isActive() to a real detector once the RMT5 NetworkDetector is lifted out of its #if FASTLED_RMT5 guard to a shared location.
  • Move the PULSES_PER_FILL_RMT4 threshold to a per-channel state->mem_blocks * 32 computation so the ISR actually exploits the extra buffer headroom during network bursts. Requires ISR path touch — separate PR.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added smarter ESP32 RMT4 channel memory handling for both transmit and receive paths.
    • The driver can now adapt memory usage based on network activity and better share limited hardware resources across channels.
  • Bug Fixes

    • Improved cleanup so channel memory is returned properly when channels shut down.
    • Reduced the risk of memory leaks or failed reconfiguration when channel setup is interrupted.
  • Tests

    • Added coverage for memory allocation, release, and capacity behavior across common RMT4 usage scenarios.

#3469)

Ports the RMT5 `RmtMemoryManager` accounting ledger + adaptive
buffer-block strategy to the classic-ESP32 / IDF 4.x RMT driver,
following the rmt_4/rmt_5 split precedent from #3461 and #3467.

## What's in

- `rmt/rmt_4/rmt_memory_manager_4.h`   - header-inline class body
- `rmt/rmt_4/rmt_memory_manager_4.cpp.hpp` - platform-default singleton
- `rmt/rmt_4/network_state_tracker_4.{h,cpp.hpp}` - v1 stub returning
  `false`. Follow-up PR wires this to the shared network detector once
  it's lifted out of the FASTLED_RMT5 guard.

The manager tracks:
- Global word-pool (512 words on classic ESP32, 256 on S2 — 8 or 4
  channels × 64 words per block).
- Per-channel allocations (TX or RX distinguished by `is_tx` flag).
- `hasActiveRxChannels()` for TX-side coordination.
- Adaptive `calculateMemoryBlocks(network_active)` that returns 3 when
  the network is active AND the pool has room for the extra 64-word
  block, else 2 (RMT4 historic default).

## What's out (deliberately)

- No DMA accounting: RMT4 hardware has no DMA channel. The RMT5
  manager's DMA slot ledger is not ported.
- No dedicated TX/RX pools: RMT4 is always a global-pool platform.
- Real network detection: `NetworkStateTracker4::isActive()` is a
  stub `return false` for v1. That means today's behaviour is
  bit-for-bit identical to pre-#3469 — the manager plumbing is in
  place, but the driver still allocates 2 mem blocks per TX channel.
  Follow-up PR flips the stub to poll the (shared) network detector.
- No ISR-side refactor: the `PULSES_PER_FILL_RMT4` refill threshold
  stays fixed. Higher `mem_block_num` values in `rmt_config()` give
  the ISR more headroom before it misses its deadline; the reactive
  `checkTime` truncation in `fillNextBuffer` stays as a belt-and-
  braces safety net.

## Wiring

- `ChannelEngineRMT4Impl::configureChannel`: calls the manager's
  free-then-tryAllocateTx idiom. Failure returns false; caller
  (acquireChannel) treats this the same as HW-channel-count-miss and
  defers the strip to the pending queue. Uses
  `calculateMemoryBlocks(network_active)` to fill in
  `Rmt4ChannelConfig::mMemBlocks`.
- `ChannelEngineRMT4Impl::~ChannelEngineRMT4Impl`: releases every
  in-use channel's allocation.
- `rmt_rx_4/RmtRxChannelImpl` constructor: pre-registers a 64-symbol
  RX reservation with channel-id `128 + (pin & 0x7F)` so TX (using
  raw 0..N-1 IDs) never collides.
- `rmt_rx_4/RmtRxChannelImpl` destructor: releases the RX reservation.

## Tests

19 host `FL_TEST_CASE`s in
`tests/platforms/esp/32/drivers/rmt/rmt_4/rmt_memory_manager_4.cpp`,
covering:
  - pool starts empty
  - TX idle allocation = 128 words, network-active = 192 words
  - graceful demotion to 128 when 192 doesn't fit
  - pool-exhausted refusals
  - duplicate-alloc refusals
  - RX symbol-count recording, zero-symbol rejection
  - free() releases correctly; wrong is_tx and unknown-channel misses
  - free-then-realloc reconfigure idiom
  - hasActiveRxChannels tracks RX lifetime, ignores TX
  - calculateMemoryBlocks 2/3 selection + fallback when the extra
    block doesn't fit
  - reset() clears the ledger
  - 4 concurrent TX up to pool limit, 5th refused

Local results:
  - `bash test rmt_memory_manager_4 --cpp --clean`:
      [doctest] test cases: 19 | 19 passed | 0 failed
  - `bash test --cpp`: 342/342 pass (no regression).
  - `clang-format --dry-run --Werror` clean on every touched file.

The manager class body is header-inline on purpose so host tests
build it directly without needing the ESP-IDF headers. Only the
platform-default `instance()` singleton lives in the .cpp.hpp behind
the usual `FL_IS_ESP32 && !FASTLED_RMT5` guard.

`bash compile esp32dev` is still blocked on the pre-existing fbuild
Windows spec-file backslash regression (`cc1plus: fatal error:
C:Usersniteris.fbuild...`) that reproduces on plain master, so
on-device WROOM validation of the network-active bump rides on that
blocker as before. CI on Linux exercises the cross-compile.

Closes #3469.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 1, 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: f7799dfa-5a6c-4968-89dd-1dc9ac5b7b37

📥 Commits

Reviewing files that changed from the base of the PR and between f0009aa and d62f1f2.

📒 Files selected for processing (8)
  • src/platforms/esp/32/drivers/rmt/rmt_4/_build.cpp.hpp
  • src/platforms/esp/32/drivers/rmt/rmt_4/channel_driver_rmt4.cpp.hpp
  • src/platforms/esp/32/drivers/rmt/rmt_4/network_state_tracker_4.cpp.hpp
  • src/platforms/esp/32/drivers/rmt/rmt_4/network_state_tracker_4.h
  • src/platforms/esp/32/drivers/rmt/rmt_4/rmt_memory_manager_4.cpp.hpp
  • src/platforms/esp/32/drivers/rmt/rmt_4/rmt_memory_manager_4.h
  • src/platforms/esp/32/drivers/rmt_rx/rmt_rx_4/rmt_rx_channel_4.cpp.hpp
  • tests/platforms/esp/32/drivers/rmt/rmt_4/rmt_memory_manager_4.cpp

📝 Walkthrough

Walkthrough

Adds a RmtMemoryManager4 allocation ledger and NetworkStateTracker4 stub for classic ESP32 RMT4, replacing fixed TX memory-block sizing in channel_driver_rmt4 with dynamic allocation. Wires RX pre-registration/free into rmt_rx_channel_4, updates the unity build, and adds host unit tests.

Changes

RMT4 Memory Manager Port

Layer / File(s) Summary
Memory manager contract and singleton
src/platforms/esp/32/drivers/rmt/rmt_4/rmt_memory_manager_4.h, rmt_memory_manager_4.cpp.hpp
Defines RmtMemoryError4, RmtAllocation4, and RmtMemoryManager4 with tryAllocateTx/tryAllocateRx/free/query APIs/calculateMemoryBlocks/reset, plus a platform-sized singleton constructor (4 vs 8 channels).
NetworkStateTracker4 stub
network_state_tracker_4.h, network_state_tracker_4.cpp.hpp
Adds a singleton NetworkStateTracker4 with stubbed isActive() (always false) and hasChanged() comparing against cached state.
TX allocation wiring in channel_driver_rmt4
channel_driver_rmt4.cpp.hpp
Replaces fixed FASTLED_RMT_MEM_BLOCKS with dynamic TX allocation via RmtMemoryManager4/NetworkStateTracker4 in configureChannel(), freeing allocations on prior config, failure, and destructor shutdown.
RX pre-registration in rmt_rx_channel_4
rmt_rx/rmt_rx_4/rmt_rx_channel_4.cpp.hpp
Adds construction-time RX memory pre-allocation and destruction-time free, tracked via new mMemoryChannelId/mMemoryRegistered members.
Unity build wiring and host tests
_build.cpp.hpp, tests/platforms/esp/32/drivers/rmt/rmt_4/rmt_memory_manager_4.cpp
Adds new files to the unity build include list and adds unit tests for allocation, free, reset, RX tracking, and block-sizing logic.

Sequence Diagram(s)

sequenceDiagram
  participant ChannelDriverRMT4
  participant NetworkStateTracker4
  participant RmtMemoryManager4
  participant Peripheral
  ChannelDriverRMT4->>RmtMemoryManager4: free(prior TX allocation)
  ChannelDriverRMT4->>NetworkStateTracker4: isActive()
  ChannelDriverRMT4->>RmtMemoryManager4: tryAllocateTx(channel_id, network_active)
  RmtMemoryManager4-->>ChannelDriverRMT4: out_words or refusal
  ChannelDriverRMT4->>ChannelDriverRMT4: compute mem_blocks
  ChannelDriverRMT4->>Peripheral: configure(mem_blocks)
  Peripheral-->>ChannelDriverRMT4: success or failure
  ChannelDriverRMT4->>RmtMemoryManager4: free(allocation) on failure
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related issues

Possibly related PRs

  • FastLED/FastLED#3461: Both modify channel_driver_rmt4.cpp.hpp's configuration/lifecycle path—this PR adds memory-manager-driven TX allocation while the other refactors configuration through an IRMT4Peripheral abstraction.

Poem

A rabbit dug a burrow deep,
to count each RMT word in its keep. 🐰
WiFi buzzes, blocks expand,
TX and RX now hand in hand.
Free when done, reset when new—
hop along, the pool stays true!

✨ 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/3469-rmt4-memory-manager

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.

Port RmtMemoryManager to RMT4 — WiFi-resistant TX + central TX/RX ledger for classic ESP32

1 participant