STM32重定向
使用微库
1.KEIL-MDK中的Use MiscroLIB选项;
MicroLib提供了一个有限的stdio子系统,它仅支持未缓冲的stdin、stdout和stderr。
我们要做的是将调试信息打印到USART1中,所以需要对printf()函数所依赖的打印输出函数fputc()重定向(MicroLib中的printf()函数打印操作依赖fputc())。
2 .重定向fputc函数
在MicroLib的stdio.h中,fputc()函数的原型为:
int fputc(int ch, FILE* stream)
此函数原本是将字符ch打印到文件指针stream所指向的文件流去的,现在我们不需要打印到文件流,而是打印到串口1。基于前面的代码:
#include <stdio.h>
int fputc(int ch, FILE* stream)
{
USART_SendChar(USART1, (uint8_t)ch);
return ch;
}
3.重定向fgetc函数
/*
** Rewrite fgetc function and make scanf function work
**/
int fgetc(FILE* file)
{
while((USART1->ISR & UART_IT_RXNE) == RESET);
return USART1->RDR;
}
注意,需要包含头文件stdio.h,否则FILE类型未定义。
不使用微库
不使用微库(那么就要强调不使用半主机(no semihosting)模式)
1、包含头文件#include “stdio.h”
2、重写fputc,但需要先加点东西:
#pragma import(__use_no_semihosting)
_sys_exit(int x)
{
x = x;
}
struct __FILE
{
int handle;
};
FILE __stdout;
int fputc(int ch, FILE *f)
{
USART_SendData(DEBUG_USART, (unsigned char) ch);
while (!(DEBUG_USART->SR & USART_FLAG_TXE));
return (ch);