怎么解決STM32(MDK)中不能使用printf()函數(shù)的問題
標(biāo)準(zhǔn)庫函數(shù)的默認(rèn)輸出設(shè)備是顯示器,要實(shí)現(xiàn)在串口或LCD輸出,必須重定義標(biāo)準(zhǔn)庫函數(shù)里調(diào)用的與輸出設(shè)備相關(guān)的函數(shù).
例如:printf輸出到串口,需要將fputc里面的輸出指向串口(重定向),方法如下:
#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 */
USART_SendData(USART1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return ch;
}
因printf()之類的函數(shù),使用了半主機(jī)模式。使用標(biāo)準(zhǔn)庫會(huì)導(dǎo)致程序無法運(yùn)行,以下是解決方法:
方法1.使用微庫,因?yàn)槭褂梦斓脑?不會(huì)使用半主機(jī)模式.
方法2.仍然使用標(biāo)準(zhǔn)庫,在主程序添加下面代碼:
#pragma import(__use_no_semihosting)
_sys_exit(int x)
{
x = x;
}
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;
如果使用的是MDK,請(qǐng)?jiān)诠こ虒傩缘?ldquo;Target“-》”Code Generation“中勾選”Use MicroLIB;今天參考了一下論壇,使用微庫可以很好的解決這個(gè)問題。
2.另一種方法:(其實(shí)大同小異)
需要添加以下代碼
(論壇里應(yīng)該有完整介紹這個(gè)的帖子,但是我沒搜到,也許是沉了。)
#pragma import(__use_no_semihosting)
/******************************************************************************
*標(biāo)準(zhǔn)庫需要的支持函數(shù)
******************************************************************************/
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;
///
/// 定義_sys_exit()以避免使用半主機(jī)模式
///
///
///
_sys_exit(int x)
{
x = x;
}
int fputc(int ch, FILE *f)
{
//USART_SendData(USART1, (u8) ch);
USART1->DR = (u8) ch;
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{
}
return ch;
}
semihosting的作用,介紹如下
Semihosting is a mechanism for ARM targets to communicate input/output requests
from application code to a host computer running a debugger. This mechanism could be
used, for example, to allow functions in the C library, such as printf() and scanf(), to use the screen and keyboard of the host rather than having a screen and keyboard on the target system.
This is useful because development hardware often does not have all the input and
output facilities of the final system. Semihosting allows the host computer to provide these facilities.
Semihosting is implemented by a set of defined software interrupt (SWI) operations.
The application invokes the appropriate SWI and the debug agent then handles the SWI
exception. The debug agent provides the required communication with the host.
In many cases, the semihosting SWI will be invoked by code within library functions. The application can also invoke the semihosting SWI directly. Refer to the C library descriptions in the ADS Compilers and Libraries Guide for more information on support for semihosting in the ARM C library.
按我的理解,這個(gè)模式是用來調(diào)試的,通過仿真器,使用主機(jī)的輸入輸出代替單片機(jī)自己的,也就是說即便單片機(jī)沒有輸出口也能printf到電腦上。反過來,由于這個(gè)模式更改了printf()等的實(shí)現(xiàn)方式,輸入輸出就不走單片機(jī)的外設(shè)了,所以只重定義fputc不起作用。
用代碼關(guān)閉此模式后,需要同時(shí)更新一下__stdout 和__stdin 的定義,所以有后面的語句。
以上僅為個(gè)人理解,如有錯(cuò)誤請(qǐng)指正。
另外,勾選microlib之后,也許編譯的時(shí)候就不把開啟semihosting的文件包進(jìn)去了,所以沒事。
C庫函數(shù)重定向:
用戶能定義自己的C語言庫函數(shù),連接器在連接時(shí)自動(dòng)使用這些新的功能函數(shù)。這個(gè)過程叫做重定向C語言庫函數(shù),如下圖所示。
舉例來說,用戶有一個(gè)I/O設(shè)備(如UART)。本來庫函數(shù)fputc()是把字符輸出到調(diào)試器控制窗口中去的,但用戶把輸出設(shè)備改成了UART端口,這樣一來,所有基于fputc()函數(shù)的printf()系列函數(shù)輸出都被重定向到UART端口上去了。
下面是實(shí)現(xiàn)fputc()重定向的一個(gè)例子:
externvoidsendchar(char*ch);
intfputc(intch,FILE*f)
{/*e.g.writeacharactertoanUART*/
chartempch=ch;
sendchar(&tempch);
returnch;
}
這個(gè)例子簡(jiǎn)單地將輸入字符重新定向到另一個(gè)函數(shù)sendchar(),sendchar()假定是個(gè)另外定義的串口輸出函數(shù)。在這里,fputc()就似乎目標(biāo)硬件和標(biāo)準(zhǔn)C庫函數(shù)之間的一個(gè)抽象層。
評(píng)論