How to create Programmable IO asm function for RPi pico in runtime #9290
Replies: 3 comments 5 replies
-
You can assemble individual instructions like this: >>> import rp2
>>> rp2.asm_pio_encode('pull()',1)
32928
>>> rp2.asm_pio_encode('set(x,31)[6]',1)
58943 But then you have to put them together in an array to pass to a StateMachine. Probably easier to use >>> exec('import rp2\n@rp2.asm_pio()\ndef f():\n pull()\n set(x,31)[6]', out:={})
>>> out
{'f': [array('H', [32928, 58943]), -1, -1, 4096, 0, None, None, None], 'rp2': <module 'rp2' from 'rp2.py'>} |
Beta Was this translation helpful? Give feedback.
-
Sorry, this perhaps doubles (the other answer was not there when i wrote this), but perhaps this is a slightly other option:
Also you should perform |
Beta Was this translation helpful? Give feedback.
-
Here is another solution: put the import time
from machine import Pin
import rp2
def create_blinker(raise_irq, cycles):
delay = (cycles // 2 - 8) // 32 - 2
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
def blink():
# fmt: off
# Cycles: 1 + 1 + 6 + 32 * (30 + 1) = 1000
if raise_irq:
irq(rel(0))
set(pins, 1)
set(x, 31) [6 - bool(raise_irq)]
label("delay_high")
nop() [delay]
jmp(x_dec, "delay_high")
# Cycles: 1 + 7 + 32 * (30 + 1) = 1000
set(pins, 0)
set(x, 31) [6]
label("delay_low")
nop() [delay]
jmp(x_dec, "delay_low")
# fmt: on
return blink
# Create the StateMachine with the blink_1hz program, outputting on Pin(25).
sm = rp2.StateMachine(0, create_blinker(True, 2000), freq=2000, set_base=Pin(25))
# Set the IRQ handler to print the millisecond timestamp.
sm.irq(lambda p: print(time.ticks_ms()))
# Start the StateMachine.
sm.active(1) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
there is a well known decorator for asm pio code within the PY script, such as:
@asm_pio(sideset_init=PIO.OUT_LOW)
def pwm_prog():
pull
set(x, 31) [6]
etc..
What I need to achieve, though, is to assemble the asm function in runtime (need to modify this [6] - in this example - to pause for a correct number of cycles and I don't know exact number in advance as this depends on measurements provided by gpio ticking I measure when the script is started).
I thought of really ugly ways such as writing a new .py file with this function by the master script itself, importing it afterwards and then using, but hope there could be a better way to achieve this? Basically, instead of a function, providing a string with the code or whatever reasonable way?
Thank You so much,
Stepan
Beta Was this translation helpful? Give feedback.
All reactions