Skip to content

Commit

Permalink
clear command buffers before flashing
Browse files Browse the repository at this point in the history
  • Loading branch information
jwunderl committed Aug 23, 2024
1 parent b075e3b commit 1fc5c3e
Showing 1 changed file with 54 additions and 53 deletions.
107 changes: 54 additions & 53 deletions editor/flash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class DAPWrapper implements pxt.packetio.PacketIOWrapper {
}

private async readSerial(): Promise<number> {
let buf = await this.dapCmdNums(0x83)
let buf = await this.dapCmdNums(0x83); // 131
const len = buf[1]
// concat received data with previous data
if (len) {
Expand Down Expand Up @@ -167,58 +167,54 @@ class DAPWrapper implements pxt.packetio.PacketIOWrapper {
return len
}

private startReadSerial(connectionId: number) {
private async startReadSerial(connectionId: number) {
const startTime = Date.now();
log(`start read serial ${connectionId}`)
const readSerialLoop = async () => {
try {
let numSer = 0
let numEv = 0
while (connectionId === this.connectionId) {
numSer = await this.readSerial()
// we need to read jacdac in a tight loop
// so we don't miss any event
if (this.xchgAddr)
numEv = await this.jacdacProcess()
else
numEv = 0

// no data on either side, wait as little as possible
// the browser will eventually throttle this call
// https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#reasons_for_delays_longer_than_specified
if (!numSer && !numEv)
await pxt.U.delay(0)
}
try {
let numSer = 0
let numEv = 0
while (connectionId === this.connectionId) {
numSer = await this.readSerial()
// we need to read jacdac in a tight loop
// so we don't miss any event
if (this.xchgAddr)
numEv = await this.jacdacProcess()
else
numEv = 0

// no data on either side, wait as little as possible
// the browser will eventually throttle this call
// https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#reasons_for_delays_longer_than_specified
if (!numSer && !numEv)
await pxt.U.delay(0)
}
log(`stopped serial reader ${connectionId}`)
} catch (err) {
log(`serial error ${connectionId}: ${err.message}`);
console.error(err)
if (connectionId != this.connectionId) {
log(`stopped serial reader ${connectionId}`)
} catch (err) {
log(`serial error ${connectionId}: ${err.message}`);
console.error(err)
if (connectionId != this.connectionId) {
log(`stopped serial reader ${connectionId}`)
} else {
pxt.tickEvent("hid.flash.serial.error");
const timeRunning = Date.now() - startTime
await this.disconnectAsync(); // force disconnect
// if we've been running for a while, try reconnecting
if (timeRunning > 1000) {
log(`auto-reconnect`)
try {
await this.reconnectAsync();
} catch (e) {
if (e.type === "devicenotfound")
return
throw e
}
} else {
pxt.tickEvent("hid.flash.serial.error");
const timeRunning = Date.now() - startTime
await this.disconnectAsync(); // force disconnect
// if we've been running for a while, try reconnecting
if (timeRunning > 1000) {
log(`auto-reconnect`)
try {
await this.reconnectAsync();
} catch (e) {
if (e.type === "devicenotfound")
return
throw e
}
}
}
finally {
this.pendingSerial = undefined
this.lastPendingSerial = undefined
}
}

readSerialLoop();
finally {
this.pendingSerial = undefined
this.lastPendingSerial = undefined
}
}

private stopReadersAsync() {
Expand Down Expand Up @@ -312,13 +308,7 @@ class DAPWrapper implements pxt.packetio.PacketIOWrapper {

await this.io.reconnectAsync()

// before calling into dapjs, push through a few commands to make sure the responses
// to commands from previous sessions (if any) are flushed. Count of 5 is arbitrary.
for (let i = 0; i < 5; i++) {
try {
await this.getDaplinkVersionAsync();
} catch (e) {}
}
await this.clearCommandsAsync()

// halt before reading from dap
// to avoid interference from data logger
Expand Down Expand Up @@ -355,6 +345,16 @@ class DAPWrapper implements pxt.packetio.PacketIOWrapper {
this.startReadSerial(connectionId)
}

private async clearCommandsAsync() {
// before calling into dapjs, push through a few commands to make sure the responses
// to commands from previous sessions (if any) are flushed. Count of 5 is arbitrary.
for (let i = 0; i < 5; i++) {
try {
await this.getDaplinkVersionAsync();
} catch (e) {}
}
}

private async getDaplinkVersionAsync() {
return await this.dapCmdNums(0x00, 0x04);
}
Expand Down Expand Up @@ -402,6 +402,7 @@ class DAPWrapper implements pxt.packetio.PacketIOWrapper {
await this.io.reconnectAsync();
}

await this.clearCommandsAsync()
await this.stopReadersAsync();
await this.cortexM.init();
await this.cortexM.reset(true);
Expand Down

0 comments on commit 1fc5c3e

Please sign in to comment.