FastLED 3.9.15
Loading...
Searching...
No Matches

◆ writeAsync()

Result< Transaction > fl::spi::Device::writeAsync ( const u8 * data,
size_t size )

Begin asynchronous write operation (returns immediately)

Parameters
dataData to transmit (caller must keep alive until transaction completes)
sizeNumber of bytes to transmit
Returns
Result containing Transaction handle or error
Note
This is the primary transmission interface
Warning
Caller must ensure data remains valid until Transaction::wait() returns

Example:

uint8_t data[] = {0x01, 0x02, 0x03};
auto result = spi.writeAsync(data, sizeof(data));
if (result.ok()) {
result.value().wait(); // Wait for completion
}
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
unsigned char uint8_t
Definition s16x16x4.h:209

Definition at line 184 of file device.cpp.hpp.

184 {
185 if (!isReady()) {
186 return Result<Transaction>::failure(SPIError::NOT_INITIALIZED, "Device not initialized");
187 }
188
189 if (!data || size == 0) {
190 return Result<Transaction>::failure(SPIError::ALLOCATION_FAILED, "Invalid data or size");
191 }
192
193 // Check if an async operation is already in progress
194 if (pImpl->async_state.active) {
195 return Result<Transaction>::failure(SPIError::BUSY, "Another async operation is in progress");
196 }
197
198 // Acquire DMA buffer
199 DMABuffer buffer = acquireBuffer(size);
200 if (!buffer.ok()) {
201 FL_WARN("SPI Device: Failed to acquire DMA buffer for async write");
202 return Result<Transaction>::failure(buffer.error(), "Failed to acquire DMA buffer");
203 }
204
205 // Copy data to DMA buffer
206 fl::span<u8> buf_span = buffer.data();
207 if (buf_span.size() < size) {
208 FL_WARN("SPI Device: Buffer size mismatch");
209 return Result<Transaction>::failure(SPIError::BUFFER_TOO_LARGE, "Buffer size mismatch");
210 }
211
212 for (size_t i = 0; i < size; i++) {
213 buf_span[i] = data[i];
214 }
215
216 // Start async transmission
217 fl::optional<fl::task::Error> tx_result = transmit(buffer, true); // true = async
218 if (tx_result) { // If error is present
219 FL_WARN("SPI Device: Failed to start async transmission");
220 return Result<Transaction>::failure(SPIError::NOT_SUPPORTED, tx_result->message.c_str());
221 }
222
223 // Store async state
224 pImpl->async_state.active = true;
225 pImpl->async_state.tx_buffer = data;
226 pImpl->async_state.rx_buffer = nullptr;
227 pImpl->async_state.size = size;
228 pImpl->async_state.start_time = fl::millis();
229
230 // Create Transaction object with proper initialization
231 Transaction txn;
232 txn.pImpl = fl::make_unique<Transaction::Impl>(this);
233 txn.pImpl->completed = false; // Will complete when hardware finishes
234
235 FL_LOG_SPI("SPI Device: Async write started (" << size << " bytes)");
237}
static expected failure(E err, const char *msg=nullptr) FL_NOEXCEPT
Create error result.
Definition expected.h:115
static expected success(T value) FL_NOEXCEPT
Create successful result.
Definition expected.h:108
constexpr fl::size size() const FL_NOEXCEPT
Definition span.h:458
bool isReady() const
Check if device is initialized and ready for use.
friend class Transaction
Definition device.h:131
fl::optional< fl::task::Error > transmit(DMABuffer &buffer, bool async=true)
Transmit from previously acquired DMA buffer.
fl::unique_ptr< Impl > pImpl
Definition device.h:134
DMABuffer acquireBuffer(size_t size)
Acquire DMA-capable buffer for zero-copy transmission.
#define FL_LOG_SPI(X)
Serial Peripheral Interface (SPI) logging Logs SPI configuration, initialization, and transfers.
Definition log.h:474
#define FL_WARN(X)
Definition log.h:276
constexpr remove_reference< T >::type && move(T &&t) FL_NOEXCEPT
Definition s16x16x4.h:28
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.
fl::enable_if<!fl::is_array< T >::value, unique_ptr< T > >::type make_unique(Args &&... args) FL_NOEXCEPT
Definition unique_ptr.h:261
Optional< T > optional
Definition optional.h:16

References acquireBuffer(), fl::span< T, Extent >::data(), fl::expected< T, E >::failure(), FL_LOG_SPI, FL_WARN, isReady(), fl::make_unique(), fl::millis(), fl::fl::move(), pImpl, fl::spi::Transaction::pImpl, fl::span< T, Extent >::size(), fl::expected< T, E >::success(), Transaction, and transmit().

+ Here is the call graph for this function: