Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


hook functions

Posted by umnrw on 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?


RE: hook functions

Posted by MEdwards on 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

RE: hook functions

Posted by umnrw on 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.

RE: hook functions

Posted by Richard on 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. */ \


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

Meet Richard Barry and learn what's new in the FreeRTOS project at Embedded World 2019

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers



STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner