Skip to content

Wait if we hit errors reading FICR on connect #14

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

Merged
merged 1 commit into from
Jul 25, 2024
Merged
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
27 changes: 25 additions & 2 deletions lib/usb-device-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,33 @@ export class DAPWrapper {
});
}

// https://support.microbit.org/support/solutions/articles/19000067679-how-to-find-the-name-of-your-micro-bit
// We wait on errors as immediately after flash the micro:bit won't be ready to respond
this._deviceId = await this.readMem32WaitOnError(FICR.DEVICE_ID_1);

this._pageSize = await this.cortexM.readMem32(FICR.CODEPAGESIZE);
this._numPages = await this.cortexM.readMem32(FICR.CODESIZE);
// https://support.microbit.org/support/solutions/articles/19000067679-how-to-find-the-name-of-your-micro-bit
this._deviceId = await this.cortexM.readMem32(FICR.DEVICE_ID_1);
}

async readMem32WaitOnError(register: number): Promise<number> {
let retries = 0;
let lastError: Error | undefined;
while (retries < 10) {
try {
return await this.cortexM.readMem32(register);
} catch (e) {
if (e instanceof Error) {
lastError = e;
if (/^Transfer/.test(e.message)) {
retries++;
await new Promise((resolve) => setTimeout(resolve, 10));
} else {
throw e;
}
}
}
}
throw lastError;
}

async startSerial(listener: (data: string) => void): Promise<void> {
Expand Down