micropython/ports/esp32
Mark Grosen 12f9948151 esp32/machine_i2c: Fix clocks and timeouts for ESP32-C3, ESP32-S3.
Each SoC family has its own clocks and timings/timeouts. For I2C, the
default source clock is either APB (ESP32, ESP32-S2) or XTAL (ESP32-S3,
ESP32-C3) as shown in the datasheets.  Since
machine_i2c.c/machine_hw_i2c_init() uses the default clk_flags (0), the
alternate low-power clock source is never selected in ESP-IDF
i2c.c/i2c_param_config().  There is not an API in i2c.c to get the source
clock frequency, so a compile-time value is used based on SoC family.

Also, the maximum timeout is different across the SoC families, so use the
I2C_LL_MAX_TIMEOUT constant to eliminate the warning from
i2c_set_timeout().

With these changes, the following results were obtained.  The I2C SCL
frequencies were measured with a Saleae logic analyzer.

ESP32 (TTGO T Dislay)
    I2C(0, scl=22, sda=21, freq=101781)  Measured: 100KHz
    I2C(0, scl=22, sda=21, freq=430107)  Measured: 400KHz
    I2C(0, scl=22, sda=21, freq=1212121) Measured: 941KHz

ESP32-S3 (TTGO T-QT)
    I2C(0, scl=34, sda=33, freq=111111)  Measured: 107KHz
    I2C(0, scl=34, sda=33, freq=444444)  Measured: 400KHz
    I2C(0, scl=34, sda=33, freq=1111111) Measured: 842KHz

ESP32-C3 (XIAO ESP32C3)
    I2C(0, scl=7, sda=6, freq=107816)  Measured: 103KHz
    I2C(0, scl=7, sda=6, freq=444444)  Measured: 380KHz
    I2C(0, scl=7, sda=6, freq=1176470) Measured: 800KHz

