STM32L0 Development Notes 10: Using the __weak modifier

Publisher:FreeSpirit123Latest update time:2018-09-12 Source: eefocusKeywords:STM32L0 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.


Keywords:STM32L0 Reference address:STM32L0 Development Notes 10: Using the __weak modifier

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

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号