前言:
针对RISC-V芯片进入HardFault_Handler函数的问题排查提供讲解。
一、HardFault函数添加PC指针打印
在公共文件的sys.c函数中找到函数并修改如下:
__INTERRUPT
__HIGH_CODE
__attribute__((weak))
void HardFault_Handler(void)
{
uint32_t v_mepc,v_mcause,v_mtval;
printf("hardfault\n");
v_mepc=__get_MEPC();
v_mcause=__get_MCAUSE();
v_mtval=__get_MTVAL();
printf("mepc:%08x\n",v_mepc);
printf("mcause:%08x\n",v_mcause);
printf("mtval:%08x\n",v_mtval);
#if 0
FLASH_ROM_SW_RESET();
sys_safe_access_enable();
R16_INT32K_TUNE = 0xFFFF;
sys_safe_access_disable();
sys_safe_access_enable();
R8_RST_WDOG_CTRL |= RB_SOFTWARE_RESET;
sys_safe_access_disable();
#endif
while(1);
}
二、使用蓝牙工程时可以进一步添加监控函数
__INTERRUPT
__HIGH_CODE
__attribute__((weak))
void HardFault_Handler(void)
{
struct __MEMORY_CTL
{
struct __MEMORY_CTL * pNext;
uint16_t len;
uint16_t used;
};
typedef struct __MEMORY_CTL MemoryCtl;
extern MemoryCtl * MemCtlStart;
extern MemoryCtl * MemCtlEnd;
MemoryCtl * MemHead;
MemHead = MemCtlStart;
while( MemHead != MemCtlEnd ){
printf("|%8x,%8x,%8d.....\n",MemHead->used,(uint32_t)MemHead,\
(uint32_t)(MemHead->pNext) -(uint32_t)MemHead - sizeof(struct __MEMORY_CTL));
MemHead = MemHead->pNext;
}printf("\n");
uint32_t v_mepc,v_mcause,v_mtval;
printf("hardfault\n");
v_mepc=__get_MEPC();
v_mcause=__get_MCAUSE();
v_mtval=__get_MTVAL();
printf("mepc:%08x\n",v_mepc);
printf("mcause:%08x\n",v_mcause);
printf("mtval:%08x\n",v_mtval);
#if 0
FLASH_ROM_SW_RESET();
sys_safe_access_enable();
R16_INT32K_TUNE = 0xFFFF;
sys_safe_access_disable();
sys_safe_access_enable();
R8_RST_WDOG_CTRL |= RB_SOFTWARE_RESET;
sys_safe_access_disable();
#endif
while(1);
}
注意:监控函数和PC指针可以同时使用。监控函数需要在蓝牙工程中使用才有意义,如果打印值为空,检查是否使用蓝牙。
附录:
PC指针打印信息排查可以参考青稞V4手册:QingKeV4_Processor_Manual.PDF - 南京沁恒微电子股份有限公司 (wch.cn)
标签:__,Hardfault,芯片,safe,mtval,sys,MemHead,CH58x,printf From: https://www.cnblogs.com/gscw/p/18159920