# RP2040 - Generate configurable period square wave 1.81..17.86 MHz
# by Anthony Vogelaar, Perth Scotland
# rev 0.10  2025-08-22  Initial release

from	machine		import	Pin
import	rp2

# ======================= PIO functions ======================

# Frequency on GPIO-26 is determined by value in Y (1.81  .. 17.86 MHz)
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
def freqY():
    set(y, 31)
    label('loop')
    mov(x, y)
    set(pins, 1)
    label("l1")
    jmp(x_dec, 'l1')					# possible to add up to 31 * nop()
    mov(x, y)
    set(pins, 0)
    label('l2')
    jmp(x_dec, 'l2')
    jmp('loop')

# ======================= State machine ========================

# helper class for PIO StateMachines
class PioMach:
    
    def  __init__(self, program, freq=125_000_000, id=0, **kwargs):
        sm = rp2.StateMachine(id, program, freq, **kwargs)
        sm.active(True)

# ========================== main ==============================

sm = PioMach(freqY, set_base=Pin(26))									# 1.81..17.86 MHz on GPIO-26 as specified in Y

# =========================== end ==============================