增加Retarget.c,实现printf重定向
你可以把 Retarget.c 添加到你的工程里, 就可以了。
D:\Program Files\Keil\ARM\Startup\
然后只重载
模拟串口使用printf函数
extern int  sendchar(int ch);  /* in Serial.c */
extern int  getkey(void);      /* in Serial.c */
int  sendchar(int ch)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
rs485_w()
USART_SendData(USART3, (unsigned char) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
rs485_r();
return ch;
}
方法二:
#include "stdio.h"
#ifdef __GNUC__
/* With GCC/RAISONANCE, 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
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
rs485_w()  //重要
USART_SendData(USART3, (unsigned char) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
rs485_r();//重要
return ch;
}
//printf()//之类的函数,使用了半主机模式。使用标准库会导致程序无法运行,以下是解决方法:
#pragma import(__use_no_semihosting)
_sys_exit(int x)
{
x = x;
} /*.\OUTPUT\STM32_4PMaser.axf: Error: L6915E: Library reports error: __use_no_semihosting was requested, but _ttywrch was referenced*/
_ttywrch(int ch)    //可以消除这个错误
{
ch=ch;
}
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
/* FILE is typedef’ d in stdio.h. */
FILE __stdout;
//串口1 GPIO_Pin_11,A
//串口2 GPIO_Pin_4,A
//串口3 GPIO_Pin_12,GPIOB