rp2 import PIO, StateMachine, asm_pio -> Info -> Black Cat in a Coal Bunker
- μpython v1.14 on 2021-04-09 (GNU 10.2.0 MinSizeRel)
from - Raspberry Pi Pico Python SDK (2021-04-07)
The idea is that for the 4 sets of pins ( in , out , set , sideset , excluding jmp ) that can be connected to a state machine,
there’s the following that need configuring for each set:
Sample code:
from rp2 import PIO, StateMachine, asm_pio
from machine import Pin
@asm_pio(set_pins=(PIO.OUT_LOW, PIO.OUT_HIGH, PIO.IN_LOW), sideset_pins=PIO.OUT_LOW)
def foo():
pass
sm0 = StateMachine( 0, foo, freq=2000, set_pins=Pin(15), sideset_pins=Pin(22))
it just produces errors
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
File "rp2.py", line 230, in asm_pio
TypeError: unexpected keyword argument 'set_pins'
I can find info -> machine -> https://docs.micropython.org/en/latest/library/machine.html
but info for rp2 -> null
looking in the rp2 stubs - StateMachine
def __init__(self, id, prog, freq: int=-1, *, in_base: Pin=None, out_base: Pin=None, set_base: Pin=None, jmp_pin: Pin=None, sideset_base: Pin=None, in_shiftdir: int=None, out_shiftdir: int=None, push_thresh: int=None, pull_thresh: int=None):
Ureka - moment - there NO set_pins in StateMachine instantiation
from rp2 import PIO, StateMachine, asm_pio
from machine import Pin
@asm_pio(set_pins=(PIO.OUT_LOW, PIO.OUT_HIGH, PIO.IN_LOW), sideset_pins=PIO.OUT_LOW)
def foo():
pass
sm0 = StateMachine( 0, foo, freq=2000, set_base=Pin(15), out_base=Pin(16), sideset_base=Pin(22))
looking in the rp2 stubs - asm_pio
def asm_pio(set_init: int = None, out_shiftdir: int = None, autopull: bool = None, pull_thresh: int = None, set_pins: Iterable[Sequence[int]] = None, sideset_pins: int = None, sideset_init: int = None, out_init: int = None, autopush: bool = None, push_thresh: int = None, in_base: int = None, out_base: int = None) -> Any:
Just hit the brick wall:(
Any chance someone that knows can put a working example up:
The idea is that for the 4 sets of pins ( in , out , set , sideset , excluding jmp ) that can be connected to a state machine,
there’s the following that need configuring for each set:
- base GPIO
- number of consecutive GPIO
- initial GPIO direction (in or out pin)
- initial GPIO value (high or low)
or do I need to add a documentation request -> Roadmap to next release v1.15
Thanks in Adv.
rp2 PIO: two statemachines cannot share the same pins if they run on differenit PIOs
Hello
I spotted the following issue:
When trying to run two different programs in two statemachines sharing the same pin(s), they work fine ONLY if both statemachines belong to the same PIO (0 or 1), eg one in PIO(0) statemachine 0 and the other in PIO(0) statemachine 1.
The situation changes if the two programs are in different PIOs (one in 0 and the other in 1)
(micropython 1.20 nightly built June 15 2023)
test code:
import time
import rp2
from machine import Pin
@rp2.asm_pio(set_init=(rp2.PIO.OUT_LOW))
def prog_0():
set(pins, 1) [31]
nop() [31]
nop() [31]
nop() [31]
set(pins, 0) [31]
nop() [31]
nop() [31]
nop() [31]
irq(0)
@rp2.asm_pio(set_init=(rp2.PIO.OUT_LOW))
def prog_1():
set(pins, 1) [31]
nop() [31]
nop() [31]
nop() [31]
nop() [31]
nop() [31]
nop() [31]
set(pins, 0) [31]
nop() [31]
nop() [31]
nop() [31]
nop() [31]
nop() [31]
nop() [31]
irq(1)
def irq_handler_0(pio):
print('pio_0', pio.irq().flags())
def irq_handler_1(pio):
print('pio_1', pio.irq().flags())
pio_0 = rp2.PIO(0)
pio_0.irq(irq_handler_0)
pio_1 = rp2.PIO(1)
pio_1.irq(irq_handler_1)
sm0 = pio_0.state_machine(0, prog_0, freq=2000, set_base=Pin(7))
sm1 = pio_1.state_machine(1, prog_1, freq=2000, set_base=Pin(7))
sm0.active(1)
sm1.active(1)
time.sleep(3)
sm0.active(0)
sm1.active(0)
In the above code, the interrupts are working fine (from both programs), so both programs are running, but the led does not respond to instructions of the FIRST assigned statemachine (sm0)
The situation is the same, even if I never activate the second statemachine (sm1): the led does not blink at all (by sm0).
If sm1 is in the same PIO, that is if I change the line
sm1 = pio_1.state_machine(1, prog_1, freq=2000, set_base=Pin(7))
to
sm1 = pio_0.state_machine(1, prog_1, freq=2000, set_base=Pin(7))
everything works fine
The need of having programs running in different PIOs is that you can have more memory (32 instructions per PIO).
Thanks in advance