gd32f407 具体rte 1.0.3 标准库 串口5收发驱动程序
时间: 2024-05-06 20:17:22 浏览: 148
以下是使用RTE 1.0.3标准库的GD32F407芯片的串口5收发驱动程序示例:
```c
#include "gd32f4xx.h"
#include "RTE_Device.h"
#include "RTE_Components.h"
/* 定义串口接口 */
#define USARTx_INSTANCE USART5
#define USARTx_CLK_ENABLE() rcu_periph_clock_enable(RCU_USART5)
#define USARTx_RX_GPIO_CLK_ENABLE() rcu_periph_clock_enable(RCU_GPIOD)
#define USARTx_TX_GPIO_CLK_ENABLE() rcu_periph_clock_enable(RCU_GPIOC)
#define USARTx_FORCE_RESET() rcu_periph_reset_enable(RCU_USART5RST)
#define USARTx_RELEASE_RESET() rcu_periph_reset_disable(RCU_USART5RST)
#define USARTx_TX_PIN GPIO_PIN_12
#define USARTx_TX_GPIO_PORT GPIOC
#define USARTx_RX_PIN GPIO_PIN_2
#define USARTx_RX_GPIO_PORT GPIOD
#define USARTx_IRQn USART5_IRQn
#define USARTx_IRQHandler USART5_IRQHandler
/* 定义缓冲区大小 */
#define RX_BUFFER_SIZE 256
#define TX_BUFFER_SIZE 256
/* 定义缓冲区 */
uint8_t rx_buffer[RX_BUFFER_SIZE];
uint8_t tx_buffer[TX_BUFFER_SIZE];
/* 定义缓冲区索引 */
volatile uint16_t rx_write_index = 0;
volatile uint16_t rx_read_index = 0;
volatile uint16_t tx_write_index = 0;
volatile uint16_t tx_read_index = 0;
/* 初始化串口 */
void usart_init(void)
{
usart_parameter_struct usart_init_struct;
USARTx_CLK_ENABLE();
USARTx_TX_GPIO_CLK_ENABLE();
USARTx_RX_GPIO_CLK_ENABLE();
usart_deinit(USARTx_INSTANCE);
gpio_init(USARTx_TX_GPIO_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, USARTx_TX_PIN);
gpio_init(USARTx_RX_GPIO_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, USARTx_RX_PIN);
usart_init_struct.baud_rate = 115200;
usart_init_struct.word_length = USART_WL_8BIT;
usart_init_struct.stop_bits = USART_STB_1BIT;
usart_init_struct.parity = USART_PM_NONE;
usart_init_struct.hardware_flow_control = USART_HFCTRL_NONE;
usart_init_struct.mode = USART_MODE_TX_RX;
usart_init(USARTx_INSTANCE, &usart_init_struct);
usart_interrupt_enable(USARTx_INSTANCE, USART_INT_RBNE);
usart_interrupt_enable(USARTx_INSTANCE, USART_INT_IDLE);
usart_enable(USARTx_INSTANCE);
}
/* 中断处理函数 */
void USARTx_IRQHandler(void)
{
uint8_t data;
if (RESET != usart_interrupt_flag_get(USARTx_INSTANCE, USART_INT_RBNE)) {
data = (uint8_t) usart_data_receive(USARTx_INSTANCE);
rx_buffer[rx_write_index++] = data;
if (rx_write_index >= RX_BUFFER_SIZE) {
rx_write_index = 0;
}
}
if (RESET != usart_interrupt_flag_get(USARTx_INSTANCE, USART_INT_IDLE)) {
data = (uint8_t) usart_data_receive(USARTx_INSTANCE);
usart_interrupt_disable(USARTx_INSTANCE, USART_INT_IDLE);
}
if (RESET != usart_interrupt_flag_get(USARTx_INSTANCE, USART_INT_TC)) {
if (tx_write_index != tx_read_index) {
usart_data_transmit(USARTx_INSTANCE, tx_buffer[tx_read_index++]);
if (tx_read_index >= TX_BUFFER_SIZE) {
tx_read_index = 0;
}
} else {
usart_interrupt_disable(USARTx_INSTANCE, USART_INT_TC);
}
}
}
/* 发送数据 */
int32_t usart_send(const void *data, uint32_t num)
{
uint16_t i;
for (i = 0; i < num; i++) {
tx_buffer[tx_write_index++] = ((uint8_t *) data)[i];
if (tx_write_index >= TX_BUFFER_SIZE) {
tx_write_index = 0;
}
}
usart_interrupt_enable(USARTx_INSTANCE, USART_INT_TC);
return i;
}
/* 接收数据 */
int32_t usart_receive(void *data, uint32_t num)
{
uint16_t i;
for (i = 0; i < num; i++) {
if (rx_write_index == rx_read_index) {
break;
}
((uint8_t *) data)[i] = rx_buffer[rx_read_index++];
if (rx_read_index >= RX_BUFFER_SIZE) {
rx_read_index = 0;
}
}
return i;
}
```
这个示例使用了缓冲区进行数据的接收和发送。在中断处理函数中将接收到的数据存储到接收缓冲区中,而发送数据时先将要发送的数据存储到发送缓冲区中,然后开启发送中断,在中断处理函数中将发送缓冲区的数据逐个发送出去。通过使用缓冲区,可以让主程序和中断处理函数之间进行数据的交换,从而避免了数据的丢失和冲突。
阅读全文