STM32 about printf redirection to serial port

Publisher:BlossomBeautyLatest update time:2018-10-05 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Simply put: 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 achieve output on 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 outputs to the serial port, you need to point the output in fputc to the serial port (redirection). The method is as follows: 
#ifdef __GNUC__ 
 
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch) 
#else 
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) 
#endif  
PUTCHAR_PROTOTYPE 

  
  
 USART_SendData(USART1, (uint8_t) ch); 
  
 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 microlibrary, because if you use the microlibrary, the semihost mode will not be used. 
Method 2. Still use the standard library and add the following code to the main program: 
#pragma import(__use_no_semihosting)  
_sys_exit(int x)  
{  
x = x;  
}  
struct __FILE  
{  
int handle;  
 
 
 
};  
 
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 the microlibrary can solve this problem well. 
2. Another method: (actually similar)   
You need to add the following code  
(there should be a post in the forum that fully introduces this, but I didn't find it, maybe it's sunk.) 
#pragma import(__use_no_semihosting)   
  
struct __FILE   
{   
int handle;   
  
  
  
};   
  
FILE __stdout;  
///   
/// Define _sys_exit() to avoid using semihost mode   
///   
///   
///   
_sys_exit(int x)    { 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. s   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.  
 According to my understanding, this mode is used for debugging. Through the emulator, the host's input and output are used instead of the microcontroller's own. That is to say, even if the microcontroller has no output port, printf can be printed to the computer. Conversely, since this mode changes the implementation of printf() and other functions, the input and output will not go through the microcontroller's peripherals, so redefining fputc 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 semihosting-enabled files may not be included in the compilation, so it's okay.
C library function redirection: 
users can define their own C language library functions, and the connector automatically uses these new function functions when connecting. This process is called redirecting C language 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. 
The following is an example of implementing fputc() redirection: 
externvoidsendchar(char*ch); 
intfputc(intch,FILE*f) 

chartempch=ch; 
sendchar(&tempch); 
returnch; 

This example simply redirects the input characters to another function sendchar(), which is assumed to be a separately defined serial port output function. Here, fputc() seems to be an abstraction layer between the target hardware and the standard C library function.
 
The second question, path: D:\Keil3.80\ARM\Examples\ST\STM32F10xFWLib\Examples

Keywords:STM32 Reference address:STM32 about printf redirection to serial port

Previous article:Implementing incremental PID on STM32
Next article:STM32 standard library is compiled into lib library file

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号