AnsweredAssumed Answered

Code for STACK usage calculation:

Question asked by Hareesha Koratikere Rameshappa on Oct 7, 2014
Latest reply on Oct 14, 2014 by Hareesha Koratikere Rameshappa

Hi All,

 

                First we were doing the stack usage calculation by manual as follows: (Controller is S9S12G128F0MLF LQFO48)

 

  • Using a typical bench setup, flash the software to be examined.
  • Attach a JTAG debugger to the JTAG connector on the PCB
  • Use CodeWarrior to attach to the PCB and download debugging information.
  • Set a breakpoint at the beginning of the function main ().
  • Start program execution. The breakpoint should be reached in a second or two.
  • Using the registers window, verify that the SP is set to 20FD. (The startup code calls the main function so that is the reason that it is not 20FF.)
  • In the memory window, right click and select fill. Set the fill range to 2000 thru 20FC and the fill value to some recognizable value that is not a valid program address, such as A1. Press OK and verify that the stack area has been filled with the chosen value.
  • Resume program execution and try to invoke as much of the possible software execution paths as you can. At least run the software for 5 minutes.
  • At the end of the software exercise, stop the debugger and examine the memory window for the lowest address in the range 2000 thru 20FC that is not a fill value. Subtract that address from 0x2100, convert to decimal and divide the result by 256. The resulting number is the fraction of the stack space used.

 

But now I want to do this stuff through code. So I have written following functions to do the same:

All functions looks fine individually, but if I call one after other, STACK is overflowing at the end of *Read_From_Stack() and Stack_Usage_Calc. even I am setting SP to top of the STACK.

 

Please anyone can identify what is going wrong with my code.

 

If anyone know / having the example/working code to calculate STACK usage, please share with me.

 

 

/*

  Function to get size of stack

*/

extern uint16_t __SEG_SIZE_SSTACK[];

 

uint16_t Stack_Size_Allocated()

{

 

  {

      return (uint16_t)__SEG_SIZE_SSTACK;

  }

   

}

 

/*

    Function to write known data into stack location

*/

void Write_To_Stack()

{

  static uint16_t SP_Value = 0x20FD;

                asm INS

                asm INS

                asm INS

                //asm LDS #$20FD

  while (SP_Value > 0x2000)

                                {

      asm LDAA #0xA1

                                                asm PSHA //Push index register data into stack;

      asm STS SP_Value

    }

    asm LDS #$20FD

}

 

/*

    Function to write known data into stack location

*/

 

static uint8_t *Read_From_Stack()

{

  static uint8_t Temp_Stack = 0;

  static uint16_t SP_Value_Top = 0xFFFF;

  static uint8_t i = 0;

  static uint8_t Stack_Data[255];

  static uint8_t x;

  for (x = 0; x < 255; ++x)

    {

      Stack_Data[x] = 0xFF;

    }

                  asm INS

                  asm INS

                  asm INS

                  asm INS

                  asm INS

                                while (SP_Value_Top > 0x2000)

                                {

                                                asm PULA // Pull data frop top of the stack into X register

                                                asm DES

                                                asm STAA Temp_Stack //Temp_Stack //Data read from Stack Last Address

                                                Stack_Data[i] = Temp_Stack;

                                                i++;

      asm DES

      asm STS SP_Value_Top

    } 

    return Stack_Data;

}

 

/*

    Function to calculate stack usage:

*/

 

void Stack_Usage_Calc()

{

  //uint8_t *Stack_Used =0; /* a pointer to an int */

                static uint8_t Stack_Used_Count = 0;

                static uint8_t Stack_Not_Used_Count = 0;

                static uint8_t i = 0;

                static uint8_t Stack_Size = 255;

                static uint16_t Stack_Usage = 0;

                static uint8_t *Stack_Used; /* a pointer to an int */

                               

  //Stack_Size = Stack_Size_Allocated();

                Stack_Used = Read_From_Stack();

 

 

                for (i=0; i<Stack_Size ; ++i)

                                {

                                                if (*(Stack_Used + i) == 0xA1)

                                                                {

                                                                                Stack_Used_Count++;

                                                                }

                                                else

                                                                {

Stack_Not_Used_Count++; //Stack is not used, No Count, do nothing

                                                                }

                                 }

  Stack_Usage = ((Stack_Used_Count/Stack_Size)*100); //It gives Stack usage in % by our BMS 

}

 

Thanks in advance

 

Regards,

Hareesha.

Outcomes