I would like to stop the PIO and set the output pin in the main loop. But it looks to me that it's not possible to set the pin output state from the main loop when it's defined to be the output of the PIO. Is this feature not available?
As an example I extended the LED blink example:
Code: Select all
import utime
import rp2
from machine import Pin
p0 = Pin(25,Pin.OUT)
# Blink state machine program. Blinks LED at 10 Hz (with freq=2000)
# 2000 Hz / (20 cycles per instruction * 10 instructions) = 10 Hz
# Single pin (base pin) starts at output and logic low
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
def blink():
wrap_target()
set(pins, 1) [19]
nop() [19]
nop() [19]
nop() [19]
nop() [19]
set(pins, 0) [19]
nop() [19]
nop() [19]
nop() [19]
nop() [19]
label("wait") # y set to '0' results in an wait loop
jmp(not_y,"wait")
wrap()
# Init state machine with "blink" program
# (state machine 0, running at 2kHz, base pin is GP25 (LED))
sm = rp2.StateMachine(0, blink, freq=2000, set_base=Pin(25))
# Continually start and stop state machine
while True:
print("Starting state machine...")
sm.exec("set(y, 1)")
sm.active(1)
utime.sleep(1)
print("Stopping state machine...")
sm.exec("set(y, 0)") // first stop it via register y (LED then is OFF)
utime.sleep(0.1)
sm.active(0)
utime.sleep(1)
print("LED on...")
p0.value(1)
utime.sleep(1)
print("LED off...")
p0.value(0)
utime.sleep(1)
Code: Select all
sm.exec("mov(osr,1)")
sm.exec("out(pins,32) ")