Need for callback functions

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all, i don't know what's the exact need for using callback functions ?
what i am thinking is if i am using RTOS in my application then to access particular API i have to use
callback functions.. is it right?

please help me on this,,...
thanks,
Prabu.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I would descrinbe it the other way around: Sometimes not only do YOU need to talk to an API, but the API needs to talk to YOU. One way to accomplish this is to submit a function pointer to the API, essentially saying "Hey, API, when you need to talk to me call this function". The API calls then calls your function when it needs to, hence "callback function".

One such scenario would be a RTOS-like environment, where you tell the scheduling API what processes to run by passing to it the funtion pointers to the functions that execute the different tasks. The RTOS-like environment then calls those functions in turns, maybe just round-robin, maybe on a priority basis to get tasks executed.

If you want to learn more about "callback functions" and the technicalities, the obvious starting point is the concept of function pointers in "C".

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Callbacks differentiate whether a task service is performed synchronously or asynchronously.

A specific example in one of my projects is IDE disk access. A task needs to get some sectors read from a disk so it posts a message to a queue owned by the ATA/IDE task to say "please read N sectors from LBA M onwards to a memory buffer at XXXXX". In the message packet it has the option of passing the address of a callback function or just NULL if it doesn't want to use the mechanism.

Now assuming the task is happy to wait on completion of the disk read (possibly many milliseconds later) it will set the callback to NULL and then after posting the message it will probably block on the acquisition of semaphore from the ATA/IDE task that signals completion. But that task is suspended during all the waiting time because of this.

If, however, it needs to get on and do other stuff while the disk data is being DMAd to the buffer it will fill in the callback function address. Now it posts the message to the Q but does not block on a Sem4 but can get on and do other work. Finally, when the ATA/IDE wants to signal completion it calls the callback function (obviously in the context of the ATA/IDE task and not the caller task) which maybe sets a flag or something to let the caller know that the operation completed which it can then act on in it's own thread context.

When no callback is used the transaction is synchronous whereas when a callback is used it is asynchronous.

But I do have to wonder what all this has to do with AVR8 micros? Unless you are programming a really big task on one of the biggest devices (128/256) I don't see the justification for using an RTOS at all.

And if you are using an RTOS I'd kind of hope that it would be well documented and all this kind of stuff would be explained in its manual. (otherwise look for a "better" RTOS!)

PS Oh and nothing in either this thread or the one you started about variants of C has anything specific to do with GCC - so they should have been posted in AVR Forum (or uncle Bob will get miffed!)