549448e8bb
This commit enables some significant optimisations for esp32: - move the VM to iRAM - move hot parts of the runtime to iRAM (map lookup, load global/name, mp_obj_get_type) - enable MICROPY_OPT_LOAD_ATTR_FAST_PATH - enable MICROPY_OPT_MAP_LOOKUP_CACHE - disable assertions - change from -Os to -O2 for compilation It's hard to measure performance on esp32 due to external flash and hardware caching. But this set of changes improves performance compared to master by (on a TinyPICO with the GENERIC build, using IDF 4.2.2, running at 160MHz): diff of scores (higher is better) N=100 M=100 esp32-master -> esp32-perf diff diff% (error%) bm_chaos.py 71.28 -> 268.08 : +196.80 = +276.094% (+/-0.04%) bm_fannkuch.py 44.10 -> 69.31 : +25.21 = +57.166% (+/-0.01%) bm_fft.py 1385.27 -> 2538.23 : +1152.96 = +83.230% (+/-0.01%) bm_float.py 1060.94 -> 3900.62 : +2839.68 = +267.657% (+/-0.03%) bm_hexiom.py 10.90 -> 32.79 : +21.89 = +200.826% (+/-0.02%) bm_nqueens.py 1000.83 -> 2372.87 : +1372.04 = +137.090% (+/-0.01%) bm_pidigits.py 288.13 -> 664.40 : +376.27 = +130.590% (+/-0.46%) misc_aes.py 102.45 -> 345.69 : +243.24 = +237.423% (+/-0.01%) misc_mandel.py 1016.58 -> 2121.92 : +1105.34 = +108.731% (+/-0.01%) misc_pystone.py 632.91 -> 1801.87 : +1168.96 = +184.696% (+/-0.08%) misc_raytrace.py 76.66 -> 281.78 : +205.12 = +267.571% (+/-0.05%) viper_call0.py 210.63 -> 273.17 : +62.54 = +29.692% (+/-0.01%) viper_call1a.py 208.45 -> 269.51 : +61.06 = +29.292% (+/-0.00%) viper_call1b.py 185.44 -> 228.25 : +42.81 = +23.086% (+/-0.01%) viper_call1c.py 185.86 -> 228.90 : +43.04 = +23.157% (+/-0.01%) viper_call2a.py 207.10 -> 267.25 : +60.15 = +29.044% (+/-0.00%) viper_call2b.py 173.76 -> 209.42 : +35.66 = +20.523% (+/-0.00%) Five tests have more than 3x speed up (200%+). The performance of the tests bm_fft, bm_pidigits and misc_aes now scale with CPU frequency (eg changing frequency to 240MHz boosts the performance of these by 50%), which means they are no longer influenced by timing of external flash access. (The viper_call* tests did previously scale with CPU frequency, and they still do.) Turning off assertions reduces code size by about 80k, and going from -Os to -O2 costs about 100k, so the net change in code size (for the GENERIC board) is about +20k. If a board wants to enable assertions, or use -Os instead of -O2, that's still possible by overriding the sdkconfig parameters. Signed-off-by: Damien George <damien@micropython.org> |
||
---|---|---|
.. | ||
boards | ||
main | ||
modules | ||
CMakeLists.txt | ||
esp32_nvs.c | ||
esp32_partition.c | ||
esp32_rmt.c | ||
esp32_ulp.c | ||
fatfs_port.c | ||
gccollect.c | ||
gccollect.h | ||
help.c | ||
machine_adc.c | ||
machine_bitstream.c | ||
machine_dac.c | ||
machine_hw_spi.c | ||
machine_i2c.c | ||
machine_i2s.c | ||
machine_pin.c | ||
machine_pwm.c | ||
machine_rtc.c | ||
machine_rtc.h | ||
machine_sdcard.c | ||
machine_timer.c | ||
machine_touchpad.c | ||
machine_uart.c | ||
machine_wdt.c | ||
main.c | ||
Makefile | ||
makeimg.py | ||
memory.h | ||
modesp32.c | ||
modesp32.h | ||
modesp.c | ||
modesp.h | ||
modmachine.c | ||
modmachine.h | ||
modnetwork.c | ||
modnetwork.h | ||
modsocket.c | ||
moduos.c | ||
modutime.c | ||
mpconfigport.h | ||
mphalport.c | ||
mphalport.h | ||
mpnimbleport.c | ||
mpthreadport.c | ||
mpthreadport.h | ||
network_lan.c | ||
network_ppp.c | ||
network_wlan.c | ||
partitions-2MiB.csv | ||
partitions-8MiB.csv | ||
partitions-16MiB-ota.csv | ||
partitions-16MiB.csv | ||
partitions-ota.csv | ||
partitions.csv | ||
qstrdefsport.h | ||
README.md | ||
README.ulp.md | ||
uart.c | ||
uart.h | ||
usb_serial_jtag.c | ||
usb_serial_jtag.h | ||
usb.c | ||
usb.h |
MicroPython port to the ESP32
This is a port of MicroPython to the Espressif ESP32 series of microcontrollers. It uses the ESP-IDF framework and MicroPython runs as a task under FreeRTOS.
Supported features include:
- REPL (Python prompt) over UART0.
- 16k stack for the MicroPython task and approximately 100k Python heap.
- Many of MicroPython's features are enabled: unicode, arbitrary-precision integers, single-precision floats, complex numbers, frozen bytecode, as well as many of the internal modules.
- Internal filesystem using the flash (currently 2M in size).
- The machine module with GPIO, UART, SPI, software I2C, ADC, DAC, PWM, TouchPad, WDT and Timer.
- The network module with WLAN (WiFi) support.
- Bluetooth low-energy (BLE) support via the bluetooth module.
Initial development of this ESP32 port was sponsored in part by Microbric Pty Ltd.
Setting up ESP-IDF and the build environment
MicroPython on ESP32 requires the Espressif IDF version 4 (IoT development framework, aka SDK). The ESP-IDF includes the libraries and RTOS needed to manage the ESP32 microcontroller, as well as a way to manage the required build environment and toolchains needed to build the firmware.
The ESP-IDF changes quickly and MicroPython only supports certain versions. Currently MicroPython supports v4.0.2, v4.1.1 and v4.2, although other IDF v4 versions may also work.
To install the ESP-IDF the full instructions can be found at the Espressif Getting Started guide.
If you are on a Windows machine then the Windows Subsystem for Linux is the most efficient way to install the ESP32 toolchain and build the project. If you use WSL then follow the Linux instructions rather than the Windows instructions.
The Espressif instructions will guide you through using the install.sh
(or install.bat
) script to download the toolchain and set up your environment.
The steps to take are summarised below.
To check out a copy of the IDF use git clone:
$ git clone -b v4.0.2 --recursive https://github.com/espressif/esp-idf.git
You can replace v4.0.2
with v4.1.1
or v4.2
or any other supported version.
(You don't need a full recursive clone; see the ci_esp32_setup
function in
tools/ci.sh
in this repository for more detailed set-up commands.)
If you already have a copy of the IDF then checkout a version compatible with MicroPython and update the submodules using:
$ cd esp-idf
$ git checkout v4.2
$ git submodule update --init --recursive
After you've cloned and checked out the IDF to the correct version, run the
install.sh
script:
$ cd esp-idf
$ ./install.sh # (or install.bat on Windows)
$ source export.sh # (or export.bat on Windows)
The install.sh
step only needs to be done once. You will need to source
export.sh
for every new session.
Note: If you are building MicroPython for the ESP32-S2, ESP32-C3 or ESP32-S3, please ensure you are using the following required IDF versions:
- ESP32-S3 currently requires latest
master
, but eventuallyv4.4
or later when it's available. - ESP32-S2 and ESP32-C3 require
v4.3.1
or later.
Building the firmware
The MicroPython cross-compiler must be built to pre-compile some of the built-in scripts to bytecode. This can be done by (from the root of this repository):
$ make -C mpy-cross
Then to build MicroPython for the ESP32 run:
$ cd ports/esp32
$ make submodules
$ make
This will produce a combined firmware.bin
image in the build-GENERIC/
subdirectory (this firmware image is made up of: bootloader.bin, partitions.bin
and micropython.bin).
To flash the firmware you must have your ESP32 module in the bootloader
mode and connected to a serial port on your PC. Refer to the documentation
for your particular ESP32 module for how to do this.
You will also need to have user permissions to access the /dev/ttyUSB0
device.
On Linux, you can enable this by adding your user to the dialout
group, and
rebooting or logging out and in again. (Note: on some distributions this may
be the uucp
group, run ls -la /dev/ttyUSB0
to check.)
$ sudo adduser <username> dialout
If you are installing MicroPython to your module for the first time, or after installing any other firmware, you should first erase the flash completely:
$ make erase
To flash the MicroPython firmware to your ESP32 use:
$ make deploy
The default ESP32 board build by the above commands is the GENERIC
one, which
should work on most ESP32 modules. You can specify a different board by passing
BOARD=<board>
to the make commands, for example:
$ make BOARD=GENERIC_SPIRAM
Note: the above "make" commands are thin wrappers for the underlying idf.py
build tool that is part of the ESP-IDF. You can instead use idf.py
directly,
for example:
$ idf.py build
$ idf.py -D MICROPY_BOARD=GENERIC_SPIRAM build
$ idf.py flash
Getting a Python prompt on the device
You can get a prompt via the serial port, via UART0, which is the same UART that is used for programming the firmware. The baudrate for the REPL is 115200 and you can use a command such as:
$ picocom -b 115200 /dev/ttyUSB0
or
$ miniterm.py /dev/ttyUSB0 115200
You can also use idf.py monitor
.
Configuring the WiFi and using the board
The ESP32 port is designed to be (almost) equivalent to the ESP8266 in terms of the modules and user-facing API. There are some small differences, notably that the ESP32 does not automatically connect to the last access point when booting up. But for the most part the documentation and tutorials for the ESP8266 should apply to the ESP32 (at least for the components that are implemented).
See http://docs.micropython.org/en/latest/esp8266/esp8266/quickref.html for a quick reference, and http://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/intro.html for a tutorial.
The following function can be used to connect to a WiFi access point (you can
either pass in your own SSID and password, or change the defaults so you can
quickly call wlan_connect()
and it just works):
def wlan_connect(ssid='MYSSID', password='MYPASS'):
import network
wlan = network.WLAN(network.STA_IF)
if not wlan.active() or not wlan.isconnected():
wlan.active(True)
print('connecting to:', ssid)
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
Note that some boards require you to configure the WiFi antenna before using the WiFi. On Pycom boards like the LoPy and WiPy 2.0 you need to execute the following code to select the internal antenna (best to put this line in your boot.py file):
import machine
antenna = machine.Pin(16, machine.Pin.OUT, value=0)
Defining a custom ESP32 board
The default ESP-IDF configuration settings are provided by the GENERIC
board definition in the directory boards/GENERIC
. For a custom configuration
you can define your own board directory. Start a new board configuration by
copying an existing one (like GENERIC
) and modifying it to suit your board.
MicroPython specific configuration values are defined in the board-specific
mpconfigboard.h
file, which is included by mpconfigport.h
. Additional
settings are put in mpconfigboard.cmake
, including a list of sdkconfig
files that configure ESP-IDF settings. Some standard sdkconfig
files are
provided in the boards/
directory, like boards/sdkconfig.ble
. You can
also define custom ones in your board directory.
See existing board definitions for further examples of configuration.
Configuration Troubleshooting
- Continuous reboots after programming: Ensure
CONFIG_ESPTOOLPY_FLASHMODE
is correct for your board (e.g. ESP-WROOM-32 should be DIO). Then perform amake clean
, rebuild, redeploy.