RP2 PIO StateMachine.exec() function #16780
-
Hello, this a general question about the inner workings of the rp2.StateMachine.exec() function. If I execute this function on a SM which is already running, how does it impact or interfere with the normal operation of the SM? Are instructions run using "exec" injected at specific points in the SM program? In particular, I am building a rate-meter using an RP2040, based around the (https://github.com/dhylands/upy-examples/blob/master/pico/pio_pulse_counter.py) pulse_counter() which continuously accumulates pulses in a PIO SM: @rp2.asm_pio() Every fixed time interval (10 to 1000 ms) the main program retrieves the pulse count for that specific time interval (it subtracts the previous value and handles overflows), by executing sm.exec("mov(isr, x)") I was wondering if, with the PIO SM continuously running, executing the code above at fixed intervals could affect or interfere with the normal SM operation, resulting in pulse count loss. How are the instructions run with “exec” inserted in the normal SM operation? would it be wiser to have the SM written as: def pulse_counter(): so that the main program will only act on ISR FIFO via sm.get() ? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
It might actually work pretty well to set your program up with autopush at 32 bits, and then just every so often do
this effectively peeks asynchronously at the x register. I haven't tried it, but it should work. |
Beta Was this translation helpful? Give feedback.
-
SM:
SM2: SM1:
SM1: and SM2, via DMA (piped from SM1 to SM2) would always access the updated count value, finally pushing it to the main program whenever needed. |
Beta Was this translation helpful? Give feedback.
What you were planning is pretty much exactly the way this can be used. Since the instructions you are exec-ing don't create any race conditions (they don't depend on where the program is executing), except for the exact snapshot time you capture 'x' in the ISR, it should do just what you want. All the 'exec' command does to the SM is (after compiling the instruction, if needed), writes the word to the appropriate register in the PIO register block. The hardwired SM logic handles how it gets put into the instruction stream.