A Practical Look at PIO on the Raspberry Pi Pico

Blues
Raspberry Pi IoT
Illustration: © IoT For All

The Raspberry Pi Pico took the microcontroller world by storm when it was released in early 2021. As the first MCU developed and released by the Raspberry Pi Foundation, the world immediately took notice (especially considering the undeniable success of the Raspberry Pi line of single-board computers).

Pulling back the covers on the Pico, we quickly learned that it was not just another MCU. Most notably, it included support for a feature called Programmable I/O (PIO).

Before we dive into PIO on the Raspberry Pi Pico, let’s take one giant step back.

What Is the Raspberry Pi Pico?

Remember when Apple made some noise with their introduction of Apple Silicon? In what I assume was a tongue-in-cheek move, the Raspberry Pi Foundation matched Apple with the first release of their own internally developed chip under the guise of “Raspberry Silicon.” That chip is the RP2040, and the Pico is the official dev kit for the RP2040 from the Raspberry Pi Foundation:

Priced at a budget level of $4, the Pico is an excellent microcontroller for beginners and experts alike. Officially known and distributed as the RP2040, the Pico includes hardware and firmware features such as:

  • A dual-core Arm Cortex-M0+ processor;
  • 264KB of on-chip RAM;
  • Built-in support for MicroPython;
  • A wide range of I/O options, including this thing called Programmable I/O (PIO).

Let’s take a high-level look at what PIO is when you might considering using it, and finally, how you would use it.

What Is Programmable I/O (PIO)?

All MCUs and SBCs include support for communication protocols like I2C and SPI. The RP2040 is no different, with 2 x UART, 2 x SPI, and 2 x I2C controllers. This allows the Pico to communicate with a wide variety of standard peripherals easily.

However, many of us have encountered scenarios where we are building a solution around legacy technology or even using multiple SPI devices with a single MCU. This is where the RP2040’s PIO support comes in to help.

Using PIO, you can create your interfaces from scratch. In theory, you could even build entirely new interfaces that haven’t even been imagined yet!

On a slightly more technical level, an instance of a PIO is comparable to a tiny processor that runs code separately from the main Cortex-M0+. So what was previously accomplished by “bit-banging” protocols (and consuming CPU cycles), PIO does independently of the CPU.

On the RP2040, each PIO instance includes four state machines that can each run instructions stored in the shared instruction memory. This memory can hold 32 instructions, and each state machine can utilize any of said instructions. Each state machine can also control any of the GPIO pins on the Pico.

Programming a PIO instance is easier than you might expect. Since it relies on special assembly instructions, you can write code in any editor (instead of a proprietary “Raspberry Pi Pico” IDE, for example).

Speaking of writing code to program a PIO instance, the PIO language consists of nine, and only nine, instructions:

  • in()
  • out()
  • push()
  • pull()
  • mov()
  • irq()
  • wait()
  • jmp()
  • set()

At first glance, this may not seem like many, but they provide a wide variety of features.

Let’s switch gears to a more practical issue: when would I use PIO? It’s a valid question, as in most maker projects, the onboard support for I2C, UART, and SPI will be plenty. However, there are times when PIO can be a critical advantage.

When Would I Use PIO?

Ever encounter a scenario where you need more UART connections that are available on a board? What about outputting directly to DVI video? Or maybe you’re trying to communicate with a legacy piece of hardware over serial without any supporting libraries available.

These are all valid scenarios for diving deeper into PIO on the Raspberry Pi Pico.

While unrelated to the Raspberry Pi Pico, a Hackster project accomplishes the above without using PIO (but maybe the author wishes they had). Remotely control the Nintendo R.O.B. robot over cellular.

How Do I Use PIO on the Pico?

If you’re like me, you can read blog posts all day and not learn much of anything. And yes, I realize this comment is very meta.

I learn best by doing, and I start “doing” by copying and pasting code and working through it line-by-line.

When working with the Pico and MicroPython, I’ve found an endless amount of use from the Pico MicroPython Examples repository on GitHub.

Case in point, in the pio directory, we can find a relatively simple PIO example that:

  1. Binds a single GPIO to the set() instruction;
  2. Uses delays to blink an LED;
  3. Instantiates a state machine to run the instructions and blink the on-board LED.

The full example is available here (lightly edited to remove comments):

import time
import rp2
from machine import Pin

@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
def blink():
    wrap_target()
    set(pins, 1)   [31]
    nop()          [31]
    nop()          [31]
    nop()          [31]
    nop()          [31]
    set(pins, 0)   [31]
    nop()          [31]
    nop()          [31]
    nop()          [31]
    nop()          [31]
    wrap()

sm = rp2.StateMachine(0, blink, freq=2000, set_base=Pin(25))

sm.active(1)
time.sleep(3)
sm.active(0)

What’s Happening in This Code Sample?

The PIO program is located within the blink() function (and note the @rp2.asm_pio decorator).

The wrap_target() and wrap() methods create a loop. The set() function accepts the target (in this case pins) and 1 denotes setting the pin high (and 0 low later on).

The nop() functions create an artificial delay of about 20 cycles each.

So, we set the GPIO pin high (illuminating the LED), pause for some cycles, and then set the pin low (turning the LED off). This causes a rapid blink visible to the human eye:

Conclusion

PIO on the Raspberry Pi Pico can be beneficial when you’re connecting to non-standard peripherals. That doesn’t mean it’s for everyone, but if you need to connect via communication protocols that are unknown or unavailable, you’ll quickly fall in love!

Be sure to check out the RP2040 datasheet for more in-depth learning and this PIO emulator for more advanced development and debugging scenarios.

Happy hacking with the Raspberry Pi Pico!

Author
Blues
Blues
Blues simplifies wireless communication. Our mission is to transform every physical product into an intelligent service, helping communities and companies to save time and money, and improve the world.
Blues simplifies wireless communication. Our mission is to transform every physical product into an intelligent service, helping communities and companies to save time and money, and improve the world.