2025-09-12 09:53:32 +00:00
|
|
|
#include "drv_usart.h"
|
|
|
|
#include "gd32f4xx.h"
|
|
|
|
|
|
|
|
// 串口配置结构体
|
|
|
|
static struct gd32_usart_config usart_config[] = {
|
|
|
|
{
|
|
|
|
.name = "test",
|
|
|
|
.usart_periph = USART0,
|
|
|
|
.rcu_periph_clock = RCU_USART0,
|
|
|
|
.gpio_port = GPIOA,
|
|
|
|
.rcu_gpio_clock = RCU_GPIOA,
|
|
|
|
.tx_pin = GPIO_PIN_9,
|
|
|
|
.rx_pin = GPIO_PIN_10,
|
|
|
|
.baud_rate = 9600,
|
|
|
|
.irq_type = USART0_IRQn
|
|
|
|
},
|
|
|
|
// 可以添加更多串口配置
|
|
|
|
};
|
|
|
|
|
|
|
|
// 初始化所有配置的串口
|
|
|
|
void gd32_usart_init(void)
|
|
|
|
{
|
2025-09-16 02:05:41 +00:00
|
|
|
for (int i = 0; i < sizeof(usart_config) / sizeof(usart_config[0]); i++)
|
|
|
|
{
|
2025-09-12 09:53:32 +00:00
|
|
|
gd32_usart_configure(&usart_config[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 配置单个串口
|
|
|
|
void gd32_usart_configure(struct gd32_usart_config *config)
|
|
|
|
{
|
|
|
|
// 启用时钟
|
|
|
|
rcu_periph_clock_enable(config->rcu_periph_clock);
|
|
|
|
rcu_periph_clock_enable(config->rcu_gpio_clock);
|
|
|
|
|
|
|
|
// 配置GPIO
|
|
|
|
gpio_af_set(config->gpio_port, GPIO_AF_7, config->tx_pin);
|
|
|
|
gpio_af_set(config->gpio_port, GPIO_AF_7, config->rx_pin);
|
|
|
|
gpio_mode_set(config->gpio_port, GPIO_MODE_AF, GPIO_PUPD_NONE, config->tx_pin);
|
|
|
|
gpio_mode_set(config->gpio_port, GPIO_MODE_AF, GPIO_PUPD_NONE, config->rx_pin);
|
|
|
|
gpio_output_options_set(config->gpio_port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, config->tx_pin);
|
|
|
|
gpio_output_options_set(config->gpio_port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, config->rx_pin);
|
|
|
|
|
|
|
|
// 配置USART
|
|
|
|
usart_deinit(config->usart_periph);
|
|
|
|
usart_baudrate_set(config->usart_periph, config->baud_rate);
|
2025-09-16 09:20:37 +00:00
|
|
|
usart_word_length_set(config->usart_periph, USART_WL_8BIT);
|
|
|
|
usart_stop_bit_set(config->usart_periph, USART_STB_1BIT);
|
|
|
|
usart_parity_config(config->usart_periph, USART_PM_NONE);
|
2025-09-16 02:05:41 +00:00
|
|
|
// usart_hardware_flow_rts_config(config->usart_periph, USART_RTS_DISABLE);
|
|
|
|
// usart_hardware_flow_cts_config(config->usart_periph, USART_CTS_DISABLE);
|
2025-09-12 09:53:32 +00:00
|
|
|
usart_receive_config(config->usart_periph, USART_RECEIVE_ENABLE);
|
|
|
|
usart_transmit_config(config->usart_periph, USART_TRANSMIT_ENABLE);
|
|
|
|
|
|
|
|
// 启用USART
|
|
|
|
usart_enable(config->usart_periph);
|
|
|
|
|
|
|
|
// 配置中断(如果需要)
|
|
|
|
// nvic_irq_enable(config->irq_type, 0, 0);
|
|
|
|
// usart_interrupt_enable(config->usart_periph, USART_INT_RBNE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 发送字符
|
|
|
|
void gd32_usart_putchar(struct gd32_usart_config *config, char ch)
|
|
|
|
{
|
|
|
|
usart_data_transmit(config->usart_periph, (uint16_t)ch);
|
|
|
|
while (usart_flag_get(config->usart_periph, USART_FLAG_TBE) == RESET);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 发送字符串
|
|
|
|
void gd32_usart_puts(struct gd32_usart_config *config, const char *str)
|
|
|
|
{
|
|
|
|
while (*str) {
|
|
|
|
gd32_usart_putchar(config, *str++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-09-16 02:05:41 +00:00
|
|
|
void usart_puts(const char *str)
|
|
|
|
{
|
|
|
|
gd32_usart_puts(&usart_config[0], str);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2025-09-12 09:53:32 +00:00
|
|
|
// 获取串口配置 by name
|
|
|
|
struct gd32_usart_config *gd32_usart_get_config(const char *name)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < sizeof(usart_config) / sizeof(usart_config[0]); i++) {
|
|
|
|
if (rt_strcmp(usart_config[i].name, name) == 0) {
|
|
|
|
return &usart_config[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return RT_NULL;
|
|
|
|
}
|