PWM assembly code questions
SSkalko
Posts: 9
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, #4gets 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
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.