串口日志在我们开发中常起到辅助调试开发,本篇讲述GD32H759I-EVAL开发板串口打印。
一.硬件原理
开发板板载USB转串口,串口引脚为:PA9--TX,PA10--RX ,原理图如下:
图1:串口原理
二.代码工程准备
1.串口引脚宏定义
/* eval board low layer COM */
#define COMn 1U
/* definition for COM, connected to USART0 */
#define EVAL_COM USART0
#define EVAL_COM_CLK RCU_USART0
#define EVAL_COM_TX_PIN GPIO_PIN_9
#define EVAL_COM_RX_PIN GPIO_PIN_10
#define EVAL_COM_GPIO_PORT GPIOA
#define EVAL_COM_GPIO_CLK RCU_GPIOA
#define EVAL_COM_AF GPIO_AF_7
2.串口初始化
/*!
\brief usart configure
\param[in] none
\param[out] none
\retval none
*/
static void usart_config(void)
{
/* enable GPIO clock */
rcu_periph_clock_enable(EVAL_COM_GPIO_CLK);
/* enable USART clock */
rcu_periph_clock_enable(EVAL_COM_CLK);
/* connect port to USART0 TX */
gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, EVAL_COM_TX_PIN);
/* connect port to USART0 RX */
gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, EVAL_COM_RX_PIN);
/* configure USART TX as alternate function push-pull */
gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, EVAL_COM_TX_PIN);
gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, EVAL_COM_TX_PIN);
/* configure USART RX as alternate function push-pull */
gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, EVAL_COM_RX_PIN);
gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, EVAL_COM_RX_PIN);
/* USART configure */
usart_deinit(EVAL_COM);
usart_word_length_set(EVAL_COM, USART_WL_8BIT);
usart_stop_bit_set(EVAL_COM, USART_STB_1BIT);
usart_parity_config(EVAL_COM, USART_PM_NONE);
usart_baudrate_set(EVAL_COM, 115200U);
usart_receive_config(EVAL_COM, USART_RECEIVE_ENABLE);
usart_transmit_config(EVAL_COM, USART_TRANSMIT_ENABLE);
usart_enable(EVAL_COM);
}
3.串口重映射
在魔术棒->Target选项卡下勾选Use MicroLIB,添加重映射函数fputc如下
图2:串口重映射
4.编译烧录
三.运行测试
1.开发板插上USB转串口线如下
图3:USB转串口线连接
2.打开串口助手,选择好COM口,波特率,按复位键,可看到日志如下
图4:串口日志输出
至此,实现GD32H759I-EVAL串口日志输出