#include "gd32f4xx.h"
#include "gd32f450z_eval.h"
void dma_init(void);
void usart_init(void);
uint8_t send_data[] = "Hello, DMA!";
int main(void)
{
dma_init();
usart_init();
// 启动DMA传输
dma_channel_enable(DMA_CH1);
while (1)
{
// 等待DMA传输完成
while (dma_flag_get(DMA_CH1, DMA_FLAG_FTF) == RESET);
// 重新启动DMA传输
dma_channel_enable(DMA_CH1);
}
}
void dma_init(void)
{
// 使能DMA时钟
rcu_periph_clock_enable(RCU_DMA);
// 配置DMA通道1
dma_deinit(DMA_CH1);
dma_periph_address_config(DMA_CH1, (uint32_t)&USART_DATA(USART0));
dma_memory_address_config(DMA_CH1, (uint32_t)send_data);
dma_transfer_number_config(DMA_CH1, sizeof(send_data) - 1);
dma_channel_direction_config(DMA_CH1, DMA_MEMORY_TO_PERIPHERAL);
dma_memory_width_config(DMA_CH1, DMA_MEMORY_WIDTH_8BIT);
dma_periph_width_config(DMA_CH1, DMA_PERIPHERAL_WIDTH_8BIT);
dma_priority_config(DMA_CH1, DMA_PRIORITY_ULTRA_HIGH);
dma_transfer_mode_config(DMA_CH1, DMA_PERIPHERAL_TO_MEMORY);
dma_memory_to_memory_disable(DMA_CH1);
}
void usart_init(void)
{
// 使能GPIOA和USART0时钟
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_USART0);
// 配置GPIOA9和GPIOA10为USART0的TX和RX引脚
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9 | GPIO_PIN_10);
// 配置USART0
usart_deinit(USART0);
usart_baudrate_set(USART0, 115200);
usart_word_length_set(USART0, USART_WL_8BIT);
usart_stop_bit_set(USART0, USART_STB_1BIT);
usart_parity_config(USART0, USART_PM_NONE);
usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
usart_enable(USART0);
// 使能USART0的DMA发送功能
usart_dma_transmit_config(USART0, USART_DENT_ENABLE);
} |