cancel
Showing results for 
Search instead for 
Did you mean: 

Cortex M7 Hard Fault Handler - freeRTOS Aware

Garnett.Robert
Senior III

Hi All,

We all know how hard it often is to track down the cause of hard faults, particularly random or intermittent ones when the debugger isn't connected and particularly when using freeRTOS in a complex system with many tasks, timers and queues.  To assist I have written a hard fault handler that provides printed output of all the mcu and if used, floating point registers, special registers and the freeRTOS task that was running when the hard fault occurred.  It also provides memory ranges to check for program counter, link register and stack addresses to check whether an address used is definitely invalid.

I have checked it on a range of fault types and it seems to work OK.  I'm not an ARM assembler expert, in fact my assembler knowledge probably is the equivalent of Donald Trump's knowledge of tariffs, so I would appreciate it if the assembler/mcu experts could critique it and suggest fixes and mod's.

I have also written a small module that generates hard faults for testing purposes. To ease the use of this handler I have provided all the files required including my startup, interrupt handlers and  linker script in the attached zip file.

The output of the handler from TeraTerm is:

 

==============================

***** HardFault Occurred *****
Link Reg Value (Lockup Addr): 0xFFFFFFED
Stack frame: PSP Process Stack Ptr (Thread mode)

Fault status registers:
HFSR (HardFault Status):          0x00000000
CFSR (Configurable Fault Status): 0x00000082
MMFAR (MemManage Fault Addr):     0x00000000
BFAR (BusFault Addr):             0x00000000
AFSR (Auxiliary Fault Status):    0x00000000

MemManage Fault:
  - MMFAR valid (0x00000000)
  - Data access violation

HardFault details:
R0 : 0x00000006
R1 : 0x00001F40
R2 : 0xDEADBEEF
R3 : 0x00000000
R12: 0xDD6CE856
Stacked PC:  0x900441A2
Stacked PSR: 0x41000000
Stacked LR:  0x900442DD

Special registers:
CONTROL: 0x00000000
PRIMASK: 0x00000000
BASEPRI: 0x00000000
FAULTMASK: 0x00000000

FPU register dump:
FPSCR: 0x00000000
S0-S1:  0x00000000 0x00000000
S2-S3:  0x00000000 0x00000000
S4-S5:  0x00000000 0x00000000
S6-S7:  0x00000000 0x00000000
S8-S9:  0x00000000 0x00000000
S10-S11: 0x00000000 0x3F800000
S12-S13: 0x4A989680 0x40000000
S14-S15: 0x4E64E1C0 0x1C9C3800

Double precision registers:
D0:  0.000000 (0x000000000000000lX)
D1:  0.000000 (0x000000000000000lX)
D2:  0.000000 (0x000000000000000lX)
D3:  0.000000 (0x000000000000000lX)
D4:  0.000000 (0x000000000000000lX)
D5:  0.007812 (0x000000000000000lX)
D6:  2.000001 (0x000000000000000lX)
D7:  0.000000 (0x000000000000000lX)
----------------------------

freeRTOS Task Status:
----------------------------
Task Name: monitorTask
Task State: 0
Task Priority: 24
Task Stack High Water Mark: 77
Task Handle Addr: 2405e5e0
==== Hard Fault Report End ====

Info to use the handler may be found in the comment block at the start of HardFault_HandlerFreeRTOS.c

 

I hope people find this useful.

0 REPLIES 0