#include "Printf.h" #include "stdio.h" #include #include "FreeRTOS.h" #include "cmsis_os.h" #include "task.h" #include "queue.h" extern osMessageQueueId_t printqueueHandle; //此处代码与使用 Printf 函数相关,使用时要添加 stdio.h 文件 #ifdef __GNUC__ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif PUTCHAR_PROTOTYPE { HAL_UART_Transmit(&huart2 , (uint8_t *)&ch, 1, 0xFFFF); return ch; } void CommonPrintf(const char *fmt, ...) { Print_msg msg; va_list args; va_start(args, fmt); vsnprintf(msg.msg, PRINT_BUF_SIZE, fmt, args); va_end(args); osStatus_t status =osMessageQueuePut(printqueueHandle, &msg, 0, 0); // 立即入队 if (status != osOK) { printf("[WARN] Failed to send log to queue (status=%d)\n", (int)status); } }