ESP32 DMA — General Purpose Guide
What DMA Is
Direct Memory Access lets a peripheral read or write memory directly, without the CPU copying each byte. The CPU sets up a transfer once, then goes off and does other work while hardware moves the data. It only gets interrupted when the transfer finishes, or hits a defined checkpoint.
Two Different DMA Architectures
DMA capability and implementation differ significantly across ESP32 generations. This guide separates classic ESP32 (original Xtensa LX6 chip) from newer variants (S2/S3/C3/C6/H2), which use a different, more unified DMA architecture called GDMA. Mixing the two casually is a common source of inaccurate assumptions, so they're treated separately below.
Classic ESP32 (Xtensa LX6) — Peripheral DMA Support
|
Peripheral |
DMA Support |
|
SPI (HSPI/VSPI) |
Yes, hardware DMA |
|
I2S |
Yes, hardware DMA |
|
UART |
Yes, hardware DMA support; exact implementation depends on driver/IDF version |
|
SD/SDMMC |
Yes, hardware DMA |
|
I2C |
Does not use the general-purpose DMA engine the way SPI/I2S/UART do |
|
CAN/TWAI |
No |
|
RMT |
Has its own internal buffer memory, not the same DMA engine |
A note on crypto (AES/SHA): these accelerators have their own internal hardware data-movement mechanisms, which are distinct from — and shouldn't be equated with — the general peripheral DMA engine used by SPI/I2S/UART. They're left out of the table above deliberately, since presenting them alongside ordinary DMA peripherals overstates the similarity.
Newer ESP32 Variants (S2/S3/C3/C6/H2) — GDMA
Many newer ESP32 variants use a GDMA-based architecture, extending DMA support to additional peripherals such as I2C on supported chips, and generally offering more flexible memory access than the classic chip. Exact capability still depends on the specific chip and the ESP-IDF driver version in use, so specifics should be checked against the datasheet/IDF docs for the exact target rather than assumed to carry over from the classic chip or between newer variants themselves.
Descriptor-Based Model
DMA on ESP32 doesn't work with one flat buffer — it works with a linked list of descriptors (lldesc_t on classic ESP32, dma_descriptor_t on newer IDF versions/GDMA). Each descriptor holds:
• Pointer to a data buffer
• length — actual bytes to transfer
• size — allocated buffer size
• owner bit — flags whether CPU or DMA hardware currently controls it
• eof flag — marks end-of-frame (triggers an interrupt if enabled)
• Pointer to the next descriptor
This chaining is what allows:
• Transfers larger than a single descriptor's max buffer size (on classic ESP32, a commonly cited rule of thumb is about 4095 bytes per descriptor — this is hardware/driver-specific and not a universal constant across ESP32 generations)
• Scatter-gather transfers (non-contiguous buffers treated as one logical stream)
• Circular/ring buffers for continuous streaming (e.g. continuous audio out, continuous ADC sampling) without CPU re-arming after every chunk
Memory Rules
• DMA buffers should be allocated as DMA-capable memory to guarantee they're in DMA-accessible internal SRAM
• On classic ESP32, DMA generally cannot access external PSRAM directly — data has to be staged through internal RAM first. Newer chips substantially improve DMA/PSRAM support, but still have alignment and cache-coherency rules that need to be checked per chip
• Buffers typically need 4-byte alignment, and some peripherals also want the length aligned to a fixed boundary (varies by peripheral)
• Never let a buffer go out of scope or get freed while DMA still owns it — check the owner bit or wait for completion first
Interrupts and Synchronization
• Instead of interrupting per-byte, DMA interrupts fire on events like IN_DONE, OUT_DONE, or EOF — i.e. once a whole descriptor or whole descriptor chain completes
• The owner bit on each descriptor prevents the CPU and DMA engine from touching the same buffer simultaneously — always confirm ownership before reading/writing a buffer manually
• For continuous/streaming use cases, a common pattern is double buffering: while DMA fills/drains buffer A, the CPU processes buffer B, then they swap
RX/TX Channels
Peripherals like SPI and I2S commonly have separate DMA channels for transmit and receive, which is what allows full-duplex transfers (simultaneous send and receive) without extra CPU juggling. This isn't a universal rule for every peripheral, though — the exact DMA channel architecture differs between ESP32 generations and peripherals, so it should be confirmed for the specific peripheral and chip in use.
Throughput Characteristics
• Transfer speed on the wire is largely determined by the peripheral's own clock (SPI clock, I2S bit clock, UART baud rate) rather than CPU instruction speed, since the CPU isn't in the data path for each byte
• That said, DMA doesn't guarantee a perfectly fixed or fully predictable system-level transfer rate — CPU load, other interrupts, bus contention, cache activity, and memory availability can still affect latency and overall throughput in practice
Typical Workflow (Conceptual)
1. Allocate DMA-capable buffer(s).
2. Fill descriptor(s) with buffer pointer and length, chain them if needed.
3. Hand the descriptor chain to the peripheral's DMA controller.
4. Peripheral starts moving data using its own clock; CPU is free for other work.
5. On completion (or EOF), an interrupt fires; CPU reads results or refills buffers for the next round.
6. For continuous operation, re-arm descriptors before the DMA engine reaches the end of the chain (or use a circular descriptor list).
Note
This is a conceptual reference, not a substitute for the ESP-IDF Technical Reference Manual for your specific chip. Peripheral DMA capability, descriptor formats, and buffer constraints should be verified against the datasheet and IDF driver documentation for the exact ESP32 variant in use before being relied on for a design.

No comments:
Post a Comment