cancel
Showing results for 
Search instead for 
Did you mean: 

stack use monitor

andrewberlin9
Associate II
Posted on August 03, 2011 at 15:06

I'd like to add stack usage monitoring to my application. I can't afford the overhead of stack checking on every function call, so I was considering adding a routine that runs periodically to monitor the stack high-water mark. I'm not using an RTOS.

I know my stack size, but how do I find the beginning of the stack? will it always start at the same place? I'm using the Keil tools, if that makes a difference.

Thanks!
4 REPLIES 4
Posted on August 03, 2011 at 17:41

The stack is set by the first vector on the STM32, and descends from there. The C startup code may also place it too, before calling main().

A 4K stack, on a 20K part, can nominally get placed at 0x20005000 and descend to 0x20004000. Fill that space with a distinctive pattern, and enumerate upward to measure the depth used.

In Keil the bottom of the stack is defined by Stack_Mem, and the top/start as __initial_sp

My stack checker loads a pointer to Stack_Mem, and the climbs 32-bits at a time until if find the bottom of the used stack, and then computes the amount used based on the known size.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
andrewberlin9
Associate II
Posted on August 03, 2011 at 21:04

Thank you; conceptually I understand what to do, but I'm getting stuck on the implementation, perhaps because I'm new to the Keil/ARM tools. Now that you've steered me towards the appropriate linker symbols, I can't seem to reference them (__initial_sp and Stack_Mem) from my C code; the linker complains the symbols are undefined. I tried declaring them as extern but it didn't help.

Posted on August 04, 2011 at 01:05

In stm32F10x.s (startup.s, whatever)

                EXPORT Stack_Size

                EXPORT Stack_Mem

Stack_Size      EQU     0x00001000

                AREA    STACK, NOINIT, READWRITE, ALIGN=3

Stack_Mem       SPACE   Stack_Size

__initial_sp

in C application

extern void * Stack_Mem;

    unsigned long *p = (unsigned long *)&Stack_Mem;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
andrewberlin9
Associate II
Posted on August 04, 2011 at 17:18

I was missing the EXPORT statements; it works now.

Thanks!