GD32f103 串口DMA模式例子
时间: 2023-07-23 12:44:41 浏览: 189
以下是一个使用GD32f103芯片的串口DMA模式例子,可以帮助你快速开始使用此功能:
```c
#include "gd32f10x.h"
#define USART0_DR_ADDRESS ((uint32_t)0x40013804)
#define USART1_DR_ADDRESS ((uint32_t)0x40004404)
#define BUFFER_SIZE 256
uint8_t TxBuffer[BUFFER_SIZE];
uint8_t RxBuffer[BUFFER_SIZE];
void RCC_Configuration(void)
{
/* 使能USART0和DMA1的时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART0, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}
void GPIO_Configuration(void)
{
/* 使能USART0的GPIO时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* 配置USART0的TX引脚为复用推挽输出 */
GPIO_InitPara GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_9;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 配置USART0的RX引脚为浮空输入 */
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_10;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART_Configuration(void)
{
/* 配置USART0 */
USART_InitPara USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
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_TX_RX;
USART_Init(USART0, &USART_InitStructure);
/* 使能USART0的DMA发送和DMA接收 */
USART_DMACmd(USART0, USART_DMAReq_Tx | USART_DMAReq_Rx, ENABLE);
}
void DMA_Configuration(void)
{
/* 配置DMA1通道4为USART0的TX通道 */
DMA_InitPara DMA_InitStructure;
DMA_InitStructure.DMA_PeripheralBaseAddr = USART0_DR_ADDRESS;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PERIPHERALDST;
DMA_InitStructure.DMA_BufferSize = BUFFER_SIZE;
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_Channel4, &DMA_InitStructure);
/* 配置DMA1通道5为USART0的RX通道 */
DMA_InitStructure.DMA_PeripheralBaseAddr = USART0_DR_ADDRESS;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PERIPHERALSRC;
DMA_InitStructure.DMA_BufferSize = BUFFER_SIZE;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
/* 使能DMA1通道4和DMA1通道5 */
DMA_Cmd(DMA1_Channel4, ENABLE);
DMA_Cmd(DMA1_Channel5, ENABLE);
}
int main(void)
{
/* 系统时钟配置 */
SystemInit();
/* RCC配置 */
RCC_Configuration();
/* GPIO配置 */
GPIO_Configuration();
/* USART配置 */
USART_Configuration();
/* DMA配置 */
DMA_Configuration();
while (1)
{
/* 发送数据 */
for (uint16_t i = 0; i < BUFFER_SIZE; i++)
{
TxBuffer[i] = i;
}
while (DMA_GetFlagStatus(DMA1_FLAG_TC4) == RESET);
/* 接收数据 */
while (DMA_GetFlagStatus(DMA1_FLAG_TC5) == RESET);
/* 处理接收到的数据 */
for (uint16_t i = 0; i < BUFFER_SIZE; i++)
{
if (RxBuffer[i] != i)
{
/* 处理接收到的数据出错 */
}
}
}
}
```
这个例子使用USART0的DMA模式进行数据的发送和接收,并且使用了DMA1通道4和DMA1通道5。在主循环中,它会先发送一个有序的数据包,然后等待DMA发送完成,再接收一个数据包,并验证接收到的数据是否正确。
阅读全文