博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HardFault_Handler 输出日志信息
阅读量:4946 次
发布时间: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

你可能感兴趣的文章
操作引入xml文件的书包(定位到指定节点)
查看>>
操作系统学习笔记系列(一)- 导论
查看>>
CSS实例:图片导航块
查看>>
window的对象有哪些(笔记)
查看>>
Boolean Expressions
查看>>
They Are Everywhere
查看>>
数据结构--汉诺塔递归Java实现
查看>>
day14 多态与抽象
查看>>
Eclipse CDT 出现 launch failed Binary not found
查看>>
apache jmeter
查看>>
Linux 基本命令
查看>>
RedHat7.0 网络源的配置
查看>>
(Mark)JS中关于闭包
查看>>
流程结构图
查看>>
ios端web app在键盘升起后缩小view防止界面仍可上下滑动
查看>>
从service弹出系统级自定义提示框,可在任意页面弹出
查看>>
Bootstrap简单介绍
查看>>
iOS Touch ID 身份认证
查看>>
springboot 注解笔记
查看>>
图解HTTP---------------------------------------4
查看>>