Shared Variable between M4 and M7 Core Portenta H7

Hi everyone,

I'm using a Portenta H7 board, and I'm not very experienced with MCUs. Let me explain my situation. I want to share a variable between the M4 and M7 cores—this variable could be a boolean (true or false) or an integer (1 or 0).

Why? I plan to run a loop on the M4 MCU that checks if a switch is pressed. If it is, the variable will change from 0 to 1. On the other MCU, there will be a long process of sampling and writing occurring within a loop function. What I want is to verify at the beginning of each loop if the switch is pressed, meaning if the variable is 0 or 1.

I've tried using separate sketches for M4 and M7, sharing a header file in which I define the shared variable.

Here is the code:

Header File:

#ifndef SHARED_DATA_H
#define SHARED_DATA_H

#include <mbed.h>

// Declare a volatile shared integer
volatile int sharedInt __attribute__((section(".SHARED_RAM")));

#endif

M7 code:

#include "SharedData.h"

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

void loop() {
  Serial.print("M7 read sharedInt: ");
  Serial.println(sharedInt);
  delay(1000);  // Read every second
}

M4 code:

#include "SharedData.h"

void setup() {}

void loop() {
  sharedInt++;  // Increment the shared integer
  delay(1000);  // Increment every second
}