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 6516f92 commit 1c36259
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
13 changes: 9 additions & 4 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 @@ -17,8 +17,9 @@ export interface FlashOptions {

export interface LoaderOptions {
transport: Transport;
port: SerialPort;
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
25 changes: 23 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,14 @@ class Transport {
await this.device.setSignals({ dataTerminalReady: state });
}

async connect(baud = 115200) {
await this.device.open({ baudRate: baud });
async connect(baud: number = 115200, serialOptions: SerialOptions = {}) {

Check failure on line 217 in src/webserial.ts

View workflow job for this annotation

GitHub Actions / ci

Type number trivially inferred from a number literal, remove type annotation
await this.device.open({ baudRate: baud,

Check failure on line 218 in src/webserial.ts

View workflow job for this annotation

GitHub Actions / ci

Insert `⏎·····`
dataBits: serialOptions?.dataBits,
stopBits: serialOptions?.stopBits,
bufferSize: serialOptions?.bufferSize,
parity: serialOptions?.parity,
flowControl: serialOptions?.flowControl

Check failure on line 223 in src/webserial.ts

View workflow job for this annotation

GitHub Actions / ci

Insert `,`
});
this.baudrate = baud;
this.left_over = new Uint8Array(0);
}
Expand Down

0 comments on commit 1c36259

Please sign in to comment.