Skip to content

senseshift/esptool.ts

 
 

Repository files navigation

esptool.ts — TypeScript port of esptool

This implementation is used in the SenseShift Web Flasher. Forked and modified from toitware/esptool.js.

Get involved: DiscordWebsiteIssuesTwitterPatreon

Discord Widget

Version Downloads per month

MIT GitHub contributors GitHub

Example

The code is written as a library. Here is an example showcasing how you can flash an ESP32 using the library:

export type Partition = {
  name: string;
  data: Uint8Array;
  offset: number;
};

// TODO: Here you have to specify the partitions you want to flash to the ESP32.
const partitions: Partition[] = [];

await port.open({ baudRate: 115200 });
try {
  const loader = new EspLoader(port, { debug: true, logger: console });
  options.logger.log("connecting...");
  await loader.connect();
  try {
    options.logger.log("connected");
    options.logger.log("writing device partitions");
    const chipName = await loader.chipName();
    const macAddr = await loader.macAddr();
    await loader.loadStub();
    await loader.setBaudRate(options.baudRate, 921600);

    if (options.erase) {
      options.logger.log("erasing device flash...");
      await loader.eraseFlash();
      options.logger.log("successfully erased device flash");
    }

    for (let i = 0; i < partitions.length; i++) {
      options.logger.log("\nWriting partition: " + partitions[i].name);
      await loader.flashData(partitions[i].data, partitions[i].offset, function (idx, cnt) {
        if (options.progressCallback) {
          options.progressCallback(partitions[i].name, idx, cnt);
        }
      });
      await sleep(100);
    }
    options.logger.log("successfully written device partitions");
    options.logger.log("flashing succeeded");
  } finally {
    await loader.disconnect();
  }
} finally {
  await port.close();
}

Similar projects

  • Toit esptool.js: original library, this one was forked and modified from.
  • Espressif esptool-js: new project with similar purpose, but still not written as a proper library.
  • Adafruit Adafruit ESPTool: the source of inspration for us, big shout-out thanks for spearheading the effort. The implementation is written as an application and hard to use as a library.

Packages

No packages published

Languages

  • TypeScript 99.5%
  • Other 0.5%