PIO - pull() question

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
Kostrinker
Posts: 4
Joined: Tue Aug 10, 2021 10:05 am

PIO - pull() question

Post by Kostrinker » Thu Mar 10, 2022 7:08 am

#### EDITED #####

Hi all,

I am trying to output a value on a pin one bit at the time using PIO.

I would like to continuously repeat the value put in the TX FIFO until a new sm.put() instruction is run. Is there a way to refill the OSR by recycling the last putted data in the TX FIFO?

I wrote the code below, thinking it would do the trick
1) Pull without blocking (so recycling the last valid TX FIFO data) <- this is uncorrect, if TX FIFO empty X register will be moved to OSR
2) entering in a loop
3) outputting 1 bit a the time on the pin
4) When OSR empty exit loop and pull new block

Code: Select all

from machine import Pin
import rp2

@rp2.asm_pio(out_init=(rp2.PIO.OUT_LOW,)*1)
def pulse():
    wrap_target()
    pull(noblock)  #Pull the value in the OSR
    label("entry") #Entrance in the loop
    out(pins,1)    # Output one bit from the OSR
    jmp(not_osre,"entry") #If OSR not empty jump back to the loop
    wrap()
      
sm1 = rp2.StateMachine(0, pulse, freq=1_000_000, out_base=Pin(12)) 
sm1.put(234)
sm1.active(1) 
This code wasn't working because using pull(noblock) would move the X register to the OSR (and the X register was not used). To solve this problem I changed my code to:

Code: Select all

@rp2.asm_pio(out_init=(rp2.PIO.OUT_LOW,)*1)
def pulse():
    pull() #Pull the data from OSR
    mov(x,osr) #Move the OSR data to X register
    wrap_target()
    pull(noblock) #Pull without blocking (since FIFO empty, OSR will be updated from X register)
    label("loop") #Loop for outputting one bit at the time from the OSR to the pin
    out(pins,1) #Output 1 bit 
    jmp(not_osre,"loop") #If OSR not empty get back to the loop
    
    wrap()  
The output suffer some jitter (so I am probably still doing something not completely correct) but hope it can help somebody else,
Best,
Kos

Post Reply