Skip to content

Commit 061ffcd

Browse files
committed
Move loading of flashing algorithms to examples.
1 parent 45901d8 commit 061ffcd

File tree

5 files changed

+61
-49
lines changed

5 files changed

+61
-49
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ this.hid = new DAPjs.HID(device);
6969
// open hid device
7070
await this.hid.open();
7171
dapDevice = new DAPjs.DAP(this.hid);
72-
const flashAlgorithm = await DAPjs.FlashAlgorithm.load(deviceCode);
72+
// contains flash algorithms data and memory map
73+
let flashAlgorithmsData = {};
74+
let flashAlgorithm = new DAPjs.FlashAlgorithm(flashAlgorithmsData, this.deviceCode);
7375
this.target = new DAPjs.FlashTarget(dapDevice, flashAlgorithm);
7476

7577
// init and halt target

examples/flash.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ function getFileName() {
4646
});
4747
}
4848

49-
// Load a file, returning a buffer
50-
function loadFile(fileName) {
49+
// Load a file
50+
function loadFile(fileName, isJson=false) {
5151
var file = fs.readFileSync(fileName);
52-
return new Uint8Array(file).buffer;
52+
return isJson ? JSON.parse(file) : new Uint8Array(file).buffer;
5353
}
5454

55-
// Download a file, returning a buffer
56-
function downloadFile(url) {
55+
// Download a file
56+
function downloadFile(url, isJson=false) {
5757
return new Promise((resolve, reject) => {
5858
console.log("Downloading file...");
5959
var scheme = (url.indexOf("https") === 0) ? https : http;
@@ -67,7 +67,12 @@ function downloadFile(url) {
6767
if (response.statusCode !== 200) return reject(response.statusMessage);
6868

6969
var download = Buffer.concat(data);
70-
resolve(new Uint8Array(download).buffer);
70+
if (isJson) {
71+
resolve(JSON.parse(data));
72+
}
73+
else {
74+
resolve(new Uint8Array(download).buffer);
75+
}
7176
});
7277
})
7378
.on("error", error => {
@@ -110,9 +115,16 @@ function getTarget(device) {
110115
return hid.open()
111116
.then(() => {
112117
console.log("Device opened");
113-
118+
// Load flashing algorithms
119+
var flashAlgorithmsFile = "flash_targets/flash_targets.json";
120+
if (flashAlgorithmsFile.indexOf("http") === 0) return downloadFile(flashAlgorithmsFile, true);
121+
return loadFile(flashAlgorithmsFile, true);
122+
})
123+
.then((flashAlgorithms) => {
124+
console.log("Flash algorithms loaded");
114125
var dapDevice = new DAPjs.DAP(hid);
115-
var flashAlgorithm = DAPjs.FlashAlgorithm.load(deviceCode);
126+
var flashAlgorithm = new DAPjs.FlashAlgorithm(flashAlgorithms, deviceCode);
127+
if (!flashAlgorithm.flashAlgo) throw new Error("Flash algorithm not found for this board.");
116128
target = new DAPjs.FlashTarget(dapDevice, flashAlgorithm);
117129
return target.init();
118130
})
@@ -124,6 +136,10 @@ function getTarget(device) {
124136
console.log("Target halted");
125137
return target;
126138
})
139+
.catch(error => {
140+
console.log(error.message || error);
141+
process.exit();
142+
});
127143
}
128144

129145
// Update device using image buffer

examples/web.html

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -218,30 +218,35 @@
218218
log("Device opened.");
219219

220220
this.dapDevice = new DAPjs.DAP(this.hid);
221-
const flashAlgorithm = await DAPjs.FlashAlgorithm.load(this.deviceCode).catch(error => log(error));
222-
if (!flashAlgorithm) return log("No flashing algorithm can be found for this board.");
223-
this.target = new DAPjs.FlashTarget(this.dapDevice, flashAlgorithm);
224-
225-
log("Initialising device.");
226-
227-
await this.target.init();
221+
const xhr = new XMLHttpRequest();
222+
xhr.responseType = "json";
223+
xhr.open("GET", "../flash_targets/flash_targets.json", true);
224+
xhr.onload = async (e) => {
225+
if (xhr.status !== 200) return log(xhr.statusText);
228226

229-
log("Halting target.");
227+
let flashAlgorithm = new DAPjs.FlashAlgorithm(xhr.response, this.deviceCode);
228+
if (!flashAlgorithm.flashAlgo) return log("No flashing algorithm can be found for this board.");
230229

231-
await this.target.halt();
230+
this.target = new DAPjs.FlashTarget(this.dapDevice, flashAlgorithm);
232231

233-
log("Target halted.");
232+
log("Initialising device.");
233+
await this.target.init();
234234

235-
const [imp, isa, type] = await this.target.readCoreType();
236-
log(`Connected to an ARM ${DAPjs.CoreNames.get(type)} (${DAPjs.ISANames.get(isa)})`);
235+
log("Halting target.");
236+
await this.target.halt();
237+
log("Target halted.");
237238

238-
document.getElementById("connect").disabled = true;
239+
const [imp, isa, type] = await this.target.readCoreType();
240+
log(`Connected to an ARM ${DAPjs.CoreNames.get(type)} (${DAPjs.ISANames.get(isa)})`);
241+
document.getElementById("connect").disabled = true;
239242

240-
const elements = Array.from(document.querySelectorAll(".when-connected"));
243+
const elements = Array.from(document.querySelectorAll(".when-connected"));
241244

242-
for (const elem of elements) {
243-
elem.disabled = false;
244-
}
245+
for (const elem of elements) {
246+
elem.disabled = false;
247+
}
248+
};
249+
xhr.send();
245250
}
246251

247252
async function halt() {

src/documentation.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ this.hid = new DAPjs.HID(device);
3131
// open hid device
3232
await this.hid.open();
3333
dapDevice = new DAPjs.DAP(this.hid);
34-
const flashAlgorithm = await DAPjs.FlashAlgorithm.load(this.deviceCode);
34+
// contains flash algorithms data and memory map
35+
let flashAlgorithmsData = {};
36+
let flashAlgorithm = new DAPjs.FlashAlgorithm(flashAlgorithmsData, this.deviceCode);
3537
this.target = new DAPjs.FlashTarget(dapDevice, flashAlgorithm);
3638

3739
// init and halt target

src/targets/FlashAlgorithm.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,21 @@ export class FlashAlgorithm {
3131
public flashAlgo: IFlashAlgo;
3232
public memoryMap: IMemoryRegion[];
3333

34-
public static async load(deviceCode: string) {
35-
const xhr = new XMLHttpRequest();
36-
xhr.open("get", "../../flash_targets/flash_targets.json", true);
37-
xhr.responseType = "json";
38-
return new Promise<FlashAlgorithm>((resolve, reject) => {
39-
xhr.onload = (_e: any) => {
40-
if (xhr.status !== 200) return reject(xhr.statusText);
41-
let flashAlgorithm = null;
42-
const algorithms = xhr.response;
43-
if (deviceCode in algorithms) {
44-
flashAlgorithm = new FlashAlgorithm();
45-
flashAlgorithm.flashAlgo = FlashAlgorithm.setFlashingAlgo(algorithms[deviceCode]);
46-
flashAlgorithm.memoryMap = FlashAlgorithm.setMemoryMap(algorithms[deviceCode]);
47-
}
48-
resolve(flashAlgorithm);
49-
};
50-
xhr.send();
51-
});
34+
public constructor(flashAlgorithmsData: any, deviceCode: string) {
35+
if (deviceCode in flashAlgorithmsData) {
36+
this.setFlashingAlgo(flashAlgorithmsData[deviceCode]);
37+
this.setMemoryMap(flashAlgorithmsData[deviceCode]);
38+
}
5239
}
5340

54-
private static setFlashingAlgo(flashAlgorithm: any): IFlashAlgo {
41+
private setFlashingAlgo(flashAlgorithm: any) {
5542
const instructions = flashAlgorithm.flash_algo.instructions.map((instruction: string) => {
5643
return parseInt(instruction, 16);
5744
});
5845
const pageBuffers = flashAlgorithm.flash_algo.page_buffers.map((pageBuffer: string) => {
5946
return parseInt(pageBuffer, 16);
6047
});
61-
return {
48+
this.flashAlgo = {
6249
analyzerAddress: parseInt(flashAlgorithm.flash_algo.analyzer_address, 16),
6350
analyzerSupported: flashAlgorithm.flash_algo.analyzer_supported,
6451
loadAddress: parseInt(flashAlgorithm.flash_algo.load_address, 16),
@@ -73,8 +60,8 @@ export class FlashAlgorithm {
7360
};
7461
}
7562

76-
private static setMemoryMap(flashAlgorithm: any): IMemoryRegion[] {
77-
return flashAlgorithm.memory_map.map((region: any) => {
63+
private setMemoryMap(flashAlgorithm: any) {
64+
this.memoryMap = flashAlgorithm.memory_map.map((region: any) => {
7865
return {
7966
blocksize: parseInt(region.blocksize, 16),
8067
end: parseInt(region.end, 16),

0 commit comments

Comments
 (0)