OSTaskStkChk()
void OSTaskStkChk (OS_TCB *p_tcb,
CPU_STK_SIZE *p_free,
CPU_STK_SIZE *p_used,
OS_ERR *p_err)
File |
Called from |
Code enabled by |
os_task.c |
Task only |
OS_CFG_TASK_STAT_CHK_EN |
OSTaskStkChk() determines a task’s stack statistics. Specifically, it computes the amount of free stack space, as well as the amount of stack space used by the specified task. This function requires that the task be created with the OS_TASK_OPT_STK_CHK and OS_TASK_OPT_STK_CLR options.
Stack sizing is accomplished by walking from the bottom of the stack and counting the number of 0 entries on the stack until a non-zero value is found.
It is possible to not set the OS_TASK_OPT_STK_CLR when creating the task if the startup code clears all RAM, and tasks are not deleted (this reduces the execution time of OSTaskCreate()).
µC/OS-III’s statistic task calls OSTaskStkChk() for each task created and stores the results in each task’s OS_TCB so your application doesn’t need to call this function if the statistic task is enabled.
Arguments
p_tcb
is a pointer to the TCB of the task where the stack is being checked. A NULL pointer indicates that the user is checking the calling task’s stack.
p_free
is a pointer to a variable of type CPU_STK_SIZE and will contain the number of free CPU_STK elements on the stack of the task being inquired about.
p_used
is a pointer to a variable of type CPU_STK_SIZE and will contain the number of used CPU_STK elements on the stack of the task being inquired about.
p_err
is a pointer to a variable that will contain an error code returned by this function.
Returned Value
None
Notes/Warnings
Execution time of this task depends on the size of the task’s stack.
The application can determine the total task stack space (in number of CPU_STK elements) by adding the value of *p_free and *p_used. This number should add up to the task’s stack size which is stored in the .StkSize field of the OS_TCB of the task.
The #define CPU_CFG_STK_GROWTH must be declared (typically from os_cpu.h). When this #define is set to CPU_STK_GROWTH_LO_TO_HI, the stack grows from low memory to high memory. When this #define is set to CPU_STK_GROWTH_HI_TO_LO, the stack grows from high memory to low memory.
Example
OS_TCB MyTaskTCB;
void Task (void *p_arg) { OS_ERR err; CPU_STK_SIZE n_free; CPU_STK_SIZE n_used;
(void)&p_arg; while (DEF_ON) { : : OSTaskStkChk(&MyTaskTCB, &n_free, &n_used, &err); /* Check “err” */ : : } } |