|  | 
| 1 | 1 | #include "Arduino.h" | 
| 2 | 2 | #include "RPC_internal.h" | 
| 3 | 3 | 
 | 
| 4 |  | -#ifdef CORE_CM7 | 
| 5 |  | -int add(int a, int b) { | 
| 6 |  | - printf("calling add on M7\n"); | 
| 7 |  | - delay(1000); | 
|  | 4 | +using namespace rtos; | 
|  | 5 | + | 
|  | 6 | +Thread subtractThread; | 
|  | 7 | + | 
|  | 8 | +/** | 
|  | 9 | + * Returns the CPU that's currently running the sketch (M7 or M4) | 
|  | 10 | + * Note that the sketch has to be uploaded to both cores.  | 
|  | 11 | + **/ | 
|  | 12 | +String currentCPU() { | 
|  | 13 | + if (HAL_GetCurrentCPUID() == CM7_CPUID) { | 
|  | 14 | + return "M7"; | 
|  | 15 | + } else { | 
|  | 16 | + return "M4"; | 
|  | 17 | + } | 
|  | 18 | +} | 
|  | 19 | + | 
|  | 20 | +/** | 
|  | 21 | + * Adds two numbers and returns the sum | 
|  | 22 | + **/ | 
|  | 23 | +int addOnM7(int a, int b) { | 
|  | 24 | + Serial.println(currentCPU() + ": executing add with " + String(a) + " and " + String(b)); | 
|  | 25 | + delay(700); // Simulate work | 
| 8 | 26 |  return a + b; | 
| 9 | 27 | } | 
| 10 |  | -int subtract(int a, int b) { | 
| 11 |  | - printf("calling subtract on M7\n"); | 
|  | 28 | + | 
|  | 29 | +/** | 
|  | 30 | + * Subtracts two numbers and returns the difference | 
|  | 31 | + **/ | 
|  | 32 | +int subtractOnM7(int a, int b) { | 
|  | 33 | + Serial.println(currentCPU() + ": executing subtract with " + String(a) + " and " + String(b)); | 
|  | 34 | + delay(700); // Simulate work | 
| 12 | 35 |  return a - b; | 
| 13 | 36 | } | 
| 14 |  | -#endif | 
| 15 |  | - | 
| 16 |  | -rtos::Thread t; | 
| 17 | 37 | 
 | 
| 18 |  | -void call_substract() { | 
| 19 |  | - while (1) { | 
| 20 |  | - delay(700); | 
| 21 |  | - RPC1.print("Calling subtract "); | 
| 22 |  | - auto res = RPC1.call("sub", 12, 45).as<int>();; | 
| 23 |  | - RPC1.println(res); | 
|  | 38 | +void callSubstractFromM4() { | 
|  | 39 | + while (true) { | 
|  | 40 | + delay(700); // Wait 700ms with the next calculation | 
|  | 41 | + int a = random(100); // Generate a random number | 
|  | 42 | + int b = random(100); // Generate a random number | 
|  | 43 | + RPC1.println(currentCPU() + ": calling subtract with " + String(a) + " and " + String(b)); | 
|  | 44 | +  | 
|  | 45 | + auto result = RPC1.call("remoteSubtract", a, b).as<int>(); | 
|  | 46 | + // Prints the result of the calculation | 
|  | 47 | + RPC1.println(currentCPU() + ": Result is " + String(a) + " - " + String(b) + " = " + String(result)); | 
| 24 | 48 |  } | 
| 25 | 49 | } | 
| 26 | 50 | 
 | 
| 27 | 51 | void setup() { | 
| 28 |  | - // put your setup code here, to run once: | 
|  | 52 | + | 
|  | 53 | + pinMode(LED_BUILTIN, OUTPUT); | 
|  | 54 | + | 
|  | 55 | + // Initialize RPC library; this also boots the M4 core | 
| 29 | 56 |  RPC1.begin(); | 
| 30 | 57 |  Serial.begin(115200); | 
| 31 |  | - //while (!Serial) {} | 
| 32 |  | - //Serial.begin(115200); | 
| 33 |  | - pinMode(LED_BUILTIN, OUTPUT); | 
| 34 |  | -#ifdef CORE_CM7 | 
| 35 |  | - RPC1.bind("add", add); | 
| 36 |  | - RPC1.bind("sub", subtract); | 
| 37 |  | -#else | 
| 38 |  | - t.start(call_substract); | 
| 39 |  | -#endif | 
|  | 58 | + //while (!Serial) {} // Uncomment this to wait until the Serial connection is ready | 
|  | 59 | + | 
|  | 60 | + // Both CPUs will execute this instruction, just at different times | 
|  | 61 | + randomSeed(analogRead(A0)); // Initializes the pseudo-random number generator | 
|  | 62 | + | 
|  | 63 | + if (currentCPU() == "M7") { | 
|  | 64 | + // M7 CPU becomes the server, so it makes two functions available under the defined names | 
|  | 65 | + RPC1.bind("remoteAdd", addOnM7); | 
|  | 66 | + RPC1.bind("remoteSubtract", subtractOnM7); | 
|  | 67 | + }  | 
|  | 68 | + | 
|  | 69 | + if (currentCPU() == "M4") { | 
|  | 70 | + // M4 CPU becomes the client, so spawns a thread that will call subtractOnM7() every 700ms | 
|  | 71 | + subtractThread.start(callSubstractFromM4); | 
|  | 72 | + } | 
| 40 | 73 | } | 
| 41 | 74 | 
 | 
| 42 | 75 | void loop() { | 
| 43 |  | - // put your main code here, to run repeatedly: | 
| 44 |  | -#ifndef CORE_CM7 | 
| 45 |  | - digitalWrite(LED_BUILTIN, HIGH); | 
| 46 |  | - delay(1000); | 
| 47 |  | - digitalWrite(LED_BUILTIN, LOW); | 
| 48 |  | - delay(1000); | 
| 49 |  | - | 
| 50 |  | - RPC1.print("Calling add "); | 
| 51 |  | - auto res = RPC1.call("add", 12, 45).as<int>();; | 
| 52 |  | - RPC1.println(res); | 
| 53 |  | -#else | 
| 54 |  | - while (RPC1.available()) { | 
| 55 |  | - Serial.write(RPC1.read()); | 
|  | 76 | + | 
|  | 77 | + if (currentCPU() == "M4") { | 
|  | 78 | + // On M4 let's blink an LED. While it's blinking, the callSubstractFromM4() thread is running,  | 
|  | 79 | + // so it will execute roughly 3 times (2000 / 700 ms) | 
|  | 80 | + digitalWrite(LED_BUILTIN, LOW); | 
|  | 81 | + delay(1000); | 
|  | 82 | + digitalWrite(LED_BUILTIN, HIGH); | 
|  | 83 | + delay(1000); | 
|  | 84 | + | 
|  | 85 | + int a = random(100); | 
|  | 86 | + int b = random(100); | 
|  | 87 | + // PRC.print works like a Serial port, but it needs a receiver (in this case the M7)  | 
|  | 88 | + // to actually print the strings to the Serial port | 
|  | 89 | + RPC1.println(currentCPU() + ": calling add with " + String(a) + " and " + String(b)); | 
|  | 90 | + // Let's invoke addOnM7() and wait for a result. | 
|  | 91 | + // This will be delayed by the forced delay() in addOnM7() function | 
|  | 92 | + // Exercise: if you are not interested in the result of the operation, what operation would you invoke? | 
|  | 93 | + auto result = RPC1.call("remoteAdd", a, b).as<int>();  | 
|  | 94 | + RPC1.println(currentCPU() + ": Result is " + String(a) + " + " + String(b) + " = " + String(result)); | 
| 56 | 95 |  } | 
| 57 |  | -#endif | 
|  | 96 | +  | 
|  | 97 | + if (currentCPU() == "M7") { | 
|  | 98 | + // On M7, let's print everything that is received over the RPC1 stream interface | 
|  | 99 | + // Buffer it, otherwise all characters will be interleaved by other prints | 
|  | 100 | + String buffer = ""; | 
|  | 101 | + while (RPC1.available()) { | 
|  | 102 | + buffer += (char)RPC1.read(); // Fill the buffer with characters | 
|  | 103 | + } | 
|  | 104 | + | 
|  | 105 | + if (buffer.length() > 0) { | 
|  | 106 | + Serial.print(buffer); | 
|  | 107 | + } | 
|  | 108 | + } | 
|  | 109 | + | 
| 58 | 110 | } | 
0 commit comments