What is Callback function in MPLAB Harmony

In MPLAB Harmony callback functions are used frequently. But what is callback function? This article will help you understand what callback functions are with MPLAB Harmony examples.

What is Callback function in MPLAB Harmony? Callback functions are client functions that are called by drivers. In other words, callback functions are user created functions that are called by the driver whenever an event occurs in the driver.

In MPLAB Harmony, peripherals such as USART, SPI, I2S, USB etc are used by client application by interacting with their drivers via functions. Normally, when we wish to use these drivers we use the provided functions such as DRV_USART_Open() or DRV_I2S_Write(). These are client functions to interact with the drivers. The Callback functions are also client functions. However they are functions called by the driver. The following picture illustrates this concept.

What is Callback function?

Drivers that support callback function have functions to register the callback function. For example the timer driver has function DRV_TMR_AlarmRegister() to register a callback function. Let’s call the callback function myTmrCallback. Then using the DRV_TMR_AlarmRegister() function we can register the myTmrCallback callback function. The following C program code illustrates this.

The meaning of this is that the client wants the driver to call the function myTmrCallback whenever some event happens with the driver. Also here we have passed the address of the callback function.

Now that we have registered a callback function with the driver, we have to implement the myTmrCallback function. That is, what to do when the driver calls this callback function myTmrCallback. For example, we can blink a LED to indicate alarm. The following C program code demonstrates implementation of callback function myTmrCallback().

Overall, a callback function is registered with a driver and used by client application to act on event called backed by the driver. In the above example, the timer driver is used as an example. A callback function called myTmrCallback is registered with the timer driver using the DRV_TMR_AlarmRegister driver function. When there is certain amount of period elapsed, the timer driver call backs the client callback function myTmrCallback and with the callback function the user can take appropriate action such as blinking a LED in the above example.

Learning what callback functions are and how they works is essential if you want to use the MPLAB Harmony framework for your embedded system application. Drivers and callback functions are used frequently in harmony such as with USB drivers, I2S drivers, USART drivers, timer driver etc. In cases where data transfer via buffer is used event handlers are used. For example, in I2S driver tutorial with MPLAB Harmony, in buffered I2S data transfer example, APP_BufferEventHandler() callback function is used.

What do you think?

31 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Leave a Reply

Your email address will not be published. Required fields are marked *

I2S driver tutorial with MPLAB Harmony

I2S driver tutorial with MPLAB Harmony

Passing Structure Char Pointer to Function in C programming language