Shop OBEX P1 Docs P2 Docs Learn Events
waitcnt asm — Parallax Forums

waitcnt asm

TomSTomS Posts: 128
edited 2009-04-12 18:13 in Propeller 1
I'm having a problem with waitcnt in assembly that I can't figure out. The loop repeats in about 20 seconds no matter what I assign to LoopTime. The "transmit" code seems to work fine and was taken from FullDuplexSerial and extensively modified.

Here's my code:

{{Serial_tx_asm.spin
Tom Smuts                                                                 
}}
VAR
  long cog

  long  tx_pin                  '6 contiguous longs (Don't change order or add/delete var's)
  long  desc_ptr
  long  ip_ptr
  long  dbm_ptr
  long  pcent_ptr
  long  bit_ticks
PUB Start(txpin, descPntr, ipPntr, dBmPntr, pcentPntr, baudrate) : okay
  stop
  longmove(@tx_pin, @txpin, 5)          'Fill var's from passed parms
  bit_ticks := clkfreq / baudrate       'bit_ticks = num clks per bit
  okay := cog := cognew(@entry, @tx_pin) + 1
PUB Stop
  if cog
    cogstop(cog~ - 1)
DAT

entry           org     0
                mov     t1,             par                 'get structure address

                rdlong  t2,             t1                  'get tx_pin
                mov     txmask,         #1
                shl     txmask,         t2                  'txmask defines transmit pin

                add     t1,             #4                  'get desc_ptr
                rdlong  descptr,        t1

                add     t1,             #4                  'get ip_ptr
                rdlong  ipptr,          t1

                add     t1,             #4                  'get dbm_ptr
                rdlong  dbmptr,        t1

                add     t1,             #4                  'get pcent_ptr
                rdlong  pcentptr,       t1

                add     t1,             #4                  'get bit_ticks
                rdlong  bitticks,       t1                           

                mov     rxtxmode,       #0                  'mode = 0
                test    rxtxmode,       #%100       wz      'init tx pin according to mode
                test    rxtxmode,       #%010       wc
                or      outa,           txmask
                or      dira,           txmask
'----------------------------------------------------------------------------------------------
loop_start
'----------------------------------------------------------------------------------------------
'Line 1
                mov     bytecntr,       descptr
                call    #transmit
                
                mov     bytecntr,       ipptr
                call    #transmit       
'wait
                mov     time,           cnt
                add     time,           LoopTime
                waitcnt time,           LoopTime                

                jmp     #loop_start
'----------------------------------------------------------------------------------------------
'----------------------------------------------------------------------------------------------
transmit        rdbyte  txdata,         bytecntr            'get 1st byte
                cmp     txdata,         #0          wz      'check for end of string
  if_z          jmp     transmit_ret                        'exit when end of string reached

                'ready byte to transmit
                or      txdata,         #$100                      
                shl     txdata,         #2                  ' 2 stop bits
                or      txdata,         #1
                mov     txbits,         #11                 ' 11 total bits
                mov     txcnt,          cnt

'----------------------------------------------------------------------------------------------
:bit            shr     txdata,         #1          wc      'output bit on tx pin
                muxc    outa,           txmask        
                add     txcnt,          bitticks            'ready next cnt
'----------------------------------------------------------------------------------------------
:wait           mov     t1,             txcnt              'check if bit transmit period done
                sub     t1,             cnt
                cmps    t1,             #0          wc
  if_nc         jmp     #:wait
'----------------------------------------------------------------------------------------------
                djnz    txbits,         #:bit               'another bit to transmit?
'----------------------------------------------------------------------------------------------
                add     bytecntr,       #1                  'next byte in string
                jmp     #transmit
'----------------------------------------------------------------------------------------------
transmit_ret    ret
{
######################################## Undefined data ####################################
}                                                           ' temp variables
t1              res     1
t2              res     1
t3              res     1

descptr         res     1
ipptr           res     1
dbmptr          res     1
pcentptr        res     1
bitticks        res     1
bytecntr        res     1
rxtxmode        res     1
txmask          res     1
txbuff          res     1
txdata          res     1
txbits          res     1
txcnt           res     1
time            res     1
{
######################################### Defined data #####################################
}
LoopTime        Long    2_000

line1a          Byte "test string 1", 0 


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tom

Comments

  • TreeLabTreeLab Posts: 138
    edited 2009-04-12 14:39
    I am not sure about the waitcnt, but in the transmit:wait code you are comparing cnt-t1 to zero using a signed compare. I think that this causes problems when the cnt rolls over at 2^32. Also, cnt is unsigned always.

    Cheers!
    Paul Rowntree
  • TomSTomS Posts: 128
    edited 2009-04-12 15:07
    Paul,

    That portion of the code is direct from FullDuplexSerial so I don't think that's the problem. Thanks all the same.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-04-12 16:32
    The problem is that you've got LoopTime defined after a bunch of RESes (which, in this case, gives it whatever value is assigned to t1). Always put all of your RESes last so they don't overlap with subsequent definitions.

    -Phil
  • TomSTomS Posts: 128
    edited 2009-04-12 16:51
    Thanks,

    That did it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom
  • Chris_DChris_D Posts: 305
    edited 2009-04-12 17:07
    Paul,

    I believe you are incorrect on CNT always being unsigned.· In my experiments here, CNT is signed running from -max to +max.

    Chris
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-04-12 18:13
    CNT produces a 32-bit long. Whether or not it's "signed" depends entirely upon how its interpreted. Spin always interprets longs as 32-bit signed twos-complement integers. When writing in assembly, however, you can interpret them however you want: signed or unsigned. But that interpretation has nothing to do with the source (CNT or otherwise) from whence a long originates.

    -Phil
Sign In or Register to comment.