Skip to content
This repository has been archived by the owner on Jul 20, 2022. It is now read-only.

Commit

Permalink
Change Baud Rate fixes (#16)
Browse files Browse the repository at this point in the history
Fix to send ESP_CHANGE_BAUDRATE on the instance not the parent, so that status bytes received match the STUB or the ROM.
  • Loading branch information
conradopoole authored Jun 21, 2021
1 parent cebbd59 commit 251ff35
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/esp_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
ESP_ERASE_FLASH,
CHIP_ERASE_TIMEOUT,
timeoutPerMb,
ESP_ROM_BAUD,
} from "./const";
import { getStubCode } from "./stubs";
import { pack, sleep, slipEncode, toHex, unpack } from "./util";
Expand Down Expand Up @@ -450,20 +451,15 @@ export class ESPLoader extends EventTarget {
}

async setBaudrate(baud: number) {
if (this._parent) {
await this._parent.setBaudrate(baud);
return;
}

if (this.chipFamily == CHIP_FAMILY_ESP8266) {
throw new Error("Changing baud rate is not supported on the ESP8266");
}

this.logger.log("Attempting to change baud rate to " + baud + "...");

try {
// Send 115200 as the old one, otherwise the STUB seems to not work properly after changing the baud rate.
let buffer = pack("<II", baud, 115200);
// Send ESP_ROM_BAUD(115200) as the old one if running STUB otherwise 0
let buffer = pack("<II", baud, this.IS_STUB ? ESP_ROM_BAUD : 0);
await this.checkCommand(ESP_CHANGE_BAUDRATE, buffer);
} catch (e) {
console.error(e);
Expand All @@ -472,6 +468,14 @@ export class ESPLoader extends EventTarget {
);
}

if (this._parent) {
await this._parent.reconfigurePort(baud);
} else {
await this.reconfigurePort(baud);
}
}

async reconfigurePort(baud: number) {
try {
// SerialPort does not allow to be reconfigured while open so we close and re-open
this.stopReadLoop = true;
Expand Down

0 comments on commit 251ff35

Please sign in to comment.