关于STM32中 printf 与 scanf 的重定向问题在此我仅对不使用 "USE MircoLIB" 的情况做整理(针对Keil RVMDK开发环境)。
① :首先需要在 usart.h 中包含 “stdio.h” 头文件
② :在 usart.c 中,加入如下代码块,以此避免使用半主机模式,并重定向 printf 和scanf 函数;
#if 1
#pragma import (__use_no_semihosting_swi)
/*标准库需要的支持函数,use_no_semihosting_swi以避免使用半主机模式*/
struct __FILE
{
int handle;
};
FILE __stdout;
FILE __stdin;
/*重定向Printf函数*/
int fputc(int ch,FILE *f)
{
return (SendChar(ch));
}
/*重定向Scanf函数*/
int fgetc(FILE *f)
{
return (SendChar(GetKey()));
/*调用scanf()在串口中输入数据时,必须以空格结束,否则无法完成发送*/
}
void _ttywrch(int ch)
{
SendChar(ch);
}
int _ferror(FILE *f) {
/* Your implementation of ferror */
return EOF;
}
//定义_sys_exit()以避免使用半主机模式
void _sys_exit(int return_code){
//x = x;
label:goto label;
}
#endif
③ :在 usart.c 中添加SendChar()与GetKey()函数