RISC-V|RISC-V_GD32VF103-USART 串口中断接收 printf发送

需要把板子上的跳线帽插上才可以正常使用 usart0 。
USART固件库函数介绍
RISC-V|RISC-V_GD32VF103-USART 串口中断接收 printf发送
文章图片

RISC-V|RISC-V_GD32VF103-USART 串口中断接收 printf发送
文章图片

RISC-V|RISC-V_GD32VF103-USART 串口中断接收 printf发送
文章图片

usart0.c

#include "usart0.h" #include "gd32vf103.h" #include "gd32vf103_libopt.h"// ************************************************************************************************************** // //USART0初始化 void USART0_Init() { rcu_periph_clock_enable(RCU_GPIOA); //使能GPIOA时钟 rcu_periph_clock_enable(RCU_USART0); //使能USART0时钟 gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9); //USART_TX gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10); //USART_RX usart_deinit(USART0); usart_baudrate_set(USART0, 115200U); //波特率 115200 usart_word_length_set(USART0, USART_WL_8BIT); //8位数据位 usart_stop_bit_set(USART0, USART_STB_1BIT); //1位停止位 usart_parity_config(USART0, USART_PM_NONE); //无奇偶校验 usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE); usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE); usart_receive_config(USART0, USART_RECEIVE_ENABLE); //使能USART接收 usart_transmit_config(USART0, USART_TRANSMIT_ENABLE); //使能USART发送 usart_interrupt_enable(USART0,USART_INT_RBNE); //使能接收中断 eclic_irq_enable(USART0_IRQn,1,0); //选择USART0中断线,中断优先级和子优先级 usart_enable(USART0); //使能USART0 } // ************************************************************************************************************** // //USART0中断服务函数 void USART0_IRQHandler(void) { uint8_t Usart_rec = 0; if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE) != RESET) //接收中断 { Usart_rec = usart_data_receive(USART0); usart_data_transmit(USART0, Usart_rec); } //if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_TBE)) //发送中断 //{ //} } // ************************************************************************************************************** // //重定向 C库函数 int _put_char(int ch) { usart_data_transmit(USART0, (uint8_t) ch ); while ( usart_flag_get(USART0, USART_FLAG_TBE) == RESET) {} return ch; }

usart.h
#ifndef__USART0_H__ #define__USART0_H__ #include "gd32vf103.h" #include "systick.h" #include #include // ************************************************************************************************************** //// ************************************************************************************************************** // void USART0_Init(void); void USART0_IRQHandler(void); #endif

main.c
#include "gd32vf103.h" #include "gd32vf103c_start.h" #include "systick.h" #include #include #include "led.h" #include "key.h" #include "usart0.h"// ************************************************************************************************************* // //主函数 int main(void) { uint8_t key = 0; eclic_global_interrupt_enable(); eclic_priority_group_set(ECLIC_PRIGROUP_LEVEL3_PRIO1); LED_Init(); //LED灯初始化 KEY_Init(); //KEY按键初始化 USART0_Init(); //USART0初始化 printf("串口测试 USART TEST 0123456789 \n\r"); while(1) { key = KEY_Scan(); if(key == 1) {LED0_T; printf("按键按下 LED 反转 \n\r"); } } }

GD 的 USART 换行是 \n\r 不过在串口助手里面不能正常换行,在软件自带终端才可以,不过自带终端显示不全,不知道是我问题还是啥 -x_x-
【RISC-V|RISC-V_GD32VF103-USART 串口中断接收 printf发送】个人见解,感谢阅读。

    推荐阅读