Hi
Compiler independent C-only version for Cortex:
void *fnGetSP(void)
{
volatile unsigned long var = 0;
return (void *)((unsigned long)&var + 4);
}
If the variable is not set to 0 the code will generally be in-lined and result in the compiler directly generating a single instruction similar to
ADD R0, SP, #0x04
which is the SP value but I don't trust that because it may be slightly off when it optimises out the temporary variable (and the +4 may not be needed - making it compiler/optimisation dependent - although still accurate to one stack pointer location).
This code does generate a warning though because it uses the address of a temporary variable...
Regards
Mark