This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430FR5043: MSP430 runtime stack analysis

Part Number: MSP430FR5043

Hello TI, 

I am currently using MSP430FR5043 MCU in my project. In my code i want to do runtime stack analysis. So, I written following code using IAR embedded workbench.

I want to fill some random work of entire stack and need to check at the end of the while loop to make sure that stack memory should not change after 90% mark.

// Last 10% entries to check.
#define ENTRIES_TO_CHECK_AFTER 230

#pragma segment="CSTACK"

// Fill the stack immediate to main.
static void init_stack(void)
{
    uint16_t       * p_begin     = __segment_begin("CSTACK");
    uint16_t const * const p_end = __segment_end("CSTACK");

    while (p_begin != p_end)
    {
        *p_begin = 0xDEADu;
        ++p_begin;
    }
}

// Verify stack at the end of for loop in main.
static void verify_stack(void)
{
    uint16_t const * const p_begin  = __segment_begin("CSTACK");

    uint16_t const * p_check_from   = p_begin + ENTRIES_TO_CHECK_AFTER;
    uint16_t const * const p_end    = __segment_end("CSTACK");

    while (p_check_from!= p_end)
    {
        taf_assert(0xDEADu != *p_check_from);
        ++p_check_from;
    }
}
int main()
{
   init_stack();
   /// board initlaization
   // application init
   while(1)
  {
      .........
      .........
      .........
     verify_stack();
   }
   return 0;
}

I need to know better implementation if any other then above. And let me know if above code is best?
  • In a typical embedded application the starting stack address will be at the top of SRAM. The bottom is determined by the symbol sbrk which is used to manage the heap. (malloc() and friends) Your program does not care in the slightest about this stack segment and will happily push data onto the stack outside of it. It may be the case that the code managing the heap will check to see if allocating memory will extend sbrk past the stack pointer but I wouldn't count on it.

    You can get a fair idea of how much stack space you need by examining your source code. Count up how much space is required for auto variables, add more for each function call (return address plus saved registers and maybe parameters that don't fit in registers). Walk the code to find the deepest call tree, or at least the tree that requires the most space. (You can use something like the Linux program cflow to generate a call graph.)  If it looks like a problem, try to find where you can save parameter space. Avoid recursive functions.

  • Thanks for the help. I think my runtime stack is C_STACK segement which is specified in the linker file. Need to write at the starting of the porogram and read at the end of while loop inside to check same bytes are there or occupied by program?

  • Hi,

    Sorry we were not able to provide timely support on your issue. If you are still needing assistance please post back or open a new thread.

**Attention** This is a public forum