Author Topic: Cortex-M0+ Hard Fault handler porting  (Read 3108 times)

0 Members and 1 Guest are viewing this topic.

Offline jsilvaTopic starter

  • Newbie
  • Posts: 8
  • Country: pt
    • GitHub
Cortex-M0+ Hard Fault handler porting
« on: January 31, 2021, 02:58:00 am »
Hello everyone,

I am porting a Hard Fault handler code I have been using for a while. It was originally written for a Cortex-M3, and has worked so far for M3, M4 and M7:
Code: [Select]
void __attribute__ ((naked)) _hardfault_isr()
{
    __asm__ volatile
    (
        " tst lr, #4                                                \n"
        " ite eq                                                    \n"
        " mrseq r0, msp                                             \n"
        " mrsne r0, psp                                             \n"
        " ldr r1, [r0, #24]                                         \n"
        " ldr r2, hardfault_trace_stack_addr                        \n"
        " bx r2                                                     \n"
        " hardfault_trace_stack_addr: .word hardfault_trace_stack   \n"
    );
}
But now I am working with a Cortex-M0+, and it wouldn't compile. After a close look, one quickly realises some of the instructions are not supported by the poor M0+.
After some head scratching, I came up with a solution that I think should implement the same algorithm, in an M0+ compatible way, but I am not sure:
Code: [Select]
void __attribute__ ((naked)) _hardfault_isr()
{
    __asm__ volatile
    (
        " movs r0, #4                                               \n"
        " movs r1, lr                                               \n"
        " tst r1, r0                                                \n"
        " mrs r0, msp                                               \n"
        " beq not_psp                                               \n"
        " mrs r0, psp                                               \n"
        " nop                                                       \n"
        " nop                                                       \n"
        " nop                                                       \n"
        " not_psp:                                                  \n"
        " ldr r1, [r0, #24]                                         \n"
        " ldr r2, hardfault_trace_stack_addr                        \n"
        " bx r2                                                     \n"
        " hardfault_trace_stack_addr: .word hardfault_trace_stack   \n"
    );
}
I would like someone who is more experienced to give some input, please.

It should determine whether the program was running off the PSP or MSP when the fault happened, determine the address of the stack frame and then call a C routine that will use that information.

Thank you,
João
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4676
  • Country: us
Re: Cortex-M0+ Hard Fault handler porting
« Reply #1 on: January 31, 2021, 11:13:45 am »
The M0+ also saves considerably less information on hard faults than the "bigger" Cortex CPUs.
From the ARM ARM:
Quote
In ARMv6-M, faults are considered fatal. The only fault status information provided on entry to the HardFault handler is the EXC_RETURN value, that indicates whether the fault originated from Thread or Handler mode. The fault handler returns according to the rules defined in Exception entry behavior on page B1-224 for details.
I'm not sure whether the info you're trying to collect is even there.  (I'm not sure it's NOT, either, but ... investigate carefully.)

(and yeah, it can come as a big shock how many instructions are "just not there" in v6m vs v7m.  :-( )
 

Offline bson

  • Supporter
  • ****
  • Posts: 2830
  • Country: us
Re: Cortex-M0+ Hard Fault handler porting
« Reply #2 on: February 01, 2021, 07:18:43 pm »
Can't gas automatically convert some of the nonexistent instructions?  If so it might be sufficient to use the correct compiler options for the target architecture.  (If nothing else, they should produce errors and fail to compiler/assemble.)

But of course it can't make up for missing fault status or other CSRs.
« Last Edit: February 01, 2021, 07:21:54 pm by bson »
 

Offline jsilvaTopic starter

  • Newbie
  • Posts: 8
  • Country: pt
    • GitHub
Re: Cortex-M0+ Hard Fault handler porting
« Reply #3 on: February 02, 2021, 03:41:07 am »
Can't gas automatically convert some of the nonexistent instructions?  If so it might be sufficient to use the correct compiler options for the target architecture.  (If nothing else, they should produce errors and fail to compiler/assemble.)

But of course it can't make up for missing fault status or other CSRs.

Nope, I did port the rest of the code, including all the compiler/linker options, and then tried to build to check if I missed anything, and it complained about that inline asm, so clearly the asmer didn't convert that.
 

Offline GromBeestje

  • Frequent Contributor
  • **
  • Posts: 310
  • Country: nl
Re: Cortex-M0+ Hard Fault handler porting
« Reply #4 on: February 05, 2021, 12:15:32 pm »
An interesting article about fault handling on Cortex-M0.   https://dmitry.gr/?r=05.Projects&proj=27.%20m0FaultDispatch
It should also be applicable to M0+,
 
The following users thanked this post: westfw


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->