1
1
MicroPython port to the ESP32
2
2
=============================
3
3
4
- This is an experimental port of MicroPython to the Espressif ESP32
5
- microcontroller . It uses the ESP-IDF framework and MicroPython runs as
4
+ This is a port of MicroPython to the Espressif ESP32 series of
5
+ microcontrollers . It uses the ESP-IDF framework and MicroPython runs as
6
6
a task under FreeRTOS.
7
7
8
8
Supported features include:
9
9
- REPL (Python prompt) over UART0.
10
- - 16k stack for the MicroPython task and 96k Python heap.
10
+ - 16k stack for the MicroPython task and approximately 100k Python heap.
11
11
- Many of MicroPython's features are enabled: unicode, arbitrary-precision
12
12
integers, single-precision floats, complex numbers, frozen bytecode, as
13
13
well as many of the internal modules.
14
14
- Internal filesystem using the flash (currently 2M in size).
15
15
- The machine module with GPIO, UART, SPI, software I2C, ADC, DAC, PWM,
16
16
TouchPad, WDT and Timer.
17
17
- The network module with WLAN (WiFi) support.
18
+ - Bluetooth low-energy (BLE) support via the bluetooth module.
18
19
19
- Development of this ESP32 port was sponsored in part by Microbric Pty Ltd.
20
+ Initial development of this ESP32 port was sponsored in part by Microbric Pty Ltd.
20
21
21
- Setting up the toolchain and ESP-IDF
22
- ------------------------------------
22
+ Setting up ESP-IDF and the build environment
23
+ --------------------------------------------
23
24
24
- There are two main components that are needed to build the firmware:
25
- - the Xtensa cross-compiler that targets the CPU in the ESP32 (this is
26
- different to the compiler used by the ESP8266)
27
- - the Espressif IDF (IoT development framework, aka SDK)
25
+ MicroPython on ESP32 requires the Espressif IDF version 4 (IoT development
26
+ framework, aka SDK). The ESP-IDF includes the libraries and RTOS needed to
27
+ manage the ESP32 microcontroller, as well as a way to manage the required
28
+ build environment and toolchains needed to build the firmware.
29
+
30
+ The ESP-IDF changes quickly and MicroPython only supports certain versions.
31
+ Currently MicroPython supports v4.0.2, although other IDF v4 versions may also
32
+ work.
33
+
34
+ To install the ESP-IDF the full instructions can be found at the
35
+ [ Espressif Getting Started guide] ( https://docs.espressif.com/projects/esp-idf/en/v4.0.2/get-started/index.html#installation-step-by-step ) .
28
36
29
37
If you are on a Windows machine then the [ Windows Subsystem for
30
38
Linux] ( https://msdn.microsoft.com/en-au/commandline/wsl/install_guide ) is the
31
39
most efficient way to install the ESP32 toolchain and build the project. If
32
40
you use WSL then follow the Linux instructions rather than the Windows
33
41
instructions.
34
42
35
- The ESP-IDF changes quickly and MicroPython only supports certain versions.
36
- The git hash of these versions (one for 3.x, one for 4.x) can be found by
37
- running ` make ` without a configured ` ESPIDF ` . Then you can fetch the
38
- required IDF using the following command:
39
-
40
- ``` bash
41
- $ cd ports/esp32
42
- $ make ESPIDF= # This will print the supported hashes, copy the one you want.
43
- $ export ESPIDF=$HOME /src/github.com/espressif/esp-idf # Or any path you like.
44
- $ mkdir -p $ESPIDF
45
- $ cd $ESPIDF
46
- $ git clone https://github.com/espressif/esp-idf.git $ESPIDF
47
- $ git checkout < Current supported ESP-IDF commit hash>
48
- $ git submodule update --init --recursive
49
- ```
50
-
51
- Note: The ESP IDF v4.x support is currently experimental. It does not
52
- currently support PPP or wired Ethernet.
53
-
54
- Python dependencies
55
- ===================
56
-
57
- You will also need other dependencies from the IDF, see
58
- ` $ESPIDF/requirements.txt ` , but at a minimum you need ` pyserial>=3.0 ` and
59
- ` pyparsing>=2.0.3,<2.4.0 ` .
60
-
61
- You can use Python 2 or Python 3. If you need to override the system default
62
- add (for example) ` PYTHON=python3 ` to any of the ` make ` commands below.
63
-
64
- It is recommended to use a Python virtual environment. Even if your system
65
- package manager already provides these libraries, the IDF v4.x is currently
66
- incompatible with pyparsing 2.4 and higher.
67
-
68
- For example, to set up a Python virtual environment from scratch:
69
-
70
- ``` bash
71
- $ cd ports/esp32
72
- $ python3 -m venv build-venv
73
- $ source build-venv/bin/activate
74
- $ pip install --upgrade pip
75
- $ pip install -r path/to/esp-idf/requirements.txt
76
- ```
43
+ The Espressif instructions will guide you through using the ` install.sh `
44
+ (or ` install.bat ` ) script to download the toolchain and set up your environment.
45
+ The steps to take are summarised below.
77
46
78
- To re-enter this virtual environment in future sessions, you only need to
79
- source the ` activate ` script, i.e.:
47
+ To check out a copy of the IDF use git clone:
80
48
81
49
``` bash
82
- $ cd ports/esp32
83
- $ source build-venv/bin/activate
50
+ $ git clone -b v4.0.2 --recursive https://github.com/espressif/esp-idf.git
84
51
```
85
52
86
- Then, to install the toolchain (which includes the GCC compiler, linker, binutils,
87
- etc), there are two options:
88
-
89
- 1 . Using the IDF scripts to install the toolchain (IDF 4.x only)
90
- ================================================================
91
-
92
- Follow the steps at the [ Espressif Getting Started guide] ( https://docs.espressif.com/projects/esp-idf/en/v4.0/get-started/index.html#step-3-set-up-the-tools ) .
93
-
94
- This will guide you through using the ` install.sh ` (or ` install.bat ` ) script
95
- to download the toolchain and add it to your ` PATH ` . The steps are summarised
96
- below:
97
-
98
- After you've cloned and checked out the IDF to the correct version (see
99
- above), run the ` install.sh ` script:
100
-
101
- ``` bash
102
- $ cd $ESPIDF
103
- $ ./install.sh # (or install.bat on Windows)
104
- ```
53
+ (You don't need a full recursive clone; see the ` ci_esp32_setup ` function in
54
+ ` tools/ci.sh ` in this repository for more detailed set-up commands.)
105
55
106
- Then in the ` ports/esp32 ` directory, source the ` export.sh ` script to set the
107
- ` PATH ` .
56
+ After you've cloned and checked out the IDF to the correct version, run the
57
+ ` install.sh ` script:
108
58
109
59
``` bash
110
- $ cd micropython/ports/esp32
111
- $ source $ESPIDF /export .sh # (or path\to\esp-idf\export .bat on Windows)
112
- $ # Run make etc, see below.
60
+ $ cd esp-idf
61
+ $ ./install .sh # (or install .bat on Windows)
62
+ $ source export.sh # (or export.bat on Windows)
113
63
```
114
64
115
65
The ` install.sh ` step only needs to be done once. You will need to source
116
66
` export.sh ` for every new session.
117
67
118
- Note: If you get an error about ` --no-site-packages ` , then modify
119
- ` $ESPIDF/tools/idf_tools.py ` and make the same change as [ this
120
- commit] ( https://github.com/espressif/esp-idf/commit/7a18f02acd7005f7c56e62175a8d1968a1a9019d ) .
121
-
122
- 2 . or, Downloading pre-built toolchain manually (IDF 3.x and 4.x)
123
- =============================================================
124
-
125
- Note: while this works with 4.x, if you're using the 4.x IDF, it's much
126
- simpler to use the guide above, which will also get a more recent version of
127
- the toolchain.
128
-
129
- You can follow the 3.x guide at:
130
-
131
- * [ Linux installation] ( https://docs.espressif.com/projects/esp-idf/en/v3.3.2/get-started/linux-setup.html )
132
- * [ MacOS installation] ( https://docs.espressif.com/projects/esp-idf/en/v3.3.2/get-started/macos-setup.html )
133
- * [ Windows installation] ( https://docs.espressif.com/projects/esp-idf/en/v3.3.2/get-started/windows-setup.html )
134
-
135
- You will need to update your ` PATH ` environment variable to include the ESP32
136
- toolchain. For example, you can issue the following commands on (at least)
137
- Linux:
138
-
139
- $ export PATH=$PATH:$HOME/esp/crosstool-NG/builds/xtensa-esp32-elf/bin
140
-
141
- You can put this command in your ` .profile ` or ` .bash_login ` , or do it manually.
142
-
143
- Configuring the MicroPython build
144
- ---------------------------------
145
-
146
- You then need to set the ` ESPIDF ` environment/makefile variable to point to
147
- the root of the ESP-IDF repository. The recommended way to do this is to have
148
- a custom ` makefile ` in ` ports/esp32 ` which sets any additional variables, then
149
- includes the main ` Makefile ` . Note that GNU Make will preferentially run
150
- ` GNUmakefile ` , then ` makefile ` , then ` Makefile ` , which is what allows this to
151
- work. On case-insensitive filesystems, you'll need to use ` GNUmakefile ` rather
152
- than ` makefile ` .
153
-
154
- Create a new file in the esp32 directory called ` makefile ` (or ` GNUmakefile ` )
155
- and add the following lines to that file:
156
-
157
- ```
158
- ESPIDF ?= <path to root of esp-idf repository>
159
- BOARD ?= GENERIC
160
- #PORT ?= /dev/ttyUSB0
161
- #FLASH_MODE ?= qio
162
- #FLASH_SIZE ?= 4MB
163
- #CROSS_COMPILE ?= xtensa-esp32-elf-
164
-
165
- include Makefile
166
- ```
167
-
168
- Be sure to enter the correct path to your local copy of the IDF repository
169
- (and use ` $(HOME) ` , not tilde (` ~ ` ), to reference your home directory).
170
-
171
- If the Xtensa cross-compiler is not in your path you can use the
172
- ` CROSS_COMPILE ` variable to set its location. Other options of interest are
173
- ` PORT ` for the serial port of your ESP32 module, and ` FLASH_MODE ` (which may
174
- need to be ` dio ` for some modules) and ` FLASH_SIZE ` . See the Makefile for
175
- further information.
176
-
177
- The default ESP IDF configuration settings are provided by the ` GENERIC `
178
- board definition in the directory ` boards/GENERIC ` . For a custom configuration
179
- you can define your own board directory.
180
-
181
- Any of these variables can also be set on the make command line, e.g. to set
182
- the ` BOARD ` variable, use:
183
-
184
- ``` bash
185
- $ make BOARD=TINYPICO
186
- ```
187
-
188
- Note the use of ` ?= ` in the ` makefile ` which allows them to be overridden on
189
- the command line. There is also a ` GENERIC_SPIRAM ` board for for ESP32
190
- modules that have external SPIRAM, but prefer to use a specific board target
191
- (or define your own as necessary).
192
-
193
68
Building the firmware
194
69
---------------------
195
70
@@ -198,8 +73,7 @@ built-in scripts to bytecode. This can be done by (from the root of
198
73
this repository):
199
74
200
75
``` bash
201
- $ cd mpy-cross
202
- $ make mpy-cross
76
+ $ make -C mpy-cross
203
77
```
204
78
205
79
Then to build MicroPython for the ESP32 run:
@@ -210,16 +84,14 @@ $ make submodules
210
84
$ make
211
85
```
212
86
213
- This will produce binary firmware images in the ` build/ ` subdirectory
214
- (three of them: bootloader.bin, partitions.bin and application.bin).
87
+ This will produce a combined ` firmware.bin ` image in the ` build-GENERIC/ `
88
+ subdirectory (this firmware image is made up of: bootloader.bin, partitions.bin
89
+ and micropython.bin).
215
90
216
91
To flash the firmware you must have your ESP32 module in the bootloader
217
92
mode and connected to a serial port on your PC. Refer to the documentation
218
- for your particular ESP32 module for how to do this. The serial port and
219
- flash settings are set in the ` Makefile ` , and can be overridden in your
220
- local ` makefile ` ; see above for more details.
221
-
222
- You will also need to have user permissions to access the /dev/ttyUSB0 device.
93
+ for your particular ESP32 module for how to do this.
94
+ You will also need to have user permissions to access the ` /dev/ttyUSB0 ` device.
223
95
On Linux, you can enable this by adding your user to the ` dialout ` group, and
224
96
rebooting or logging out and in again. (Note: on some distributions this may
225
97
be the ` uucp ` group, run ` ls -la /dev/ttyUSB0 ` to check.)
@@ -242,8 +114,23 @@ To flash the MicroPython firmware to your ESP32 use:
242
114
$ make deploy
243
115
```
244
116
245
- This will use the ` esptool.py ` script (provided by ESP-IDF) to flash the
246
- binary images to the device.
117
+ The default ESP32 board build by the above commands is the ` GENERIC ` one, which
118
+ should work on most ESP32 modules. You can specify a different board by passing
119
+ ` BOARD=<board> ` to the make commands, for example:
120
+
121
+ ``` bash
122
+ $ make BOARD=GENERIC_SPIRAM
123
+ ```
124
+
125
+ Note: the above "make" commands are thin wrappers for the underlying ` idf.py `
126
+ build tool that is part of the ESP-IDF. You can instead use ` idf.py ` directly,
127
+ for example:
128
+
129
+ ``` bash
130
+ $ idf.py build
131
+ $ idf.py -D MICROPY_BOARD=GENERIC_SPIRAM build
132
+ $ idf.py flash
133
+ ```
247
134
248
135
Getting a Python prompt on the device
249
136
-------------------------------------
262
149
$ miniterm.py /dev/ttyUSB0 115200
263
150
```
264
151
152
+ You can also use ` idf.py monitor ` .
153
+
265
154
Configuring the WiFi and using the board
266
155
----------------------------------------
267
156
@@ -301,9 +190,27 @@ import machine
301
190
antenna = machine.Pin(16 , machine.Pin.OUT , value = 0 )
302
191
```
303
192
193
+ Defining a custom ESP32 board
194
+ -----------------------------
195
+
196
+ The default ESP-IDF configuration settings are provided by the ` GENERIC `
197
+ board definition in the directory ` boards/GENERIC ` . For a custom configuration
198
+ you can define your own board directory. Start a new board configuration by
199
+ copying an existing one (like ` GENERIC ` ) and modifying it to suit your board.
200
+
201
+ MicroPython specific configuration values are defined in the board-specific
202
+ ` mpconfigboard.h ` file, which is included by ` mpconfigport.h ` . Additional
203
+ settings are put in ` mpconfigboard.cmake ` , including a list of ` sdkconfig `
204
+ files that configure ESP-IDF settings. Some standard ` sdkconfig ` files are
205
+ provided in the ` boards/ ` directory, like ` boards/sdkconfig.ble ` . You can
206
+ also define custom ones in your board directory.
207
+
208
+ See existing board definitions for further examples of configuration.
209
+
210
+ Configuration
304
211
Troubleshooting
305
212
---------------
306
213
307
- * Continuous reboots after programming: Ensure FLASH_MODE is correct for your
308
- board (e.g. ESP-WROOM-32 should be DIO). Then perform a ` make clean ` , rebuild,
309
- redeploy.
214
+ * Continuous reboots after programming: Ensure ` CONFIG_ESPTOOLPY_FLASHMODE ` is
215
+ correct for your board (e.g. ESP-WROOM-32 should be DIO). Then perform a
216
+ ` make clean ` , rebuild, redeploy.
0 commit comments