Skip to content

Add multicore support with setup1/loop1 #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion cores/rp2040/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <Arduino.h>
#include <pico/stdlib.h>
#include <pico/multicore.h>

extern void setup();
extern void loop();
Expand All @@ -29,6 +30,21 @@ extern void loop();
void initVariant() __attribute__((weak));
void initVariant() { }


// Optional 2nd core setup and loop
extern void setup1() __attribute__((weak));
extern void loop1() __attribute__((weak));
static void main1() {
if (setup1) {
setup1();
}
while (true) {
if (loop1) {
loop1();
}
}
}

extern "C" int main() {
#if F_CPU != 125000000
set_sys_clock_khz(F_CPU / 1000, true);
Expand All @@ -45,8 +61,12 @@ extern "C" int main() {
DEBUG_RP2040_PORT.begin();
#endif

if (setup1 || loop1) {
multicore_launch_core1(main1);
}

setup();
while (1) {
while (true) {
loop();
if (arduino::serialEventRun) {
arduino::serialEventRun();
Expand Down
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ For the latest version, always check https://github.com/earlephilhower/arduino-p

Ported/Optimized Libraries <libraries>

Multicore Processing <multicore>

Using Pico-SDK <sdk>

Licenses <license>
13 changes: 13 additions & 0 deletions docs/multicore.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Multicore Processing
====================

The RP2040 chip has 2 cores that can run independently of each other, sharing
peripherals and memory with each other. Arduino code will normally execute
only on core 0, with the 2nd core sitting idle in a low power state.

By adding a ``setup1()`` and ``loop1()`` function to your sketch you can make
use of the second core. Anything called from within the ``setup1()`` or
``loop1()`` routines will execute on the second core.

See the ``Multicore.ino`` example in the ``rp2040`` example directory for a
quick introduction.
40 changes: 40 additions & 0 deletions libraries/rp2040/examples/Multicore/Multicore.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Demonstrates a simple use of the setup1()/loop1() functions
// for a multiprocessor run.

// Will output something like, where C0 is running on core 0 and
// C1 is on core 1, in parallel.

// 11:23:07.507 -> C0: Blue leader standing by...
// 11:23:07.507 -> C1: Red leader standing by...
// 11:23:07.507 -> C1: Stay on target...
// 11:23:08.008 -> C1: Stay on target...
// 11:23:08.505 -> C0: Blue leader standing by...
// 11:23:08.505 -> C1: Stay on target...
// 11:23:09.007 -> C1: Stay on target...
// 11:23:09.511 -> C0: Blue leader standing by...
// 11:23:09.511 -> C1: Stay on target...
// 11:23:10.015 -> C1: Stay on target...

// Released to the public domain

// The normal, core0 setup
void setup() {
Serial.begin();
delay(5000);
}

void loop() {
Serial.printf("C0: Blue leader standing by...\n");
delay(1000);
}

// Running on core1
void setup1() {
delay(5000);
Serial.printf("C1: Red leader standing by...\n");
}

void loop1() {
Serial.printf("C1: Stay on target...\n");
delay(500);
}