Skip to content

System Hooks

FreeRTOS can call user functions at specific kernel events:

  • vApplicationIdleHook() — CPU has no runnable tasks
  • vApplicationTickHook() — every system tick interrupt
  • vApplicationMallocFailedHook() — heap allocation failed
  • vApplicationStackOverflowHook() — a task overflowed its stack

Enable hooks in FreeRTOSConfig.h:

#define configUSE_IDLE_HOOK 1
#define configUSE_TICK_HOOK 1
#define configUSE_MALLOC_FAILED_HOOK 1
#define configCHECK_FOR_STACK_OVERFLOW 2
#define configUSE_TRACE_FACILITY 1 // useful for diagnostics

Runs when no task is ready to run. Keep it short and non-blocking.

void vApplicationIdleHook(void){
// Optionally enter low power, blink a LED slowly, or run background checks
// Never block here. Do minimal work.
}

Called on each tick interrupt. Keep it extremely short; use only ...FromISR APIs.

void vApplicationTickHook(void){
// DO: increment counters; very small work
// DO NOT: call blocking APIs or heavy code
}

Called if a pvPortMalloc() fails (e.g., not enough heap for creating tasks/queues).

void vApplicationMallocFailedHook(void){
// Stop safely and aid debugging
taskDISABLE_INTERRUPTS();
for(;;){}
}

Checklist when this triggers:

  • Increase configTOTAL_HEAP_SIZE or reduce allocations
  • Check task stack sizes (don’t over-allocate)
  • Ensure the chosen heap implementation (e.g., heap_4.c) is linked

Detects task stack overflow (method depends on configCHECK_FOR_STACK_OVERFLOW).

void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName){
(void)xTask; (void)pcTaskName;
taskDISABLE_INTERRUPTS();
for(;;){}
}

How to avoid overflows:

  • Use uxTaskGetStackHighWaterMark() during development
  • Avoid large stack arrays; use static/global or heap when appropriate
  • Keep ISRs short; do heavy work in tasks