cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing MSR when switched to PSR

lo0446
Associate II
Posted on March 17, 2014 at 12:28

AREA StackSwitch, CODE, READONLY

IMPORT PrintHex

EXPORT Start

EXPORT Reset_Handler

stack_base DCD 0x28000

THUMB

PRESERVE8

ENTRY

Reset_Handler

Start

MOV r0, &sharp0xf00d

PUSH {r0}

MOV r0, &sharp0

BL Mode_Switch

POP {r0}

BL PrintHex ; TASK 1: Fix the program so that this prints 0xf00d

; ** by only making changes inside Mode_Switch

; and ensuring that you are still in process

; mode when you

; get here **  B Finish

Mode_Switch

MOV r0, &sharp0x2 ; set stack to PSP

MSR CONTROL, r0 ; do it

ISB ; wait for it to be done

BX lr ; and return safely

; ================

; End your program

; ================

Finish

B Finish

END ; Mark end of file

Hi, I'm currently learning how to use the ARM STM32F4 Discovery board and I've come across a problem I'm not sure how to solve. The code above doesn't actually do what is expected since we switch stacks and then POP r0 (from the wrong stack) which isn't what we want.

I cannot switch back to the stack like in MODE_SWITCH and I don't want to copy the value into a register before I make the switch to the other stack. How do I use MSR or MRS (presuming I am in privileged mode at this point) to get r0 from the other register so I can pop the correct value?

I've spent a while looking through the documentation and experimenting but I can't seem to get it work.

thanks!

#discovery #stm32f4
6 REPLIES 6
Posted on March 17, 2014 at 14:36

You could save R0/R1, store the SP in R1, and the use R1 to recover the original R0/R1 before returning.

Or pop and push the top value in the subroutine.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lo0446
Associate II
Posted on March 17, 2014 at 21:52

Thanks for your reply Clive but I'm afraid I still don't quite understand what you are suggestion. Assume any code that I add will be in the MODE_SWITCH subroutine. I am specifically trying to learn how to use the MSR/MRS operations.

Apologies for the simple questions but you have to start somewhere!

Much appreciated.

Posted on March 17, 2014 at 22:53

Mode_Switch
POP {r1}
MOV r0, #0x2 ; set stack to PSP
MSR CONTROL, r0 ; do it
ISB ; wait for it to be done
PUSH {r1}
BX lr ; and return safely

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lo0446
Associate II
Posted on March 21, 2014 at 17:33

I'm afraid the purpose of this task is for me to try and get to grips with the MSR and MRS instructions, my solution should involve those instructions. Does anyone have any experience with this? Feel free to point me towards any resources you have found useful for me to research.

Regards

Posted on March 21, 2014 at 18:19

My code uses MSR, and performs the stated task

; TASK 1: Fix the program so that this prints 0xf00d
; ** by only making changes inside Mode_Switch
; and ensuring that you are still in process
; mode when you
; get here **

I can see another clever way of doing this too. Perhaps the secondary task is to use MRS, and to print out the content of the CONTROL register, or a message, that addresses the latter clause and ensuring, or proving, that was in the Process Mode, using the PSP
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lo0446
Associate II
Posted on March 22, 2014 at 18:07

Thanks for your help Clive, I've figured it out - much appreciated.