之前文章说了原理,这里把最终实现的代码总结:

 IAR

 1 void hard_fault_handler_c (unsigned int * hardfault_args)
 2 {
 3   unsigned int stacked_r0;
 4   unsigned int stacked_r1;
 5   unsigned int stacked_r2;
 6   unsigned int stacked_r3;
 7   unsigned int stacked_r12;
 8   unsigned int stacked_lr;
 9   unsigned int stacked_pc;
10   unsigned int stacked_psr;
11 
12   stacked_r0 = ((unsigned long) hardfault_args[0]);
13   stacked_r1 = ((unsigned long) hardfault_args[1]);
14   stacked_r2 = ((unsigned long) hardfault_args[2]);
15   stacked_r3 = ((unsigned long) hardfault_args[3]);
16  
17   stacked_r12 = ((unsigned long) hardfault_args[4]);
18   stacked_lr = ((unsigned long) hardfault_args[5]);
19   stacked_pc = ((unsigned long) hardfault_args[6]);
20   stacked_psr = ((unsigned long) hardfault_args[7]);
21  
22   printf ("\r\n[Hard fault handler - all numbers in hex]\r\n");
23   printf ("R0=%x\r\n",stacked_r0);
24   printf ("R1=%x\r\n",stacked_r1);
25   printf ("R2=%x\r\n",stacked_r2);
26   printf ("R3=%x\r\n",stacked_r3);
27   printf ("R12=%x\r\n",stacked_r12);
28   printf ("LR[R14]=%x subroutine call return address\r\n",stacked_lr);
29   printf ("PC[R15]=%x program counter\r\n",stacked_pc);
30   printf ("PSR=%x\r\n",stacked_psr);
31   printf ("BFAR=%x\r\n",(*((volatile unsigned long *)(0xE000ED38))));
32   printf ("CFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED28))));
33   printf ("HFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED2C))));
34   printf ("DFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED30))));
35   printf ("AFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED3C))));
36   printf ("SCB_SHCSR=%x\r\n", SCB->SHCSR);
37  
38   
39   while (1);
40 }
hard_fault_handler_c

相关文章: