Hi,
I am new to the forum and Python so I hope I am providing enough information.
I am using a Raspberry Pi Pico with 2040 cpu.
When running a program with several state machines each connected to one input pin and generating interrupts I want to take different actions depending upon which state machine(input pin) that generated the interrupt. In this example program I get a printout saying StateMachine(0) or StateMachine(1) depending on where the interrupt came from. However I don't know how to use this for testing. A compare with 'StateMachine(0)' does not work when I try it and I think I can see why. But is there a way to check for the interrupting state machine number.
In the real application I plan to receive four channels with PWM pulses from a RC-receiver, measure the pulse length and activate different functions depending on the pulse length.
What am I missing?
PS I have seen examples written in C but would prefer to use Python in this case.
Test program code from SDK example:
# Example using PIO to wait for a pin change and raise an IRQ.
#
# Demonstrates:
# - PIO wrapping
# - PIO wait instruction, waiting on an input pin
# - PIO irq instruction, in blocking mode with relative IRQ number
# - setting the in_base pin for a StateMachine
# - setting an irq handler for a StateMachine
# - instantiating 2x StateMachine's with the same program and different pins
import time
from machine import Pin
import rp2
@rp2.asm_pio()
def wait_pin_low():
wrap_target()
wait(0, pin, 0)
irq(block, rel(0))
wait(1, pin, 0)
wrap()
def handler(sm):
# Print a (wrapping) timestamp, and the state machine object.
print(time.ticks_ms(), sm)
#Here I would like to test which input generated the interrupt
#and store each result in a separate place for later use
# Instantiate StateMachine(0) with wait_pin_low program on Pin(16).
pin16 = Pin(16, Pin.IN, Pin.PULL_UP)
sm0 = rp2.StateMachine(0, wait_pin_low, in_base=pin16)
sm0.irq(handler)
# Instantiate StateMachine(1) with wait_pin_low program on Pin(17).
pin17 = Pin(17, Pin.IN, Pin.PULL_UP)
sm1 = rp2.StateMachine(1, wait_pin_low, in_base=pin17)
sm1.irq(handler)
# Start the StateMachine's running.
sm0.active(1)
sm1.active(1)
# Now, when Pin(16) or Pin(17) is pulled low a message will be printed to the REPL.
Printed result
MPY: soft reboot
15226251 StateMachine(0)
>15226252 StateMachine(1)
15237332 StateMachine(0)
15237830 StateMachine(0)
15237837 StateMachine(1)
15237851 StateMachine(0)
15237857 StateMachine(1)
15237871 StateMachine(0)
I am new to the forum and Python so I hope I am providing enough information.
I am using a Raspberry Pi Pico with 2040 cpu.
When running a program with several state machines each connected to one input pin and generating interrupts I want to take different actions depending upon which state machine(input pin) that generated the interrupt. In this example program I get a printout saying StateMachine(0) or StateMachine(1) depending on where the interrupt came from. However I don't know how to use this for testing. A compare with 'StateMachine(0)' does not work when I try it and I think I can see why. But is there a way to check for the interrupting state machine number.
In the real application I plan to receive four channels with PWM pulses from a RC-receiver, measure the pulse length and activate different functions depending on the pulse length.
What am I missing?
PS I have seen examples written in C but would prefer to use Python in this case.
Test program code from SDK example:
Code:
#
# Demonstrates:
# - PIO wrapping
# - PIO wait instruction, waiting on an input pin
# - PIO irq instruction, in blocking mode with relative IRQ number
# - setting the in_base pin for a StateMachine
# - setting an irq handler for a StateMachine
# - instantiating 2x StateMachine's with the same program and different pins
import time
from machine import Pin
import rp2
@rp2.asm_pio()
def wait_pin_low():
wrap_target()
wait(0, pin, 0)
irq(block, rel(0))
wait(1, pin, 0)
wrap()
def handler(sm):
# Print a (wrapping) timestamp, and the state machine object.
print(time.ticks_ms(), sm)
#Here I would like to test which input generated the interrupt
#and store each result in a separate place for later use
# Instantiate StateMachine(0) with wait_pin_low program on Pin(16).
pin16 = Pin(16, Pin.IN, Pin.PULL_UP)
sm0 = rp2.StateMachine(0, wait_pin_low, in_base=pin16)
sm0.irq(handler)
# Instantiate StateMachine(1) with wait_pin_low program on Pin(17).
pin17 = Pin(17, Pin.IN, Pin.PULL_UP)
sm1 = rp2.StateMachine(1, wait_pin_low, in_base=pin17)
sm1.irq(handler)
# Start the StateMachine's running.
sm0.active(1)
sm1.active(1)
# Now, when Pin(16) or Pin(17) is pulled low a message will be printed to the REPL.
Printed result
MPY: soft reboot
15226251 StateMachine(0)
>15226252 StateMachine(1)
15237332 StateMachine(0)
15237830 StateMachine(0)
15237837 StateMachine(1)
15237851 StateMachine(0)
15237857 StateMachine(1)
15237871 StateMachine(0)
Statistics: Posted by rccontrol — Fri Mar 14, 2025 9:56 pm