diff --git a/ESPLoader.js b/ESPLoader.js
index 5ab55a2..12a2d22 100644
--- a/ESPLoader.js
+++ b/ESPLoader.js
@@ -602,6 +602,7 @@ class ESPLoader {
ESP_SPI_FLASH_MD5 = 0x13;
ESP_READ_REG = 0x0A;
ESP_SPI_ATTACH = 0x0D;
+ ESP_CHANGE_BAUDRATE = 0x0F;
// Only Stub supported commands
ESP_ERASE_FLASH = 0xD0;
@@ -620,8 +621,9 @@ class ESPLoader {
DETECTED_FLASH_SIZES = {0x12: '256KB', 0x13: '512KB', 0x14: '1MB', 0x15: '2MB', 0x16: '4MB', 0x17: '8MB', 0x18: '16MB'};
- constructor(transport, terminal) {
+ constructor(transport, baudrate, terminal) {
this.transport = transport;
+ this.baudrate = baudrate;
this.terminal = terminal;
this.IS_STUB = false;
this.chip = null;
@@ -750,11 +752,8 @@ class ESPLoader {
read_reg = async({addr, timeout = 3000} = {}) => {
var val, data;
- console.log("read reg " + addr + " " + timeout);
var pkt = this._int_to_bytearray(addr);
val = await this.command({op:this.ESP_READ_REG, data:pkt, timeout:timeout});
- console.log("Read reg resp");
- console.log(val);
return val[0];
}
@@ -1222,6 +1221,22 @@ class ESPLoader {
}
}
+ change_baud = async() => {
+ this.log("Changing baudrate to " + this.baudrate);
+ console.log("Changing baudrate to " + this.baudrate);
+ let second_arg = this.IS_STUB ? this.transport.baudrate : 0;
+ let pkt = this._appendArray(this._int_to_bytearray(this.baudrate), this._int_to_bytearray(second_arg));
+ let resp = await this.command({op:this.ESP_CHANGE_BAUDRATE, data:pkt});
+ this.log("Changed");
+ await this.transport.disconnect();
+ await this._sleep(50);
+ await this.transport.connect({baud:this.baudrate});
+ try {
+ await this.transport.rawRead({timeout:500});
+ } catch (e) {
+ }
+ }
+
main_fn = async () => {
await this.detect_chip();
if (this.chip == null) {
@@ -1238,6 +1253,8 @@ class ESPLoader {
await this.run_stub();
+ await this.change_baud();
+
}
flash_size_bytes = function(flash_size) {
diff --git a/index.html b/index.html
index 0f5785e..3118f90 100644
--- a/index.html
+++ b/index.html
@@ -19,6 +19,13 @@
Program
+
+
diff --git a/index.js b/index.js
index e353e29..a99aea5 100644
--- a/index.js
+++ b/index.js
@@ -1,3 +1,4 @@
+const baudrates = document.getElementById("baudrates");
const connectButton = document.getElementById("connectButton");
const disconnectButton = document.getElementById("disconnectButton");
const resetButton = document.getElementById("resetButton");
@@ -109,18 +110,16 @@ connectButton.onclick = async () => {
}
try {
- esploader = new ESPLoader(transport, term);
- connected = true;
+ esploader = new ESPLoader(transport, baudrates.value, term);
+ connected = true;
- await esploader.main_fn();
-
- console.log("Flash ID 1");
- await esploader.flash_id();
+ await esploader.main_fn();
+ await esploader.flash_id();
} catch(e) {
- console.log(e);
}
console.log("Settings done");
+ baudrates.style.display = "none";
connectButton.style.display = "none";
disconnectButton.style.display = "initial";
filesDiv.style.display = "initial";
@@ -151,6 +150,7 @@ disconnectButton.onclick = async () => {
await transport.disconnect();
term.clear();
connected = false;
+ baudrates.style.display = "initial";
connectButton.style.display = "initial";
disconnectButton.style.display = "none";
filesDiv.style.display = "none";
diff --git a/webserial.js b/webserial.js
index 6f5fd57..5c9be9e 100644
--- a/webserial.js
+++ b/webserial.js
@@ -146,17 +146,32 @@ class Transport {
}
}
- rawRead = async () => {
+ rawRead = async ({timeout=0} = {}) => {
let reader = this.device.readable.getReader();
let done = false;
let value = new Uint8Array(0);
this.reader = reader;
+ if (timeout > 0) {
+ t = setTimeout(function() {
+ reader.cancel();
+ reader.releaseLock();
+ }, timeout);
+ }
+
let o = await reader.read();
this.reader = null;
done = o.done;
- reader.releaseLock();
- return o.value;
+
+ if (done) {
+ throw("timeout");
+ } else {
+ if (timeout > 0) {
+ clearTimeout(t);
+ }
+ reader.releaseLock();
+ return o.value;
+ }
}
setRTS = async (state) => {
@@ -166,9 +181,9 @@ class Transport {
setDTR = async (state) => {
await this.device.setSignals({dataTerminalReady:state});
}
- connect = async () => {
- await this.device.open({baudRate: 115200});
- this.baudrate = 115200;
+ connect = async ({baud=115200} = {}) => {
+ await this.device.open({baudRate: baud});
+ this.baudrate = baud;
}
disconnect = async () => {
if (this.reader !== null) {