写一个GD32F303通过DMA发送串口中断接受的RS485串口DMA函数
时间: 2024-04-15 16:28:20 浏览: 270
好的,下面是一个GD32F303通过DMA发送串口中断接收的RS485串口DMA函数的示例代码:
```c
#include "gd32f30x.h"
#define USART USART1
#define USART_DMA_CHANNEL DMA_CH5
#define USART_DMA_STREAM DMA0_STREAM5
#define BUFFER_SIZE 128
uint8_t tx_buffer[BUFFER_SIZE];
uint8_t rx_buffer[BUFFER_SIZE];
uint16_t rx_len = 0;
void rs485_init(void)
{
rcu_periph_clock_enable(RCU_USART1);
rcu_periph_clock_enable(RCU_AF);
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
gpio_init(GPIOA, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
usart_deinit(USART);
usart_baudrate_set(USART, 9600);
usart_parity_config(USART, USART_PM_NONE);
usart_word_length_set(USART, USART_WL_8BIT);
usart_stop_bit_set(USART, USART_STB_1BIT);
usart_hardware_flow_rts_config(USART, USART_RTS_DISABLE);
usart_hardware_flow_cts_config(USART, USART_CTS_DISABLE);
usart_transmit_config(USART, USART_TRANSMIT_ENABLE);
usart_receive_config(USART, USART_RECEIVE_ENABLE);
usart_enable(USART);
nvic_irq_enable(USART1_IRQn, 0, 0);
}
void rs485_dma_init(void)
{
rcu_periph_clock_enable(RCU_DMA0);
dma_deinit(USART_DMA_STREAM);
dma_parameter_struct dma_init_struct;
dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
dma_init_struct.memory_addr = (uint32_t)tx_buffer;
dma_init_struct.memory_inc = DMA_MEMORY_INC_ENABLE;
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
dma_init_struct.periph_addr = (uint32_t)&USART_DATA(USART);
dma_init_struct.periph_inc = DMA_PERIPH_INC_DISABLE;
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
dma_init_struct.priority = DMA_PRIORITY_HIGH;
dma_init(USART_DMA_STREAM, dma_init_struct);
dma_circulation_disable(USART_DMA_STREAM);
dma_memory_to_memory_disable(USART_DMA_STREAM);
dma_channel_subperipheral_select(USART_DMA_STREAM, DMA_SUBPERI0_USART1_TX);
usart_dma_transmit_config(USART, USART_DMA_CHANNEL, USART_DENT_ENABLE);
dma_interrupt_enable(USART_DMA_STREAM, DMA_INT_FTF);
nvic_irq_enable(DMA0_Channel5_IRQn, 0, 0);
}
void rs485_send_dma(uint8_t *buffer, uint16_t len)
{
memcpy(tx_buffer, buffer, len);
dma_transfer_number_config(USART_DMA_STREAM, len);
dma_memory_address_config(USART_DMA_STREAM, (uint32_t)tx_buffer);
dma_channel_enable(USART_DMA_STREAM);
}
void rs485_receive_dma(void)
{
rx_len = 0;
dma_transfer_number_config(USART_DMA_STREAM, BUFFER_SIZE);
dma_memory_address_config(USART_DMA_STREAM, (uint32_t)rx_buffer);
dma_channel_enable(USART_DMA_STREAM);
}
void USART1_IRQHandler(void)
{
if (usart_interrupt_flag_get(USART, USART_INT_FLAG_RBNE))
{
uint8_t data = usart_data_receive(USART);
// 处理接收到的数据
if (rx_len < BUFFER_SIZE)
{
rx_buffer[rx_len++] = data;
}
}
}
void DMA0_Channel5_IRQHandler(void)
{
if (dma_interrupt_flag_get(DMA0, DMA_CH5, DMA_INT_FLAG_FTF))
{
dma_interrupt_flag_clear(DMA0, DMA_CH5, DMA_INT_FLAG_FTF);
dma_channel_disable(USART_DMA_STREAM);
}
}
```
这个示例代码中,我们使用了DMA来发送和接收RS485串口数据。在初始化函数 `rs485_dma_init()` 中,我们配置了USART的DMA传输通道,并设置了DMA的参数。在发送函数 `rs485_send_dma()` 中,我们将要发送的数据拷贝到发送缓冲区,并启动DMA传输。在接收函数 `rs485_receive_dma()` 中,我们启动DMA接收。
同时,我们也实现了USART和DMA的中断处理函数 `USART1_IRQHandler()` 和 `DMA0_Channel5_IRQHandler()`,用于处理接收和发送完成的中断事件。
希望这个示例对你有帮助!如果有任何问题,请随时提问。
阅读全文