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.

Compiler/MSP432P401R: Stack Usage

Part Number: MSP432P401R


Tool/software: TI C/C++ Compiler

I would like to 'monitor' the stack usage in my code so I can insure I have allocated sufficient stack size.  My project has a display, so this maximum stack usage is part of my diagnostic display.

In the MSP430, at boot I filled the stack with "0xAAAA" then periodically read backwards from the end of the stack until the content was not 0xAAAA.

I have not figured a way to access read/write the stack in the MSP432.

If anyone has figured out a way to monitor stack usage in the MSP432, I would like to hear from you.

Thanks

  • The linker creates two symbols: __STACK_END and __STACK_SIZE. The stack grows downward.

    The current SP (R13) can be retrieved using __get_MSP(), which appears to return a uint32_t.

    Your project gets a private copy of startup_msp432p401r_ccs.c, which seems like a good place to do the initialization (careful of the Watchdog).

    I did this some years ago on a Cortex from [Other Guys], and I recall it not being overly difficult.
  • A few years back I tripped over a program (script?) buried in CCS somewhere which could do static stack analysis. I used it for the MSP430, but as I recall it started from the C source, so maybe it would work with the MSP432 as well. It did a fairly good job -- it mainly had trouble with function pointers (printf being significant).

    I wasn't able to find it just now with a cursory search. I mention this in hopes that someone with a better memory than mine will speak up.
  • Bruce McKenney47378 said:
    A few years back I tripped over a program (script?) buried in CCS somewhere which could do static stack analysis.

    There is the call_graph program which is part of the cg_xml package - see Finding out static stack usage. This isn't integrated into CCS, but can be run from a command line or from a post build rule in CCS. call_graph can be used on programs for Cortex-M devices which use the TI ARM compiler.

    From CCS 6.2 onwards there is a "Stack Usage" view.

    I consider call_graph to be better than the CCS Stack View since:

    a) call_graph allows indirect calls (function pointers) to be specified in text files, where the CCS Stack Usage view stores annotations for indirect calls in the workspace rather than a user specified file - see Is it possible for the CCS 6.2 "Stack Usage" view to use text "indirect" files like the cg_xml call_graph utility?

    b) Found a case where the CCS Stack View under-reported the stack usage - see CCS/MSP430FG4618: CCS 7.2 Stack Usage can under-report the stack size requirement when Function Symbol Aliasing occurs

  • Thanks for the info.  __STACK_END and __STACK_SIZE did the trick.

    here is my code.

    #define STACK_MAGIC_VALUE 0x33445566

    #define STACK_TOP_PROTECTED (20*sizeof(uint32_t))

    extern unsigned long __STACK_END;
    extern unsigned long __STACK_SIZE;


    ///////////////////////////////////////////////////////////////////////////
    // we assume StackWipe got called at boot and then starting at the bottom
    // of the stack that it filled we search up until we find a don't find the magic number
    // and assume this is as far as the stack got pushed during execution.
    // returns the stack free (unused bytes, 255 or less )
    uint8_t  StackChecker( void )
    {

    uint32_t  unused = 0;

    uint32_t* pStackBottom = (uint32_t*)(__STACK_END - __STACK_SIZE);
    uint32_t* pFilled = pStackBottom + (__STACK_SIZE/sizeof(uint32_t) - STACK_TOP_PROTECTED);

    while( pStackBottom < pFilled)
    {
    if( *pStackBottom++ == STACK_MAGIC_VALUE )
    unused += sizeof(uint32_t);
    else
    break;
    }
    if(unused > 255 )
    unused = 255;
    return (uint8_t) unused;
    }

    /////////////////////////////////////////////////////////////////////////////
    // Fills stack with magic value (except top few bytes)
    void StackWipe( void )
    {
    uint32_t* pStackBottom = (uint32_t*)(__STACK_END - __STACK_SIZE);
    uint32_t* pFilled = pStackBottom + (__STACK_SIZE/sizeof(uint32_t) -STACK_TOP_PROTECTED);
    while( pStackBottom < pFilled)
    *pStackBottom++ = STACK_MAGIC_VALUE;
    }

**Attention** This is a public forum