Skip to main content
ELitv.2
Associate II
July 14, 2022
Question

after calling __set_MSP the local variables get corrupted

  • July 14, 2022
  • 8 replies
  • 6972 views

Hello,

First, I am sharing some basic information.

The target - STM32G0, compiler Keil C11.

I am trying to jump from a Bootloader to an application. The scenario is very frequent and there are many questions about that on the web, but unfortunately I failed to find a solution to my problem. One of the problems I haven't yet resolved is corruption of the local variables right before the jump as result of __set_MSP which stores in the stack the base address of the applicatio, where I am trying to jump to.

You see here 2 pictures.

On the 1st picture you can see that appJumpAddress is 0x800515D, which is correct.

On the 2nd picture , after calling __set_MSP, written in asm, you see that appJumpAddress is already wrong and therefore my BL jumps to the wrong place in Flash memory .

The operation is standard , described everywhere. What I might did wrong?

Thanks!

P.S.

If someone here has an implementation of such a jump in Keil, can you please share these 3-4 lines?

This topic has been closed for replies.

8 replies

Tesla DeLorean
Guru
July 14, 2022

No kidding... half this stuff is coded by people who barely know what they are doing.

The use of statics/globals would have been safer.

Look, don't set the MSP here, it's not coming back. Explicitly load the SP as the first thing you do in the Reset_Handler of the *Application*. Some of the GNU startup.s examples do this.

Reset_Handler PROC
 EXPORT Reset_Handler [WEAK]
 IMPORT SystemInit
 IMPORT __main
 
; LDR SP, =__initial_sp 
 
 LDR R0, =__initial_sp ; Cortex-M0(+) method
 MOV SP, R0
 
 LDR R0, =SystemInit
 BLX R0
 
 LDR R0, =__main
 BX R0
 ENDP

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
July 14, 2022

Also the interrupts shouldn't be disabled at the MCU level, nothing enables them again.

And don't change the SCB->VTOR here. That should be handled in the App side SystemInit() with a symbol the linker can fix up.

extern uint32_t *__Vectors;

SCB->VTOR = (uint32_t)(&__Vectors);

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
ELitv.2
ELitv.2Author
Associate II
July 14, 2022

Actually, SCB->VTOR I changed in the SystemInit in the App, because Keil changed it to the FLASH_BASE which is 0. Thus, setting VTOR before the jump is redundant.

Thank you for your help!

Piranha
Principal III
July 14, 2022

A complete solution:

https://community.st.com/s/question/0D50X0000AFpTmUSQV/using-nvicsystemreset-in-bootloaderapplication-jumps

P.S. The line 229 perfectly describes almost all ST's firmware related code in general!

ELitv.2
ELitv.2Author
Associate II
July 14, 2022

Thank you!

Andrew Neil
Super User
July 15, 2022
A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
ELitv.2
ELitv.2Author
Associate II
July 15, 2022

You're right and I appologize for not providing pictures of good quality. It is because I asked the question with my phone and had these pictures because i discussed the issue with my friends on Telegram. Next time i will do a better job. Thank you!