← index #13248Issue #11797
Related · high · value 0.771
QUERY · ISSUE

rp2 PIO state machine pin config enhancements

openby nludbanopened 2023-12-21updated 2023-12-21
enhancement

Several annoyances with trying to configure pins for PIO, lumping them into one ticket since they're minor and overlapping in the implementation.

The rp2040 datasheet, 3.5.6 GPIO Mapping, specifies pin base + count are "wrapping after GPIO31". The rp2 port does not currently support this. Since this is only executed once, a loop to configure one pin at a time would be fine:
https://github.com/micropython/micropython/blob/5d28bb4adb24094917038a506248ff7fb351ac44/ports/rp2/rp2_pio.c#L214-L215
https://github.com/micropython/micropython/blob/5d28bb4adb24094917038a506248ff7fb351ac44/ports/rp2/rp2_pio.c#L227

The rp2 port requires initial value and direction to be specified for every pin in every group. When the pin groups (in, out, set, etc) overlap, initial value and direction must be duplicated and there are no checks for conflicts. It may also be desirable for multiple state machines to use the same pin, one for output and one for input - seems to work, at least when on the same PIO. Feature request is to allow None for the initial setting, the user is then responsible for making sure the configuration happens.

Pull (up and down) should be configurable in the same place as value and direction. Workaround is to instantiate a Pin or poke the config registers directly.

Open drain can be emulated in the rp2040 with value=0 and changing the pin direction to drive or float the output. It also needs a pull up to be configured. Request is to be able to specify PINDIR_OD_LOW or PINDIR_OD_HIGH so the code is all in one place with obvious intent.

CANDIDATE · ISSUE

rp2 PIO: two statemachines cannot share the same pins if they run on differenit PIOs

closedby makis-mopened 2023-06-16updated 2023-06-17
bug

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

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied