|
| 1 | +--- |
| 2 | +title: MicroPython on the GIGA R1 |
| 3 | +description: Get started with MicroPython on the GIGA R1. |
| 4 | +author: Karl Söderby |
| 5 | +tags: [MicroPython, dfu-util] |
| 6 | +--- |
| 7 | + |
| 8 | +***Arduino Docs now have a dedicated page for MicroPython related documentation. Please visit [MicroPython with Arduino](/micropython) to learn more. This article is specific only to the GIGA R1 / GIGA R1 WiFi boards.*** |
| 9 | + |
| 10 | +[MicroPython](https://micropython.org/) is an implementation of Python in a *micro* format. It can be installed on the GIGA R1, where instead of compiling and uploading sketches, the board has a built-in interpreter that converts Python code to machine code in runtime. |
| 11 | + |
| 12 | +This makes it possible to load various scripts instantly on the board, without the need of compiling the code, which is a great advantage of MicroPython. |
| 13 | + |
| 14 | +In this article, we will guide you through the installation steps, as well as some cool tips and tricks to get creative with MicroPython on the GIGA R1. |
| 15 | + |
| 16 | +## Hardware & Software Needed |
| 17 | + |
| 18 | +You will need the following hardware: |
| 19 | + |
| 20 | +- [Arduino GIGA R1](/giga-r1) or [Arduino GIGA R1 WiFi](/hardware/giga-r1-wifi) |
| 21 | + |
| 22 | +You will need the following software/tools installed: |
| 23 | +- [dfu-util](https://dfu-util.sourceforge.net/)\* |
| 24 | +- An editor with support for MicroPython (such as [Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython) or [Thonny](https://thonny.org/)) |
| 25 | +- `firmware.dfu` file, available for download at [MicroPython downloads](/micropython). |
| 26 | + |
| 27 | +***\*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`.*** |
| 28 | + |
| 29 | +## Installation |
| 30 | + |
| 31 | +**1.** Download the `firmware.dfu` file from the [MicroPython downloads](/micropython) page. |
| 32 | + |
| 33 | +**2.** Download the [dfu-util](https://dfu-util.sourceforge.net/) tool, or install via Brew (or other package managers). Make sure it is in your PATH. |
| 34 | + |
| 35 | +``` |
| 36 | +$ brew install dfu-util |
| 37 | +``` |
| 38 | + |
| 39 | +**3.** Double tap the reset button on the GIGA R1 board. The green LED should now be pulsing. This means it is in bootloader mode. |
| 40 | + |
| 41 | +**4.** Open a terminal, and navigate to the directory of your `firmware.dfu` file. Run the following command: |
| 42 | + |
| 43 | +``` |
| 44 | +$ dfu-util -w -a 0 -d 2341:0366 -D <firmware>.dfu |
| 45 | +``` |
| 46 | + |
| 47 | +>Replace `<firmware>` with the name of the file. |
| 48 | +
|
| 49 | +At the end of the process, you will see the following message on success: |
| 50 | + |
| 51 | +``` |
| 52 | +Done parsing DfuSe file |
| 53 | +``` |
| 54 | + |
| 55 | +**5.** Reset the board by clicking the reset button (just once). You should now be able to view the GIGA R1's mass storage in your file manager. |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +Congratulations! You have now installed MicroPython on the GIGA R1 board. |
| 60 | + |
| 61 | +## Programming Your Board |
| 62 | + |
| 63 | +To program your GIGA R1 with MicroPython, you will need an editor with support for MicroPython (such as [Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython) or [Thonny](https://thonny.org/)). |
| 64 | + |
| 65 | +The editors listed above are great for getting things to work quickly. One important aspect is that in the editor, you will need to **connect to your board**, as this will make it possible to load scripts. |
| 66 | + |
| 67 | +As the MicroPython installation has a built-in interpreter, in the editor, you can just write a script and click on the **Run Script** button, and if it compiles, it will load almost instantly. You can make sure it works by running something simple, such as: |
| 68 | + |
| 69 | +```python |
| 70 | +print("Hello world!") |
| 71 | +``` |
| 72 | + |
| 73 | +If it is printed in the REPL, it means it works, and you're ready to take on bigger challenges! |
| 74 | + |
| 75 | +To find out examples and more fundamental knowledge, please visit the [MicroPython with Arduino documentation](/micropython). Here you will find an introduction to this environment and useful examples to get started. |
| 76 | + |
| 77 | +***It is important to note that different Arduino boards have different implementations. This is mainly due to what microcontroller the board is based on. For example, to access digital pin 2, on the GIGA R1, you will need to use `'PA3'` (with the quotation marks). See more in [GIGA R1 Pin Map](#pin-map)*** |
| 78 | + |
| 79 | +## Main.py |
| 80 | + |
| 81 | +## File System (Mass Storage) |
| 82 | + |
| 83 | +The MicroPython installation exposes the mass storage device on your board, which can be edited directly from your computer. This allows you to install modules, edit the boot & main script as well as adding the possibility of switching between scripts. |
| 84 | + |
| 85 | +### Boot & Main Scripts |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +You will notice that in the mass storage device, there's two `.py` files: `boot.py` and `main.py`. |
| 90 | + |
| 91 | +- `boot.py` - when device loads, this script is executed. Typically you do not edit this script. |
| 92 | +- `main.py` - this script executes right after `boot.py`. You can consider this your classic Arduino sketch, that runs as soon your board is powered. If you want a specific program to run on your board, save it to `main.py`. |
| 93 | + |
| 94 | +### Adding Modules |
| 95 | + |
| 96 | +It is possible to add modules, drivers or scripts directly to your board. It is also as simple as dragging and dropping it into the mass storage, and `import` it in your script. |
| 97 | + |
| 98 | +### Execute Scripts |
| 99 | + |
| 100 | +A really cool thing with this implementation is that you can pack the mass storage with many different scripts, and load them when needed. |
| 101 | + |
| 102 | +This can be done using only a simple line of code: |
| 103 | + |
| 104 | +```python |
| 105 | +exec(open("script.py").read()) |
| 106 | +``` |
| 107 | + |
| 108 | +This allows you to create a number of scripts and store it on the mass storage, and execute them from the board itself (e.g. a button). |
| 109 | + |
| 110 | +## Pin Map |
| 111 | + |
| 112 | +In the table below, you will find the matching between the **STM32H7** and **Arduino**. When using MicroPython, you need to use the STM32H7 pins. |
| 113 | + |
| 114 | +| STM32H7 | Arduino | |
| 115 | +| ------- | ------- | |
| 116 | +| PB7 | D0 | |
| 117 | +| PA9 | D1 | |
| 118 | +| PA3 | D2 | |
| 119 | +| PA2 | D3 | |
| 120 | +| PJ8 | D4 | |
| 121 | +| PA7 | D5 | |
| 122 | +| PD13 | D6 | |
| 123 | +| PB4 | D7 | |
| 124 | +| PB8 | D8 | |
| 125 | +| PB9 | D9 | |
| 126 | +| PK1 | D10 | |
| 127 | +| PJ10 | D11 | |
| 128 | +| PJ11 | D12 | |
| 129 | +| PH6 | D13 | |
| 130 | +| PG14 | D14 | |
| 131 | +| PC7 | D15 | |
| 132 | +| PH13 | D16 | |
| 133 | +| PI9 | D17 | |
| 134 | +| PD5 | D18 | |
| 135 | +| PD6 | D19 | |
| 136 | +| PB11 | D20 | |
| 137 | +| PH4 | D21 | |
| 138 | +| PJ12 | D22 | |
| 139 | +| PG13 | D23 | |
| 140 | +| PG12 | D24 | |
| 141 | +| PJ0 | D25 | |
| 142 | +| PJ14 | D26 | |
| 143 | +| PJ1 | D27 | |
| 144 | +| PJ15 | D28 | |
| 145 | +| PJ2 | D29 | |
| 146 | +| PK3 | D30 | |
| 147 | +| PJ3 | D31 | |
| 148 | +| PK4 | D32 | |
| 149 | +| PJ4 | D33 | |
| 150 | +| PK5 | D34 | |
| 151 | +| PJ5 | D35 | |
| 152 | +| PK6 | D36 | |
| 153 | +| PJ6 | D37 | |
| 154 | +| PJ7 | D38 | |
| 155 | +| PI14 | D39 | |
| 156 | +| PE6 | D40 | |
| 157 | +| PK7 | D41 | |
| 158 | +| PI15 | D42 | |
| 159 | +| PI10 | D43 | |
| 160 | +| PG10 | D44 | |
| 161 | +| PI13 | D45 | |
| 162 | +| PH15 | D46 | |
| 163 | +| PB2 | D47 | |
| 164 | +| PK0 | D48 | |
| 165 | +| PE4 | D49 | |
| 166 | +| PI11 | D50 | |
| 167 | +| PE5 | D51 | |
| 168 | +| PK2 | D52 | |
| 169 | +| PG7 | D53 | |
| 170 | +| PI5 | D54 | |
| 171 | +| PH8 | D55 | |
| 172 | +| PA6 | D56 | |
| 173 | +| PJ9 | D57 | |
| 174 | +| PI7 | D58 | |
| 175 | +| PI6 | D59 | |
| 176 | +| PI4 | D60 | |
| 177 | +| PH14 | D61 | |
| 178 | +| PG11 | D62 | |
| 179 | +| PH11 | D63 | |
| 180 | +| PH10 | D64 | |
| 181 | +| PH9 | D65 | |
| 182 | +| PA1 | D66 | |
| 183 | +| PD4 | D67 | |
| 184 | +| PC6 | D68 | |
| 185 | +| PI0 | D69 | |
| 186 | +| PI1 | D70 | |
| 187 | +| PI2 | D71 | |
| 188 | +| PI3 | D72 | |
| 189 | +| PC1 | D73 | |
| 190 | +| PB12 | D74 | |
| 191 | +| PD3 | D75 | |
| 192 | +| PC4 | A0 | |
| 193 | +| PC5 | A1 | |
| 194 | +| PB0 | A2 | |
| 195 | +| PB1 | A3 | |
| 196 | +| PC3 | A4 | |
| 197 | +| PC2 | A5 | |
| 198 | +| PC0 | A6 | |
| 199 | +| PA0 | A7 | |
| 200 | +| PA4 | A12 | |
| 201 | +| PA5 | A13 | |
| 202 | + |
| 203 | +## Conclusion |
| 204 | + |
| 205 | +In this article, we have learned how to install MicroPython on the GIGA R1, using the `dfu-util` tool. We have also gone through some useful tips and tricks that can help you develop and run MicroPython code on your board. |
| 206 | + |
| 207 | +For more information, visit the [MicroPython with Arduino documentation](/micropython). |
0 commit comments