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
Copy file name to clipboardExpand all lines: content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md
+107-8
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
@@ -194,7 +195,7 @@ void blink(int led, int delaySeconds) {
194
195
- The `CM7_CPUID` flag that we compare with holds the value `0x00000003` (hexadecimal), or `3` (decimal).
195
196
- It is also possible to use `CM4_CPUID` flag which holds the value `0x00000003`, or `1` (decimal).
196
197
197
-
## Remote Call Procedures (RPC)
198
+
## Remote Procedure Call (RPC)
198
199
199
200
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.
200
201
@@ -235,6 +236,103 @@ When `call()` is used, a request is sent, it is processed on the server side, an
235
236
236
237

237
238
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
+
defsub(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
+
classFoo:
287
+
def__init__(self):
288
+
pass
289
+
290
+
defadd(a, b):
291
+
return a + b
292
+
293
+
defsub(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
+
238
336
## RPC Examples
239
337
240
338
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
0 commit comments