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
+156-9
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.
@@ -235,6 +237,103 @@ When `call()` is used, a request is sent, it is processed on the server side, an
235
237
236
238

237
239
240
+
### Using the RPC Library with MicroPython
241
+
242
+
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.
243
+
244
+
The following sections introduce the `msgpackrpc` library API and some use cases in detail.
245
+
246
+
#### The `msgpackrpc` Library
247
+
248
+
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:
249
+
250
+
```python
251
+
import msgpackrpc
252
+
253
+
# Create a MsgPackRPC object.
254
+
rpc = msgpackrpc.MsgPackRPC()
255
+
```
256
+
257
+
The RPC object created above can then be used to bind Python callables, start the M4 core and invoke remote calls from MicroPython scripts.
258
+
259
+
#### Binding Python Functions, Callables and Objects
260
+
261
+
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`:
262
+
263
+
```python
264
+
defsub(a, b):
265
+
return a - b
266
+
267
+
# Register a function to be called by the remote processor.
268
+
rpc.bind("sub", sub)
269
+
```
270
+
271
+
Similarly, an object's bound method can also be bound to a name. For example:
272
+
273
+
```python
274
+
foo = Foo()
275
+
rpc.bind("sub", foo.add)
276
+
```
277
+
278
+
Both of those functions can be called in the same way from the Arduino sketch:
279
+
280
+
```arduino
281
+
int res = RPC.call("sub", 2, 1).as<int>();
282
+
```
283
+
284
+
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`:
285
+
286
+
```python
287
+
classFoo:
288
+
def__init__(self):
289
+
pass
290
+
291
+
defadd(a, b):
292
+
return a + b
293
+
294
+
defsub(a, b):
295
+
return a - b
296
+
297
+
# Register an object of Foo
298
+
rpc.bind("foo1", Foo())
299
+
```
300
+
301
+
Now the object's methods can be invoked by the Arduino sketch using their fully qualified name, for example:
302
+
303
+
```arduino
304
+
int res1 = RPC.call("foo1.add", 1, 2).as<int>();
305
+
int res2 = RPC.call("foo1.sub", 2, 1).as<int>();
306
+
```
307
+
308
+
#### Starting the M4 Core From Micropython
309
+
310
+
The next step is starting the M4 core by calling `MsgPackRPC.start()` with the firmware entry point as an argument:
311
+
312
+
```python
313
+
# Start the remote processor and wait for it to be ready to communicate.
314
+
rpc.start(firmware=0x08180000)
315
+
```
316
+
317
+
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`.
318
+
319
+
#### Calling Remote Functions From Micropython
320
+
321
+
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.
322
+
323
+
```python
324
+
# Perform a synchronous call, which blocks until it returns.
325
+
res = rpc.call("add", 1, 2)
326
+
327
+
# Perform an asynchronous call, which returns immediately with a Future object.
328
+
f1 = rpc.call_async("add", 1, 2)
329
+
330
+
# The Future object returned above, must be joined at some point to get the results.
331
+
print(f1.join())
332
+
```
333
+
334
+
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).
335
+
336
+
238
337
## RPC Examples
239
338
240
339
In this section, you will find a series of examples that is based on the `RPC` library.
@@ -251,12 +350,12 @@ The `Serial.print()` command only works on the **M7 core**. In order to print va
251
350
#include <RPC.h>
252
351
253
352
void setup() {
254
-
RPC.begin();
353
+
RPC.begin();
255
354
}
256
355
257
356
void loop() {
258
-
RPC.println("Printed from M4 core");
259
-
delay(1000);
357
+
RPC.println("Printed from M4 core");
358
+
delay(1000);
260
359
}
261
360
```
262
361
@@ -266,8 +365,8 @@ delay(1000);
266
365
#include <RPC.h>
267
366
268
367
void setup() {
269
-
Serial.begin(9600);
270
-
RPC.begin();
368
+
Serial.begin(9600);
369
+
RPC.begin();
271
370
}
272
371
273
372
void loop() {
@@ -308,6 +407,7 @@ void setup() {
308
407
}
309
408
310
409
void loop() {
410
+
311
411
}
312
412
313
413
/*
@@ -454,6 +554,53 @@ int servoMove(int angle) {
454
554
}
455
555
```
456
556
557
+
### MicroPython RPC LED
558
+
559
+
This example demonstrates how to use MicroPython (running on the M7 core) to remotely control an LED connected to the M4 core.
560
+
561
+
**M4 sketch:**
562
+
563
+
```arduino
564
+
#include "RPC.h"
565
+
566
+
void led(bool on) {
567
+
if (on) {
568
+
digitalWrite(LEDG, LOW);
569
+
} else {
570
+
digitalWrite(LEDG, HIGH);
571
+
}
572
+
}
573
+
574
+
void setup() {
575
+
RPC.bind("led", led);
576
+
RPC.begin();
577
+
}
578
+
579
+
void loop() {
580
+
581
+
}
582
+
```
583
+
584
+
**M7 Python script:**
585
+
586
+
```python
587
+
import time
588
+
import msgpackrpc
589
+
590
+
# Create an RPC object
591
+
rpc = msgpackrpc.MsgPackRPC()
592
+
593
+
# Start the remote processor.
594
+
rpc.start(firmware=0x08180000)
595
+
596
+
# Toggle the LED every 500ms
597
+
whileTrue:
598
+
rpc.call("led", True)
599
+
time.sleep_ms(500)
600
+
rpc.call("led", False)
601
+
time.sleep_ms(500)
602
+
```
603
+
457
604
## RPC Library API
458
605
459
606
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.
0 commit comments