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.StateMachine: state machine fails when I2S protocol is used
A rp2 state machine instance can be made to fail when the I2S protocol is used.
Minimal test to reproduce
The code below uses a PIO state machine to flash the onboard LED on a Raspberry Pi Pico development board.
- Working case: Onboard LED will flash for STATEMACHINE_INSTANCE = 1...7
- Failure case: Onboard LED will NOT flash for STATEMACHINE_INSTANCE = 0 (shows the Issue)
import time
from machine import Pin, I2S
import rp2
STATEMACHINE_INSTANCE = 0
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
def prog():
pass
sm = rp2.StateMachine(STATEMACHINE_INSTANCE, prog, set_base=Pin(25))
i2s = I2S(0, sck=Pin(16), ws=Pin(17), sd=Pin(18), mode=I2S.TX, bits=16, format=I2S.STEREO, rate=22_050, ibuf=10000)
while True:
sm.exec("set(pins, 1)")
time.sleep(0.5)
sm.exec("set(pins, 0)")
time.sleep(0.5)
In the failure case (STATEMACHINE_INSTANCE = 0) , both the rp2.StateMachine instance and the machine.I2S instance try to use state machine 0. Because I2S instantiation follows the rp2.StateMachine instantiation, the state machine configuration used by rp2.StateMachine is overwritten by I2S initialization. This causes a failure in the rp2.StateMachine instance (LED does not flash).
The root cause of this Issue seems to emerge from the rp2.StateMachine implementation. rp2.StateMachine does not make use of the functions for cooperative claiming of hardware that are provided by the Pico SDK. e.g. pio_sm_is_claimed(), pio_sm_unclaim(), pio_claim_unused_sm(). This does not allow any other MicroPython entities (such as I2S) to detect state machine configuration conflicts.
Suggestions for a fix to rp2.StateMachine implementation:
Enhance the rp2.StateMachine implementation to use the Pico SDK functions for cooperative claiming of PIO hardware. The I2S class implementation shows one way to use these functions, in the functions pio_configure() and pio_deinit().