Skip to content

Commit 1815c45

Browse files
Add multicore support with setup1/loop1 (earlephilhower#113)
Support running code on the second core by adding a setup1() and/or a loop1() routine to a sketch. These functions operate exactly like the normal Arduino ones, and anything they call will be run on the second core automatically. Add a simple multicore example.
1 parent 2d58f08 commit 1815c45

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

cores/rp2040/main.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <Arduino.h>
2222
#include <pico/stdlib.h>
23+
#include <pico/multicore.h>
2324

2425
extern void setup();
2526
extern void loop();
@@ -29,6 +30,21 @@ extern void loop();
2930
void initVariant() __attribute__((weak));
3031
void initVariant() { }
3132

33+
34+
// Optional 2nd core setup and loop
35+
extern void setup1() __attribute__((weak));
36+
extern void loop1() __attribute__((weak));
37+
static void main1() {
38+
if (setup1) {
39+
setup1();
40+
}
41+
while (true) {
42+
if (loop1) {
43+
loop1();
44+
}
45+
}
46+
}
47+
3248
extern "C" int main() {
3349
#if F_CPU != 125000000
3450
set_sys_clock_khz(F_CPU / 1000, true);
@@ -45,8 +61,12 @@ extern "C" int main() {
4561
DEBUG_RP2040_PORT.begin();
4662
#endif
4763

64+
if (setup1 || loop1) {
65+
multicore_launch_core1(main1);
66+
}
67+
4868
setup();
49-
while (1) {
69+
while (true) {
5070
loop();
5171
if (arduino::serialEventRun) {
5272
arduino::serialEventRun();

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ For the latest version, always check https://github.com/earlephilhower/arduino-p
3434

3535
Ported/Optimized Libraries <libraries>
3636

37+
Multicore Processing <multicore>
38+
3739
Using Pico-SDK <sdk>
3840

3941
Licenses <license>

docs/multicore.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Multicore Processing
2+
====================
3+
4+
The RP2040 chip has 2 cores that can run independently of each other, sharing
5+
peripherals and memory with each other. Arduino code will normally execute
6+
only on core 0, with the 2nd core sitting idle in a low power state.
7+
8+
By adding a ``setup1()`` and ``loop1()`` function to your sketch you can make
9+
use of the second core. Anything called from within the ``setup1()`` or
10+
``loop1()`` routines will execute on the second core.
11+
12+
See the ``Multicore.ino`` example in the ``rp2040`` example directory for a
13+
quick introduction.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Demonstrates a simple use of the setup1()/loop1() functions
2+
// for a multiprocessor run.
3+
4+
// Will output something like, where C0 is running on core 0 and
5+
// C1 is on core 1, in parallel.
6+
7+
// 11:23:07.507 -> C0: Blue leader standing by...
8+
// 11:23:07.507 -> C1: Red leader standing by...
9+
// 11:23:07.507 -> C1: Stay on target...
10+
// 11:23:08.008 -> C1: Stay on target...
11+
// 11:23:08.505 -> C0: Blue leader standing by...
12+
// 11:23:08.505 -> C1: Stay on target...
13+
// 11:23:09.007 -> C1: Stay on target...
14+
// 11:23:09.511 -> C0: Blue leader standing by...
15+
// 11:23:09.511 -> C1: Stay on target...
16+
// 11:23:10.015 -> C1: Stay on target...
17+
18+
// Released to the public domain
19+
20+
// The normal, core0 setup
21+
void setup() {
22+
Serial.begin();
23+
delay(5000);
24+
}
25+
26+
void loop() {
27+
Serial.printf("C0: Blue leader standing by...\n");
28+
delay(1000);
29+
}
30+
31+
// Running on core1
32+
void setup1() {
33+
delay(5000);
34+
Serial.printf("C1: Red leader standing by...\n");
35+
}
36+
37+
void loop1() {
38+
Serial.printf("C1: Stay on target...\n");
39+
delay(500);
40+
}

0 commit comments

Comments
 (0)