from machine import Pin
from rp2 import PIO, StateMachine, asm_pio
class PIO_TPO:
def __init__(self, sm_id, pin, prop_band):
self._sm = StateMachine(sm_id, self.tpo_prog, freq=60_000_000 // prop_band, set_base=Pin(pin))
max_count=1000
max_count=max_count-4
self._sm.put(max_count)
self._sm.exec("pull()")
self._sm.exec("mov(isr, osr)")
self._sm.active(1)
self._max_count = max_count
@asm_pio(set_init=PIO.OUT_LOW)
def tpo_prog():
set (pins,0)[29]
pull(noblock)[29]
mov(x, osr) [29]
mov(y, isr) [29]
label("pwmloop")
jmp(x_not_y, "skip") [29]
set(pins,1) [29]
label("skip")
jmp(y_dec, "pwmloop") [29]
def set(self, value):
value = max(value, -1)
value = (min(value, self._max_count))-1
self._sm.put(value)