概述
当我用STM32F407ZGT6变量进行任何操作时,我的float出现了一个奇怪的问题。Infinite_Loop默认处理程序会被调用,而我会被卡住。我正在使用STM32CubeIDE。以下内容足以造成STM32CubeIDE崩溃:
int main(void)
{
float my_float = 2.5;
for(;;)
{
my_float += 1; //Crashes here
}
}详细信息
失败时,调试堆栈显示:
Thread #1 [main] 1 [core: 0] (Suspended : Step)
WWDG_IRQHandler() at C:\~\Documents\stm32\LM_STM32\Startup\startup_stm32f407zgtx.s:115 0x8000f80
<signal handler called>() at 0xfffffff9
main() at C:\Users\Pesquisa2\Documents\stm32\LM_STM32\Src\main.c:60 0x8000d8e
arm-none-eabi-gdb (10.2.90.20210621)
ST-LINK (ST-LINK GDB server) 反汇编错误消息似乎控制堆栈溢出(?):
fffffff9: Failed to execute MI command:
-data-disassemble -s 4294967289 -e 4294967437 -- 3
Error message from debugger back end:
Cannot access memory at address 0xfffffffe我试过查看STM浮点演示,但是在使用float变量之前,示例并没有做任何特别的事情.起初,我以为只有在尝试像其他人一样体验[2][3.][4.][5][6][7][8],时才会尝试printf,但现在很明显,问题在于float本身。
我做错什么了?我需要图书馆什么的吗??
设置
MCU设置:
Floating-point uinit: FPv4-SP-D16
Floating-point ABI: Hardware implementation (-mfloat-abi=hard)
Instruction set: Thumb2
Runtime library: Reduced C (--specs=nano.specs)
[x] -u_printf_float
[x] -u_scanf_floatGCC汇编程序选项:
-mcpu=cortex-m4 -g3 -DDEBUG -c -x assembler-with-cpp --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumbGCC编译选项:
--mcpu=cortex-m4 -std=gnu11 -g3 -DDEBUG -DSTM32 -DSTM32F4 -DSTM32F407ZGTx -c -I"C:\Users\Pesquisa2\STM32Cube\Repository\STM32Cube_FW_F4_V1.27.0\Drivers\CMSIS\Include" -I"C:\Users\Pesquisa2\STM32Cube\Repository\STM32Cube_FW_F4_V1.27.0\Drivers\CMSIS\Device\ST\STM32F4xx\Include" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\Inc\App" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\Inc\HAL" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\Inc\Midware" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\Src\App" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\Src\HAL" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\Src\Midware" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\Inc" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\FATFS" -I"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\mma845x_inc" -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumbGCC链接选项:
-mcpu=cortex-m4 -T"C:\Users\Pesquisa2\Documents\stm32\LM_STM32\STM32F407ZGTX_FLASH.ld" --specs=nosys.specs -Wl,-Map="${BuildArtifactFileBaseName}.map" -Wl,--gc-sections -static --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -u _printf_float -u _scanf_float -Wl,--start-group -lc -lm -Wl,--end-group发布于 2022-07-26 20:25:20
这很简单。在执行任何FP操作之前,需要启用FPU。否则,您将启动HardFault,并将在空异常处理程序中结束。由于您可能没有编写不同的处理程序,所以只有一个处理程序(编译器将丢弃相同的处理程序,处理程序的名称将具有误导性)。
如果使用Cube,请将__FPU_PRESENT nad __FPU_USED宏设置为1
或者简单地启用FPU
SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); 顺便说一句,执行您的代码只是因为您没有启用任何优化。我建议调试-Og。
int main(void)
{
SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));
volatile float my_float;
my_float = 2.5f;
for(;;)
{
my_float += 1; //Crashes here
}
}还请记住,2.5不是浮点,而是double。您需要使用'f‘后缀,否则编译器将调用转换代码。
float my_float = 2.5f;https://stackoverflow.com/questions/73129308
复制相似问题