Skip to content

SD performance: use the SAMD DMA SPI transfer for the 512-byte read/write payloads (help wanted) #20

Description

@baorepo

Background

#18 was fixed in v2.1.5 by sending SD data byte-by-byte. That is correct and — on SAMD — costs nothing versus the old block call, because the core's transfer(void*, size_t) is itself a per-byte loop. But the CPU still busy-waits through every byte: at 4 MHz SPI, roughly 1 ms of blocked CPU per 512-byte sector, on every sector read or written.

Opportunity

The Seeeduino/Adafruit SAMD core provides a DMA-capable overload:

void SPIClass::transfer(const void *txbuf, void *rxbuf, size_t count, bool block);

Expected gain is not raw throughput (bounded by the SPI clock) but CPU availability during sector I/O — important for sample-while-logging workloads (exactly the accelerometer logger from #18, where a 1 ms busy-wait per sector competes with the sampling loop), plus reduced jitter.

Constraints to design around

  1. Platform guard — this overload only exists on the SAMD core. Use #ifdef ARDUINO_ARCH_SAMD (or equivalent) with the byte-by-byte path as fallback on other cores.
  2. DMA channel exhaustion — inside the core, if the TX DMA channel allocation fails, the transfer is silently skipped. A PR needs either a documented compile-time opt-in or a runtime fallback for this case.
  3. Token bytes, CRC (transfer16), and the sdReadBytes() token-wait loop must stay single-byte — only the 512-byte data payload is a candidate for DMA.
  4. sdWriteSectors() is covered automatically (it routes through sdWriteBytes()).

Acceptance criteria for a PR

Hardware: any Wio Terminal + microSD. The reproduction firmware from #18 is a good starting workload.

help wanted — happy to review.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions