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
_TXrequest, 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 0 | Stream 1 | Stream 2 | Stream 3 | Stream 4 | Stream 5 | Stream 6 | Stream 7 | |
|---|---|---|---|---|---|---|---|---|
| Channel 0 | ADC1 | – | – | – | ADC1 | – | – | – |
| Channel 3 | SPI1_RX | – | SPI1_RX | SPI1_TX | – | SPI1_TX | – | – |
| Channel 4 | – | – | USART1_RX | – | – | USART1_RX | – | USART1_TX |
| Channel 5 | – | USART6_RX | USART6_RX | – | – | – | USART6_TX | USART6_TX |
DMA_SxCR, stream x configuration register (reference manual, excerpt)
| Bits | Field | Description |
|---|---|---|
| 27:25 | CHSEL | channel selected for the stream, 0 to 7 |
| 14:13 | MSIZE | memory data size: 00 byte, 01 half-word, 10 word |
| 12:11 | PSIZE | peripheral data size: 00 byte, 01 half-word, 10 word |
| 10 | MINC | 1: memory address incremented after each transfer |
| 8 | CIRC | 1: circular mode |
| 7:6 | DIR | 00 peripheral to memory, 01 memory to peripheral |
| 4 | TCIE | 1: transfer complete interrupt enabled |
| 0 | EN | 1: 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 0x06000410Learn it first
- Reading datasheets and schematics · Where the answers are, min/typ/max, calculating from the tables and reading register maps.
More Reading datasheets and schematics problems
- Build a register from its datasheet table · Assemble the ADS1115 ADC's 16-bit config register from its fields.
- Baud rate divider from the reference manual · Turn the reference manual's baud rate formula into a BRR register value, in both oversampling modes.
- ADC timing from the electrical characteristics · Pick the ADC prescaler and sampling time from the datasheet's limits, and work out the conversion time.
- Bit-bang SPI from a timing diagram · Read the clock polarity and phase off a sensor's timing diagram and drive the pins to match.
- A sensor frame, its CRC and its transfer function · Check the CRC the datasheet specifies, then convert raw readings to temperature and humidity.
- Apply the errata sheet's workarounds · An ADC driver that follows the reference manual still reads wrong values. The errata sheet explains why.
- Route a UART to its pins from the alternate-function table · Find USART2's pins and function number in the datasheet, then set MODER, AFRL, OSPEEDR and PUPDR without disturbing other pins.
- An open-drain interrupt line, from the sensor's pin description · The datasheet says INT is open-drain and active low. Configure the pin so it reads correctly, and report when the sensor asserts it.
- A 10 ms tick, from the clock tree · Work out the timer's input clock from the clock tree, including the APB timer rule, then set the prescaler and auto-reload for a 10 ms period.
- Configure a sensor over SPI from its datasheet · Pick the SPI mode and the fastest clock the sensor allows, then set its data rate and range from the register map, including a non-obvious field encoding.
- Fast-mode I2C and the EEPROM's address, from three documents · Compose TIMINGR from the reference manual's timing table, work out the EEPROM's address from its datasheet and the schematic, and probe for it.
- Receive from a GPS module, from its interface description · Set the baud rate divider and frame format the module's datasheet gives, with only the receiver enabled, and read one NMEA sentence.
- An LED and a button, from the board schematic · The schematic decides which level lights the LED and which level means pressed. Write the three functions that hide it.
- Battery voltage through a resistor divider · Read the divider ratio and the ADC's reference voltage off the schematic, and turn a raw reading into millivolts.
- Trace an expansion header to the microcontroller's pins · Follow each header pin's net through resistors, a level shifter and solder bridges to the pin it ends at.
- A relay and a sensor's power switch, from the transistors · One transistor turns its load on with a high pin, the other with a low pin. Work out which from the schematic, and keep both off from reset.
- Route a pin's interrupt, from the reference manual · Use the SYSCFG and EXTI register descriptions and the vector table to send any pin's edges to its interrupt.