1
votes

On STM32F4, how can I read a CPU register?

Are these registers mapped anywhere in memory?

If not, can you please provide a simple assembly code for reading a register (LR for example) into a local variable?

Thanks.

3

3 Answers

3
votes

With Keil toolchain I used this approach:

register int R0 __asm("r0");

where 'R0' is now recognized by compiler as variable.

Search for 'Named register variables' e.g. :

http://www.keil.com/support/man/docs/armcc/armcc_chr1359125006491.htm

2
votes

First off, if you're simply trying to peek into program execution, you should just download the IAR Embedded Workbench for ARM (IAR EWARM) size-limited version (no trial-end date).

  • You can create a new project, drag-and-drop your code to it, and even run it in the simulator (e.g. choose "Simulator" in Project->Options->Debugger). Then you can profile all the registers you want.

But if you really want to do it in code...well it depends on which registers you want to read, what you want to do with them, and what the compiler decides to use them for.

General-purpose registers are going to be used however the compiler decides it wants to use them, so you need to understand the underlying assembly to understanding what they represent.

Your local variables are usually stored in GPR's, so reading the value of a local variable (in c code) is essentially performing a register-read in this case.

Reading Special-purpose registers, such as the LR, is probably going to require assembly. For example, you could write a pure-assembly function that returns the LR of the calling function, and then call that function from c-code.

For example, from main, one might execute x = lr_return_func();, where lr_return_func() is a pure-assembly function that copies the LR used in main (stored on the stack above function parameters) to register R0 (used for return values). When lr_return_func returns, this LR would then get stored in x (e.g. some register back in main).

You could do it with in-line, but you need to determine which register your local variable is using first. So if you determine that x is being stored in R4 for example, then your inline assembly would want to copy the LR to R4, and be done...essentially storing your LR in x.

1
votes

There is a good tool available for Eclipse, called EmbSys Registers. It's really handy even though sometimes it lags