Skip to main content
Simon1
Associate
March 12, 2019
Question

HardFault handler asm msp/psp routine

  • March 12, 2019
  • 1 reply
  • 1483 views

Trying to track the cause of hardfault handler and got next problem.

MCU - STM32F302CB and i'm using eclipse with gnu mcu plugins. I'm trying to use the following code for assembly part of handler, which determines which stack is used:

 __asm volatile
 (
 " tst lr, #4 \n"
 " ite eq \n"
 " mrseq r0, msp \n"
 " mrsne r0, psp \n"
 " ldr r1, [r0, #24] \n"
 " ldr r2, handler2_address_const \n"
 " bx r2 \n"
 " handler2_address_const: .word prvGetRegistersFromStack \n"
 );

However when hardfault occurs and this piece of code is executing the program just jumps to some strange address and then stops executing. So when i execute line by line (in disassembly) it's happening on branch instruction (line 9). And in r2 there is really an address which is not valid, i mean it is not the address of prvGetRegistersFromStack, they differ.

But if i change code to something like this:

 __asm volatile
 (
 " tst lr, #4 \n"
 " ite eq \n"
 " mrseq r0, msp \n"
 " mrsne r0, psp \n"
 " b prvGetRegistersFromStack \n"
 
 );

Then it works fine and jumps to this C function which logs stack data.

I've seen this code in a couple of articles. Exactly this code is from:

https://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html

But it doesn't work for me, what am i missing ?

This topic has been closed for replies.

1 reply

Tesla DeLorean
Guru
March 12, 2019

That looks like an awful hack. Problem with in-line assembler is that it is highly dependent on the compiler/linker version/vendor involved. Most rational people put this stuff in the startup.s file where it belongs.

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