Vscode开发STM32G431使用串口重定向

  • 在Vscode中使用STM32 VS Code Extension来开发STM32G431不同于keil,在keil中,我们只要勾选MicroLIB就能实现串口重定向
  • 在Vscode中我们要在STM32cubemx生成的代码下,在usart.c文件中加入如下代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "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;

}

注意:使用printf函数时,必须在末尾加上\r\n,否则无法输出