Skip to content

Commit b4c522c

Browse files
authored
Merge pull request #18 from ARMmbed/f/flashing-algorithms
Add flashing algorithms for all boards supported by pyOCD.
2 parents 667dfe8 + 061ffcd commit b4c522c

File tree

11 files changed

+19749
-267
lines changed

11 files changed

+19749
-267
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: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,28 +218,35 @@
218218
log("Device opened.");
219219

220220
this.dapDevice = new DAPjs.DAP(this.hid);
221-
this.target = new DAPjs.FlashTarget(this.dapDevice, DAPjs.FlashTargets.get(this.deviceCode));
222-
223-
log("Initialising device.");
224-
225-
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);
226226

227-
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.");
228229

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

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

233-
const [imp, isa, type] = await this.target.readCoreType();
234-
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.");
235238

236-
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;
237242

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

240-
for (const elem of elements) {
241-
elem.disabled = false;
242-
}
245+
for (const elem of elements) {
246+
elem.disabled = false;
247+
}
248+
};
249+
xhr.send();
243250
}
244251

245252
async function halt() {

0 commit comments

Comments
 (0)