STM32 Cubemx Printf重定向

1.对于版本比较低的STM32CubeMX(比如V4.25.0),生成的工程缺少syscall.c文件。syscall.c文件复制到startup目录下,而新版本的STM32CubeMX(V5.6.0)会自动生成syscall.c文件,在Src文件夹下。

 2.添加必须要的代码段:

 1 /* Private function prototypes -----------------------------------------------*/
 2 void SystemClock_Config(void);
 3  
 4 /* USER CODE BEGIN PFP */
 5 /* Private function prototypes -----------------------------------------------*/
 6 #ifdef __GNUC__
 7 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
 8 #else
 9 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
10 #endif 
11 /* USER CODE END PFP */
12  
13 /* USER CODE BEGIN 4 */
14 /*retargets the C library printf function to the USART*/
15 PUTCHAR_PROTOTYPE
16 {
17   HAL_UART_Transmit(&huart1,(uint8_t*)&ch, 1, 0xFFFF);
18   return ch;
19 }
20 /* USER CODE END 4 */

3.属性设置,添加链接标识用于打印浮点型数据  -u _printf_float

4.Printf 一定要带上 \r\n, 不然无法输出打印信息。

猜你喜欢

转载自www.cnblogs.com/mickey-double/p/12515270.html