Calculating portMINIMAL_STACK_SIZE

hermarcel wrote on Monday, February 07, 2005:

In the port I’m working on, the portMINIMAL_STACK_SIZE depends on several things. The compiler, however calculates this size and makes it available as an assembler-definition.

What I am currently missing is a way to influence the task-requested-stacksize just before it is allocated in task.c.

Add a line to sTaskCreate() like:
usStackDepth = portCalcStack(usStackDepth);

The function (or macro) portCalcStack() can then modify the requested stackdepth as needed.

rtel wrote on Monday, February 07, 2005:


I’m not sure what you mean when you say the compiler calculates this size.

When your application starts (before the scheduler has been started) the stack that is used is that allocated by the compiler/linker.  Normally you can influence it’s size using a linker command - or something similar as per your particular compiler.

The size of the stack used by each task is passed as a parameter to sTaskCreate(), so you could use your portCalcStack() function to generate the parameter value.

The only task that is out of the applications control that uses portMINIMAL_STACK_SIZE is the idle task.  Normally the idle task does not require much stack so this can be set small.  The demo application tasks sometimes use portMINIMAL_STACK_SIZE but this is just for convenience - they could use any appropriate stack size.

One issue for very memory constrained systems is how to recover the memory used by the original - linker assigned - stack.  Once the scheduler has started this is not required any more so is wasted RAM [unless you have the scheduler end function implemented, in which case you need a stack to return to.  A nice solution to this is to turn your main() function into a task before the scheduler starts and use this in place of one of your application tasks.  I have not implemented this as part of the standard kernel as it uses more ROM for the extra functionality, maybe it should be a conditional compilation option?


hermarcel wrote on Monday, February 07, 2005:

compare it to math-sections of the mplab port: WizC also has these ram-regions of varying size. Due to the nature of wizC, the size of this area is available as an assembler-definition.

When I would add this varying size to the size of the context-specific registers (sfr’s, fixed size) and add the stack the task needs, I get a total size the new stack should be able to hold.

I am looking for a convenient way (without users that write tasks need to know about it) to do this calculation. I could, of course, define a wrapper for sTaskCreate(), but then I am sure confusion will arise when people read the freertos-api-docs (they won’t use the wrapper but call sTaskCreate() directly.

About the original stack: use it as stack for the idle task??

rtel wrote on Monday, February 07, 2005:

>About the original stack: use it as stack for the idle task??

That might be a simpler alternative.