| 182 | // ============================================================================ |
| 183 | |
| 184 | Result<Transaction> Device::writeAsync(const u8* data, size_t size) { |
| 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)"); |
| 236 | return Result<Transaction>::success(fl::move(txn)); |
| 237 | } |
| 238 | |
| 239 | // Commented out - methods not declared in header |
| 240 | // Result<Transaction> Device::readAsync(uint8_t* buffer, size_t size) { |