Chain RP2040 DMA Channels for Continuous Transfers
Chain RP2040 DMA channels with chain_to so one starts as the other finishes, and use a control channel to rotate through a list of buffers without the CPU.

A single DMA transfer ends when its count reaches zero, and then something has to start the next one. If that something is an interrupt handler, the gap between the two is the time it takes the CPU to notice and respond, and for a stream that gap is lost data. The RP2040 gives you a way to remove the CPU from that step: a channel can be configured so that finishing it triggers another channel. Chained channels start the next transfer in hardware, in a handful of bus cycles. This post builds two arrangements with Pico SDK 2.1 on a Raspberry Pi Pico: a simple ping-pong of two channels, and the more powerful control-channel pattern that rotates through a list of buffers. The behavior is specified in the DMA section of the RP2040 datasheet, and the function names in the Pico SDK hardware_dma reference.
The first DMA transfer post covers the configuration calls used below, and the complete DMA guide puts chaining in context.
What chaining does
Every channel has a CHAIN_TO field in its control register. When the channel's transfer count reaches zero, the controller triggers the channel named there, as though the CPU had written its trigger register. The default value is the channel's own number, which means "chain to myself", and the controller treats that as no chaining. In the SDK you set it with channel_config_set_chain_to(&cfg, other_channel).
Two properties make it useful.
First, the triggered channel starts from whatever configuration it holds. A channel keeps its read address, write address and count between triggers, with the count reloaded to the last value you wrote. So a channel that has finished a transfer and is triggered again repeats the same transfer, starting from its current addresses, which are wherever the previous run left them. Increments advanced them. Unless you reset an address, the next run continues past the end of the buffer. This is the main reason the chained designs below include an interrupt or a control channel to rewind the addresses.
Second, the handover involves no CPU. The trigger is a hardware signal inside the controller, not an interrupt, so no CPU latency enters the path between one buffer ending and the next beginning.
Ping-pong with two channels
The simplest chain is a loop of two. Channel A fills buffer A and then triggers B, B fills buffer B and then triggers A. The data source here is the ADC's FIFO, paced by its request signal, with the destination write address advancing through each buffer.
#include "pico/stdlib.h"
#include "hardware/adc.h"
#include "hardware/dma.h"
#include "hardware/irq.h"
#define N 128
static uint16_t buf_a[N];
static uint16_t buf_b[N];
static int ch_a, ch_b;
static volatile int last_done; // 0 = A finished, 1 = B finished
static volatile uint32_t done_count;
static void dma_isr(void) {
if (dma_channel_get_irq0_status(ch_a)) {
dma_channel_acknowledge_irq0(ch_a);
dma_channel_set_write_addr(ch_a, buf_a, false); // rewind for the next lap
last_done = 0;
done_count++;
}
if (dma_channel_get_irq0_status(ch_b)) {
dma_channel_acknowledge_irq0(ch_b);
dma_channel_set_write_addr(ch_b, buf_b, false);
last_done = 1;
done_count++;
}
}
void pingpong_start(void) {
adc_init();
adc_gpio_init(26);
adc_select_input(0);
adc_fifo_setup(true, true, 1, false, false); // enable FIFO, DREQ at 1 sample
adc_set_clkdiv(0); // free running
ch_a = dma_claim_unused_channel(true);
ch_b = dma_claim_unused_channel(true);
dma_channel_config ca = dma_channel_get_default_config(ch_a);
channel_config_set_transfer_data_size(&ca, DMA_SIZE_16);
channel_config_set_read_increment(&ca, false);
channel_config_set_write_increment(&ca, true);
channel_config_set_dreq(&ca, DREQ_ADC);
channel_config_set_chain_to(&ca, ch_b);
dma_channel_config cb = ca;
channel_config_set_chain_to(&cb, ch_a);
dma_channel_configure(ch_a, &ca, buf_a, &adc_hw->fifo, N, false);
dma_channel_configure(ch_b, &cb, buf_b, &adc_hw->fifo, N, false);
dma_channel_set_irq0_enabled(ch_a, true);
dma_channel_set_irq0_enabled(ch_b, true);
irq_set_exclusive_handler(DMA_IRQ_0, dma_isr);
irq_set_enabled(DMA_IRQ_0, true);
dma_channel_start(ch_a);
adc_run(true);
}The interrupt handler's work is the part that is easy to skip. When channel A finishes, B starts at once and fills while the handler runs. The handler rewinds A's write address to the start of buf_a, so when B finishes and triggers A, A writes to the beginning of the buffer again. The handler has the whole time B takes to fill, 128 conversions, to do that, which makes it a deadline in the same sense as a half buffer: if the interrupt is delayed past the end of B's transfer, A starts from the end of buf_a and writes past the array.
The main loop sees which buffer finished:
#include <stdio.h>
int main(void) {
stdio_init_all();
pingpong_start();
uint32_t seen = 0;
while (true) {
if (done_count != seen) {
seen = done_count;
const uint16_t *buf = last_done ? buf_b : buf_a;
printf("buffer %c ready, first sample %u\n", last_done ? 'B' : 'A', buf[0]);
}
}
}The output alternates between the two buffers for as long as the ADC runs. The sample values depend on the input, so the first lines look like this for an input near half scale:
buffer A ready, first sample 2051
buffer B ready, first sample 2048
buffer A ready, first sample 2053
buffer B ready, first sample 2049The CPU printing a line per buffer must keep pace with the buffer rate. With the ADC free running at its maximum rate, 128 samples arrive in a few milliseconds, and printing over USB blocks for longer than that. Slow the ADC with adc_set_clkdiv or enlarge the buffers before you add serious work to the loop.
The control-channel pattern
Ping-pong rewinds addresses in software. The RP2040 supports a way to do it in hardware, by dedicating a second channel to reprogramming the first. The control channel copies a word from a list in memory into one of the data channel's registers, and because some register addresses are triggering aliases, the write also starts the data channel. The data channel is chained back to the control channel, closing the loop.
The RP2040 exposes each channel's registers in four alias layouts. Writing the last register of an alias triggers the channel, and alias 2 puts the write address last, so writing a new write address to it both sets the address and starts the transfer. With a list of buffer addresses in memory, the control channel steps through it:
#include "hardware/dma.h"
#define N 128
static uint16_t buffers[2][N];
static uint16_t *write_list[2] = { buffers[0], buffers[1] };
static int data_ch, ctrl_ch;
void control_start(void) {
data_ch = dma_claim_unused_channel(true);
ctrl_ch = dma_claim_unused_channel(true);
dma_channel_config d = dma_channel_get_default_config(data_ch);
channel_config_set_transfer_data_size(&d, DMA_SIZE_16);
channel_config_set_read_increment(&d, false);
channel_config_set_write_increment(&d, true);
channel_config_set_dreq(&d, DREQ_ADC);
channel_config_set_chain_to(&d, ctrl_ch);
dma_channel_configure(data_ch, &d, buffers[0], &adc_hw->fifo, N, false);
dma_channel_config c = dma_channel_get_default_config(ctrl_ch);
channel_config_set_transfer_data_size(&c, DMA_SIZE_32);
channel_config_set_read_increment(&c, true);
channel_config_set_write_increment(&c, false);
channel_config_set_ring(&c, false, 3); // read address wraps within 8 bytes
dma_channel_configure(ctrl_ch, &c,
&dma_hw->ch[data_ch].al2_write_addr_trig,
write_list, 1, false);
dma_channel_start(data_ch);
}Trace one lap. The data channel fills buffers[0] and finishes. Its chain triggers the control channel, which does one 32-bit transfer: it reads the next pointer from write_list, and writes it to the data channel's al2_write_addr_trig register. That sets the data channel's write address to buffers[1] and triggers it. The data channel fills buffers[1], chains to the control channel, which reads the following pointer. The control channel's read address has a ring of 8 bytes, two 32-bit pointers, so after the second pointer it wraps back to the first, and the sequence repeats forever. No interrupt is involved in the loop.
On a 32-bit ARM core the write_list array holds addresses, so the two pointers fit in 8 bytes, which is why the ring size is 3 (2^3 = 8). Extending to four buffers means a list of four pointers and a ring of 16 bytes, size 4, with the list aligned to 16 bytes, since a ring needs an aligned start.
The data channel's count reloads at each trigger, which is why only the address needs rewriting. That detail is why the pattern works with a one-word control transfer.
Interrupts are still available for the CPU to learn which buffer is ready. Enable the interrupt on the data channel and, in the handler, read the data channel's write address, which now points at the buffer being filled next, or keep a counter and alternate. The point of the pattern is that the transfer chain never waits for that handler.
When to use which
The ping-pong with interrupt rewinds is easier to read and debug, and it gives the handler a clear deadline. It suits two buffers and modest sample rates. The control-channel pattern removes the interrupt from the critical path, so it holds up at high rates and under heavy CPU load, and it extends to any number of buffers. It costs a second channel and an extra layer of indirection, which makes the failure modes harder to see. Start with ping-pong, and move to the control channel when a missed handler deadline shows up in testing.


