DMA (STM32), dmastm32
1. DMA: data memory access // actual memory storage
Note: CPU interference is not required when DMA is used.
2.
① Memory (Defined variables) --- peripherals (registers );
② Memory --- memory
③ Peripherals --- peripherals (registers of One peripheral to another)
3.
STM32 has two DMA controllers
4. Example: (memory to peripherals, Serial Port 1 tx dma)
1 void USART1_DMA_Config(void) 2 { 3 DMA_InitTypeDef DMA_InitStructure; 4 5 /*Open the DMA clock*/ 6 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); 7 8 /*Set the DMA source: serial port data register address*/ 9 DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base; 10 11 /*Memory address,(A pointer to the variable transmission)*/12 DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff;13 14 /*Direction:From memory to the peripherals*/ 15 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; 16 17 /*Transmission size:DMA_BufferSize=SENDBUFF_SIZE*/ 18 DMA_InitStructure.DMA_BufferSize = SENDBUFF_SIZE;19 20 /*It does not increase peripheral address*/ 21 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 22 23 /*Memory address since the increase*/24 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 25 26 /*A peripheral unit of data*/ 27 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;28 29 /*Memory data unit:8bit*/30 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; 31 32 /*The DMA mode: cycle*/33 //DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ;34 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; 35 36 /*priority:medium*/ 37 DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; 38 39 /*Memory to memory transmission is prohibited*/40 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;41 42 /*Configuration of the channel 4*/ 43 DMA_Init(DMA1_Channel4, &DMA_InitStructure); 44 45 /*enable DMA1_Channel4*/46 DMA_Cmd (DMA1_Channel4,ENABLE); 47 //DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE); //Configuration after completion of the DMA interrupt48 }
① Usart0000tx is mounted to Channel 4 of DMA1 (each DMA has 7 channels, corresponding to different peripherals)
② The ninth line of the program source (source)
That is, the source address (peripheral address)
1 # define USART1_DR_Base 0x40013804 // 0x40013800 + 0x04 = 0x40013804
How can we calculate it?
Please refer:
What is the base address?
Therefore, the data register address of Serial Port 1:
// 0x40013800 + 0x04 = 0x40013804
③ Memory address, custom Array
Direction 4: memory to peripherals
⑤ Transmission size: 5000 bytes
6. If the peripheral address is not increased, the memory address is automatically increased, and 5000 bytes are sent cyclically.
7. Sending mode: Normal: the sending of 5000 bytes is finished, and circular is sent cyclically.
Priority: If several peripherals in a program have applied for a DMA request at the same time, select
Bytes disables memory-to-memory transmission.
4. Check the main function.
Uint16_t I;/* Filling is going to send data */for (I = 0; I <SENDBUFF_SIZE; I ++) {SendBuff [I] = 'a ';} /* USART1 sends a TX request to DMA */USART_DMACmd (USART1, USART_DMAReq_Tx, ENABLE);/* the CPU is idle at this time, you can do other things * // LED At the same time scintillation for (;) {LED1 (ON); Delay (0 xFFFFF); LED1 (OFF ); delay (0 xFFFFF );}
Note)
DMA in M3 can only be triggered by software, and some chips can be triggered by external pulses;
DMA channel in STM32
Well, you should have modified the stm32 Firmware Library function.
DMA_Channel_TypeDef * refers to the pointer of DMA_Channel_TypeDef, which points to an address.
DMA_CHx should be a macro definition, which defines this address.
The x value indicates the DMA channel number. Depending on the chip, there may be 1 ~ 7.
To understand the macro definition, use goto difinition to view the macro definition.
Follow-up
When configuring DMA for STM32: Error: L6218E: Undefined symbol DMA_Cmd (referred from maino) keil 412
Add XXX_DMA.C to the StdPeriph_Driver folder of the project.