(ESP32-S2 board was not available for testing.)
2022-10-28 19:06:41 +11:00
..
boards top: Replace upip with mip everywhere. 2022-09-30 17:34:03 +10:00
main esp32/mpconfigport: Enable BLE synchronous events and pairing/bonding. 2022-07-22 17:41:21 +10:00
modules esp32/modules: Create ffat partition object with block_size=512. 2022-02-22 00:37:55 +11:00
CMakeLists.txt esp32: Allow overriding cmake frozen manifest from the command line. 2022-04-04 23:49:10 +10:00
Makefile esp32/Makefile: Add link to build troubleshooting on failure. 2022-09-15 12:22:18 +10:00
README.md esp32/README.md: Update list of supported IDF versions. 2022-02-24 15:23:50 +11:00
README.ulp.md esp32: Add support for the esp32's ULP. 2018-05-01 16:19:37 +10:00
esp32_nvs.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
esp32_partition.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
esp32_rmt.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
esp32_ulp.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
fatfs_port.c all: Update to point to files in new shared/ directory. 2021-07-12 17:08:10 +10:00
gccollect.c esp32: Add initial support for ESP32C3 SoCs. 2021-07-18 23:58:24 +10: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 py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
machine_adc.h esp32/machine_adcblock: Add new machine.ADCBlock class and update ADC. 2022-01-21 22:55:24 +11:00
machine_adcblock.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
machine_adcblock.h esp32/machine_adcblock: Add new machine.ADCBlock class and update ADC. 2022-01-21 22:55:24 +11:00
machine_bitstream.c esp32/machine_bitstream: Reinstate bitstream bit-bang implementation. 2022-01-14 16:40:01 +11:00
machine_dac.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
machine_hw_spi.c esp32: Rename machine I2C and SPI types consistently across ports. 2022-10-22 12:56:10 +11:00
machine_i2c.c esp32/machine_i2c: Fix clocks and timeouts for ESP32-C3, ESP32-S3. 2022-10-28 19:06:41 +11:00
machine_i2s.c esp32/machine_i2s: Add I2S finaliser which calls deinit(). 2022-10-04 11:47:06 +11:00
machine_pin.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
machine_pwm.c esp32/machine_pwm: Don't use LEDC_USE_REF_TICK on ESP32-C3 variants. 2022-09-26 00:11:42 +10:00
machine_rtc.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
machine_rtc.h esp32/modesp32: Add wake_on_ulp() so ULP can wake CPU from deepsleep. 2022-06-23 17:31:01 +10:00
machine_sdcard.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
machine_timer.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
machine_touchpad.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
machine_uart.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
machine_wdt.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
main.c esp32: Use MP_REGISTER_ROOT_POINTER(). 2022-07-18 13:50:34 +10:00
makeimg.py esp32: Create .uf2 binaries for S2 and S3 chips. 2022-02-02 23:47:49 +11:00
memory.h ports: Update for move of crypto-algorithms, uzlib to lib. 2021-07-12 16:37:20 +10:00
modesp.c all: Remove third argument to MP_REGISTER_MODULE. 2022-06-02 16:31:37 +10:00
modesp32.c esp32/modesp32: Add wake_on_ulp() so ULP can wake CPU from deepsleep. 2022-06-23 17:31:01 +10:00
modesp32.h esp32/modesp32: Properly define RTC_VALID_EXT_PINS on S2/S3 variants. 2022-04-14 16:17:52 +10:00
modmachine.c esp32: Rename machine I2C and SPI types consistently across ports. 2022-10-22 12:56:10 +11:00
modmachine.h esp32: Rename machine I2C and SPI types consistently across ports. 2022-10-22 12:56:10 +11:00
modnetwork.c esp32/modnetwork: Add network.MODE_LR constant. 2022-08-23 16:33:03 +10:00
modnetwork.h esp32: Split out WLAN code from modnetwork.c to network_wlan.c. 2021-09-24 12:41:35 +10:00
modsocket.c esp32/modsocket: Use mp_obj_is_integer to test port type. 2022-09-26 00:07:58 +10:00
moduos.c py/objstr: Split mp_obj_str_from_vstr into bytes/str versions. 2022-08-26 16:43:55 +10:00
modutime.c all: Remove third argument to MP_REGISTER_MODULE. 2022-06-02 16:31:37 +10:00
mpconfigport.h ports: Remove unused mp_type_{fileio/textio} macros in mpconfigport.h. 2022-07-26 18:07:22 +10:00
mphalport.c esp32/mphalport: Fix calculation of large sleep by using 64-bit arith. 2022-09-14 14:03:37 +10:00
mphalport.h esp32: Pin MicroPython to core 1 again. 2022-01-22 00:10:16 +11:00
mpnimbleport.c extmod/modbluetooth: Refactor stack/hci/driver/port bindings. 2020-09-08 11:41:31 +10:00
mpthreadport.c ports: Rename thread_t to mp_thread_t. 2022-05-24 00:51:23 +10:00
mpthreadport.h esp32: Add new port to Espressif ESP32 SoC. 2017-12-13 14:48:53 +11:00
network_lan.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
network_ppp.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
network_wlan.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
partitions-2MiB.csv esp32/partitions-2MiB.csv: Update table so firmware fits. 2021-06-11 17:57:40 +10:00
partitions-8MiB.csv esp32: Add support for ESP32-S3 SoCs. 2021-09-16 22:58:47 +10:00
partitions-16MiB-ota.csv esp32/boards: Add Silicognition wESP32 board configuration. 2021-08-07 12:50:13 +10:00
partitions-16MiB.csv esp32/partitions-16MiB.csv: Increase 14MiB filesystem to maximum size. 2022-02-08 12:25:10 +11:00
partitions-ota.csv esp32/makeimg.py: Load sizes from partition table and verify data fits. 2021-06-11 15:43:49 +10:00
partitions.csv esp32/partitions: Increase size of app-part from 0x180000 to 0x1F0000. 2022-01-27 16:44:13 +11:00
qstrdefsport.h all: Add *FORMAT-OFF* in various places. 2020-02-28 10:31:07 +11:00
uart.c esp32/machine_uart: Allow limited configuration of REPL UART. 2022-03-01 18:16:50 +11:00
uart.h esp32: Add MICROPY_HW_ENABLE_UART_REPL and enable on generic S2/S3. 2022-04-14 14:39:20 +10:00
usb.c esp32/usb: Further improve speed of USB CDC output. 2021-10-28 11:37:35 +11:00
usb.h esp32: Add support for USB with CDC ACM. 2021-04-15 10:31:06 +10:00
usb_serial_jtag.c esp32/boards: Add GENERIC_C3_USB board with USB serial/JTAG support. 2021-08-31 00:12:41 +10:00
usb_serial_jtag.h esp32/boards: Add GENERIC_C3_USB board with USB serial/JTAG support. 2021-08-31 00:12:41 +10:00

README.md

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, v4.2.2, v4.3.2 and v4.4, 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.2.2 or v4.4 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 v4.4 or later.
  • 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 a make clean, rebuild, redeploy.