zoukankan      html  css  js  c++  java
  • 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 ("
    [Hard fault handler - all numbers in hex]
    ");
    23   printf ("R0=%x
    ",stacked_r0);
    24   printf ("R1=%x
    ",stacked_r1);
    25   printf ("R2=%x
    ",stacked_r2);
    26   printf ("R3=%x
    ",stacked_r3);
    27   printf ("R12=%x
    ",stacked_r12);
    28   printf ("LR[R14]=%x subroutine call return address
    ",stacked_lr);
    29   printf ("PC[R15]=%x program counter
    ",stacked_pc);
    30   printf ("PSR=%x
    ",stacked_psr);
    31   printf ("BFAR=%x
    ",(*((volatile unsigned long *)(0xE000ED38))));
    32   printf ("CFSR=%x
    ",(*((volatile unsigned long *)(0xE000ED28))));
    33   printf ("HFSR=%x
    ",(*((volatile unsigned long *)(0xE000ED2C))));
    34   printf ("DFSR=%x
    ",(*((volatile unsigned long *)(0xE000ED30))));
    35   printf ("AFSR=%x
    ",(*((volatile unsigned long *)(0xE000ED3C))));
    36   printf ("SCB_SHCSR=%x
    ", 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 PSP
    4       ITE EQ
    5       MRSEQ r0, MSP
    6       MRSNE r0, PSP
    7       IMPORT HardFault_Handler_C
    8       B HardFault_Handler_C
    9 }
    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 ("
    
    [Hard fault handler - all numbers in hex]
    ");
    23   printf ("R0 = %x
    ", stacked_r0);
    24   printf ("R1 = %x
    ", stacked_r1);
    25   printf ("R2 = %x
    ", stacked_r2);
    26   printf ("R3 = %x
    ", stacked_r3);
    27   printf ("R12 = %x
    ", stacked_r12);
    28   printf ("LR [R14] = %x  subroutine call return address
    ", stacked_lr);
    29   printf ("PC [R15] = %x  program counter
    ", stacked_pc);
    30   printf ("PSR = %x
    ", stacked_psr);
    31   printf ("BFAR = %x
    ", (*((volatile unsigned long *)(0xE000ED38))));
    32   printf ("CFSR = %x
    ", (*((volatile unsigned long *)(0xE000ED28))));
    33   printf ("HFSR = %x
    ", (*((volatile unsigned long *)(0xE000ED2C))));
    34   printf ("DFSR = %x
    ", (*((volatile unsigned long *)(0xE000ED30))));
    35   printf ("AFSR = %x
    ", (*((volatile unsigned long *)(0xE000ED3C))));
    36   printf ("SCB_SHCSR = %x
    ", 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 = 1000000
    10 BFAR = 11111111
    11 CFSR = 8200
    12 HFSR = 40000000
    13 DFSR = 1
    14 AFSR = 0
    15 SCB_SHCSR = 0

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

  • 相关阅读:
    P2422 良好的感觉
    拉格朗日插值
    C# 中的委托和事件(详解)
    异步委托
    ManualResetEvent详解
    快速理解C#高级概念事件与委托的区别
    拉格朗日多项式
    oracle 插入一个从别处查询获得字段的值
    decode和nvl的用法
    C#将像素值转换为图片
  • 原文地址:https://www.cnblogs.com/wwjdwy/p/3818354.html
Copyright © 2011-2022 走看看