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