Question about using printf function in stm32

Publisher:Blissful567Latest update time:2019-01-17 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

If you want to use printf in mdk, you need to redefine the fputc function and avoid using semihosting (semihosting mode).


The default output device of the standard library function is the display. To output to the serial port or LCD, you must redefine the functions related to the output device called in the standard library function. 

For example: if printf is output to the serial port, the output in fputc needs to be directed to the serial port (redirection), the method is as follows: 


#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 //Macro replacement is equivalent to int fputc(int ch,FILE *f)

 /* Place your implementation of fputc here */ 
 /* eg 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; 


Because functions such as printf() use semihost mode, using the standard library will cause the program to fail to run. Here are the solutions: 

Method 1: Use the micro library, because if you use the micro library, the semi-host mode will not be used. 

Method 2. Still using the standard library, add the following code to the main program: 


#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; 

If you are using MDK, please check "Use MicroLIB" in "Target"->"Code Generation" of the project properties; I referred to the forum today and found that using microlibrary can solve this problem well.

2. Another method: (actually similar)   

You need to add the following code  

(There should be a complete forum post introducing this, but I couldn't find it. Maybe it's been buried.) 
#pragma import(__use_no_semihosting)   
/**************************************************************************  
*Support functions required by the standard library   
**************************************************************************/  
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(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;  
}  
The role of semihosting is introduced as follows.  
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.  
 

As far as I understand, this mode is used for debugging. Through the emulator, the host's input and output are used instead of the MCU's own. That is to say, even if the MCU has no output port, printf can be sent to the computer. Conversely, since this mode changes the implementation of printf() and other functions, the input and output will not go through the MCU's peripherals, so redefining fputc alone will not work. 


After turning off this mode with code, you need to update the definitions of __stdout and __stdin at the same time, so there are the following statements. 


The above is only my personal understanding. Please correct me if there are any errors. 


In addition, after checking microlib, the files for enabling semihosting may not be included during compilation, so it’s okay.


C library function redirection: 

Users can define their own C library functions, and the connector automatically uses these new functions when connecting. This process is called redirecting C library functions, as shown in the figure below. 

For example, the user has an I/O device (such as UART). Originally, the library function fputc() outputs characters to the debugger control window, but the user changes the output device to the UART port. In this way, the output of all printf() series functions based on the fputc() function is redirected to the UART port.

Here is an example of implementing fputc() redirection: 


externvoidsendchar(char*ch); 
intfputc(intch,FILE*f) 
{/*egwriteacharactertoanUART*/ 
chartempch=ch; 
sendchar(&tempch); 
returnch; 


This example simply redirects the input characters to another function sendchar(), which is assumed to be a serial port output function defined separately. Here, fputc() acts as an abstraction layer between the target hardware and the standard C library function.


Keywords:stm32 Reference address:Question about using printf function in stm32

Previous article:About STM32 serial port printf output debugging information problem
Next article:STM32 multiple serial ports share printf to print serial port data

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号