<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 怎么解決STM32(MDK)中不能使用printf()函數(shù)的問題

          怎么解決STM32(MDK)中不能使用printf()函數(shù)的問題

          作者: 時間:2016-11-18 來源:網(wǎng)絡 收藏
          簡單地說:想在mdk 中用printf,需要同時重定義fputc函數(shù)和避免使用semihosting(半主機模式),
          標準庫函數(shù)的默認輸出設備是顯示器,要實現(xiàn)在串口或LCD輸出,必須重定義標準庫函數(shù)里調用的與輸出設備相關的函數(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ù),使用了半主機模式。使用標準庫會導致程序無法運行,以下是解決方法:
          方法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“-》”Code Generation“中勾選”Use MicroLIB;今天參考了一下論壇,使用微庫可以很好的解決這個問題。
          2.另一種方法:(其實大同小異)
          需要添加以下代碼
          (論壇里應該有完整介紹這個的帖子,但是我沒搜到,也許是沉了。)
          #pragma import(__use_no_semihosting)
          /******************************************************************************
          *標準庫需要的支持函數(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()以避免使用半主機模式
          ///

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

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

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


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

          C庫函數(shù)重定向:
          用戶能定義自己的C語言庫函數(shù),連接器在連接時自動使用這些新的功能函數(shù)。這個過程叫做重定向C語言庫函數(shù),如下圖所示。
          舉例來說,用戶有一個I/O設備(如UART)。本來庫函數(shù)fputc()是把字符輸出到調試器控制窗口中去的,但用戶把輸出設備改成了UART端口,這樣一來,所有基于fputc()函數(shù)的printf()系列函數(shù)輸出都被重定向到UART端口上去了。
          下面是實現(xiàn)fputc()重定向的一個例子:
          externvoidsendchar(char*ch);
          intfputc(intch,FILE*f)
          {/*e.g.writeacharactertoanUART*/
          chartempch=ch;
          sendchar(&tempch);
          returnch;

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


          關鍵詞: STM32MDKprintf()函

          評論


          技術專區(qū)

          關閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();