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.

Fill stack with a pre-defined value



https://e2e.ti.com/support/microcontrollers/c2000/f/171/t/196862

The above post explains how to initialize the stack of a c28x-application with a known value (for the purpose of online monitoring of stack usage):

     .sect     ".stack"

     .asg      330000h, addr   ; adjust the address here as you need

     .loop     00C000h           ; adjust the count here as you need

     .long     YOUR_PATTERN

     .eval     addr + 2, addr

     .endloop

I don't understand where I should place this code. Should it go into F28M35x_CodeStartBranch.asm? Please provide some more details, for example, if changes to the linker cmd file are also needed..

Many thanks,

Christian L 

  • Christian,

    I am not clear on what you are looking to do.  Is this for development so that you can determine how large a stack you need to allocate in the code project?  If so, then you can just use CCS to fill the stack memory before you run the program.  Are you looking to do this each and every time you run your code, and maybe check the end of the stack periodically in your code to see how much is left?  If that is the case, then you need to fill the stack at runtime.  The assembly code you cited will not actually fill the stack at runtime.  It fills the stack at LOADTIME (when CCS loads the program).  To fill the stack at runtime, you'd need to write actual assembly instructions that stores the desired value to the stack in a loop.  The code you showed above is not really code.  It is a bunch of assembler directives.

    Regards,

    David

  • Hi David,
    Thanks for your quick reply! Yes, I am looking to do this each and every time I run the code and also in the final standalone product. So I would like actual assembly instructions to do this before calling _c_int_int00. (At the moment, I do fill the bottom of the stack from C-code for monitoring a "high" watermark, but would like to fill the whole stack and then online estimate the actual usage periodically.)
    Christian
  • Hello Christian,


    you do not have to write any assembly code, see the following function:

    #define STACK_PATTERN (0xAAAAu)
    
    uint16_t *globalIterator;
    
    void stackInit(void)
    {
      uint16_t topOfStack;
    
      globalIterator = &topOfStack;
      while(globalIterator < &vStackEnd)
      {
        *globalIterator = STACK_PATTERN;
        globalIterator++;
      }
    }
    

    This function defines a variable topOfStack, which will be placed on top of the stack (it cannot be assigned to any register, because we are using a pointer to this variable - and a register doesn't have any address). The symbol vStackEnd is generated by linker (see RUN_END operator in SPRU513) and its address is the last address of the stack.


    I hope this helps, regards,

      Jan

  • Hello Jan,

    Thank you for this suggestion. I think this would require a "pre-loader" programme written in C-code which after executing your code jumps to the actual programme. If not, you will corrupt the stack as it is in use while running your C-code?

    Best regards,

    Christian

  • This routine is being executed during the initialization process. No interrupts are enabled, so no stack corruption can occur. This routine initializes the stack from its top (the last used value is occupied by the topOfStack variable) to the end of the stack. All bytes which have been allocated on stack before calling this function stay intact!

    Example: if your stack occupies addresses in the range from 400h to 7FFh and addresses 400h to 410h are already allocated at the moment of stackInit() execution, then return address of stackInit() will occupy the addresses 411h and 412h, the address of topOfStack will be 413h and all bytes in memory range from 413h to 7FFh will be rewritten by STACK_PATTERN.
  • Christian,

    The method Jan shows is perfectly valid.  I'd agree that you don't care about the first few entries of the stack that his method may skip because they are already in use.  If you really want to do it from ASM code before the _c_int00 routine runs, I've attached a CodeStartBranch.asm file that I added a stack fill routine to.

    Regards,

    David

    CodeStartBranch.asm

  • Thank you Jan, and thank you David,

    That's two very good solutions!

    Best regards,

    Christian