You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.6 KiB
65 lines
1.6 KiB
//
|
|
// Created by iflyt on 2025/2/27.
|
|
//
|
|
|
|
#include "uart_dbg.h"
|
|
#include "bsp.h"
|
|
#include "main.h"
|
|
|
|
UART_HandleTypeDef huart_dbg;
|
|
|
|
// 初始化调试串口
|
|
void DBG_UART_Init(void)
|
|
{
|
|
// 使能 GPIO 和 USART 时钟
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
__HAL_RCC_USART1_CLK_ENABLE();
|
|
|
|
// 配置 GPIO 为复用功能
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
GPIO_InitStruct.Pin = DBG_UART_TX_PIN | DBG_UART_RX_PIN;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
GPIO_InitStruct.Alternate = DBG_UART_GPIO_AF;
|
|
HAL_GPIO_Init(DBG_UART_GPIO_PORT, &GPIO_InitStruct);
|
|
|
|
// 配置 USART
|
|
huart_dbg.Instance = DBG_UART;
|
|
huart_dbg.Init.BaudRate = DBG_UART_BAUDRATE;
|
|
huart_dbg.Init.WordLength = UART_WORDLENGTH_8B;
|
|
huart_dbg.Init.StopBits = UART_STOPBITS_1;
|
|
huart_dbg.Init.Parity = UART_PARITY_NONE;
|
|
huart_dbg.Init.Mode = UART_MODE_TX_RX;
|
|
huart_dbg.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
huart_dbg.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
if (HAL_UART_Init(&huart_dbg) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
// 重定向 write 函数,实现 printf 重映射
|
|
__attribute__((used)) int _write(int file, char *ptr, int len)
|
|
{
|
|
#if 1
|
|
HAL_UART_Transmit(&huart_dbg, (uint8_t *)ptr, len, HAL_MAX_DELAY);
|
|
return len;
|
|
#else
|
|
int i;
|
|
for (i = 0; i < len; i++) {
|
|
uint8_t c = *ptr++;
|
|
HAL_UART_Transmit(&huart_dbg, &c, 1, 100);
|
|
}
|
|
return len;
|
|
#endif
|
|
}
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|