🛠 Dual-core M7 ↔ M4 : appels de fonctions distants avec la bibliothèque RPC

Arduino Portenta H7 · intermediaire · Schéma : IMG-portenta-h7-intermediaire-1-portenta-h7-dual-core-rpc.svg — SoC STM32H747XI éclaté en deux coeurs Cortex-M7 480 MHz et Cortex-M4 240 MHz reliés par une flèche double étiquetée RPC/OpenAMP, croquis identique flashé sur chaque coeur, terminal série affichant add(1,2)=3

Objectives

Leverage the true superpower of the Portenta H7: its two physical cores (Cortex-M7 up to 480 MHz + Cortex-M4 up to 240 MHz) that share all internal peripherals. The RPC library of the Mbed core, built on OpenAMP/rpmsg, allows calling a function bound on one core from the other core using RPC.bind / RPC.call — including MicroPython code on one core and C++ on the other (see the OpenMV tutorial). You will adapt the official Basic_AddSub example.

Hardware

Complete Code

// rpc_addsub.ino — identique sur M7 et M4 ; RPC les relie.
// (adapte de l'exemple officiel Basic_AddSub, ArduinoCore-mbed)
#include "RPC.h"

int add(int a, int b) { return a + b; }    // fonctions appelables
int sub(int a, int b) { return a - b; }    // depuis l'autre coeur

void setup {
  Serial.begin(115200);
  while (!Serial) {}

  RPC.begin;                 // sur M7, demarre aussi le coeur M4
  RPC.bind("add", add);        // expose la fonction sous un nom
  RPC.bind("sub", sub);

  if (RPC.cpu_id == CM7_CPUID) {
    delay(100);                // laisse le M4 binder ses fonctions
  }
  pinMode(LEDG, OUTPUT);
}

void loop {
  // L'appel part sur le coeur qui a bindé la fonction, le
  // résultat revient typé via .as<int>.
  int res = RPC.call("add", 1, 2).as<int>;
  if (RPC.cpu_id == CM7_CPUID) {
    Serial.println("add(1, 2) = " + String(res));
    res = RPC.call("sub", res, 1).as<int>;
    Serial.println("sub(3, 1) = " + String(res));
  }
  delay(250);
}

Expected Verification

Flash on M7 → nothing is displayed (the M4 is not yet present); flash the same sketch on M4 → the console displays add(1, 2) = 3 then sub(3, 1) = 2 in a loop. This proves that the M7 executed code physically on the M4. The official RPC_m4 variant adds SerialRPC (the M4 prints via the M7's USB serial) and an Mbed Thread making non-blocking remote calls.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials