在有效除法运算过程中发生了硬故障

编程语言 2026-07-11

调用 __udivsi3 时出现硬故障。更具体地说,当跳转到 __udivsi3 时,程序会再跳两次、三次,然后尝试在0x992处执行 .word 0xff1ee8bd,在我的控制器MSPM0C1104上该地址位于ROM/闪存区域。由此触发硬故障。

volatile uint32_t test; test = 24000000; test /= 24000000; 实际上这些值是来自控制器库的可配置宏/变量。我也可以用其他方程式复现这个错误。
为记录起见,我在该函数上使用 __attribute__((optimize("-O0"))),但问题也出现在 -O2 上。

链接器文件内存区段:

    FLASH           (RX)  : ORIGIN = 0x00000000, LENGTH = 0x00003BFF
    SRAM            (RWX) : ORIGIN = 0x20000000, LENGTH = 0x00000400
    DEVICE_CONFIG   (RW!x) : ORIGIN = 0x00003C00, LENGTH = 0x000001FF
    BCR_CONFIG      (R)   : ORIGIN = 0x41C00000, LENGTH = 0x000000FF

在使用GCC ARM库时,写操作为何会被放入闪存?更重要的是,如何将它们移动到RAM?

编译器设置:

"C:/TI/gcc_arm_none_eabi_9_2_1/bin/arm-none-eabi-gcc-9.2.1.exe" 
-c 
@"device.opt" 
-mcpu=cortex-m0plus 
-march=armv6-m 
-mthumb 
-mfloat-abi=soft 
-I"C:/TI/mspm0_sdk_2_09_00_01/source/third_party/CMSIS/Core/Include" 
-I"C:/TI/mspm0_sdk_2_09_00_01/source" 
-I"C:/MYPROJ/src" 
-I"C:/MYPROJ/src/modules" 
-I"C:/MYPROJ/src/modules/comp1" 
-I"C:/MYPROJ/src/modules/communication" 
-I"C:/MYPROJ/src/modules/mylib" 
-I"C:/MYPROJ/src/modules/battery" 
-I"C:/MYPROJ/src/modules/accelerometer" 
-I"C:/MYPROJ" 
-I"C:/MYPROJ/Debug" 
-O2 
-ffunction-sections 
-fdata-sections 
-gdwarf-3 
-gstrict-dwarf 
-Wall 
-fno-math-errno 
-MMD 
-MP 
-MF"src/modules/communication/communication.d_raw" 
-MT"src/modules/communication/communication.o" 
-std=c99 
@"./device.opt" 
-o"src/modules/communication/communication.o" 
"../src/modules/communication/communication.c"

链接器设置:

"C:/TI/gcc_arm_none_eabi_9_2_1/bin/arm-none-eabi-gcc-9.2.1.exe" 
@"device.opt" 
-O2
-ffunction-sections 
-fdata-sections 
-gdwarf-3
-gstrict-dwarf 
-Wall
-fno-math-errno 
-mthumb
-mfloat-abi=soft 
-Wl,-Map,"myproj.map" 
-nostartfiles 
-nostdlib
-static 
-Wl,--gc-sections 
-L"C:/TI/mspm0_sdk_2_09_00_01/source/ti/driverlib/lib/gcc/m0p/mspm0c110x" 
-L"C:/TI/mspm0_sdk_2_09_00_01/source" 
-L"C:/MYPROJ" 
-L"C:/MYPROJ/Debug/syscfg" 
-L"C:/TI/gcc_arm_none_eabi_9_2_1/lib/gcc/arm-none-eabi/9.2.1" 
-L"C:/TI/gcc_arm_none_eabi_9_2_1/arm-none-eabi/lib/thumb/v6-m/nofp" 
-march=armv6-m 
-mthumb
--specs=nano.specs 
-o"myproj.out" 
"./ti_msp_dl_config.o" 
"./startup_mspm0c110x_gcc.o" 
"./main.o" 
"./src/modules/accelerometer/accelerometer.o" 
"./src/modules/battery/battery.o" 
"./src/modules/mylib/communication/communication.o" 
"./src/modules/rear_light/comp1.o" 
-Wl,-T"../make/myproj.lds" 
-Wl,-Tdevice.lds.genlibs 
-lm
-lgcc 
-lc 
-l:driverlib.a

main

__udivsi3

hard fault source

 .text          0x000004e8      0x114 C:/TI/gcc_arm_none_eabi_9_2_1/lib/gcc/arm-none-eabi/9.2.1\libgcc.a(_udivsi3.o)
                0x000004e8                __aeabi_uidiv
                0x000004e8                __udivsi3
// controller configuration header. Example works even without explicit include.
#include "ti_msp_dl_config.h"

#include <stdint.h>

int main (void)
{
    volatile uint32_t test;  

    test = 24000000;
    test /= 100; // after this line the hard fault occurs
    while(1);
}

解决方案

我使用的 libgcc.a 版本是错误的。正确的路径是 "C:\TI\gcc_arm_none_eabi_9_2_1\lib\gcc\arm-none-eabi\9.2.1\thumb\v6-m\nofp\libgcc.a"

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章