FastLED 3.10.10
Loading...
Searching...
No Matches

◆ transmitBatch()

bool fl::SpiChannelEngineAdapter::transmitBatch ( fl::span< const ChannelDataPtr > channels)
private

Transmit a batch of channels sharing one SPI configuration.

Parameters
channelsChannels to transmit (must have matching pins, host, and rate)
Returns
true on success, false on error

Definition at line 404 of file spi_channel_adapter.cpp.hpp.

404 {
405 if (channels.empty()) {
406 return true;
407 }
408
409 // Extract clock pin and data pin from first channel (all same in batch)
410 const auto& chipset = channels[0]->getChipset();
411 if (!chipset.is<SpiChipsetConfig>()) {
412 FL_WARN("SpiChannelEngineAdapter: Non-SPI chipset in transmitBatch");
413 return false;
414 }
415
416 const SpiChipsetConfig& spiConfig = chipset.get<SpiChipsetConfig>();
417 int clockPin = spiConfig.clockPin;
418 int dataPin = channels[0]->getPin(); // MOSI pin from channel config
419
420 // Select controller for this clock pin
421 int controllerIndex = selectControllerForClockPin(clockPin, spiConfig.spiBus);
422 if (controllerIndex < 0) {
423 FL_WARN("SpiChannelEngineAdapter: No available controller for clock pin " << clockPin);
424 return false;
425 }
426
427 ControllerInfo& ctrl = mControllers[controllerIndex];
428
429 // Initialize controller if needed
430 if (!initializeControllerIfNeeded(ctrl, clockPin, dataPin,
431 spiConfig.timing.clock_hz)) {
432 return false;
433 }
434
435 // Transmit using selected controller
436 for (const auto& channel : channels) {
437 if (!channel) {
438 continue;
439 }
440
441 // Mark channel as in use
442 channel->setInUse(true);
443
444 // Get encoded data
445 const auto& data = channel->getData();
446 if (data.empty()) {
447 FL_WARN("SpiChannelEngineAdapter: Empty channel data");
448 channel->setInUse(false);
449 continue;
450 }
451
452 FL_DBG("SpiChannelEngineAdapter: Transmitting channel via " << ctrl.name << " (pin " << channel->getPin() << ", " << data.size() << " bytes)");
453
454 if (ctrl.controller->supportsStreaming()) {
455 // The backend retains or copies encoded bytes until completion.
456 // advanceStreaming() submits one channel at a time, preserving
457 // pin/rate changes and enqueue order without blocking show().
458 if (!ctrl.controller->transmitStream(channel)) {
459 channel->setInUse(false);
460 return false;
461 }
462 return true;
463 }
464
465 // Acquire DMA buffer
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);
470 return false;
471 }
472
473 // Copy data to DMA buffer
474 fl::span<u8> buffer = dmaBuffer.data();
475 if (buffer.size() < data.size()) {
476 FL_WARN("SpiChannelEngineAdapter: DMA buffer too small (" << buffer.size() << " < " << data.size() << ")");
477 channel->setInUse(false);
478 return false;
479 }
480
481 fl::memcpy(buffer.data(), data.data(), data.size());
482
483 // Transmit (async mode for non-blocking operation)
484 if (!ctrl.controller->transmit(TransmitMode::ASYNC)) {
485 FL_WARN("SpiChannelEngineAdapter: Transmission failed");
486 channel->setInUse(false);
487 return false;
488 }
489
490 FL_DBG("SpiChannelEngineAdapter: Transmission queued successfully");
491 }
492
493 // Wait for transmission to complete (synchronous for now)
494 // TODO: Make fully async by returning BUSY state and polling in poll()
495 if (!ctrl.controller->waitComplete(1000)) { // 1 second timeout
496 FL_WARN("SpiChannelEngineAdapter: Transmission timeout");
497 return false;
498 }
499
500 FL_DBG("SpiChannelEngineAdapter: Batch transmission complete");
501 return true;
502}
fl::vector< ControllerInfo > mControllers
All managed controllers.
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.
constexpr bool empty() const FL_NO_EXCEPT
Definition span.h:535
const T * data() const FL_NO_EXCEPT
Definition span.h:486
constexpr fl::size size() const FL_NO_EXCEPT
Definition span.h:483
#define FL_WARN(...)
Definition log.h:437
#define FL_DBG
Definition log.h:553
FASTLED_FORCE_INLINE void * memcpy(void *dest, const void *src, size_t n) FL_NO_EXCEPT
Definition cstring.h:136
Information about a registered SPI hardware controller.

References fl::SpiEncoder::clock_hz, fl::SpiChipsetConfig::clockPin, fl::SpiChannelEngineAdapter::ControllerInfo::controller, fl::span< T, Extent >::data(), FL_DBG, fl::FL_NO_EXCEPT, FL_WARN, initializeControllerIfNeeded(), mControllers, fl::memcpy(), fl::SpiChannelEngineAdapter::ControllerInfo::name, selectControllerForClockPin(), fl::basic_string::size(), fl::span< T, Extent >::size(), fl::SpiChipsetConfig::spiBus, and fl::SpiChipsetConfig::timing.

Referenced by advanceStreaming(), and show().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: