STM32通过rt_hw_hard_fault_exception中的LR寄存器追溯程序问题

1. 问题现象

程序运行导致rt_hw_hard_fault_exception

如图

显示错误相关代码

c 复制代码
struct exception_stack_frame
{
    uint32_t r0;
    uint32_t r1;
    uint32_t r2;
    uint32_t r3;
    uint32_t r12;	
    uint32_t lr;	 // 链接寄存器 (LR)
    uint32_t pc;	// 程序计数器 (PC)
    uint32_t psr;	// 程序状态寄存器 (PSR)
};

void rt_hw_hard_fault_exception(struct exception_stack_frame *contex)
{
	Fm24cl64bInit();
//	static struct exception_stack_frame test;
	static CoreRegister_Type s_error_save;
//	test = *contex;   
	s_error_save.r0 = contex->r0;
	s_error_save.r1 = contex->r1;
	s_error_save.r2 = contex->r2;
	s_error_save.r3 = contex->r3;
	s_error_save.r12 = contex->r12;
	s_error_save.lr = contex->lr;
	s_error_save.pc = contex->pc;
	s_error_save.psr = contex->psr;
	uint8_t save_res = WriteCoreRegisterToEeprom(&s_error_save);
	
	lcd_Init();
	lcd_Init_PowerOn();
	lcd_Init_BackLight(1);
	lcd_BufferClear();
    lcd_BufferPrint(0, 0, 16, "R0:0x%08X  R1:0x%08X", s_error_save.r0, s_error_save.r1);
    lcd_BufferPrint(0, 2, 16, "R2:0x%08X  R3:0x%08X", s_error_save.r2, s_error_save.r3);
    lcd_BufferPrint(0, 4, 16, "R12:0x%08X LR:0x%08X", s_error_save.r12, s_error_save.lr);
    lcd_BufferPrint(0, 6, 16, "PC:0x%08X PSR:0x%08X", s_error_save.pc, s_error_save.psr);
	lcd_BufferPrint(0, 8, 16, "save_res:%d", save_res);
	lcd_BufferPrint(0, 10, 16, "Dst:%d 1st:%d 2st:%d", msg.MsgDstId, msg.FirstSubId, msg.SecondSubId);
	
	lcd_ScreenRefresh();
	
	while(1);
}

2. 问题分析

在ARM Cortex-M架构中,LR (Link Register, R14) 的值在HardFault或其他异常发生时,对追溯问题根源非常关键。

LR---保存函数返回地址

3. 问题追溯

1. 定位触发HardFault的代码位置

通过 LR:0x08009B47 找到对应的代码位置

使用调试器(如ST-Link + IDE)直接跳转到该地址。

​​

2. 分析LR地址附近的代码

常见触发HardFault的操作:

  • 访问非法地址(野指针、数组越界)。
  • 未对齐的内存访问(如对非4字节对齐地址执行LDR)。
  • 堆栈溢出(检查线程栈大小是否足够)。
  • 错误的函数指针调用(如跳转到无效地址)

这里的LPTIM1_IRQ_callback是个空指针,最终导致硬件错误。

相关推荐
e***74956 分钟前
SpringBoot项目集成ONLYOFFICE
java·spring boot·后端
qq_3363139313 分钟前
java基础-常用的API
java·开发语言
百锦再18 分钟前
第21章 构建命令行工具
android·java·图像处理·python·计算机视觉·rust·django
极光代码工作室24 分钟前
基于SpringBoot的校园招聘信息管理系统的设计与实现
java·前端·spring
未若君雅裁34 分钟前
斐波那契数列 - 动态规划实现 详解笔记
java·数据结构·笔记·算法·动态规划·代理模式
断剑zou天涯35 分钟前
【算法笔记】从暴力递归到动态规划(三)
java·算法·动态规划
断剑zou天涯38 分钟前
【算法笔记】从暴力递归到动态规划(一)
java·算法·动态规划
Ace_317508877643 分钟前
微店平台关键字搜索接口深度解析:从 Token 动态生成到多维度数据挖掘
java·前端·javascript
天天爱吃肉82181 小时前
智能网联汽车信息安全深度解析:从UN-R155与GB44495标准到OBD/UDS技术实践
网络·嵌入式硬件·汽车
yyt3630458411 小时前
Maven 命令构建成功但 IDEA 构建失败原因解析
java·maven·intellij-idea