Fork me on GitHub

随笔 - 997  文章 - 5  评论 - 181  阅读 - 300万 

   在编写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权威指南中可以找到对应的寄存器

 

posted on   虚生  阅读(1105)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
历史上的今天:
2021-10-25 无线智能录音工牌解决方案技术解析
2018-10-25 Python中parameters与argument区别
2018-10-25 神经网络架构PYTORCH-初相识(3W)
2018-10-25 神经网络架构pytorch-MSELoss损失函数
2018-10-25 ubuntu16.04 下鼠标变成十字形状无法点击情况解决
2017-10-25 使用speex动态链接库过程中遇到问题及解决方法
2016-10-25 ssh配置文件及问题解决
点击右上角即可分享
微信分享提示