Noob Question - cognew and the independance of cogs
coryco2
Posts: 107
I went through the Spin programming tutorial in the Propeller help, and got all excited about using cognew to run two of my Spin files simultaneously in separate cogs. I set up a main method like this:
CON
_clkmode = xtal1 + pll16x ' System clock settings
_xinfreq = 5_000_000
OBJ
MyFirstObject: "MyFirstSpinFile"
MySecondObject: "MySecondSpinFile"
VAR
LONG MyFirstObjectStack[2000]
LONG MySecondObjectStack[2000]
PUB Start
cognew (MyFirstObject, @MyFirstObjectStack)
cognew (MySecondObject, @MySecondObjectStack)
Both of the Spin objects are nowhere near 2000 longs; I just wanted to make sure they had PLENTY of stack space. Maddeningly, though, only the first object called in the Start method will run. If I switch around the two cognew lines, then the other object will run (i.e. so it's whichever is called first). Both objects contain repeat loops, but if they are running in separate cogs, they shouldn't have any effect on each other, should they? (They are not addressing any of the same I/O pins, nor using any of the same objects.)
Can anyone tell me what I am missing here?
CON
_clkmode = xtal1 + pll16x ' System clock settings
_xinfreq = 5_000_000
OBJ
MyFirstObject: "MyFirstSpinFile"
MySecondObject: "MySecondSpinFile"
VAR
LONG MyFirstObjectStack[2000]
LONG MySecondObjectStack[2000]
PUB Start
cognew (MyFirstObject, @MyFirstObjectStack)
cognew (MySecondObject, @MySecondObjectStack)
Both of the Spin objects are nowhere near 2000 longs; I just wanted to make sure they had PLENTY of stack space. Maddeningly, though, only the first object called in the Start method will run. If I switch around the two cognew lines, then the other object will run (i.e. so it's whichever is called first). Both objects contain repeat loops, but if they are running in separate cogs, they shouldn't have any effect on each other, should they? (They are not addressing any of the same I/O pins, nor using any of the same objects.)
Can anyone tell me what I am missing here?
Comments
cognew (MyFirstObject.Init, @MyFirstStack)
cognew (MySecondObject.Init, @MySecondStack)
Both the actual objects compile separately just fine, they just don't seem to work when called simultaneously in separate cogs. Is there any way one cog can even interfere with another?
-Phil
you can't use a construction like cognew(objectname.methodname,@Stack)
it has to be cognew(methodname)
or if you want to have the method in separate *.spin-files (=objects)
the cognew-command has to be INSIDE of MyFirstObject.spin and MySecondobject.spin
the standard for doing this is to have a method called "Start" inside every object-file
then you start the new cog through
MyFirstObject.Start
MySecondobject.Start
There is a special reason why the compiler allows online this way
each cog has it's own pin-direction and output-register
so if you would setup the IO-direction register in cog A
and then try to set IO-pins high-low with cog B this will not work
from the mainmenu inside the propeller-tool you can access the
Propeller Education Kit Labs Fundamentals V1.1
read chapter 6 "Objects that Launch Processes into Cogs" to get more information about how it is done
Again this is a case of "the computer does ALWAYS exactly what you have coded"
Only sometimes you haven't understand in detail yet what you have coded
best regards
Stefan
P.S: a stack of 200 longs will do in most cases. The stack is NOT for storing your whole code. You just need place for the parameters of the called methods.
The stackspace can increase if you do a lot of nested calls. As each parameter needs a long this isn't that much.
Say I have processes running in three cogs, and I want each of them to be able to be able to arbitrarily modify or read the same set of variables. What is the most efficient way of coding this?
I also had a few questions about your example approach (i.e. all three cogs managed in the same object):
Is it more efficient to combine my multi-cog objects into a single "super-object", or to keep them separated? I am guessing all the code starts out in the global RAM anyway, so it's six of one or half dozen of the other? And the cogs can't actually access any given variable at exactly the same time can they (i.e. two of them won't be trying to modify the same variable at once)? They would take turns via the hub?
Finally (and I maybe should start a new thread--heh), is there a way to store a set of variables in the EEPROM that the cogs can modify, and will then retain those values through the next power cycle?
Thanks again! :-)