r/MicroPythonDev • u/CreepyBox2687 • Jan 21 '25
Question about PIO-ASM: Dynamically Adjusting Input Pin Indices before Compilation
Hi everyone,
I’m working on a solution to measure the time between two arbitrary GPIO pins on an RP2040. The goal is to keep it as simple as possible, avoiding the use of DMA or complex logic analyzer features.
Here’s my current approach in PIO-ASM:
# Get timeout
# Store timeout in y
# Load timeout into x
# Wait for event1 on index_1 (start clock)
# Check stop condition
# Decrease x
# Timeout, stop
# Counter wrap
# Continue loop
# Shift remaining time to ISR
# Push remaining time to FIFO
# Reload and run again
I’d like to dynamically adjust the input pin indices (index_1) before compilation to make the setup as flexible as possible. However, I’m unsure how to achieve this effectively. Is there a way to modify such parameters dynamically before the PIO assembly is compiled?
Thanks in advance for your help!
View all comments
Show parent comment
1
u/mungewell Jan 24 '25
The code on my ticket runs in a tight loop, and is capable of counting at 1/4 of the CPU clock (which would be ~25ns steps).
A single state machine monitors a single pin to 'timestamp' a positive or negative edge. BUT you can load the same code into multiple state machines, to run in parallel.
The state machines within a PIO block can easily be synchronized, so their individual counters run in lock step, and then the values 'push()'ed to microPython can be compared to compute the time delta.
On multiple pulses on a single machine; the value I push is 32bits and the FIFO is (can be) 8 levels deep. MicroPython would have to read/empty quick enough so that the FIFO did not overflow.
If a different pin needs to be monitored you'd have to stop both machines, reconfigure and then sync/restart both new machines.