(1)main.c
#include "gd32e50x.h"
int main(void)
{
systick_config();
Usart5_Init();
while(1)
{
vTaskDelay(1000);
}
}
(2)bsp_usart.c
#include "bsp_usart.h"
void Usart5_Init(void)
{
rcu_periph_clock_disable(RCU_USART5);
rcu_periph_clock_enable(RCU_USART5);
rcu_periph_clock_enable(RCU_GPIOC);
rcu_periph_clock_enable(RCU_AF);
gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
gpio_afio_port_config(AFIO_PC6_USART5_CFG, ENABLE); //配置端口备用功能
gpio_afio_port_config(AFIO_PC7_USART5_CFG, ENABLE); //配置端口备用功能
usart_deinit(RCU_USART5);
usart_dma_transmit_config(RCU_USART5, USART_TRANSMIT_DMA_ENABLE);
usart_dma_receive_config(RCU_USART5, USART_RECEIVE_DMA_ENABLE);
usart_baudrate_set(RCU_USART5, 115200U);
usart_receive_config(RCU_USART5, USART_RECEIVE_ENABLE);
usart_transmit_config(RCU_USART5, USART_TRANSMIT_ENABLE);
usart_enable(RCU_USART5);
//开启中断之前,先清除中断的标志位
usart5_flag_clear(USART5, USART5_FLAG_RBNE);
usart5_flag_clear(USART5, USART5_FLAG_IDLE);
usart5_interrupt_enable(USART5, USART5_INT_RBNE);
usart5_interrupt_enable(USART5, USART5_INT_IDLE);
//配置串口5的DMA
DMA_USART5_RX_Init();
DMA_USART5_TX_Init();
}
void DMA_USART5_RX_Init(void)
{
dma_parameter_struct dma_init_struct;
rcu_periph_clock_enable(RCU_DMA0);
rcu_periph_clock_enable(RCU_DMA1);
/* initialize DMA channel*/
dma_deinit(DMA1,DMA_CH4);
dma_struct_para_init(&dma_init_struct);
dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY; //数据传输方向,从外设读取到内存
dma_init_struct.memory_addr = (uint32_t)USART5_RX_buff; //DMA内存基地址
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE; //内存地址寄存器递增
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT; //数据宽度为8位
dma_init_struct.number = USART_DATA_AREA_LEN;
dma_init_struct.periph_addr = USART5_RDATA_ADDRESS; //DMA外设基地址
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE; //外设地址寄存器不变
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT; //数据宽度为8位
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH; //DMA通道 x拥有高优先级
dma_init(DMA1,DMA_CH4,&dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA1,DMA_CH4);
dma_channel_enable(DMA1,DMA_CH4);
}
void DMA_USART5_RX_Init(void)
{
rcu_periph_clock_enable(RCU_DMA0);
rcu_periph_clock_enable(RCU_DMA1);
dma_parameter_struct dma_init_struct;
/* initialize DMA channel */
dma_deinit(DMA1,DMA_CH2);
dma_struct_para_init(&dma_init_struct);
dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL; //数据传输方向,从外设读取到内存
dma_init_struct.memory_addr = (uint32_t)USART5_TX_buff; //DMA内存基地址
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE; //内存地址寄存器递增
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT; //数据宽度为8位
dma_init_struct.number = USART_DATA_AREA_LEN;
dma_init_struct.periph_addr = USART5_TDATA_ADDRESS; //DMA外设基地址
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE; //外设地址寄存器不变
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT; //数据宽度为8位
dma_init_struct.priority = DMA_PRIORITY_MEDIUM; //DMA通道 x拥有高优先级
dma_init(DMA1,DMA_CH2,&dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA1,DMA_CH2);
dma_channel_disable(DMA1,DMA_CH2);
}
//串口5的DMA的发送函数
void Usart_DMA_Tx(uint32_t usart_periph,uint32_t DMA_periph,dma_channel_enum dam_rx_channel,uint16_t len, uint8_t *buff)
{
/* Channel disable */
dma_channel_disable(DMA_periph, dam_rx_channel);
dma_memory_address_config(DMA_periph, dam_rx_channel,(uint32_t)buff);
dma_transfer_number_config(DMA_periph,dam_rx_channel,len);
/* enable DMA channel to start send */
dma_channel_enable(DMA_periph, dam_rx_channel);
}
//串口5的中断服务函数
void USART5_IRQHandler(void)
{
uint16_t rec_len = 0;
if(RESET != usart5_interrupt_flag_get(USART5, USART5_INT_FLAG_IDLE))
{
rec_len = USART_DATA_AREA_LEN - dma_transfer_number_get(DMA1,DMA_CH2);
dma_channel_disable(DMA1,DMA_CH2);
dma_transfer_number_config(DMA1,DMA_CH2,USART_DATA_AREA_LEN);
memcpy(USART5_TX_buff,USART_DATA.USART5_RX_buff,rec_len);
memset(USART5_RX_buff,0,rec_len);
dma_channel_enable(DMA1,DMA_CH2);
Usart_DMA_Tx(USART5,DMA1,DMA_CH4,rec_len,USART5_TX_buff);
usart_data_receive(USART5);
}
usart5_interrupt_flag_clear(USART5, USART5_INT_FLAG_IDLE);
}
(3)bsp_usart.h
#ifndef BSP_USART_H
#define BSP_USART_H
#define USART_DATA_AREA_LEN 40
#define USART5_TDATA_ADDRESS (uint32_t)&USART5_TDATA(USART5)
#define USART5_RDATA_ADDRESS (uint32_t)&USART5_RDATA(USART5)
void Usart5_Init(void);
void DMA_USART5_RX_Init(void)
void DMA_USART5_TX_Init(void)
void Usart_DMA_Tx(uint32_t usart_periph,uint32_t DMA_periph,dma_channel_enum dam_rx_channel,uint16_t len, uint8_t *buff);
#endif