I'm working on incrementally adding complexity to PIO programs to learn how to use them. Here are a few examples. Note that I'm using Pin 6 for the LED on the Arduino Connect Nano RP2040 board. I think the pin is different for the Pi Pico.
Most Simple Blinking the LED:
The most simple classic blinking example - toggle a pin on and off with the slowest freqency (2000) and a bunch of nop waits to slow the blinking down enough that we can see it!
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
def blink():
wrap_target()
set(pins, 1) [31]
nop() [31]
nop() [31]
nop() [31]
nop() [31]
set(pins, 0) [31]
nop() [31]
nop() [31]
nop() [31]
nop() [31]
wrap()
# sm = rp2.StateMachine(0, pio_junk.blink, freq=2000, set_base=Pin(6))
# sm.active(1)
Blinking the LED Using sideset and jmp for more nop:
In this example, we use sideset to toggle the LED pin on and off and jmp to loop for more nop.
Normally, in each PIO assembled instruction, 5 bits are available to specify length of wait, so 0-31 wait cycles. But when we enable sideset, 2 (or more??) bits are used to specify the sideset value. In this case, we use Pin 6 (the LED) as the sideset pin, and we have a max wait of 7 (three bits).
This speeds up the program execution without a bunch more nop calls. So we can count on the x scratch register to repeatedly call nop with a jmp and slow the blinking enough that we can see it, like before using sideset and the longer [31] wait.
@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW)
def side_blink():
wrap_target()
set(x, 31).side(0x0)
label("aloop")
nop() [7]
jmp(x_dec, "aloop")
set(x, 31).side(0x1)
label("bloop")
nop() [7]
jmp(x_dec, "bloop")
wrap()
# sm = rp2.StateMachine(0, pio_junk.side_blink, freq=2000, sideset_base=Pin(6))
# sm.active(1)
Up next...
I'm trying to read the value of all of the pins and put the values into the fifo to be used by the Micropython program...
1 comment:
Very valuable for those of us trying to make sense of things like sideset.
Post a Comment