Question about SPIN keyword RETURN and stack variable
whiteoxe
Posts: 794
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
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
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.
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.
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.