STM32进入HardFault_Handler的调试方法
在编写STM32程序代码时由于自己的粗心会发现有时候程序跑着跑着就进入了
HardFault_Handler中断,按照经验来说进入HardFault_Handler故障的原因主要有两个方面:
1:内存溢出或则访问越界。
2:堆栈溢出。
发生异常后我们可以首先查看LR寄存器的值,确认当前使用的堆栈是MSP还是PSP,然后找到相对应的堆栈指针,并在内存中查看相对应堆栈的内容,内核将R0~R3,R12,LR,PC,XPRS寄存器依次入栈,其中LR即为发生异常前PC将要执行的下一条指令地址。那么Cortex-M3内核HardFault错误调试定位方法有:
解决方法 精确定位出问题代码的所在位置:
①首先更改startup.s的启动文件,把里面的HardFault_Handler代码段换成下面的代码
②然后把HardFault_Handler_c的函数放在c文件的代码中,代码如下:
void hard_fault_handler_c(unsigned int * hardfault_args)
{
static unsigned int stacked_r0;
static unsigned int stacked_r1;
static unsigned int stacked_r2;
static unsigned int stacked_r3;
static unsigned int stacked_r12;
static unsigned int stacked_lr;
static unsigned int stacked_pc;
static unsigned int stacked_psr;
static unsigned int SHCSR;
static unsigned char MFSR;
static unsigned char BFSR;
static unsigned short int UFSR;
static unsigned int HFSR;
static unsigned int DFSR;
static unsigned int MMAR;
static unsigned int BFAR;
stacked_r0 = ((unsigned long) hardfault_args[0]);
stacked_r1 = ((unsigned long) hardfault_args[1]);
stacked_r2 = ((unsigned long) hardfault_args[2]);
stacked_r3 = ((unsigned long) hardfault_args[3]);
stacked_r12 = ((unsigned long) hardfault_args[4]);
stacked_lr = ((unsigned long) hardfault_args[5]);
stacked_pc = ((unsigned long) hardfault_args[6]);
stacked_psr = ((unsigned long) hardfault_args[7]);
SHCSR = (*((volatile unsigned long *)(0xE000ED24)));
MFSR = (*((volatile unsigned char *)(0xE000ED28)));
BFSR = (*((volatile unsigned char *)(0xE000ED29)));
UFSR = (*((volatile unsigned short int *)(0xE000ED2A)));
HFSR = (*((volatile unsigned long *)(0xE000ED2C)));
DFSR = (*((volatile unsigned long *)(0xE000ED30)));
MMAR = (*((volatile unsigned long *)(0xE000ED34)));
BFAR = (*((volatile unsigned long *)(0xE000ED38)));
printf("nn[Hard fault handler - all numbers in hex]nn");
printf("R0 = %xn",stacked_r0);
printf("R1 = %xn",stacked_r1);
printf("R2 = %xn",stacked_r2);
printf("R3 = %xn",stacked_r3);
printf("R12 = %xn",stacked_r12);
printf("LR[R14] = %x subroutine call return addressn",stacked_lr);
printf("PC[R15] = %x program countern",stacked_pc);
printf("PSR = %xn",stacked_psr);
printf("SHCSR = %xn",(*((volatile unsigned long*)(0xE000ED24))));
printf("BFAR = %xn",(*((volatile unsigned long*)(0xE000ED38))));
printf("CFSR = %xn",(*((volatile unsigned long*)(0xE000ED28))));
printf("HFSR = %xn",(*((volatile unsigned long*)(0xE000ED2C))));
printf("DFSR = %xn",(*((volatile unsigned long*)(0xE000ED30))));
printf("AFSR = %xn",(*((volatile unsigned long*)(0xE000ED3C))));
printf("SCB_SHCSR = %xn",SCB->SHCSR);
while (1);
}
③执行程序后,若发生内核错误,则程序会运行到最后while(1);处。此时观察相应的堆栈和故障寄存器值,stacked_lr即为故障发生时进入故障中断前PC的值,在MDK软件调试状态下,假如stacked_lr的值为0x1a002d08,在左下方的命令窗口输入“PC = 0x1a002d08”回车,即可定位发生错误的代码位置。
④根据内核错误状态寄存器的值,对应下面的说明,可以看出是发生了何种内核错误。同样的在Cortex_M3权威指南中可以找到对应的寄存器
STM32进入HardFault_Handler的调试方法的更多相关文章
- stm32进入HardFault_Handler的定位方法
写程序偶尔会遇到程序死机的现象.这个时候,就需要debug来定位. 通常情况下,程序会进入HardFault_Handler的死循环(针对stm32系列),我遇到过两次. 第一次是使用数组之前,数组的 ...
- Cortex-M3/4的Hard Fault调试方法
1 Cortex-M3/4的Fault简介 Cortex-M3/4的Fault异常是由于非法的存储器访问(比如访问0地址.写只读存储位置等)和非法的程序行为(比如除以0等)等造成的.常见的4种异常及产 ...
- STM32进入HardFault_Handler处理办法
STM32进入HardFault_Handler处理办法 HardFault_Handler出现的情况一般有两种: 一种是:数组越界 一种是:堆栈溢出,程序指针指飞 方法一 在中断HardFault_ ...
- FreeRTOS 调试方法(printf---打印任务执行情况)
以下转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家介绍 FreeRTOS 的调试方法,这里的调试方法主要是教会大家如何获取任务的执行情况,通过获取 ...
- keil结合st-link使用SWO的两种调试方法笔记
通过strongerHuang的教程,实现了SWO的两种调试方法, 1.在keil调试的过程中,使用debug printf viewer打印信息, 2.在STM32 ST-LINK Utility中 ...
- 单片机项目中使用新IC芯片的调试方法
前两天,一位小伙伴咨询我一款新IC芯片怎么使用,借此机会我顺便把我日常工作中经常用到的一种调试方法介绍给小伙伴们,希望对对大家有所帮助.准备仓促,文中难免有技术性错误,欢迎大家给予指正,并给出好的建议 ...
- Linux环境下段错误的产生原因及调试方法小结(转)
最近在Linux环境下做C语言项目,由于是在一个原有项目基础之上进行二次开发,而且 项目工程庞大复杂,出现了不少问题,其中遇到最多.花费时间最长的问题就是著名的“段错误”(Segmentation F ...
- linux Ubuntu(Segmentation fault)段错误出现原因及调试方法
在linux下编译了一个程序,尝试运行的时候出现: Segmentation fault (core dumped) 初步确认为...完全不知道是什么玩意. 于是找度娘了. ----------- ...
- kernel启动console_init之前console不可用时发生crash的调试方法
http://code.google.com/p/innosoc/wiki/KernelBootCrashDebug 注: 如在i386_start_kernel中加入:early_printk(&q ...
- Linux环境下段错误的产生原因及调试方法小结
转载自http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html 最近在Linux环境下做C语言项目,由于是在一个原有项目基础之 ...
随机推荐
- Properties集合的使用
Properties集合是唯一一个可以和IO流相结合的集合 可以将集合中的数据持久化存储,也可以将硬盘上的数据加载到该集合中. 1 Properties集合添加.遍历 1 private static ...
- 自己封装的 Python 常用工具库(prestool)
一.安装 需Python 版本建议 3.7 以上 pip install --upgrade prestool 二.常用工具 from prestool.Tool import Tool tool = ...
- nginx适配thinkphp3.2.3
环境 centos7.9 nginx1.23.2 thinkphp3.2.3 PHP7.4.30 配置 配置nginx 默认位置在/usr/local/nginx/conf/nginx.conf主要配 ...
- Python中文件读写操作
1 txt文件 1.1 写操作 import numpy as np def write(fileName,data): file=open(fileName,'w') row,col=data.sh ...
- 解决ufw下pptp客户端连接问题
解决ufw下pptp客户端连接问题 解决ubuntu在启动ufw的情况下pptp客户端无法链接的问题. 修改/etc/ufw/before.rules 在COMMIT之前添加如下内容: -A ufw- ...
- Ubuntu在无网络环境下,用离线源apt-get安装软件
步骤概要如下: 1.假设目标安装的是服务器A,需先准备一台正常环境,且操作系统版本与A一致的服务器B: 2.用apt-get在服务器B上下载需要安装的包,并用dpkg-scanpackages依赖打包 ...
- 用ELK分析每天4亿多条腾讯云MySQL审计日志(3)--下载日志
当初分析日志,麻烦的是腾讯云的SQL审计日志下载,有下列限制: 1,单次最多1000万条下载 2,单个实例最多生成5条日志文件,多的要先删除以前文件才能生成 腾讯云日志文件生成界面: 一 ...
- scrcpy-Android投屏神器
介绍 scrcpy 是免费开源的投屏软件,支持将安卓手机屏幕投放在 Windows.macOS.GNU/Linux 上,并可直接借助鼠标在投屏窗口中进行交互和录制. 下载scrcpy 解压. http ...
- Java I/O 教程(五) BufferedOutputStream 类
Java BufferedOutputStream Class Java BufferedOutputStream class 用于缓冲一个输出流 其内部使用缓冲区存储数据,可以更有效率的往流中写入数 ...
- Divisors of the Divisors of an Integer题解
Divisors of the Divisors of an Integer 题意:定义d[x]为x的因子个数,sndd[y]为y的因子的因子个数和. 思路:任意一个大于一的数,都可以分解为若干个质数 ...