STM32 DMA example

Publisher:心语乐章Latest update time:2018-12-11 Source: eefocusKeywords:STM32  DMA Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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:


Keywords:STM32  DMA Reference address:STM32 DMA example

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

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号