STM32开发踩坑——基于GCC环境下的printf输出重定向

成立这个专栏的目的是,记录自己嵌入式开发遇到的问题,与成功的解决方法,方便自己回顾。

注意:GCC环境下printf函数实现机制与MDK环境下实现机制不同,以下操作可以合二为一

实现步骤:

1.添加头文件stdio

#include "stdio.h"

2.在 usart.c 的前部加入:

#ifdef __GNUC__
/* With GCC, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

实现了统一接口,只需要实现PUTCHAR_PROTOTYPE函数即可

3.在 usart.c 的后部加入:

PUTCHAR_PROTOTYPE
{
	while((USART2->SR & 0x40) == 0);
	USART2->DR = (uint8_t)ch;
	return ch;
}

输出无数据时,注意缓冲区刷新条件是否满足。

你可能感兴趣的:(STM32,stm32,嵌入式硬件,单片机)