What precisely does "pindirs" accomplish? I initially believed it defined the pin direction, indicating whether it's set as an input or an output. However, in a Raspberry Pi forum (link) it appears that "pindirs" can not only determine the pin direction but also concurrently set the outputs to high and low. So, does it not just set the pin direction, but does it also simultaneously set the pin to a high state based on the discussions in the forum?

set pins 3
set pindirs 3 [31]
set pindirs 1 [31]
set pindirs 2 [31]
set pindirs 0 [31]
jmp loop

enter image description here


1 Answer 1


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
  • \$\begingroup\$ Why does altering the direction of a pin to output result in a high output rather than a low one? \$\endgroup\$ Nov 21, 2023 at 9:09
  • \$\begingroup\$ @Sanfourbensanfer I rewrote this answer after reading your linked article more carefully, I hope it's helpful. \$\endgroup\$
    – jonathanjo
    Nov 22, 2023 at 11:47
  • \$\begingroup\$ Thank you very much for the clear response. The crucial point i missed was that "set pins 3" at the very beginning. \$\endgroup\$ Nov 22, 2023 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.