Shop OBEX P1 Docs P2 Docs Learn Events
Question about waitcnt — Parallax Forums

Question about waitcnt

MazziniMazzini Posts: 58
edited 2011-08-13 05:51 in Propeller 1
Good day,

sry for trivial question ,Can you explain me waitcnt ?
In the code below, it would copy CNT's Value and add 101, then wait for CNT+ t1 cycles and toggle OUTA,1 ?I'm not sure
waitcnt t1,#0 ' zero What does it for?


''***************
''* PASD Test   *
''***************
''

CON

        'Use the following 2 lines if running on a Parallax PropDemo board
        _clkmode        = xtal1 + pll16x
        _xinfreq        = 5_000_000

        'Use the following 2 lines if running on a Hydra board
        '_clkmode        = xtal1 + pll8x
        '_xinfreq        = 10_000_000

VAR

  long  Cog, TestVar

OBJ
  dbg   :       "PASDebug"                '<---- Add for Debugger 


PUB main

  Cog := cognew(@entry, @TestVar) + 1

  dbg.start(31,30,@entry)                 '<---- Add for Debugger


PUB stop

  if Cog
    cogstop(Cog~ -  1)


DAT

                        org     0
entry

'  --------- Debugger Kernel add this at Entry (Addr 0) ---------
   long $34FC1202,$6CE81201,$83C120B,$8BC0E0A,$E87C0E03,$8BC0E0A
   long $EC7C0E05,$A0BC1207,$5C7C0003,$5C7C0003,$7FFC,$7FF8
'  --------------------------------------------------------------
                        mov DIRA,#$FF
loop 
                     
                        mov     t1,CNT
                        add     t1,#101    
                        waitcnt t1,#0
                        xor     OUTA,#1
                        jmp     #loop       
t1                      res 1                        

Comments

  • Mark_TMark_T Posts: 1,981
    edited 2011-08-12 09:23
    waitcnt waits for cnt to reach the value of the dest register and also then adds the source to the dest. The example is unnecessarily having to re-read the CNT register and do an explicit add that could be part of the waitcnt instruction - the usual idiom is
                mov   t1, cnt  ' setup
                add   t1, #101 ' first add
    :loop       waitcnt t1, #101 'all subsequent adds use the waitcnt
                xor   outa, #1
                jmp   #:loop
    
    The advantage of this way of doing things is you don't have to count cycles, the wait points are spaced exactly 101 clocks apart as waitcnt auto-adds 101 each time to t1 and never reloads cnt into t1.
  • MazziniMazzini Posts: 58
    edited 2011-08-12 11:36
    Hi,

    It works thanks , but my goal is reach 1 second pause and $1FF is the upper limit
    Do you know how achieve that ?may be I need an kind of nested loop..
    My osc run at 80Mhz --> 20MIPS = 50ns *512 = about 25us, its right ?

    Thank so much
  • ericballericball Posts: 774
    edited 2011-08-12 11:47
    Since you want a 1 second delay, you can simply read long[0] which is CLKFREQ.
                rdlong  delay, #0
                mov     t1, cnt    ' setup
                add     t1, delay  ' first add
    :loop       waitcnt t1, delay  'all subsequent adds use the waitcnt
                xor     outa, #1
                jmp     #:loop
    delay       long    80_000_000 ' overwritten with clkfreq
    
  • MazziniMazzini Posts: 58
    edited 2011-08-12 12:21
    ok , so waitcnt wait for delay (80.000.000 cycles) ?
    delay*50ns = 4 second(?)
    Thanks
  • StefanL38StefanL38 Posts: 2,292
    edited 2011-08-12 13:55
    no the systemcounter increments by one every clocktick. So at 80MHz 80_000_000 means exactly 1 second
    the waitcnt command makes the cog stop completely until the systemcounter matches the value "t1 + delay"

    As long as the cog is stopped as if you would stop the cpu-clock no commands are executed the cog is just frozen.

    keep the questions coming
    best regards

    Stefan
  • MazziniMazzini Posts: 58
    edited 2011-08-12 15:29
    Do you mean every clocktick = 12.5ns ?
    from Propeller's datasheet
    <<
    Note that for a cog clock frequency of 80 MHz, the
    ***counter update period is a mere 12.5 ns***. This high speed,
    combined with 32-bit precision, allows for very dynamic
    signal generation and measurement...
    >>

    Thanks
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-08-12 15:39
    Ja! Si!
    waitcnt (1 + cnt) is 12.5ns
    waitcnt (2 + cnt) is 25.0ns
    waitcnt (1_000 + cnt) is 12.5us
    ...
  • MazziniMazzini Posts: 58
    edited 2011-08-12 15:51
    ok :D thanks
  • StefanL38StefanL38 Posts: 2,292
    edited 2011-08-12 22:58
    there still is a limitation

    if you use waitcnt in spin the minimum waittime is 385 clockticks.
    This is because of the overhead caused by the SPIN-interpreter.

    in PASM the minimum waittime is 9 clockticks. it needs minimum 8 clockticks to load actual value of cnt and then execute the waitcnt-command.

    if you try to wait for a shorter timeperiod the systemcounter has already "overrun" the target-value and then it will take 2^32 / 80_000_000 = 53 seconds until
    the free running systemcounter again matches the value used in the waitcnt command.
    keep the questions coming
    best regards

    Stefan
  • MazziniMazzini Posts: 58
    edited 2011-08-13 02:02
    Hi,

    I don't know SPIN , I just started with Pasm and I read this :
    Instruction  Description                           Z     Result      C Result         R  Clocks
    WAITCNT D,S  Wait for CNT = D, then add S into D   -                Unsigned Carry  1      5+ 
    
  • Mark_TMark_T Posts: 1,981
    edited 2011-08-13 04:07
    PJ Allen wrote: »
    Ja! Si!
    waitcnt (1 + cnt) is 12.5ns
    waitcnt (2 + cnt) is 25.0ns
    waitcnt (1_000 + cnt) is 12.5us
    ...

    The first too will wait for a long time - by the time you've calculated cnt+1 or cnt+2 the cnt register will already be 8 greater and you'll be waiting for it to wrap round again...
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-08-13 05:51
    Figured that I was reinforcing the clockticks-to-12.5ns aspect.

    But, true, the time to setup the waitcnt/spin takes up a lot more time ( >> ) than the clockperiod/cnt when you get way, way down there, and that's something to consider.
Sign In or Register to comment.