Start a DMA Transfer on Raspberry Pi Pico With dma_channel_configure
Configure and start an RP2040 DMA transfer with dma_channel_get_default_config and dma_channel_configure in Pico SDK 2.1, including fills, rings and completion.

The RP2040 has twelve DMA channels, and the Pico SDK reduces starting one to a single call, dma_channel_configure. Everything the call does not take as an argument lives in a small configuration structure that you fill with helper functions before the call. This post walks through that structure field by field with a Raspberry Pi Pico and Pico SDK 2.1: a plain memory copy, a fill that writes one value across a block, a ring that repeats a pattern, and the ways to find out when a transfer has finished. The functions are documented in the Pico SDK hardware_dma reference and the controller itself in the DMA section of the RP2040 datasheet.
If DMA is new to you, the overview post explains the model first. For where this call fits among peripherals and chaining, see the complete DMA guide.
The three steps
Every transfer in the SDK follows the same sequence:
Claim a channel.
dma_claim_unused_channel(true)returns the number of a free channel and marks it as used so that no other library takes it. Thetrueargument makes the call panic if none is free, which is the right behavior in a program that cannot proceed without one.Build a configuration.
dma_channel_get_default_config(channel)returns a structure of defaults, andchannel_config_set_*functions change the fields you care about.Configure and start.
dma_channel_configure(channel, &config, write_addr, read_addr, count, trigger)writes the addresses, count and configuration to the channel's registers, and starts it iftriggeris true.
The order of the addresses in the call catches people from other platforms: the destination comes first, then the source. It matches the order of arguments to memcpy, and it differs from the order in the STM32 HAL.
What the defaults are
The default configuration, from the SDK source, is a useful reference because it tells you what to override:
| Field | Default |
|---|---|
| Transfer size | 32 bits |
| Read increment | On |
| Write increment | Off |
| Request source | Permanent, "always ready" |
| Chain to | Itself, meaning no chaining |
| Ring | Off |
| Priority | Normal |
| Byte swap | Off |
Two defaults surprise. Write increment starts off, so a transfer that is supposed to fill an array writes every unit to the same address until you turn the increment on. And the request source is "permanent", which means the channel runs flat out with no peripheral pacing it, correct for memory-to-memory and wrong for a peripheral, where you must name the peripheral's request with channel_config_set_dreq.
A memory copy
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/dma.h"
#define WORDS 64
static uint32_t src[WORDS];
static uint32_t dst[WORDS];
int main(void) {
stdio_init_all();
for (uint32_t i = 0; i < WORDS; i++) {
src[i] = 0xAB000000u | i;
dst[i] = 0;
}
int ch = dma_claim_unused_channel(true);
dma_channel_config cfg = dma_channel_get_default_config(ch);
channel_config_set_transfer_data_size(&cfg, DMA_SIZE_32);
channel_config_set_read_increment(&cfg, true);
channel_config_set_write_increment(&cfg, true);
dma_channel_configure(ch, &cfg, dst, src, WORDS, true);
dma_channel_wait_for_finish_blocking(ch);
printf("dst[0]=0x%lX dst[63]=0x%lX\n",
(unsigned long)dst[0], (unsigned long)dst[WORDS - 1]);
dma_channel_unclaim(ch);
return 0;
}dst[0]=0xAB000000 dst[63]=0xAB00003FThe transfer count is the number of transfers, in units of the configured size, not bytes. Sixty-four 32-bit transfers move 256 bytes. If you change the size to DMA_SIZE_8 for a byte copy, the count becomes the number of bytes, and the arrays should be uint8_t to match. dma_channel_wait_for_finish_blocking spins until the channel is no longer busy, which suits a demonstration and wastes the CPU time DMA is supposed to save, so a real program takes an interrupt or checks dma_channel_is_busy between other tasks.
Finally dma_channel_unclaim returns the channel to the pool. A program that claims in a loop without unclaiming runs out of channels after twelve iterations.
A fill
A fill turns read increment off and write increment on. The single source word is read repeatedly and written across the destination.
static uint32_t pattern = 0xDEADBEEF;
static uint32_t buffer[32];
void dma_fill(void) {
int ch = dma_claim_unused_channel(true);
dma_channel_config cfg = dma_channel_get_default_config(ch);
channel_config_set_transfer_data_size(&cfg, DMA_SIZE_32);
channel_config_set_read_increment(&cfg, false);
channel_config_set_write_increment(&cfg, true);
dma_channel_configure(ch, &cfg, buffer, &pattern, 32, true);
dma_channel_wait_for_finish_blocking(ch);
dma_channel_unclaim(ch);
}buffer[0]=0xDEADBEEF buffer[15]=0xDEADBEEF buffer[31]=0xDEADBEEFThis is the model for clearing a frame buffer or initializing a large array while the CPU does something else: pass false as the trigger to dma_channel_configure, call dma_channel_start(ch) and carry on, and check dma_channel_is_busy(ch) before using the buffer.
A ring: repeating a pattern
The RP2040 DMA can wrap an address inside a power-of-two window. That is the ring feature, set with channel_config_set_ring(&cfg, write, size_bits). The first argument chooses whether the ring applies to the write address, true, or the read address, false, and the second is the size of the window as a power of two in bytes. A ring on the read side repeats a short pattern across a long destination.
static uint32_t pattern[4] __attribute__((aligned(16))) = {1, 2, 3, 4};
static uint32_t out[12];
void dma_repeat(void) {
int ch = dma_claim_unused_channel(true);
dma_channel_config cfg = dma_channel_get_default_config(ch);
channel_config_set_transfer_data_size(&cfg, DMA_SIZE_32);
channel_config_set_read_increment(&cfg, true);
channel_config_set_write_increment(&cfg, true);
channel_config_set_ring(&cfg, false, 4); // read address wraps within 2^4 = 16 bytes
dma_channel_configure(ch, &cfg, out, pattern, 12, true);
dma_channel_wait_for_finish_blocking(ch);
dma_channel_unclaim(ch);
}out: 1 2 3 4 1 2 3 4 1 2 3 4The ring window must be aligned to its size, which is why the pattern array carries aligned(16). A ring of 2^4 bytes needs a start address that is a multiple of 16, or the wrap happens at the wrong place and the data come out shifted. With a write ring and a fixed read address you get a circular receive buffer instead, where the controller wraps the destination without being re-armed, and a peripheral request paces it.
Pacing with a peripheral request
Changing the request source is how the same call becomes a peripheral transfer. A UART transmit, for instance, sets the request to the UART's transmit signal and turns write increment off, because the destination is the data register.
#include "hardware/uart.h"
void dma_uart_send(const char *msg, uint len) {
int ch = dma_claim_unused_channel(true);
dma_channel_config cfg = dma_channel_get_default_config(ch);
channel_config_set_transfer_data_size(&cfg, DMA_SIZE_8);
channel_config_set_read_increment(&cfg, true);
channel_config_set_write_increment(&cfg, false);
channel_config_set_dreq(&cfg, uart_get_dreq(uart0, true));
dma_channel_configure(ch, &cfg, &uart_get_hw(uart0)->dr, msg, len, true);
}uart_get_dreq(uart0, true) returns the request number for transmit, and the controller then writes one byte each time the UART's transmit FIFO has room. The message buffer must remain valid until the transfer ends, as with any DMA, so pass a static string or a buffer that outlives the call.
Knowing when it is done
Three ways to wait, from simplest to most useful:
Block with
dma_channel_wait_for_finish_blocking(ch), as in the examples above.Poll with
dma_channel_is_busy(ch), between other work, for a transfer you check once per main-loop pass.Interrupt by enabling a channel interrupt and handling it. Each channel can raise one of two shared DMA interrupts, selected by the
dma_channel_set_irq0_enabledordma_channel_set_irq1_enabledcalls.
static volatile bool dma_finished;
static int ch_global;
static void dma_handler(void) {
dma_channel_acknowledge_irq0(ch_global); // clear the flag, or the IRQ repeats
dma_finished = true;
}
void dma_setup_irq(int ch) {
ch_global = ch;
dma_channel_set_irq0_enabled(ch, true);
irq_set_exclusive_handler(DMA_IRQ_0, dma_handler);
irq_set_enabled(DMA_IRQ_0, true);
}Clearing the flag in the handler is not optional. The interrupt line stays asserted until you acknowledge it, and a handler that returns without doing so is entered again at once, which hangs the program.
What goes wrong
If nothing is written and the program continues, the channel was probably not triggered, so check the last argument of dma_channel_configure or call dma_channel_start. If only the first element of the destination changes, write increment is still off. If a peripheral transfer runs instantly and fills the buffer with garbage, the request source was left on the permanent default and the controller read the data register before the peripheral had anything. And a program that panics at the second dma_claim_unused_channel after a few hundred iterations has forgotten to unclaim.


