FreeRTOS Timer Callback

witc wrote on Tuesday, February 20, 2018:


I want to use a FreeRTOS timer (on STM32L071), but I have never found the answer for my simple question.

Can I use a callback function without the input parameters TimerHandle_t xTimer?

I want something like this:
void vTimerCallback( void )
… do semething

The reason why I want to use it in this way is, that I want to use my callback also as simple function which I can call from anywhere.

Hope my question is clear. Thank you for answers.

rtel wrote on Tuesday, February 20, 2018:

The prototype of the function used as a timer callback must have the parameter, but that doesn’t mean the parameter has to be used inside the function - it can just be ignored. If you call the function directly just pass I’m NULL.

richard_damon wrote on Tuesday, February 20, 2018:

Or the other way to do it is to wrap your function that takes no parameters with an actual call back function that take a parameter, and have that call your function.

It is also possible on many implementations to cast the type of the function to the needed type and it will just work, as long as the systems calling convention doesn’t require the callee to clean up the parameter from the stack. This of course if very implementation dependent and not and not recommended unless you are really tight on timing, note that on machines where this works, the wrapper function can normally be reduced to a single jump (which even that can possibly be eliminated) with optimizations turned on.