tag 标签: overflow

相关博文
  • 热度 21
    2013-3-10 12:08
    1775 次阅读|
    0 个评论
    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 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. 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. 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. 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: 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. 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 ).