Simvik

Choose a DMA stream from the request mapping table

Find a free stream and channel for a peripheral's DMA request in the reference manual's table, and compose the stream's control register. A hard embedded C…

hard · Reading datasheets and schematics · datasheets · registers · dma

Write uint32_t dma_plan(const char *request, uint8_t busy, int *stream) for DMA2. request is a name from the Datasheet table, such as "USART1_TX"; bit x of busy is 1 while stream x is already in use.

Pick the lowest-numbered free stream that serves the request, store its number in *stream, and return the value to write to its DMA_SxCR, with EN still 0:

  • CHSEL: the request's channel on that stream.
  • DIR: memory to peripheral for a _TX request, peripheral to memory otherwise.
  • MINC and TCIE set.
  • ADC1: half-word data on both sides (the result is 12 bits) and circular mode. The others: byte data, not circular.

If no free stream serves the request, or the request is not in the table, store -1 and return 0.

Datasheet excerpt

DMA2 request mapping (reference manual, excerpt)

Each peripheral request reaches DMA2 on one channel of one or two streams. A stream serves one request at a time.

Stream 0Stream 1Stream 2Stream 3Stream 4Stream 5Stream 6Stream 7
Channel 0ADC1–––ADC1–––
Channel 3SPI1_RX–SPI1_RXSPI1_TX–SPI1_TX––
Channel 4––USART1_RX––USART1_RX–USART1_TX
Channel 5–USART6_RXUSART6_RX–––USART6_TXUSART6_TX

DMA_SxCR, stream x configuration register (reference manual, excerpt)

BitsFieldDescription
27:25CHSELchannel selected for the stream, 0 to 7
14:13MSIZEmemory data size: 00 byte, 01 half-word, 10 word
12:11PSIZEperipheral data size: 00 byte, 01 half-word, 10 word
10MINC1: memory address incremented after each transfer
8CIRC1: circular mode
7:6DIR00 peripheral to memory, 01 memory to peripheral
4TCIE1: transfer complete interrupt enabled
0EN1: stream enabled

Starter code

uint32_t dma_plan(const char *request, uint8_t busy, int *stream) {
  *stream = -1;
  return 0;
}

Example tests

A UART transmit and an SPI receive

show("USART1_TX", 0x00);
show("SPI1_RX", 0x01);

Expected output:

USART1_TX busy 00: stream 7 CR 0x08000450
SPI1_RX busy 01: stream 2 CR 0x06000410

Learn it first

More Reading datasheets and schematics problems