Esp32 (esp32 v0.2.0)

Copy Markdown View Source

Serial bootloader client for ESP32-family chips, for Elixir and Nerves.

{:ok, esp} = Esp32.connect("/dev/ttyUSB0", baud_rate: 921_600)
:ok = Esp32.flash_file(esp, "firmware.bin", 0x10000)
:ok = Esp32.close(esp)

Summary

Functions

Closes the serial port.

Opens port, resets the chip into its bootloader, and prepares it for flashing.

Erases the whole flash chip. Requires the flasher stub; may take up to two minutes.

Finds the first serial port backed by an Espressif chip or a common USB-serial bridge.

Writes binary to flash at offset.

Reads path and writes it with flash/4.

Reads a 32-bit register.

Hard-resets the chip so it boots normally. Reconnect before sending further commands.

Writes a 32-bit register.

Functions

close(device)

@spec close(Esp32.Device.t()) :: :ok

Closes the serial port.

connect(port, opts \\ [])

@spec connect(
  String.t() | :auto,
  keyword()
) :: {:ok, Esp32.Device.t()} | {:error, term()}

Opens port, resets the chip into its bootloader, and prepares it for flashing.

port is a serial device name, or :auto to use find_port/0.

Options:

  • :baud_rate - speed used after connecting (default 115200)
  • :initial_baud_rate - speed used to connect and load the stub (default 115200)
  • :use_stub - load the flasher stub (default true)
  • :reset - reset the chip into the bootloader (default true)
  • :reset_pin, :boot_pin - Circuits.GPIO pins wired to EN and IO0; without them DTR/RTS are used
  • :connect_attempts - reset and sync attempts (default 7)

The chosen reset strategy is kept on the device for reset/1.

erase(device)

@spec erase(Esp32.Device.t()) :: :ok | {:error, term()}

Erases the whole flash chip. Requires the flasher stub; may take up to two minutes.

find_port()

@spec find_port() :: {:ok, String.t()} | {:error, :no_port_found}

Finds the first serial port backed by an Espressif chip or a common USB-serial bridge.

flash(device, binary, offset, opts \\ [])

@spec flash(Esp32.Device.t(), binary(), non_neg_integer(), keyword()) ::
  :ok | {:error, term()}

Writes binary to flash at offset.

Options:

  • :flash_mode, :flash_freq, :flash_size - rewrite the header of a bootloader image, see Esp32.Image.patch_header/3 (default :keep); :flash_size also tells the loader the chip size, which the ROM loader otherwise assumes is 2 MB
  • :verify - compare the flash MD5 afterwards (default true)
  • :reboot - hard-reset the chip into the application when done, see reset/1 (default false)

Images built for a different chip are refused; other data is written as is.

flash_file(device, path, offset, opts \\ [])

@spec flash_file(Esp32.Device.t(), Path.t(), non_neg_integer(), keyword()) ::
  :ok | {:error, term()}

Reads path and writes it with flash/4.

read_reg(device, address)

@spec read_reg(Esp32.Device.t(), non_neg_integer()) ::
  {:ok, non_neg_integer()} | {:error, term()}

Reads a 32-bit register.

reset(device)

@spec reset(Esp32.Device.t()) :: :ok | {:error, term()}

Hard-resets the chip so it boots normally. Reconnect before sending further commands.

write_reg(device, address, value)

@spec write_reg(Esp32.Device.t(), non_neg_integer(), non_neg_integer()) ::
  :ok | {:error, term()}

Writes a 32-bit register.