# RP2040 - large range period square wave with multiple outputs
# by Anthony Vogelaar, Perth Scotland
# rev 0.10  2025-08-22  Initial release

from	machine		import	Pin
import	rp2

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

# Frequency on Led and GPIO-26 is 1 Hz
@rp2.asm_pio(set_init=(rp2.PIO.OUT_LOW, rp2.PIO.OUT_LOW))
def freq1HzLed():
    set(y, 0x01); in_(y, 5)				# shift 5 bit from Y --> ISR
    set(y, 0x1B); in_(y, 5)
    set(y, 0x13); in_(y, 5)
    set(y, 0x0B); in_(y, 5)
    set(y, 0x05); in_(y, 5)
    set(y, 0x00); in_(y, 5)
    mov(y, isr)
    label('loop')
    mov(x, y)
    set(pins, 0b11)
    label('l1')
    jmp(x_dec, 'l1')					# possible to add up to 31 * nop()
    mov(x, y)
    set(pins, 0b00)
    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(freq1HzLed, set_base=Pin(25))								# 1 Hz Led and GPIO-26

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