hello everyone , I am boundless .

Let's talk about chips today / Written by module manufacturer SDK A technology that must be used : Callback function .

The knowledge point of callback function is not very difficult , Is it difficult? Many people who explain callback functions on the Internet are too academic , Not close to the people at all .

Even if many people know how to write callback functions, they simply don't understand their use in actual products , When .

So in this lesson, we will take the requirements of program architecture as the starting point , Explain how the callback function meets this requirement .

In order to facilitate everyone's understanding , This article also corresponds to an article , You can find infinite MCU programming to obtain .

one , Through this course, you can master the following knowledge :

* Master the core concepts or requirements of program architecture .
* Master the function of callback function
* Master the programming of callback function
* Master the application of callback function in products

two , Core concepts and requirements of program architecture

Many people may say a good program architecture , The code is very compact , The implementation efficiency is also high .

In fact, this is very one-sided , Not exactly , This only shows that your program algorithm is well written , But the architecture is not necessarily good .

That is, architecture , That's naturally from ” overall situation ” For heavy , Thinking cannot be limited to the current product functions , We should also consider the increase and clipping of functions in the future , So for MCU development ,
I think a good program architecture should at least meet the following requirements :

The program code of hardware layer and application layer is separated , Interface for control and communication between each other , And will not share global variables or arrays .

Here , That's all I ask , Don't underestimate this requirement , Because this requirement contains a lot of knowledge , For example, it is called portability , Scalability .

So let's imagine the way we usually write MCU code , stay 51 Basically one at the time .c File resolution , Including register configuration , Product function .

This is a program without architecture , Then we evolved to STM32 After this MCU , The program is big , Slowly, several folders and directories will be added to the project files to separate the hardware layer and application layer code .

So we will put some different peripheral functions , such as Led, Key , Serial port and other peripheral function codes are written in different languages .c In the document , Then use the function interface to call it .

Let's say controlling a LED Light on , Directly in led.c Write a driver in the file led The function of light status and then call it externally .

Let's look at this Led The control function of does meet the needs of program architecture , Hardware layer and application layer code are separated , The application layer is controlled by the interface provided by the hardware layer , And there will not be all variables or arrays shared by the hardware layer and the application layer . Is it easy like this ?

Then I wonder if you have encountered another situation , That is, the application needs to collect data from the hardware layer , For example, the serial port receives data , Key acquisition ,ADC Value acquisition .

How can I inform the application layer to get the data from the hardware layer , Or how to offer it ?

Is the simplest and crudest way we used to use a global variable , For example, the serial port of the hardware layer has received data , So we throw the data into the array , Then mark the position of the received global variable 1.

For example, the global variable name is RcvFlag, The application layer then polls for judgment RcvFlag==1? If yes, start to take out and analyze the data in the array .

Many people will say , You see, I can still realize the function in this way , Why learn other architectures .

This can certainly achieve the function , However, there will be the problem of poor portability .

For example, your boss asked you to package the hardware layer of the serial port for customers , But you can't let customers see the source code of your implementation , Interface only ( Function name ) For each other .

So at this time, do you want to tell the customer which variable is 1, Then take the data of which array LOW What's your practice ?

So if you are a knowledgeable customer, you will doubt whether your company's technical strength is up to the standard .

What can be done to be both convenient and professional ? Here we need to use the callback function .

three , Role of callback function

So before talking about callback functions , For function calls, I generally divide them into 2 Type :

1. Output type

I don't know if you've used it C Some library functions of the language , for instance sizeof() Function to get data length ,memcpy() Is a memory copy function , After we call this function, we can complete the corresponding functions .

There are some program functions based on MCU , Let's say control LED On off , Relay on / off ,LCD Drive, etc .

What about these , I generally call it an output function .

We play a leading role in output functions , We know when to call it .

2. Input type

What about input type , Also known as a responsive function .

What is a responsive function ?

For example, receive data from the serial port , We don't know when the data will come .

For example , The function of our key detection , We don't know when to press the button , Then these should be defined as responsive functions , The responsive function can be implemented by callback function .

