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
Previous article:Implementing incremental PID on STM32
Next article:STM32 standard library is compiled into lib library file
- 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
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- XL6019 output is not normal, please have a look
- Baidu really only cares about money but not shame
- 51 series connected to sht 30, can't read data just after power on
- x-nucleo-iks01a3 related information
- 【Home Electronic Guard】Work Submission
- Machine Vision Inspection
- Research on JPEG2000 Core Algorithm and Its DSP Implementation
- Why are relays still used to drive automotive motors?
- [AIoT Smart Smoke Detection System] Product Design Background
- Simulation research on high frequency antenna protection design