Skip to content

Refactor classic-ESP32 RMT (rmt_4) into the ChannelEngine + Peripheral API-object pattern #3458

Closed

Description

@zackees

Goal

Bring the classic-ESP32 RMT driver (src/platforms/esp/32/drivers/rmt/rmt_4/) in line with the canonical ChannelEngine + Peripheral API-object pattern that the rest of the channel-driver registry now uses. This is the last clockless-TX driver on esp32dev that pre-dates the #2428 channel-manager refactor and still carries Phase-3B stubs.

This is a refactor + completion task, not a feature add. No new Bus enum, no SPI mode. RMT stays clockless-only.

Survey of the current state

What's there today

src/platforms/esp/32/drivers/rmt/rmt_4/
├── channel_driver_rmt4.h              -- IChannelDriver impl (mostly shaped)
├── channel_driver_rmt4.cpp.hpp        -- has Phase-3B stubs
├── bus_traits.h                       -- BusTraits<Bus::RMT> + addDriver(priority=2)
├── idf4_clockless_rmt_esp32.h         -- legacy ClocklessIdf4<PIN, TIMING> template
└── _build.cpp.hpp                     -- unity-build dispatcher

Feature flags (src/platforms/esp/32/feature_flags/enabled.h):

  • FASTLED_ESP32_HAS_RMT = SOC_RMT_SUPPORTED
  • FASTLED_RMT5 = 0 on classic ESP32 → rmt_4 is the active impl

