hook functions

umnurw wrote on Wednesday, January 26, 2011:


I need to know about the hook functions in FreeRTOS kernel which are called whenever a scheduling decision is made.
I have used traceTASK_SWITCHED_OUT, and traceTASK_SWITCHED_IN macros, but they work with tasks, not at every context switch.
Can someone provide a little information or any example to use them?


edwards3 wrote on Wednesday, January 26, 2011:

In FreeRTOSConfig.h:

For an idle hook set configUSE_IDLE_HOOK to 1, then in your application write a function
void vApplicationIdleHook( void );

For a tick hook set configUSE_TICK_HOOK to 1, then in your application write a function
void vApplicationTickHook( void );

Those are mentioned on http://www.freertos.org/a00016.html

Also, for a malloc() failed hook set configUSE_MALLOC_FAILED_HOOK to 1, then in your application write a function
void vApplicationMallocFailedHook( void ); /* used for heap_1.c and heap_2.c and heap_3.c only */

Is there a link for the malloc failed thing?

For stack overflow hook set configCHECK_FOR_STACK_OVERFLOW to 1 or 2, and provide a function
void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName );

see http://www.freertos.org/Stacks-and-stack-overflow-checking.html

umnurw wrote on Thursday, January 27, 2011:

thanks a lot for a detailed answer.

tick hook is called during each tick interrupt by the kernel.
I need a hook function that is called at every context switch (whenever a scheduling decision is made).
Is there any hook function to fulfill this requirement?

best regards.

rtel wrote on Thursday, January 27, 2011:

You can do this using the traceTASK_SWITCHED_OUT and traceTASK_SWITCHED_IN macros.  The sections I’m currently adding to the tutorial books actually contain an example that show you how to do just that. 

These macros run in the context of the tasks.c file, so have access to the pxCurrentTCB variable.  Therefore, what you do is something along the lines of:

xTaskHandle xTaskThatWasRunning;

#define traceTASK_SWITCHED_OUT xTaskThatWasRunning = pxCurrentTCB

#define traceTASK_SWITCHED_IN \
____if( xTaskThatWasRunning != pxCurrentTCB ) \
____{ \
________/* A context switch has occurred, do something. */ \