Stack sizes: STM32L-Discovery

Comments

3 comments

  • Avatar
    Horac

    task1Stack[ 0 ] = task1Stack[ 1 + STACKSIZE ] = 0xfacefeed;   

    This is out of array range by 8 bytes, array index in C is  from 0 (included) to ElementCount (excluded). Next lines same problem.

    task1Stack[ 0 ] = task1Stack[ STACKSIZE - 1] = 0xfacefeed;

    ctl_task_run( &new_task1, 1, task1, 0, "task1", STACKSIZE-1, task1Stack + 1, CALLSTACKSIZE );

    0
    Comment actions Permalink
  • Avatar
    Jason Beens

    The Rowley Guys use the definition:

     

    unsigned someStack[1+STACKSIZE+1];

     

    when making allocations for task stack space.  The 1+  and +1 additions to stack size in the definition account for the 8 bytes you identified as out of bounds.  The actual stack used by the CTL tasking system will start at address someStack[1] and continue to someStack[STACKSIZE].  someStack[0] and someStack[STACKSIZE + 1] are effectively reserved addresses.

     

    Go look at their examples, particularly where the array is declared that will later house the task stack.  The arrangement Crossworks uses for defining the stack has a several benefits. 

    1. You can at the application layer (or task layer) deal with stack size in units of memory that are comfortable for your program, using only the STACKSIZE macro.  This allows the hiding of implementation details of the task from the application.
    2. You can check for stack overrun and underrun errors by checking the contents of the array (someStack[] in this example) at address 0 and address STACKSIZE+1, and verifying that the value 0xfacefeed is at those addresses.

     

    The assumption is that an underrun or overrun will write some value other than 0xfacefeed into the addresses at someStack[0] or someStack[STACKSIZE + 1].  In my experience with CTL, this assumption has always held true.

     

    Our company has a kernel that is based on the Rowley CTL.  The Kernel actively checks the stack boundaries and asserts if there is an overrun or underrun detected.  It is quite handy. 

     

    Change your definitions for:

    static unsigned task1Stack[ STACKSIZE ]; // Stack for task 1 static unsigned task2Stack[ STACKSIZE ]; // Stack for task 2

    to:

    static unsigned task1Stack[ 1 + STACKSIZE  + 1]; // Stack for task 1 static unsigned task2Stack[  1 + STACKSIZE  + 1]; // Stack for task 2

     

     

    0
    Comment actions Permalink
  • Avatar
    Gavin Tait

    Thanks kindly Jason and Horac.

    0
    Comment actions Permalink

Please sign in to leave a comment.