STM32 Getting Started with DMA (STM32F030F4P6 based on CooCox IDE)

Publisher:数字翻飞Latest update time:2018-07-10 Source: eefocusKeywords:STM32  DMA  STM32F030F4P6  CooCox  IDE Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This code is based on STM32F030F4P6, and the use case is DMA control serial port transmission. The following points should be noted

1. Pay attention to the serial port IO configuration, including IO configuration and multiplexing function selection

2. The DMA of the STM32F030 series does not support peripheral to peripheral. The STM32F030 only has DMA1 but no DMA2.

3. Note that different peripherals correspond to different DMA channels, and the channels of different chips are not necessarily the same. Please pay attention to the data sheet.

The direct code is as follows:


#include "stm32_lib/inc/stm32f0xx_rcc.h"

#include "stm32_lib/inc/stm32f0xx_gpio.h"

#include "stm32_lib/inc/stm32f0xx_usart.h"

#include "stm32_lib/inc/stm32f0xx_dma.h"

 

//The following two address writing methods are acceptable

#define USART1_TDR_Address ((uint32_t)(USART1_BASE+0x28))

//#define USART1_TDR_Address (uint32_t)(&USART1->TDR)

 

unsigned char hellStr[]={'H','E','L','L','O'};

 

void RCC_Config(void);

void GPIO_Config(void);

void USART_Config(void);

void DMA_Config(void);

 

int main(void)

{

 

SystemInit();

 

RCC_Config();

GPIO_Config();

USART_Config();

DMA_Config();

 

//Enable DMA channel 2, which means starting transmission

DMA_Cmd(DMA1_Channel2,ENABLE);

 

while (1)

{

 

}

}

 

void RCC_Config(void)

{

//Clock configuration

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);

}

void GPIO_Config(void)

{

//Select pin

//For F030F4P6, TX has PA2 and PA9 options, RX has PA3 and PA10 options, pay special attention to the AF number in the document

//GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);

//GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);

//Pin settings

GPIO_InitTypeDef PORT_UART1_TX; //Define TX pin, PORT_UART1_TX is a custom name, you can use any

PORT_UART1_TX.GPIO_Pin = GPIO_Pin_9; //This parameter is determined according to the pin selected above

PORT_UART1_TX.GPIO_Speed = GPIO_Speed_10MHz;

PORT_UART1_TX.GPIO_Mode = GPIO_Mode_AF;

PORT_UART1_TX.GPIO_PuPd = GPIO_PuPd_NOPULL;

//IO initialization

GPIO_Init(GPIOA, &PORT_UART1_TX);

 

GPIO_InitTypeDef PORT_UART1_RX; //Define RX pin

PORT_UART1_RX.GPIO_Pin = GPIO_Pin_10; //This parameter is determined according to the pin selected above

PORT_UART1_RX.GPIO_Speed = GPIO_Speed_10MHz;

PORT_UART1_RX.GPIO_Mode = GPIO_Mode_AF;

PORT_UART1_RX.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOA, &PORT_UART1_RX);

}

 

void USART_Config(void)

{

//Serial port parameter configuration

USART_InitTypeDef USART_InitStructure;

USART_InitStructure.USART_BaudRate = 115200; //Set the baud rate

USART_InitStructure.USART_WordLength = USART_WordLength_8b; //Set data length

USART_InitStructure.USART_Parity = USART_Parity_No; //Set parity

USART_InitStructure.USART_StopBits = USART_StopBits_1; //Set stop bit

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //Set flow control

USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //Set mode

USART_Init(USART1, &USART_InitStructure);

// Enable DMA transmission

USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); //Serial port 1 transmit DMA enable

//Enable

USART_Cmd(USART1, ENABLE);

}

 

void DMA_Config(void)

{

// Pay special attention to the fact that for STM32F030F4P6, USART1_TX corresponds to channel 2. Please read the data sheet carefully.

DMA_DeInit(DMA1_Channel2);

DMA_InitTypeDef DMA_InitStructure;

DMA_InitStructure.DMA_PeripheralBaseAddr=USART1_TDR_Address;

DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)hellStr;

DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST;

DMA_InitStructure.DMA_BufferSize=5;

DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;

DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;

DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;

DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;

DMA_InitStructure.DMA_Mode=DMA_Mode_Normal;

DMA_InitStructure.DMA_Priority=DMA_Priority_High;

DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;

DMA_Init(DMA1_Channel2,&DMA_InitStructure);

}


Keywords:STM32  DMA  STM32F030F4P6  CooCox  IDE Reference address:STM32 Getting Started with DMA (STM32F030F4P6 based on CooCox IDE)

Previous article:STM32F030 series implements emulation of position band operation
Next article:STM32F030F4P6 Flash capacity problem

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号