PORTENTA-H7, Where could be an example code for RPC and OpenAMP

PORTENTA-H7, Where could be an example code for RPC and OpenAMP,
to test the dual core communication on

From the standard Arduino IDE, there's an example: "RPC_m4" within the Portenta section. The name is misleading as it's actually written for both cores. Also, as the libraries are written, it will require the "-fexceptions" flag to be set to compile. With regards to the example itself, it produces a somewhat confusing Serial output with the combination of an rtos thread, main loop and delays. Nevertheless the RPC calls produce the desired results...

Additionally, there are some rpc library examples here: C:\Users*YourUser*\AppData\Local\Arduino15\packages\arduino-beta\hardware\mbed\1.2.0\libraries\rpclib\examples

-Terry

I tried this RPC demo but found some issues (e.g. CM4 was not released).

Here is my Scatch (to demonstrate how CM4 could run two threads and use Serial, whereby CM4 sends RPC calls to CM7 to let this core do the UART).

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

//in order to compile this, you had to add -fexceptions in file 'platform.txt' as option !!

#ifdef CORE_CM7
int add(int a, int b) {
  ////printf(" M7: calling add\n");
  //-->this printf does not work on UART !!
  Serial.println(" M7: calling add\n");
  return a + b;
}
int subtract(int a, int b) {
  printf(" M7: calling subtract\n");
  return a - b;
}
#endif

#ifndef CORE_CM7
//this is just for CM4
rtos::Thread t;

void call_substract() {
  //this is a CM4 RTOS thread
  while (1) {
    delay(1000);
    RPC1.print(" M4: Calling subtract | ");
    auto res = RPC1.call("sub", 12, 45).as<int>();
    RPC1.println(res);
  }
}
#endif

void setup() {
  //for both cores using RPC
  RPC1.begin();
#ifdef CORE_CM7
  Serial.begin(115200); //never mind: TeraTerm will receive with any baudrate set there
  //while (!Serial) {}
  //Serial.begin(115200);
  pinMode(LEDG, OUTPUT); //CM7 configures LED GPIO
  RPC1.bind("add", add);
  RPC1.bind("sub", subtract);
  Serial.println("CM7: release and boot CM4");
  //boot CM4
  //-->it has to be done in order to release CM4 !!
  LL_RCC_ForceCM4Boot();
#else
  t.start(call_substract);
#endif
}

void loop() {
  // put your main code here, to run repeatedly:
#ifndef CORE_CM7
  //this is the CM4 main thread
  digitalWrite(LEDG, HIGH); //CM4 toggles the LED (GPIO)
  delay(1000);
  digitalWrite(LEDG, LOW);
  delay(1000);

  RPC1.print(" M4: Calling add | ");
  auto res = RPC1.call("add", 12, 45).as<int>();
  RPC1.println(res);
#else
  //let CM7 also do something, to see CM7 runs and prints as well
  add(10, 20);
  delay(1000);

  //CM7 will check for RPC calls and forward to UART
  while (RPC1.available()) {
    Serial.write(RPC1.read());
  }
#endif
}

Attention: you have to add compile option -fexceptions in (hidden) platform.txt file.