Core specific multi-threading

Hi,

I am testing out the multi-threading capacities of the board and I am encountering issues. Basically I am implementing the asynchronous blink in a multi-threaded fashion by relying on rtos.

Here is the code: (It is derived from the RPCm4 example)

#include "Arduino.h"

rtos::Thread t1;
rtos::Thread t2;

void call_red_led() {
  while (1) {
    digitalWrite(LEDR, HIGH);
    delay(750);
    digitalWrite(LEDR, LOW);
    delay(750);
  }
}

void call_green_led() {
  while (1) {
    digitalWrite(LEDG, HIGH);
    delay(500);
    digitalWrite(LEDG, LOW);
    delay(500);
  }
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
#ifdef CORE_CM7
  t1.start(call_red_led);
#endif
#ifdef CORE_CM4
  t2.start(call_green_led);
#endif
  

}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(LEDB, HIGH);
  delay(1000);
  digitalWrite(LEDB, LOW);
  delay(1000);
}

If I use it like that it does not work: the green led is not blinking. So it looks like it is not asking the Core M4 to do its job. It is unclear to me how this should be done. I am used to the ESP32, and freeRTOS where it’s to some extent clearer where a job is running. If someone could clarify this or pointing what I’m doing wrong I would appreciate it.

Thanks,

Antoine

You have to upload your sketch to both the M7 and M4 cores, sequentially. The example below works. Note the added command for the M7 core to boot the M4, and the selection of the core for control of the blue LED.

#include "Arduino.h"
#include "RPC_internal.h"

using namespace rtos;

rtos::Thread t1;
rtos::Thread t2;

void call_red_led() {
  while (1) {
    digitalWrite(LEDR, HIGH);
    delay(1000);
    digitalWrite(LEDR, LOW);
    delay(1000);
  }
}

void call_green_led() {
  while (1) {
    digitalWrite(LEDG, HIGH);
    delay(500);
    digitalWrite(LEDG, LOW);
    delay(500);
  }
}

void setup() {
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
  #ifdef CORE_CM7
    bootM4();
    t1.start(call_red_led);
  #endif
  #ifdef CORE_CM4
    t2.start(call_green_led);
  #endif
}

void loop() {
  #ifdef CORE_CM7  
    digitalWrite(LEDB, HIGH);
    delay(2000);
    digitalWrite(LEDB, LOW);
    delay(2000);
  #endif  
}

1 Like