Shop OBEX P1 Docs P2 Docs Learn Events
Howto pass variable from cog to main cog program? — Parallax Forums

Howto pass variable from cog to main cog program?

bmentinkbmentink Posts: 107
edited 2010-03-04 20:18 in Propeller 1
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 ..


''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

  • MagIO2MagIO2 Posts: 2,243
    edited 2010-03-04 06:44
    PASM code does not know about variables in the VAR section. The line

    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
  • kuronekokuroneko Posts: 3,623
    edited 2010-03-04 06:49
    MagIO2 said...
    PASM code does not know about variables in the VAR section. The line

    mov frqa, #1

    should give you an compile error.
    Why should it? The VAR section defines freq not frqa.
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-03-04 07:07
    Please forgive me .. it's early in the morning ;o) The "a" just was a bit to small to see.

    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.
  • kuronekokuroneko Posts: 3,623
    edited 2010-03-04 07:14
    MagIO2 said...
    Please forgive me .. it's early in the morning ;o) The "a" just was a bit to small to see.
    No, forgive me! I forgot that you just got up. Here it's nearly time for dinner [noparse]:)[/noparse]
  • bmentinkbmentink Posts: 107
    edited 2010-03-04 18:29
    MagIO2: Thanks I did not realize the VAR variable freq IS a HUB variable. What variables are declared for cog use then?

    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:

    OBJ
        fr: "freq"
    
    VAR
        long freq
    
    PUB go
       fr.start(@freq)
    
    



    And in the freq.spin module:
    PUB start(freq)
    
        cogstart(@entry,freq)
    
    
    



    Is this correct?

    Thanks.

    Post Edited (bmentink) : 3/4/2010 7:16:10 PM GMT
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-03-04 20:08
    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
    
    

    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
  • bmentinkbmentink Posts: 107
    edited 2010-03-04 20:18
    Many thanks smile.gif
Sign In or Register to comment.