9#include "platforms/shared/spi_hw_1.h"
20 const char* adapterName
23 if (hwControllers.empty()) {
24 FL_WARN(
"SpiChannelEngineAdapter::create: No controllers provided");
28 if (hwControllers.size() != priorities.size() || hwControllers.size() != names.size()) {
29 FL_WARN(
"SpiChannelEngineAdapter::create: Size mismatch in arguments");
33 if (!adapterName || adapterName[0] ==
'\0') {
34 FL_WARN(
"SpiChannelEngineAdapter::create: Empty adapter name");
42 for (
size_t i = 0; i < hwControllers.size(); i++) {
43 if (!hwControllers[i]) {
44 FL_WARN(
"SpiChannelEngineAdapter: Null controller at index " << i);
48 adapter->mControllers.emplace_back(hwControllers[i], priorities[i], names[i]);
50 FL_DBG(
"SpiChannelEngineAdapter: Registered controller '" << names[i] <<
"' (priority " << priorities[i] <<
", lanes: " << (
static_cast<int>(hwControllers[i]->getLaneCount())) <<
")");
53 if (adapter->mControllers.empty()) {
54 FL_WARN(
"SpiChannelEngineAdapter: No valid controllers registered");
58 for (
auto& ctrl : adapter->mControllers) {
72 FL_DBG(
"SpiChannelEngineAdapter: Destructor for '" <<
mName <<
"'");
79 while (
poll() != DriverState::READY) {
86 if (ctrl.controller && ctrl.controller->isInitialized()) {
87 ctrl.controller->end();
89 if (ctrl.controller) ctrl.controller->setStreamCompletionCallback(
nullptr,
nullptr);
96 if (ctrl.priority > maxPriority) {
97 maxPriority = ctrl.priority;
106 if (assignment.clockPin == clockPin && assignment.requestedBus == requestedBus) {
107 return assignment.controllerIndex;
113 int bestPriority = -1;
132 if (bestIndex >= 0) {
143 if (!ctrl.isInitialized) {
148 for (
int assignedPin : ctrl.assignedClockPins) {
149 if (assignedPin == clockPin) {
161 if (ctrl.isInitialized) {
162 if (!ctrl.assignedClockPins.empty() &&
163 ctrl.assignedClockPins[0] == clockPin &&
164 ctrl.configuredDataPin == dataPin &&
165 ctrl.configuredClockSpeedHz == clockSpeedHz) {
168 ctrl.controller->end();
169 ctrl.isInitialized =
false;
170 ctrl.assignedClockPins.clear();
174 u8 lanes = ctrl.controller->getLaneCount();
177 SpiHw1::Config config;
178 config.bus_num =
static_cast<fl::u8>(ctrl.controller->getBusId());
179 config.clock_pin = clockPin;
180 config.data_pin = dataPin;
181 config.clock_speed_hz = clockSpeedHz;
182 config.max_transfer_sz = 65536;
184 if (!ctrl.controller->begin(&config)) {
185 FL_WARN(
"SpiChannelEngineAdapter: Failed to initialize " << ctrl.name);
190 FL_WARN(
"SpiChannelEngineAdapter: Multi-lane init not yet implemented");
194 ctrl.isInitialized =
true;
195 ctrl.configuredDataPin = dataPin;
196 ctrl.configuredClockSpeedHz = clockSpeedHz;
197 ctrl.assignedClockPins.push_back(clockPin);
199 FL_DBG(
"SpiChannelEngineAdapter: Initialized " << ctrl.name <<
" with clock pin " << clockPin);
218 return data->isSpi();
223 FL_WARN(
"SpiChannelEngineAdapter: Null channel data passed to enqueue()");
229 FL_WARN(
"SpiChannelEngineAdapter: Cannot handle non-SPI channel data (chipset mismatch)");
253 if (ctrl.controller->supportsStreaming()) {
264 FL_DBG(
"SpiChannelEngineAdapter: Grouped into " << groups.size() <<
" SPI configuration groups");
267 for (
size_t i = 0; i < groups.size(); i++) {
270 FL_DBG(
"SpiChannelEngineAdapter: Transmitting group with clock pin " << group.
clockPin <<
" (" << group.
channels.
size() <<
" channels)");
273 FL_WARN(
"SpiChannelEngineAdapter: Failed to transmit batch for clock pin " << group.
clockPin);
278 FL_DBG(
"SpiChannelEngineAdapter: show() complete");
284 bool anyBusy =
false;
286 if (ctrl.controller && ctrl.controller->isBusy()) {
293 return DriverState::BUSY;
302 channel->setInUse(
false);
310 return DriverState::DRAINING;
313 return DriverState::READY;
326 if (ctrl.controller->isBusy())
return DriverState::BUSY;
331 if (previous) previous->setInUse(
false);
337 if (!channel)
continue;
342 FL_ERROR(
"SpiChannelEngineAdapter: SPI frame submission failed on pin "
343 << channel->getPin() <<
" (" << channel->getData().size() <<
" bytes)");
344 channel->setInUse(
false);
348 if (ctrl.controller->isBusy())
return DriverState::BUSY;
350 channel->setInUse(
false);
364 for (
const auto& channel :
channels) {
370 const auto& chipset = channel->getChipset();
372 FL_WARN(
"SpiChannelEngineAdapter: Non-SPI chipset in groupByClockPin");
378 int dataPin = channel->getPin();
382 if (!groups.
empty()) {
410 const auto& chipset =
channels[0]->getChipset();
412 FL_WARN(
"SpiChannelEngineAdapter: Non-SPI chipset in transmitBatch");
418 int dataPin =
channels[0]->getPin();
422 if (controllerIndex < 0) {
423 FL_WARN(
"SpiChannelEngineAdapter: No available controller for clock pin " << clockPin);
436 for (
const auto& channel :
channels) {
442 channel->setInUse(
true);
445 const auto& data = channel->getData();
447 FL_WARN(
"SpiChannelEngineAdapter: Empty channel data");
448 channel->setInUse(
false);
452 FL_DBG(
"SpiChannelEngineAdapter: Transmitting channel via " << ctrl.
name <<
" (pin " << channel->getPin() <<
", " << data.
size() <<
" bytes)");
458 if (!ctrl.
controller->transmitStream(channel)) {
459 channel->setInUse(
false);
466 DMABuffer dmaBuffer = ctrl.
controller->acquireDMABuffer(data.size());
467 if (!dmaBuffer.ok()) {
468 FL_WARN(
"SpiChannelEngineAdapter: Failed to acquire DMA buffer (error " << (
static_cast<int>(dmaBuffer.error())) <<
")");
469 channel->setInUse(
false);
475 if (buffer.
size() < data.size()) {
476 FL_WARN(
"SpiChannelEngineAdapter: DMA buffer too small (" << buffer.
size() <<
" < " << data.size() <<
")");
477 channel->setInUse(
false);
484 if (!ctrl.
controller->transmit(TransmitMode::ASYNC)) {
485 FL_WARN(
"SpiChannelEngineAdapter: Transmission failed");
486 channel->setInUse(
false);
490 FL_DBG(
"SpiChannelEngineAdapter: Transmission queued successfully");
496 FL_WARN(
"SpiChannelEngineAdapter: Transmission timeout");
500 FL_DBG(
"SpiChannelEngineAdapter: Batch transmission complete");
CLEDController * controller
bool transmitBatch(fl::span< const ChannelDataPtr > channels) FL_NO_EXCEPT
Transmit a batch of channels sharing one SPI configuration.
fl::vector< ClockPinGroup > groupByClockPin(fl::span< const ChannelDataPtr > channels) FL_NO_EXCEPT
Group adjacent channels with matching pins, host, and clock rate.
DriverState advanceStreaming() FL_NO_EXCEPT
Advance ordered streaming channels after the preceding source is reaped.
size_t mNextStreamingChannel
void enqueue(ChannelDataPtr channelData) FL_NO_EXCEPT override
Enqueue channel data for transmission.
fl::string mName
Engine name for debugging.
int getPriority() const FL_NO_EXCEPT
Get maximum priority among all controllers.
void show() FL_NO_EXCEPT override
Trigger transmission of enqueued data.
static void streamCompletionThunk(void *context) FL_NO_EXCEPT
bool canHandle(const ChannelDataPtr &data) const FL_NO_EXCEPT override
Check if adapter can handle channel data.
fl::vector< ControllerInfo > mControllers
All managed controllers.
static fl::shared_ptr< SpiChannelEngineAdapter > create(fl::vector< fl::shared_ptr< SpiHwBase > > hwControllers, fl::vector< int > priorities, fl::vector< const char * > names, const char *adapterName) FL_NO_EXCEPT
Create unified adapter managing multiple controllers.
PollNeededCallbackSlot mPollNeededCallback
bool initializeControllerIfNeeded(ControllerInfo &ctrl, int clockPin, int dataPin, u32 clockSpeedHz) FL_NO_EXCEPT
Initialize controller if needed for this clock pin.
int selectControllerForClockPin(int clockPin, Esp32SpiBus requestedBus) FL_NO_EXCEPT
Select best controller for a given clock pin.
SpiChannelEngineAdapter(const char *name) FL_NO_EXCEPT
Private constructor - use create() factory method.
bool canControllerHandleClockPin(const ControllerInfo &ctrl, int clockPin) const FL_NO_EXCEPT
Check if controller can handle this clock pin.
DriverState poll() FL_NO_EXCEPT override
Query driver state and perform maintenance.
~SpiChannelEngineAdapter() FL_NO_EXCEPT override
Destructor.
void setPollNeededCallback(PollNeededCallback callback) FL_NO_EXCEPT override
Install the manager-owned poll-needed callback for ISR wakeups.
fl::vector< ChannelDataPtr > mEnqueuedChannels
Channels waiting for show()
fl::vector< ClockPinAssignment > mClockPinAssignments
Clock pin → controller mapping.
fl::vector< ChannelDataPtr > mTransmittingChannels
Channels currently transmitting.
fl::vector< ChannelDataPtr > channels
Channels sharing one SPI bus configuration.
fl::size size() const FL_NO_EXCEPT
const T * data() const FL_NO_EXCEPT
constexpr fl::size size() const FL_NO_EXCEPT
fl::size size() const FL_NO_EXCEPT
bool empty() const FL_NO_EXCEPT
void push_back(const T &value) FL_NO_EXCEPT
Centralized logging categories for FastLED hardware interfaces and subsystems.
shared_ptr< T > make_shared(Args &&... args) FL_NO_EXCEPT
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Esp32SpiBus
ESP-IDF SPI host selection for an individual clocked LED controller.
u8 u8 fract8 blur_amount FL_NO_EXCEPT
FASTLED_FORCE_INLINE void * memcpy(void *dest, const void *src, size_t n) FL_NO_EXCEPT
Base definition for an LED controller.
Adapter that wraps HW SPI controllers for ChannelManager.
Driver state with optional error message.
ISR-safe callback handle invoked when the manager should poll again.
fl::shared_ptr< SpiHwBase > controller
Hardware instance.
fl::string name
Name (e.g., "SPI2", "SPI3", "I2S0")
Information about a registered SPI hardware controller.
SpiEncoder timing
SPI encoder configuration.
Esp32SpiBus spiBus
ESP-IDF host override, or AUTO.
int clockPin
GPIO clock pin (SCK)
SPI chipset configuration (data + clock pins)
u32 clock_hz
SPI clock frequency in Hz (e.g., 6000000 for 6MHz)