Esp32

A library for managing ESP32 firmware on Nerves and other Elixir systems. This package implements the ESP32 serial bootloader protocol and handles hardware reset and strapping pins to automate entry into bootloader mode.

Features

Usage

To use Esp32, you need to provide the UART port and reset/boot options. For many development boards, you can use the automatic reset feature via DTR/RTS.

# Automatic discovery and reset (common for devboards via DTR/RTS)
{:ok, uart} = Esp32.connect("auto", auto_reset: true, baud_rate: 921600)

# Detect the chip family
{:ok, chip} = Esp32.detect_chip(uart)
IO.puts("Connected to: #{chip}")

# Flash a firmware file (patches header metadata if written to bootloader offset)
:ok = Esp32.flash_file(uart, "firmware.bin", 0x10000, reboot: true)

# Flash a raw binary blob
binary_data = <<...>>
:ok = Esp32.flash(uart, binary_data, 0x8000)

# Erase the entire flash chip
:ok = Esp32.erase(uart)

# Close the connection when done
Esp32.UART.close(uart)

Connection Options

When calling Esp32.connect/2, you can specify several options:

Common Firmware Offsets

When flashing your device, ensure you use the correct memory offsets. These offsets vary depending on the chip family:

Chip Family Bootloader Partition Table Application
ESP320x10000x80000x10000
ESP32-S20x10000x80000x10000
ESP32-S30x00x80000x10000
ESP32-C20x00x80000x10000
ESP32-C30x00x80000x10000
ESP32-C60x00x80000x10000

Note: For chips with a 0x0 bootloader offset, the library automatically handles header patching if you use flash_file/4 at that address.

Installation

Add esp32 to your list of dependencies in mix.exs:

def deps do
  [
    {:esp32, "~> 0.1.0"},
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/esp32.

License

MIT License. The ESP32 stub firmware binaries found in priv/stubs are from the esptool project and are licensed separately (GPL-2.0). Please see the source repository for more details