playing with PIO on a Pico - constructive criticism

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
pjr4171
 
Posts: 15
Joined: Wed Mar 10, 2021 1:54 am

playing with PIO on a Pico - constructive criticism

Post by pjr4171 »

I am trying to get my head around PIO on the Pico in CircuitPython. In order to stress test my understanding I decided to write a state machine for debouncing up to 8 buttons in parallel. I know this could be done directly in CP but my aim is to do it via PIO.

If anyone is up for giving my code some constructive criticism (including my comments - maybe I have some misunderstanding) it would be greatly appreciated.

Say I have NUM_BUTTONS = 5
in my CP code. Is there a way to reference NUM_BUTTONS in my assembler code?

Here is my code:

Code: Select all

# Debounce up to 8 buttons in parallel

import board
import rp2pio
import adafruit_pioasm

debounce = """
.program debounce

    pull block      ; wait for write (and ignore value)
    in pins, 8      ; set ISR to the state of the button
    mov x, isr      ; save ISR to x
loop:
    out y, 20       ; loop counter for sample loop
sample_loop:
    nop [31]        ; wait a bit before taking another sample
    mov osr, y      ; save y (loop counter) into OSR
    mov y, x        ; save current ISR value for next comparison
    in pins, 8      ; take annother sample
    mov x, isr      ; save ISR to x for testing
    jmp x!=y loop   ; state changed - start again
    mov y, osr      ; reload loop counter into y for decrement/testing
    jmp y-- sample_loop
    
    in x,8          ; the state of the buttons has been stable
    push            ; put x into ISR and output

"""

assembled = adafruit_pioasm.assemble(debounce)

sm = rp2pio.StateMachine(
        assembled,
        frequency=2000,
        first_in_pin=board.GP16,
        in_pin_count = 8,  
    )

def buttons_state():
    """ Return a list of booleans representing the state of the buttons"""
    b = bytearray(1)
    sm.write_readinto(bytes((0,)), b)
    state = b[0]
    return [(state & (1 << i)) != 0 for i in range(8)]
    

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”