当前位置:网站首页>Embedded development: embedded foundation callback function
Embedded development: embedded foundation callback function
2022-06-27 13:11:00 【Guangdong embedded Education】
Callback function is a basic and often critical concept for developers to create drivers or custom libraries . A callback function is a reference to executable code , It is passed as a parameter to other code , Allow lower level software layers to call functions defined in higher level layers (10). Callbacks allow driver or library embedded developers to specify behavior at a lower level , But leave the implementation definition to the application layer .
The simplest callback function is just a function pointer , It's passed as an argument to another function . in the majority of cases , The callback will consist of three parts :
• Callback function
• Callback registration
• Callback execution
The following figure shows how these three parts work together in a typical callback implementation .

First , Developers create libraries or modules that will have implementation elements determined by the application developer . for example , The developer created a GPIO The driver , The driver has an interrupt service routine , The code is specified by the application developer . Interrupts can handle button presses or other functions . The driver doesn't care about functionality , It is only concerned that at runtime it knows what function should be called when an interrupt is triggered . The code that calls a callback function in a module is often called a signal handler .
Next , There needs to be some way to tell the underlying code what functions to execute . There are many ways to do this , But for driver modules , The recommended practice is to create a function in the module that is specifically used to register the function as a callback . Having a separate function to register a callback function makes it clear to embedded developers that the callback function is being registered to a specific signal handler . When a register function is called , Pass the called required function as a parameter to the module and store the function address .
Last , Application developers write their applications , This includes the creation of callbacks and the implementation of initialization code , This code registers the function in the library or module . When executing an application , Low level code stores the callback function address , When you need to perform a function , It dereferences the callback function and executes it .
There are two main examples that developers can consider using callbacks . First , In the driver , The developer will not know how the final application might need to use any interrupt service program . If the developer is creating libraries for some microcontroller peripherals , You can use callbacks to specify all interrupt behaviors . Using callbacks will allow developers to ensure that if the application developer does not register a custom callback function , Each interrupt has a default service routine . When callbacks are used with interrupts , Developers need to remember , Need to follow best practices for interruptions .

secondly , As long as there are common behaviors in the application that may have implementation specific behaviors , You can use callbacks . for example , Initializing arrays is a very common task , Need to execute in the application . If for some applications , Embedded developers want to initialize array elements to all zeros , In another application, they want to initialize array elements to random numbers ? under these circumstances , They can use callbacks to initialize arrays .
ArrayInit Function accepts a pointer to an array with element size , Then it takes a pointer to the function that returns an integer . The function at this time has not been defined , But it can be defined by the application code . When calling ArrayInit when , Developers will pass any function they choose to initialize array elements .
Function with callback :
void ArrayInit(int * Array, size_t size, int (*Function)(void))
{
for(size_t i = 0; i < size; i++)
{
Array[i] = Function();
}
}
Initialize element to 0:
int Zeros(void)
{
return 0;
}
Initialize the element to a random number :
int Random(void)
{
return rand();
}
function Zeros or Random Be passed on to ArrayInit, It depends on how the application embedded developer wants to initialize the array .
边栏推荐
猜你喜欢
随机推荐
【Acwing】第57场周赛 题解
Database Series: MySQL index optimization and performance improvement summary (comprehensive version)
Hardware development notes (VII): basic process of hardware development, making a USB to RS232 module (VI): creating 0603 package and associating principle graphic devices
Review summary of database
The world's fastest download tool XDM
防火墙基础之华为华三防火墙web页面登录
[medical segmentation] unet3+
LeetCode_ Fast power_ Recursion_ Medium_ 50.Pow(x, n)
每日刷题记录 (六)
Centos7 command line installation Oracle11g
Today's sleep quality record 78 points
手把手教你搭一个永久运行的个人服务器!
隐私计算FATE-离线预测
VS调试技巧
JSON.stringify用法
Intranet learning notes (8)
推荐系统的下一步?阿里时空聚合GNN,效果吊打LightGCN!
与生活握手言和
What is the next step in the recommendation system? Alispacetime aggregates GNN, and the effect is to sling lightgcn!
Differences in perspectives of thinking








