娇小w搡bbbb搡bbb,《第一次の人妻》,中国成熟妇女毛茸茸,边啃奶头边躁狠狠躁视频免费观看

歷史上的今天

今天是:2024年10月30日(星期三)

正在發生

2020年10月30日 | STM32(MDK)中不能使用printf()函數的問題

發布者:獨享留白1028 來源: eefocus關鍵字:STM32  MDK  printf()函數 手機看文章 掃描二維碼
隨時隨地手機看文章

簡單地說:想在mdk 中用printf,需要同時重定義fputc函數和避免使用semihosting(半主機模式),

標準庫函數的默認輸出設備是顯示器,要實現在串口或LCD輸出,必須重定義標準庫函數里調用的與輸出設備相關的函數.


例如: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 */

return ch;

}


因printf()之類的函數,使用了半主機模式。使用標準庫會導致程序無法運行,以下是解決方法:

方法1.使用微庫,因為使用微庫的話,不會使用半主機模式.


方法2.仍然使用標準庫,在主程序添加下面代碼:

#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,請在工程屬性的“Target“-》”Co de Generation“中勾選”Use MicroLIB;今天參考了一下論壇,使用微庫可以很好的解決這個問題。


2.另一種方法:(其實大同小異)  

需要添加以下代碼 

(論壇里應該有完整介紹這個的帖子,但是我沒搜到,也許是沉了。)

#pragma 

import(__use_no_semihosting)  

/******************************************************************************  

*標準庫需要的支持函數  

******************************************************************************/  

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()以避免使用半主機模式  

///

  

/// name="x">  

/// 

  

_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 co de 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 co de 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. 


按我的理解,這個模式是用來調試的,通過仿真器,使用主機的輸入輸出代替單片機自己的,也就是說即便單片機沒有輸出口也能printf到電腦上。反過來,由于這個模式更改了printf()等的實現方式,輸入輸出就不走單片機的外設了,所以只重定義fputc不起作用。 


用代碼關閉此模式后,需要同時更新一下__stdout 和__stdin 的定義,所以有后面的語句。 

以上僅為個人理解,如有錯誤請指正。 


另外,勾選microlib之后,也許編譯的時候就不把開啟semihosting的文件包進去了,所以沒事。


C庫函數重定向:

用戶能定義自己的C語言庫函數,連接器在連接時自動使用這些新的功能函數。這個過程叫做重定向C語言庫函數,如下圖所示。


舉例來說,用戶有一個I/O設備(如UART)。本來庫函數fputc()是把字符輸出到調試器控制窗口中去的,但用戶把輸出設備改成了UART端口,這樣一來,所有基于fputc()函數的printf()系列函數輸出都被重定向到UART端口上去了。


下面是實現fputc()重定向的一個例子:

externvoidsendchar(char*ch);

intfputc(intch,FILE*f)

{/*e.g.writeacharactertoanUART*/

    chartempch=ch;

    sendchar(&tempch);

    return ch;


這個例子簡單地將輸入字符重新定向到另一個函數sendchar(),sendchar()假定是個另外定義的串口輸出函數。在這里,fputc()就似乎目標硬件和標準C庫函數之間的一個抽象層。


關鍵字:STM32  MDK  printf()函數 引用地址:STM32(MDK)中不能使用printf()函數的問題

上一篇:STM32 USB 大容量存儲器Mass Storage工程的講解
下一篇:STM32 USB數據接收與數據發送程序流程分析

推薦閱讀

集微網消息,三星的Galaxy Note9發布時,官方稱將會提供512GB的存儲卡供用戶購買以便擴展它的存儲,然而當Galaxy Note9全球銷售的時候,三星說的512GB存儲卡并沒有推出來,當時官方稱將會在某個時間段發布該容量的存儲卡,目前容量為512GB的存儲卡已經在三星德國官方網站上架了。三星的512GB存儲卡作為EVO Plus系列的一部分,它的等級為C10,讀寫速度分...
一、利用三星已經寫好的初始化文件1。先見項目文件夾2。建立項目保存在新建文件夾中3。將ARM三星寫好的INIT文件夾中的INC和SRC兩個文件夾拖入新建文件夾中。4。建立C文件,如LCD1602.c,編寫void xmain()函數,或void xxxx()函數5。添加SRC中的2440init到項目中來。6。將2440init中的相應主函數入口修改為LCD1602.c中的函數xmain,這一點很重要,這...
英特爾宣布將收購總部位于舊金山的AI軟件供應商SigOpt,交易條款暫未對外披露。英特爾預計該交易將在本季度完成。SigOpt的AI軟件技術可通過深度學習,機器學習和數據分析中的硬件和軟件參數,用例和工作負載來提高生產力和性能。英特爾計劃在英特爾的AI硬件產品中使用SigOpt的軟件技術,以幫助加速,擴大和擴展面向開發人員的英特爾AI軟件解決方案。
根據調研機構Strategy Analytics的最新報告,2021年Q3,全球智能手機出貨量同比下降10.8%,為3.273億部。受到供應限制的影響,智能手機市場結束了過去四個季度的復蘇階段,供應限制重創智能手機市場,許多廠商無法滿足即將到來的假日季的強勁需求,預計供應限制將持續到2022年上半年。具體數據顯示,2021年Q3,三星保持領先地位——其全球智能手機出貨量...

史海拾趣

小廣播
設計資源 培訓 開發板 精華推薦

最新單片機文章
何立民專欄 單片機及嵌入式寶典

北京航空航天大學教授,20余年來致力于單片機與嵌入式系統推廣工作。

 
EEWorld訂閱號

 
EEWorld服務號

 
汽車開發圈

 
機器人開發圈

電子工程世界版權所有 京ICP證060456號 京ICP備10001474號-1 電信業務審批[2006]字第258號函 京公網安備 11010802033920號 Copyright ? 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
主站蜘蛛池模板: 田阳县| 滕州市| 尖扎县| 咸阳市| 莎车县| 且末县| 鄂尔多斯市| 台山市| 绩溪县| 得荣县| 沭阳县| 璧山县| 玛多县| 方城县| 临城县| 万安县| 石柱| 龙胜| 宿迁市| 三明市| 定安县| 潮安县| 普安县| 伊吾县| 保亭| 北流市| 石渠县| 新丰县| 仁化县| 金溪县| 南皮县| 井陉县| 临夏县| 嘉禾县| 云和县| 阿鲁科尔沁旗| 玉山县| 鄂伦春自治旗| 庐江县| 德令哈市| 莱州市|