之前文章说了原理,这里把最终实现的代码总结:
IAR
void hard_fault_handler_c (unsigned int * hardfault_args)
{
unsigned int stacked_r0;
unsigned int stacked_r1;
unsigned int stacked_r2;
unsigned int stacked_r3;
unsigned int stacked_r12;
unsigned int stacked_lr;
unsigned int stacked_pc;
unsigned int stacked_psr; stacked_r0 = ((unsigned long) hardfault_args[]);
stacked_r1 = ((unsigned long) hardfault_args[]);
stacked_r2 = ((unsigned long) hardfault_args[]);
stacked_r3 = ((unsigned long) hardfault_args[]); stacked_r12 = ((unsigned long) hardfault_args[]);
stacked_lr = ((unsigned long) hardfault_args[]);
stacked_pc = ((unsigned long) hardfault_args[]);
stacked_psr = ((unsigned long) hardfault_args[]); printf ("\r\n[Hard fault handler - all numbers in hex]\r\n");
printf ("R0=%x\r\n",stacked_r0);
printf ("R1=%x\r\n",stacked_r1);
printf ("R2=%x\r\n",stacked_r2);
printf ("R3=%x\r\n",stacked_r3);
printf ("R12=%x\r\n",stacked_r12);
printf ("LR[R14]=%x subroutine call return address\r\n",stacked_lr);
printf ("PC[R15]=%x program counter\r\n",stacked_pc);
printf ("PSR=%x\r\n",stacked_psr);
printf ("BFAR=%x\r\n",(*((volatile unsigned long *)(0xE000ED38))));
printf ("CFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED28))));
printf ("HFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED2C))));
printf ("DFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED30))));
printf ("AFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED3C))));
printf ("SCB_SHCSR=%x\r\n", SCB->SHCSR); while ();
}
hard_fault_handler_c
void HardFault_Handler( void )
{
__ASM("TST LR, #4");
__ASM("ITE EQ");
__ASM("MRSEQ R0, MSP");
__ASM("MRSNE R0, PSP");
__ASM("B hard_fault_handler_c");
}
HardFault_Handler
这里的HardFault_Handler里是内联汇编
MDK不能使用内联汇编
__ASM void HardFault_Handler(void)
{
TST lr, # // Test for MSP or PSP
ITE EQ
MRSEQ r0, MSP
MRSNE r0, PSP
IMPORT HardFault_Handler_C
B HardFault_Handler_C
}
HardFault_Handler
void HardFault_Handler_C (unsigned int * hardfault_args)
{
unsigned int stacked_r0;
unsigned int stacked_r1;
unsigned int stacked_r2;
unsigned int stacked_r3;
unsigned int stacked_r12;
unsigned int stacked_lr;
unsigned int stacked_pc;
unsigned int stacked_psr; stacked_r0 = ((unsigned long) hardfault_args[]);
stacked_r1 = ((unsigned long) hardfault_args[]);
stacked_r2 = ((unsigned long) hardfault_args[]);
stacked_r3 = ((unsigned long) hardfault_args[]); stacked_r12 = ((unsigned long) hardfault_args[]);
stacked_lr = ((unsigned long) hardfault_args[]);
stacked_pc = ((unsigned long) hardfault_args[]);
stacked_psr = ((unsigned long) hardfault_args[]); printf ("\n\n[Hard fault handler - all numbers in hex]\r\n");
printf ("R0 = %x\r\n", stacked_r0);
printf ("R1 = %x\r\n", stacked_r1);
printf ("R2 = %x\r\n", stacked_r2);
printf ("R3 = %x\r\n", stacked_r3);
printf ("R12 = %x\r\n", stacked_r12);
printf ("LR [R14] = %x subroutine call return address\r\n", stacked_lr);
printf ("PC [R15] = %x program counter\r\n", stacked_pc);
printf ("PSR = %x\r\n", stacked_psr);
printf ("BFAR = %x\r\n", (*((volatile unsigned long *)(0xE000ED38))));
printf ("CFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED28))));
printf ("HFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED2C))));
printf ("DFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED30))));
printf ("AFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED3C))));
printf ("SCB_SHCSR = %x\r\n", SCB->SHCSR); while ();
}
HardFault_Handler_C
[Hard fault handler - all numbers in hex]
R0 =
R1 =
R2 =
R3 = c0
R12 =
LR [R14] = subroutine call return address
PC [R15] = program counter
PSR =
BFAR =
CFSR =
HFSR =
DFSR =
AFSR =
SCB_SHCSR =
对于调试很有帮助的,注意PC[R15]的数值是我们需要的