Skip to content

Commit

Permalink
enhancement(web-serial): Enable serial options during port initializa…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
Rushikesh Patange committed Nov 1, 2023
1 parent 6e9ae9b commit e89547e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
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

0 comments on commit e89547e

Please sign in to comment.