Page 1 of 1

Using PIO OUT instruction for parallel interfaces

Posted: Tue Mar 23, 2021 8:32 am
by rafl
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!

Re: Using PIO OUT instruction for parallel interfaces

Posted: Tue Mar 23, 2021 9:44 am
by Roberthh
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

Re: Using PIO OUT instruction for parallel interfaces

Posted: Tue Mar 23, 2021 10:19 am
by rafl
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.

Re: Using PIO OUT instruction for parallel interfaces

Posted: Tue Mar 23, 2021 10:27 am
by Roberthh
The bug mentioned here https://github.com/micropython/micropython/issues/6933 is fixed in the actual daily builds.

Re: Using PIO OUT instruction for parallel interfaces

Posted: Tue Mar 23, 2021 10:42 am
by rafl
Thanks - I didn't realise daily builds were a thing and just built from source after seeing the fix was merged.