STM32: Storing ADC Data with DMA.
DMA (Direct Memory Access) lets you transfer data without involving the core. That is, if you use DMA, then when an ADC conversion completes, we don't need to fetch the conversion result and store it in memory; DMA will do it for us. To do this, when configuring DMA it's enough to specify the address to take data from and where to store it, plus a few more simple settings, which we'll look at below.
Data is transferred over dedicated channels; the STM32F103 has as many as 12 of them, distributed between two controllers: 7 channels on the first and 5 on the second. You can enlarge the pictures by clicking on them.
As you can see, each peripheral is assigned to a specific DMA channel.
Each request can be assigned one of the priorities:
If two requests with the same priority occur simultaneously, the one with the lower channel number is served first. In this situation channel 2 will take precedence over channel 4.
Each DMA channel can generate an interrupt request on one of the events:
As written above, when configuring DMA you need to specify the source and destination addresses; these addresses are base addresses, and you can also increment the source/destination address after each transfer, which can come in handy when the received data needs to be stored in an array.
During initialization you need to specify the data width that will be sent and received via DMA; it can be 8, 16 or 32 bits. If the source and destination data widths don't match, the DMA controller aligns them; RM0008 has a whole table showing how the alignment happens.
The maximum DMA buffer size is 65535 values.
Now let's look at how to store the data received from ADC1 in memory.
In the existing ADC initialization, it's enough to enable DMA transfers in the CR2 register.
Now we can move on to configuring DMA.
Circular mode works as follows: after DMA starts, the value in the DMA_CNDTRx register is automatically decremented after each transfer until it reaches zero; then the value in the register is automatically reloaded with DMA_BUFF_SIZE and everything starts over. With this mode it's very easy to organize a ring buffer, which is what we did. Now the buff array will always hold fresh values from the ADC. All that's left is to stop this merry-go-round in time and display the values on the screen.
What's it for? For example, my oscilloscope prototype works on this principle; you can see it here.
Data is transferred over dedicated channels; the STM32F103 has as many as 12 of them, distributed between two controllers: 7 channels on the first and 5 on the second. You can enlarge the pictures by clicking on them.
As you can see, each peripheral is assigned to a specific DMA channel.
Each request can be assigned one of the priorities:
- very high
- high
- medium
- low
If two requests with the same priority occur simultaneously, the one with the lower channel number is served first. In this situation channel 2 will take precedence over channel 4.
Each DMA channel can generate an interrupt request on one of the events:
- half of the buffer transferred
- transfer complete
- transfer error
As written above, when configuring DMA you need to specify the source and destination addresses; these addresses are base addresses, and you can also increment the source/destination address after each transfer, which can come in handy when the received data needs to be stored in an array.
During initialization you need to specify the data width that will be sent and received via DMA; it can be 8, 16 or 32 bits. If the source and destination data widths don't match, the DMA controller aligns them; RM0008 has a whole table showing how the alignment happens.
The maximum DMA buffer size is 65535 values.
Now let's look at how to store the data received from ADC1 in memory.
In the existing ADC initialization, it's enough to enable DMA transfers in the CR2 register.
ADC1->CR2 |= ADC_CR2_DMA;
Now we can move on to configuring DMA.
//Address of the ADC conversion result register
#define ADC1_DR_Address ((u32)0x40012400+0x4c)
#define DMA_BUFF_SIZE 10//Buffer size
uint16_t buff[DMA_BUFF_SIZE];//Buffer
RCC->AHBENR |= RCC_AHBENR_DMA1EN; //Enable clocking of the first DMA module
DMA1_Channel1->CPAR = ADC1_DR_Address; //Set the peripheral address - the ADC conversion result register for regular channels
DMA1_Channel1->CMAR = (uint32_t)buff; //Set the memory address - the base address of the array in RAM
DMA1_Channel1->CCR &= ~DMA_CCR1_DIR; //Set the data transfer direction, from peripheral to memory
DMA1_Channel1->CNDTR = DMA_BUFF_SIZE; //Number of values to transfer
DMA1_Channel1->CCR &= ~DMA_CCR1_PINC; //Don't increment the peripheral address after each transfer
DMA1_Channel1->CCR |= DMA_CCR1_MINC; //Increment the memory address after each transfer.
DMA1_Channel1->CCR |= DMA_CCR1_PSIZE_0; //Peripheral data size - 16 bits
DMA1_Channel1->CCR |= DMA_CCR1_MSIZE_0; //Memory data size - 16 bits
DMA1_Channel1->CCR |= DMA_CCR1_PL; //Priority - very high
DMA1_Channel1->CCR |= DMA_CCR1_CIRC; //Enable DMA circular mode
DMA1_Channel1->CCR |= DMA_CCR1_EN; //Enable DMA channel 1
Circular mode works as follows: after DMA starts, the value in the DMA_CNDTRx register is automatically decremented after each transfer until it reaches zero; then the value in the register is automatically reloaded with DMA_BUFF_SIZE and everything starts over. With this mode it's very easy to organize a ring buffer, which is what we did. Now the buff array will always hold fresh values from the ADC. All that's left is to stop this merry-go-round in time and display the values on the screen.
What's it for? For example, my oscilloscope prototype works on this principle; you can see it here.

