This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to Program Stack memory in MSP430F5419 in my application

Basically I'm downloading an image(.hex format) of size 430KB via over the air & storing in external Flash memory interfaced with MSP430 MCU. I want to download 5 images. When I'm downloading 3rd image, IAR Embedded Workbench shows stack overflowed. How to solve this issue?

  • Ajit k said:
    How to solve this issue?

    Fix the bug(s) and/or design flaw(s) which cause the stack to keep growing with each image!!

  • Is there any tool to know run time RAM / stack  usage for IAR EMbedded workbench IDE. Please suggest how to efficiently use the stack & control its overflow.

  • Your program shouldn't be using more and more stack for each "image" that you download - that is a bug, or a design error (or both).

    At the end of each "image" download, your stack usage should be back to where it started (other things being equal).

    So you could start by breaking at the end of each "image" download, and examining stack usage.

    You could also set a memory-access breakpoint at the end of your expected Stack area.

    For specific details of the features & functions of the IAR EMbedded workbench you need to study the IAR documentation.

    For support on IAR tools, you should be talking to IAR.

     

  • Please suggest how do i optimize my code to control stack overflow. I tried to increase the stack size in IAR Embedded Workbench to 600(default stack size would be 160) & was able to download 6 images. But this scenario is not repeating when I test for still more images. What is the maximum stack size of MSP4305419 MCU? How do i optimize my code to avoid overflow & what are the methods involved, please suggest.

  • Ajit k said:
    What is the maximum stack size of MSP4305419 MCU?

    The maximum stack size is 1MB since the stack pointer is a 20 bit register.

    Of course the usable size is much smaller. It is as much as there's unused ram above your global and static variables.

    It's a common misunderstanding that you can set a stack size. You cannot. The stack begins (usually) at the top of the ram and grows down as much as the code flow requires. If you use local variables, the stack grows. If you call a funciton, the stack grows (and shrinks if you return form this function). If you pass many parameters to a function, the stack grows. If a function needs to save CPU registers for doingits job, the stack grows. If an interrupt happens, the stack grows.
    If the stack grows so much that it reeaches your global variables, it overwrites them (and any write to the global variables will then corrupt the stack too). If it grows larger than the availabel ram, it will grow into the void and everyhting put on it (including return addresses) is lost and a system crash is imminent.

    The 'stack size' settings you can do in the projects will only ensure that the linker will complain if your global and static variables do not leave that much space for the stack. However, this setting has no effect on the generated code at all.

    However, a typical project won't use more than 100 bytes of stack (usually less). If it does, it's likely due to significant design flaws.

    If you post your code, we might be able to show you what's wrong. But currently, you just try to cure the disease by suppressing the symptom.

  • We simulated our code & found that, it is happening because of buffer overflow. please advice how to overcome this scenario.

  • Ajit k said:
    it is happening because of buffer overflow. please advice how to overcome this scenario.

    What exactly do you want to know? How to handle buffers in your code?

  • Yes, basically we have two two functions defined in two different files (which contain many arrays). Now we are sure that stack is not overflowing. but the variables of second file when returned back to first file seem to be filled with junk characters. This means that some arrays are getting corrupted. please suggest how to step in to solve this issue?

  • Ajit k said:
    This means that some arrays are getting corrupted. please suggest how to step in to solve this issue?

    1. Using your head. If you know which buffer runs over other variables, then check code handling particular buffer, look for errors.

    2. Using debugger. Well, if you don't know how to do it, then look for some tutorial or debugger manual. Generic debug questions is out of scope here in this forum.

     

**Attention** This is a public forum