diff --git a/src/esploader.ts b/src/esploader.ts index b30d718..569e287 100644 --- a/src/esploader.ts +++ b/src/esploader.ts @@ -84,6 +84,8 @@ export class ESPLoader { 0x18: "16MB", }; + USB_JTAG_SERIAL_PID = 0x1001; + chip: ROM; IS_STUB: boolean; FLASH_WRITE_SIZE: number; @@ -278,20 +280,40 @@ export class ESPLoader { async _connect_attempt(mode = "default_reset", esp32r0_delay = false) { this.log("_connect_attempt " + mode + " " + esp32r0_delay); if (mode !== "no_reset") { - await this.transport.setDTR(false); - await this.transport.setRTS(true); - await this._sleep(100); - if (esp32r0_delay) { - //await this._sleep(1200); - await this._sleep(2000); + if (this.transport.get_pid() === this.USB_JTAG_SERIAL_PID) { + // Custom reset sequence, which is required when the device + // is connecting via its USB-JTAG-Serial peripheral + await this.transport.setRTS(false); + await this.transport.setDTR(false); + await this._sleep(100); + + await this.transport.setDTR(true); + await this.transport.setRTS(false); + await this._sleep(100); + + await this.transport.setRTS(true); + await this.transport.setDTR(false); + await this.transport.setRTS(true); + + await this._sleep(100); + await this.transport.setRTS(false); + await this.transport.setDTR(false); + } else { + await this.transport.setDTR(false); + await this.transport.setRTS(true); + await this._sleep(100); + if (esp32r0_delay) { + //await this._sleep(1200); + await this._sleep(2000); + } + await this.transport.setDTR(true); + await this.transport.setRTS(false); + if (esp32r0_delay) { + //await this._sleep(400); + } + await this._sleep(50); + await this.transport.setDTR(false); } - await this.transport.setDTR(true); - await this.transport.setRTS(false); - if (esp32r0_delay) { - //await this._sleep(400); - } - await this._sleep(50); - await this.transport.setDTR(false); } let i = 0; let keepReading = true; @@ -999,22 +1021,22 @@ export class ESPLoader { } async hard_reset() { - this.transport.setRTS(true); // EN->LOW + await this.transport.setRTS(true); // EN->LOW await this._sleep(100); - this.transport.setRTS(false); + await this.transport.setRTS(false); } async soft_reset() { if (!this.IS_STUB) { // "run user code" is as close to a soft reset as we can do - this.flash_begin(0, 0); - this.flash_finish(false); + await this.flash_begin(0, 0); + await this.flash_finish(false); } else if (this.chip.CHIP_NAME != "ESP8266") { throw new ESPError("Soft resetting is currently only supported on ESP8266"); } else { // running user code from stub loader requires some hacks // in the stub loader - this.command(this.ESP_RUN_USER_CODE, undefined, undefined, false); + await this.command(this.ESP_RUN_USER_CODE, undefined, undefined, false); } } } diff --git a/src/webserial.ts b/src/webserial.ts index 26b17c4..c97e392 100644 --- a/src/webserial.ts +++ b/src/webserial.ts @@ -12,6 +12,10 @@ class Transport { : ""; } + get_pid() { + return this.device.getInfo().usbProductId; + } + slip_writer(data: Uint8Array) { let count_esc = 0; let i = 0, @@ -181,11 +185,18 @@ class Transport { } } + _DTR_state = false; async setRTS(state: boolean) { await this.device.setSignals({ requestToSend: state }); + // # Work-around for adapters on Windows using the usbser.sys driver: + // # generate a dummy change to DTR so that the set-control-line-state + // # request is sent with the updated RTS state and the same DTR state + // Referenced to esptool.py + await this.setDTR(this._DTR_state); } async setDTR(state: boolean) { + this._DTR_state = state; await this.device.setSignals({ dataTerminalReady: state }); }