freertos如何确定任务堆栈大小(已找到相关检测堆栈溢出方法,分享给大家)
各位有谁知道freertos如何确定任务堆栈大小?非常感谢! 提示一点,FREERTOS没有管理中断,所以要额外的考虑中断用到的堆栈。 回复【1楼】zhonghua_li 蓝色天空-----------------------------------------------------------------------
具体如何确定堆栈大小,有没有具体的方法么? Stack Usage and Stack Overflow Checking
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Stack Usage
Each task maintains its own stack. The memory used by the task stack is allocated
automatically when the task is created, and dimensioned by a parameter passed to
the xTaskCreate() API function. Stack overflow is a very common cause of application
instability. FreeRTOS.org therefore provides two optional mechanisms that can be
used to assist in the detection and correction of just such an occurrence. The option
used is configured using the configCHECK_FOR_STACK_OVERFLOW configuration constant.
Note that these options are only available on architectures where the memory map is
not segmented and the stack grows down from high memory. Also, some processors could
generate a fault or exception in response to a stack corruption before the kernel
overflow check can occur. The application must provide a stack overflow hook function
if configCHECK_FOR_STACK_OVERFLOW is not set to 0. The hook function must be called
vApplicationStackOverflowHook(), and have the prototype below:
void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed portCHAR *pcTaskName );
The pxTask and pcTaskName parameters pass to the hook function the handle and name
of the offending task respectively. Note however, depending on the severity of the
overflow, these parameters could themselves be corrupted.
Stack overflow checking introduces a context switch overhead so its use is only
recommended during the development or testing phases.
Stack Overflow Detection - Method 1
It is likely that the stack will reach its greatest (deepest) value after the kernel
has swapped the task out of the Running state (when the stack contains the task context).
At this point the kernel can check that the processor stack pointer remains within
the valid stack space. The stack overflow hook function is called should the stack
pointer contain an invalid value.
This method is quick but not guaranteed to catch all stack overflows. To use this
method only set configCHECK_FOR_STACK_OVERFLOW to 1.
Stack Overflow Detection - Method 2
When a task is first created its stack is filled with a known value. When swapping a
task out of the Running state the kernel can check the last 16 bytes within the valid
stack range to ensure that these known values have not been overwritten by the task
or interrupt activity. The stack overflow hook function is called should any of
these 16 bytes not remain at their initial value.
This method is less efficient than method one, but still fairly fast. It is very
likely to catch stack overflows but is still not guaranteed to catch all overflows.
To use this method in combination with method 1 set configCHECK_FOR_STACK_OVERFLOW
to 2. It is not possible to use only this method. mark 如果是RT-Thread那么就简单得多了,在finsh shell下输入命令:
list_thread()
会显示如下结果:
threadpristatus sp stack size max used left tickerror
---------- ------ ---------- ---------- ---------- ---------- -----
tidle 0xff ready0x00000074 0x00000100 0x00000074 0x0000003b 000
tshell0x14 ready0x0000024c 0x00000800 0x00000418 0x00000064 000
其中:
thread字段表示线程的名称
pri字段表示线程的优先级
status字段表示线程当前的状态
sp字段表示线程当前的栈位置
stack size字段表示线程的栈大小
max used字段表示线程历史上使用的最大栈位置
left tick字段表示线程剩余的运行节拍数
error字段表示线程的错误号
所以,可以开始的时候设置一个比较大的栈,然后系统运行一段时候后,再按照max used指示的值来调整栈大小即可(至少应该是max userd + 32字节)。 mark 怎么还是没有结果啊?不是可以看编译文件。 mark,学习了 这个有需要,正在学习中。多谢分享。 这个有需要,正在学习中。多谢分享。
页:
[1]