Using multiple cores to execute code simultaneously.

C Interface

Documentation

// Start core 1 running a function.
void multicore_launch_core1(void(*entry)(void));
 
// Initialise a queue for communication between cores.
void queue_init(queue_t* q, uint element_size, uint element_count);
 
// Add data onto a queue. If full, waits until the queue has room.
void queue_add_blocking(queue_t* q, void* data);
 
// Remove data onto a queue. If no data, waits until there is.
void queue_remove_blocking(queue_t* q, void* data);

Instructions

The RP2350 that powers the Pico 2 has two cores. Core 0 is the main core, and it is the one that runs main. Core 1 is the second core that we can use to do additional work. We can use both of them to get double the processing power. For this example, we are going to offload an “expensive” computation to the second core, leaving core 0 with less work to do.

Look at and run the multicore.cpp file. Connect to the serial port and view the results. Also watch the LED to see it blink.

The example uses a queue to send data between the cores. This is important because we need a way to synchronise the cores so they know when new data is available. Each core has its own local variables, so the only way they can communicate is by using main memory.

But what if one core tries to read data at the same time the other core tries to write? A queue is a safe way to handle this situation. The course COMP SCI 3004 covers these concepts in much more detail.

Exercises

Exercise

Edit the core 0 code to make core 1 do Fibonacci for odd numbers and Factorial for even numbers. Don’t edit the core 1 code.

Fibonacci(x) = Fibonacci(x-1) + Fibonacci(x-2), Fibonacci(0, 1) = 1 Factorial(x) = x * Factorial(x - 1), Factorial(0) = 1

Hint: These functions are recursive. Hint 2: Find some way to edit the func field of the queue_entry struct.

Exercise

Make core 0 tell core 1 when a button is being pressed, then make core 1 turn on the LED depending on whether the button is being pressed.

Hint: Refer to Module 2.2