PIO: meaning of PIO.OUT_HIGH and PIO.OUT_LOW

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
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

PIO: meaning of PIO.OUT_HIGH and PIO.OUT_LOW

Post by pythoncoder » Sun Feb 21, 2021 4:48 pm

I have been playing with this script from the official demos:

Code: Select all

import time
from machine import Pin
import rp2

# Define an empty program that uses a single set pin.
@rp2.asm_pio(set_init=rp2.PIO.OUT_HIGH)
def prog():
    pass

# Construct the StateMachine, binding Pin(25) to the set pin.
sm = rp2.StateMachine(0, prog, set_base=Pin(25))

# Turn on the set pin via an exec instruction.
sm.exec("set(pins, 1)")
In my testing this behaves identically whether I use rp2.PIO.OUT_HIGH or rp2.PIO.OUT_LOW, and I haven't yet found anything in the docs to explain it.

Can anyone help? Also with the matching IN_x values. The PIO has a steep learning curve ;)
Peter Hinch
Index to my micropython libraries.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Half solved. I think.

Post by pythoncoder » Sun Feb 21, 2021 6:11 pm

I've found this on P24 of this doc:
You need to specify an initial OUT pin state in your program in order to be able to pass OUT mapping to your SM instantiation
So it defines the level on the pin before you set it; with hindsight I guess this is obvious. It still doesn't make a lot of sense for an input pin.
Peter Hinch
Index to my micropython libraries.

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

Re: PIO: meaning of PIO.OUT_HIGH and PIO.OUT_LOW

Post by Roberthh » Sun Feb 21, 2021 7:22 pm

What confused me a little bit besides the missing documentation, was the fact, the the length of the tuple assigned to iout_base, sideset_base defines the number of pins used for it. Therefore you have to specify something, even for IN. By the program logic it has to be 2 values for in. It could have been something useful like specifying the Pull. But maybe it is.
Edit: Glancing through the code I see no hint, how I can define more than one Pin as input? Is that not possible, or was is simple forgotten in the implementation?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: PIO: meaning of PIO.OUT_HIGH and PIO.OUT_LOW

Post by pythoncoder » Mon Feb 22, 2021 8:36 am

Does this, from the SPI example in the Python doc, give a clue?

Code: Select all

self._sm = rp2.StateMachine(sm_id, spi_cpha0, freq=4*freq, sideset_base=Pin(
pin_sck), out_base=Pin(pin_mosi), in_base=Pin(pin_sck))
The Python doc is seriously lacking :(

[EDIT]
My understanding of I/O is that input and output pins must be contiguous. Input from multiple pins (using in_ or mov) is done by specifying a number of bits.

But do tell me if I'm adrift here, I'm struggling with this doc.

I'm baffled why, in the SPI example, we have

Code: Select all

in_base=Pin(pin_sck)
rather than pin_miso
Peter Hinch
Index to my micropython libraries.

cebersp
Posts: 30
Joined: Mon Feb 08, 2021 12:07 pm

Re: PIO: meaning of PIO.OUT_HIGH and PIO.OUT_LOW

Post by cebersp » Mon Feb 22, 2021 9:39 am

Yes, docu is not completely clear....
There seem to be more than one places where to define the initial state of pins.
For my sigma delta adc I would like to use the same pin as input and output. It is not clear to me, if this is possible at all and if yes, then how to set it up and how to switch direction dynamicly.

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

Re: PIO: meaning of PIO.OUT_HIGH and PIO.OUT_LOW

Post by Roberthh » Mon Feb 22, 2021 10:10 am

There are several ways to do so in the PIO code. You can change the Pin direction by writing with PINDIRS as target. You can to so with the SET and the OUT instrucion. Set takes the bit pattern from the argument, using the pib numbers starting at set_base. So it is limited to 5 pins at once. OUT takes the bit pattern from the OSR, using out_base as the lowest numbered pin, and is able to set up to 32 bits in one instruction. A bit value of 1 means output, 0 means input.

What confused me more is that using out_init in the decorator you can specify a number of pins as output, but there is no equivalent for input. Howeevr, when looking through the code, it seems to me that using IN_LOW or IN_HIGH at the out_init or set_init declaration that pin would be set to IN mode. A first test did reveal that.

See. ports/rp2/rp2_pio.c, lines 181-188 and
lib/pico-sdk/src/rp2-common/hardware-pio/pio.c, lines 172-196.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: PIO: meaning of PIO.OUT_HIGH and PIO.OUT_LOW

Post by pythoncoder » Mon Feb 22, 2021 1:01 pm

Just in case you guys haven't got there first...

It took me a long while to get it to jump on a pin. The trick is

Code: Select all

sm0 = rp2.StateMachine(0, period, in_base=pin16, jmp_pin=pin16)
The documentation for this is here which gives a clue about other args.
Peter Hinch
Index to my micropython libraries.

Post Reply