Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add esp32c2 baud rate option #131

Merged
merged 8 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"source": "src/index.html",
"scripts": {
"genDocs": "cd ../.. && npm run genDocs && mkdir -p examples/typescript/dist && cp -r docs examples/typescript/dist && cd examples/typescript",
"dev": "npm run genDocs && parcel src/index.html",
"dev": "npm run clean && npm run genDocs && parcel src/index.html",
"build": "npm run clean && npm run genDocs && parcel build src/index.html --no-optimize --public-url ./",
"clean": "rimraf dist .parcel-cache",
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
9 changes: 9 additions & 0 deletions examples/typescript/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ <h3> Program </h3>
<option value="115200">115200</option>
</select>

<br><br>

<input class="btn btn-info btn-sm" type="button" id="connectButton" value="Connect" />
<input class="btn btn-info btn-sm" type="button" id="copyTraceButton" value="Copy Trace" />
<input class="btn btn-warning btn-sm" type="button" id="disconnectButton" value="Disconnect" />
Expand Down Expand Up @@ -68,6 +70,13 @@ <h3> Program </h3>
<div id="console">
<h3>Console </h3>
<label style="display:none" id="lblConsoleFor">Connected to device: </label>
<label for="consoleBaudrates" id="lblConsoleBaudrate">Baudrate:</label>
<select name="consoleBaudrates" id="consoleBaudrates">
<option value="115200">115200</option>
<option value="74880">74880</option>
</select>

<br><br>

<input class="btn btn-info" type="button" id="consoleStartButton" value="Start" />
<input class="btn btn-info" type="button" id="consoleStopButton" value="Stop" />
Expand Down
36 changes: 24 additions & 12 deletions examples/typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const baudrates = document.getElementById("baudrates") as HTMLSelectElement;
const consoleBaudrates = document.getElementById("consoleBaudrates") as HTMLSelectElement;
const connectButton = document.getElementById("connectButton") as HTMLButtonElement;
const traceButton = document.getElementById("copyTraceButton") as HTMLButtonElement;
const disconnectButton = document.getElementById("disconnectButton") as HTMLButtonElement;
Expand All @@ -13,6 +14,7 @@
const programDiv = document.getElementById("program");
const consoleDiv = document.getElementById("console");
const lblBaudrate = document.getElementById("lblBaudrate");
const lblConsoleBaudrate = document.getElementById("lblConsoleBaudrate");
const lblConsoleFor = document.getElementById("lblConsoleFor");
const lblConnTo = document.getElementById("lblConnTo");
const table = document.getElementById("fileTable") as HTMLTableElement;
Expand All @@ -38,6 +40,7 @@
traceButton.style.display = "none";
eraseButton.style.display = "none";
consoleStopButton.style.display = "none";
resetButton.style.display = "none";
filesDiv.style.display = "none";

/**
Expand Down Expand Up @@ -95,11 +98,11 @@
// Temporarily broken
// await esploader.flashId();
} catch (e) {
console.error(e);

Check warning on line 101 in examples/typescript/src/index.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
term.writeln(`Error: ${e.message}`);
}

console.log("Settings done for :" + chip);

Check warning on line 105 in examples/typescript/src/index.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
lblBaudrate.style.display = "none";
lblConnTo.innerHTML = "Connected to device: " + chip;
lblConnTo.style.display = "block";
Expand All @@ -119,14 +122,11 @@
};

resetButton.onclick = async () => {
if (device === null) {
device = await navigator.serial.requestPort({});
transport = new Transport(device, true);
if (transport) {
await transport.setDTR(false);
await new Promise((resolve) => setTimeout(resolve, 100));
await transport.setDTR(true);
}

await transport.setDTR(false);
await new Promise((resolve) => setTimeout(resolve, 100));
await transport.setDTR(true);
};

eraseButton.onclick = async () => {
Expand All @@ -134,7 +134,7 @@
try {
await esploader.eraseFlash();
} catch (e) {
console.error(e);

Check warning on line 137 in examples/typescript/src/index.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
term.writeln(`Error: ${e.message}`);
} finally {
eraseButton.disabled = false;
Expand Down Expand Up @@ -212,8 +212,10 @@
disconnectButton.onclick = async () => {
if (transport) await transport.disconnect();

term.clear();
term.reset();
lblBaudrate.style.display = "initial";
baudrates.style.display = "initial";
consoleBaudrates.style.display = "initial";
connectButton.style.display = "initial";
disconnectButton.style.display = "none";
traceButton.style.display = "none";
Expand All @@ -232,11 +234,14 @@
transport = new Transport(device, true);
}
lblConsoleFor.style.display = "block";
lblConsoleBaudrate.style.display = "none";
consoleBaudrates.style.display = "none";
consoleStartButton.style.display = "none";
consoleStopButton.style.display = "initial";
resetButton.style.display = "initial";
programDiv.style.display = "none";

await transport.connect();
await transport.connect(parseInt(consoleBaudrates.value));
isConsoleClosed = false;

while (true && !isConsoleClosed) {
Expand All @@ -247,17 +252,24 @@
break;
}
}
console.log("quitting console");

Check warning on line 255 in examples/typescript/src/index.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
};

consoleStopButton.onclick = async () => {
isConsoleClosed = true;
await transport.disconnect();
await transport.waitForUnlock(1500);
term.clear();
if (transport) {
await transport.disconnect();
await transport.waitForUnlock(1500);
}
term.reset();
lblConsoleBaudrate.style.display = "initial";
consoleBaudrates.style.display = "initial";
consoleStartButton.style.display = "initial";
consoleStopButton.style.display = "none";
resetButton.style.display = "none";
lblConsoleFor.style.display = "none";
programDiv.style.display = "initial";
cleanUp();
};

/**
Expand Down Expand Up @@ -339,7 +351,7 @@
} as FlashOptions;
await esploader.writeFlash(flashOptions);
} catch (e) {
console.error(e);

Check warning on line 354 in examples/typescript/src/index.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
term.writeln(`Error: ${e.message}`);
} finally {
// Hide progress bars and show erase buttons
Expand Down
31 changes: 20 additions & 11 deletions src/webserial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Transport {
public baudrate = 0;
private traceLog = "";
private lastTraceTime = Date.now();
private reader: ReadableStreamDefaultReader<Uint8Array> | undefined;

constructor(public device: SerialPort, public tracing = false, enableSlipReader = true) {
this.slipReaderEnabled = enableSlipReader;
Expand Down Expand Up @@ -259,15 +260,17 @@ class Transport {
return this.leftOver;
}

const reader = this.device.readable.getReader();
this.reader = this.device.readable.getReader();
try {
if (timeout > 0) {
t = setTimeout(function () {
reader.cancel();
t = setTimeout(() => {
if (this.reader) {
this.reader.cancel();
}
}, timeout);
}
do {
const { value, done } = await reader.read();
const { value, done } = await this.reader.read();
if (done) {
this.leftOver = packet;
throw new Error("Timeout");
Expand All @@ -279,7 +282,7 @@ class Transport {
if (timeout > 0) {
clearTimeout(t);
}
reader.releaseLock();
this.reader.releaseLock();
}

if (this.tracing) {
Expand Down Expand Up @@ -312,17 +315,19 @@ class Transport {
if (!this.device.readable) {
return this.leftOver;
}
const reader = this.device.readable.getReader();
this.reader = this.device.readable.getReader();
let t;
try {
if (timeout > 0) {
t = setTimeout(function () {
reader.cancel();
t = setTimeout(() => {
if (this.reader) {
this.reader.cancel();
}
}, timeout);
}
const { value, done } = await reader.read();
const { value, done } = await this.reader.read();
if (done) {
throw new Error("Timeout");
return value;
}
if (this.tracing) {
console.log("Raw Read bytes");
Expand All @@ -333,7 +338,7 @@ class Transport {
if (timeout > 0) {
clearTimeout(t);
}
reader.releaseLock();
this.reader.releaseLock();
}
}

Expand Down Expand Up @@ -401,7 +406,11 @@ class Transport {
* Disconnect from serial device by running SerialPort.close() after streams unlock.
*/
async disconnect() {
if (this.device.readable?.locked) {
await this.reader?.cancel();
}
await this.waitForUnlock(400);
this.reader = undefined;
await this.device.close();
}
}
Expand Down
Loading