使用OpenOCD调试STM32F103C8T6 Flash启动模式时触发HardFault_Handler故障的解决求助
OpenOCD调试STM32F103C8T6 Flash启动模式时触发HardFault_Handler故障的解决求助
各位大佬好,我最近遇到一个头疼的问题,先跟大家说下我的开发环境:
- Windows10
- Clion2023.3.6
- arm-gnu-toolchain-13.2.Rel1-mingw-w64-i686-arm-none-eabi
- mingw64(Clion自带版本)
- openocd-v0.12.0-i686-w64-mingw32
- Jlink(V12)
- STM32F103C8T6
我已经把Clion的各项配置都弄好了,代码编译完全正常,而且SRAM启动模式(BOOT0设为1)下能正常调试,但切换到主Flash启动(BOOT0设为0)时,程序直接触发异常挂掉了。
我感觉问题应该不是代码本身的问题——毕竟特定代码能稳定复现这个故障,但我就是搞不清楚到底哪里出了问题,甚至程序连时钟配置都没完成就挂了,根本到不了main函数。
下面是OpenOCD的调试日志:
Info : J-Link V12 compiled Oct 9 2024 10:52:31 Info : Hardware version: 12.00 Info : VTarget = 3.212 V Info : clock speed 1000 kHz Info : SWD DPIDR 0x2ba01477 Info : [stm32f1x.cpu] Cortex-M3 r2p1 processor detected Info : [stm32f1x.cpu] target has 6 breakpoints, 4 watchpoints Info : starting gdb server for stm32f1x.cpu on 3333 Info : Listening on port 3333 for gdb connections [stm32f1x.cpu] halted due to debug-request, current mode: Thread xPSR: 0x01000000 pc: 0x08000198 msp: 0x20010000 ** Programming Started ** Info : device id = 0x20036410 Info : flash size = 128 KiB Warn : Adding extra erase range, 0x080003d0 .. 0x080003ff ** Programming Finished ** (((READY))) Info : tcl server disabled Info : Listening on port 4444 for telnet connections Info : acceptDebugger connected to tcp:localhost:3333 ing 'gdb' connection on tcp/3333 [stm32f1x.cpu] halted due to debug-request, current mode: Handler HardFault xPSR: 0x61000003 pc: 0x08000366 msp: 0x2000ffd4 Warn : Prefer GDB command "target extended-remote :3333" instead of "target remote :3333" Signal: SIGINT (Interrupt) Info : SWD DPIDR 0x2ba01477 Error: Failed to read memory at 0x2000fff8 Info : SWD DPIDR 0x2ba01477 Error: Failed to read memory at 0x2000ffc4
程序最终会卡在HardFault处理函数里:
/** * @brief This function handles Hard Fault exception. * @param None * @retval None */ void HardFault_Handler(void) { /* Go to infinite loop when Hard Fault exception occurs */ while (1) { } }
我的main函数是空的,因为程序根本到不了这里——甚至在调用SystemInit的时候就触发了硬件错误,启动代码如下:
Reset_Handler: movs r1, #0 b LoopCopyDataInit CopyDataInit: ldr r3, =_sidata ldr r3, [r3, r1] str r3, [r0, r1] adds r1, r1, #4 LoopCopyDataInit: ldr r0, =_sdata ldr r3, =_edata adds r2, r0, r1 cmp r2, r3 bcc CopyDataInit ldr r2, =_sbss b LoopFillZerobss FillZerobss: movs r3, #0 str r3, [r2], #4 LoopFillZerobss: ldr r3, = _ebss cmp r2, r3 bcc FillZerobss bl SystemInit //**This is usually where a hardware error occurs** bl __libc_init_array bl main bx lr .size Reset_Handler, .-Reset_Handler int main(void) { while(1){ } }
补充一下:我用Keil调试的时候,Flash启动模式完全正常,目前还没试过用ST-Link来调试。
现在想请教大家,怎么解决这个问题,让程序能正常在Flash启动模式下调试和烧录?希望能给我一些排查思路或者具体的解决建议,非常感谢!
备注:内容来源于stack exchange,提问作者njki
相关产品推荐
相关产品推荐

