方法1.栈顶地址是否小于等于栈底
在FreeRTOSConfig.h中,配置
点击查看代码
#define configCHECK_FOR_STACK_OVERFLOW 1
在StackMacros.h文件中可以看到如下代码
点击查看代码
#if( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) )
/* Only the current stack state is to be checked. */
#define taskCHECK_FOR_STACK_OVERFLOW() \
{ \
/* Is the currently saved stack pointer within the stack limit? */ \
if( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack ) \
{ \
vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
} \
}
#endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
方法2.栈底的4个word的在任务创建赋值的初始值,是否被修改
在FreeRTOSConfig.h中,配置
点击查看代码
#define configCHECK_FOR_STACK_OVERFLOW 2
在StackMacros.h文件中可以看到如下代码
点击查看代码
#if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) )
#define taskCHECK_FOR_STACK_OVERFLOW() \
{ \
const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack; \
const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5; \
\
if( ( pulStack[ 0 ] != ulCheckValue ) || \
( pulStack[ 1 ] != ulCheckValue ) || \
( pulStack[ 2 ] != ulCheckValue ) || \
( pulStack[ 3 ] != ulCheckValue ) ) \
{ \
vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
} \
}
#endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
栈溢出处理函数,可在main.c中定义如下:
点击查看代码
void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName )
{
dbg_printf("Stack Overflow!!!\n");
while(1);
// 处理栈溢出的代码
// 例如:记录错误,重启任务等
}