Choose the transfer width
Pick the widest beat that suits both addresses and the length. An easy embedded C problem (DMA), run and graded in the browser.
easy · DMA · dma · alignment
A DMA channel copies in beats of 1, 2 or 4 bytes. Wider beats mean fewer bus transactions, but a beat of width w needs the source address, the destination address and the length to be multiples of w; otherwise the transfer faults or copies the wrong bytes.
Write dma_width(src, dst, len): return the widest of 4, 2 and 1 that fits all three.
Starter code
int dma_width(uint32_t src, uint32_t dst, uint32_t len) {
(void)src; (void)dst;
if (len % 4 == 0) return 4;
if (len % 2 == 0) return 2;
return 1;
}Example tests
Everything aligned
show(0x20000000, 0x20001000, 64);Expected output:
0x20000000 -> 0x20001000, 64 bytes: width 4, 16 beatsA half-word aligned source
show(0x20000002, 0x20001000, 64);Expected output:
0x20000002 -> 0x20001000, 64 bytes: width 2, 32 beatsLearn it first
- DMA · Transfer modes, circular buffers, half-transfer interrupts and cache coherency.
More DMA problems
- Receive a UART stream with circular DMA · Copy out the bytes DMA has written since the last poll, using its remaining-count register.
- Process ADC halves on time · Average the half of the buffer DMA has just finished, and count overruns.