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.
Previous article:Detailed analysis of STM32 startup files
Next article:Question about using printf function in stm32
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Wireless Security
- Make the needles on the car instrument cluster move
- Why is the error reported when opening the GD official website routine in keil5?
- One article to understand the RF chip industry chain and its domestic status!
- Super detailed teaching you how to use HFSS to design and simulate inverted F antenna 1
- How to implement the function?
- Configuration and use of TMS320F28379D SCID SCIB
- 40 Practical Knowledge Summary of Analog Circuits
- Research on the application of white light LED driving based on electric field induced power amplifier
- About DSP clock and PLL issues