Using PIO OUT instruction for parallel interfaces

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
rafl
Posts: 10
Joined: Tue Mar 23, 2021 7:15 am

Using PIO OUT instruction for parallel interfaces

Post by rafl » Tue Mar 23, 2021 8:32 am

I wrote my first few PIO programs today and had success using set and side-set mappings to have PIO programs change GPIO pins. I now want to move on to interfacing with a device that needs more pins than set or side-set mappings can provide, so I want to use an out mapping instead.

I started with

Code: Select all

@rp2.asm_pio(out_init=(rp2.PIO.OUT_HIGH,)*6, out_shiftdir=rp2.PIO.SHIFT_RIGHT)
def prog0():
    pull() # just hang

rp2.StateMachine(0, prog0, freq=2000, out_base=machine.Pin(4)).active(1)
This seems to work as expected, driving GPIO pins 4 through 9 high.

I then moved on to

Code: Select all

@rp2.asm_pio(out_init=(rp2.PIO.OUT_HIGH,)*6, out_shiftdir=rp2.PIO.SHIFT_RIGHT)
def prog1():
    mov(osr, 0xffffffff) # fill osr with all 1s
    out(pins, 6) # output the 6 least-significant bits of osr to our 6 pins
    pull()

rp2.StateMachine(0, prog1, freq=2000, out_base=machine.Pin(4)).active(1)
In this example I would've expected all six pins to remain high, but instead I observe all of them being driven low.

Why is that? Did I misunderstand how the OUT instruction works or how it interacts with the output shift register or out IO mapping?

Thanks!

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Using PIO OUT instruction for parallel interfaces

Post by Roberthh » Tue Mar 23, 2021 9:44 am

The statement:

mov(osr, 0xffffffff) # fill osr with all 1s

is wrong. there is no way to use an arbitrary constant as source. You have to use:

mov(osr, invert(null)) # fill osr with all 1s

rafl
Posts: 10
Joined: Tue Mar 23, 2021 7:15 am

Re: Using PIO OUT instruction for parallel interfaces

Post by rafl » Tue Mar 23, 2021 10:19 am

Thank you for clearing up my misunderstanding, Robert. There isn't even space in the instruction to fit a 32 bit value :oops:!

With that insight, and the work you and others put into https://github.com/micropython/micropython/issues/6933, I am now able drive the 10 GPIOs I need for this particular application.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Using PIO OUT instruction for parallel interfaces

Post by Roberthh » Tue Mar 23, 2021 10:27 am

The bug mentioned here https://github.com/micropython/micropython/issues/6933 is fixed in the actual daily builds.

rafl
Posts: 10
Joined: Tue Mar 23, 2021 7:15 am

Re: Using PIO OUT instruction for parallel interfaces

Post by rafl » Tue Mar 23, 2021 10:42 am

Thanks - I didn't realise daily builds were a thing and just built from source after seeing the fix was merged.

Post Reply