2020-05-24 23:08:58 +00:00
|
|
|
#include "configuration.h"
|
|
|
|
#include <core_cm4.h>
|
|
|
|
|
|
|
|
// Based on reading/modifying https://blog.feabhas.com/2013/02/developing-a-generic-hard-fault-handler-for-arm-cortex-m3cortex-m4/
|
|
|
|
|
|
|
|
enum { r0, r1, r2, r3, r12, lr, pc, psr };
|
|
|
|
|
2020-07-15 20:24:57 +00:00
|
|
|
// we can't use the regular DEBUG_MSG for these crash dumps because it depends on threading still being running. Instead use the
|
|
|
|
// segger in memory tool
|
|
|
|
#define FAULT_MSG(...) SEGGER_MSG(__VA_ARGS__)
|
|
|
|
|
2020-05-24 23:08:58 +00:00
|
|
|
// Per http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0552a/Cihcfefj.html
|
|
|
|
static void printUsageErrorMsg(uint32_t cfsr)
|
|
|
|
{
|
2020-07-15 20:24:57 +00:00
|
|
|
FAULT_MSG("Usage fault: ");
|
2020-05-24 23:08:58 +00:00
|
|
|
cfsr >>= SCB_CFSR_USGFAULTSR_Pos; // right shift to lsb
|
|
|
|
if ((cfsr & (1 << 9)) != 0)
|
2020-07-15 20:24:57 +00:00
|
|
|
FAULT_MSG("Divide by zero\n");
|
2020-05-24 23:08:58 +00:00
|
|
|
if ((cfsr & (1 << 8)) != 0)
|
2020-07-15 20:24:57 +00:00
|
|
|
FAULT_MSG("Unaligned\n");
|
2020-05-24 23:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void printBusErrorMsg(uint32_t cfsr)
|
|
|
|
{
|
2020-07-15 20:24:57 +00:00
|
|
|
FAULT_MSG("Bus fault: ");
|
2020-05-24 23:08:58 +00:00
|
|
|
cfsr >>= SCB_CFSR_BUSFAULTSR_Pos; // right shift to lsb
|
|
|
|
if ((cfsr & (1 << 0)) != 0)
|
2020-07-15 20:24:57 +00:00
|
|
|
FAULT_MSG("Instruction bus error\n");
|
2020-05-24 23:08:58 +00:00
|
|
|
if ((cfsr & (1 << 1)) != 0)
|
2020-07-15 20:24:57 +00:00
|
|
|
FAULT_MSG("Precise data bus error\n");
|
2020-05-24 23:08:58 +00:00
|
|
|
if ((cfsr & (1 << 2)) != 0)
|
2020-07-15 20:24:57 +00:00
|
|
|
FAULT_MSG("Imprecise data bus error\n");
|
2020-05-24 23:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void printMemErrorMsg(uint32_t cfsr)
|
|
|
|
{
|
2020-07-15 20:24:57 +00:00
|
|
|
FAULT_MSG("Memory fault: ");
|
2020-05-24 23:08:58 +00:00
|
|
|
cfsr >>= SCB_CFSR_MEMFAULTSR_Pos; // right shift to lsb
|
|
|
|
if ((cfsr & (1 << 0)) != 0)
|
2020-07-15 20:24:57 +00:00
|
|
|
FAULT_MSG("Instruction access violation\n");
|
2020-05-24 23:08:58 +00:00
|
|
|
if ((cfsr & (1 << 1)) != 0)
|
2020-07-15 20:24:57 +00:00
|
|
|
FAULT_MSG("Data access violation\n");
|
2020-05-24 23:08:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 20:24:57 +00:00
|
|
|
extern "C" void HardFault_Impl(uint32_t stack[])
|
2020-05-24 23:08:58 +00:00
|
|
|
{
|
2020-07-15 20:45:57 +00:00
|
|
|
FAULT_MSG("Hard Fault occurred! SCB->HFSR = 0x%08lx\n", SCB->HFSR);
|
2020-05-24 23:08:58 +00:00
|
|
|
|
|
|
|
if ((SCB->HFSR & SCB_HFSR_FORCED_Msk) != 0) {
|
2020-07-15 20:45:57 +00:00
|
|
|
FAULT_MSG("Forced Hard Fault: SCB->CFSR = 0x%08lx\n", SCB->CFSR);
|
2020-05-24 23:08:58 +00:00
|
|
|
|
|
|
|
if ((SCB->CFSR & SCB_CFSR_USGFAULTSR_Msk) != 0) {
|
|
|
|
printUsageErrorMsg(SCB->CFSR);
|
|
|
|
}
|
|
|
|
if ((SCB->CFSR & SCB_CFSR_BUSFAULTSR_Msk) != 0) {
|
|
|
|
printBusErrorMsg(SCB->CFSR);
|
|
|
|
}
|
|
|
|
if ((SCB->CFSR & SCB_CFSR_MEMFAULTSR_Msk) != 0) {
|
|
|
|
printMemErrorMsg(SCB->CFSR);
|
|
|
|
}
|
|
|
|
|
2020-07-15 20:24:57 +00:00
|
|
|
FAULT_MSG("r0 = 0x%08lx\n", stack[r0]);
|
|
|
|
FAULT_MSG("r1 = 0x%08lx\n", stack[r1]);
|
|
|
|
FAULT_MSG("r2 = 0x%08lx\n", stack[r2]);
|
|
|
|
FAULT_MSG("r3 = 0x%08lx\n", stack[r3]);
|
|
|
|
FAULT_MSG("r12 = 0x%08lx\n", stack[r12]);
|
|
|
|
FAULT_MSG("lr = 0x%08lx\n", stack[lr]);
|
|
|
|
FAULT_MSG("pc = 0x%08lx\n", stack[pc]);
|
|
|
|
FAULT_MSG("psr = 0x%08lx\n", stack[psr]);
|
2020-05-24 23:08:58 +00:00
|
|
|
}
|
2020-07-15 20:45:57 +00:00
|
|
|
|
|
|
|
FAULT_MSG("Done with fault report - Waiting to reboot\n");
|
|
|
|
asm volatile("bkpt #01"); // Enter the debugger if one is connected
|
|
|
|
while (1)
|
|
|
|
;
|
2020-05-24 23:08:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 20:24:57 +00:00
|
|
|
extern "C" void HardFault_Handler(void)
|
2020-05-24 23:08:58 +00:00
|
|
|
{
|
|
|
|
asm volatile(" mrs r0,msp\n"
|
|
|
|
" b HardFault_Impl \n");
|
|
|
|
}
|