-
Notifications
You must be signed in to change notification settings - Fork 9
/
USBCOMFlashTx.html
375 lines (375 loc) · 12.5 KB
/
USBCOMFlashTx.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<!DOCTYPE html>
<!-- Created on: 02.01.2021, Author: pvvx -->
<html>
<head>
<!-- style type="text/css"></style -->
<title>TLSR825x USB-COM Flash Writer (TX-SWS only!)</title>
</head>
<body>
<h3>TLSR825x USB-COM Flash Writer v0.1 (TX-SWS only!)</h3>
<div><br>
1. USB-COM: <input type="button" id="butOpen" value="Open">
Baud: <select id="ubaud"><option value="115200">115200</option><option value="230400">230400</option><option value="460800" selected>460800</option><option value="921600">921600</option></select>
Atime: <select id="uatime"><option value="0">0 ms</option><option value="100">100 ms</option><option value="1000">1 sec</option><option value="2000">2 sec</option><option value="3000" selected>3 sec</option><option value="4000">4 sec</option><option value="8000">8 sec</option><option value="16000">16 sec</option></select>
<br><br>
2. Select Firmware: <input type="file" accept=".bin" id="file"/><br><br>
3. <input type="button" id="butWrite" value="Write to Flash" disabled> <span id="swrite">Open COM & Select file</span><br><br>
<input type="button" id="butErase" value="Erase All Flash" disabled> <input type="button" id="butReset" value="Soft Reset MCU" disabled> <input type="button" id="butClrlog" value="Clear log"><br><hr>
</div>
<div id="slog"></div>
</body>
<script type="text/javascript">
"use strict";
//Firmware values
var firmwareArray = null;
var $ = function(id) {
return document.getElementById(id);
}
const connect = $('butOpen');
const fwriter = $('butWrite');
const swrite = $('swrite');
const berase = $('butErase');
const breset = $('butReset');
const ubaud = $('ubaud');
const uatime = $('uatime');
function log(data) {
console.log(data);
$("slog").innerHTML += new Date().toLocaleTimeString() + ": " + data + "<br>";
}
$('butClrlog').onclick = function() {$("slog").innerHTML='';};
function delay(ms) {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms);
});
}
function hex(number, length) {
var str = (number.toString(16)).toUpperCase();
while (str.length < length) str = '0' + str;
return str;
}
function sws_wr_addr(addr, data) {
// log(addr + ':['+data+']');
let d = new Uint8Array(10); // word swire 10 bits = 10 bytes UART
let h = new Uint8Array([0x5a, (addr>>16)&0xff, (addr>>8)&0xff, addr & 0xff, 0x00]);
let pkt = new Uint8Array((data.byteLength+6)*10);
d[0] = 0x80; // start bit byte cmd swire = 1
d[9] = 0xfe; // stop bit swire = 0
h.forEach((el,n) => {
let m = 0x80; // mask bit
let idx = 1;
do {
if((el & m) != 0) d[idx] = 0x80;
else d[idx] = 0xfe;
idx += 1; m >>= 1;
} while(m != 0);
pkt.set(d, n*10);
d[0] = 0xfe; // start bit next byte swire = 0
});
data.forEach((el,n) => {
let m = 0x80; // mask bit
let idx = 1;
do {
if((el & m) != 0) d[idx] = 0x80;
else d[idx] = 0xfe;
idx += 1; m >>= 1;
} while(m != 0);
pkt.set(d, (n+5)*10);
});
// swire stop cmd = 0xff
d.fill(0x80,0,9);
pkt.set(d, (data.byteLength+5)*10);
return pkt;
}
function sws_wr_addr_var2(addr, data) {
// log(addr + ':['+data+']');
let d = new Uint8Array([0xe8,0xef,0xef,0xef,0xef]); // word swire 10 bits = 5 bytes UART
let h = new Uint8Array([0x5a, (addr>>16)&0xff, (addr>>8)&0xff, addr & 0xff, 0x00]);
let pkt = new Uint8Array((data.byteLength+6)*5);
h.forEach((el,n) => {
if((el & 0x80) != 0)
d[0] &= 0x0f
if((el & 0x40) != 0)
d[1] &= 0xe8
if((el & 0x20) != 0)
d[1] &= 0x0f
if((el & 0x10) != 0)
d[2] &= 0xe8
if((el & 0x08) != 0)
d[2] &= 0x0f
if((el & 0x04) != 0)
d[3] &= 0xe8
if((el & 0x02) != 0)
d[3] &= 0x0f
if((el & 0x01) != 0)
d[4] &= 0xe8
pkt.set(d, n*5);
d.fill(0xef, 0); // start bit next byte swire = 0
});
data.forEach((el,n) => {
if((el & 0x80) != 0)
d[0] &= 0x0f
if((el & 0x40) != 0)
d[1] &= 0xe8
if((el & 0x20) != 0)
d[1] &= 0x0f
if((el & 0x10) != 0)
d[2] &= 0xe8
if((el & 0x08) != 0)
d[2] &= 0x0f
if((el & 0x04) != 0)
d[3] &= 0xe8
if((el & 0x02) != 0)
d[3] &= 0x0f
if((el & 0x01) != 0)
d[4] &= 0xe8
pkt.set(d, (n+5)*5);
d.fill(0xef, 0); // start bit next byte swire = 0
});
// swire stop cmd = 0xff
d.fill(0x08,0,5);
d[4]=0xe8;
pkt.set(d, (data.byteLength+5)*5);
return pkt;
}
async function FlashByteCmd(cmd) {
await serialController.write_raw(sws_wr_addr(0x0d, new Uint8Array([0x00]))); // cns low
return await serialController.write_raw(sws_wr_addr(0x0c, new Uint8Array([cmd & 0xff, 0x01]))); // Flash cmd + cns high
}
async function FlashWriteEnable() { // send flash cmd 0x06 write enable to flash
return FlashByteCmd(0x06);
}
async function FlashWakeUp() { // send flash cmd 0xab to wakeup flash
return FlashByteCmd(0xab);
}
async function FlashEraseAll() { // send flash cmd 0x60 erase all flash
return FlashByteCmd(0x60);
}
async function WriteFifo(addr, data) { // send all data to one register (no increment address - fifo mode)
await serialController.write_raw(sws_wr_addr(0x00b3, new Uint8Array([0x80]))); // [0xb3]=0x80 ext.SWS into fifo mode
await serialController.write_raw(sws_wr_addr(addr, data)); // send all data to one register (no increment address - fifo mode)
return await serialController.write_raw(sws_wr_addr(0x00b3, new Uint8Array([0x00]))); // [0xb3]=0x00 ext.SWS into normal(ram) mode
}
async function SectorErase(addr) {
await FlashWriteEnable();
await serialController.write_raw(sws_wr_addr(0x0d, new Uint8Array([0x00]))); // cns low
await serialController.write_raw(sws_wr_addr(0x0c, new Uint8Array([0x20]))); // Flash cmd erase sector
await serialController.write_raw(sws_wr_addr(0x0c, new Uint8Array([(addr >> 16) & 0xff]))); // Faddr hi
await serialController.write_raw(sws_wr_addr(0x0c, new Uint8Array([(addr >> 8) & 0xff]))); // Faddr mi
await serialController.write_raw(sws_wr_addr(0x0c, new Uint8Array([addr & 0xff, 0x01]))); // Faddr lo + cns high
return await delay(300);
}
async function WriteFlashBlk(addr, data) {
await FlashWriteEnable();
await serialController.write_raw(sws_wr_addr(0x0d, new Uint8Array([0x00]))); // cns low
let blk = new Uint8Array(4 + data.byteLength);
blk[0] = 0x02;
blk[1] = (addr >> 16) & 0xff;
blk[2] = (addr >> 8) & 0xff
blk[3] = addr & 0xff;
blk.set(data, 4);
await WriteFifo(0x0c, blk); // send all data to SPI data register
await serialController.write_raw(sws_wr_addr(0x0d, new Uint8Array([0x01]))); // cns high
return await delay(10);
}
async function SoftResetMSU() {
return await serialController.write_raw(sws_wr_addr(0x06f, new Uint8Array([0x20]))); // Soft Reset MCU
}
async function Activate(tim) {
let blk = sws_wr_addr(0x0602, new Uint8Array([0x05])); // CPU stop
let s = 'Reset DTR/RTS (100 ms)';
swrite.innerHTML = s;
log(s);
await serialController.reset(100); // DTR & RTS
console.log('Soft Reset MCU');
await SoftResetMSU();
s = 'Activate ('+(tim/1000.0)+' sec)...';
swrite.innerHTML = s;
log(s);
let t = new Date().getTime();
while(new Date().getTime() - t < tim) {
await serialController.write_raw(blk); // CPU stop
}
await serialController.write_raw(sws_wr_addr(0x00b2, new Uint8Array([55]))); // Set SWS Speed
await serialController.write_raw(blk);
return await FlashWakeUp();
}
//---------------------------------
async function FlashWrite() {
if(firmwareArray == null || connect.opened == false)
return;
let t = new Date().getTime();
await Activate(uatime.value);
log('Write ' + firmwareArray.byteLength + ' bytes in to Flash...');
let len = firmwareArray.byteLength;
let addr = 0;
let sblk = 256;
while(len > 0) {
if((addr & 0x0fff) == 0) {
let s = 'Flash Sector Erase at 0x'+hex(addr, 6);
console.log(s);
swrite.innerHTML = s;
await SectorErase(addr);
}
if(len < sblk) sblk = len;
let s = 'Flash Write '+sblk+' bytes at 0x'+hex(addr, 6);
console.log(s);
swrite.innerHTML = s;
await WriteFlashBlk(addr, new Uint8Array(firmwareArray.slice(addr, addr + sblk)));
addr += sblk;
len -= sblk;
}
swrite.innerHTML = 'ok';
log('Done ('+((new Date().getTime()-t)/1000.0)+' sec).');
log('Soft Reset MCU');
return await SoftResetMSU();
}
async function DeviceStop() {
return serialController.close();
}
connect.addEventListener('pointerdown', () => {
if(connect.value == 'Close') {
DeviceStop();
} else {
serialController.init();
}
});
fwriter.onclick = async function() {
fwriter.disabled = true;
berase.disabled = true;
breset.disabled = true;
connect.disabled = true;
await FlashWrite();
connect.disabled = false;
berase.disabled = false;
breset.disabled = false;
fwriter.disabled = false;
};
berase.onclick = async function() {
fwriter.disabled = true;
berase.disabled = true;
breset.disabled = true;
connect.disabled = true;
await Activate(uatime.value);
swrite.innerHTML = 'Erased!';
log('Flash Erase All (3.5 sec)...');
await FlashWriteEnable();
await FlashEraseAll();
await delay(3500);
log('Done.');
connect.disabled = false;
berase.disabled = false;
breset.disabled = false;
if(connect.opened && firmwareArray != null)
fwriter.disabled = false;
}
breset.onclick = async function() {
fwriter.disabled = true;
berase.disabled = true;
breset.disabled = true;
connect.disabled = true;
await Activate(uatime.value);
log('Soft Reset MCU');
swrite.innerHTML = '';
await SoftResetMSU();
log('Done.');
connect.disabled = false;
berase.disabled = false;
breset.disabled = false;
if(connect.opened && firmwareArray != null)
fwriter.disabled = false;
}
window.onload = function() {
document.querySelector("#file").addEventListener("change", function() {
var reader = new FileReader();
reader.onload = function() {
firmwareArray = this.result;
log("File was selected, size: " + firmwareArray.byteLength + " bytes");
if(firmwareArray.byteLength < 16 || (new Uint32Array(firmwareArray.slice(8,12)))[0] != 0x544C4E4B){
log("Select file is no telink firmware .bin");
alert("Select file is no telink firmware .bin");
firmwareArray = null;
return;
}
if(connect.opened) {
fwriter.disabled = false;
swrite.innerHTML = '';
}
}
if (this.files[0] != null)
reader.readAsArrayBuffer(this.files[0]);
else
log("No file selected");
}, false);
}
//---------------------------------
class SerialController {
async init(init_cb) {
if ('serial' in navigator) {
try {
connect.disabled = true;
this.port = await navigator.serial.requestPort();
await this.port.open({baudRate: ubaud.value, baudrate: ubaud.value,
bufferSize: 240, buffersize: 240});
this.writer = this.port.writable.getWriter();
console.log('DTR, RTS on.');
await this.port.setSignals({dataTerminalReady: false, requestToSend: false});
connect.value = 'Close';
log('USB-COM opened.');
connect.opened = true;
berase.disabled = false;
breset.disabled = false;
if(firmwareArray != null) {
fwriter.disabled = false;
swrite.innerHTML = '';
}
if (typeof init_cb == 'function') await init_cb(this.port);
}
catch (err) {
log('There was an error opening the serial port: ' + err);
berase.disabled = true;
breset.disabled = true;
fwriter.disabled = true;
}
connect.disabled = false;
}
else {
log('There was an error opening the serial port: ' + err);
log('Web serial doesn\'t seem to be enabled in your browser. Try enabling it by visiting:');
log('chrome://flags/#enable-experimental-web-platform-features');
log('opera://flags/#enable-experimental-web-platform-features');
log('edge://flags/#enable-experimental-web-platform-features');
alert('Web serial doesn\'t seem to be enabled in your browser. Try enabling it by visiting:\r\nchrome://flags/#enable-experimental-web-platform-features\r\nopera://flags/#enable-experimental-web-platform-features\r\nedge://flags/#enable-experimental-web-platform-features');
connect.disabled = false;
connect.opened = false;
berase.disabled = true;
breset.disabled = true;
fwriter.disabled = true;
}
}
async write_raw(data) {
return await this.writer.write(data);
}
async reset(t_ms) {
console.log('DTR, RTS off.');
await this.port.setSignals({dataTerminalReady: true, requestToSend: true});
await delay(t_ms);
console.log('DTR, RTS on.');
return await this.port.setSignals({dataTerminalReady: false, requestToSend: false});
}
async close() {
fwriter.disabled = true;
swrite.innerHTML = '';
await this.writer.close();
await this.port.close();
connect.value = 'Open';
connect.opened = false;
fwriter.disabled = true;
berase.disabled = true;
breset.disabled = true;
log('USB-COM closed.');
connect.disabled = false;
}
}
var serialController = new SerialController();
</script>
</html>