-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
336 lines (306 loc) · 11.3 KB
/
index.html
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
<!doctype html>
<html>
<head>
<title>HalfKay test</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="main">
<div class="wrapper">
<div class="bot">
<h1>Upload-o-matic</h1>
<p>Choose a program to upload to an Teensy (currently TeensyLC ONLY)</p>
<p> Note: there has to be a serial interface in your sketch, otherwise reset to bootloader works only with the "program" button</p>
<form id="uploadForm">
<label>Program:
<div class="fileButtonWrapper">
<button id="fileButton" type="button" aria-controls="fileInput">Choose file</button>
<input id="fileInput" tabindex="-1" type="file"/>
<span id="fileName">no file chosen</span>
</div>
</label>
<button type="submit" id="uploadBtn">Upload!</button>
</form>
<form id="resetForm">
<button type="submit" id="resetBtn">Reset!</button>
</form>
</div>
<div class="board">
<img src="images/TeensyLC.jpg"/>
</div>
<div id="pipe"></div>
<div id="progress"></div>
<div id="gear"><img src="images/gear4.svg"/></div>
</div>
</div>
<!-- HELPER FUNCTIONS -->
<script>
//credits: https://stackoverflow.com/questions/38987784/how-to-convert-a-hexadecimal-string-to-uint8array-and-back-in-javascript
const fromHexString = hexString =>
new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
</script>
<!-- Intel HEX parser by https://github.com/bminer/intel-hex.js -->
<script>
//Intel Hex record types
const DATA = 0,
END_OF_FILE = 1,
EXT_SEGMENT_ADDR = 2,
START_SEGMENT_ADDR = 3,
EXT_LINEAR_ADDR = 4,
START_LINEAR_ADDR = 5;
const EMPTY_VALUE = 0xFF;
/* intel_hex.parse(data)
`data` - Intel Hex file (string in ASCII format or Buffer Object)
`bufferSize` - the size of the Buffer containing the data (optional)
returns an Object with the following properties:
- data - data as a Buffer Object, padded with 0xFF
where data is empty.
- startSegmentAddress - the address provided by the last
start segment address record; null, if not given
- startLinearAddress - the address provided by the last
start linear address record; null, if not given
Special thanks to: http://en.wikipedia.org/wiki/Intel_HEX
*/
function parseIntelHex(data) {
//if(data instanceof Buffer)
data = data.toString("ascii");
//Initialization
//TODO: this size is set for TeensyLC, is bigger for other ARM Teensy devices!
var buf = new Uint8Array(65536); //max. words in mega32u4
var bufLength = 0, //Length of data in the buffer
highAddress = 0, //upper address
startSegmentAddress = null,
startLinearAddress = null,
lineNum = 0, //Line number in the Intel Hex string
pos = 0; //Current position in the Intel Hex string
const SMALLEST_LINE = 11;
while(pos + SMALLEST_LINE <= data.length)
{
//Parse an entire line
if(data.charAt(pos++) != ":")
throw new Error("Line " + (lineNum+1) +
" does not start with a colon (:).");
else
lineNum++;
//Number of bytes (hex digit pairs) in the data field
var dataLength = parseInt(data.substr(pos, 2), 16);
pos += 2;
//Get 16-bit address (big-endian)
var lowAddress = parseInt(data.substr(pos, 4), 16);
pos += 4;
//Record type
var recordType = parseInt(data.substr(pos, 2), 16);
pos += 2;
//Data field (hex-encoded string)
var dataField = data.substr(pos, dataLength * 2);
if(dataLength) var dataFieldBuf = fromHexString(dataField);
else var dataFieldBuf = new Uint8Array();
pos += dataLength * 2;
//Checksum
var checksum = parseInt(data.substr(pos, 2), 16);
pos += 2;
//Validate checksum
var calcChecksum = (dataLength + (lowAddress >> 8) +
lowAddress + recordType) & 0xFF;
for(var i = 0; i < dataLength; i++)
calcChecksum = (calcChecksum + dataFieldBuf[i]) & 0xFF;
calcChecksum = (0x100 - calcChecksum) & 0xFF;
if(checksum != calcChecksum)
throw new Error("Invalid checksum on line " + lineNum +
": got " + checksum + ", but expected " + calcChecksum);
//Parse the record based on its recordType
switch(recordType)
{
case DATA:
var absoluteAddress = highAddress + lowAddress;
//Expand buf, if necessary
/*if(absoluteAddress + dataLength >= buf.length)
{
var tmp = Buffer.alloc((absoluteAddress + dataLength) * 2);
buf.copy(tmp, 0, 0, bufLength);
buf = tmp;
}*/
//Write over skipped bytes with EMPTY_VALUE
if(absoluteAddress > bufLength)
buf.fill(EMPTY_VALUE, bufLength, absoluteAddress);
//Write the dataFieldBuf to buf
//dataFieldBuf.copy(buf, absoluteAddress);
dataFieldBuf.forEach( function(val,index) {
buf[absoluteAddress+index] = val;
});
bufLength = Math.max(bufLength, absoluteAddress + dataLength);
break;
case END_OF_FILE:
if(dataLength != 0)
throw new Error("Invalid EOF record on line " +
lineNum + ".");
return {
"data": buf.slice(0, bufLength),
"startSegmentAddress": startSegmentAddress,
"startLinearAddress": startLinearAddress
};
break;
case EXT_SEGMENT_ADDR:
if(dataLength != 2 || lowAddress != 0)
throw new Error("Invalid extended segment address record on line " +
lineNum + ".");
highAddress = parseInt(dataField, 16) << 4;
break;
case START_SEGMENT_ADDR:
if(dataLength != 4 || lowAddress != 0)
throw new Error("Invalid start segment address record on line " +
lineNum + ".");
startSegmentAddress = parseInt(dataField, 16);
break;
case EXT_LINEAR_ADDR:
if(dataLength != 2 || lowAddress != 0)
throw new Error("Invalid extended linear address record on line " +
lineNum + ".");
highAddress = parseInt(dataField, 16) << 16;
break;
case START_LINEAR_ADDR:
if(dataLength != 4 || lowAddress != 0)
throw new Error("Invalid start linear address record on line " +
lineNum + ".");
startLinearAddress = parseInt(dataField, 16);
break;
default:
throw new Error("Invalid record type (" + recordType +
") on line " + lineNum);
break;
}
//Advance to the next line
if(data.charAt(pos) == "\r")
pos++;
if(data.charAt(pos) == "\n")
pos++;
}
throw new Error("Unexpected end of input: missing or invalid EOF record.");
};
</script>
<script>
//credits: https://www.geeksforgeeks.org/how-to-delay-a-loop-in-javascript-using-async-await-with-promise/
function waitforme(milisec) {
return new Promise(resolve => {
setTimeout(() => { resolve('') }, milisec);
})
}
//credits: https://www.30secondsofcode.org/articles/s/javascript-array-comparison
const equals = (a, b) =>
a.length === b.length &&
a.every((v, i) => v === b[i]);
/****************/
// 1.) reset TeensyLC into bootloader mode (user interaction required)
/****************/
async function handleReset(e) {
if (!("serial" in navigator)) {
// The Web Serial API is not supported.
alert("Please use Chromium based browsers!");
}
e.preventDefault();
gear.classList.add('spinning');
let filters = [
{ usbVendorId: 0x16C0, usbProductId: 0x0487 }
//TODO: I think there are more possible PIDs...
];
port = await navigator.serial.requestPort({filters});
//open & close
// Wait for the serial port to open.
//source for this value: https://github.com/PaulStoffregen/teensy_loader_cli/blob/master/teensy_loader_cli.c "soft_reboot"
await port.open({ baudRate: 0x86 });
await waitforme(200);
await port.close();
//await waitforme(500);
gear.classList.remove('spinning');
}
/****************/
// 2.) open the new bootloader USB-RAW HID (new USB-PID!); user interaction required
/****************/
async function handleSubmit(e) {
if (!("hid" in navigator)) {
// The Web HID API is not supported.
alert("Please use Chromium based browsers!");
}
e.preventDefault();
let filecontents;
const file = fileInput.files[0];
const readerF = new FileReader();
readerF.onload = async function(event) {
filecontents = event.target.result;
gear.classList.add('spinning');
//parse intel hex
let flashData = parseIntelHex(filecontents);
//request serial port
let filters = [
{ vendorId: 0x16C0, productId: 0x0478 }
];
const [port] = await navigator.hid.requestDevice({filters});
//open & close
// Wait for the RAW HID port to open.
await port.open();
//main source:
//https://github.com/PaulStoffregen/teensy_loader_cli/blob/master/teensy_loader_cli.c
let block_size = 512;
for(let addr = 0; addr < flashData.data.length; addr += block_size) {
//create addr array
cmd = new Uint8Array(64); //this is the address block, TODO: size depends on device
cmd.fill(0); // clear array
cmd[0] = addr & 0xFF;
cmd[1] = (addr >> 8) & 0xFF;
cmd[2] = (addr >> 16) & 0xFF;
//check if this is the last page, if yes fill with 0xFF
if(addr + block_size > flashData.data.length)
{
data = flashData.data.slice(addr); //take the remaining bit
pad = new Uint8Array(block_size-data.length); //create a new padding array
pad.fill(0xFF);
txx = Uint8Array.from([...cmd, ...data, ...pad]); //concat command, remaining data and padding
console.log("last page");
} else {
data = flashData.data.slice(addr,addr+block_size); //take subarray with blocksize
txx = Uint8Array.from([...cmd, ...data]); //concate command with page data
console.log("page @" + addr);
}
//send the HID block now
await port.sendReport(0,txx);
//wait longer for the first block
if(addr == 0)
{
console.log("First page, wait for 5s");
await waitforme(4500);
}
//wait 0.5s for each block
await waitforme(500);
}
//reboot by setting first 3 bytes to 0xFF
cmd = new Uint8Array(64+block_size); //this is the address block, TODO: size depends on device
cmd.fill(0); // clear array
cmd[0]= cmd[1] = cmd[2] = 0xFF; //reboot command
await port.sendReport(0,cmd);
//finished, close port
gear.classList.remove('spinning');
console.log("finished");
await port.close();
};
readerF.readAsText(file);
}
const uploadForm = document.getElementById('uploadForm');
const resetForm = document.getElementById('resetForm');
const fileInput = document.getElementById('fileInput');
const fileButton = document.getElementById('fileButton');
const fileName = document.getElementById('fileName');
const boardType = document.getElementById('boardType');
const uploadBtn = document.getElementById('uploadBtn');
const log = document.getElementById('log');
const progress = document.getElementById('progress');
const gear = document.getElementById('gear');
uploadForm.addEventListener('submit', handleSubmit, false);
resetForm.addEventListener('submit', handleReset, false);
fileButton.addEventListener('click', () => fileInput.click());
fileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (file) fileName.textContent = file.name;
});
</script>
</body>
</html>