关于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()函数 int SendChar(int ch) { while(!(USART1->SR & USART_FLAG_TXE)); USART1->DR = (ch & 0x1FF); return ch; } int GetKey(void) { while(!(USART1->SR & USART_FLAG_RXNE)); return ((int)(USART1->DR & 0X1FF)); }
完成以上三步,即可实现printf()函数与scanf()的串口重定向,将标准输入输出流的来源或去向改为串口。
关于第二步所使用的避免使用半主机模式的代码,其实Kei已经为我们写好了一个Retarget.c文件,在Keil/ARM/Startup目录下.
另外本文针对的开发环境为Keil RVMDK,本人在Emblocks开源开发工具中实验时,本方法是无法实现printf和Scanf的重定向的,以及在开源工具下如何"Use microLIB"的问题都有待进一步探讨。
