Two cores on the RP2350
Starting core 1, the SIO FIFOs, spin locks and splitting the work.
Two cores on the Raspberry Pi Pico 2
The RP2350 has two Cortex-M33 cores (and two RISC-V cores, of which only one pair runs at a time). After reset only core 0 runs the program; core 1 waits in the boot ROM until core 0 hands it something to do. They share the flash, the SRAM and the peripherals, and each has its own registers, stack, NVIC, SysTick and FPU. This lesson covers starting core 1, passing data between the cores, and keeping shared data consistent.
Starting core 1
Core 1 sleeps in the boot ROM, waiting on its FIFO from core 0. To launch it, core 0 sends a fixed sequence of words: 0, 0, 1, the address of core 1's vector table, its initial stack pointer and its entry point. The ROM echoes each word back, and restarts the sequence if an echo does not match. The SDK wraps this in multicore_launch_core1(fn); in the Arduino core for the Pico 2, a sketch that defines setup1() and loop1() gets them run on core 1 automatically:
void setup() { Serial.begin(115200); } // core 0
void loop() { /* reports, USB, the slow work */ }
void setup1() { pinMode(14, INPUT_PULLUP); } // core 1
void loop1() { /* the time-critical loop */ }
get_core_num() (in the SDK, rp2040.cpuid() in the Arduino core) reads SIO's CPUID register: 0 or 1, so shared code can tell which core it is on.
The FIFOs
The SIO block (single-cycle I/O) has a FIFO in each direction, 4 words of 32 bits deep on the RP2350 (the RP2040's are 8). FIFO_WR writes to the other core's FIFO, FIFO_RD reads this core's, and FIFO_ST reports VLD (there is a word to read), RDY (there is room to write), and two sticky error flags: WOF (a write to a full FIFO, the word lost) and ROE (a read of an empty one). A word arriving can raise the SIO_IRQ_FIFO interrupt on the receiving core, and writing a word also signals an event, so a core waiting in WFE wakes up.
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
// One direction of an inter-core FIFO, as the RP2350's SIO keeps it: 4 words, sticky error flags.
#define DEPTH 4
static uint32_t word[DEPTH];
static int first = 0, count = 0;
static bool wof = false, roe = false;
bool fifo_write(uint32_t w) {
if (count == DEPTH) { wof = true; return false; }
word[(first + count) % DEPTH] = w;
count++;
return true;
}
bool fifo_read(uint32_t *w) {
if (count == 0) { roe = true; return false; }
*w = word[first];
first = (first + 1) % DEPTH;
count--;
return true;
}
int main(void) {
for (uint32_t i = 1; i <= 10; i++) fifo_write((1u << 16) | i); // core 1 sends ten counts
uint32_t w;
while (fifo_read(&w)) printf("core %lu counted %lu\n", (unsigned long)(w >> 16), (unsigned long)(w & 0xffff));
printf("WOF %d (six words were lost), ROE %d\n", wof, roe);
return 0;
}
The Arduino core's rp2040.fifo.push(w) waits while the FIFO is full and pop_nb(&w) returns false when it is empty, so a sketch never loses a word or reads garbage. A FIFO passes one word at a time; for more, send a pointer or an index into a buffer that the receiver then owns until it sends it back.
Shared data and the race
Both cores can read and write the same SRAM. A line such as count = count + 1 is a load, an add and a store; if both cores load the same value before either stores, one increment is lost. Nothing in C prevents it, and it happens rarely enough to pass a test and fail in the field:
#include <stdio.h>
// Two cores both doing count = count + 1, one instruction at a time, interleaved badly.
int main(void) {
int count = 0;
int r0 = count; // core 0 loads 0
int r1 = count; // core 1 loads 0
r0 = r0 + 1;
r1 = r1 + 1;
count = r0; // core 0 stores 1
count = r1; // core 1 stores 1: core 0's increment is gone
printf("after two increments: %d\n", count);
return 0;
}
Three tools fix it:
- Pass, do not share. Send the value through the FIFO, so only one core ever writes it.
- Hardware spin locks. SIO has 32 spin locks. Reading
SPINLOCKnclaims the lock if it is free and returns nonzero, or returns 0 if the other core holds it; writing any value frees it. One bus access both tests and claims, so the cores cannot both win. The SDK'sspin_lock_blocking()also turns off this core's interrupts while it holds the lock. - Exclusive accesses. The Cortex-M33's
LDREXandSTREX(used by C11<stdatomic.h>and the SDK's mutexes) work across the two cores:STREXfails if the other core wrote the location in between, and the code tries again.
Hold a lock for as little time as possible: the other core spins, doing nothing, until it is free.
Splitting the work
A common split is to give core 1 the hard real-time work (a control loop, sampling, a PIO-fed protocol) with nothing else on it, and core 0 everything that can be late (USB, Wi-Fi, logging, the user interface). Core 1's loop then has a steady timing that interrupts from USB or the radio cannot disturb, as long as those interrupts are enabled only on core 0: each core has its own NVIC, and an interrupt is taken by the core that enabled it.
Flash writes need care: while the flash is being erased or programmed nothing can run from it, so the SDK's multicore_lockout (or the Arduino core's rp2040.idleOtherCore()) parks the other core in RAM first.
Interview question: How do you start the second core on an RP2040 or RP2350, and what happens underneath?
Answer: Core 1 waits in the boot ROM after reset. Core 0 sends it, through the inter-core FIFO, the words 0, 0, 1, then the vector table address, the stack pointer and the entry function; the ROM echoes each word and starts over if an echo is wrong, then sets VTOR and SP and jumps to the entry. The SDK's multicore_launch_core1() does this sequence; the Arduino core does it when a sketch defines setup1() and loop1().
Interview question: Two cores share a ring buffer. How do you make it safe?
Answer: With one producer and one consumer, keep the write index written only by the producer and the read index only by the consumer, make them volatile or atomic, and order the accesses with a memory barrier (DMB) so the data is written before the index that publishes it; then no lock is needed. With more than one writer, protect the update with a hardware spin lock or an LDREX/STREX-based mutex held only for the few instructions of the update, or pass ownership of whole buffers through the FIFO instead.
Going deeper
Interview question: Why put the time-critical loop on core 1 rather than core 0?
Answer: Core 0 usually runs the system code: USB, the radio, the Arduino core's or SDK's background work, and their interrupts. Each core has its own NVIC, so if those interrupts are enabled only on core 0, core 1 runs its loop with nothing preempting it and its timing stays steady. It also keeps a crash or a long blocking call in the comms code from stopping the control loop.
Interview question: What is the difference between the RP2350's FIFO interrupt and an event?
Answer: A word arriving in a core's FIFO can raise SIO_IRQ_FIFO on that core, which runs a handler. Writing a word also sends an event, like SEV, which wakes a core waiting in WFE without any handler. Waiting code often polls the FIFO's VLD flag and sleeps in WFE between checks, so it costs no power and needs no interrupt.
Explain it: In your own words, explain why count = count + 1 on two cores can lose an increment, and how a spin lock prevents it.
Answer: The statement is three steps: load count into a register, add one, store it back. If both cores load the same value before either stores, both store that value plus one, so one increment disappears. With a spin lock around the statement, a core must claim the lock before the load and free it after the store; reading the lock claims it in one access, so only one core can be between the claim and the release at a time and the three steps of the two cores can no longer interleave.