Skip to content

Python library

Like esptool, every command is also a plain function. Each takes a State, which holds the serial connection and the global options, plus the same arguments as the CLI command. Reuse one State to run several operations over a single connection.

from idftool import State, ota, get_boot, set_nvs

state = State(
    port="/dev/cu.usbmodem1101", baud=115200, no_reset=False,
    partition_table_file=None, partition_table_offset=0x8000, partition_table_size=0x1000,
    primary_bootloader_offset=None, recovery_bootloader_offset=None,
)

ota(state, "build/app.bin")
get_boot(state)

state.esp.hard_reset()  # the CLI does this after each command; here it's up to you

State also takes mac= and probe=, which behave like -m and --probe.

When you already hold a connection, State.attached(esp) drives it instead of opening one, and never resets the chip:

from idftool import State, write_bundle

write_bundle(State.attached(esp), "reflash.zip")
esp.hard_reset()  # boot what was written, when you're ready

Names are imported lazily, so import idftool has no side effects.

Write options

The write options are keyword arguments, with the flag's name in snake case:

from idftool import write_image, write_nvs

write_image(state, "flash.img", erase=False, diff=True)
write_nvs(state, "nvs", "provision.csv", no_progress=True)

esptool options that take data rather than a yes/no are available here only: diff_with and no_diff_verify (esptool's fast reflashing), encrypt_files, and erase_all.

An unknown option raises TypeError rather than being ignored. esptool reads its options with kwargs.get, so a misspelled one would otherwise be a write that quietly did something else.

Output

A State from State.attached is quiet. It does not print the tables commands show on the way (the partition table, a filesystem's listing), nor read what only they show: otadata and each app's description. What the writes have to say goes through esptool's logger, so a logger of your own receives idftool's lines along with esptool's, and nothing reaches stdout:

from esptool.logger import log

log.set_logger(MyLogger())  # an esptool.logger.TemplateLogger

State(quiet=True) does the same for a State that makes its own connection.

Choosing a device

idftool.ports.select_device() shows the picker and returns the chosen device as a dict with port, chip, mac, and adapter. It returns None when there's no terminal to ask on.

Pass identify to name devices your own way. It's called with a connected ESPLoader for each port, and whatever it returns is shown in place of the chip name:

from idftool.ports import select_device

def identify(esp):
    return read_board_name(esp)  # e.g. from a read-only NVS partition

found = select_device(115200, identify=identify, message="Select a controller")
state.port = found["port"]

identify needs a connection, so every port is probed (and reset) when it's given, unless probe=False: then it runs only on ports probed with p. If it raises, the device is still listed and usable, with the error next to it.

State(identify=...) uses it in the picker the same way as the CLI uses plugins.

To find ports without asking, idftool.ports.esp_ports() lists the ones an ESP board may be on: those on a USB-serial adapter from ESP_ADAPTER_VIDS, the vendors idftool names in the picker, or every port when none is. Pass it a list of pyserial ports to filter your own selection instead:

from idftool.ports import esp_ports

ports = [p.device for p in esp_ports()]

Available functions

Group Functions
Discovery list_devices, monitor, enter_bootloader
Firmware factory, ota, get_boot, set_boot, clear_boot
Partition I/O read_partition, write_partitions, erase_partition, view_partition
Images create_image, dump_image, write_image, print_image
Bundles create_bundle, dump_bundle, write_bundle, print_bundle
Partition table print_table, create_table, dump_table, write_table
NVS create_nvs, write_nvs, print_nvs, extract_nvs, read_nvs, get_nvs, set_nvs
Filesystems create_fs, write_fs, read_fs, extract_fs, print_fs