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 change the stack amount

Other Parts Discussed in Thread: MSP430F5438A

Hi everyone. I am using MSP430F5438A on IAR embedded workbench for MSP.

I have two questions:

1. How to change the size of  the stack segment?

2. How to detect stack overflow? 

Thank you in advance.

  • In IAR compiler under Project->Option->General option you canchange your stack size.
  • I think that "stack size" setting is not very meaningful.
    If your stack overflows, enlarging that setting will not help at all.
  • Isn't that only the warning threshold?
  • Dennis Eichmann said:
    Isn't that only the warning threshold?

    Well, yes and no.

    It is a linker error if there isn't enough RAM left to fufill the stack reservation you make.

    Also, if you use an .xcl linker control file in your project, that is the place to make your stack size reservation.

    There is no automatic way to catch stack overflow (no MMU), but if you must you can code up a stack check function. You would fill the stack in a custom startup code (prior to cstart) with a known value and then in your main loop you could check the bottom X words of the stack to make sure there is buffer.

  • " You would fill the stack in a custom startup code"
    Can you please show me how to make a custom startup code?
  • Not directly. It depends on the toolchain you use.

    For example, in IAR, you would make a project-specific cstartup.s43 and put your changes in that and include it as part of your project.

    In that file:

           // --------------------

           // Initialize SP to point to the top of the stack.

           //

           MOV     #SFE(CSTACK), SP

    --> Code here to fill stack with known value

           //

           // Ensure that main is called.

           //

           REQUIRE ?cstart_call_main

    So, read the manual for your toolchain and poke around to figure out what you need to override. Note that stack filling needs to occur before your C environment is initialized and function calls are made so that you don't trash the stack while it's in use.

  • "Isn't that only the warning threshold?"

    It's a bit more. It's like defining an array
    unsigned char stack[x];
    If you write to this with an index >=x, you will write into memory that is probably used by something else. Same for the stack. But the linker will know that that much ram is to be reserved for the stack and trigger an error if there isn't enough space. Just as it would happen for the array.

**Attention** This is a public forum