Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training


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 ]    [ Sitemap ]    [ ]

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

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

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

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

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists