简体   繁体   English

Arm 4微控制器上的硬故障错误

[英]Hard Fault Error on Arm 4 microcontroller

I have a problem with my Tiva C controller which is very strange and I would like some help with it, basically it crashes on an sprintf line with no obvious reason to do so. 我的Tiva C控制器有一个问题,这很奇怪,我需要一些帮助,基本上,它在sprintf行上崩溃,没有明显的理由。 My application is simply a smart toy which can be controlled through Bluetooth in various modes. 我的应用程序只是一个智能玩具,可以通过蓝牙以各种模式进行控制。 For the modes we use a finite state machine code and for reading the current acceleration for the toy just for monitoring only and it is sent every once in a while using a periodic timer which generates a periodic interrupt every couple of secs to update the value of acceleration 对于这些模式,我们使用有限状态机代码并读取玩具的当前加速度,仅用于监控,并且使用周期性计时器每隔一段时间发送一次,周期性计时器每隔几秒钟生成一次周期性中断以更新其值。加速

Now the reading of acceleration is done by a simple function reading goes fine until trying to store that reading inside a string and the program just throws a hard fault. 现在,通过简单的函数读取就可以完成加速度的读取,直到尝试将其存储在字符串中,程序会抛出硬故障。 This function (which reads the acceleration) works fine in all other modes of toy car except this new mode it always crashes and throws the hard fault error. 该功能(读取加速度)在玩具车的所有其他模式下均能正常工作,但这种新模式始终会崩溃并引发硬故障错误。 My current thinking is that it might be due to too many function calls so the stack is getting full but how can I know whether the stack is full or not? 我目前的想法是,这可能是由于函数调用过多而导致堆栈已满,但是我怎么知道堆栈是否已满?

This is the line that it crashes: 这是它崩溃的行:

sprintf(acceleration,"x%.2f y%.2f z%.2f", X_Axis1, Y_Axis1, Z_Axis1); 

It's a simple sprintf which stores the value read from the acceleration axis x , y and z then stores in acceleration variable any idea why this is happening? 这是一个简单的sprintf ,它存储从加速度轴xyz读取的值,然后将其存储在加速度变量中,这是为什么发生的?

This is also the fault report on keil inside the timer handler I used to call a ReadAccel() function which I made which works in all modes except the most recent one so I thought I would move the code inside the function inside the timer handler to save a bit from the stack but it still gave a hard fault 这也是关于定时器处理程序内部的keil的故障报告,我曾经调用过该函数,我将该函数制成了ReadAccel()函数,该函数可在除最新模式之外的所有模式下工作,因此我想我会将定时器处理程序内部函数内的代码移至从堆栈上节省了一点,但仍然给您带来了严重的错误 在此处输入图片说明

If someone can redirect me to how to figure out if this is a stack full error I would be very thankful since I think this is what it is . 如果有人可以将我重定向到如何确定这是否是堆栈已满的错误,我将非常感激,因为我认为这就是事实。

void TIMER2A_Handler(void)
{
        char acceleration[22];
        RawX_Axis1=ReadAccelX();
        X_Axis1 = RawX_Axis1 * 0.00390625+0.35;
        RawY_Axis1=ReadAccelY();
        Y_Axis1 = RawY_Axis1 * 0.00390625+0.08;
        RawZ_Axis1=ReadAccelZ();
        Z_Axis1 = (RawY_Axis1 *  0.00390625)+1.08;
        sprintf(acceleration,"x%.2f y%.2f z%.2f",X_Axis1,Y_Axis1,Z_Axis1); 
        UARTSend(UART3_BASE,acceleration);
        UARTCharPut(UART3_BASE,'\n');
        TIMER2_RIS_R = 0xFFFFFFFF;
        TIMER2_ICR_R = 0xFFFFFFFF;  
}

First try it without a call to sprintf, just send a hard coded string in UARTSend . 首先尝试它而不调用sprintf,只需在UARTSend发送一个硬编码字符串UARTSend If that stops the crash, then you need to find out why the sprintf is causing the crash. 如果这样可以停止崩溃,则您需要找出sprintf导致崩溃的原因。 I see two problems: 我看到两个问题:

You allocate a 22 byte buffer for your sprintf, that might be too small since it only allows six characters for each acceleration (including the possible minus sign and decimal point). 您为sprintf分配了22个字节的缓冲区,该缓冲区可能太小,因为每个加速仅允许六个字符(包括可能的负号和小数点)。 Try formatting and sending each acceleration individually. 尝试格式化并分别发送每个加速度。 it might also alleviate a potential stack overflow in sprintf. 它也可能减轻sprintf中潜在的堆栈溢出。

Also, try using a simplified inline string conversion. 另外,请尝试使用简化的内联字符串转换。 eg cast each number to an int after multiplying it by 100 (gives you a 2 dp fixed point number) and use simple division and mod by 10 and addition to 0x30 (ASCII 0 ) to create the string. 例如,将每个数字乘以100(为您提供2 dp定点数字),然后将其转换为整数,并使用简单的除法和mod除以10,再加上0x30(ASCII 0 )来创建字符串。

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

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