Pico: two cores, a FIFO and a spin lock
Core 1 samples a knob and blinks an LED while core 0 reports and dims a second LED. Words cross between the cores through a FIFO, and a counter both cores…
Firmware · Advanced
In a real product: Motor controller with a real-time core and a comms core (Robotics): Dual-core controllers keep the time-critical loop on one core and the slow communication on the other.
The RP2040 has two Cortex-M0+ cores. In the Arduino core for the Pico, setup() and loop() run on core 0. If a sketch also defines setup1() and loop1(), core 0 launches core 1 to run them, side by side, at the same time.
Here core 1 is the "real-time" core: every 20 ms it reads the knob on GP26 and sends the reading to core 0. It also blinks the green LED. Core 0 is the "comms" core: it receives the readings, sets the yellow LED's brightness to match, and prints a report. At start-up both cores also run a race on purpose, to show what goes wrong without a lock.
What you'll learn
- How a sketch starts the second core, and how
get_core_num()tells the two apart rp2040.fifo: passing 32-bit words between the cores without sharing variables- Why
count = count + 1on two cores at once loses increments, and how a spin lock makes it exact - Splitting work between a fast loop and a slow one
How it works
At start-up, core 0 launches core 1 through the RP2040's inter-core FIFO. The SDK's multicore_launch_core1 sends the vector table, the stack pointer and the entry point to the boot ROM, which is waiting on core 1. Then both cores run.
The race. Each core adds 1 to two shared counters 20 000 times. unlocked = unlocked + 1 is three instructions: load, add, store. When both cores load the same value before either stores, one increment is lost. locked is incremented inside spin_lock_blocking() and spin_unlock(). A spin lock is one of the SIO block's 32 hardware locks: reading it claims it if it is free, and only one core can hold it. So locked ends at exactly 40 000, and unlocked usually ends a little lower. On the real chip, how many are lost changes from run to run.
The FIFO. rp2040.fifo.push() puts a word in a queue for the other core, and pop_nb() takes one out if there is one. Core 1 pushes each knob reading. Core 0 takes them in its loop, so neither core ever reads a variable while the other is writing it.
The work split. Core 1 does short, regular work: one ADC read every 20 ms. Core 0 does the slow work: printing a report every 500 ms. A slow Serial.printf on core 0 never delays core 1's sampling.
Try this
- Make
N100 000. The unlocked count loses more, the locked one is still exact. - Remove the spin lock around
lockedand watch it lose increments too. - Move the knob: the yellow LED's brightness and the reported value follow it, 20 ms behind.
- Print
get_core_num()inloop()and inloop1().
sketch.ino
// Raspberry Pi Pico, both cores: core 1 samples the knob on GP26 every 20 ms, sends each reading to core 0 through the
// FIFO and blinks GP15; core 0 dims GP14 to match and reports. At start-up both cores race on two shared counters,
// one guarded by a hardware spin lock.
#include <pico/multicore.h>
#include <hardware/sync.h>
const int KNOB = 26, BLINK_LED = 15, DIM_LED = 14;
const uint32_t N = 20000;
volatile uint32_t locked = 0, unlocked = 0;
spin_lock_t *lock;
// Both cores run this at once.
void race() {
for (uint32_t i = 0; i < N; i++) {
uint32_t saved = spin_lock_blocking(lock); // claim the lock (and mask interrupts on this core)
locked = locked + 1;
spin_unlock(lock, saved);
unlocked = unlocked + 1; // no lock: load, add, store can interleave with the other core
}
}
// ---- core 0: the comms core ----------------------------------------------------------------------------------------
void setup() {
Serial.begin(115200);
while (!Serial) delay(1);
pinMode(DIM_LED, OUTPUT);
lock = spin_lock_init(spin_lock_claim_unused(true));
rp2040.fifo.push(0); // core 1 may start its half of the race
race();
rp2040.fifo.pop(); // core 1 has finished its half
Serial.printf("core %d: locked %lu of %lu, unlocked %lu (%lu lost)\n", get_core_num(), locked, 2 * N, unlocked,
2 * N - unlocked);
}
void loop() {
static uint32_t last = 0, readings = 0, value = 0;
uint32_t word;
while (rp2040.fifo.pop_nb(&word)) { value = word; readings++; }
analogWrite(DIM_LED, value / 16); // 12-bit reading, 8-bit PWM
if (millis() - last >= 500) {
last = millis();
Serial.printf("core %d: knob %lu (%lu%%), %lu readings from core 1\n", get_core_num(), value, value * 100 / 4095, readings);
}
delay(5);
}
// ---- core 1: the real-time core ------------------------------------------------------------------------------------
void setup1() {
rp2040.fifo.pop(); // wait for core 0's start signal
Serial.printf("core %d started\n", get_core_num());
race();
rp2040.fifo.push(0);
pinMode(BLINK_LED, OUTPUT);
analogReadResolution(12);
}
void loop1() {
static uint32_t ticks = 0;
rp2040.fifo.push(analogRead(KNOB));
if (++ticks % 10 == 0) digitalWrite(BLINK_LED, !digitalRead(BLINK_LED)); // toggles every 200 ms
delay(20);
}