-
Notifications
You must be signed in to change notification settings - Fork 3
/
flash.js
executable file
·161 lines (139 loc) · 3.54 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
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
#!//usr/bin/env node
const fs = require('fs');
const SerialPort = require('serialport');
const SamBA = require('./sam_ba');
let argv = require('yargs')
.demand(['port'])
.alias('p', 'port')
.describe('port', 'path to device to update')
.describe('bin', 'path to *.bin file')
.alias('b', 'bin')
.boolean('boot')
.default('boot', true)
.alias('B', 'boot')
.describe('boot', 'set the board to boot to flash (--no-boot to not)')
.boolean('reset')
.default('reset', true)
.alias('R', 'reset')
.describe('reset', 'reset the board (after everything else)')
.count('debug')
.alias('D', 'debug')
.describe('debug', 'turn up the verbosity (a lot!)')
.help('h')
.alias('h', 'help')
.argv;
let options = {
baudRate: 921600,
flowcontrol: ['RTSCTS'],
};
let port = argv.port || '/dev/tty.usbmodem1422';
let sp = new SerialPort(port, options);
let samBa = new SamBA(sp, argv.debug);
const EefcFlash = require('./eefc');
let eefc = null;
let processPromise = new Promise((done, fail)=>{
sp.once('open', ()=> {
samBa.init()
.then(()=>{
eefc = EefcFlash.EEFCFactory(samBa, samBa.chipId);
return eefc.init();
})
.then(()=>done())
.catch((e)=>fail(e));
});
});
let doingSomething = false;
if (argv.bin !== undefined) {
doingSomething = true;
processPromise = processPromise
.then(()=>{
return writeBin(argv.bin);
});
}
if (argv.boot === undefined || argv.boot === true) {
doingSomething = true;
processPromise = processPromise
.then(()=>{
console.log('setting boot-from-flash!');
return eefc.setBoot(true);
});
}
if (argv.reset === undefined || argv.reset === true) {
doingSomething = true;
processPromise = processPromise
.then(()=>{
console.log('resetting the board');
return eefc.reset();
});
}
if (doingSomething) {
processPromise = processPromise
.then(()=>{
console.log('done!');
sp.close();
}).catch((e)=>{
console.log(`FAILED: ${e}`);
sp.close();
});
}
// global store of the firmware data -- eww, I know.
let firmwareData = null;
/**
* writeBin
* @param {path} firmwareBin path to the firmware .bin file
* @return {Promise} promise to be resolved when it's done writing
*/
function writeBin(firmwareBin) {
return new Promise((resolve, fail)=>{
fs.readFile(firmwareBin, (err, data) => {
if (err) {
return fail(err);
}
firmwareData = data;
resolve();
});
})
.then(()=>{
return eefc.erase();
})
.then(()=>{
return writePage(0);
});
};
/**
* writePage
* @param {number} page page number to write to
* @return {Promise} promise to write that page
*/
function writePage(page) {
if ((page * eefc.size) < firmwareData.length) {
console.log(`writing page ${page+1} with data from` +
` ${page * eefc.size} to ` +
`${Math.min(((page+1) * eefc.size), firmwareData.length)-1} out of ` +
`${firmwareData.length} bytes.`);
let pageData = firmwareData.slice(page * eefc.size, (page+1) * eefc.size);
return eefc.writePage(page, pageData)
// .then((changed)=>{
// return new Promise((f)=>{
// setTimeout(()=>{
// f(changed);
// }, 10);
// });
// })
// .then(()=> {
// return waitForDone();
// })
.then(()=>{
return eefc.verifyPage(page, pageData);
})
.then((passed)=>{
if (passed) {
return writePage(page+1);
}
console.log('VERIFY FAILED!');
return Promise.reject();
});
} else {
return Promise.resolve();
}
}