Preface,
In the HAL library, many callback functions are preceded by the __weak modifier.
As the name implies, weak means "weak", so if the __weak modifier is added before the function name, we generally call this function a "weak function".
For a function with the __weak modifier, the user can redefine a function with the same name in the user file. When the compiler finally compiles, it will select the user-defined function. If the user does not redefine this function,
Then the compiler will execute the function declared with __weak and the compiler will not report an error.
for example:
We open the project template, find and open the file stm32f4xx_hal.c, which defines a function HAL_MspInit, which is defined as follows:
__weak void HAL_MspInit(void)
{
__IO uint32_t tmpreg = 0x00;
UNUSED(tmpreg);
}
It can be seen that the modifier __weak is added in front of the HAL_MspInit function. At the same time, the function HAL_Init is defined in the front of the file, and the function HAL_MspInit is called in the HAL_Init function.
[plain] view plain copy
HAL_StatusTypeDef HAL_Init(void)
{
...//Some code is omitted here
HAL_MspInit();
return HAL_OK;
}
If we do not redefine the HAL_MspInit() function elsewhere in the project, when the HAL_Init initialization function is executed, the HAL_MspInit function defined in the stm32f4xx_hal.c file will be executed by default, and this function does not have any control logic.
If the user redefines the function HAL_MspInit in the project, then after calling HAL_Init, the user-defined HAL_MspInit function will be executed instead of the default function defined in stm32f4xx_hal.c. In other words, on the surface, we see that the function HAL_MspInit is defined twice, but because one of the definitions is a weak function and uses the __weak modifier, the compiler will not report an error.
Summarize:
__weak is often used in callback functions. The advantage is that the system defines an empty callback function by default, ensuring that the compiler will not report an error. At the same time, if the user wants to define a user callback function, then only redefine it, without considering the problem of repeated function definition, which is very convenient to use. The __weak keyword is widely used in the HAL library.
Previous article:STM32L0 low power design 2: Use Keil and ST-Link to download low power programs
Next article:STM32L0 Development Notes 9: Manually add driver files
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
- Micro Python Web Framework: microdot
- EEWORLD University ---- Building Block DAC: System Thinking Method
- Single package six-channel digital isolator and IPM interface reference design for inverter
- [Silicon Labs Development Kit Review] + Building Simplicity Studio Development Environment
- Tiger Wish: Make a wish for 2022, usher in good luck in the new year, and take away the EEWorld New Year gift~
- Live broadcast tonight at 20:00 [Developing AI intelligent robots based on TI's newly released Robotics SDK] Admission at 19:30
- 【Short-term weather forecast system】v0.0.1 implementation
- Talk about the serial port RS485 interface of MSP430F149
- First release, unboxing post? All the boards I applied for were received~
- Various short-range wireless communication technologies