Detailed explanation of serial port redirection in stm32

Publisher:脑力舞者Latest update time:2017-09-07 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere


/* __GNUC__ */ PUTCHAR_PROTOTYPE { // bypass __GNUC__ int ch (int ch) #else #define __GNUC__ int fputc(int ch, FILE *f) #endif /* __GNUC__ */ PUTCHAR_PROTOTYPE { // bypass __GNUC__ int ch (int ch) #else #define __GNUC__ int fputc(int ch, FILE *f) #endif /* __GNUC__ */ PUTCHAR_PROTOTYPE { //  bypass
__GNUC__ int ch (int ch) #else #define __GNUC__ int 
fputc  (int ch, FILE *f) #endif /* __GNUC__ */ PUTCHAR_PROTOTYPE { // bypass __GNUC__ int ch (int ch) #else  #define __GNUC__ int fputc(int ch, FILE *f) 
#endif  /* __GNUC__ */  PUTCHAR_PROTOTYPE  {       // bypass __GNUC__ int ch 
(   int ch)  #else 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 of functions such as printf(), semihost mode is used. 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 semihosting 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;   /* 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 the microlibrary can solve this problem well.  2. Another method: (actually the same)    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 has sunk.)  #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;  















































///

   
/// Define _sys_exit() to avoid using semihost mode   
///
   
///    
///    
_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 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 microcontroller's own. That is to say, even if the microcontroller has no output port, it can printf to the computer. Conversely, due to this mode The implementation of printf() and other functions has been changed, so the input and output will not go through the microcontroller's peripherals, so just 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 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 linker will automatically use these new functions when linking. 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). The library function fputc() originally outputs characters to the debugger control window, but the user changes the output device to the UART port. In this way, all the printf() series functions based on the fputc() function are redirected to the UART port. 
The following 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 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:Detailed explanation of serial port redirection in stm32

Previous article:Serial port learning based on stm32f103zet6
Next article:Startup file for lighting LED based on stm32f103zet6

Recommended ReadingLatest update time:2024-11-16 16:35

STM32 Series No. 27 - Infrared Remote Control
Infrared coding classification: The infrared remote controller uses a single bus communication method. Commonly used infrared codes are: NEC Protocol PWM (Pulse Width Modulation) PPM (Pulse Position Modulation) of Philips RC-5 Protocol. NEC protocol features: 8-bit address and 8-bit instruction length Address an
[Microcontroller]
Uip transplantation in STM32 platform to establish UDP link
Data transmission is achieved by establishing a UDP connection on STM32. In the previous section, it was mentioned that the lightweight TCP/IP protocol stack Uip is used. To create a UDP connection in the Uip protocol, the following steps are required: The first step is to open the configuration items that support the
[Microcontroller]
How to find the HardFault_Handler problem in STM32
There are two main reasons why HardFault_Handler failure occurs in STM32: 1. Memory overflow or out-of-bounds access. This requires standardizing the code when writing the program yourself, and you need to slowly troubleshoot when you encounter it. 2. Stack overflow. Increase the size of the stack.   How to troublesho
[Microcontroller]
How to find the HardFault_Handler problem in STM32
stm32 teaches you how to compile LIB files
This is a trick to generate LIB. Maybe you will find some programs on the Internet that use this method to include STM32 Library files: Instead of a bunch of C like DX32 routines: Well, this tip is to teach you how to generate the .LIB file. First open the program, and you will see only the library file in
[Microcontroller]
stm32 teaches you how to compile LIB files
STM32 multiplexing clock is turned on
Why turn on the clock in the first place? Answer: Because you need to read and write registers! In STM32, you need to turn on the clock corresponding to the register to read and write registers . Then it is clear when the AFIO clock is turned on (this is true for all clocks): the AFIO clock is turned on when the "AFIO
[Microcontroller]
Application of STM32 in variable frequency pulsator washing machine
With the guidance of the national energy conservation and emission reduction policy and the improvement of people's awareness of energy conservation and environmental protection, frequency conversion technology has been widely used in white household appliances such as air conditioners, refrigerators, and washing mach
[Microcontroller]
Application of STM32 in variable frequency pulsator washing machine
STM32 byte alignment #pragma pack
1. Alignment principle min(sizeof(word ), 4) = 2, so it is 2-byte aligned, not 4-byte aligned as we thought. 1) Each member is aligned in its own way and minimizes its length; 2) The default alignment of a complex type (such as a structure) is the alignment of its longest member, so that the length can be minimi
[Microcontroller]
Use of assert_param() in STM32
In the STM32 firmware library and the provided routines, assert_param() can be seen everywhere. If you open the stm32f10x_conf.h file in any routine, you can see that assert_param is actually a macro definition; in the firmware library, its function is to detect whether the parameters passed to the function are valid
[Microcontroller]
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号