Ahmed Abdalrahiem
Ahmed Abdalrahiem

Reputation: 49

What is the use of callback function in peripheral driver?

I am reading a book on writing embedded system peripheral drivers and the author wrote a C callback function which i really don't understand and want to know the use of it, is it related to interrupts handling? The function prototype is as following: void Spi_CallbackRegister(SpiCallback_t Function, TYPE (*CallbackFunction)(type));

Upvotes: 0

Views: 343

Answers (1)

Milind Deore
Milind Deore

Reputation: 3053

Short Answer: Callback function are function pointers with which you invoke them, when a specific event occurs.

Let us say you have an IO device like ethernet (MAC) device. Driver for such device will always be waiting for an events to occur, these events are either hardware or software interrupts, example:

  1. A new packet has arrived.
  2. A packet is going to dispatch.
  3. Initiate packet transfer from device FIFO to host memory (DMA).
  4. A protocol state has changed/etc.

To handle various interrupt(s) type you need a specific function for each of them, these functions are called Interrupt Service Routine(ISR) or Interrupt handler or a callback function. When an event is mapped to a function, it stores its pointer and thats why the term callback the function.

Upvotes: 3

Related Questions