50#include "cmsis_compiler.h"
54#if NRF_HARDFAULT_HANDLER_EXTENDED
58#define LOG_MODULE "NRF HARDFAULT"
59#define LOG_LEVEL LOG_LEVEL_INFO
61typedef struct HardFault_stack {
88HardFault_c_handler(uint32_t *p_stack_address)
91#define CFSR_MMARVALID (1 << (0 + 7))
95#define CFSR_BFARVALID (1 << (8 + 7))
98 HardFault_stack_t *p_stack = (HardFault_stack_t *)p_stack_address;
99 static const char *cfsr_msgs[] = {
100 [0] =
"The processor has attempted to execute an undefined instruction",
101 [1] =
"The processor attempted a load or store at a location that does not permit the operation",
103 [3] =
"Unstack for an exception return has caused one or more access violations",
104 [4] =
"Stacking for an exception entry has caused one or more access violations",
105 [5] =
"A MemManage fault occurred during floating-point lazy state preservation",
108 [8] =
"Instruction bus error",
109 [9] =
"Data bus error (PC value stacked for the exception return points to the instruction that caused the fault)",
110 [10] =
"Data bus error (return address in the stack frame is not related to the instruction that caused the error)",
111 [11] =
"Unstack for an exception return has caused one or more BusFaults",
112 [12] =
"Stacking for an exception entry has caused one or more BusFaults",
113 [13] =
"A bus fault occurred during floating-point lazy state preservation",
116 [16] =
"The processor has attempted to execute an undefined instruction",
117 [17] =
"The processor has attempted to execute an instruction that makes illegal use of the EPSR",
118 [18] =
"The processor has attempted an illegal load of EXC_RETURN to the PC, as a result of an invalid context, or an invalid EXC_RETURN value",
119 [19] =
"The processor has attempted to access a coprocessor",
124 [24] =
"The processor has made an unaligned memory access",
125 [25] =
"The processor has executed an SDIV or UDIV instruction with a divisor of 0",
128 uint32_t cfsr = SCB->CFSR;
130 if(p_stack != NULL) {
132 LOG_INFO(
"HARD FAULT at 0x%08lX\n", p_stack->pc);
133 LOG_INFO(
" R0: 0x%08lX R1: 0x%08lX R2: 0x%08lX R3: 0x%08lX\n",
134 p_stack->r0, p_stack->r1, p_stack->r2, p_stack->r3);
135 LOG_INFO(
" R12: 0x%08lX LR: 0x%08lX PSR: 0x%08lX\n",
136 p_stack->r12, p_stack->lr, p_stack->psr);
138 LOG_INFO(
"Stack violation: stack pointer outside stack area.\n");
141 if(SCB->HFSR & SCB_HFSR_VECTTBL_Msk) {
142 LOG_INFO(
"Cause: BusFault on a vector table read during exception processing.\n");
145 for(uint32_t i = 0; i <
sizeof(cfsr_msgs) /
sizeof(cfsr_msgs[0]); i++) {
146 if(((cfsr & (1 << i)) != 0) && (cfsr_msgs[i] != NULL)) {
147 LOG_INFO(
"Cause: %s.\n", cfsr_msgs[i]);
151 if(cfsr & CFSR_MMARVALID) {
152 LOG_INFO(
"MemManage Fault Address: 0x%08lX\n", SCB->MMFAR);
155 if(cfsr & CFSR_BFARVALID) {
156 LOG_INFO(
"Bus Fault Address: 0x%08lX\n", SCB->BFAR);
176 " .syntax unified \n"
178 " ldr r0, =0xFFFFFFFD \n"
180 " bne HardFault_Handler_ChooseMSP \n"
183 " b HardFault_Handler_Continue \n"
184 "HardFault_Handler_ChooseMSP: \n"
190 " ldr r1, =__StackTop \n"
191 " ldr r2, =__StackLimit \n"
195 " bhi HardFault_MoveSP \n"
197 " bhi HardFault_Handler_Continue \n"
199 "HardFault_MoveSP: \n"
203 "HardFault_Handler_Continue: \n"
208 : :
"X" (HardFault_c_handler)
void HardFault_Handler(void)
Hardfault handler.
Header file for the logging system.