| 329 | } |
| 330 | |
| 331 | DMABuffer SPIDualSAMD21::acquireDMABuffer(size_t bytes_per_lane) { |
| 332 | if (!mInitialized) { |
| 333 | return DMABuffer(SPIError::NOT_INITIALIZED); |
| 334 | } |
| 335 | |
| 336 | // Auto-wait if previous transmission still active |
| 337 | if (mTransactionActive) { |
| 338 | if (!waitComplete()) { |
| 339 | return DMABuffer(SPIError::BUSY); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // For dual-lane SPI: total size = bytes_per_lane × 2 (interleaved) |
| 344 | constexpr size_t num_lanes = 2; |
| 345 | const size_t total_size = bytes_per_lane * num_lanes; |
| 346 | |
| 347 | // Validate size against platform max (256KB practical limit for embedded) |
| 348 | constexpr size_t MAX_SIZE = 256 * 1024; |
| 349 | if (total_size > MAX_SIZE) { |
| 350 | return DMABuffer(SPIError::BUFFER_TOO_LARGE); |
| 351 | } |
| 352 | |
| 353 | // Allocate new DMABuffer - it will manage its own memory |
| 354 | mDMABuffer = DMABuffer(total_size); |
| 355 | if (!mDMABuffer.ok()) { |
| 356 | return DMABuffer(SPIError::ALLOCATION_FAILED); |
| 357 | } |
| 358 | |
| 359 | mBufferAcquired = true; |
| 360 | |
| 361 | // Return the DMABuffer |
| 362 | return mDMABuffer; |
| 363 | } |
| 364 | |
| 365 | bool SPIDualSAMD21::transmit(TransmitMode mode) { |
| 366 | if (!mInitialized || !mBufferAcquired) { |