博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HardFault_Handler 输出日志信息
阅读量:4948 次
发布时间:2019-06-11

本文共 4136 字,大约阅读时间需要 13 分钟。

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

 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]的数值是我们需要的

转载于:https://www.cnblogs.com/wwjdwy/p/3818354.html

你可能感兴趣的文章
你应该知道的 RPC 原理
查看>>
Ubuntu安装词典
查看>>
KVM虚拟机在线添加网卡
查看>>
Spring解析
查看>>
支付宝签约教程及注意事项
查看>>
Linux Glibc溢出漏洞凶猛来袭 可让***者获取操作系统的控制权限
查看>>
设计模式之原则
查看>>
Maven修改全局和局部JDK版本
查看>>
设计模式——组合模式(Composite Pattern)
查看>>
java设计模式之——代理模式
查看>>
php页面防止重复提交
查看>>
Perl DBI模块的例子
查看>>
python中str和repr区别
查看>>
升级win10后无法使用桥接网络解决方法
查看>>
如何进行跨网段的远程唤醒
查看>>
数据挖掘-同比与环比
查看>>
nginx+php详解
查看>>
怎样取php一个字符串中的某个字符
查看>>
我的友情链接
查看>>
RedHat6 管理应用服务【11】
查看>>