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

Enhancement(web-serial): Enable Serial Options During Port Initialization #115

Merged
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
11 changes: 8 additions & 3 deletions src/esploader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ESPError } from "./error";
import { Data, deflate, Inflate } from "pako";
import { Transport } from "./webserial";
import { Transport, SerialOptions } from "./webserial";
import { ROM } from "./targets/rom";
import { customReset, usbJTAGSerialReset } from "./reset";

Expand All @@ -19,6 +19,7 @@ export interface LoaderOptions {
transport: Transport;
port?: SerialPort;
baudrate: number;
serialOptions?: SerialOptions;
terminal?: IEspLoaderTerminal;
romBaudrate: number;
debugLogging?: boolean;
Expand Down Expand Up @@ -135,6 +136,7 @@ export class ESPLoader {

public transport: Transport;
private baudrate: number;
private serialOptions?: SerialOptions;
private terminal?: IEspLoaderTerminal;
private romBaudrate = 115200;
private debugLogging = false;
Expand All @@ -145,6 +147,9 @@ export class ESPLoader {

this.transport = options.transport;
this.baudrate = options.baudrate;
if (options.serialOptions) {
this.serialOptions = options.serialOptions;
}
if (options.romBaudrate) {
this.romBaudrate = options.romBaudrate;
}
Expand Down Expand Up @@ -394,7 +399,7 @@ export class ESPLoader {
let i;
let resp;
this.info("Connecting...", false);
await this.transport.connect(this.romBaudrate);
await this.transport.connect(this.romBaudrate, this.serialOptions);
for (i = 0; i < attempts; i++) {
resp = await this._connect_attempt(mode, false);
if (resp === "success") {
Expand Down Expand Up @@ -830,7 +835,7 @@ export class ESPLoader {
this.info("Changed");
await this.transport.disconnect();
await this._sleep(50);
await this.transport.connect(this.baudrate);
await this.transport.connect(this.baudrate, this.serialOptions);

/* original code seemed absolutely unreliable. use retries and less sleep */
try {
Expand Down
26 changes: 24 additions & 2 deletions src/webserial.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
export interface SerialOptions {
/*
Note: According to the documentation of the Web Serial API, 'baudRate' is a
'required' field as part of serial options. However, we are currently
maintaining 'baudRate' as a separate parameter outside the options
dictionary, and it is effectively used in the code. For now, we are
keeping it optional in the dictionary to avoid conflicts.
*/
baudRate?: number | undefined;
dataBits?: number | undefined;
stopBits?: number | undefined;
parity?: ParityType | undefined;
bufferSize?: number | undefined;
flowControl?: FlowControlType | undefined;
}
class Transport {
public slip_reader_enabled = false;
public left_over = new Uint8Array(0);
Expand Down Expand Up @@ -199,8 +214,15 @@ class Transport {
await this.device.setSignals({ dataTerminalReady: state });
}

async connect(baud = 115200) {
await this.device.open({ baudRate: baud });
async connect(baud = 115200, serialOptions: SerialOptions = {}) {
await this.device.open({
baudRate: baud,
dataBits: serialOptions?.dataBits,
stopBits: serialOptions?.stopBits,
bufferSize: serialOptions?.bufferSize,
parity: serialOptions?.parity,
flowControl: serialOptions?.flowControl,
});
this.baudrate = baud;
this.left_over = new Uint8Array(0);
}
Expand Down
Loading