Tutorials

Size and Align an RP2040 DMA Ring for 16-Bit Samples

Published · Updated

A buffer can contain 512 uint16_t elements and still be an invalid base for a 1 KiB hardware ring. The DMA wraps address bits. A C array declaration specifies the storage size and may promise only halfword alignment.

Set the wrap boundary in bytes

Component side of the original Raspberry Pi Pico. U1 contains the RP2040 peripherals discussed in this example.
Component side of the original Raspberry Pi Pico. U1 contains the RP2040 peripherals discussed in this example. Phiarc, Raspberry Pi Pico top (Wikimedia Commons), CC BY-SA 4.0. WebP adaptation.

For a peripheral-to-memory capture, choose which address increments and apply the ring to the write address. The hardware retains address bits above the selected wrap size. For a ring size setting of ten, the lowest ten address bits change, so the base must lie on a 1,024-byte boundary.

Check the final linked address. A pointer ending in hexadecimal 0200 is half a ring away from such a boundary even when the allocation is exactly 1,024 bytes long.

Compare ring and ping-pong buffering · Budget FIFO capacity during a service stall

Turn the buffer length into the consumer’s time budget

Ring bytes / 2^10 = 1,024 / Align base to 1 KiB; Sample slots / 1,024 / 2 / 512 halfwords; Half-ring interval / 256 / 200,000 / 1.28 ms
Quantity / Example / Meaning.

At 200,000 samples per second, 512 halfword samples span 2.56 ms. A half-ring contains 256 samples and spans 1.28 ms. If software consumes the ring in halves, use the smaller number when budgeting wake-up and processing time.

Address wrapping does not reload the transfer counter or mark unread samples. Arrange a bounded transfer or an explicit rearm scheme, and keep a producer count separate from the wrapped index to detect dropped data.

The write pointer returned to the starting address. Is the buffer empty?

It could also be full or have lapped the consumer. Compare monotonic progress counters or recorded block completions before reusing that region.

Trace the three worksheet rowsRing bytes / 2^10 = 1,024 / Align base to 1 KiB; Sample slots / 1,024 / 2 / 512 halfwords; Half-ring interval / 256 / 200,000 / 1.28 ms
  1. Ring bytes · 2^10 = 1,024 · Align base to 1 KiB
  2. Sample slots · 1,024 / 2 · 512 halfwords
  3. Half-ring interval · 256 / 200,000 · 1.28 ms

Bound queue growth during a storage stall · Capture the first rejected FIFO write

Work through a sensor FIFO watermark.

Raspberry Pi RP2040 Datasheet, 2.5 DMA, address wrapping.

Photograph credit and licence

Phiarc, Raspberry Pi Pico top (Wikimedia Commons), CC BY-SA 4.0. WebP adaptation.

Original photograph https://commons.wikimedia.org/wiki/File:Raspberry_Pi_Pico_top.jpg

Licence https://creativecommons.org/licenses/by-sa/4.0/

Back to Tutorials

Related evidence and next tasks

Continue the task with the relevant source scope. These reading paths do not establish interchangeability or current supply.

Source addresses and citation trail

Full source addresses are retained below. Read each citation with its document revision, page, table and applicability conditions above.

  1. https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf#page=115