Skip to content

Commit bc68f93

Browse files
committed
Update the Giga RPC/dual-core tutorial.
Add a section on using the RPC library with MicroPython. Signed-off-by: iabdalkader <[email protected]>
1 parent 68af80a commit bc68f93

File tree

1 file changed

+107
-8
lines changed
  • content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core

1 file changed

+107
-8
lines changed

content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md

+107-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ The M4 and M7 cores are programmed with separate sketches, using the same serial
2424
In this guide you will discover:
2525
- How to configure and program the M4/M7 cores and conventional approaches to do so.
2626
- 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.
2829
- Useful examples based on the dual core & RPC features.
2930
- The `RPC` library API.
3031

@@ -194,7 +195,7 @@ void blink(int led, int delaySeconds) {
194195
- The `CM7_CPUID` flag that we compare with holds the value `0x00000003` (hexadecimal), or `3` (decimal).
195196
- It is also possible to use `CM4_CPUID` flag which holds the value `0x00000003`, or `1` (decimal).
196197

197-
## Remote Call Procedures (RPC)
198+
## Remote Procedure Call (RPC)
198199

199200
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.
200201

@@ -235,6 +236,103 @@ When `call()` is used, a request is sent, it is processed on the server side, an
235236

236237
![Communication between M7 and M4 core.](assets/rpc-m7-m4.png)
237238

239+
### Using the RPC Library with MicroPython
240+
241+
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. The first one is that MicroPython firmware always targets the main M7 core, consequently, Arduino sketches can only run on the M4 core. Additionally, only flash-based firmware, with the `1.5MB M7 + 0.5MB M4` flash partitioning scheme, is supported. While the `msgpackrpc` library does support loading firmware images to any address space, the firmware currently generated for the M4 core with an SDRAM target does not work. This issue may be fixed in future releases.
242+
243+
The following sections introduce the `msgpackrpc` library API and some use cases in detail.
244+
245+
#### The msgpackrpc Library
246+
247+
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:
248+
249+
```python
250+
import msgpackrpc
251+
252+
# Create an MsgPackRPC object
253+
rpc = msgpackrpc.MsgPackRPC()
254+
```
255+
256+
The RPC object created above can then be used to bind Python callables, start the M4 core and invoke remote calls from MicroPython scripts.
257+
258+
#### Binding Python Functions, Callables and Objects
259+
260+
The next step is binding callables. Any Python callable (such as functions, bound methods, callable objects etc..) can be bound to a name and be made available to the remote core to call. The following example binds a function to the name `sub`:
261+
262+
```python
263+
def sub(a, b):
264+
return a - b
265+
266+
# Register a function to be called by the remote processor.
267+
rpc.bind("sub", sub)
268+
```
269+
270+
Similarily, an object's bound method can also be bound to a name. For example:
271+
272+
```python
273+
foo = Foo()
274+
rpc.bind("sub", foo.add)
275+
```
276+
277+
Both of those functions can be called in the same way from the Arduino sketch:
278+
279+
```arduino
280+
int res = RPC.call("sub", 2, 1).as<int>();
281+
```
282+
283+
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`:
284+
285+
```python
286+
class Foo:
287+
def __init__(self):
288+
pass
289+
290+
def add(a, b):
291+
return a + b
292+
293+
def sub(a, b):
294+
return a - b
295+
296+
# Register an object of Foo
297+
rpc.bind("foo1", Foo())
298+
```
299+
300+
Now the object's methods can be invoked by the Arduino sketch using their fully qualified name, for example:
301+
302+
```arduino
303+
int res1 = RPC.call("foo1.add", 1, 2).as<int>();
304+
int res2 = RPC.call("foo1.sub", 2, 1).as<int>();
305+
```
306+
307+
#### Starting the M4 core from MicroPython:
308+
309+
The next step is starting the M4 core by calling `MsgPackRPC.start()` with the firmware entry point as an argument:
310+
311+
```python
312+
# Start the remote processor and wait for it to be ready to communicate.
313+
rpc.start(firmware=0x08180000)
314+
```
315+
316+
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`.
317+
318+
#### Calling Remote Functions from MicroPython
319+
320+
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.
321+
322+
```python
323+
# Perform a synchronous call, which blocks until it returns.
324+
res = rpc.call("add", 1, 2)
325+
326+
# Perform an asynchronous call, which returns immediately with a Future object.
327+
f1 = rpc.call_async("add", 1, 2)
328+
329+
# The Future object returned above, must be joined at some point to get the results.
330+
print(f1.join())
331+
```
332+
333+
That covered most of the `msgpackrpc` library's API and use cases. For more complete examples and applications, see the `msgpackrpc` [repository](#https://github.com/arduino/arduino-lib-mpy/tree/main/lib/msgpackrpc).
334+
335+
238336
## RPC Examples
239337

240338
In this section, you will find a series of examples that is based on the `RPC` library.
@@ -251,12 +349,12 @@ The `Serial.print()` command only works on the **M7 core**. In order to print va
251349
#include <RPC.h>
252350
253351
void setup() {
254-
RPC.begin();
352+
RPC.begin();
255353
}
256354
257355
void loop() {
258-
RPC.println("Printed from M4 core");
259-
delay(1000);
356+
RPC.println("Printed from M4 core");
357+
delay(1000);
260358
}
261359
```
262360

@@ -266,8 +364,8 @@ delay(1000);
266364
#include <RPC.h>
267365
268366
void setup() {
269-
Serial.begin(9600);
270-
RPC.begin();
367+
Serial.begin(9600);
368+
RPC.begin();
271369
}
272370
273371
void loop() {
@@ -308,6 +406,7 @@ void setup() {
308406
}
309407
310408
void loop() {
409+
311410
}
312411
313412
/*
@@ -575,4 +674,4 @@ RPC.read();
575674
#### Returns
576675

577676
- The first available byte from the M4.
578-
- `-1` if there is none.
677+
- `-1` if there is none.

0 commit comments

Comments
 (0)