Skip to content

refactor(esp32,spi): route ChannelEngineSpi through ISpiPeripheral (#4910) - #5363

Merged
zackees merged 1 commit into
masterfrom
refactor/4910-spi-engine-peripheral
Nov 21, 2026
Merged

zackees merged 1 commit into
masterfrom
refactor/4910-spi-engine-peripheral

Conversation

@zackees

@zackees zackees commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Fixes #4910

What

ChannelEngineSpi called ESP-IDF directly, so its host tests were #ifdef ESP32 and covered only canHandle(). The engine now takes a detail::ISpiPeripheral&, the same way ChannelEngineRMT takes IRMT5Peripheral. On device the default is SpiPeripheralESP::instance(), and stub builds use SpiPeripheralMock::instance(). The engine now compiles for FASTLED_STUB_IMPL.

Interface additions. These are only the calls the engine makes:

  • pollTransaction(timeout_ms, void** out_user) now reports which transaction completed (the engine tells transA from transB).
  • pollingStart() / pollingEnd() cover the ESP32-C6 split-polling path.
  • getActualFrequencyKhz() replaces spi_device_get_actual_freq.
  • allocateInternal() / freeInternal() replace heap_caps_malloc(INTERNAL|8BIT) for the LED source buffer.
  • resetPin() / getPinRouting() + kPinRoutingUnknown replace gpio_reset_pin and gpio_get_io_config (ESP32 SPI clockless engine: reused SPI channel loses its pin after another driver used it #4907).
  • Flag constants kSpiDeviceHalfDuplex, kSpiTransModeDio/Qio and kSpiWaitForeverMs; the ESP implementation FL_STATIC_ASSERTs them against the SDK.

gpio_set_drive_capability is not called by the engine (only comments mention it), so I did not add it.

ESP implementation. Transaction descriptors now live in a persistent ring. The old queueTransaction() queued a stack descriptor, which had no callers until now. The device config also sets clock_source = SPI_CLK_SRC_DEFAULT, as the engine did. Bus and device config values, host (SPI2), DMA channel and allocation caps match the previous direct calls.

Mock.

  • Completed transactions go to a list that pollTransaction() drains.
  • The mock now accepts SCLK=-1, which the clockless driver uses; it rejects a bus with no data pin instead.
  • It models pin routing (simulatePinReroute).
  • It counts bus init/free and device removal.

Host build compatibility. The host library is C++11, so the engine drops binary literals, structured bindings and a multi-statement constexpr. The compiled code is unchanged. The unused getSpiMosiSignalIndex and the never-defined ISR declarations, both ESP-typed, are removed.

Evidence

  • bash test channel_driver_spi: pass (7 cases). The new case ChannelEngineSpi - idle channel rebuilds its bus after a pin reroute (#4907) runs three frames:
    • Frame 1 creates the channel.
    • Frame 2, with no reroute, reuses it (bus init count stays 1).
    • Frame 3 runs after simulatePinReroute(5). It checks remove-device = 1, free-bus = 1, bus init = 2, MOSI = 5, and that the device is attached again.
      With the release block disabled (... && false), this case fails (7 cases, 1 failed).
  • canHandle tests now run on the host (the #ifdef ESP32 gate is gone).
  • bash test --cpp: 468/469. The one failure was fl_codec hitting the 20 s runner watchdog in the minimp3 SIMD-vs-scalar timing test while the machine was loaded. It passes in --debug and in a quick re-run.
  • bash lint: clean.
  • ESP32-C6 on /dev/ttyACM1, bash autoresearch esp32c6 --spi --upload-port /dev/ttyACM1 --skip-lint:
    • master baseline: TEST SPI lanes=1 leds=100 PASS 181ms, Tests 4/4.
    • this branch: TEST SPI lanes=1 leds=100 PASS 182ms, Tests 4/4, RESULT PASS.

Summary by CodeRabbit

  • Bug Fixes
    • Improved ESP32 SPI channel handling when pins are rerouted, ensuring idle channels rebuild their bus as needed while reusing it when routing is unchanged.
    • Improved SPI transaction polling and completion tracking across ESP32 and mock platforms.

…4910)

ChannelEngineSpi called ESP-IDF directly (spi_bus_*, spi_device_*,
heap_caps_*, gpio_reset_pin, gpio_get_io_config), so its host tests were
#ifdef ESP32 and covered only canHandle(). The engine now takes an
ISpiPeripheral (default: SpiPeripheralESP on device, SpiPeripheralMock on
stub builds), as ChannelEngineRMT does with IRMT5Peripheral.

ISpiPeripheral gains only what the engine needs: pollTransaction() reports
the completed transaction's user pointer, pollingStart()/pollingEnd() for
the ESP32-C6 split-polling path, getActualFrequencyKhz(),
allocateInternal()/freeInternal(), resetPin() and getPinRouting(). The ESP
implementation now keeps transaction descriptors in a persistent ring
(the old queueTransaction() queued a stack descriptor) and sets the
default clock source, matching the engine's previous direct calls.

The SPI mock completes transactions into a list that pollTransaction()
drains, accepts SCLK=-1, models pin routing and counts bus init/free and
device removal. A new host test covers #4907: an idle channel whose pin is
rerouted releases its device and host and re-initializes the bus.

Fixes #4910
@zackees
zackees enabled auto-merge November 21, 2026 21:23
@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

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
Walkthrough

The SPI channel driver now routes hardware, memory, and pin-routing operations through a peripheral interface. ESP and mock implementations provide the interface. Host tests exercise channel reuse and rebuilding after simulated pin rerouting.

Changes: SPI peripheral abstraction and channel driver

Layer / File(s) Summary
Peripheral and channel contracts
src/platforms/esp/32/drivers/spi/channel_driver_spi.h, src/platforms/esp/32/drivers/spi/ispi_peripheral.h, src/platforms/esp/32/drivers/spi/spi_peripheral_esp.h
The channel state and peripheral interfaces use platform-independent pin and host values, transaction types, and methods for polling, memory allocation, and pin routing. ChannelEngineSpi accepts an injected peripheral.
ESP and mock peripheral operations
src/platforms/esp/32/drivers/spi/spi_peripheral_esp.cpp.hpp, src/platforms/shared/mock/esp/32/drivers/spi_peripheral_mock.*, tests/platforms/esp/32/drivers/spi/spi_peripheral.cpp
The ESP and mock implementations provide the expanded peripheral operations. The mock tracks completed transactions, routing, and bus and device lifecycle counts. The peripheral test checks a bus configuration with no data pin.
Channel setup, reuse, and cleanup
src/platforms/esp/32/drivers/spi/channel_driver_spi.cpp.hpp, tests/platforms/esp/32/drivers/spi/channel_driver_spi.cpp
The driver uses the peripheral for bus and device setup, resource allocation, and cleanup. It compares pin routing when reusing an idle channel. Tests cover reuse with unchanged routing and rebuilding after simulated rerouting.
Transaction submission and completion
src/platforms/esp/32/drivers/spi/channel_driver_spi.cpp.hpp
The driver submits queued and polling transactions through the peripheral interface and checks completion through the abstraction.

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Refactor

Possibly related PRs

  • FastLED/FastLED#4909: Adds the idle-channel pin-routing check and release-and-reinitialize behavior exercised by this PR’s host test.
Merge Risk: Moderate · up to 96e55

The change makes the SPI driver testable on the host through a peripheral interface. Before merge, replace the new FASTLED_* stub checks with the permitted FL_* detection. Fix the mock's blocking-poll completion so host tests cover frames larger than one staging buffer. Consider bounding the ESP descriptor ring against the device queue depth. The in-tree driver does not hit the ring reuse today.

- [ ] 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

@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Nov 21, 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 21, 2026
@zackees
zackees added this pull request to the merge queue Nov 21, 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)
esp32dev 287,783 ($\color{green}\downarrow$ 4) 18,747 (Same)
esp32s3 320,383 (Same) 22,499 (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 21, 2026
Merged via the queue into master with commit 8a7c636 Nov 21, 2026
133 of 135 checks passed
@zackees
zackees deleted the refactor/4910-spi-engine-peripheral branch November 21, 2026 21:34
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Nov 21, 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 21, 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.

test(esp32,spi): route ChannelEngineSpi through ISpiPeripheral so host tests can cover it

1 participant