Repository navigation
refactor(esp32,spi): route ChannelEngineSpi through ISpiPeripheral (#4910) - #5363
Conversation
…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
|
Caution Review failedThe pull request is closed. Note
|
| 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.
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.
Memory usage (this PR vs. its merge base)Change in
|
Fixes #4910
What
ChannelEngineSpicalled ESP-IDF directly, so its host tests were#ifdef ESP32and covered onlycanHandle(). The engine now takes adetail::ISpiPeripheral&, the same wayChannelEngineRMTtakesIRMT5Peripheral. On device the default isSpiPeripheralESP::instance(), and stub builds useSpiPeripheralMock::instance(). The engine now compiles forFASTLED_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()replacesspi_device_get_actual_freq.allocateInternal()/freeInternal()replaceheap_caps_malloc(INTERNAL|8BIT)for the LED source buffer.resetPin()/getPinRouting()+kPinRoutingUnknownreplacegpio_reset_pinandgpio_get_io_config(ESP32 SPI clockless engine: reused SPI channel loses its pin after another driver used it #4907).kSpiDeviceHalfDuplex,kSpiTransModeDio/QioandkSpiWaitForeverMs; the ESP implementationFL_STATIC_ASSERTs them against the SDK.gpio_set_drive_capabilityis 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 setsclock_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.
pollTransaction()drains.simulatePinReroute).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 unusedgetSpiMosiSignalIndexand the never-defined ISR declarations, both ESP-typed, are removed.Evidence
bash test channel_driver_spi: pass (7 cases). The new caseChannelEngineSpi - idle channel rebuilds its bus after a pin reroute (#4907)runs three frames: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).canHandletests now run on the host (the#ifdef ESP32gate is gone).bash test --cpp: 468/469. The one failure wasfl_codechitting the 20 s runner watchdog in the minimp3 SIMD-vs-scalar timing test while the machine was loaded. It passes in--debugand in a quick re-run.bash lint: clean.bash autoresearch esp32c6 --spi --upload-port /dev/ttyACM1 --skip-lint:TEST SPI lanes=1 leds=100 PASS 181ms, Tests 4/4.TEST SPI lanes=1 leds=100 PASS 182ms, Tests 4/4,RESULT PASS.Summary by CodeRabbit