This technique is not specific to the RP4020, so is described generically.
In brief
The direction register is selecting between a high when dir is out (caused by 1 in the data output register) or a low when dir is input (caused by the pulldown).
This technique is used so that another circuit (often the same at the other end) can pull the line up when we are weakly pulling it down. Thus we can make a bidirectional circuit that can be driven from either end.
Detailed Explanation
As with pretty much any GPIO pin, there are a few things to consider:
- The direction
- If it's an input then
- Is a pullup is enabled?
- Is a pulldown enabled?
- If an output, then
- What is the output data bit set to?
set pindirs X
does exactly what you expect: sets the pins to outputs where the bit is 1. If it's an output, the pullups don't do anything. If it's an input, the data bit (pins
) doesn't do anything.
We are considering the case where there is no external circuit on the physical pins, pulldowns are enabled, and the pins
register is set to 3
, ie binary 11
.
- When an output the voltage on the pin is the value of
pins
, which is 1
, so high
.
- When an input the voltage on the pin is the pulldown, which is
low
The four phases in your example are:
Pindirs |
Dir1 |
Dir0 |
Pin1 |
Pin0 |
Pull1 |
Pull0 |
Volt1 |
Volt0 |
3 |
Out |
Out |
1 |
1 |
x |
x |
High |
High |
1 |
In |
Out |
x |
1 |
Down |
x |
Low |
High |
2 |
Out |
In |
1 |
x |
x |
Down |
High |
Low |
0 |
In |
In |
x |
x |
Down |
Down |
Low |
Low |
x
means "Don't Care", which is to say, it doesn't affect the output voltage.
Pin1
means the value of bit 1 of pins
; pin0
is bit 0.
Pull1
means the configuration of the pullups/pulldowns for each pin
Volt1
means what we'd measure
The Pico PIO system is vastly more sophisticated than this, that's not relevant to this question because the individual GPIO pins are conventional except that pullups and pulldowns are both available. Many IO devices don't have configurable pulldowns.
The PIO system of the RP4020 is much more sophisticated than most, as it has built-in state machines which execute tiny programs. Linking to the documentation as it's a little tricky to find.
- Good article about Pico's PIO system Admantium
- Definition documentation of RP 2040 PIO system chapter 3