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 output to 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 is output to the serial port, the output in fputc needs to be directed to the serial port (redirection), the method is as follows:
#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 //Macro replacement is equivalent to int fputc(int ch,FILE *f)
{
/* Place your implementation of 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 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 micro library, because if you use the micro library, the semi-host mode will not be used.
Method 2. Still using the standard library, 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 microlibrary can solve this problem well.
2. Another method: (actually similar)
You need to add the following code
(There should be a complete forum post introducing this, but I couldn't find it. Maybe it's been buried.)
#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;
///
///
///
_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 library.
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 MCU's own. That is to say, even if the MCU has no output port, printf can be sent to the computer. Conversely, since this mode changes the implementation of printf() and other functions, the input and output will not go through the MCU's peripherals, so redefining fputc alone 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 connector automatically uses these new functions when connecting. 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). 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.
Here 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 serial port output function defined separately. Here, fputc() acts as an abstraction layer between the target hardware and the standard C library function.
Previous article:About STM32 serial port printf output debugging information problem
Next article:STM32 multiple serial ports share printf to print serial port data
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Circuit Analysis
- Tms320VC5502 and isp1581 high-speed data acquisition solution
- 2019FPGA Employment Competition and Guidance
- What kind of activities do you hope the RF/Radio Frequency section will carry out? Please leave a message in the thread.
- Oscilloscope usage tips you don't know
- Free Trial | Keysight Accelerates Signal Integrity and Power Integrity Testing
- Learning PLC technology is super easy
- Advantages of modules
- EEWORLD University Hall----Live Replay: What to listen to when stuck in traffic? New generation of in-car audio systems and software-defined cars
- Beautiful Pico Quad Keyboard