Pico: blink through the SIO registers
Blink two LEDs on a Raspberry Pi Pico by writing its SIO registers and timing them with the RP2040's 1 MHz timer, through the Pico SDK under the Arduino core.
Firmware · Intermediate
In a real product: Status LEDs on a controller board (Industrial): Fast, glitch-free status outputs written straight to the chip's registers.
digitalWrite() is easy to read, but it hides what the chip really does. Under the Arduino core of the Pico is the Pico SDK, Raspberry Pi's own vendor library. Under that are the RP2040's registers. This sketch uses the SDK to set the pins up, then writes the registers itself.
What you'll learn
- The RP2040's SIO block:
gpio_set,gpio_clrandgpio_toglchange pins in one write, without a read-modify-write - Choosing a pin's function (
gpio_set_function, the IO_BANK0CTRLregister) and its direction (gpio_oe_set) - The 64-bit microsecond timer
timer_hw->timerawl, and comparing times so the 32-bit wrap does no harm - How a vendor SDK's functions (
gpio_init) map onto those registers
How it works
gpio_init(pin) selects the SIO function for the pin in its IO_BANK0 control register and clears its output. Then sio_hw->gpio_oe_set makes both pins outputs in one write.
The SIO (single-cycle IO) is wired straight to the core, so a write takes one clock cycle. It has separate set, clear and toggle (XOR) registers. sio_hw->gpio_togl = mask toggles every pin whose bit is 1 and leaves the others alone, so an interrupt changing another pin at the same moment can't be undone, as it could with out = out ^ mask.
The RP2040's timer counts microseconds from reset. timer_hw->timerawl is its low 32 bits, which wrap after 71 minutes. (int32_t)(now - next) >= 0 stays right across the wrap, where now >= next would fail once.
The green LED toggles every 500 ms and the red one every 125 ms. Both are timed from the same counter, with no delay().
Try this
- Replace the XOR write with
sio_hw->gpio_out = sio_hw->gpio_out ^ GREEN_MASKand explain why it is worse. - Read
sio_hw->gpio_inand print it each second: the LED pins read back as they are driven. - Change the red LED's period to 1 µs × your favourite number and check it with the logic analyzer.
sketch.ino
// Raspberry Pi Pico: blink two LEDs by writing the RP2040's SIO registers, timed by its 1 MHz timer. The Pico SDK
// (under the Arduino core) gives the register names; setup() and loop() are the only Arduino parts.
#include "hardware/gpio.h"
#include "hardware/structs/sio.h"
#include "hardware/structs/timer.h"
const uint GREEN = 14, RED = 15;
const uint32_t GREEN_MASK = 1u << GREEN, RED_MASK = 1u << RED;
uint32_t nextGreen, nextRed, nextReport;
uint32_t toggles;
void setup() {
Serial.begin(115200);
gpio_init(GREEN); // function SIO, output low
gpio_init(RED);
sio_hw->gpio_oe_set = GREEN_MASK | RED_MASK; // both outputs, in one write
nextGreen = nextRed = timer_hw->timerawl;
nextReport = nextGreen + 250000; // report between toggles
}
void loop() {
uint32_t now = timer_hw->timerawl; // microseconds since reset, low 32 bits
if ((int32_t)(now - nextGreen) >= 0) {
sio_hw->gpio_togl = GREEN_MASK; // toggle GP14 only
nextGreen += 500000;
toggles++;
}
if ((int32_t)(now - nextRed) >= 0) {
sio_hw->gpio_togl = RED_MASK;
nextRed += 125000;
}
if ((int32_t)(now - nextReport) >= 0) {
nextReport += 1000000;
Serial.printf("t=%lu us out=0x%08lx green toggles=%lu\n", (unsigned long)now,
(unsigned long)sio_hw->gpio_out, (unsigned long)toggles);
}
}