Skip to content

Commit 18dd3ec

Browse files
authored
Merge branch 'master' into f/serial
2 parents f41923a + b4c522c commit 18dd3ec

File tree

11 files changed

+19751
-266
lines changed

11 files changed

+19751
-266
lines changed

README.md

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

7477
// init and halt target
7578
await this.target.init();

examples/flash.js

Lines changed: 25 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,17 @@ 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-
target = new DAPjs.FlashTarget(dapDevice, DAPjs.FlashTargets.get(deviceCode));
126+
var flashAlgorithm = new DAPjs.FlashAlgorithm(flashAlgorithms, deviceCode);
127+
if (!flashAlgorithm.flashAlgo) throw new Error("Flash algorithm not found for this board.");
128+
target = new DAPjs.FlashTarget(dapDevice, flashAlgorithm);
116129
return target.init();
117130
})
118131
.then(() => {
@@ -123,6 +136,10 @@ function getTarget(device) {
123136
console.log("Target halted");
124137
return target;
125138
})
139+
.catch(error => {
140+
console.log(error.message || error);
141+
process.exit();
142+
});
126143
}
127144

128145
// Update device using image buffer

examples/web.html

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -261,27 +261,37 @@
261261
log("Device opened.");
262262

263263
this.dapDevice = new DAPjs.DAP(this.hid);
264-
this.serial = new DAPjs.Serial(dapDevice);
265-
this.target = new DAPjs.FlashTarget(this.dapDevice, DAPjs.FlashTargets.get(this.deviceCode));
266264

267-
log("Initialising device.");
265+
const xhr = new XMLHttpRequest();
266+
xhr.responseType = "json";
267+
xhr.open("GET", "../flash_targets/flash_targets.json", true);
268+
xhr.onload = async (e) => {
269+
if (xhr.status !== 200) return log(xhr.statusText);
268270

269-
await this.target.init();
271+
let flashAlgorithm = new DAPjs.FlashAlgorithm(xhr.response, this.deviceCode);
272+
if (!flashAlgorithm.flashAlgo) return log("No flashing algorithm can be found for this board.");
270273

271-
log("Halting target.");
272-
await this.target.halt();
273-
log("Target halted.");
274+
this.serial = new DAPjs.Serial(dapDevice);
275+
this.target = new DAPjs.FlashTarget(this.dapDevice, flashAlgorithm);
274276

275-
const [imp, isa, type] = await this.target.readCoreType();
276-
log(`Connected to an ARM ${DAPjs.CoreNames.get(type)} (${DAPjs.ISANames.get(isa)})`);
277+
log("Initialising device.");
278+
await this.target.init();
277279

278-
document.getElementById("connect").disabled = true;
280+
log("Halting target.");
281+
await this.target.halt();
282+
log("Target halted.");
279283

280-
const elements = Array.from(document.querySelectorAll(".when-connected"));
284+
const [imp, isa, type] = await this.target.readCoreType();
285+
log(`Connected to an ARM ${DAPjs.CoreNames.get(type)} (${DAPjs.ISANames.get(isa)})`);
286+
document.getElementById("connect").disabled = true;
281287

282-
for (const elem of elements) {
283-
elem.disabled = false;
284-
}
288+
const elements = Array.from(document.querySelectorAll(".when-connected"));
289+
290+
for (const elem of elements) {
291+
elem.disabled = false;
292+
}
293+
};
294+
xhr.send();
285295
}
286296

287297
async function halt() {

0 commit comments

Comments
 (0)