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.

MSP430FR5989: Determining Stack Space Usage

Part Number: MSP430FR5989


Tool/software:

I am trying to a small function to allow me to determine how much stack space my application is using during its run time.

One of the big issues I am coming across is that when I read SP/R1, the value does not make sense. It reports back >128k. When looking at all 32 bits, it displays values well in excess of 1M which is not correct for this part as it has 128KB of FRAM on board.

Code snippet in question

```

uint32_t get_SP_register(void)
{
uint32_t sp_value;

// Using the `movx` instruction to handle 20-bit addressing in large memory model
__asm__ __volatile__("movx.a SP, %0" : "=r"(sp_value) : : "memory");

return sp_value & 0xFFFFF;
}
extern uint32_t _stack_start[];

void fill_stack_space()
{
uint32_t sp = get_SP_register();
uint32_t fill_size = sp - (uint32_t)_stack_start;

logger.info("Stack Pointer value: %lu bytes", sp);
logger.info("Stack Start: %lu bytes", (uint32_t)_stack_start);
logger.info("fill size: %lu bytes", fill_size);
 
}
```
In my linker script
```
.stack :
{
. = ALIGN(2);
_stack_start = .; /* Define the start of the stack */
. += STACK_SIZE;
_stack_end = .; /* Define the end of the stack */
PROVIDE(__stack = .);
*(.stack)
} > HIFRAM
```
Where STACK_SIZE is equal to 40K for the sake of testing
I am using the large memory model and compiling with -Os
I'm not sure why the stack pointer would be off by so much but replacing the return value of the function with any known value does show my logger statements properly print out what I am returning. 
  • Some examples of bad data would be helpful.

    I see that GCC includes a word sized version of this:

    #define _get_SP_register() \
    ({ \
            unsigned int __x; \
            __asm__ __volatile__( \
            "mov SP, %0" \
            : "=r" ((unsigned int) __x) \
            :); \
            __x; \
    })
    
    

  • After a bit of deleting it all and trying again, doing the following

      uintptr_t sp_value;
      // Using the `movx` instruction to handle 20-bit addressing in large memory model
      __asm__ __volatile__("movx.a SP, %0" : "=r"(sp_value) : : "memory");
      uint32_t stack_pointer = (uint32_t)sp_value;

    works reliably now.

    Casting to uint32_t afterwards just to have a easy / known format vs uintptr_t. Personal preference.

    Seems like the data type itself had to be of size uintptr_t instead of any arbitrary value. 

**Attention** This is a public forum