Add multicore support with setup1/loop1 (#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.
This commit is contained in:
parent
2d58f08bf2
commit
1815c45f92
4 changed files with 76 additions and 1 deletions
|
|
@ -20,6 +20,7 @@
|
|||
|
||||
#include <Arduino.h>
|
||||
#include <pico/stdlib.h>
|
||||
#include <pico/multicore.h>
|
||||
|
||||
extern void setup();
|
||||
extern void loop();
|
||||
|
|
@ -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);
|
||||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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
docs/multicore.rst
Normal file
13
docs/multicore.rst
Normal file
|
|
@ -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
libraries/rp2040/examples/Multicore/Multicore.ino
Normal file
40
libraries/rp2040/examples/Multicore/Multicore.ino
Normal file
|
|
@ -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);
|
||||
}
|
||||
Loading…
Reference in a new issue