The SIO's hardware spin locks
Model the RP2350's 32 spin locks, claimed by reading and freed by writing. An easy embedded C problem (Two cores on the RP2350), run and graded in the browser.
easy · Two cores on the RP2350 · multicore · spinlock
The RP2350's SIO has 32 spin locks, kept as the bits of one 32-bit state. Reading lock n claims it if it is free and returns 1 << n; if it is already claimed the read returns 0 and changes nothing. Writing any value to it frees it.
Write lock_read(state, n) and lock_write(state, n) on the state *state. Only the read of a free lock claims it, so of two cores that read at once only one gets a nonzero answer.
Starter code
uint32_t lock_read(uint32_t *state, int n) {
// Claims the lock every time, even when the other core holds it.
*state |= 1u << n;
return 1u << n;
}
void lock_write(uint32_t *state, int n) {
}Example tests
A free lock is claimed by the first read
uint32_t s = 0;
printf("%lx %lx\n", (unsigned long)lock_read(&s, 3), (unsigned long)s);Expected output:
8 8A claimed lock reads 0 until it is freed
uint32_t s = 0;
lock_read(&s, 5);
uint32_t second = lock_read(&s, 5);
lock_write(&s, 5);
printf("%lu %lx\n", (unsigned long)second, (unsigned long)lock_read(&s, 5));Expected output:
0 20Learn it first
- Two cores on the RP2350 · Starting core 1, the SIO FIFOs, spin locks and splitting the work.
More Two cores on the RP2350 problems
- A FIFO between two cores · Model one direction of the RP2350's inter-core FIFO, 4 words deep, with its sticky error flags.