circuitpython/ports/esp32
Damien George f12462ddc4 esp32: Remove obsolete IDF v3 code wrapped in MICROPY_ESP_IDF_4.
Signed-off-by: Damien George <damien@micropython.org>
2021-02-15 23:22:05 +11:00
..
boards esp32/boards: Enable BLE on all boards. 2021-02-15 16:40:07 +11:00
main esp32: Add support to build with ESP-IDF v4.2. 2021-02-15 23:12:20 +11:00
modules esp32,esp8266: Remove "FAT" from warning message in inisetup.py. 2020-11-18 16:26:19 +11:00
CMakeLists.txt esp32: Add support to build using IDF with cmake. 2021-02-15 12:48:39 +11:00
esp32_partition.c esp32: Improve support for OTA updates. 2020-05-03 15:00:45 +10:00
esp32_rmt.c esp32/esp32_rmt: Don't do unnecessary check for unsigned less than zero. 2021-02-15 16:37:52 +11:00
esp32_ulp.c esp32: Remove obsolete IDF v3 code wrapped in MICROPY_ESP_IDF_4. 2021-02-15 23:22:05 +11:00
espneopixel.c esp32/espneopixel: Use integer arithmetic to compute timing values. 2020-04-09 16:23:34 +10:00
fatfs_port.c esp32: Use path relative to root for netutils/timeutils headers. 2020-10-06 12:32:20 +11:00
gccollect.c all: Reformat C and Python source code with tools/codeformat.py. 2020-02-28 10:33:03 +11:00
gccollect.h esp32: Add new port to Espressif ESP32 SoC. 2017-12-13 14:48:53 +11:00
help.c all: Reformat C and Python source code with tools/codeformat.py. 2020-02-28 10:33:03 +11:00
machine_adc.c all: Clean up error strings to use lowercase and change cannot to can't. 2020-04-13 22:19:37 +10:00
machine_dac.c all: Clean up error strings to use lowercase and change cannot to can't. 2020-04-13 22:19:37 +10:00
machine_hw_spi.c ports: Support legacy soft I2C/SPI construction via id=-1 arg. 2020-10-01 12:57:10 +10:00
machine_i2c.c esp32: Add explicit initialisers to silence compiler warnings. 2021-02-15 16:37:58 +11:00
machine_pin.c esp32/machine_pin: Reset pin if init sets mode. 2020-11-12 15:27:41 +11:00
machine_pwm.c all: Clean up error strings to use lowercase and change cannot to can't. 2020-04-13 22:19:37 +10:00
machine_rtc.c esp32: Use path relative to root for netutils/timeutils headers. 2020-10-06 12:32:20 +11:00
machine_rtc.h all: Reformat C and Python source code with tools/codeformat.py. 2020-02-28 10:33:03 +11:00
machine_sdcard.c esp32/machine_sdcard: Add "freq" keyword arg to SDCard constructor. 2020-05-28 12:19:04 +10:00
machine_timer.c esp32: Consolidate check_esp_err functions and add IDF error string. 2020-04-23 10:59:07 +10:00
machine_touchpad.c all: Use MP_ERROR_TEXT for all error messages. 2020-04-05 15:02:06 +10:00
machine_uart.c esp32: Add support to build with ESP-IDF v4.1.1. 2021-02-15 23:11:26 +11:00
machine_wdt.c all: Use MP_ERROR_TEXT for all error messages. 2020-04-05 15:02:06 +10:00
main.c esp32: Remove obsolete IDF v3 code wrapped in MICROPY_ESP_IDF_4. 2021-02-15 23:22:05 +11:00
Makefile esp32: Remove traditional "make" capability. 2021-02-15 16:37:58 +11:00
makeimg.py all: Reformat C and Python source code with tools/codeformat.py. 2020-02-28 10:33:03 +11:00
memory.h esp32: Add new port to Espressif ESP32 SoC. 2017-12-13 14:48:53 +11:00
modesp32.c esp32: Use path relative to root for netutils/timeutils headers. 2020-10-06 12:32:20 +11:00
modesp32.h all: Reformat C and Python source code with tools/codeformat.py. 2020-02-28 10:33:03 +11:00
modesp.c esp32: Remove obsolete IDF v3 code wrapped in MICROPY_ESP_IDF_4. 2021-02-15 23:22:05 +11:00
modesp.h esp32: Add new port to Espressif ESP32 SoC. 2017-12-13 14:48:53 +11:00
modmachine.c esp32: Remove obsolete IDF v3 code wrapped in MICROPY_ESP_IDF_4. 2021-02-15 23:22:05 +11:00
modmachine.h ports: Add SoftI2C and SoftSPI to machine module where appropriate. 2020-10-01 12:57:10 +10:00
modnetwork.c esp32: Add support to build with ESP-IDF v4.1.1. 2021-02-15 23:11:26 +11:00
modnetwork.h esp32/network_lan: Add support for IP101 PHY. 2020-07-21 00:59:47 +10:00
modsocket.c esp32: Remove obsolete IDF v3 code wrapped in MICROPY_ESP_IDF_4. 2021-02-15 23:22:05 +11:00
moduos.c all: Reformat C and Python source code with tools/codeformat.py. 2020-02-28 10:33:03 +11:00
modutime.c extmod/utime_mphal: Add generic utime.time_ns() function. 2020-10-01 14:20:42 +10:00
mpconfigport.h esp32: Remove obsolete IDF v3 code wrapped in MICROPY_ESP_IDF_4. 2021-02-15 23:22:05 +11:00
mphalport.c esp32: Remove obsolete IDF v3 code wrapped in MICROPY_ESP_IDF_4. 2021-02-15 23:22:05 +11:00
mphalport.h esp32: Pin MicroPython and NimBLE tasks to core 0. 2020-09-08 12:53:24 +10:00
mpnimbleport.c extmod/modbluetooth: Refactor stack/hci/driver/port bindings. 2020-09-08 11:41:31 +10:00
mpthreadport.c esp32: Remove obsolete IDF v3 code wrapped in MICROPY_ESP_IDF_4. 2021-02-15 23:22:05 +11:00
mpthreadport.h esp32: Add new port to Espressif ESP32 SoC. 2017-12-13 14:48:53 +11:00
network_lan.c esp32/network_lan: Add support for IP101 PHY. 2020-07-21 00:59:47 +10:00
network_ppp.c esp32: Use path relative to root for netutils/timeutils headers. 2020-10-06 12:32:20 +11:00
partitions-2MiB.csv esp32/partitions: Update comments in files regarding offset. 2020-05-03 15:01:26 +10:00
partitions-ota.csv esp32/partitions: Update comments in files regarding offset. 2020-05-03 15:01:26 +10:00
partitions.csv esp32/partitions: Update comments in files regarding offset. 2020-05-03 15:01:26 +10:00
qstrdefsport.h all: Add *FORMAT-OFF* in various places. 2020-02-28 10:31:07 +11:00
README.md esp32: Add support to build with ESP-IDF v4.2. 2021-02-15 23:12:20 +11:00
README.ulp.md esp32: Add support for the esp32's ULP. 2018-05-01 16:19:37 +10:00
uart.c esp32: Add support to build with ESP-IDF v4.1.1. 2021-02-15 23:11:26 +11:00
uart.h esp32: Add new port to Espressif ESP32 SoC. 2017-12-13 14:48:53 +11:00

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 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.)

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.

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 a make clean, rebuild, redeploy.