C++ 初始化器上的 Cortex M4 硬故障
Cortex M4 Hard Fault on C++ Initializer
我有一个很奇怪的问题。在我看来,这是一个已知问题,但我找不到任何实际的解决方案,也找不到任何可靠的解释。
这是我的设置:
主机: Win7 电脑,内存充足。
目标: STM32F303RE Cortex M4 @ 64 MHz,在带集成 ST-LINK 2.1
的 Nucleo32 板上
工具链:uVision V5.16
预处理器符号: USE_STDPERIPH_DRIVER,STM32F303xE,__CPLUSPLUS__FPU_PRESENT,__FPU_USED
杂项控制: --C99 --cpp
一切都很好。关闭所有优化。
问题出在 运行 时间。在一些 C++ 对象实例化之后,处理器最终进入硬故障处理程序。具体在哪里?我通过拆分我的代码片段来标记这一点。现在,我对 C++ 细节和内部工作原理有点陌生,但听起来我和这个家伙有同样的问题:Segmentation fault caused/avoided by changing source file order in Makefile
而且,解释不清楚的解决方案。也许这不是错误的 C++ 实例化。虽然没有找到解决方案。这是我的程序的要点,它演示了这个问题:代码的第一部分 "appears" 到 运行 好吧,直到我分离出来供您注意的部分块。
#include "main.h"
#include "stm32f30x.h"
#include "stdint.h"
#include "stdio.h"
#include "string.h"
#include "math.h"
#include "usart.h"
#include "can.h"
#include "utils.h"
#include "led.h"
#include "i2c.h"
#include "sabertooth.h"
#include "FuzzyRule.h"
#include "FuzzyComposition.h"
#include "Fuzzy.h"
#include "FuzzyRuleConsequent.h"
#include "FuzzyOutput.h"
#include "FuzzyInput.h"
#include "FuzzyIO.h"
#include "FuzzySet.h"
#include "FuzzyRuleAntecedent.h"
Fuzzy* fuzzy = new Fuzzy();
int main(void)
{
/*************************************
* Input 1
************************************/
// Two "crossing ramp" sets for rowWidth i.e. "tolerance" of the row
FuzzyInput* rowWidth = new FuzzyInput(1);
FuzzySet* lowTolerance = new FuzzySet(0.0f, 0.0f, 0.0f, 120.0f);
rowWidth->addFuzzySet(lowTolerance);
FuzzySet* highTolerance = new FuzzySet(0.0f, 120.0f, 120.0f, 120.0f);
rowWidth->addFuzzySet(highTolerance);
fuzzy->addFuzzyInput(rowWidth);
USART1_puts("row width added as fuzzy input..");
/*************************************
* Input 2
************************************/
// Five Sets for "difference between R and L distances"
FuzzyInput* distDiff = new FuzzyInput(2);
FuzzySet* tooFarRight = new FuzzySet(-60.0f, -60.0f, -54.0f, -30.0f);
distDiff->addFuzzySet(tooFarRight);
FuzzySet* right = new FuzzySet(-54.0f, -30.0f, -30.0f, 0.0f);
distDiff->addFuzzySet(right);
FuzzySet* centered = new FuzzySet(-30.0f, 0.0f, 0.0f, 30.0f);
distDiff->addFuzzySet(centered);
FuzzySet* left = new FuzzySet(0.0f, 30.0f, 30.0f, 54.0f);
distDiff->addFuzzySet(left);
FuzzySet* tooFarLeft = new FuzzySet(30.0f, 54.0f, 60.0f, 60.0f);
distDiff->addFuzzySet(tooFarLeft);
fuzzy->addFuzzyInput(distDiff);
USART1_puts("centering dist added as fuzzy input...");
/*************************************
* Output 1
************************************/
FuzzyOutput* motorSpeedDiff = new FuzzyOutput(1);
// Seven sets for steering modes to take (close ones narrow far ones wider)
FuzzySet* hardRight = new FuzzySet(-30.0f, -30.0f, -30.0f, -15.0f);
motorSpeedDiff->addFuzzySet(hardRight);
USART1_puts("\thardRight");
FuzzySet* lightRight = new FuzzySet(-15.0f, -5.0f, -5.0f, 0.0f);
motorSpeedDiff->addFuzzySet(lightRight);
USART1_puts("\tlightRight");
这是我在终端看到的最后一条串行消息"lightRight"
在此行下方调用 new FuzzySet() 时发生硬故障。
FuzzySet* nomRight = new FuzzySet(-30.0f, -15.0f, -15.0f, -5.0f);
motorSpeedDiff->addFuzzySet(nomRight);
USART1_puts("\tnomRight");
FuzzySet* lightLeft = new FuzzySet(0.0f, 5.0f, 5.0f, 15.0f);
motorSpeedDiff->addFuzzySet(lightLeft);
USART1_puts("\tlightLeft");
FuzzySet* goStraight = new FuzzySet(-5.0f, 0.0f, 0.0f, 5.0f);
motorSpeedDiff->addFuzzySet(goStraight);
USART1_puts("\tgoStraight");
FuzzySet* nomLeft = new FuzzySet(5.0f, 15.0f, 15.0f, 30.0f);
motorSpeedDiff->addFuzzySet(nomLeft);
USART1_puts("\tnomLeft");
FuzzySet* hardLeft = new FuzzySet(15.0f, 30.0f, 30.0f, 30.0f);
motorSpeedDiff->addFuzzySet(hardLeft);
USART1_puts("\thardLeft");
fuzzy->addFuzzyOutput(motorSpeedDiff);
USART1_puts("motor steering added as fuzzy output");
lotsMoreSetupCode();
while(1)
{
USART1_puts("Done!");
stop(1); // Blink LED forever
}
}
很明显,我只是做了一堆这样的模糊集,每个都是 4 个浮点数的集合,但是某处有一个 le-wild 指针飞过?
这是在 FuzzySet.cpp 中找到的构造函数:(我没有编写的模糊逻辑库的一部分)这个相同的程序 运行 在 Arduino 上很好,但不是这个处理器。编译器差异?
</p>
<pre><code>FuzzySet::FuzzySet(){
}
FuzzySet::FuzzySet(float a, float b, float c, float d){
this->a = a;
this->b = b;
this->c = c;
this->d = d;
this->pertinence = 0.0;
}
这听起来像是与在其他上下文中被其他静态声明的函数访问的静态变量有关。
但是我没有声明静态的东西。
堆栈交换中的某人 link 说这可能是 linker 的错误。你同意?如何修复?
对实际发生的事情有什么想法吗?
我已经设置了一个很好的硬故障处理程序来打印寄存器信息,但显然错误甚至发生在 main() 之前。所以当事情变得混乱时去汇编代码似乎没有用。
如何修复这个?感谢您的 C++ 专业知识!
根据您在 startup_stm32fxxx.s 文件
中使用的微控制器,尝试更改代码的堆和堆栈大小
这是例子
;******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
;* File Name : startup_stm32f407xx.s
;* Author : MCD Application Team
;* Version : V2.4.3
;* Date : 22-January-2016
;* Description : STM32F407xx devices vector table for MDK-ARM toolchain.
;* This module performs:
;* - Set the initial SP
;* - Set the initial PC == Reset_Handler
;* - Set the vector table entries with the exceptions ISR address
;* - Branches to __main in the C library (which eventually
;* calls main()).
;* After Reset the CortexM4 processor is in Thread mode,
;* priority is Privileged, and the Stack is set to Main.
;* <<< Use Configuration Wizard in Context Menu >>>
;*******************************************************************************
;
;* Redistribution and use in source and binary forms, with or without modification,
;* are permitted provided that the following conditions are met:
;* 1. Redistributions of source code must retain the above copyright notice,
;* this list of conditions and the following disclaimer.
;* 2. Redistributions in binary form must reproduce the above copyright notice,
;* this list of conditions and the following disclaimer in the documentation
;* and/or other materials provided with the distribution.
;* 3. Neither the name of STMicroelectronics nor the names of its contributors
;* may be used to endorse or promote products derived from this software
;* without specific prior written permission.
;*
;* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
;* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
;* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
;* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
;* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
;* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;
;*******************************************************************************
; Amount of memory (in bytes) allocated for Stack
; Tailor this value to your application needs
; <h> Stack Configuration
; <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>
; </h>
Stack_Size EQU 0x200 ; <your stack size>
AREA STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem SPACE Stack_Size
__initial_sp
; <h> Heap Configuration
; <o> Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>
; </h>
Heap_Size EQU 0x400 ; <your heap size>
AREA HEAP, NOINIT, READWRITE, ALIGN=3
__heap_base
Heap_Mem SPACE Heap_Size
__heap_limit
PRESERVE8
THUMB
; Vector Table Mapped to Address 0 at Reset
AREA RESET, DATA, READONLY
EXPORT __Vectors
EXPORT __Vectors_End
EXPORT __Vectors_Size
堆栈的值由那行设置
Stack_Size EQU 0x200 ; <your stack size>
和堆
Heap_Size EQU 0x400 ; <your heap size>
在 Keil uVision 中,左下角还有 2 个选项卡,您可以在其中切换 "Text Editor" 和 "Config Wizard" 以更改代码堆和堆栈大小。
我有一个很奇怪的问题。在我看来,这是一个已知问题,但我找不到任何实际的解决方案,也找不到任何可靠的解释。
这是我的设置:
主机: Win7 电脑,内存充足。
目标: STM32F303RE Cortex M4 @ 64 MHz,在带集成 ST-LINK 2.1
的 Nucleo32 板上
工具链:uVision V5.16
预处理器符号: USE_STDPERIPH_DRIVER,STM32F303xE,__CPLUSPLUS__FPU_PRESENT,__FPU_USED
杂项控制: --C99 --cpp
一切都很好。关闭所有优化。
问题出在 运行 时间。在一些 C++ 对象实例化之后,处理器最终进入硬故障处理程序。具体在哪里?我通过拆分我的代码片段来标记这一点。现在,我对 C++ 细节和内部工作原理有点陌生,但听起来我和这个家伙有同样的问题:Segmentation fault caused/avoided by changing source file order in Makefile
而且,解释不清楚的解决方案。也许这不是错误的 C++ 实例化。虽然没有找到解决方案。这是我的程序的要点,它演示了这个问题:代码的第一部分 "appears" 到 运行 好吧,直到我分离出来供您注意的部分块。
#include "main.h" #include "stm32f30x.h" #include "stdint.h" #include "stdio.h" #include "string.h" #include "math.h" #include "usart.h" #include "can.h" #include "utils.h" #include "led.h" #include "i2c.h" #include "sabertooth.h" #include "FuzzyRule.h" #include "FuzzyComposition.h" #include "Fuzzy.h" #include "FuzzyRuleConsequent.h" #include "FuzzyOutput.h" #include "FuzzyInput.h" #include "FuzzyIO.h" #include "FuzzySet.h" #include "FuzzyRuleAntecedent.h" Fuzzy* fuzzy = new Fuzzy(); int main(void) { /************************************* * Input 1 ************************************/ // Two "crossing ramp" sets for rowWidth i.e. "tolerance" of the row FuzzyInput* rowWidth = new FuzzyInput(1); FuzzySet* lowTolerance = new FuzzySet(0.0f, 0.0f, 0.0f, 120.0f); rowWidth->addFuzzySet(lowTolerance); FuzzySet* highTolerance = new FuzzySet(0.0f, 120.0f, 120.0f, 120.0f); rowWidth->addFuzzySet(highTolerance); fuzzy->addFuzzyInput(rowWidth); USART1_puts("row width added as fuzzy input.."); /************************************* * Input 2 ************************************/ // Five Sets for "difference between R and L distances" FuzzyInput* distDiff = new FuzzyInput(2); FuzzySet* tooFarRight = new FuzzySet(-60.0f, -60.0f, -54.0f, -30.0f); distDiff->addFuzzySet(tooFarRight); FuzzySet* right = new FuzzySet(-54.0f, -30.0f, -30.0f, 0.0f); distDiff->addFuzzySet(right); FuzzySet* centered = new FuzzySet(-30.0f, 0.0f, 0.0f, 30.0f); distDiff->addFuzzySet(centered); FuzzySet* left = new FuzzySet(0.0f, 30.0f, 30.0f, 54.0f); distDiff->addFuzzySet(left); FuzzySet* tooFarLeft = new FuzzySet(30.0f, 54.0f, 60.0f, 60.0f); distDiff->addFuzzySet(tooFarLeft); fuzzy->addFuzzyInput(distDiff); USART1_puts("centering dist added as fuzzy input..."); /************************************* * Output 1 ************************************/ FuzzyOutput* motorSpeedDiff = new FuzzyOutput(1); // Seven sets for steering modes to take (close ones narrow far ones wider) FuzzySet* hardRight = new FuzzySet(-30.0f, -30.0f, -30.0f, -15.0f); motorSpeedDiff->addFuzzySet(hardRight); USART1_puts("\thardRight"); FuzzySet* lightRight = new FuzzySet(-15.0f, -5.0f, -5.0f, 0.0f); motorSpeedDiff->addFuzzySet(lightRight); USART1_puts("\tlightRight");
这是我在终端看到的最后一条串行消息"lightRight" 在此行下方调用 new FuzzySet() 时发生硬故障。
FuzzySet* nomRight = new FuzzySet(-30.0f, -15.0f, -15.0f, -5.0f); motorSpeedDiff->addFuzzySet(nomRight); USART1_puts("\tnomRight"); FuzzySet* lightLeft = new FuzzySet(0.0f, 5.0f, 5.0f, 15.0f); motorSpeedDiff->addFuzzySet(lightLeft); USART1_puts("\tlightLeft"); FuzzySet* goStraight = new FuzzySet(-5.0f, 0.0f, 0.0f, 5.0f); motorSpeedDiff->addFuzzySet(goStraight); USART1_puts("\tgoStraight"); FuzzySet* nomLeft = new FuzzySet(5.0f, 15.0f, 15.0f, 30.0f); motorSpeedDiff->addFuzzySet(nomLeft); USART1_puts("\tnomLeft"); FuzzySet* hardLeft = new FuzzySet(15.0f, 30.0f, 30.0f, 30.0f); motorSpeedDiff->addFuzzySet(hardLeft); USART1_puts("\thardLeft"); fuzzy->addFuzzyOutput(motorSpeedDiff); USART1_puts("motor steering added as fuzzy output"); lotsMoreSetupCode(); while(1) { USART1_puts("Done!"); stop(1); // Blink LED forever } }
很明显,我只是做了一堆这样的模糊集,每个都是 4 个浮点数的集合,但是某处有一个 le-wild 指针飞过?
这是在 FuzzySet.cpp 中找到的构造函数:(我没有编写的模糊逻辑库的一部分)这个相同的程序 运行 在 Arduino 上很好,但不是这个处理器。编译器差异?
</p>
<pre><code>FuzzySet::FuzzySet(){
}
FuzzySet::FuzzySet(float a, float b, float c, float d){
this->a = a;
this->b = b;
this->c = c;
this->d = d;
this->pertinence = 0.0;
}
这听起来像是与在其他上下文中被其他静态声明的函数访问的静态变量有关。
但是我没有声明静态的东西。
堆栈交换中的某人 link 说这可能是 linker 的错误。你同意?如何修复?
对实际发生的事情有什么想法吗?
我已经设置了一个很好的硬故障处理程序来打印寄存器信息,但显然错误甚至发生在 main() 之前。所以当事情变得混乱时去汇编代码似乎没有用。
如何修复这个?感谢您的 C++ 专业知识!
根据您在 startup_stm32fxxx.s 文件
中使用的微控制器,尝试更改代码的堆和堆栈大小这是例子
;******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
;* File Name : startup_stm32f407xx.s
;* Author : MCD Application Team
;* Version : V2.4.3
;* Date : 22-January-2016
;* Description : STM32F407xx devices vector table for MDK-ARM toolchain.
;* This module performs:
;* - Set the initial SP
;* - Set the initial PC == Reset_Handler
;* - Set the vector table entries with the exceptions ISR address
;* - Branches to __main in the C library (which eventually
;* calls main()).
;* After Reset the CortexM4 processor is in Thread mode,
;* priority is Privileged, and the Stack is set to Main.
;* <<< Use Configuration Wizard in Context Menu >>>
;*******************************************************************************
;
;* Redistribution and use in source and binary forms, with or without modification,
;* are permitted provided that the following conditions are met:
;* 1. Redistributions of source code must retain the above copyright notice,
;* this list of conditions and the following disclaimer.
;* 2. Redistributions in binary form must reproduce the above copyright notice,
;* this list of conditions and the following disclaimer in the documentation
;* and/or other materials provided with the distribution.
;* 3. Neither the name of STMicroelectronics nor the names of its contributors
;* may be used to endorse or promote products derived from this software
;* without specific prior written permission.
;*
;* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
;* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
;* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
;* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
;* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
;* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;
;*******************************************************************************
; Amount of memory (in bytes) allocated for Stack
; Tailor this value to your application needs
; <h> Stack Configuration
; <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>
; </h>
Stack_Size EQU 0x200 ; <your stack size>
AREA STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem SPACE Stack_Size
__initial_sp
; <h> Heap Configuration
; <o> Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>
; </h>
Heap_Size EQU 0x400 ; <your heap size>
AREA HEAP, NOINIT, READWRITE, ALIGN=3
__heap_base
Heap_Mem SPACE Heap_Size
__heap_limit
PRESERVE8
THUMB
; Vector Table Mapped to Address 0 at Reset
AREA RESET, DATA, READONLY
EXPORT __Vectors
EXPORT __Vectors_End
EXPORT __Vectors_Size
堆栈的值由那行设置
Stack_Size EQU 0x200 ; <your stack size>
和堆
Heap_Size EQU 0x400 ; <your heap size>
在 Keil uVision 中,左下角还有 2 个选项卡,您可以在其中切换 "Text Editor" 和 "Config Wizard" 以更改代码堆和堆栈大小。