Multicore FIFO example sketch Hands slow work to the second core and gets the answers back through rp2040.fifo. Core 0 sends four numbers; core 1 counts the primes below each one and sends back the count and how long it took. Core 0 is free in the meantime: it keeps the LED blinking and prints each answer as it arrives.
A value pushed on one core is popped on the other; pop() waits until there is one, pop_nb() and available() do not wait. See Multicore.
- Output
- Serial Monitor at 115200 baud. The counts are always these; the times, shown here as <t>, depend on Tools > CPU Speed; the last one takes about 20 s at the default 133 MHz:
Work sent to core 1.
Primes below 1000: 168 (<t> ms on core 1)
Primes below 10000: 1229 (<t> ms on core 1)
Primes below 100000: 9592 (<t> ms on core 1)
Primes below 1000000: 78498 (<t> ms on core 1)
const int JOBS = 4;
const uint32_t LIMITS[JOBS] = {1000, 10000, 100000, 1000000};
for (int i = 0; i < JOBS; i++) {
}
Serial.println(
"Work sent to core 1.");
}
static int done = 0;
if (done < JOBS &&
rp2040.fifo.available() >= 2) {
const uint32_t count =
rp2040.fifo.pop();
const uint32_t ms =
rp2040.fifo.pop();
Serial.print(
"Primes below ");
Serial.println(
" ms on core 1)");
done++;
}
}
bool isPrime(uint32_t n) {
if (n < 2) return false;
for (uint32_t d = 2; d * d <= n; d++) {
if (n % d == 0) return false;
}
return true;
}
const uint32_t limit =
rp2040.fifo.pop();
const unsigned long start =
millis();
uint32_t count = 0;
for (uint32_t n = 2; n < limit; n++) {
if (isPrime(n)) count++;
}
}
@ OUTPUT
Definition Common.h:40
SerialUSBClass Serial
USB serial port, the one the Serial Monitor uses.
void digitalWrite(pin_size_t pinNumber, PinStatus status)
Sets an output pin HIGH (3.3 V) or LOW (0 V).
void pinMode(pin_size_t pinNumber, PinMode pinMode)
Sets a pin as an input or an output.
void loop1(void) __attribute__((weak))
Written by the sketch (optional): runs over and over on core 1 after setup1().
void loop(void)
Written by the sketch: runs over and over after setup(), on core 0.
void setup(void)
Written by the sketch: runs once after reset, on core 0.
unsigned long millis(void)
Milliseconds since the board started.
#define LED_BUILTIN
On-board LED.
Definition pins_arduino.h:63