FastLED 3.10.8
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 stdint.h:208

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

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

References acquireBuffer(), fl::span< T, Extent >::data(), fl::expected< T, E >::failure(), FL_LOG_SPI, fl::FL_NO_EXCEPT, FL_WARN, isReady(), fl::adl_barrier::make_unique(), fl::millis(), fl::adl_barrier::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: