Shop OBEX P1 Docs P2 Docs Learn Events
Stack Size? — Parallax Forums

Stack Size?

JoergJoerg Posts: 91
edited 2008-01-01 19:53 in Propeller 1
I am dealing with a stepper motor module and have noticed, that the module did not run when the stack was to tight.
How can i calculate the stack size needed?

Saluti Joerg

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-12-31 19:50
    There is no easy way to calculate the stack size needed. It can be done with a flow analysis of the process, looking at the worst case call depth and the control path used. Basically, every time one routine calls another, some stack is allocated to store the return address, the result field for the call, and some pointers. There's also memory for any parameters and local variables for the call. There is a program in the Object Exchange that will monitor another cog's stack and see what's actually used over time. It's called "Stack Length".
  • CardboardGuruCardboardGuru Posts: 443
    edited 2007-12-31 20:14
    Here's a simple but effective approach. In your program initialisation, do:

    long[noparse][[/noparse]$7ffc]:=$DEADBEEF
    



    Then in your program's main loop do:

    if long[noparse][[/noparse]$7ffc]<>$DEADBEEF
          dira[noparse][[/noparse]0]:=1
          outa[noparse][[/noparse]0]:=1
    



    ...assuming you have a debug LED on pin 0.

    So if ever the stack reaches the last location of RAM and overwrites it, the debug LED comes on, and you know you need a bigger stack.

    If you want a margin for error, you could test a location slightly further down in memory. If you use block of memory at the top of RAM for a frame buffer or smething, test the location just below the frame buffer.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Help to build the Propeller wiki - propeller.wikispaces.com
    Play Defender - Propeller version of the classic game
    Prop Room Robotics - my web store for Roomba spare parts in the UK
  • hippyhippy Posts: 1,981
    edited 2008-01-01 02:06
    To expand on CardboardGuru's advice, where its a long array stack passed as the parameter in CogNew or CogInit, start with an arbitrarily large array, fill it with $DEADBEEF then check which entries in the stack have been overwritten.

    Untested, but something along the lines of ...

    VAR
      long stack[noparse][[/noparse]1000]
    
    PUB Main | i
      repeat i from 0 to 999
        stack[noparse][[/noparse] i ] := $DEAD_BEEF
    
      cognew( MySub, @stack )
    
      i := 0
      tv.Start( tv.pin )
      repeat
        repeat while stack[noparse][[/noparse] i ] <> $DEAD_BEEF
          i++
        tv.Out( $01 )
        tv.Str( String("Stack used : ") )
        tv.Dec( i )
    
    
    
  • JoergJoerg Posts: 91
    edited 2008-01-01 15:40
    Thanks to all.

    I am felling like in the early 80's, when i had to do debuting with LED's and other
    genius stuff (Z80 and CP/M80!!). Is there really no simple debugging tool.
    I think a simple debugger and a simulator would help a lot. I am using CodeWarrier
    for Freescale MCU's (for other projects) an i appreciate much the simulator to check if
    my code is (more or less) doing what i need, and the debugger helps a lot to reach the
    "target" of the project in a reasonable time!

    Who has experience in this field? Teaching for a long time microcontroller stuff has shown
    to me that it is extremely important to offer simple but powerful tools to beginners, so they
    are motivated and not frustrated. Even if i can live with the actual situation (used to poor environments!).


    Saluti e grazie mille per l'aiuto

    Joerg
  • hippyhippy Posts: 1,981
    edited 2008-01-01 18:03
    There is no Spin Language debugger or simulator available I know of. The Spin bytecode is proprietary and not documented by Parallax and is unlikely to be any time soon. Third parties ( like myself ) are working towards understanding the bytecode and something may come out of that. One day we may have source code level stepping, breakpoints and so on, with full-speed interaction with the target hardware but don't hold your breath while waiting.

    At present Spin is probably best debugged by adding some form of debugging object which allows progress and variable values to be displayed; the TV objects are brilliant for this and serial is easy to add and use as well.

    For determining stack size used, one could create a stack size monitoring object and I believe something like that is available in the Object Exchange already. In most cases it's usually enough to create a large enough stack to get the code running then reduce it until it fails then add a bit extra on for safety. Trying to find the exact, absolute minimum stack size is probably unnecessary in most cases.
  • JoergJoerg Posts: 91
    edited 2008-01-01 19:53
    Thanks hippy

    I was only thinking a little loud, but since i have seen many MCUs on my way and also many tools i think it is
    necessary that PARALLAX is including an interface for easy debugging. The communication channel is already present
    and for downloading code only its a waste!
    So i kindly ask the PARALLAX guys to improve the environment and i am pretty sure they will have more success with the
    Propeller story.

    Saluti Joerg
Sign In or Register to comment.