繁体   English   中英

浮点运算调用Infinite_Loop STM32CubeIDE

[英]Float operation calls Infinite_Loop STM32CubeIDE

概述

在尝试使用float变量进行任何操作时,我的STM32F407ZGT6遇到了一个奇怪的问题。 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 Floating point demonstration ,但这些示例在使用float变量之前没有做任何特别的事情......起初,我认为它只是在尝试printf时像其他人一样经历过 [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ][ 7 ][ 8 ],但现在很明显问题出在float本身。

我做错了什么? 我需要图书馆什么的吗???


设置

单片机设置:

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_float

GCC 汇编器选项:

-mcpu=cortex-m4 -g3 -DDEBUG -c -x assembler-with-cpp --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb

GCC 编译器选项:

--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 -mthumb

GCC Linker 选项:

-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

这很简单。 在进行任何 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不仅仅是float只有double 你需要使用'f'后缀,否则编译器会调用转换代码。

float my_float = 2.5f;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM