circuitpython/ports/esp32
robert-hh 61cb293b76 esp32/machine_pin: Implement Pin.toggle() method.
The actual output pin value is taken from the OUT register, not from the
pad.

Tested with:
- ESP32   low and high Pin numbers
- ESP32C3 low Pin numbers
- ESP32C6 low Pin numbers
- ESP32S2 low and high Pin numbers
- ESP32S3 low and high Pin numbers

Signed-off-by: robert-hh <robert@hammelrath.com>
2025-02-28 16:47:29 +11:00
..
boards extmod/modtls_mbedtls: Wire in support for DTLS. 2025-02-14 12:55:25 +11:00
main_esp32 esp32: Drop support for ESP-IDF below V5.2.0. 2024-12-10 23:21:46 +11:00
main_esp32c3 esp32: Drop support for ESP-IDF below V5.2.0. 2024-12-10 23:21:46 +11:00
main_esp32c6 esp32: Drop support for ESP-IDF below V5.2.0. 2024-12-10 23:21:46 +11:00
main_esp32s2 esp32: Drop support for ESP-IDF below V5.2.0. 2024-12-10 23:21:46 +11:00
main_esp32s3 esp32: Drop support for ESP-IDF below V5.2.0. 2024-12-10 23:21:46 +11:00
modules ports: Use vfs module instead of os. 2024-02-07 13:25:09 +11:00
tools esp32/tools: Add metrics_esp32 size comparison script. 2024-08-14 15:58:05 +10:00
.gitignore esp32: Update port to support IDF v5.0.2. 2023-06-23 15:34:22 +10:00
adc.c esp32: Use capability defines to configure features. 2024-12-10 21:47:05 +11:00
adc.h esp32/adc: Use new ADC calibration API in all cases. 2024-08-14 15:57:26 +10:00
CMakeLists.txt esp32: Remove IDF-version-specific sdkconfig. 2024-12-10 23:21:46 +11:00
esp32_common.cmake esp32/esp32_common.cmake: Allow overriding linker.lf. 2025-02-28 16:22:05 +11:00
esp32_nvs.c ports: Include py/mphal.h instead of mphalport.h. 2024-10-09 14:39:34 +11:00
esp32_partition.c ports: Include py/mphal.h instead of mphalport.h. 2024-10-09 14:39:34 +11:00
esp32_rmt.c ports: Include py/mphal.h instead of mphalport.h. 2024-10-09 14:39:34 +11:00
esp32_ulp.c all: Remove the "STATIC" macro and just use "static" instead. 2024-03-07 14:20:42 +11: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 support for esp32c6. 2024-10-09 23:29:38 +11:00
gccollect.h esp32: Add new port to Espressif ESP32 SoC. 2017-12-13 14:48:53 +11:00
help.c esp32: Use the recommended network.WLAN.IF_[AP|STA] constants. 2024-11-08 10:11:03 +11:00
lwip_patch.c esp32: Apply the LWIP active TCP socket limit. 2024-10-10 17:55:03 +11:00
machine_adc.c esp32: Use capability defines to configure features. 2024-12-10 21:47:05 +11:00
machine_adc_block.c esp32: Use capability defines to configure features. 2024-12-10 21:47:05 +11:00
machine_bitstream.c esp32: Use capability defines to configure features. 2024-12-10 21:47:05 +11:00
machine_dac.c esp32: Drop support for ESP-IDF below V5.2.0. 2024-12-10 23:21:46 +11:00
machine_hw_spi.c esp32/machine_hw_spi: Reject invalid number of bits in constructor. 2024-10-30 15:14:30 +11:00
machine_i2c.c esp32/machine_i2c: Make I2C bus ID arg optional with default. 2025-02-28 15:18:46 +11:00
machine_i2s.c esp32/machine_i2s: Ensure 2 DMA buffers and improve I2S error handling. 2024-08-02 10:30:14 +10:00
machine_pin.c esp32/machine_pin: Implement Pin.toggle() method. 2025-02-28 16:47:29 +11:00
machine_pin.h esp32: Add support for esp32c6. 2024-10-09 23:29:38 +11:00
machine_pwm.c esp32: Drop support for ESP-IDF below V5.2.0. 2024-12-10 23:21:46 +11:00
machine_rtc.c ports: Fix machine.RTC.init() method so argument order matches the docs. 2024-12-19 16:04:47 +11: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 esp32/machine_sdcard: Fix invalid result of SDCard.read/writeblocks. 2025-02-12 13:28:28 +11:00
machine_timer.c esp32/machine_timer: Restrict timer numbers for ESP32C6 to 0 and 1. 2024-12-21 00:37:49 +11:00
machine_timer.h esp32/machine_uart: Implement UART.RX_IDLE based on machine.Timer. 2024-08-29 16:34:33 +10:00
machine_touchpad.c esp32: Fix machine_touchpad compiling on IDFv5.3. 2024-12-10 11:20:15 +11:00
machine_uart.c esp32: Drop support for ESP-IDF below V5.2.0. 2024-12-10 23:21:46 +11:00
machine_wdt.c all: Remove the "STATIC" macro and just use "static" instead. 2024-03-07 14:20:42 +11:00
main.c esp32: Enable machine.USBDevice to configure USB at runtime. 2024-12-16 11:41:40 +11:00
Makefile py/mkrules: Add GIT_SUBMODULES_FAIL_IF_EMPTY flag for CMake ports. 2025-01-29 11:41:32 +11:00
makeimg.py esp32: Create .uf2 binaries for S2 and S3 chips. 2022-02-02 23:47:49 +11:00
modesp.c all: Remove the "STATIC" macro and just use "static" instead. 2024-03-07 14:20:42 +11:00
modesp32.c esp32: Drop support for ESP-IDF below V5.2.0. 2024-12-10 23:21:46 +11:00
modesp32.h esp32/modesp32: Properly define RTC_VALID_EXT_PINS on S2/S3 variants. 2022-04-14 16:17:52 +10:00
modespnow.c ports: Include py/mphal.h instead of mphalport.h. 2024-10-09 14:39:34 +11:00
modespnow.h esp32,esp8266: Add support for the Espressif ESP-NOW protocol. 2023-05-01 16:47:21 +10:00
modmachine.c esp32: Drop support for ESP-IDF below V5.2.0. 2024-12-10 23:21:46 +11:00
modmachine.h extmod/modmachine: Provide common Python bindings for bootloader(). 2023-11-30 16:11:11 +11:00
modnetwork.h esp32,mimxrt,stm32: Implement ipconfig() for more network interfaces. 2024-06-04 12:45:01 +10:00
modnetwork_globals.h esp32: Drop support for ESP-IDF below V5.2.0. 2024-12-10 23:21:46 +11:00
modos.c all: Remove the "STATIC" macro and just use "static" instead. 2024-03-07 14:20:42 +11:00
modsocket.c esp32/modsocket: Fix getaddrinfo hints to set AI_CANONNAME. 2024-11-13 13:35:37 +11:00
modtime.c all: Remove the "STATIC" macro and just use "static" instead. 2024-03-07 14:20:42 +11:00
mpconfigport.h esp32: Enable machine.USBDevice to configure USB at runtime. 2024-12-16 11:41:40 +11:00
mphalport.c esp32/mphalport: Always poll stdin ring-buffer to include UART use. 2024-10-16 14:50:11 +11:00
mphalport.h esp32/machine_pin: Implement Pin.toggle() method. 2025-02-28 16:47:29 +11:00
mpnimbleport.c esp32: Fix heap corruption triggered by bluetooth.active(0). 2024-08-01 11:01:05 +10:00
mpthreadport.c all: Upgrade codespell to v2.4.1. 2025-02-25 16:11:33 +11:00
mpthreadport.h esp32: Add new port to Espressif ESP32 SoC. 2017-12-13 14:48:53 +11:00
network_common.c esp32/network_wlan: Add missing WLAN security constants. 2024-11-05 11:34:35 +11:00
network_lan.c esp32: Drop support for ESP-IDF below V5.2.0. 2024-12-10 23:21:46 +11:00
network_ppp.c esp32: Add support for IDF v5.4. 2025-01-24 23:17:05 +11:00
network_wlan.c esp32: Add support for IDF v5.4. 2025-01-24 23:17:05 +11:00
panichandler.c esp32/panichandler: Print support information on panic. 2024-05-16 12:25:45 +10:00
partitions-2MiB.csv esp32/partitions-2MiB.csv: Update table so firmware fits. 2021-06-11 17:57:40 +10:00
partitions-4MiB-ota.csv esp32/partitions.csv: Rename to partitions-4MiB.csv. 2023-08-15 17:38:02 +10:00
partitions-4MiB.csv esp32/partitions.csv: Rename to partitions-4MiB.csv. 2023-08-15 17:38:02 +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-32MiB-ota.csv esp32: Add 32MiB partition table templates. 2022-11-09 10:57:44 +11:00
partitions-32MiB.csv esp32: Add 32MiB partition table templates. 2022-11-09 10:57:44 +11:00
ppp_set_auth.c esp32/network_ppp: Make PPP support optional. 2024-02-15 13:29:43 +11:00
ppp_set_auth.h esp32/ppp_set_auth: Add pppapi_set_auth from ESP-IDF. 2023-06-23 15:34:22 +10:00
qstrdefsport.h all: Add *FORMAT-OFF* in various places. 2020-02-28 10:31:07 +11:00
README.md esp32/README: Make some minor improvements to the README. 2025-02-28 16:13:55 +11:00
README.ulp.md all: Prune trailing whitespace. 2024-03-07 16:25:17 +11:00
uart.c esp32: Drop support for ESP-IDF below V5.2.0. 2024-12-10 23:21:46 +11:00
uart.h esp32: Add MICROPY_HW_USB_CDC macro for native USB-CDC serial. 2024-09-03 14:28:26 +10:00
usb.c esp32: Add automatic bootloader handling for S2 and S3. 2024-10-07 11:06:57 +11:00
usb.h esp32: Add automatic bootloader handling for S2 and S3. 2024-10-07 11:06:57 +11:00
usb_serial_jtag.c esp32: Add basic espressif IDF v5.3 compatibility. 2024-12-10 11:20:00 +11:00
usb_serial_jtag.h esp32: Poll serial/JTAG for unread data to prevent blocking. 2023-10-31 11:59:15 +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.