Legacy template path (addLeds<WS2812B, PIN>) → ClocklessIdf4<PIN, TIMING> → ODR-uses BusTraits<Bus::RMT>::instancePtr() directly (Phase 5c #2441 / #2450), bypassing ChannelManager. The legacy fast-path stays — this refactor only fixes the modern FastLED.add<>() / Channel::create<>() path.

Reference template

Use src/platforms/arm/teensy/teensy4_common/drivers/flexio/channel_engine_flexio.h as the shape model. RMT4 should mirror:

  • Factory create(...) returning shared_ptr<ChannelEngineRMT>
  • Abstract base + concrete impl split (header/cpp.hpp)
  • Peripheral injected via constructor — explicit ChannelEngineRMT(fl::shared_ptr<IRmtPeripheral>)
  • enqueue() / poll() / show() state machine: READY → BUSY → DRAINING → READY
  • Clean canHandle() vetting

Differences from FlexIO (so we don't blindly copy):

  • RMT is not a parallel-IO peripheral, so the unified-clockless+SPI rule from agents/docs/cpp-standards.md does not apply. Capabilities stays (true, false).
  • RMT uses ISR-driven double-buffer refill, not DMA completion. Keep that.
  • TX and RX are separate peripherals (rmt_4 TX + rmt_rx RX). Don't merge — that's a separate future question.

Gap list

# Severity Gap Where
1 HIGH acquireChannel() is a Phase-3B stub — allocates pool slots but never calls rmt_driver_install() or configures hardware. New-API channels can't actually transmit through the ChannelManager path. channel_driver_rmt4.cpp.hpp:172
2 HIGH fillNextBuffer() ISR-refill skeleton has placeholder memPtr and pixel iteration. No actual byte→symbol encoding wired up. same file, ISR section
3 MEDIUM No IRmtPeripheral abstraction. Driver directly touches RMT.* register struct, soc/rmt_struct.h, and the inline spinlock. Tightly couples driver to silicon. channel_driver_rmt4.{h,cpp.hpp}
4 LOW No unit tests for symbol-encoding math (makeZeroSymbol, makeOneSymbol, NS_TO_ESP_CYCLES, ESP_TO_RMT_CYCLES). WS2812B / SK6812 timing not verified post-refactor. tests/
5 INFO TX (rmt_4) and RX (rmt_rx) drivers are independent. Not a blocker — flag only. —

Plan

Phase 1 — Peripheral wrapper

Extract IRmtPeripheral (analogous to IUartPeripheral at src/platforms/esp/32/drivers/uart/iuart_peripheral.h). Surface:

class IRmtPeripheral {
public:
    virtual ~IRmtPeripheral() = default;
    virtual bool initialize(const RmtPeripheralConfig& cfg) = 0;
    virtual void deinitialize() = 0;
    virtual bool isInitialized() const = 0;

    // Channel allocation (returns -1 on failure)
    virtual int  allocateChannel(int gpio_pin, uint32_t clk_div) = 0;
    virtual void releaseChannel(int channel) = 0;

    // Symbol-buffer access for ISR refill
    virtual void writeSymbols(int channel, const rmt_item32_t* symbols, size_t count, size_t offset) = 0;

    // ISR registration
    virtual bool installIsr(rmt_isr_handler_t handler, void* arg) = 0;
    virtual void startTransmission(int channel) = 0;
    virtual void stopTransmission(int channel) = 0;
};

Concrete RmtPeripheralEsp implements this against driver/rmt.h + the RMT.* register struct. Singleton lifetime via a RmtBusHolder struct (mirror UartBusHolder at src/platforms/esp/32/drivers/uart/bus_traits.h:30-37).

Phase 2 — Finish acquireChannel() and fillNextBuffer()

Replace the Phase-3B stubs with real hardware init. Reference the working RMT timing path from idf4_clockless_rmt_esp32.h (the legacy template) — that code already encodes WS2812-family symbols correctly. Lift its symbol-encode + refill logic into ChannelEngineRMT::fillNextBuffer() behind the new IRmtPeripheral surface.

Phase 3 — ChannelEngineRMT shape cleanup

Mirror ChannelEngineFlexIO:

  • create(fl::shared_ptr<IRmtPeripheral>) factory
  • enqueue(ChannelDataPtr) pushes to mEnqueuedChannels
  • show() iterates, calls mPeripheral->startTransmission(), returns
  • poll() returns DriverState from peripheral status
  • getCapabilities() returns Capabilities(true, false) — no SPI (RMT isn't a parallel-IO peripheral, per agents/docs/cpp-standards.md rule clarification)
  • getName() returns "RMT" (matches Bus::RMT dispatch — must not change)

BusTraits<Bus::RMT> becomes:

struct RmtBusHolder {
    fl::shared_ptr<RmtPeripheralEsp>   peripheral;
    fl::shared_ptr<ChannelEngineRMT>   driver;
    RmtBusHolder() FL_NO_EXCEPT
        : peripheral(fl::make_shared<RmtPeripheralEsp>()),
          driver(fl::make_shared<ChannelEngineRMT>(peripheral)) {}
};

Priority stays at 2 (see src/fl/channels/bus_priorities.h) so RMT keeps the same dispatch ranking; no other drivers move.

Phase 4 — Tests

Add unit tests under tests/ for symbol-encoding math, independent of hardware:

  • makeZeroSymbol(timing) produces correct rmt_item32_t bit pattern for WS2812B/SK6812 T0H/T0L
  • makeOneSymbol(timing) likewise for T1H/T1L
  • Round-trip NS_TO_ESP_CYCLES → ESP_TO_RMT_CYCLES for the documented APB clock divisors

Phase 5 — Validation

Out of scope

Acceptance

  • IRmtPeripheral exists; ChannelEngineRMT takes it via constructor
  • acquireChannel() and fillNextBuffer() are no longer Phase-3B stubs
  • bash compile esp32dev --examples AutoResearch passes
  • bash test --cpp passes (including new symbol-encoding unit tests)
  • Legacy addLeds<WS2812B, PIN> path still works on classic ESP32 (no behavioral change there)
  • AutoResearch on the live WROOM board still reports the GPIO 33↔34 short

Survey source: code grep across src/platforms/esp/32/drivers/rmt/, src/fl/channels/, src/platforms/esp/32/drivers/uart/, src/platforms/arm/teensy/teensy4_common/drivers/flexio/, and src/platforms/esp/32/feature_flags/enabled.h. No prior issues filed for rmt_4 refactor (no hits for rmt_4 in agents/ or docs/). Last related work: #2441 / #2450 (Phase 5c pre-bind), #2247 / #2252 (ISR contention fixes, 2023).

Plan drafted with assistance from Claude Code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions