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

cleanup: app should provide programmer #67

Merged
merged 1 commit into from
Mar 9, 2025
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
9 changes: 7 additions & 2 deletions demo/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,13 @@ window.connectDevice = async () => {
throw new Error("Browser missing WebUSB support");
}

// Initialize QDL device with programmer URL
const qdl = new qdlDevice(programmerSelect.value);
// Fetch programmer
const programmer = await fetch(programmerSelect.value)
.then((response) => response.blob())
.then((blob) => blob.arrayBuffer());

// Initialize QDL device with programmer
const qdl = new qdlDevice(programmer);
window.qdl = qdl;

// Start the connection
Expand Down
12 changes: 6 additions & 6 deletions src/qdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export class qdlDevice {
#firehose = null

/**
* @param {string} programmerUrl
* @param {ArrayBuffer} programmer
*/
constructor(programmerUrl) {
if (!programmerUrl) {
throw "programmerUrl is required";
constructor(programmer) {
if (!programmer) {
throw "programmer is required";
}
this.programmerUrl = programmerUrl;
this.programmer = programmer;
/**
* @type {string|null}
*/
Expand All @@ -42,7 +42,7 @@ export class qdlDevice {
if (!cdc.connected) await cdc.connect();
if (!cdc.connected) throw new Error("Could not connect to device");
console.debug("[qdl] QDL device detected");
this.sahara = new Sahara(cdc, this.programmerUrl);
this.sahara = new Sahara(cdc, this.programmer);
if (!await runWithTimeout(this.sahara.connect(), 10000)) throw new Error("Could not connect to Sahara");
console.debug("[qdl] Connected to Sahara");
this.mode = "sahara";
Expand Down
67 changes: 8 additions & 59 deletions src/sahara.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,14 @@ import { CommandHandler, cmd_t, sahara_mode_t, status_t, exec_cmd_t } from "./sa
import { concatUint8Array, packGenerator } from "./utils";


class localFile {
constructor(url) {
this.url = url;
this.filename = url.substring(url.lastIndexOf("/") + 1);
}

async download() {
const rootDir = await navigator.storage.getDirectory();
let writable;
try {
const fileHandle = await rootDir.getFileHandle(this.filename, { create: true });
writable = await fileHandle.createWritable();
} catch (error) {
throw `Sahara - Error getting file handle ${error}`;
}
const response = await fetch(this.url, { mode: "cors" })
if (!response.ok || !response.body) {
throw `Sahara - Failed to fetch loader: ${response.status} ${response.statusText}`;
}
try {
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
await writable.write(value);
}
} catch (error) {
throw `Sahara - Could not read response body: ${error}`;
}
try {
await writable.close();
} catch (error) {
throw `Sahara - Error closing file handle: ${error}`;
}
}

async get() {
const rootDir = await navigator.storage.getDirectory();
let fileHandle;
try {
fileHandle = await rootDir.getFileHandle(this.filename);
} catch (error) {
throw `Sahara - Error getting file handle: ${error}`;
}
return await fileHandle.getFile();
}
}


export class Sahara {
/**
* @param {usbClass} cdc
* @param {string} programmerUrl
* @param {ArrayBuffer} programmer
*/
constructor(cdc, programmerUrl) {
constructor(cdc, programmer) {
this.cdc = cdc;
this.programmer = new localFile(programmerUrl);
this.programmer = programmer;
this.ch = new CommandHandler();
this.id = null;
this.serial = "";
Expand Down Expand Up @@ -182,14 +133,12 @@ export class Sahara {

await this.connect();
console.debug("[sahara] Uploading loader...");
await this.programmer.download();
const programmer = await this.programmer.get().then((file) => file.arrayBuffer());
if (!(await this.cmdHello(sahara_mode_t.SAHARA_MODE_IMAGE_TX_PENDING))) {
throw "Sahara - Error while uploading loader";
}

const start = performance.now();
let remainingBytes = programmer.byteLength;
let remainingBytes = this.programmer.byteLength;
while (remainingBytes >= 0) {
const resp = await this.getResponse();
if (!resp || !("cmd" in resp)) {
Expand All @@ -208,13 +157,13 @@ export class Sahara {
}

let dataToWrite;
if (data_offset + data_len > programmer.byteLength) {
if (data_offset + data_len > this.programmer.byteLength) {
dataToWrite = new Uint8Array(data_len);
if (data_offset < programmer.byteLength) {
dataToWrite.set(new Uint8Array(programmer, data_offset, programmer.byteLength - data_offset));
if (data_offset < this.programmer.byteLength) {
dataToWrite.set(new Uint8Array(this.programmer, data_offset, this.programmer.byteLength - data_offset));
}
} else {
dataToWrite = new Uint8Array(programmer, data_offset, data_len);
dataToWrite = new Uint8Array(this.programmer, data_offset, data_len);
}

await this.cdc.write(dataToWrite);
Expand Down