Cortex-M Hardfault Interrupt Handler

Tech question

Is it possible to exit the hardfault interrupt handler and return back to the main program?

I know it is possible to reset the processor from the hardfault handler but I would prefer this not too happen.

If this is not possible, is it possible to make the processor just ignore invalid memory writes? This is just a test program hence the strange requirements.

Posted Oct 1, 2019, 7:24 PM UTC

1315
Xenoamor•

I know on the M0 they are fatal and can't be returned from. The processor enters some form of lockup mode

As far as I'm aware this is the same on the M3 and M4.

What you're after is probably more likely to exist in the A series

12
UnicycleBlokeC++ advocate•

Don't think so. A hard fault is a sign of something badly wrong such as accessing non existent memory addresses. You lose the stack, but can recover some information such as the address where the fault occurred.

11
Recursive-NOP•

Hardfaults aren't always a software issue. A battery operated device can throw a hardfault when the processor voltage drops too low. Of course, internally this this some sort of memory issue. My handler updates a hardfault event counter in EEPROM and reboots.

It is hard so see why you would want to recover from this condition even in testing. If the system may be compromised, it makes sense to reboot.

6
prettygoodiguess•

It may be possible, but it wouldbe extremely difficult. The most that I've really seen done (and what I would suggest) is to utilize the handler to log somewhere that the fault happened and where it occurred. Your best bet is to prevent faults and you can use test-driven-design techniques to exercise edge cases to sus out faults.

If you were running an RTOS and it happened in a thread there could be the possibility of stopping that thread, but even then it's majorly sketchy since your stack is now corrupted.

4
AssemblerGuy•

ARM Cortex-M TRM says:

Typically, HardFault is used for unrecoverable system failures, although this is not required and some uses of HardFault might be recoverable.

So, yes, you can try returning back to the application, but depending on what caused (or got escalated to) the hardfault, the application might be in an undefined state. You'll have to thoroughly read the TRM and especially every little bit about exception behavior.

3
aaarnas•

If you're getting Hardfault, there is something really bad with your code (memory access) and it shouldn't be ignored. It can be found using debugger. Add breakpoint inside hardfault interrupt and then go back to find from where it's fired.

Ignoring it, may lead to writing out of variables scope and application starts to behave unexpectedly.

2
dgendreau•

go back to find from where it's fired

To be more clear, OP look at the call stack at the time the Hard Fault occurred. What function was executing just before the hard fault occurred? if the call stack is complete garbage then something as smashed the stack. If not, then you should take a good look at that function and see what it was doing and why.

2
aaarnas•

Of course, this method won't give 100% success rate of finding faulty line of code. Also, compiler optimisations will remove some lines of code and it may be harder to inspect what is getting called. To help with optimisations issue: For GCC compiler add attribute attribute((optimize("-O0"))) to function declaration, where you may expect hardfault is happening. This may help a bit to see all the instructions. Also, you can make some local variables to global in case they are "optimised out" and you need to check their state during debugging. Also, this method may not work if using RTOS. Other solution, if you can reproduce error: Add LOG("1");, LOG("2"); lines to multiple places to see in what flow functions are called or instructions executed. Also, you will see which LOG was called last. Let's say log 6 was called, but instead of 7 you got hardfault. This means something bad happened between these log calls. Inspect that place, add more detail logging, print out variables status and you will find the problem. I always use Segger RTT logs, because they are least intrusive to application and they work on all platforms, just by simply adding RTT library to the code base.

2
duskwuff•

Add breakpoint inside hardfault interrupt and then go back to find from where it's fired.

This can get complicated. Hardfaults can be imprecise -- if a memory access is buffered, the stacked PC may point a few instructions (or more) beyond the one that triggered the exception.

1
macisaint•

Our organization has written code that successfully returns from a fault handler, but--- we knew exactly which instruction would fault, we did not attempt to return to the context that faulted, we did it for the purpose of working around a hardware bug, and we rebooted before returning to normal operation. it's a messy affair that involves manipulating the stack.

2
kendiser5000•

Kinda late but I wrote this

It'll recover from faults that can be recovered from.

2
[deleted]•

[deleted]

1
WarrantOfPartition•

If you have access to Yiu's Cortex books, read the section on fault handling. ( https://dl.acm.org/citation.cfm?id=2846134 ). Obviously this isn't helpful if you don't, but what you can do depends on the cause of the fault.

1
rsaxvc•

Yes, but you must disable imprecise errors.

1
NLJeroen•

You could. But you’d have undo some hardware stackframing, and remove or fix the faulty instruction. Usually you just skip it, or kill the task that caused it.

However, you need a really good reason to.

1
playaspec•

What is the test you're trying to do?

1