You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Fix typos, code formatting and minor code issues.
- Update the API to include the new RPC.cpu_id() function.
- Add a section on using the RPC library with MicroPython.
Signed-off-by: iabdalkader <[email protected]>
Copy file name to clipboardExpand all lines: content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md
+180-16
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,8 @@ The M4 and M7 cores are programmed with separate sketches, using the same serial
24
24
In this guide you will discover:
25
25
- How to configure and program the M4/M7 cores and conventional approaches to do so.
26
26
- How to boot the M4 core.
27
-
- How to communicate between the cores via Remote Call Procedures (RPC).
27
+
- How to communicate between the cores via Remote Procedure Call (RPC).
28
+
- Using the RPC Library with MicroPython.
28
29
- Useful examples based on the dual core & RPC features.
29
30
- The `RPC` library API.
30
31
@@ -144,9 +145,9 @@ Once the M4 is booted from the M7, both cores will run in parallel, much like tw
144
145
145
146
Uploading new sketches works the same as a typical upload procedure. The new sketch will overwrite the current sketch running on the core you upload to.
146
147
147
-
## Identify Core Used
148
+
## Identifying the Current CPU Core
148
149
149
-
To identify which core is being used, use the `HAL_GetCurrentCPUID()` method. Below is a function that returns which core is currently being used. This can be useful to identify that your program is running on the right core.
150
+
The `RPC.cpu_id()` method can be used to identify current CPU core running the sketch. This can be used to run different code paths based on the CPU core ID. For example, the following sketch blinks the blue LED if the current core is the Cortex-M7, and the Green LED if the current core is the Cortex-M4:
150
151
151
152
```arduino
152
153
/*
@@ -169,7 +170,7 @@ void setup() {
169
170
pinMode(LEDB, OUTPUT);
170
171
pinMode(LEDG, OUTPUT);
171
172
172
-
if (HAL_GetCurrentCPUID() == CM7_CPUID) {
173
+
if (RPC.cpu_id() == CM7_CPUID) {
173
174
blink(LEDB, 100); //blink blue LED (M7 core)
174
175
} else {
175
176
blink(LEDG, 100); //blink green LED (M4 core)
@@ -190,11 +191,7 @@ void blink(int led, int delaySeconds) {
190
191
}
191
192
```
192
193
193
-
- The `HAL_GetCurrentCPUID()` is a method that checks the CPU ID, and returns the value in a `uint32_t` format.
194
-
- The `CM7_CPUID` flag that we compare with holds the value `0x00000003` (hexadecimal), or `3` (decimal).
195
-
- It is also possible to use `CM4_CPUID` flag which holds the value `0x00000003`, or `1` (decimal).
196
-
197
-
## Remote Call Procedures (RPC)
194
+
## Remote Procedure Call (RPC)
198
195
199
196
RPC is a method that allows programs to make requests to programs located elsewhere. It is based on the client-server model (also referred to as caller/callee), where the client makes a request to the server.
@@ -235,6 +233,105 @@ When `call()` is used, a request is sent, it is processed on the server side, an
235
233
236
234

