Files

92 lines
3.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "uart.h"
#include "gd32f4xx_usart.h"
#include "gd32f4xx_rcu.h"
#include "gd32f4xx_gpio.h"
#include "board_config.h"
#include "uart_ring_buffer.h"
void uart_init(void) {
/* 使能 GPIOA 和 USART 时钟 */
rcu_periph_clock_enable(UART_GPIO_RCU);
rcu_periph_clock_enable(UART_RCU);
/* 配置 PA2 为 USART1_TX,PA3 为 USART1_RX(AF7),debug 口纯串口 */
gpio_af_set(UART_GPIO_PORT, GPIO_AF_7, UART_TX_PIN | UART_RX_PIN);
gpio_mode_set(UART_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, UART_TX_PIN | UART_RX_PIN);
gpio_output_options_set(UART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, UART_TX_PIN | UART_RX_PIN);
/* 配置波特率、数据位、停止位等 */
usart_deinit(UART_PHY);
usart_word_length_set(UART_PHY, USART_WL_8BIT);
usart_stop_bit_set(UART_PHY, USART_STB_1BIT);
usart_parity_config(UART_PHY, USART_PM_NONE);
usart_baudrate_set(UART_PHY, UART_BAUDRATE);
usart_receive_config(UART_PHY, USART_RECEIVE_ENABLE);
usart_transmit_config(UART_PHY, USART_TRANSMIT_ENABLE);
usart_enable(UART_PHY);
nvic_irq_enable(UART_IRQ, 0, 0);
usart_interrupt_enable(UART_PHY, USART_INT_RBNE);
}
void uart_rx_irq_pause(void) {
usart_interrupt_disable(UART_PHY, USART_INT_RBNE);
}
void uart_rx_irq_resume(void) {
usart_interrupt_enable(UART_PHY, USART_INT_RBNE);
}
void debug_uart_write(uint8_t byte) {
/* 等待发送缓冲区空后写入 debug 串口 */
while (RESET == usart_flag_get(DEBUG_UART, USART_FLAG_TBE)) {}
usart_data_transmit(DEBUG_UART, byte);
}
/******************************************************************************/
/* 具体的中断处理函数实现 */
/******************************************************************************/
void usart1_irq_handler(void) {
// 处理USART1的接收中断
if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_RBNE)) {
uint8_t data = usart_data_receive(USART1);
(void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计
}
// 处理USART1的空闲中断
if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_IDLE)) {
usart_interrupt_flag_clear(USART1, USART_INT_FLAG_IDLE);
}
}
/* 调试串口初始化(与 uart_init 相同配置的独立入口,便于单独初始化打印口) */
void debug_usart_init(void)
{
/* 使能 GPIOA 和 USART1 时钟 */
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_USART1);
/* 配置 PA2(TX) 和 PA3(RX) 复用功能 AF7 */
gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_2 | GPIO_PIN_3);
/* 配置 GPIO 模式 */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2 | GPIO_PIN_3);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2 | GPIO_PIN_3);
/* USART1 配置 */
usart_deinit(USART1);
usart_baudrate_set(USART1, 115200U);
usart_word_length_set(USART1, USART_WL_8BIT);
usart_stop_bit_set(USART1, USART_STB_1BIT);
usart_parity_config(USART1, USART_PM_NONE);
usart_hardware_flow_rts_config(USART1, USART_RTS_DISABLE);
usart_hardware_flow_cts_config(USART1, USART_CTS_DISABLE);
usart_receive_config(USART1, USART_RECEIVE_ENABLE);
usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);
usart_enable(USART1);
}