首页 > 代码库 > HardFault_Handler 输出日志信息

HardFault_Handler 输出日志信息

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

 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
1 void HardFault_Handler( void )2 {3 __ASM("TST LR, #4");4 __ASM("ITE EQ");5 __ASM("MRSEQ R0, MSP");6 __ASM("MRSNE R0, PSP");7 __ASM("B hard_fault_handler_c");8 }
HardFault_Handler

这里的HardFault_Handler里是内联汇编

 

MDK不能使用内联汇编

1 __ASM void HardFault_Handler(void)2 {3       TST lr, #4     // Test for MSP or PSP4       ITE EQ5       MRSEQ r0, MSP6       MRSNE r0, PSP7       IMPORT HardFault_Handler_C8       B HardFault_Handler_C9 }
HardFault_Handler
 1 void HardFault_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 ("\n\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   while (1);39 } 
HardFault_Handler_C
 1 [Hard fault handler - all numbers in hex] 2 R0 = 11111000 3 R1 = 8000378 4 R2 = 8000379 5 R3 = c0 6 R12 = 400 7 LR [R14] = 8001759  subroutine call return address 8 PC [R15] = 8000340  program counter 9 PSR = 100000010 BFAR = 1111111111 CFSR = 820012 HFSR = 4000000013 DFSR = 114 AFSR = 015 SCB_SHCSR = 0

对于调试很有帮助的,注意PC[R15]的数值是我们需要的