DMA Introduction:
DMA (Direct Memory Access) is a data access method that can reduce the workload of the CPU and is now widely used. While transmitting data, the CPU can do other things, such as data calculations or responding to interrupts, etc. DMA relieves the CPU of a lot of workload!
DMA work analysis:
As shown in the figure, we can see the connections between the STM32 core, memory, peripherals and DMA. These hardware are ultimately connected to the bus matrix through various lines. The data transfer between the hardware structures is coordinated by the bus matrix, so that each peripheral uses the bus to transmit data harmoniously.
Let's see how the data collected by ADC is stored in SRAM with and without DMA.
1. If there is no DMA, the CPU still needs to use the kernel as a transit station to transfer data. For example, if the data collected by the ADC is to be transferred to the SRAM, the process is as follows: the kernel coordinates through the bus matrix through DCode, uses AHB to collect the data from the peripheral ADC, and then the kernel and DCode coordinate through the bus matrix to store the data in the memory SRAM.
2. If there is DMA, the DMA bus of the DMA controller coordinates with the bus matrix and uses AHB to store the data collected by the peripheral ADC into the SRAM via the DMA channel. During the transmission of this data, the kernel, that is, the CPU, does not need to be involved at all. However, a request must be made to the DMA peripheral during DMA transmission to trigger its work.
Below I am learning DMA through the example of serial communication!
Main function main.c:
#include
uint8_t sendbuff[500];
uint16_t i;
int main()
{
printf_init();
dma_init();
for(i=0;i<500;i++)
{
sendbuff[i] =0xaf;
}
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
LED3_ON;
while(1);
The function implemented by this main function is to use DMA to transfer data (array, which stores 500 AF characters) from memory to peripherals (serial port), and finally transmit it to our PC through the serial port for display. In order to prove that the CPU can do other things while DMA is transferring data, we turn on LED3 to test it. Why does the main function generate an interrupt only when adding while(1)? I still don't understand...
DMA configuration dma.c:
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_dma.h"
#include "misc.h"
#include "dma.h"
void dma_init()
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
NVIC_Config();
/*DMA configuration*/
DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base; //Serial port data register address
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)sendbuff; //Memory address (pointer to the variable to be transferred)
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //Direction (from memory to peripherals)
DMA_InitStructure.DMA_BufferSize = 500; //The size of the transferred content
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //Peripheral address does not increase
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //Memory address auto-increment
DMA_InitStructure.DMA_PeripheralDataSize =
DMA_PeripheralDataSize_Byte; //Peripheral data unit
DMA_InitStructure.DMA_MemoryDataSize =
DMA_MemoryDataSize_Byte; //Memory data unit
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //DMA mode: one transfer, loop
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //Priority: High
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //Disable memory-to-memory transfer
DMA_Init(DMA1_Channel4, &DMA_InitStructure); //Configure DMA1's 4 channels
DMA_Cmd(DMA1_Channel4,ENABLE);
DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);//Configure DMA to generate an interrupt after sending is completed
}
static void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Interrupt placement */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
The serial port data register address configuration here is set with reference to the STM32 datasheet. USART1_DR_Base is a macro, #define USART1_DR_Base 0x40013804. As for why the address is this, please see the figure below:
Memory map, find the base address of USART1, and then look at the offset address of USART1 data register to know the address of serial port 1.
As for why it is channel 4 instead of other channels when configuring DMA1 channel, please see the figure below:
dma.h:
#ifndef _dma_H
#define _dma_H
#include "stm32f10x.h"
#define USART1_DR_Base 0x40013804;
extern uint8_t sendbuff[500];
static void NVIC_Config(void);
void dma_init(void);
#endif
Serial port configuration: printf.c
#include "printf.h"
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h"
#include "misc.h"
int fputc(int ch,FILE *f)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC) != SET);
USART_SendData(USART1,(unsigned char)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC) != SET);
return (ch);
}
void printf_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/*config USART clock*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
/*USART1 GPIO config*/
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplex push-pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //Multiplexed open-drain input
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOD,&GPIO_InitStructure);
/*USART1 mode Config*/
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
printf.h:
#ifndef __printf_H
#define __printf_H
#include "stm32f10x.h"
#include
#define LED3_ON GPIO_SetBits(GPIOD,GPIO_Pin_3)
#define LED3_OFF GPIO_ResetBits(GPIOD,GPIO_Pin_3)
void printf_init(void);
int fputc(int ch,FILE *f);
#endif
Interrupt code: stm32f10x_it.c
#include "stm32f10x_it.h"
#include "stm32f10x.h"
#include "printf.h"
void DMA1_Channel4_IRQHandler(void)
{
if(DMA_GetFlagStatus(DMA1_FLAG_TC4)==SET)
{
LED3_OFF;
DMA_ClearFlag(DMA1_FLAG_TC4);
}
}
Effect picture:
Previous article:ADC sampling example based on DMA in STM32 MQ-2 smoke sensor
Next article:A brief analysis of assert_param function in STM32
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Award-winning live broadcast | ADI motor control solutions
- [Xianji HPM6750EVKMINI Review] 1# Hardware Introduction and Development Environment Construction
- How can an electric mosquito swatter kill mosquitoes without harming people? Is it because the voltage is high and the time is short? Isn't that related to ESD?
- Competition sharing, Xiaobai unboxing
- Tips for beginners in electronics
- I need help~What is the interface level standard of DAC8812ICPW?
- Today's broadcast starts at 10:00: Interpreting ON Semiconductor's power solutions and understanding the latest power technology trends
- Antenna-on-Package Design Simplifies mmWave Sensing in Buildings and Factories
- [FS-IR02 + D1CS-D54] - 0: Study two PDFs
- Peak Detection