From Silicon Labs: "Detecting Task Stack Overflows Through the Kernel's Red Zone Feature"

Dec 11, 2018

The following blog explains a simple way of detecting task stack overflows while debugging your Micrium OS application.

Micrium OS Kernel Red Zone

The kernel's red zone is a feature that, when enabled through OS_CFG_TASK_STK_REDZONE_EN in os_cfg.hcreates a monitored zone at the end of a task's stack. The length of the red zone is user-configurable via the #define OS_CFG_TASK_STK_REDZONE_DEPTH in os_cfg.h. By default, this is set to 8 stack elements (CPU_STK). 

When the red zone is enabled, every time a task is switched out, either at the task level or at the interrupt level, the kernel checks if the red zone has been hit. By default, a software exception is thrown with the use of the CPU_SW_EXCEPTION macro in the ARMv7m port. However, if you want more control over what your application should do in the event that the red zone is hit, you can turn on the application hooks by setting OS_CFG_APP_HOOKS_EN to DEF_ENABLED in os_cfg.h.

Using the Red Zone Application Hook

If you decided that you want your application to determine what to do in the event that a task hits the red zone instead of throwing a software exception, then you will have to define a hook function to do so.

A simple example:

static  void  RedzoneHitHook (OS_TCB  *p_tcb)


With that, you can tell that if you see LED0 permanently on and with your code trapped at the while loop you have hit the red zone. You can then check p_tcp->NamePtr in your local variables panel and find what was the task that hit the limit.

Don't forget to assign the red zone hit hook pointer to your custom function. Using the example above:

OS_AppRedzoneHitHookPtr = RedzoneHitHook;


Make sure that this is added prior to calling OSStart() otherwise the pointer is never assigned to your application hook.

For more details about detecting stack overflows and the red zone, visit the following link:




Looking to integrate Silicon Labs products with your design? Our Applications Engineers offer free design and technical help for your latest designs. Contact us today!



Symmetry Electronics Logo


Why partner with Symmetry Electronics? Symmetry's technical staff is specially trained by our suppliers to provide a comprehensive level of technical support. Our in-house Applications Engineers provide free design services to help customers early in the design cycle, providing solutions to save them time, money and frustration. Contact Symmetry for more information.


Sign up to get tech news, product updates and store promos from Symmetry Electronics.


Blog Archive


Previously Viewed Products