views:

161

answers:

2

I need to access the MSP and PSP registers (the main and process stack registers) of the Cortex-M3 processor.

I'm writing in C/C++.

The µVision and associated compiler doesn't let you do inline assembly for this Thumb-2 only core (and I'm not sure that'd be such a good idea anyway).

I need to do this so that I can extract the immediate value of an svc instruction regardless of whether it was executed while in thread or handler mode.

Thanks,

A: 

For something like this I usually use a tiny assembly call-out to do it.

You can use the special register access instruction (MRS / MSR) to access either SP. I think with uVision, the MSP is *SP_main*, and the PSP is *SP_process*.

My ARM assembly is a little rusty, but I think it'd be something like:

MRS   R0, SP_process   // R0 holds retval
BX    LR                // return

From your question, it seems that you already understand the distinction between the MSP & PSP, and know how to determine which one to read (via the LR).

Dan
+3  A: 

I don't have access to a Keil compiler at the moment to verify this, but according to the docs you should be able to use the compiler's "named register variable" feature:

register uint32_t msp __asm("msp");
register uint32_t psp __asm("psp");
Michael Burr
Thanks, Michael. I didn't realize that I could do inline assembly. The compiler refuses to do block inline assembly because that, apparently, requires Arm mode instructions and the Cortex-M3 only does Thumb-2.
Captain NedD
A limitation of Keil, of course. GCC allows inline assembly on Cortex-M3.
David Grayson