So through these two types of analysis , We can know , Callback functions are basically used in input processing .

For example, serial port data reception , Then the data is input into the MCU , MCU is in the role of slave .

Key detection , The key state is input into the single chip microcomputer .

For example ADC Value acquisition ,ADC The value is also input into the MCU .

Then the time nodes they input are unknown , These can be handled with callback functions .

How to deal with it? We will give you examples with code later .

Callback functions also serve to encapsulate code .

For example, manufacturers of chips or modules , We take the typical STM32 For example , Like external interrupt , timer , Interrupt functions such as serial port are callback functions , The purpose of this function is to pass the collected data to the user , Or application layer .

So the core function of callback function is :

1. Take the data from a .c Transfer file to another .c file , Instead of sharing data with global variables LOW Method of .

2. For this data transfer method , Callback functions are more conducive to code encapsulation .

four , Master the programming of callback function

I said a lot of conceptual things earlier , It may be difficult for us to understand , Callback functions are ultimately implemented by function pointers .

So I'll use some examples of simulated keys to demonstrate how to deal with them by calling functions .

Here's our c-free engineering , Use this to simulate convenience :

From the idea of modular programming , The whole project is divided into 2 Parts , application layer main.c file , Hardware layer key.c and key.h file .

No matter how complicated the program is , We all have to start with main Functions dig down step by step ,main The function code is as follows .
int main(int argc, char *argv[]) { KeyInit();
KeyScanCBSRegister(KeyScanHandle); KeyPoll(); return 0; }
KeyInit(); yes key.c Key initialization function of file

KeyScanCBSRegister(KeyScanHandle); yes key.c Register function with function pointer .

This function may be a little confused , Please follow our rhythm , Let's start brain burning , It is also a necessary step to write callback functions ,

To understand this callback function, register the function , We need to start with the hardware layer (key.h) Function pointer definition of header file , See the figure below for details .

A function pointer type is customized here , With two formal parameters .

then , We are key.c This file defines a function pointer variable .

Here's the point , We are through this function pointer , Function address pointing to the application layer ( Function name ).

How to realize pointing ? Is to register functions through function pointers .

This function is in main Call in function , Using this registration function is also very flexible , Where do you want to be .c Where the file uses the key function is called .

Pay attention here ,main.c This file defines a function to receive the hardware layer (key.c) Incoming data .

The definition here is not random , Be sure to match the return value of the custom function pointer type , Formal parameter consistency .

Then copy the function name directly to KeyScanCBSRegister The formal parameters of the function are OK .

After this call , We key.c Document pKeyScanCBS This pointer is actually pointing to KeyScanHandle function .

That is, execution pKeyScanCBS When , It's execution KeyScanHandle function .

The specific function of detecting keys is KeyPoll function , This is main Call in function .

When keyboard input is detected , Eventually called pKeyScanCBS.

The final implementation is main.c Document KeyScanHandle function .

therefore , Let's look at the output .

If it's still a little fuzzy , Next, I'll give you a look at the process of writing and using callback functions :

* Custom function pointer , Formal parameters are the data to be transmitted to the application layer as the hardware layer .
* The hardware layer defines a function pointer and a function pointer registration function .
* The application layer defines a function , The return value and formal parameter should be consistent with the function pointer .
* Application layer call function pointer registration function , Pass in the defined function name as a formal parameter .
Ok, This is the use of callback functions .

If you still don't understand it, it is recommended to read it twice more .

Now please think about it , This program is simple , But the architecture is not bad ? The application layer and hardware layer are completely independent ?

Technology
©2019-2020 Toolsou All rights reserved,
【Java8 New features 1】Lambda Expression summary What is a process , Concept of process ?hdfs dfs Common basic commands java When creating objects, you must _Java Basic test questions Generation of random numbers + Figure guessing game When will we enter the old generation ?HDFS Common commands ( summary ) It is never recommended to spend a lot of time studying for employment pythonmacOS Big Sur The installation could not be completed Big Sur Why can't I install it ?Python pyttsx3| Text reading ( Various languages )