Moving the stack from the top of RAM to near the bottom is an often over looked method.
When it overflows it will generate an exception rather than silently corrupting things.
Explained here: Are We Shooting Ourselves in the Foot with Stack Overflow? « State Space
How to read the stack pointer:
In Generic C, a good tools set should complain about returning the address of a local variable:
/*
* void *CheckStackDepth( void )
* {
* volatile uint32_t dummy; // Put a variable on the stack
* return( (void *) &dummy ); // Return its address - therefore the (approx.) present SP value
* }
*/
In GCC:
static __inline__ void *sp_get(void)
{
void *sp;
__asm__ __volatile__ ("mrs %0, msp" : "=r"(sp));
return( sp );
}
I call sp_get() in IRQs then test and save to a global variable (which in general should be avoided) that will show the stack depth. Something different would need done if have nested IRQs.