About STM32 serial port printf output debugging information problem

Publisher:不羁少年Latest update time:2019-01-17 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Problems encountered (using HAL library)


In the process of using STM32, we usually use printf to redirect the serial port to output debugging information for program development and debugging. We found the redirection code part on the Internet and added it to the serial port code file, as follows:


UART_HandleTypeDef husart_printf;


#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 USART2 and Loop until the end of transmission */

    HAL_UART_Transmit(&husart_printf, (uint8_t *)&ch, 1, 0xFFFF);

  return ch;

}


After the program is edited, I thought I could print out the message directly from the serial port, but after the program is downloaded, no information is printed to the serial port. This is strange. Is the redirection part above wrong?


2. Find the problem 


I searched online again and found a mode called semihost mode. Semihost is a mechanism that allows the code running on the ARM target to use the input and output devices of the host computer if the host computer is running a debugger/emulator. That is, the code can directly use the host computer's peripherals to implement input and output debugging, instead of using the peripheral input and output devices of the ARM target device. In short, semihosting requires the emulator to input and output debugging information. Since we are using the serial port peripherals of the microcontroller, that is, not using the emulator, we must disable semihost mode.


3. Solve the problem


From the above understanding, we know that functions like printf() use the semihost mode. Now we only need to not use printf in semihost mode to solve the problem.


Solution 1: 


Use MicroLIB, a miniature version of the C99 standard library, referred to as microlibrary. Using microlibrary will not use semi-host mode, so it can run normally and print debugging information. When programming with keil5, just check Use MicroLIB in the configuration. You can search for detailed information about MicroLIB yourself.


Solution 2: 


To use the standard library but disable semihost mode, just add the following code after the main program:


#pragma import(__use_no_semihosting) 

int _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;


__use_no_semihosting_swi, that is, do not use semihosting mode, 


Because there is no underlying implementation of these functions in the program, the device will enter the software interrupt BAEB when it is running. At this time, the declaration __use_no_semihosting_swi is needed to prevent the program from stopping at this interrupt when encountering these file operation functions.

Keywords:STM32 Reference address:About STM32 serial port printf output debugging information problem

Previous article:Detailed analysis of STM32 startup files
Next article:Question about using printf function in stm32

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号