Everything
13.5 Stack Size Estimation
13.5.1 System stack size
The formula for calculating the system stack size is shown below.
[Expression 1: System stack size]
sys_stk = MAX(sys_stkA, sys_stkB, sys_stkC) + 2 (bytes)
[Expression 2: System stack size use pattern A]
sys_stkA = tsksvc + int0 + int1 + int2 + int3
[Expression 3: System stack size use pattern B]
sys_stkB = Size used by user in idle routine
[Expression 4: System stack size use pattern C]
sys_stkC = Size used by user in initialization routine
[Expression 5: Maximum size of system stack used during service call executed by task]
Maximum size of system stack used during service call executed by task
[Expression 6: Size of int0, int1]
Intx = Maximum size of interrupts used by stack in interrupts of level x
= Size used by user in interrupts
[Expression 7: Size of int2, int2]
intx = Maximum size of interrupts used by stack in interrupts of level x
= Size used by user in interrupts + allsvc + 18
[Expression 8: Total size used by system calls used in interrupt]
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.
The example is shown below.
[Conditions]
- Execute a pol_flg service call from task "task1".
- Execute a snd_mbx service call from task "task2".
- 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.
- Idle "idl" does not use the stack.
- The initialization routine "ini" uses 24 bytes of stack in the routine.
[Expression]
tsksvc = MAX(size of system stack used by pol_flg, size of system stack used by snd_mbx)
= MAX(16,8) = 16 bytes
int0 = 0 + 0 = 0 byte
int1 = undefined = 0 byte
int2 = 12 + ( 0 + 6 + 4 ) + 18 = 40 bytes
int3 = MAX(int3A, int3B) = MAX(56,32) = 56 bytes
int3A = 16 + ( 0 + 6 + 16 ) + 18 = 56 bytes
int3B = 0 + ( 0 + 0 + 14 ) + 18 = 32 bytes
sys_stkA = tsksvc + int0 + int1 + int2 + int3
= 16004056
= 112 bytes
Note This is the max in sys_stkA/B/C, so after this size or greater is secured.
sys_stkB = Stack size used by user in idle routine = 0 byte
sys_stkC = Stack size used by user in initialization routine = 20 bytes
sys_stk = MAX(sys_stkA, sys_stkB, sys_stkC)2
= MAX(112, 0, 20)
= 112 + 2 = 114 bytes
The system stack size will be the 112 bytes of sys_stkA.
The size specified in the system configuration file will be 112 bytes.
Note Below is shown the stack size used in service calls/functions used in the example.
For Service Call Arguments
For Internal Processing by Program Issued the Service Call
For System Stack Internal Processing
pol_flg
0
6
16
twai_flg
4
6
16
snd_mbx
0
6
4
Timer_Handler function
0
14

13.5.2 Stack size of the task
The formula for calculating the stack size of the task is shown below.
[Expression 1: No interrupts generated in task]
Task stack size = size used by user + service-call argument size + 6 (bytes)
[Expression 2: Interrupts generated in task]
Task stack size = size used by user + service-call argument size + 6 + 20 (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.
Because there are no functions or processes that use the stack, the size used by the user is 0.
When the size of arguments to all service calls is investigated, the results are as shown below.
Service-call argument size (twai_flg) = 4 bytes
Service-call argument size (snd_mbx) = 0 bytes
The largest stack size is used in the call to twai_flg, so this is specified in Expression 1.
Task stack size = size used by user + service-call argument size (twai_flg) + 6
= 046
= 10 bytes
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
Pattern B = size used by user (for function B) + service-call argument size (snd_mbx) + 6 + 20
= 200620
= 26 bytes
Compare pattern B with pattern A. The pattern that uses the most stack is pattern A, at 42 bytes.
The size specified in the system configuration file will be the above minus 6 bytes, which equals 36 bytes.