Files
bishe-STM32/System/Debug.c
T
2026-05-01 20:29:02 +08:00

92 lines
2.4 KiB
C
Raw 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 "Debug.h"
#include <stdarg.h>
#include <string.h>
#if DEBUG_ENABLE
// 调试缓冲区
static char debug_buffer[256];
/**
* @brief 初始化调试串口(USART2, PA2=TX, PA3=RX)
*/
void Debug_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// 使能时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
// 配置PA2为推挽输出(TX)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置PA3为浮空输入(RX)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置USART2
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx; // 只需要发送
USART_Init(USART2, &USART_InitStructure);
// 使能USART2
USART_Cmd(USART2, ENABLE);
// 发送启动信息
Debug_Printf("\r\n========================================\r\n");
Debug_Printf(" STM32 Face Recognition System\r\n");
Debug_Printf(" Debug Console Started\r\n");
Debug_Printf("========================================\r\n");
}
/**
* @brief 通过调试串口发送一个字节
*/
static void Debug_SendByte(uint8_t byte)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, byte);
}
/**
* @brief 通过调试串口发送字符串
*/
static void Debug_SendString(const char* str)
{
while (*str)
{
Debug_SendByte(*str++);
}
}
/**
* @brief 格式化调试信息输出(类似printf)
*/
void Debug_Printf(const char* format, ...)
{
va_list args;
va_start(args, format);
vsnprintf(debug_buffer, sizeof(debug_buffer), format, args);
va_end(args);
Debug_SendString(debug_buffer);
}
#else
// 调试关闭时,提供空实现
void Debug_Init(void) {}
void Debug_Printf(const char* format, ...) {}
#endif