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

IAR

 void hard_fault_handler_c (unsigned int * hardfault_args)
{
unsigned int stacked_r0;
unsigned int stacked_r1;
unsigned int stacked_r2;
unsigned int stacked_r3;
unsigned int stacked_r12;
unsigned int stacked_lr;
unsigned int stacked_pc;
unsigned int stacked_psr; stacked_r0 = ((unsigned long) hardfault_args[]);
stacked_r1 = ((unsigned long) hardfault_args[]);
stacked_r2 = ((unsigned long) hardfault_args[]);
stacked_r3 = ((unsigned long) hardfault_args[]); stacked_r12 = ((unsigned long) hardfault_args[]);
stacked_lr = ((unsigned long) hardfault_args[]);
stacked_pc = ((unsigned long) hardfault_args[]);
stacked_psr = ((unsigned long) hardfault_args[]); printf ("\r\n[Hard fault handler - all numbers in hex]\r\n");
printf ("R0=%x\r\n",stacked_r0);
printf ("R1=%x\r\n",stacked_r1);
printf ("R2=%x\r\n",stacked_r2);
printf ("R3=%x\r\n",stacked_r3);
printf ("R12=%x\r\n",stacked_r12);
printf ("LR[R14]=%x subroutine call return address\r\n",stacked_lr);
printf ("PC[R15]=%x program counter\r\n",stacked_pc);
printf ("PSR=%x\r\n",stacked_psr);
printf ("BFAR=%x\r\n",(*((volatile unsigned long *)(0xE000ED38))));
printf ("CFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED28))));
printf ("HFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED2C))));
printf ("DFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED30))));
printf ("AFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED3C))));
printf ("SCB_SHCSR=%x\r\n", SCB->SHCSR); while ();
}

hard_fault_handler_c

 void HardFault_Handler( void )
{
__ASM("TST LR, #4");
__ASM("ITE EQ");
__ASM("MRSEQ R0, MSP");
__ASM("MRSNE R0, PSP");
__ASM("B hard_fault_handler_c");
}

HardFault_Handler

这里的HardFault_Handler里是内联汇编

MDK不能使用内联汇编

 __ASM void HardFault_Handler(void)
{
TST lr, # // Test for MSP or PSP
ITE EQ
MRSEQ r0, MSP
MRSNE r0, PSP
IMPORT HardFault_Handler_C
B HardFault_Handler_C
}

HardFault_Handler

 void HardFault_Handler_C (unsigned int * hardfault_args)
{
unsigned int stacked_r0;
unsigned int stacked_r1;
unsigned int stacked_r2;
unsigned int stacked_r3;
unsigned int stacked_r12;
unsigned int stacked_lr;
unsigned int stacked_pc;
unsigned int stacked_psr; stacked_r0 = ((unsigned long) hardfault_args[]);
stacked_r1 = ((unsigned long) hardfault_args[]);
stacked_r2 = ((unsigned long) hardfault_args[]);
stacked_r3 = ((unsigned long) hardfault_args[]); stacked_r12 = ((unsigned long) hardfault_args[]);
stacked_lr = ((unsigned long) hardfault_args[]);
stacked_pc = ((unsigned long) hardfault_args[]);
stacked_psr = ((unsigned long) hardfault_args[]); printf ("\n\n[Hard fault handler - all numbers in hex]\r\n");
printf ("R0 = %x\r\n", stacked_r0);
printf ("R1 = %x\r\n", stacked_r1);
printf ("R2 = %x\r\n", stacked_r2);
printf ("R3 = %x\r\n", stacked_r3);
printf ("R12 = %x\r\n", stacked_r12);
printf ("LR [R14] = %x subroutine call return address\r\n", stacked_lr);
printf ("PC [R15] = %x program counter\r\n", stacked_pc);
printf ("PSR = %x\r\n", stacked_psr);
printf ("BFAR = %x\r\n", (*((volatile unsigned long *)(0xE000ED38))));
printf ("CFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED28))));
printf ("HFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED2C))));
printf ("DFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED30))));
printf ("AFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED3C))));
printf ("SCB_SHCSR = %x\r\n", SCB->SHCSR); while ();
}

HardFault_Handler_C

 [Hard fault handler - all numbers in hex]
R0 =
R1 =
R2 =
R3 = c0
R12 =
LR [R14] = subroutine call return address
PC [R15] = program counter
PSR =
BFAR =
CFSR =
HFSR =
DFSR =
AFSR =
SCB_SHCSR =

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

HardFault_Handler 输出日志信息的更多相关文章

  1. Myeclipse集成Jboss 6.1控制台不输出日志信息

    在使用myeclipse+jboss 6.1开发的时候发现jboss能够正常启动但是myeclipse的控制台却没有任何的信息输出,这使得我没有办法开发,在查找了大部分的资料发现很多说要改什么jbos ...

  2. Android开发3——查看和输出日志信息

    一.错误级别 Error > Warn > Info > Debug > Verbose(冗余) 二.Android项目日志查看视图 Console视图只能看项目的部署到模拟器 ...

  3. 如何正确地在SOE中输出日志信息

    ArcGIS for Server提供完善的日志管理机制,用于日志的记录.查询和自动清除.开发人员在开发编写SOE代码时,应该采用该机制进行日志记录的输出.如果不采用该机制,输出的日志消息会写到Arc ...

  4. nohup不输出日志信息的方法,及linux重定向学习

    起因 最近使用nohup创建了一个后台进程,默认日志输出到了nohup.out文件中,程序跑起来也就没再管,过了大约一周,发现硬盘空间不够了,于是查找原因,发现这个nohup.out文件已经到了70G ...

  5. nohup不输出日志信息的方法及linux重定向

    最近使用nohup创建了一个后台进程,默认日志输出到了nohup.out文件中,程序跑起来也就没再管,过了大约一周,发现硬盘空间不够了,于是查找原因,发现这个nohup.out文件已经到了70G了,导 ...

  6. tomcat的日志不输出日志信息的解决方法

    1.下载日志jar包,例如:commons-logging-1.1.1.jar.放在tomcat的bin目录下,或者是自己项目的lib包里. 2.修改tomcat的bin目录下面的catalina.b ...

  7. Android学习笔记Log类输出日志信息

    Log类提供的方法 代码示例 .. Log.e(TAG,"[错误信息]"); Log.w(TAG,"[警告信息]"); Log.i(TAG,"[普通信 ...

  8. 解决spark-shell输出日志信息过多

    import org.apache.log4j.Logger import org.apache.log4j.Level Logger.getLogger("org").setLe ...

  9. shell脚本实现定时重启任务并输出日志信息

    #!/bin/bash #当前日期 time=`date` pidno=`ps aux|grep adserver-beta|grep -v "grep"|awk '{print ...

随机推荐

  1. [codevs] 1699 开关灯

    题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...

  2. Codeforces Round #297 (Div. 2) D. Arthur and Walls [ 思维 + bfs ]

    传送门 D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input stan ...

  3. 视图中使用foreach报错问题

    问题情境:thinkphp3.2版本,使用四层<foreach></foreach>循环变量时,报错以下错误: syntax error, unexpected 'endfor ...

  4. Swift--错误集:couldn’t be opened because you don’t have permission to view it

    bug复现过程  把snapkit拉入代码中时,也把里面的info.plist文件拖到项目中,运行时,提示“couldn’t be opened because you don’t have perm ...

  5. Apache 文件根目录设置修改方法 (Document Root)

    最近在学习WordPress,使用appServ 在windows上搭建Php开发环境 在网上查找到的关于修改Apache服务器根目录的资料,对比学习,再此记录 在安装 Apache 时,系统会给定一 ...

  6. 进入一个jsp直接跳到另一个jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  7. 魔咒词典--hdu1880(字符串 暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=1880 不要想其他的   暴力就能过 #include <iostream> #include < ...

  8. [Poj1185][Noi2001]炮兵阵地(状压dp)

    炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29476   Accepted: 11411 Descriptio ...

  9. loj515 贪心只能过样例(bitset)

    题目: https://loj.ac/problem/515 分析: 所有可能和的最大值是1e6 如果dp的话,dp[i][j]表示前i个数能否凑出和为j的数 这样是O(n^5)的 考虑到[j]可以用 ...

  10. loj6157 A^B Problem (并查集)

    题目: https://loj.ac/problem/6157 分析: 这种树上异或,一般是采用分位考虑,但是这题即使分位,也会发现非常不好处理 这里考虑维护一个点到其根的路径的异或值 用并查集去检测 ...