Shop OBEX P1 Docs P2 Docs Learn Events
PWM assembly code questions — Parallax Forums

PWM assembly code questions

SSkalkoSSkalko Posts: 9
edited 2011-03-30 12:14 in Propeller 1
I am trying to work out how this PWM code works, so far I understand that data transferred to ASM code by initializing cognew(@entry, @sDuty) and that the order of the variables after sDuty is important but what I can't wrap my head around how
add t1, #4
gets me the next long from main memory
VAR
  long  cogon, cog       
  long sDuty                       
  long sPinOut 
  long sCtraVal
  long sPeriod
  

PUB Start( Pin) : okay
  longfill(@sDuty, 0, 4)       
  sDuty := 5                   ' default duty
  sPinOut := |< Pin   
  sCtraVal :=  %00100 << 26 + Pin
  sPeriod := 200              ' 400 kHz
  
  okay := cogon := (cog := cognew(@entry,@sDuty)) > 0

DAT
        org

entry   mov     t1,par                          'get first parameter
        rdlong  value, t1
         
        add     t1,#4                 
        rdlong  pinOut, t1
        or      dira, pinOut                    ' set pinOut to output      

        add     t1, #4
        rdlong  ctraval, t1
        mov     ctra, ctraval                   'establish counter A mode and APIN

        add     t1, #4
        rdlong  period, t1


        mov     frqa, #1                        'set counter to increment 1 each cycle

        mov     time, cnt                       'record current time
        add     time, period                    'establish next period

:loop   rdlong  value, par                      'get an up to date pulse width
        waitcnt time, period                    'wait until next period
        neg     phsa, value                     'back up phsa so that it  trips "value" cycles from now
        jmp     #:loop                          'loop for next cycle



period  res 1                    
time    res 1
value   res 1
t1      res 1
pinOut  res 1
ctraval res 1

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-30 12:14
    The COGNEW passes the address of sDuty to the cog in its PAR register. Read the description of the COGINIT PASM instruction in the Propeller Manual for details. The program uses this address as the starting address of a table with 4 entries (all longs) where it's to get its parameters. At the entry point, the program copies PAR to a temporary register (because PAR is read-only) and uses it to read the 1st parameter from hub memory. It then increments the temporary register by 4 and gets the 2nd parameter value, then repeats this to get the 3rd and 4th parameters.

    Just as an aside, the "mov ctra, ctraval" is placed where it is because there's a limit on how close together two RDLONG instructions can be placed. If they're executed within 9 clock cycles of each other, the cog will stall until the 9 cycles have passed. The MOV can be placed there because it will take no extra execution time. It's not important in this case, but the author probably is used to working that way. Read the description of hub memory access and timing in the Propeller Manual for details.
Sign In or Register to comment.