Class ESPLoader

Constructors

  • Create a new ESPLoader to perform serial communication such as read/write flash memory and registers using a LoaderOptions object.

    Parameters

    • options: LoaderOptions

      LoaderOptions object argument for ESPLoader.

      const myLoader = new ESPLoader({ transport: Transport, baudrate: number, terminal?: IEspLoaderTerminal });
      

    Returns ESPLoader

Properties

CHIP_DETECT_MAGIC_REG_ADDR: number = 0x40001000
CHIP_ERASE_TIMEOUT: number = 120000
DETECTED_FLASH_SIZES: {
    [key: number]: string;
} = ...

Type declaration

  • [key: number]: string
DETECTED_FLASH_SIZES_NUM: {
    [key: number]: number;
} = ...

Type declaration

  • [key: number]: number
ERASE_REGION_TIMEOUT_PER_MB: number = 30000
ERASE_WRITE_TIMEOUT_PER_MB: number = 40000
ESP_CHANGE_BAUDRATE: number = 0x0f
ESP_CHECKSUM_MAGIC: number = 0xef
ESP_ERASE_FLASH: number = 0xd0
ESP_ERASE_REGION: number = 0xd1
ESP_FLASH_BEGIN: number = 0x02
ESP_FLASH_DATA: number = 0x03
ESP_FLASH_DEFL_BEGIN: number = 0x10
ESP_FLASH_DEFL_DATA: number = 0x11
ESP_FLASH_DEFL_END: number = 0x12
ESP_FLASH_END: number = 0x04
ESP_IMAGE_MAGIC: number = 0xe9
ESP_MEM_BEGIN: number = 0x05
ESP_MEM_DATA: number = 0x07
ESP_MEM_END: number = 0x06
ESP_RAM_BLOCK: number = 0x1800
ESP_READ_FLASH: number = 0xd2
ESP_READ_REG: number = 0x0a
ESP_RUN_USER_CODE: number = 0xd3
ESP_SPI_ATTACH: number = 0x0d
ESP_SPI_FLASH_MD5: number = 0x13
ESP_WRITE_REG: number = 0x09
FLASH_READ_TIMEOUT: number = 100000
FLASH_WRITE_SIZE: number
IS_STUB: boolean
MAX_TIMEOUT: number = ...
MD5_TIMEOUT_PER_MB: number = 8000
ROM_INVALID_RECV_MSG: number = 0x05
USB_JTAG_SERIAL_PID: number = 0x1001
baudrate: number
checksum: ((data) => number) = ...

Get the checksum for given unsigned 8-bit array

Type declaration

    • (data): number
    • Parameters

      • data: Uint8Array

        Unsigned 8-bit integer array

      Returns number

Returns

  • Array checksum
chip: ROM
debugLogging: boolean = false
flashSizeBytes: ((flashSize) => number) = ...

Get flash size bytes from flash size string.

Type declaration

    • (flashSize): number
    • Parameters

      • flashSize: string

        Flash Size string

      Returns number

Returns

Flash size bytes

romBaudrate: number = 115200
serialOptions?: SerialOptions
syncStubDetected: boolean = false
timeoutPerMb: ((secondsPerMb, sizeBytes) => number) = ...

Scale timeouts which are size-specific.

Type declaration

    • (secondsPerMb, sizeBytes): number
    • Parameters

      • secondsPerMb: number

        Seconds per megabytes as number

      • sizeBytes: number

        Size bytes number

      Returns number

Returns

  • Scaled timeout for specified size.
transport: Transport

