My goal is that I'm reading an analog signal on the M4, putting that in a bufferarray. Sending it after a second has passed (or array reaches a certain size) to the M7. Receiving it on the M7, copying it to an array on the M7 core and then do calculations on the array.
#include <Arduino.h>
#include "RPC_internal.h" // comes with the mbed board installation
// Global variables used by both cores
const long BAUD = 115200;
/*---------------------------M7_CORE---------------------------*/
#ifdef CORE_CM7
// Set an M7 core global variable
float globArray[18100];
void setArray(float a[]){
memcpy(a, globArray, sizeof(a));
}
void setup() {
RPC1.begin();
LL_RCC_ForceCM4Boot();
Serial.begin(BAUD);
RPC1.bind("setArray", setArray);
}
void loop() {
while (RPC1.available()) {
Serial.write(RPC1.read()); // check if the M4 has sent an RPC println
}
/*
Do something with the globArray
*/
}
#endif
/*---------------------------M4_CORE---------------------------*/
#ifdef CORE_CM4
// Set an M4 core global variable
float bufferArray[18100];
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 1000;
int count = 0;
void setup() {
RPC1.begin();
startMillis = millis();
}
void loop() {
bufferArray[count] = analogRead(A0);
count++;
currentMillis = millis();
if (currentMillis - startMillis >= period)
{
auto res = RPC1.call("setArray", bufferArray); //Sends vector over RPC to the M7 Core
RPC1.println("From M4 arraysize : " + String(sizeof(bufferArray)));
memset(bufferArray, 0, sizeof(bufferArray));
count = 0;
startMillis = currentMillis;
}
}
#endif
I am not entirely clear what you are trying to do, but when you pass an array to a function what is actually passed is a pointer to the array. So, if in the function you use sizeof() to determine the size of the array what you are actually doing is to find the size of the pointer to the array
I might have actually had a little mistake in that function
void setArray(float a[])
{
memcpy(globArray, a, sizeof(a));
}
I want to copy a to globArray
This doesn't have to be done with arrays, I would prefer vectors, as seen below.
The code below compiles for both cores, but as soon as the M4 code is executed the onboard LEDs blinks red. I suspect it is the stack that is getting overflowed?
Does a vector containing ~20000 floats take up that much space?
#include <Arduino.h>
#include "RPC_internal.h" // comes with the mbed board installation
#include <vector>
// Global variables used by both cores
const long BAUD = 115200;
int myLED;
/*---------------------------M7_CORE---------------------------*/
#ifdef CORE_CM7
// Set an M7 core global variable
std::vector<float> globArray(20000);
std::vector<float> setArray(std::vector<float> a){
globArray = a;
return a;
}
void setup() {
RPC1.begin();
LL_RCC_ForceCM4Boot();
myLED = LEDB;
Serial.begin(BAUD);
RPC1.bind("setArray", setArray);
pinMode(myLED, OUTPUT);
}
void loop() {
while (RPC1.available()) {
digitalWrite(myLED, !digitalRead(myLED)); //Switches LED state
Serial.write(RPC1.read()); // check if the M4 has sent an RPC println
}
delay(200);
/*
Do something with the globArray
*/
}
#endif
/*---------------------------M4_CORE---------------------------*/
#ifdef CORE_CM4
// Set an M4 core global variable
std::vector<float> bufferArray(20000);
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 1000;
void setup() {
RPC1.begin();
pinMode(myLED, OUTPUT);
startMillis = millis();
}
void loop() {
bufferArray.push_back(analogRead(A0)); //Reads from pin A0 and adds the value to the bufferArray
currentMillis = millis();
if (currentMillis - startMillis >= period) // Executes every 1000 ms
{
auto res = RPC1.call("setArray", bufferArray); //Sends vector over RPC to the M7 Core
RPC1.println("From M4 arraysize : " + String(bufferArray.size()));
bufferArray.clear();
digitalWrite(myLED, !digitalRead(myLED)); //Switches LED state
startMillis = currentMillis;
}
}
#endif
What do expect to be printed ?
What is actually printed ?
How many bytes will be copied into the destination array ?
What values will be in the destination array ?