STM32 DMA explanation and configuration process, with code instructions

Publisher:rockstar6Latest update time:2018-12-21 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

DMA involves the following concepts: 


①: DMA stands for Direct Memory Access, which is a peripheral unique to STM32. Large-capacity STM32 products integrate two DMAs, namely DMA1 and DMA2. DMA1 has 7 channels and DMA2 has 5 channels. For the specific peripherals connected to each channel, please refer to the data manual of the STM32 chip.


②: DMA can be used to transfer data between two different addresses, such as memory to peripheral registers, peripheral registers to memory, or from memory to memory.


③: When two data are transferred between different addresses, it is necessary to determine the number of bytes transmitted each time in the program configuration, and determine whether it is a byte, half word or word.


④: The priority of each DMA channel is variable. Take DMA1 as an example. It has 7 channels. Each channel can be configured with one of the following four priority levels: very high, high, medium, or low. If two channels have the same priority, when both channels have DMA requests at the same time, the channel with the smaller number has a higher priority.


⑤: The amount of data transferred by DMA each time is variable. There is a special register in DMA to store this data value. This register is 32 bits, but the upper 16 bits are all reserved as 0. In fact, the lower 16 bits are effective, so the maximum amount of data transferred each time is 65536.


⑥: As shown in ⑤, for example, if the data volume value is set to 100, if the DMA transmission is set to cyclic mode, the next round of transmission will be automatically carried out after 100 data transmissions are completed. If it is set to non-cyclic mode, you need to turn off DMA first, then set the data volume value, and then turn on DMA before the next round of transmission can be carried out.


⑦: During the DMA transmission process, there are three common flags: half of the transmission is completed, the transmission is completed, and an error occurs during the transmission process. You can set the interrupt corresponding to the flag in the program. When the flag arrives, the interrupt service program will be executed. You can also not enable the interrupt of the corresponding flag.


⑧: After determining the peripheral and memory address to be transferred, you need to set the transfer direction in the program, that is, whether the transfer direction is from the peripheral to the register, or from the register to the peripheral.


⑨: DMA is generally used to transfer data between peripherals and memory, so it is also necessary to set whether the peripheral address and memory address are incremented. For example, if an array is defined, char data[100], and the peripheral address is &UART->TX, if the 100 data in the array are transferred to UART->TX, the memory address needs to be incremented each time, but the peripheral address does not need to be incremented.


DMA configuration process:


①: Determine the peripheral and register address of the transmitted data


②: Determine the transmission direction


③: Determine the amount of data transferred each time


④: Determine the number of bytes of transmitted data


⑤: Configure channel priority


⑥: Determine whether the transmission is in cyclic mode or non-cyclic mode


⑦: If you need to enable interrupt, enable the response bit interrupt




Note: DMA can also be from memory to memory, but the memory to memory process can only be non-circular mode.




Program explanation:


Example: The function implemented by the program is to transfer the data in the memory to the transmit register TX of the serial port. The amount of data transmitted each time is 100, and the transmission is in non-cyclic mode.


void MYDMA_Config(DMA_Channel_TypeDef* DMA_CHx, u32 cpar, u32 cmar, u16 cndtr)


{


RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //Start the clock


DMA_DeInit(DMA_CHx); //Initialize the channel and set it to default configuration


DMA1_MEM_LEN=cndtr;


DMA_InitStructure.DMA_PeripheralBaseAddr = cpar; //Peripheral address


DMA_InitStructure.DMA_MemoryBaseAddr = cmar; //Memory address


DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //Transmission direction, from memory to peripherals


DMA_InitStructure.DMA_BufferSize = cndtr; //The amount of data transferred in each cycle


DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //Peripheral address remains unchanged


DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //The memory address increases by 1 each time


DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //Byte transfer


DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //Byte transfer


DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //Non-cyclic mode


DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //Set priority - Medium


DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //Not memory to memory


DMA_Init(DMA_CHx, &DMA_InitStructure); //Initialize DMA configuration process


}


//Start a transmission

void MYDMA_Enable(DMA_Channel_TypeDef*DMA_CHx)

DMA_Cmd(DMA_CHx, DISABLE );  //关闭DMA     

  DMA_SetCurrDataCounter(DMA_CHx,DMA1_MEM_LEN); //Redetermine the amount of data to be transferred in each cycle

  DMA_Cmd(DMA_CHx, ENABLE); //Enable DMA again

}


int main()


{


u8 SendBuff[100]; //Memory data


MYDMA_Config(DMA1_Channel4, (u32)&USART1->DR, (u32)SendBuff, 100); //Call function


MYDMA_Enable(DMA1_Channel4); //Start a transmission


}

Keywords:STM32 Reference address:STM32 DMA explanation and configuration process, with code instructions

Previous article:STM32 interrupt control process
Next article:STM32 basic design (5) --- ADC conversion (interrupt mode)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号