Methods

  • Append a buffer array after another buffer array

    Parameters

    • arr1: Uint8Array

      First array buffer.

    • arr2: Uint8Array

      magic hex number to select ROM.

    Returns Uint8Array

    Return a 8 bit unsigned array.

  • Append a buffer array after another buffer array

    Parameters

    • buffer1: ArrayBuffer

      First array buffer.

    • buffer2: ArrayBuffer

      magic hex number to select ROM.

    Returns ArrayBufferLike

    Return an array buffer.

  • Convert a byte array to integer.

    Parameters

    • i: number

      Number to convert.

    • j: number

      Number to convert.

    • k: number

      Number to convert.

    • l: number

      Number to convert.

    Returns number

    Return a integer number.

  • Convert a byte array to short integer.

    Parameters

    • i: number

      Number to convert.

    • j: number

      Number to convert.

    Returns number

    Return a short integer number.

  • Attempt to connect to the chip by sending a reset sequence and later a sync command.

    Parameters

    • mode: string = "default_reset"

      Reset mode to use

    • esp32r0Delay: boolean = false

      Enable delay for ESP32 R0

    Returns Promise<"error" | "success">

    • Returns 'success' or 'error' message.
  • Convert an integer to byte array

    Parameters

    • i: number

      Number to convert.

    Returns Uint8Array

    The chip ROM class related to given magic hex number.

  • Convert short integer to byte array

    Parameters

    • i: number

      Number to convert.

    Returns Uint8Array

    Byte array.

  • Parameters

    • ms: number

    Returns Promise<unknown>

  • Update the image flash parameters with given arguments.

    Parameters

    • image: string

      binary image as string

    • address: number

      flash address number

    • flashSize: string

      Flash size string

    • flashMode: string

      Flash mode string

    • flashFreq: string

      Flash frequency string

    Returns string

    modified image string

  • Convert a byte string to unsigned 8 bit integer array.

    Parameters

    • bStr: string

      binary string input

    Returns Uint8Array

    Return a 8 bit unsigned integer array.

  • Change the chip baudrate.

    Returns Promise<void>

  • Execute the command and check the command response.

    Parameters

    • opDescription: string = ""

      Command operation description.

    • op: null | number = null

      Command operation number

    • data: Uint8Array = ...

      Command value

    • chk: number = 0

      Checksum to use

    • timeout: number = 3000

      TImeout number in milliseconds (ms)

    Returns Promise<number | Uint8Array>

    Command result

  • Write a serial command to the chip

    Parameters

    • op: null | number = null

      Operation number

    • data: Uint8Array = ...

      Unsigned 8 bit array

    • chk: number = 0

      channel number

    • waitResponse: boolean = true

      wait for response ?

    • timeout: number = 3000

      timeout number in milliseconds

    Returns Promise<[number, Uint8Array]>

    Return a number and a 8 bit unsigned integer array.

  • Perform a connection to chip.

    Parameters

    • mode: string = "default_reset"

      Reset mode to use. Example: 'default_reset' | 'no_reset'

    • attempts: number = 7

      Number of connection attempts

    • detecting: boolean = false

      Detect the connected chip

    Returns Promise<void>

  • Write debug message to ESP Loader constructor's terminal with or without new line.

    Parameters

    • str: string

      String to write.

    • withNewline: boolean = true

      Add new line at the end ?

    Returns void

  • Connect and detect the existing chip.

    Parameters

    • mode: string = "default_reset"

      Reset mode to use for connection.

    Returns Promise<void>

  • Execute the erase flash command

    Returns Promise<number | Uint8Array>

    Erase flash command result

  • Write error message to ESP Loader constructor's terminal with or without new line.

    Parameters

    • str: string

      String to write.

    • withNewline: boolean = true

      Add new line at the end ?

    Returns void

  • Start downloading to Flash (performs an erase)

    Parameters

    • size: number

      Size to erase

    • offset: number

      Offset to erase

    Returns Promise<number>

    Number of blocks (of size self.FLASH_WRITE_SIZE) to write.

  • Write block to flash, retry if fail

    Parameters

    • data: Uint8Array

      Unsigned 8-bit array data.

    • seq: number

      Sequence number

    • timeout: number

      Timeout in milliseconds (ms)

    Returns Promise<void>

  • Start downloading compressed data to Flash (performs an erase)

    Parameters

    • size: number

      Write size

    • compsize: number

      Compressed size

    • offset: number

      Offset for write

    Returns Promise<number>

    Returns number of blocks (size self.FLASH_WRITE_SIZE) to write.

  • Write block to flash, send compressed, retry if fail

    Parameters

    • data: Uint8Array

      Unsigned int 8-bit array data to write

    • seq: number

      Sequence number

    • timeout: number

      Timeout in milliseconds (ms)

    Returns Promise<void>

  • Leave compressed flash mode and run/reboot

    Parameters

    • reboot: boolean = false

      Reboot after leaving flash mode ?

    Returns Promise<void>

  • Leave flash mode and run/reboot

    Parameters

    • reboot: boolean = false

      Reboot after leaving flash mode ?

    Returns Promise<void>

  • Read SPI flash manufacturer and device id.

    Returns Promise<void>

  • Calculate the MD5 Checksum command

    Parameters

    • addr: number

      Address number

    • size: number

      Package size

    Returns Promise<string>

    MD5 Checksum string

  • Configure SPI flash pins

    Parameters

    • hspiArg: number

      Argument for SPI attachment

    Returns Promise<void>

  • Flush the serial input by raw read with 200 ms timeout.

    Returns Promise<void>

  • Perform a chip hard reset by setting RTS to LOW and then HIGH.

    Returns Promise<void>

  • Write information message to ESP Loader constructor's terminal with or without new line.

    Parameters

    • str: string

      String to write.

    • withNewline: boolean = true

      Add new line at the end ?

    Returns void

  • Execute the main function of ESPLoader.

    Parameters

    • mode: string = "default_reset"

      Reset mode to use

    Returns Promise<string>

    chip ROM

  • Start downloading an application image to RAM

    Parameters

    • size: number

      Image size number

    • blocks: number

      Number of data blocks

    • blocksize: number

      Size of each data block

    • offset: number

      Image offset number

    Returns Promise<void>

  • Send a block of image to RAM

    Parameters

    • buffer: Uint8Array

      Unsigned 8-bit array

    • seq: number

      Sequence number

    Returns Promise<void>

  • Leave RAM download mode and run application

    Parameters

    • entrypoint: number

      Entrypoint number

    Returns Promise<void>

  • Parse a given flash size string to a number

    Parameters

    • flsz: string

      Flash size to request

    Returns number

    Flash size number

  • Parameters

    • addr: number
    • size: number
    • onPacketReceived: FlashReadCallback = null

    Returns Promise<Uint8Array>

  • Read flash id by executing the SPIFLASH_RDID flash command.

    Returns Promise<number>

    Register SPI_W0_REG value

  • Use the device serial port read function with given timeout to create a valid packet.

    Parameters

    • op: null | number = null

      Operation number

    • timeout: number = 3000

      timeout number in milliseconds

    Returns Promise<[number, Uint8Array]>

    valid response packet.

  • Read a register from chip.

    Parameters

    • addr: number

      Register address number

    • timeout: number = 3000

      Timeout in milliseconds (Default: 3000ms)

    Returns Promise<number>

    • Command number value
  • Run an arbitrary SPI flash command.

    This function uses the "USR_COMMAND" functionality in the ESP SPI hardware, rather than the precanned commands supported by hardware. So the value of spiflashCommand is an actual command byte, sent over the wire.

    After writing command byte, writes 'data' to MOSI and then reads back 'readBits' of reply on MISO. Result is a number.

    Parameters

    • spiflashCommand: number

      Command to execute in SPI

    • data: Uint8Array

      Data to send

    • readBits: number

      Number of bits to read

    Returns Promise<number>

    Register SPI_W0_REG value

  • Upload the flasher ROM bootloader (flasher stub) to the chip.

    Returns Promise<ROM>

    The Chip ROM

  • Soft reset the device chip. Soft reset with run user code is the closest.

    Returns Promise<void>

  • Sync chip by sending sync command.

    Returns Promise<[number, Uint8Array]>

    Command result

  • Convert a number or unsigned 8-bit array to hex string

    Parameters

    • buffer: number | Uint8Array

      Data to convert to hex string.

    Returns string

    A hex string

  • Convert a unsigned 8 bit integer array to byte string.

    Parameters

    • u8Array: Uint8Array

      magic hex number to select ROM.

    Returns string

    Return the equivalent string.

  • Write to ESP Loader constructor's terminal with or without new line.

    Parameters

    • str: string

      String to write.

    • withNewline: boolean = true

      Add new line at the end ?

    Returns void

  • Write set of file images into given address based on given FlashOptions object.

    Parameters

    • options: FlashOptions

      FlashOptions to configure how and what to write into flash.

    Returns Promise<void>

  • Write a number value to register address in chip.

    Parameters

    • addr: number

      Register address number

    • value: number

      Number value to write in register

    • mask: number = 0xffffffff

      Hex number for mask

    • delayUs: number = 0

      Delay number

    • delayAfterUs: number = 0

      Delay after previous delay

    Returns Promise<void>