Currently supports ESP32, ESP32-C3, ESP32-C6, ESP32-S2 and ESP32-S3 (ESP8266 is supported by a separate MicroPython port).

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 5 (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 v5.2, v5.2.2, v5.3 and v5.4.

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 v5.2.2 --recursive https://github.com/espressif/esp-idf.git

You can replace v5.2.2 with 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 v5.2.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 esp32 # (or install.bat on Windows)
$ source export.sh   # (or export.bat on Windows)

The install.sh step only needs to be done once. Change esp32 if you are targeting other chip. Use comma-separated list like esp32,esp32s2 to install for multiple chips. Or omit the chip to install for all Espressif chips (which is slower).

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 ESP32 run:

$ cd ports/esp32
$ make submodules
$ make

This will produce a combined firmware.bin image in the build-ESP32_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 usermod -aG dialout <username>

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 ESP32_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=ESP32_GENERIC_S3

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=ESP32_GENERIC build
$ idf.py flash

Some boards also support "variants", which allow for small variations of an otherwise similar board. For example different flash sizes or features. For example to build the OTA variant of ESP32_GENERIC.

$ make BOARD=ESP32_GENERIC BOARD_VARIANT=OTA

or to enable octal-SPIRAM support for the ESP32_GENERIC_S3 board:

$ make BOARD=ESP32_GENERIC_S3 BOARD_VARIANT=SPIRAM_OCT

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 or mpremote.

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.WLAN.IF_STA)
    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 ESP32_GENERIC board definition in the directory boards/ESP32_GENERIC. For a custom configuration you can define your own board directory. Start a new board configuration by copying an existing one (like ESP32_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.

Deployment instructions usually invoke the boards/deploy.md file (for boards with a USB/Serial converter connection), or the boards/deploy_nativeusb.md file (for boards with only a native USB port connection). These files are formatted for each board using template strings found in the boards.json files. You can also include the common boards/deploy_flashmode.md file if you have a board which requires manual resetting via the RESET and BOOT buttons. Boards with unique flashing steps can include custom deploy.md file(s). Existing board.json files contain examples of all of these combinations.

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.