/* __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
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- 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
- The idea of implementing crawlers
- Design and implementation of temperature controller based on single chip microcomputer and fuzzy control
- I'm moving, so I'm selling some nearly new boards.
- Lichee RV 86 PANEL Review (2)——System Burning and Application
- Non-isolated 'mains' voltage acquisition circuit
- [GD32L233C-START Review] 1. GD32L233C-START with obvious advantages and disadvantages (unboxing)
- LTC2325-16 Sampling Issues
- Design considerations for single-chip microcomputer key scanning program
- Free application: Domestic FPGA Gaoyunjia Little Bee Family GW1N Series Development Board
- Taking stock of the college entrance examination experience of Internet tycoons! How many points did you get in the exam?