广告

原创 STM32/MCU getting stack overflow easily?

2013-3-10 12:08 692 1 1 分类: MCU/ 嵌入式

Many people may have encountered problems like this:
Even though there isn't a while/for loop in the program written,
the MCU would run in a dead loop (not responding until reset) eventually...

If we use debug mode to run the program for STM32, we would see the MCU would finally go into the infinite loop in HardFault_Handler() ISR.
Why is this event happening?哭

The problem may occur when there is large array of variables declared like buffer[xxxx] in functions. A program is more likely to get this problem when the large variable is in a recursive function.

开明To solve this problem, we have to know what caused the error.
When non-static variables are declared in functions and there is an interrupt/function call they would be stored in the stack area. When the function call/ISR finishes, the variables would be loaded again from the stack area. If the variable is of large size, the stack may be full and cannot store the variables, causing the MCU to fail to execute anymore.

There are several ways to solve the problem.大笑

The easiest way is to increase the default stack size:
This method is not only working with STM32, but also other MCUs like LPC213x/ADuC702x (ARM7).
Make sure that the startup file (e.g. startup_stm32f1xx_xx.s) is not shared with other projects to prevent affecting other projects. To do this, save the startup file as another file and replace the current startup file by the newly saved one.

0003-275x94.jpg
Fig. 1 The standard startup file of STM32 Medium Density Device

Open the startup file and save it to the project folder. Then remove the original startup file and add the one just saved in the project folder.

2.jpg
Fig.2 Removing the original startup file

After adding the file, the icon will not have a 'key' on it indicating the file is NOT read-only.

3.jpg
Fig.3 The startup file added, icon without a key

Everything is ready, then open the startup file by double-clicking on the item.
Then you may see the trick on the stack size:

4.jpg
Fig.4 The stack size configuration in the startup file
 
Increasing the stack size like modifying it to 0x00000800 or 0x00000F00 may help to solve the problem. The MCU would divide more RAM space for the stack.

The same technique also applies to other MCUs, with modifying the corresponding startup file.
5.jpg
Fig.5 Stack size config section of startup file of NXP's LPC2000 series MCU (ARM7)

However, there is a drawback - the area in the RAM for storing variables would be decreased - less variables could be declared (we would see the error during compilation)... But if the MCU has loads of RAM space, this method should be the best to the problem.

Another method is to use global variables for storing large variables. This would not only help to reduce the stack loading, but also reduce the CPU's time to store the variables into the stack when interrupt/function call takes place.

One solution is to use static variables, so the variables would not be stored to the stack. However, this method very probably CANNOT be applied to variables declared in recursive functions since each call to this function would modify the one and only one variable and may cause unexpected error (logic error occurred then 酷).

广告

文章评论 0条评论)

登录后参与讨论
相关推荐阅读
mariohk321_586285434 2014-09-10 23:58
我的手机
苹果发布了iPhone6​​,再次掀起了一鼓追「疯」潮...没有留意苹果产品的我,也听到了不少消息...虽然我是一名电子爱好者,但是到2009年尾才拥有第一部智能手机。那是一部的Windows M...
mariohk321_586285434 2014-09-01 00:53
Updates from Me
Hi, long time no see! This September will be a different one for me, since I am not a student an...
mariohk321_586285434 2014-01-25 18:44
Reusing Card Reader from a Canon inkjet Printer
Inspired from Charlieb000: http://icrontic.com/discussion/93568/reusing-card-reader-from-mp180-can...
mariohk321_586285434 2013-12-30 14:22
Power? Energy? Electronics?
Electricity has brought numerous benefits to our daily life. But we should be aware of the energy ...
mariohk321_586285434 2013-11-29 20:19
Device is not powered on? Replacing the capacitors may help!
Hi! Long time no see! Life has been busy over the past few months... Students' lives are busy... ...
mariohk321_586285434 2013-08-23 10:14
STM32F1 with 1.5inch CSTN Color LCD
First of all, happy new year and happy Valentine's day! Life has been very busy over the past fe...
我要评论
0
1
广告