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
The GIGA R1's microcontroller, the **STM32H747XI** has two processor cores, the **M7** and **M4**, clocking in at 480MHz and 240Mhz respectively.
9
9
10
-
Having two cores in a microcontroller brings a significant advantage, to run two main applications simultaneuously, and communicate with them through something called **Remote Procedure Call (RPC)**.
11
-
10
+
Having two cores in a microcontroller brings a significant advantage, to run two main applications simultaneuously, and communicate with them through something called **Remote Procedure Call (RPC)**.
12
11
13
12
## Goals
14
13
15
14
In this tutorial, we will take a closer look at how to make use of the dual core, by:
16
-
- Running MicroPython on the M7 core.
17
-
- Running Arduino code on the M4 core.
18
-
- Set up a communication line between the two cores through RPC.
15
+
- Set up and run MicroPython on the M7 core.
16
+
- Set up and run Arduino code on the M4 core.
17
+
- Link, or bind, the two cores through RPC, using a specific serial protocol.
18
+
- Control a servo motor by requesting the service from the M7 to the M4.
@@ -32,6 +33,10 @@ For programming the M7 core (MicroPython):
32
33
-[dfu-util](https://dfu-util.sourceforge.net/)
33
34
-[firmware.dfu](linktofw)
34
35
36
+
You will also need a MicroPython supported editor, such as:
37
+
-[Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython)
38
+
-[Thonny](https://thonny.org/)
39
+
35
40
***The installation for `dfu-util` varies between operation systems. For installation using [brew.sh](https://formulae.brew.sh/formula/dfu-util) simply use `brew install dfu-util`.***
36
41
37
42
## Remote Procedure Call (RPC)
@@ -40,15 +45,15 @@ RPC is a method that allows programs to make requests to programs located elsewh
40
45
41
46
An RPC is a synchronous operation, and while a request is being made (client/caller) to another system, the operation is suspended. On return of the results, the operation is resumed. On the other side, (server/callee) performs the subroutine on request, and suspends any other operation as well. After it sends the result to the client, it resumes its operation, while waiting for another request.
42
47
43
-
![Request routine.]()
48
+

44
49
45
50
### RPCs in the Arduino Environment
46
51
47
52
As some microcontrollers, including the STM32H747XI, has two processors, it is possible to program them both to perform individual tasks, and enable communication between them via an RPC.
48
53
49
-
The advantage of this is great, as you essentially have "two" Arduino boards running (on the some board). For example, you can run a machine learning module on one of the core, while another core is connected to a network system such as the [Arduino IoT Cloud](). Running these applications in parallel increases performance, as you split the work load and allow them to run with less blocking code for example.
54
+
The advantage of this is great, as you essentially have "two" Arduino boards running (on the some board). For example, you can run a machine learning module on one of the core, while another core is connected to a network system such as the [Arduino IoT Cloud](https://create.arduino.cc/iot/). Running these applications in parallel increases performance, as you split the work load and allow them to run with less blocking code for example.
50
55
51
-
![Communication between cores]()
56
+

52
57
53
58
### RPC vs Multi-Threading
54
59
@@ -72,15 +77,64 @@ The GIGA R1's STM32H747XI microcontroller includes the M7 and M4 core. In this e
72
77
- Upload a sketch to the **M4 that sets it up as a client/caller**.
73
78
- Make a request from the M4 to the M7.
74
79
75
-
### Installing MicroPython
80
+
### 1. Upload Sketch to M4 Core
81
+
82
+
We will start by uploading a sketch to the M4 core, which will be running Arduino code. This requires the [Arduino IDE](https://www.arduino.cc/en/software) to be installed, as well as the **Arduino MbedOS OS GIGA Boards** core installed.
83
+
84
+
**1.1.** Open the Arduino IDE, and the board manager (in the menu to the left). Install the **Arduino MbedOS OS GIGA Boards** package.
85
+
86
+
**1.2.** Now we need to select the right board. This is **not** the GIGA R1, as the default is H7. We instead need to select the **STM32H747 M4 coprocessor**. This is the M4 on the GIGA R1.
87
+
88
+
**1.3.** With the board selected, you will now have the option to select **Flash Split** and **Target Board**. These are manual options available in the **Tools** section of the IDE. Here, we need to select the **1.5MB M7 + 0.5MB M4** configuration for the Flash Split, and **Giga** for Target Board.
**1.4.** Upload the following sketch to the board:
93
+
94
+
```arduino
95
+
#include "RPC.h"
96
+
#include <Servo.h>
97
+
98
+
uint32_t servo_new(uint32_t id) {
99
+
Servo* servo = new Servo();
100
+
return (uint32_t)servo;
101
+
}
102
+
103
+
void servo_attach(uint32_t id, int pin) {
104
+
Servo* servo = (Servo*)id;
105
+
servo->attach(pin);
106
+
}
107
+
108
+
void servo_write(uint32_t id, float angle) {
109
+
Servo* servo = (Servo*)id;
110
+
servo->write(angle);
111
+
}
112
+
113
+
void setup() {
114
+
RPC.begin();
115
+
RPC.bind("servo_new", servo_new);
116
+
RPC.bind("servo_attach", servo_attach);
117
+
RPC.bind("servo_write", servo_write);
118
+
}
119
+
120
+
void loop() {
121
+
delay(1000);
122
+
}
123
+
```
124
+
125
+
### 2. Installing MicroPython
126
+
127
+
To install MicroPython on the GIGA R1, you will need to flash a specific MicroPython firmware to the **M7 processor.** This requires the [dfu-util](https://dfu-util.sourceforge.net/) tool.
76
128
77
-
To install MicroPython on the GIGA R1, you will need to flash a specific MicroPython firmware to the **M7 processor.**This requires the [dfu-util]() tool.
129
+
**2.1.**Download the [MicroPython firmware for GIGA R1]().
78
130
79
-
1. Download the [MicroPython firmware for GIGA R1]().
80
-
2. Download [dfu-util]() (also available via [brew.sh](https://formulae.brew.sh/formula/dfu-util)). Make sure the tool is added to your PATH on your machine.
81
-
3. Open a terminal, and navigate to the directory where you saved the downloaded MicroPython firmware.
82
-
4. Double tap the reset button on the GIGA R1 (while it is powered). This will enter bootloader mode.
83
-
5. Finally, load the MicroPython firmware, by using the following command:
131
+
**2.2.** Download [dfu-util](https://dfu-util.sourceforge.net/) (also available via [brew.sh](https://formulae.brew.sh/formula/dfu-util)). Make sure the tool is added to PATH on your machine.
132
+
133
+
**2.3.** Open a terminal, and navigate to the directory where you saved the downloaded MicroPython firmware.
134
+
135
+
**2.4.** Double tap the reset button on the GIGA R1 (while it is powered). This will enter bootloader mode.
136
+
137
+
**2.5.** Finally, load the MicroPython firmware, by using the following command:
84
138
85
139
```sh
86
140
dfu-util -w -a 0 -d 2341:0366 -D <firmware>.dfu
@@ -90,15 +144,46 @@ This will start an uploading process that can be tracked in the terminal. Once i
90
144
91
145
Make sure to reset the board before continuing (tap the reset button).
92
146
93
-
### Flash M4
147
+
### 3. MessagePack
94
148
95
-
### Load MicroPython Script
149
+
***[MessagePack]() is a serialization protocol made by Peter Hinch that we are using in this implementation to communicate between the cores.***
96
150
151
+
In order to implement an RPC between the M4 and M7 cores, we use MessagePack, or the `umsg` module. This module needs to be present on your board, so that we can use it a MicroPython script (it is not part of the MicroPython installation).
97
152
98
-
###
153
+
When we install MicroPython on the M7 core, we create a mass storage device that we can simply drag and drop files to.
99
154
155
+
**3.1.** First, download the [umsg module]() and unpack it.
100
156
101
-
## Conclusion
157
+
**3.2.** Open the mass storage device (available in e.g. finder), and drag only the `umsg` folder to the root of the mass storage device.
158
+
159
+
![Drag and drop the umsg folder.]()
160
+
161
+
You now have the `umsg` module available on your board, and can be called from a MicroPython script.
162
+
163
+
### 4. Load MicroPython Script
164
+
165
+
The final step is to load a MicroPython script to the M7 that will interact with the M4 core, essentially using the "server-client" protocol.
166
+
167
+
**4.1.** Open the MicroPython editor (such as [Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython)).
102
168
103
-
Add a conclusion to what this tutorial has gone through. Connect back to what you wrote in the "Goals" section.
169
+
**4.2.** Select/connect your board. You can test out if it is connected properly to your computer by running a simple `print ("test")`. If it shows in the REPL, it works.
170
+
171
+
**4.3.** Paste the script below to the editor, and press the play/run button.
172
+
173
+
```python
174
+
import arduino
175
+
arduino.init()
176
+
servo = arduino.Servo()
177
+
servo.attach(6)
178
+
servo.write(64)
179
+
```
180
+
181
+
This basic script includes the API for controlling a servo motor. But, the main difference here, is that the servo motor is controlled on the M4 core.
182
+
183
+
When for example `servo.attach(6)` is run, this action is not executed on the M7 core, but instead requested to run on the M4.
184
+
185
+
You can revisit [the code in the first step](#1-upload-sketch-to-m4-core), to understand how the link is made between the MicroPython script, and the Arduino sketch.
186
+
187
+
## Conclusion
104
188
189
+
At the end of this tutorial, we have achieved the communication between the M4 and the M7 core, via something called **RPC**. The example used in this tutorial is a servo motor, but there are of course many more alternatives that you can choose from.
0 commit comments