-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash.js
70 lines (58 loc) · 1.66 KB
/
flash.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
const fs = require('fs');
const { SerialPort } = require('serialport');
const port = new SerialPort({
path: 'COM8',
baudRate: 500000,
});
const { ReadlineParser } = require('@serialport/parser-readline')
var rom = fs.readFileSync(process.argv[2]);
console.log(rom.length);
var currAddr = 0;
//var mode = 'compare'; // erase ; compare
var mode = 'erase'; // erase ; compare
const parser = port.pipe(new ReadlineParser({ delimiter: '\r\n' }))
parser.on('data', function(data){
if(data == 'BOOT' || data == 'DONE'){
if(currAddr == rom.length && mode == 'erase'){
currAddr = 0;
mode = 'write';
console.log('erased');
}
if(currAddr == rom.length && mode == 'write'){
currAddr = 0;
mode = 'compare';
console.log('done writing');
}
if(mode == 'erase'){
var cmd = 'WR'+currAddr.toString(16).padStart(4,'0')+'ff\n';
currAddr++;
port.write(cmd);
}
if(mode == 'write'){
var cmd = 'WR'+currAddr.toString(16).padStart(4,'0')+rom[currAddr].toString(16).padStart(2,'0')+'\n';
currAddr++;
port.write(cmd);
}
if(mode == 'compare'){
var cmd = 'RD'+currAddr.toString(16).padStart(4,'0')+'\n';
port.write(cmd);
}
}else{
var red = data.padStart(2,'0');
if(red != rom[currAddr].toString(16).toUpperCase().padStart(2,'0')){
console.log('mismatch at '+currAddr.toString(16),red,rom[currAddr].toString(16).toUpperCase().padStart(2,'0'));
}
if(currAddr < (rom.length-1)){
currAddr++;
var cmd = 'RD'+currAddr.toString(16).padStart(4,'0')+'\n';
port.write(cmd);
}else{
mode = 'finished';
console.log('compare completed');
port.close();
}
}
})
port.on('open', async () => {
console.log('serial port opened');
})