Printf.c 869 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "Printf.h"
  2. #include "stdio.h"
  3. #include <stdarg.h>
  4. #include "FreeRTOS.h"
  5. #include "cmsis_os.h"
  6. #include "task.h"
  7. #include "queue.h"
  8. extern osMessageQueueId_t printqueueHandle;
  9. //此处代码与使用 Printf 函数相关,使用时要添加 stdio.h 文件
  10. #ifdef __GNUC__
  11. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  12. #else
  13. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  14. #endif
  15. PUTCHAR_PROTOTYPE
  16. {
  17. HAL_UART_Transmit(&huart2 , (uint8_t *)&ch, 1, 0xFFFF);
  18. return ch;
  19. }
  20. void CommonPrintf(const char *fmt, ...)
  21. {
  22. Print_msg msg;
  23. va_list args;
  24. va_start(args, fmt);
  25. vsnprintf(msg.msg, PRINT_BUF_SIZE, fmt, args);
  26. va_end(args);
  27. osStatus_t status =osMessageQueuePut(printqueueHandle, &msg, 0, 0); // 立即入队
  28. if (status != osOK) {
  29. printf("[WARN] Failed to send log to queue (status=%d)\n", (int)status);
  30. }
  31. }