STM32 DMA MEM2MEM.
In one of my previous articles I already described what DMA is and gave an example of it working together with an ADC. In that example, the ADC conversion results were written to a buffer without involving the core. This time I needed to move a buffer from one memory area to another, and I knew for sure that this can be done with DMA using MEM2MEM mode.
Anyone who has worked with DMA or read the article mentioned above knows that when configuring DMA you need to:
In fact, configuring DMA involves more steps, but I listed only these because they raised questions for me when configuring them.
As for choosing the channel, that question went away after looking at a picture from the RM.
The picture shows that every DMA2 channel of the controller I use supports MEM2MEM mode, so we pick any free one.
The data transfer direction turned out not to be so simple. I couldn't find the right picture in the RM for my MCU, but it turned up in the RM for another MCU.
The picture shows that the source (Memory Source) is on the Peripheral Bus, and the destination (Memory Destination) is on the Memory Bus. So when setting the data transfer direction we choose from peripheral to memory. And most importantly, don't forget to set the MEM2MEM bit.
It should also be noted that DMA in MEM2MEM mode cannot work in circular mode. To restart the DMA module, you need to execute the following code.
Anyone who has worked with DMA or read the article mentioned above knows that when configuring DMA you need to:
- select a DMA channel
- specify the data transfer direction (from memory to peripheral or from peripheral to memory)
In fact, configuring DMA involves more steps, but I listed only these because they raised questions for me when configuring them.
As for choosing the channel, that question went away after looking at a picture from the RM.
The picture shows that every DMA2 channel of the controller I use supports MEM2MEM mode, so we pick any free one.
The data transfer direction turned out not to be so simple. I couldn't find the right picture in the RM for my MCU, but it turned up in the RM for another MCU.
The picture shows that the source (Memory Source) is on the Peripheral Bus, and the destination (Memory Destination) is on the Memory Bus. So when setting the data transfer direction we choose from peripheral to memory. And most importantly, don't forget to set the MEM2MEM bit.
#define DMA_BUFF_SIZE 100
volatile uint16_t buff[DMA_BUFF_SIZE] = {0};
volatile uint16_t old_buff[DMA_BUFF_SIZE] = {0};
void DMA_Copy_Memory_Init(void)
{
RCC->AHBENR |= RCC_AHBENR_DMA2EN; //Enable clocking of the second DMA module
DMA2_Channel4->CCR |= DMA_CCR_MEM2MEM; //Enable MEM2MEM mode
DMA2_Channel4->CPAR = (uint32_t)buff; //Set the peripheral address
DMA2_Channel4->CMAR = (uint32_t)old_buff; //Set the memory address
DMA2_Channel4->CCR &= ~DMA_CCR_DIR; //Set the data transfer direction, from peripheral to memory
DMA2_Channel4->CNDTR = DMA_BUFF_SIZE; //Number of values to transfer
DMA2_Channel4->CCR |= DMA_CCR_PINC; //Increment the peripheral address after each transfer
DMA2_Channel4->CCR |= DMA_CCR_MINC; //Increment the memory address after each transfer.
DMA2_Channel4->CCR |= DMA_CCR_PSIZE_0; //Peripheral data size - 16 bits
DMA2_Channel4->CCR |= DMA_CCR_MSIZE_0; //Memory data size - 16 bits
DMA2_Channel4->CCR |= DMA_CCR_PL; //Priority - very high
DMA2_Channel4->CCR &= ~DMA_CCR_CIRC; //Disable DMA circular mode
//DMA2_Channel4->CCR |= DMA_CCR_TCIE;//Enable the transfer complete interrupt
DMA2_Channel4->CCR |= DMA_CCR_EN; //Enable DMA channel 4
}
It should also be noted that DMA in MEM2MEM mode cannot work in circular mode. To restart the DMA module, you need to execute the following code.
DMA2_Channel4->CCR &= ~DMA_CCR_EN;//disable DMA
DMA2_Channel4->CNDTR = DMA_BUFF_SIZE;//set the number of values to transfer
DMA2_Channel4->CCR |= DMA_CCR_EN;//start DMA

