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);
}
Previous article:STM32F030 series implements emulation of position band operation
Next article:STM32F030F4P6 Flash capacity problem
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- LPS22HB pressure sensor PCB package and code
- In PADS, VIA Stitch is gray
- test
- DC_DC power supply inductance calculation
- Isn't it fun? The area where I got the vaccine was hotter than other places.
- Please recommend IGBT
- The meaning of Mosfet's capacitance coer and cotr
- [ESK32-360 Review] + DHT22 and single bus detection of temperature and humidity
- [TI recommended course] #Zero-based entry: Hand-in-hand teaching you how to quickly develop MSP430? Project#
- [Free Trial] Submit your application here and have a chance to win an Infineon drone~~