Howto pass variable from cog to main cog program?
bmentink
Posts: 107
Hi,
As being very new to the propeller, I have a question about Hub variables.
I have a cog running a frequency counting program, the results of which (freq) I would like to pass back
to my main program on cog0 ..
Can someone show me the syntax on how to declare the hub variable in cog0 and how to access (store to it) from another cog.
So far (using a local variable) my code looks like this for the cog running the freq counter:
(I want to change the local variable to a hub variable ..
Many Thanks,
Bernie
As being very new to the propeller, I have a question about Hub variables.
I have a cog running a frequency counting program, the results of which (freq) I would like to pass back
to my main program on cog0 ..
Can someone show me the syntax on how to declare the hub variable in cog0 and how to access (store to it) from another cog.
So far (using a local variable) my code looks like this for the cog running the freq counter:
(I want to change the local variable to a hub variable ..
''A frequency counter using counter A CON _clkmode = xtal1 + pll16x _XinFREQ = 5_000_000 VAR long [b]freq[/b] PUB start : okay okay := cognew(@entry, @freq) DAT org entry mov ctra, ctra_ 'establish mode and start counter mov frqa, #1 'increment for each edge seen mov cnt_, cnt 'setup time delay add cnt_, cntadd :loop waitcnt cnt_, cntadd 'wait for next sample mov new, phsa 'record new count mov temp, new 'make second copy sub new, old mov old, temp wrlong new, par jmp #:loop ctra_ long %01010 << 26 + 15 'mode + APIN cntadd long 80_000_000 'wait 1 second, answer in Hz cnt_ res 1 new res 1 old res 1 temp res 1
Many Thanks,
Bernie
Comments
mov frqa, #1
should give you an compile error.
If you only have the need to communicate one long, you can use the special purpose register PAR directly. So, in your cognew you'd pass the address of that variable:
cognew( @entry, @freq )
The second parameter in cognew is stored in PAR-register and you can access it in PASM:
mov temp, par
this would copy the address from par to temp. (NOT THE CONTENT .. ONLY THE ADDRESS)
You can access the content by:
rdlong temp, par
wrlong temp, par
the first one will copy the content of freq to temp, the other one will copy the content of temp to freq.
If you have to pass more than one long there are other strategies. For example define an array for all the parameters and pass the address of that array.
long parameters
...
cognew( @entry, @parameters )
...
dat
entry
add second_par, par
add third_par, par
...
rdlong temp, second_par
...
wrlong temp, par
...
second_par long 4
third_par long 8
But then I don't see the point of the question. freq is a HUB variable.
Is it propably that you want to access this variable in several COGs? Then you can modify your start method. You pass the address of the variable you want to be used with the start function and the start function directly passes it to the PASM. The code which manages the COGs can then pass the same address to any start-function that might be interested in the freq.
However, as it stands it is declared in the freq.spin code module. I would like to declare the HUB variable freq in my main cog0 code module
and pass it to the freq.spin module ... is it as simple as passing the address of it to my "start" function?
Then freq.spin will update it, as it is doing now and I can simply read it in my main cog0 module ...
Example: In main code module:
And in the freq.spin module:
Is this correct?
Thanks.
Post Edited (bmentink) : 3/4/2010 7:16:10 PM GMT
As you can see you already used COG variables. Keep in mind, when you start a COG the code content of ctra_ and cntadd is copied into the COG-RAM.·There the·variables are only usable by PASM code inside of that COG. HUB variables can be used in SPIN and in PASM (by wrxxx and rdxxx) as long as PASM knows the address of the variables.
And yes, your changes look fine.
Post Edited (MagIO2) : 3/4/2010 8:14:36 PM GMT