2025-09-12 09:53:32 +00:00
|
|
|
#include "drv_usart.h"
|
|
|
|
|
2025-09-30 10:06:38 +00:00
|
|
|
void usart_init(void)
|
|
|
|
{
|
|
|
|
usart_colok_config();
|
|
|
|
|
|
|
|
usart_gpio_config();
|
|
|
|
}
|
2025-09-12 09:53:32 +00:00
|
|
|
|
2025-09-30 10:06:38 +00:00
|
|
|
void usart_colok_config(void)
|
2025-09-12 09:53:32 +00:00
|
|
|
{
|
2025-09-30 10:06:38 +00:00
|
|
|
rcu_periph_clock_enable(USART_GPIO_CLK);//使能引脚时钟
|
|
|
|
rcu_periph_clock_enable(USART_PERIPH_CLK);//使能外设时钟
|
2025-09-12 09:53:32 +00:00
|
|
|
}
|
|
|
|
|
2025-09-30 10:06:38 +00:00
|
|
|
void usart_gpio_config(void)
|
2025-09-12 09:53:32 +00:00
|
|
|
{
|
|
|
|
// 配置GPIO
|
2025-09-30 10:06:38 +00:00
|
|
|
gpio_af_set(USART_GPIO_PORT, USART_GPIO_AF, USART_TX_PIN);
|
|
|
|
gpio_af_set(USART_GPIO_PORT, USART_GPIO_AF, USART_RX_PIN);
|
|
|
|
|
|
|
|
// gpio_mode_set(USART_GPIO_PORT, USART_GPIO_AF, GPIO_PUPD_PULLUP, USART_TX_PIN | USART_RX_PIN);
|
|
|
|
gpio_mode_set(USART_GPIO_PORT, USART_GPIO_AF, GPIO_PUPD_NONE, USART_TX_PIN | USART_RX_PIN);
|
|
|
|
gpio_output_options_set(USART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, USART_TX_PIN | USART_RX_PIN);
|
2025-09-12 09:53:32 +00:00
|
|
|
// 配置USART
|
2025-09-30 10:06:38 +00:00
|
|
|
usart_deinit(USART_PERIPH);
|
|
|
|
usart_baudrate_set(USART_PERIPH, USART_BAUD_RATE);
|
|
|
|
|
|
|
|
usart_receive_config(USART_PERIPH, USART_RECEIVE_ENABLE);
|
|
|
|
usart_transmit_config(USART_PERIPH, USART_TRANSMIT_ENABLE);
|
2025-09-12 09:53:32 +00:00
|
|
|
|
|
|
|
// 启用USART
|
2025-09-30 10:06:38 +00:00
|
|
|
usart_enable(USART_PERIPH);
|
|
|
|
|
2025-09-12 09:53:32 +00:00
|
|
|
// 配置中断(如果需要)
|
|
|
|
// nvic_irq_enable(config->irq_type, 0, 0);
|
|
|
|
// usart_interrupt_enable(config->usart_periph, USART_INT_RBNE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|