Stack usage. STMicroelectronics OS20
விளம்பரம்
விளம்பரம்
5.13 Stack usage 5 Tasks
5.13 Stack usage
A common problem when developing applications is not allocating enough stack for a task, or the need to tune stack allocation to minimize memory wastage. OS20 provides a couple of techniques which can be used to address this.
The first technique is to enable stack checking in the compiler see the ST20 Embedded
Toolset User Manual, chapter st20cc compile/link tool. This adds an additional function call at the start of each of the user’s functions, just before any additional stack is allocated. The called stack check function can then determine whether there is sufficient space available for the function which is about to execute.
As OS20 is multi-threaded, a special version of the stack check function needs to be used, which can determine the current task, and details about the task’s stack. When using
-runtime os20 to link the application, the stack check function is linked in automatically.
Otherwise it is necessary to link with the configuration file os20scc1.cfg
(for a C1 target) or os20scc2.cfg
(for a C2 target) to ensure the correct function is linked in.
Whilst stack checking has the advantage that stack overflows are reported immediately, it has a number of problems.
●
There is a run-time cost incurred for every function call to perform the check.
●
It can only report on functions which are recompiled with stack checking enabled.
An alternative technique is to determine experimentally how much stack a task uses by giving the task a large stack initially, running the code, and then seeing how much stack has been used. To allow this, OS20 normally fills a task’s stack with a known value. As the task runs, it writes its own data into the stack, altering this value, and later the stack can be inspected to determine the highest address which has not been altered.
To support this, OS20 provides the function: int task_status( task_t* Task,
task_status_t *Status,
task_status_flags_t Flags );
This function can be used to determine information about the task’s stack, in particular the base and size specified when the task was created, and the amount of stack which has been used.
Stack filling is enabled by default, however, in some cases the user may want to control it, so two functions are provided: int task_stack_fill(task_stack_fill_t* fill); returns details about the current stack fill settings, and: int task_stack_fill_set(task_stack_fill_t* fill); allows them to be altered. Stack filling can be enabled or disabled, or the fill value changed.
By default it is enabled, and the fill value set to 0x12345678 .
By placing a call to task_stack_fill_set in a start-up function, before the OS20 kernel is initialized, it is possible to control the filling of the root task’s stack.
To determine how much stack has been used task_status can be called, with the Flags parameter set to task_status_flags_stack_used . For this to work correctly, task stack filling must have been enabled when the task was created, and the fill value must have the same value as the one which was in effect when the task was created.
37
ADCS 7473749D
பதிவிறக்கு
விளம்பரம்