Shop OBEX P1 Docs P2 Docs Learn Events
Question about SPIN keyword RETURN and stack variable — Parallax Forums

Question about SPIN keyword RETURN and stack variable

whiteoxewhiteoxe Posts: 794
edited 2009-09-08 14:24 in Propeller 1
VAR
long stack[noparse][[/noparse]30]

PUB LaunchBlinkCogs

cognew(Blink (4,clkfreq/3,9),@stack[noparse][[/noparse]0])
cognew(Blink (5,clkfreq/7,21),@stack[noparse][[/noparse]10])
cognew(Blink (6,clkfreq/11,39),@stack[noparse][[/noparse]20])

PUB Blink(pin,rate,reps)

dira[noparse][[/noparse]pin]~~
outa[noparse][[/noparse]pin]~

repeat reps * 2
waitcnt(rate/2 + cnt)
!outa[noparse][[/noparse]pin]

I copied the above program from the PE Manual and have two questions please..........

As stack is a variable that's declared it could be called anything but there must be an array address included when calling
cognew method. The above program does little so how do you decide the size of the array ? You might just stick with 10 elements for most calls of this method I guess.?? Not understanding how looking at the Object Info Window helps me either, Im pretty fuzzy on the whole stack thing.

Do the three COGNEW statements get executed before the first COGNEW statement has finished its Blink call ?

repeat in the first cog would repeat reps * 2 times ?

Post Edited (thewhiteoxe) : 9/8/2009 9:04:05 AM GMT

Comments

  • localrogerlocalroger Posts: 3,452
    edited 2009-09-08 12:29
    Stack space needs have been discussed quite a bit; they're not cut and dried. You don't need much to accommodate a simple routine like these blinkers, but you do need to supply enough to accommodate any sub-methods or objects your cog might ever use. Some people advocate a minimum of 100 or 200 longs just in case, if you have the room. There's also an object in the Obex that identifies just how much stack your object is using by observation.

    The cognew statements operate together; they each take about 8,000 cycles to load the cog image from Hub RAM, but they all run at the same time because the Hub is shared. Since the timing is all deterministic they will start in the order they were called, but if there were Spin statements following the COGNEWs quite a few of them might get executed before the cogs start up.

    And yes, repeat (expression) repeats the following indented code (expression) times.
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-09-08 13:11
    reps * 2 because the loop toggles the pin. So, in the first iteration it turns on the PIN, in the second it turns off the PIN.
    This means it's switched on reps times and it's switched off reps times. So, all in all an LED attached would blink reps times.

    With the stack ... the parameters you pass in the cognew to the function are already passed via stack. Each level of function-call adds need of stackspace. So, especially when you use ObjectExchange code there is no easy way to find out how much stack space you need per cog.
    What I learned so far is, whenever your program is doing really weird things, you should first think of checking the size of the stack. As it's better to have a to big stack than a to small stack, I prefere to give the SPIN-COGs plenty of stack-space and start optimizing if I run out of memory.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-09-08 14:24
    Just to add to localroger's comment ...

    COGNEW starts up the new cog then returns to the original program. The new cog begins by loading 512 longs from somewhere, either hub RAM if you're starting an assembly program or from ROM (the Spin interpreter) if you're starting a Spin program. This takes around 8K system clocks. In your case, 3 cogs are overlapped, all loading a copy of the Spin interpreter from ROM. Once a Spin interpreter is completely loaded, the cog starts executing the requested Spin method.
Sign In or Register to comment.