forked from devanlai/webstlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
355 lines (298 loc) · 11.3 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import * as libstlink from './src/lib/package.js';
import WebStlink from './src/webstlink.js';
import { hex_octet, hex_word, hex_octet_array } from './src/lib/util.js';
function fetchResource(url) {
return new Promise(function(resolve, reject) {
let xhr = new XMLHttpRequest();
xhr.responseType = "arraybuffer";
xhr.addEventListener("load", function() {
if (this.status != 200) {
reject(this.status);
} else {
resolve(this.response);
}
});
xhr.addEventListener("error", function() {
reject(this.status);
});
xhr.open("GET", url);
xhr.send();
});
}
function read_file_as_array_buffer(file) {
return new Promise(function (resolve, reject) {
let reader = new FileReader();
reader.onload = function() {
resolve(reader.result);
};
reader.onerror = function() {
reject(reader.error);
};
reader.readAsArrayBuffer(file);
});
}
function show_error_dialog(error) {
let dialog = document.createElement("dialog");
let header = document.createElement("h1");
header.textContent = "Uh oh! Something went wrong.";
let contents = document.createElement("p");
contents.textContent = error.toString();
let button = document.createElement("button");
button.textContent = "Close";
button.addEventListener("click", (evt) => {
dialog.close();
});
dialog.addEventListener("close", (evt) => {
dialog.remove();
});
dialog.appendChild(header);
dialog.appendChild(contents);
dialog.appendChild(document.createElement("br"));
dialog.appendChild(button);
document.querySelector("body").appendChild(dialog);
dialog.showModal();
}
async function pick_sram_variant(mcu_list) {
// Display a dialog with the MCU variants for the user to pick
let dialog = document.querySelector("#mcuDialog");
let tbody = dialog.querySelector("tbody");
// Remove old entries
for (let row of tbody.querySelectorAll("tr")) {
tbody.removeChild(row);
}
const columns = [
["type", ""],
["freq", "MHz"],
["flash_size", "KiB"],
["sram_size", "KiB"],
["eeprom_size", "KiB"],
];
for (let mcu of mcu_list) {
let tr = document.createElement("tr");
for (let [key, suffix] of columns) {
let td = document.createElement("td");
if (key == "type") {
let label = document.createElement("label");
let input = document.createElement("input");
let text = document.createTextNode(mcu[key] + suffix);
label.appendChild(input);
label.appendChild(text);
input.type = "radio";
input.name = "mcuIndex";
input.value = mcu.type;
input.required = true;
td.appendChild(label);
} else {
td.textContent = mcu[key] + suffix;
}
tr.appendChild(td);
}
tbody.appendChild(tr);
}
let submit_promise = new Promise(function (resolve, reject) {
function on_submit(evt) {
dialog.removeEventListener('cancel', on_cancel);
resolve(evt.target.elements["mcuIndex"].value);
}
function on_cancel() {
dialog.removeEventListener('submit', on_submit);
reject();
}
dialog.addEventListener('cancel', on_cancel, { once: true});
dialog.addEventListener('submit', on_submit, { once: true});
});
dialog.showModal();
// Wait for the user's selection and return it, otherwise
// return null if they canceled
try {
let type = await submit_promise;
return type;
} catch (e) {
return null;
}
}
function update_debugger_info(stlink, device) {
let probeInfo = document.getElementById("probeInfo");
let summary = probeInfo.querySelector("summary");
let version = "ST-Link/" + stlink._stlink.ver_str;
summary.textContent = `Debugger - ${version} - Connected`;
document.getElementById("productName").textContent = device.productName;
document.getElementById("mfgName").textContent = device.manufacturerName;
document.getElementById("serialNumber").textContent = device.serialNumber;
}
function update_target_status(status, target = null) {
let targetInfo = document.getElementById("targetInfo");
let targetStatus = document.getElementById("targetStatus");
if (target !== null) {
let targetType = document.getElementById("targetType");
targetType.textContent = "- " + target.type + " -";
// Remove old target fields
for (let div of targetInfo.querySelectorAll("div")) {
targetInfo.removeChild(div);
}
let fields = [
["type", "Type", ""],
["core", "Core", ""],
["dev_id", "Device ID", ""],
["flash_size", "Flash Size", "KiB"],
["sram_size", "SRAM Size", "KiB"],
];
if (target.eeprom_size > 0) {
fields.push(["eeprom_size", "EEPROM Size", "KiB"]);
}
for (let [key, title, suffix] of fields) {
let div = document.createElement("div");
div.textContent = title + ": " + target[key] + suffix;
targetInfo.appendChild(div);
}
}
let haltState = status.halted ? "Halted" : "Running";
let debugState = "Debugging " + (status.debug ? "Enabled" : "Disabled");
targetStatus.textContent = `${haltState}, ${debugState}`;
}
function prevent_submission(event) {
event.preventDefault();
return false;
}
document.addEventListener('DOMContentLoaded', event => {
var stlink = null;
var curr_device = null;
let log = document.querySelector("#log");
let logger = new libstlink.Logger(2, log);
let connectButton = document.querySelector("#connect");
let flashButton = document.querySelector("#flash");
window.setInterval(function() {
log.scrollTop = log.scrollHeight;
}, 500);
async function read_and_display_memory(explicit = false) {
if (stlink !== null && stlink.connected) {
let addr_field = document.getElementById("memoryReadAddress");
let size_field = document.getElementById("memoryReadSize");
try {
var addr = parseInt(addr_field.value, 16);
var size = parseInt(size_field.value, 10);
} catch (error) {
return;
}
let memory = await stlink.read_memory(addr, size);
let memoryContents = document.getElementById("memoryContents");
memoryContents.textContent = hex_octet_array(memory).join(" ");
}
}
flashButton.addEventListener('click', async function (evt) {
if (stlink !== null && stlink.connected) {
let addr_field = document.getElementById("flashWriteAddress");
try {
var addr = parseInt(addr_field.value, 16);
} catch (error) {
return;
}
let field = document.getElementById("flashBinaryFile");
if (field.files.length > 0) {
let file = field.files[0];
let data = await read_file_as_array_buffer(file);
if (!stlink.last_cpu_status.halted) await stlink.halt();
try {
await stlink.flash(addr, data);
} catch (err) {
logger.error(err);
show_error_dialog(err);
}
finally{
setTimeout(read_and_display_memory, 500,true);
await stlink.reset(stlink.last_cpu_status.halted);
}
}
}
});
function update_capabilities(status) {
if (status.debug) {
if (status.halted) {
flashButton.disabled = false;
} else {
flashButton.disabled = false;
}
} else {
flashButton.disabled = true;
}
}
async function on_successful_attach(stlink, device) {
// Export for manual debugging
window.stlink = stlink;
window.device = device;
// Reset settings
connectButton.textContent = "Disconnect";
// Populate debugger info
update_debugger_info(stlink, device);
// Add disconnect handler
navigator.usb.addEventListener('disconnect', function (evt) {
if (evt.device === device) {
navigator.usb.removeEventListener('disconnect', this);
if (device === curr_device) {
on_disconnect();
}
}
});
// Detect attached target CPU
let target = await stlink.detect_cpu([], pick_sram_variant);
// Update the UI with detected target info and debug state
let status = await stlink.inspect_cpu();
if (!status.debug) {
// Automatically enable debugging
await stlink.set_debug_enable(true);
status = await stlink.inspect_cpu();
}
update_target_status(status, target);
update_capabilities(status);
// Set the read memory address to the SRAM start
document.getElementById("memoryReadAddress").value = "0x" + hex_word(target.flash_start);
// Set the flash write address to the Flash start
document.getElementById("flashWriteAddress").value = "0x" + hex_word(target.flash_start);
setTimeout(read_and_display_memory, 500,true);
}
document.getElementById("memoryReadAddress").addEventListener('change', function (evt) {
return read_and_display_memory(true);
});
document.getElementById("memoryReadSize").addEventListener('change', function (evt) {
return read_and_display_memory(true);
});
function on_disconnect() {
logger.info("Device disconnected");
connectButton.textContent = "Connect";
flashButton.disabled = true;
let probeInfo = document.getElementById("probeInfo");
let summary = probeInfo.querySelector("summary");
summary.textContent = "Debugger - Disconnected";
document.getElementById("productName").textContent = "";
document.getElementById("mfgName").textContent = "";
document.getElementById("serialNumber").textContent = "";
stlink = null;
curr_device = null;
}
if (typeof navigator.usb === 'undefined') {
logger.error("WebUSB is either disabled or not available in this browser");
connectButton.disabled = true;
}
connectButton.addEventListener('click', async function() {
if (stlink !== null) {
await stlink.detach();
on_disconnect();
return;
}
try {
let device = await navigator.usb.requestDevice({
filters: libstlink.usb.filters
});
logger.clear();
let next_stlink = new WebStlink(logger);
await next_stlink.attach(device, logger);
stlink = next_stlink;
curr_device = device;
} catch (err) {
logger.error(err);
}
if (stlink !== null) {
await on_successful_attach(stlink, curr_device);
}
});
});