-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathchip.avr.avr109.js
250 lines (211 loc) · 6.33 KB
/
chip.avr.avr109.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
var
intelHex = require('intel-hex'),
Stream = require('stream').Stream,
util = require('util');
var out = module.exports = {};
var d = function(c) {
return (c + '').charCodeAt(0);
};
out.Flasher = function(serialport, options) {
var that = this;
this.options = options || {};
this.sp = serialport;
this.signature = this.options.signature || 'LUFACDC';
if (this.options.debug) {
this.sp.on('data', function(d) {
process.stdout.write(' -> ');
for (var i=0; i<d.length; i++) {
var c = d.toString().substring(i, i+1);
if (c.charCodeAt(0) < 32 || c.charCodeAt(0) > 126) {
c = '.';
}
process.stdout.write(c + ' [' + d.readUInt8(i).toString(16) + '] ');
}
process.stdout.write('\n');
});
}
this.c = function(value, fn, expectedResponseLength) {
that.cmds.push({
value : value,
callback : function(data) {
fn && fn(data);
},
expectedResponseLength: expectedResponseLength
});
return this;
}
this.flashChunkSize = 0;
this.bytes = [];
this.cmds = [];
};
out.Flasher.prototype = {
run : function(fn) {
var that = this;
process.nextTick(function() {
if (that.running) { return; }
var cmd = that.cmds.shift();
if (cmd) {
running = true;
that.options.debug && process.stdout.write('Send: ' + cmd.value);
var response = new Buffer(0);
var onData = function(d) {
response = Buffer.concat([
response,
d
]);
if (cmd.expectedResponseLength === undefined || // optional expected length not passed in
cmd.expectedResponseLength <= response.length) {
that.sp.removeListener('data', onData);
that.running = false;
cmd.callback(response);
process.nextTick(function() {
if (that.cmds.length > 0) {
that.run(fn);
} else {
fn && fn();
}
});
}
};
that.sp.on('data', onData);
that.sp.write(cmd.value);
}
});
},
prepare : function(fn) {
var that = this;
this.c('S', function(d) {
if (d.toString() !== that.signature) {
fn(new Error('Invalid device signature; expecting: ' + that.signature + ' received: ' + d.toString()));
}
})
.c('V')
.c('v')
.c('p')
.c('a')
.c('b', function(d) {
if ((d.toString() || 'X')[0] != 'Y') {
fn(new Error('Buffered memory access not supported.'));
}
that.flashChunkSize = d.readUInt16BE(1);
})
.c('t')
.c('TD')
.c('P')
.c('F')
.c('F')
.c('F')
.c('N')
.c('N')
.c('N')
.c('Q')
.c('Q')
.c('Q')
.c([d('A'), 0x03, 0xfc])
.c([d('g'), 0x00, 0x01, d('E')])
.c([d('A'), 0x03, 0xff])
.c([d('g'), 0x00, 0x01, d('E')])
.c([d('A'), 0x03, 0xff])
.c([d('g'), 0x00, 0x01, d('E')])
.c([d('A'), 0x03, 0xff])
.c([d('g'), 0x00, 0x01, d('E')])
this.run(function() {
fn(null, that);
});
},
erase : function(fn) {
this.c('e', function() {
fn && fn();
}) // erase
this.run();
},
program : function(fullString, fn) {
var
that = this,
converter,
bytes = [];
this.totalBytes = 0;
this.c([d('A'), 0x00, 0x00], function() {
converter = intelHex.parse(fullString);
that.totalBytes = converter.data.length;
// buffer the bytes so we can push them in the expected size on 'end'
Array.prototype.push.apply(bytes, converter.data);
// copy this array so we can use it strictly for comparing later
that.allBytes = bytes;
that.options.debug && console.log('programming', bytes.length, 'bytes');
that.chunksSent = [];
for (var i=0; i<bytes.length; i+=that.flashChunkSize) {
var chunk = Array.prototype.slice.call(converter.data.slice(i, i+that.flashChunkSize));
that.chunksSent.push(chunk);
that.c([d('B'), (chunk.length >> 8) & 0xFF, chunk.length & 0xFF, d('F')].concat(chunk));
}
});
this.run(function() { fn && fn() });
},
verify : function(fn) {
var that = this;
// compare flash on device with the chunks we sent
this.c([d('A'), 0x00, 0x00], function() {
var
index = 0,
compare = function(deviceData) {
var error = null;
index++;
if (!that.allBytes.length) {
fn && fn();
return;
}
var deviceDataLength = deviceData.length;
var localChunk = that.allBytes.splice(0, deviceDataLength);
// iterate through the bytes sent to compare with the latest bytes received
localChunk.forEach(function(val, idx) {
if (val !== deviceData.readUInt8(idx)) {
error = new Error('Firmware on the device does not match local data');
}
});
if (error) {
return fn(error);
}
process.nextTick(function() {
var readSize = that.flashChunkSize;
that.options.debug && console.log(that.totalBytes - index*that.flashChunkSize);
if (that.totalBytes - index*that.flashChunkSize < that.flashChunkSize) {
readSize = that.totalBytes - index*that.flashChunkSize;
}
that.c([d('g'), (readSize >> 8) & 0xFF, readSize & 0xFF, d('F')], compare, readSize);
that.run();
});
};
that.options.debug && console.log('\n\nVerifying flash..')
that.c([d('g'), (that.flashChunkSize >> 8) & 0xFF, that.flashChunkSize & 0xFF, d('F')], compare, that.flashChunkSize);
that.run();
});
that.run();
},
fuseCheck : function(fn) {
this.options.debug && console.log('checking fuses');
// fuse check
this.c('F')
.c('F')
.c('F')
.c('N')
.c('N')
.c('N')
.c('Q')
.c('Q')
.c('Q')
.c('L')
.c('E');
this.run(function() {
fn();
});
}
};
out.init = function(serialport, options, fn) {
if (typeof options === 'function' && !fn) {
fn = options;
options = {};
}
var flasher = new out.Flasher(serialport, options);
flasher.prepare(fn);
};