237
235
236
+
### Using the RPC Library with MicroPython
237
+
238
+
The `msgpackrpc` library provides the same functionality as the Arduino RPC library for MicroPython, i.e., it allows the binding of local functions or objects, starting the M4 core, and invoking remote calls from Python scripts. This library and its supporting features are enabled by default on all compatible Arduino boards starting with MicroPython release 1.23 and require no external dependencies to use. Additionally, the Arduino sketches presented in the examples section here require no changes to use with MicroPython. However, there are a few restrictions to using the RPC library with MicroPython. First, the MicroPython firmware always targets the main M7 core, consequently, Arduino sketches can only run on the M4 core. Second, only flash-based firmware, with the `1.5MB M7 + 0.5MB M4` flash partitioning scheme, is supported. Finally, SDRAM firmware is not supported. Although the `msgpackrpc` library does support loading firmware images to SDRAM, the firmware currently generated for the M4 core with an SDRAM target does not work. This issue may be fixed in future releases.
239
+
240
+
The following sections introduce the `msgpackrpc` library API and some use cases in detail.
241
+
242
+
#### The `msgpackrpc` Library
243
+
244
+
The `msgpackrpc` library is the RPC library's counterpart for MicroPython, and it provides the same functionality as the Arduino RPC library. The first steps to using the `msgpackrpc` library, are importing the module and creating a `MsgPackRPC` object:
245
+
246
+
```python
247
+
import msgpackrpc
248
+
249
+
# Create a MsgPackRPC object.
250
+
rpc = msgpackrpc.MsgPackRPC()
251
+
```
252
+
253
+
The RPC object created above can then be used to bind Python callables, start the M4 core and invoke remote calls from MicroPython scripts.
254
+
255
+
#### Binding Python Functions, Callables and Objects
256
+
257
+
Any Python callable object (such as functions, bound methods, callable instances, etc.) can be invoked by the remote core. To allow the remote core to invoke a callable, it must be first bound to a name. The following example binds a function to the name `sub`:
258
+
259
+
```python
260
+
defsub(a, b):
261
+
return a - b
262
+
263
+
# Register a function to be called by the remote processor.
264
+
rpc.bind("sub", sub)
265
+
```
266
+
267
+
Note that the name the function is bound to does not have to match the callable name.
268
+
269
+
Similarly, an object's bound method can also be bound to a name. For example:
270
+
271
+
```python
272
+
foo = Foo()
273
+
rpc.bind("sub", foo.add)
274
+
```
275
+
276
+
Both of those functions can be called in the same way from the Arduino sketch:
277
+
278
+
```arduino
279
+
int res = RPC.call("sub", 2, 1).as<int>();
280
+
```
281
+
282
+
Objects can also be bound to allow their methods to be called by the remote core. When an object is passed to `bind()`, all of its public methods (the ones that don't start with an `_`) are bound to their respective qualified names. For example, the following code binds the methods of an object of class `Foo`:
283
+
284
+
```python
285
+
classFoo:
286
+
def__init__(self):
287
+
pass
288
+
289
+
defadd(self, a, b):
290
+
return a + b
291
+
292
+
defsub(self, a, b):
293
+
return a - b
294
+
295
+
# Register an object of Foo
296
+
rpc.bind("foo1", Foo())
297
+
```
298
+
299
+
Now the object's methods can be invoked by the Arduino sketch using their fully qualified name, for example:
300
+
301
+
```arduino
302
+
int res1 = RPC.call("foo1.add", 1, 2).as<int>();
303
+
int res2 = RPC.call("foo1.sub", 2, 1).as<int>();
304
+
```
305
+
306
+
#### Starting the M4 Core From Micropython
307
+
308
+
The next step is starting the M4 core by calling `MsgPackRPC.start()` with the firmware entry point as an argument:
309
+
310
+
```python
311
+
# Start the remote processor and wait for it to be ready to communicate.
312
+
rpc.start(firmware=0x08180000)
313
+
```
314
+
315
+
This function will start the remote core (the M4), boot it from the specified firmware address, and wait for the core to be ready to communicate before it returns. The default arguments passed to this function are compatible with the Arduino RPC library and do not need to be changed for the purposes of this tutorial. Note that the firmware address used is a flash address, where the M4 firmware starts, and it's the same one used for the flash split of `1.5MB M7 + 0.5MB M4`.
316
+
317
+
#### Calling Remote Functions From Micropython
318
+
319
+
Once the M4 core is started, the `MsgPackRPC` object can be used to invoke its remote functions. Remote calls can be invoked synchronously, i.e., the call does not return until a response is received from the other side, or asynchronously. In this case, the call returns a `Future` object that must be joined at some point to read back the call's return value.
320
+
321
+
```python
322
+
# Perform a synchronous call, which blocks until it returns.
323
+
res = rpc.call("add", 1, 2)
324
+
325
+
# Perform an asynchronous call, which returns immediately with a Future object.
326
+
f1 = rpc.call_async("add", 1, 2)
327
+
328
+
# The Future object returned above, must be joined at some point to get the results.
329
+
print(f1.join())
330
+
```
331
+
332
+
That covered most of the `msgpackrpc` library's API and use cases. For a complete example, see the [MicroPython RPC LED Example](#micropython-rpc-led). For more examples and applications, see the `msgpackrpc`[repository](https://github.com/arduino/arduino-lib-mpy/tree/main/lib/msgpackrpc).
333
+
334
+
238
335
## RPC Examples
239
336
240
337
In this section, you will find a series of examples that is based on the `RPC` library.
@@ -251,12 +348,12 @@ The `Serial.print()` command only works on the **M7 core**. In order to print va
251
348
#include <RPC.h>
252
349
253
350
void setup() {
254
-
RPC.begin();
351
+
RPC.begin();
255
352
}
256
353
257
354
void loop() {
258
-
RPC.println("Printed from M4 core");
259
-
delay(1000);
355
+
RPC.println("Printed from M4 core");
356
+
delay(1000);
260
357
}
261
358
```
262
359
@@ -266,8 +363,8 @@ delay(1000);
266
363
#include <RPC.h>
267
364
268
365
void setup() {
269
-
Serial.begin(9600);
270
-
RPC.begin();
366
+
Serial.begin(9600);
367
+
RPC.begin();
271
368
}
272
369
273
370
void loop() {
@@ -308,6 +405,7 @@ void setup() {
308
405
}
309
406
310
407
void loop() {
408
+
311
409
}
312
410
313
411
/*
@@ -454,6 +552,53 @@ int servoMove(int angle) {
454
552
}
455
553
```
456
554
555
+
### MicroPython RPC LED
556
+
557
+
This example demonstrates how to use MicroPython (running on the M7 core) to remotely control an LED connected to the M4 core.
558
+
559
+
**M4 sketch:**
560
+
561
+
```arduino
562
+
#include "RPC.h"
563
+
564
+
void led(bool on) {
565
+
if (on) {
566
+
digitalWrite(LEDG, LOW);
567
+
} else {
568
+
digitalWrite(LEDG, HIGH);
569
+
}
570
+
}
571
+
572
+
void setup() {
573
+
RPC.bind("led", led);
574
+
RPC.begin();
575
+
}
576
+
577
+
void loop() {
578
+
579
+
}
580
+
```
581
+
582
+
**M7 Python script:**
583
+
584
+
```python
585
+
import time
586
+
import msgpackrpc
587
+
588
+
# Create an RPC object
589
+
rpc = msgpackrpc.MsgPackRPC()
590
+
591
+
# Start the remote processor.
592
+
rpc.start(firmware=0x08180000)
593
+
594
+
# Toggle the LED every 500ms
595
+
whileTrue:
596
+
rpc.call("led", True)
597
+
time.sleep_ms(500)
598
+
rpc.call("led", False)
599
+
time.sleep_ms(500)
600
+
```
601
+
457
602
## RPC Library API
458
603
459
604
The `RPC` library is based on the [rpclib](https://github.com/rpclib/rpclib) C++ library which provides a client and server implementation. In addition, it provides a method for communication between the M4 and M7 cores.
@@ -575,4 +720,23 @@ RPC.read();
575
720
#### Returns
576
721
577
722
- The first available byte from the M4.
578
-
-`-1` if there is none.
723
+
-`-1` if there is none.
724
+
725
+
### RPC.cpu_id()
726
+
727
+
Returns the current CPU ID, which can be one of two values: `CM7_CPUID` for the Cortex-M7 core, or `CM4_CPUID` for the Cortex-M4 core.
0 commit comments