Shop OBEX P1 Docs P2 Docs Learn Events
PASM: Are cognew 's the cause of this delay? — Parallax Forums

PASM: Are cognew 's the cause of this delay?

RonPRonP Posts: 384
edited 2011-08-08 10:51 in Propeller 1
Hello All,

I just finished potatoheads Assembly for Beginners great stuff BTW. Thanks potatohead.

I ran the code below in GEAR, I noticed that P17 was a little behind P16. I originally thought it was due to clock cycles but then remember they are running in 2 cogs so they should be parallel. So now I assume the delay comes from the time it takes cog0's main method to start the second cog. Is this assumption correct?

Trying to better understand my hardware as suggested. :smile:

'From Page 340 of the Propeller Manual
'With some small edits, for this purpose!
'Potatoheads Beginner PASM
'Ran in GEAR
{{          ____      ____
   P16  ___|    |____|    |_____
               ____      ____
   P17  ______|    |____|    |__
}}

 

CON
  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

PUB Main
{Lanch cog to toggle P16 endlessly}

  cognew(@Toggle, 0)  'Launch new cog / cog0 launces 2 cogs then shuts itself down
  cognew(@Toggle1, 0) 'Launch another new cog

DAT
'---------[ Toggle P16 ]-------------------------------------------------------

              org 0             'Begin at Cog RAM addr 0
Toggle        mov dira, Pin     'Set Pin to output
              mov Time, cnt     'Calculate delay time
              add Time, #$78     'Set initial delay here $f = 15
:loop     waitcnt Time, Delay   'Wait
              xor outa, Pin     'Toggle Pin
              jmp #:loop        'Loop endlessly

Pin     long  |< 16              'Pin number
Delay   long  600                'clock cycles to delay

Time    res   1

'---------[ Toggle P17 ]-------------------------------------------------------

              org 0             'Begin at Cog RAM addr 0
Toggle1       mov dira, Pin     'Set Pin to output
              mov Time, cnt     'Calculate delay time
              add Time, #$f     'Set initial delay here $f = 15
:loop     waitcnt Time1, Delay1 'Wait                    
              xor outa, Pin1    'Toggle Pin
              jmp #:loop        'Loop endlessly
              
Pin1    long  |< 17              'Pin number
Delay1  long  600                'clock cycles to delay

Time1   res   1

Thanks

Ron

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2011-08-07 13:18
    Yes!

    COGNEW is still a SPIN instruction, which needs to be decoded. So the execution time makes the delay from one signal to the other. If you want to run both COGs in sync you can for example pass a start-time to both PASM routines and simply use that in a waitcnt you do before you run the existing code. Both COGs will then continue at the same time.
  • RonPRonP Posts: 384
    edited 2011-08-07 13:30
    Thanks MagIO2,

    I will consider it my duty to get those signal lined up. We can't have them looking like that, in a parallel environment. :smile:

    Ron
  • ericballericball Posts: 774
    edited 2011-08-08 06:41
    Don't forget that cognew/coginit have a significant latency between when the cognew/coginit is executed and when the PASM/SPIN routine starts.
  • AleAle Posts: 2,363
    edited 2011-08-08 06:58
    Loading the COG requires 8192 cycles plus the spin overhead. You can use a waitcnt with a future count of 17000 or so to synchronize both COGs :)
  • jazzedjazzed Posts: 11,803
    edited 2011-08-08 08:09
    Try this version.

    The parameter startDelay is passed by reference because PAR is limited to 14 bits. Variable startDelay is set to clkfreq+cnt so the cogs will wait a second before starting the toggle loop. There were various references to "Time" in Toggle1's code - I fixed them for you.

    I don't know what gear will show you, but my scope shows two perfectly synchronized square waves with 600 cycle delay. I'm glad you could learn something from Potatohead's very nice tutorial.
    'From Page 340 of the Propeller Manual
    'Changed waitcnt clock setup to "best practice"
    'Added waitcnt to cog code to toggling simultaneously
    'With some small edits, for this purpose!
    'Potatoheads Beginner PASM
    'Measured on oscope
    {{          ____      ____
       P16  ___|    |____|    |__
                ____      ____
       P17  ___|    |____|    |__
    }}
    
    CON
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
    VAR
        long startDelay
    
    PUB Main
    {Lanch cog to toggle P16 endlessly}
      startdelay := clkfreq/2+cnt
    
      cognew(@Toggle, @startDelay)  'Launch new cog
      cognew(@Toggle1,@startDelay)  'Launch another new cog
      
      repeat
        waitcnt(clkfreq+cnt)        ' give cog0 something to do just in case ....
        
    DAT
    '---------[ Toggle P16 ]-------------------------------------------------------
    
                  org 0             'Begin at Cog RAM addr 0
    Toggle
               rdlong Time, par     'read start delay
              waitcnt Time,Time     'Wait for start delay count
    
                  mov dira, Pin     'Set Pin to output
                  mov Time, #$f     'Set initial delay here $f = 15
                  add Time, cnt     'Now add cnt to calculate delay time
    :loop     waitcnt Time, Delay   'Wait
                  xor outa, Pin     'Toggle Pin
                  jmp #:loop        'Loop endlessly
    
    Pin     long  |< 16             'Pin number
    Delay   long  600               'clock cycles to delay
    
    Time    res   1
    
    '---------[ Toggle P17 ]-------------------------------------------------------
    
                  org 0             'Begin at Cog RAM addr 0
    Toggle1
               rdlong Time1,par     'read start delay
              waitcnt Time1,Time1   'Wait for startdelay count
    
                  mov dira, Pin1    'Set Pin to output
                  mov Time1, #$f    'Set initial delay here $f = 15
                  add Time1, cnt    'Now add cnt to calculate delay time
    :loop     waitcnt Time1, Delay1 'Wait                    
                  xor outa, Pin1    'Toggle Pin
                  jmp #:loop        'Loop endlessly
                  
    Pin1    long  |< 17             'Pin number
    Delay1  long  600               'clock cycles to delay
    
    Time1   res   1
    
  • potatoheadpotatohead Posts: 10,261
    edited 2011-08-08 10:51
    Hi Ron.

    You are welcome, and thanks for the feedback. It's valuable. I think I'll make some edits to that portion and fold those into the current effort.

    Doug
Sign In or Register to comment.