Sunday, February 19, 2012

Prop Stack Size: Problems and Solution

I have been having problems with some Parallax Propeller programs for use with my QRP Beacons and Transmitters.

My Propeller Transmitter Station
Running KO7M CW Keyer Code
Thanks to Jeff - KO7M , I think we worked through most of the issues. One major issue was that I had was; that I had specified the Stack Size TOO small for several of the COG's or processes. See #6 on Lessons Learned a previous post. Currently my new approach will be to set the Stack arbitrarily large (i.e., 64), and trim only if necessary.

Jeff found a Prop Object that estimates and report Stack usage. Our initial reports indicated my Stack were growing as large as 48. My initial expectations were more like 16. As can be seen, my estimate was way off, and likely the cause of many of my programming problems and needless frustration.

The Stack Usage tools reports the Stack Size by bit-banging the report on one of the Prop pins that should be connected to a terminal. My problem is I do not have a easily accessible terminal in my Ubuntu environment connected to the Prop. With Ubuntu, the Prop UI wants to control the terminal I/O lines for EEPROM download, and a quick change of UNIX port would be necessary to make the switch (Yeah, I know, it's my problem, and that's what I deserve for using Ubuntu :-).

The Stack Usage tool finds the Stack size by writing a known pattern onto an empty stack. then later reports the count of over-written stack entries. The Stack Usage Object is a little complex and not very flexible for my use.

Well, I can do the same as the Tool (and maybe better), with a simple cut-n-paste routine (method) into any main Object.

My first typical PUB method appears as follows:



PUB Demo | stat

      StackLengthInit

      Init   ' Which includes the LCD setup

      < normal program stuff goes here >

      ' Report Stack Length
      LCD.moveto(1,2)
      LCD.dec(StackLength)

      repeat
        pauseSec(1)


I inserted the following just below the first PUB method. Calling "StackLengthInit" sets it up, and then "StackLength" can be used at any point to report the usage.



DAT
CON   StackSizeMax = 256
      StackPattern = 19823343 ' Some Random Pattern

PRI StackLengthInit : i | S[StackSizeMax]
      i := 0
      repeat StackSizeMax
         S[i++] := StackPattern -> 7

PRI StackLength : i | S[StackSizeMax]
      i := 0
      repeat StackSizeMax
        if S[i++] == StackPattern -> 7
           return i

DAT



Note: I used the  "StackPattern -> 7" expression to (maybe) help provide some bits in the upper bits of the saved stack space (maybe it is not really necessary, and just the StackPattern would surfice :-).

It seems to work very will for my Ubuntu Prop Development environment, and it is flexible enough that I can save or report the results as I desire, on my standard LCD or a Terminal (if connected).

From now on, I will be much more Stack Size Aware :-)

It's all fun with the Prop !

--

No comments:

Post a Comment