1

So i have a rp2040 board, and i could not find a dual core example codes. The closest i could find is the scheduler library example given to me by the IDE

/*
 Multiple Blinks

 Demonstrates the use of the Scheduler library for the boards:
 
 - Arduino Nano 33 BLE, or
 - Arduino Portenta H7, or
 - Arduino Nano RP2040 Connect

 Hardware required :
 * None (LEDs are already conencted to RGB LED)

 ATTENTION: LEDs polarity is reversed (so loop3 will turn the LED off by writing 1)

 created 8 Oct 2012
 by Cristian Maglie
 Modified by
 Scott Fitzgerald 19 Oct 2012

 This example code is in the public domain

 http://www.arduino.cc/en/Tutorial/MultipleBlinks
*/

// Include Scheduler since we want to manage multiple tasks.
#include <Scheduler.h>

// On Nano RP2040 Connect, RGB leds are connected to the wifi module
// The user APIs are the same, but we can't convert to int, so use defines
#if defined(ARDUINO_NANO_RP2040_CONNECT)

#include "WiFiNINA.h"
#define led1  LEDR
#define led2  LEDG
#define led3  LEDB

// On Nicla Sense ME, RGB leds are connected via an I2C module
// The user APIs are the same, but we can't convert to int, so use defines
#elif defined(ARDUINO_NICLA)

#include "Nicla_System.h"
#define led1  LEDR
#define led2  LEDG
#define led3  LEDB

#else

int led1 = LEDR;
int led2 = LEDG;
int led3 = LEDB;

#endif

void setup() {
  Serial.begin(9600);

  // Setup the 3 pins as OUTPUT
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);

  // Add "loop2" and "loop3" to scheduling.
  // "loop" is always started by default.
  Scheduler.startLoop(loop2);
  Scheduler.startLoop(loop3);
}

// Task no.1: blink LED with 1 second delay.
void loop() {
  digitalWrite(led1, HIGH);

  // IMPORTANT:
  // When multiple tasks are running 'delay' passes control to
  // other tasks while waiting and guarantees they get executed.
  delay(1000);

  digitalWrite(led1, LOW);
  delay(1000);
}

// Task no.2: blink LED with 0.1 second delay.
void loop2() {
  digitalWrite(led2, HIGH);
  delay(100);
  digitalWrite(led2, LOW);
  delay(100);
}

// Task no.3: accept commands from Serial port
// '0' turns off LED
// '1' turns on LED
void loop3() {
  if (Serial.available()) {
    char c = Serial.read();
    if (c == '0') {
      digitalWrite(led3, LOW);
      Serial.println("Led turned off!");
    }
    if (c == '1') {
      digitalWrite(led3, HIGH);
      Serial.println("Led turned on!");
    }
  }

  // IMPORTANT:
  // We must call 'yield' at a regular basis to pass
  // control to other tasks.
  yield();
}

but i do not think its using the second core of the rp2040. Does anybody have an example codes? because im really lost how to do it

DrakeJest
  • 201
  • 1
  • 7

1 Answers1

3

Note: This information is about the pure "Arduino" core, not the "MBED" core. The MBED core gets in the way of using the RP2040 SDK, so install the pure bare metal core from here.

By default the second core isn't doing anything.

But you can run anything you want on it directly. No need for any libraries. You use the function multicore_launch_core1(func) to start a function on that core. You could make it Arduino-like by writing a wrapper function, such as this code:


void setup2() {
    pinMode(1, OUTPUT);
}

void loop2() {
    digitalWrite(1, HIGH);
    delay(750);
    digitalWrite(1, LOW);
    delay(750);
}

void main2() {
    setup2();
    while (1) {
        loop2();
    }
}

void setup() {
    multicore_launch_core1(main2);
    pinMode(0, OUTPUT);
}

void loop() {
    digitalWrite(0, HIGH);
    delay(1000);
    digitalWrite(0, LOW);
    delay(1000);
}
Majenko
  • 103,876
  • 5
  • 75
  • 133
  • The code throws an error in arduino IDE. `sketch_nov05a:21:5: error: 'multicore_launch_core1' was not declared in this scope` – DrakeJest Nov 04 '21 at 19:34
  • @DrakeJest Sure you have the right board selected, and the right core installed? I use that function all the time without needing to do anything. – Majenko Nov 04 '21 at 19:35
  • I double checked, and yes . I am using `Arduino Mbed OS RP2040 Boards - Raspberry pi pico` – DrakeJest Nov 04 '21 at 19:38
  • @DrakeJest Ah, sounds like you're not using the Raspberry Pi core but the MBED core. I've never used that. Stick to the proper Pi one. – Majenko Nov 04 '21 at 19:39
  • 1
    @DrakeJest This is the one I use: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json – Majenko Nov 04 '21 at 19:39
  • What is the name of the pi core? The only one showing up inside the board manager is that one, it even says its made by arduino, so i think this is the official one – DrakeJest Nov 04 '21 at 19:42
  • Got it to compile now ! And wow there so many boards included in this one, can even change core clock and stuff. Whats up with that Arduino Mbed Os then? – DrakeJest Nov 04 '21 at 19:49
  • @DrakeJest The problem with the MBED one is it runs MBED. That's an operating system (like FreeRTOS) which sits between your code and the hardware. – Majenko Nov 04 '21 at 19:50
  • Oh thats a bummer, so i loose access to the scheduler library then. are there similar alternatives? I guess i will have to code my project the hard way then – DrakeJest Nov 04 '21 at 19:57
  • The scheduler library is a crutch. You don't need crutches. – Majenko Nov 04 '21 at 20:01
  • You are right, if i want an OS might as well go for a full-blown raspberry pi 4/zero. Thank you for your help Majenko, Ill get right to starting my project. Have a nice day – DrakeJest Nov 04 '21 at 20:06