micropython / micropython/micropython

RP2040 PIO: Script fails on some GPIO numbers

Open
#18,080 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug port-rp2
Dominant language
C
Stars
22.1k
Forks
9k
Avg merge
6d 4h
Merged PRs (30d)
16

Description

Port, board and/or hardware

RP Pico (RP2040)

MicroPython version

MicroPython v1.26.1 on 2025-09-11; Raspberry Pi Pico with RP2040

Reproduction

Link GPIO 1-17, attach a scope/LA to GPIO 2, and run the following:

from machine import Pin, PWM
import rp2
import time

inp = Pin(0, Pin.IN)
jmp = Pin(1, Pin.IN)
opp = Pin(2, Pin.OUT, value=1)
pwm = PWM(Pin(17))  # Link GPIO 17 to GPIO 1
pwm.freq(1000)
pwm.duty_u16(0xFFFF // 2)

@rp2.asm_pio(out_init=rp2.PIO.OUT_LOW) 
def sm_test():
    set(x, 0)
    wrap_target()
    wait(0, pins, 1)  # Wait for GPIO 1 to go low
    mov(osr, x)  # Ouput==0 while input==0
    out(pins, 1)
    label("loop")  # Loop until jump pin goes high
    jmp(pin, "pulse") 
    jmp("loop")
    label("pulse")
    mov(osr, invert(x))  # Emit a pulse
    out(pins, 1)
    mov(osr, x)
    out(pins, 1)
    wrap()

sm0 = rp2.StateMachine(0, sm_test, freq=100_000, out_base=opp , in_base=inp, jmp_pin=jmp)
sm0.restart()
sm0.active(1)
while True:
    time.sleep(1)
    print("Running")
Expected behaviour

Expected to output a single pulse on GPIO 2 after every positive going edge of GPIO 17.

Observed behaviour

As written, works as designed.

If the link is changed to link GPIO 17 and 14, and the code changed to read

inp = Pin(13, Pin.IN)
jmp = Pin(14, Pin.IN)

pulses are emitted all the time the input is high. If GPIO 1 is strapped to 3V3, pulses stop. Note GPIO 1 is not referenced in the code.

If GPIO 1 and GPIO 14 are linked it runs as designed. This behaviour continues after a power cycle!

Additional Information

RP2350 behaves similarly except that the sensitivity to GPIO 1 is lacking.

The decorator arg is necessary for the code to work. If this is intended it should be documented as this seems unexpected.

Code of Conduct

Yes, I agree

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the RP2040 case with the supplied rp2 PIO decorator and StateMachine code, comparing GPIO 1/14 behavior across power cycles. Then trace the rp2 PIO handling involved; done means the unexpected pulses are fixed or the decorator requirement and GPIO sensitivity are documented if intentional.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.