13.5 Stack Size Estimation
Intx = Maximum size of interrupts used by stack in interrupts of level x
= Size used by user in interrupts
= Size used by user in interrupts
intx = Maximum size of interrupts used by stack in interrupts of level x
= Size used by user in interrupts + allsvc + 18
= Size used by user in interrupts + allsvc + 18
allsvc = For service call arguments + For internal processing by program issued the service call + For system stack internal processing
Specify the system stack size in the system configuration file. Note, however, that the size that is actually secured is the value specified in the configurator + 2 bytes. Consequently, the value that is actually specified in the system configuration file is the sys_stk value calculated in expression 1 minus 2 bytes.
We recommend specifying a system stack size higher than the estimate in order to reduce the danger of a stack overflow.
- Interrupt int0 is a level-0 interrupt process not managed by the OS. The stack is not used in the interrupt.
- Interrupt int2 is a level-2 OS interrupt handler. Execute the snd_mbx service call, and use 12 bytes of stack in the interrupt.
- Interrupt int3A is a level-3 OS interrupt handler. Execute the pol_flg service call, and use 16 bytes of stack in the interrupt.
- Interrupt int3B is a level-3 OS interrupt handler. Execute Timer_Handler, the stack is not used in the interrupt.
tsksvc = MAX(size of system stack used by pol_flg, size of system stack used by snd_mbx)
= MAX(16,8) = 16 bytes
= MAX(16,8) = 16 bytes
Specify the task stack size in the system configuration file. Note, however, that the size that is actually secured is the value specified in the configurator 6 bytes. Consequently, the value that is actually specified in the system configuration file is the sys_stk value calculated in expression 1 or expression 2 minus 6 bytes.
These 6 bytes include the stack size used when system calls are issued. Note, however, that the stack size used when issuing system calls must secure the size used by the user in addition to the 6 bytes of argument stack size. The argument stack sized used by each service call is different. Table 12-1 summarizes these sizes.
The task stack size is the largest stack size used in the task in question. For this reason, if there is a service call with an argument stack of 4 bytes, and another with 8 bytes, then the pattern that uses the most stack - 8 bytes - will be secured.
The above material refers to tasks where interrupts are not accepted (all interrupts are disabled). An additional 18 bytes must be secured for tasks where interrupts are accepted.
Note that these 20 bytes include the stack size when the _kernel_int_entry function is called (required to be called when an interrupt starts). _kernel_int_entry only retires the 20 bytes of data from the stack, it does not replace it. The data is recovered upon the call to the _kernel_int_exit function, which must be called when the interrupt ends.
Example 1 Task "task1" uses the twai_flg and snd_mbx service calls, and has no other functions or processes that use the stack.
If interrupts are not accepted in the task, interrupts are not accepted in task1, so Expression 1 is the formula for calculating stack usage.
The size specified in the system configuration file will be the above minus 6 bytes, which equals 4 bytes.
Example 2 In task "task1", function A (using 12 bytes of stack) makes a twai_flg service call, and function B (using 20 bytes of stack) makes a snd_mbx service call.
Since interrupts are accepted in the task, Expression 2 is used as the calculation formula. List the patterns in order to find the one that uses the most stack.
Pattern A = size used by user (for function A) + service-call argument size (twai_flg) + 6 + 20
= 124620
= 42 bytes
= 124620
= 42 bytes