forked from noopkat/avrgirl-stk500v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avrgirl-stk500v2.js
527 lines (452 loc) · 14.6 KB
/
avrgirl-stk500v2.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
var C = require('./lib/c');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var async = require('async');
var bufferEqual = require('buffer-equal');
var libusb = require('./lib/libusb-comms');
var serialcom = require('./lib/serialport-comms');
var fs = require('fs');
var intelhex = require('intel-hex');
function avrgirlStk500v2(options) {
this.options = {
comm: options.comm || null,
chip: options.chip || null,
debug: options.debug || false,
frameless: options.frameless || false
};
this.device;
this.seq = 0;
this.debug = this.options.debug ? console.log : function() {};
if (this.options.comm.path) {
this.debug('serialport!');
this.commType = 'serialcom';
this.device = new serialcom(this.options.comm);
} else {
this.debug('libusb!');
this.commType = 'libusb';
this.device = new libusb(this.options.comm);
}
EventEmitter.call(this);
this._setupComms();
};
util.inherits(avrgirlStk500v2, EventEmitter);
avrgirlStk500v2.prototype._setupComms = function() {
this.debug('setting up communication interface');
var self = this;
// libusb
this.open();
this.device.setUpInterface(function(error) {
if (!error) {
setImmediate(emitReady, self);
}
});
}
function emitReady(self) {
self.emit('ready');
}
avrgirlStk500v2.prototype.open = function() {
this.device.open();
};
avrgirlStk500v2.prototype.close = function() {
this.device.close();
};
avrgirlStk500v2.prototype.frame = function (buffer) {
var lMSB = buffer.length >> 8;
var lLSB = buffer.length & 0xFF;
var head = new Buffer([0x1B, this.seq, lMSB, lLSB, C.TOKEN]);
var headed = Buffer.concat([head, buffer]);
var checksum = 0;
for (var i = 0; i < headed.length; i += 1) {
checksum ^= headed[i];
}
this.seq += 1;
var framed = Buffer.concat([headed, new Buffer([checksum])]);
return framed;
};
avrgirlStk500v2.prototype.write = function (buffer, callback) {
if (!Buffer.isBuffer(buffer)) {
if (!Array.isArray(buffer)) {
return callback(new Error('Failed to write: data was not Buffer or Array object.'));
}
var buffer = new Buffer(buffer);
}
var data = (this.options.frameless) ? buffer : this.frame(buffer);
this.debug('writing', data);
this.device.write(data, function (error) {
callback(error);
});
};
avrgirlStk500v2.prototype.read = function (length, callback) {
var self = this;
if (typeof length !== 'number') { return callback(new Error('Failed to read: length must be a number.')) }
this.device.read(length, function (error, data) {
//var buffer = (self.options.frameless) ? data : data.slice(6);
var buffer = data || [];
self.debug('read', buffer);
callback(error, buffer);
});
};
avrgirlStk500v2.prototype.sendCmd = function(cmd, callback) {
var self = this;
var frameless = this.options.frameless;
var readLen = frameless ? 2 : 8;
var statusPos = frameless ? 1 : 6;
this.write(cmd, function (error) {
if (error) { return callback(error); }
self.read(readLen, function (error, data) {
if (!error && data && data.length > 0 && data[statusPos] !== C.STATUS_CMD_OK) {
var error = new Error('Return status was not OK. Received instead: ' + data.toString('hex'));
}
callback(error);
});
});
};
avrgirlStk500v2.prototype.getSignature = function (callback) {
var self = this;
var cmd = new Buffer([C.CMD_SIGN_ON]);
var frameless = this.options.frameless;
var readLen = frameless ? 3 : 9;
var statusPos = frameless ? 1 : 6;
var sigPos = frameless ? 3 : 8;
var foot = frameless ? 0 : 1;
this.write(cmd, function (error) {
if (error) { callback(error); }
self.read(20 + readLen, function (error, data) {
if (data[statusPos] !== C.STATUS_CMD_OK) {
error = new Error('Failed to verify: programmer return status was not OK.');
}
var signature = data.slice(sigPos, data.length - foot);
callback(null, signature);
});
});
};
avrgirlStk500v2.prototype.verifySignature = function (sig, data, callback) {
var error = null;
if (!bufferEqual(data, sig)) {
error = new Error('Failed to verify: signature does not match.');
}
callback(error);
};
avrgirlStk500v2.prototype.loadAddress = function (memType, address, callback) {
var dMSB = memType === 'flash' ? 0x80 : 0x00;
var msb = (address >> 24) & 0xFF | dMSB;
var xsb = (address >> 16) & 0xFF;
var ysb = (address >> 8) & 0xFF;
var lsb = address & 0xFF;
var cmd = new Buffer([C.CMD_LOAD_ADDRESS, msb, xsb, ysb, lsb]);
this.sendCmd(cmd, function (error) {
var error = error ? new Error('Failed to load address: return status was not OK.') : null;
callback(error);
});
};
avrgirlStk500v2.prototype.loadPage = function (memType, data, callback) {
if (!Buffer.isBuffer(data)) {
return callback(new Error('Failed to write page: data was not Buffer'));
}
var lMSB = data.length >> 8;
var lLSB = data.length & 0xFF;
var mem = this.options.chip[memType];
var cmd = memType === 'flash' ? C.CMD_PROGRAM_FLASH_ISP : C.CMD_PROGRAM_EEPROM_ISP
var cmd = new Buffer([
cmd,
lMSB, lLSB,
0xC1, mem.delay,
mem.write[0], mem.write[1], mem.write[2],
mem.poll1, mem.poll2
]);
cmd = Buffer.concat([cmd, data]);
this.sendCmd(cmd, function (error) {
return callback(error);
});
};
avrgirlStk500v2.prototype.writeMem = function (memType, hex, callback) {
var self = this;
var options = this.options.chip;
var pageAddress = 0;
var useAddress;
var pageSize = options[memType].pageSize;
var addressOffset = options[memType].addressOffset;
var data;
if (typeof hex === 'string') {
try {
readFile = fs.readFileSync(hex, { encoding: 'utf8' });
} catch (e) {
if (e.code === 'ENOENT') {
return callback(new Error('could not write ' + memType + ': please supply a valid path to a hex file.'));
} else {
return callback(e);
}
}
hex = intelhex.parse(readFile).data;
} else if (!Buffer.isBuffer(hex)) {
return callback(new Error('could not write ' + memType + ': please supply either a hex buffer or a valid path to a hex file.'));
}
async.whilst(
function testEndOfFile() {
// case for data being flashed being less than one page in size
if (pageAddress === 0 && hex.length < pageSize) {
return false;
}
return pageAddress < hex.length;
},
function programPage(pagedone) {
async.series([
function loadAddress(done) {
useAddress = pageAddress >> addressOffset;
self.loadAddress(memType, useAddress, done);
},
function writeToPage(done) {
data = hex.slice(pageAddress, (hex.length > pageSize ? (pageAddress + pageSize) : hex.length - 1))
self.loadPage(memType, data, done);
},
function calcNextPage(done) {
pageAddress = pageAddress + data.length;
done();
}
],
function pageIsDone(error) {
pagedone(error);
});
},
function(error) {
return callback(error);
}
);
};
avrgirlStk500v2.prototype.enterProgrammingMode = function (callback) {
var self = this;
var options = this.options.chip;
var enable = options.pgmEnable;
var cmd = new Buffer([
C.CMD_ENTER_PROGMODE_ISP,
options.timeout, options.stabDelay,
options.cmdexeDelay, options.syncLoops,
options.byteDelay,
options.pollValue, options.pollIndex,
enable[0], enable[1],
enable[2], enable[3]
]);
this.sendCmd(cmd, function (error) {
//var error = error ? new Error('Failed to enter prog mode: programmer return status was not OK.') : null;
var error = error ? new Error(error) : null;
callback(error);
});
};
avrgirlStk500v2.prototype.exitProgrammingMode = function (callback) {
var self = this;
var options = this.options.chip;
var cmd = new Buffer([
C.CMD_LEAVE_PROGMODE_ISP, options.preDelay, options.postDelay
]);
this.sendCmd(cmd, function (error) {
var error = error ? new Error('Failed to leave prog mode: programmer return status was not OK.') : null;
callback(error);
});
};
avrgirlStk500v2.prototype.eraseChip = function (callback) {
var self = this;
var options = this.options.chip;
var erase = options.erase;
var cmd = new Buffer([
C.CMD_CHIP_ERASE_ISP,
erase.delay, options.pollMethod,
erase.cmd[0], erase.cmd[1],
erase.cmd[2], erase.cmd[3]
]);
this.sendCmd(cmd, function (error) {
var error = error ? new Error('Failed to erase chip: programmer return status was not OK.') : null;
callback(error);
});
};
avrgirlStk500v2.prototype.writeFlash = function (hex, callback) {
// optional convenience method
this.writeMem('flash', hex, function(error) {
return callback(error);
});
};
avrgirlStk500v2.prototype.writeEeprom = function (hex, callback) {
// optional convenience method
this.writeMem('eeprom', hex, function(error) {
callback(error);
});
};
avrgirlStk500v2.prototype.quickFlash = function (hex, callback) {
var self = this;
async.series([
self.enterProgrammingMode.bind(self),
self.writeFlash.bind(self, hex),
self.exitProgrammingMode.bind(self)
], function (error) {
return callback(error);
}
);
};
avrgirlStk500v2.prototype.quickEeprom = function (hex, callback) {
var self = this;
async.series([
self.enterProgrammingMode.bind(self),
self.writeEeprom.bind(self, hex),
self.exitProgrammingMode.bind(self)
], function (error) {
return callback(error);
}
);
};
avrgirlStk500v2.prototype.readFlash = function (length, callback) {
// optional convenience method
this.readMem('flash', length, function(error, data) {
callback(error, data);
});
};
avrgirlStk500v2.prototype.readEeprom = function (length, callback) {
// optional convenience method
this.readMem('eeprom', length, function(error, data) {
callback(error, data);
});
};
avrgirlStk500v2.prototype.readMem = function (memType, length, callback) {
var self = this;
var options = this.options.chip;
var headLen = this.options.frameless ? 3 : 6;
var cmd = memType === 'flash' ? C.CMD_READ_FLASH_ISP : C.CMD_READ_EEPROM_ISP
var buf = new Buffer([
cmd,
length >> 8, length,
options[memType].read[0]
]);
this.write(buf, function (error) {
var error = error ? new Error('Failed to initiate read memory: programmer return status was not OK.') : null;
if (error) { return callback(error, null); }
self.read(length + headLen, function(error, data) {
var error = error ? new Error('Failed to read memory: programmer return status was not OK.') : null;
callback(error, data);
});
});
};
avrgirlStk500v2.prototype.getChipSignature = function (callback) {
var self = this;
var options = this.options.chip;
var signature = options.signature;
var signatureLength = signature.size;
var frameless = this.options.frameless;
var readLen = frameless ? 4 : 10;
var statusPos = frameless ? 1 : 6;
var sigPos = frameless ? 2 : 7;
var set = 0;
var cmd = new Buffer([
C.CMD_READ_SIGNATURE_ISP,
signature.startAddress,
signature.read[0], signature.read[1],
signature.read[2], signature.read[3]
]);
var response = new Buffer(3);
function getSigByte() {
self.write(cmd, function (error) {
if (error) { return callback(error); }
self.read(readLen, function(error, data) {
if (error) { return callback(error); }
response[set] = data[sigPos];
set += 1;
cmd[4] = set;
if (set < signatureLength) {
getSigByte();
} else {
callback(null, response);
}
});
});
};
getSigByte();
};
avrgirlStk500v2.prototype.cmdSpiMulti = function (options, callback) {
// // P02
//options are [cmd, numTx, numRx, rxStartAddr, txData]
};
avrgirlStk500v2.prototype.readFuses = function (callback) {
var self = this;
var chip = this.options.chip;
var fuses = chip.fuses;
var reads = Object.keys(fuses.read);
var fusePos = (this.options.frameless) ? 2 : 7;
var response = {};
async.eachSeries(reads, function iterator(item, cb) {
self.readFuse(item, function(error, data) {
if (error) { return callback(new Error(error), null); }
response[item] = data;
cb();
});
}, function done() {
callback(null, response);
});
};
avrgirlStk500v2.prototype.readFuse = function (fuseType, callback) {
if ((typeof fuseType).toLowerCase() !== 'string') {
return callback(new Error('Failed to read fuse: fuse type should be a string'));
}
var self = this;
var chip = this.options.chip;
var fuse = chip.fuses.read[fuseType];
var readLen = (this.options.frameless) ? 4 : 10;
var fusePos = (this.options.frameless) ? 2 : 7;
var cmd = new Buffer([
C.CMD_READ_FUSE_ISP,
chip.fuses.startAddress
]);
var cmdf = Buffer.concat([cmd, new Buffer(fuse)], cmd.length + fuse.length);
self.write(cmdf, function (error) {
if (error) { callback(error); }
self.read(readLen, function(error, data) {
if (error) { callback(error); }
response = new Buffer([data[fusePos]]);
callback(null, response);
});
});
};
avrgirlStk500v2.prototype.writeFuse = function (fuseType, value, callback) {
var self = this;
var options = this.options.chip;
var frameless = this.options.frameless;
var readLen = frameless ? 3 : 9;
var statusPos = frameless ? 1 : 6;
var fuseCmd = options.fuses.write[fuseType];
var cmd = new Buffer([
C.CMD_PROGRAM_FUSE_ISP,
fuseCmd[0], fuseCmd[1],
fuseCmd[2], value
]);
this.write(cmd, function (error) {
if (error) { callback(error); }
self.read(readLen, function (error, data) {
if (data[statusPos] !== C.STATUS_CMD_OK) {
error = new Error('Failed to program fuse: programmer return status was not OK.');
}
callback(null);
});
});
};
avrgirlStk500v2.prototype.setParameter = function (param, value, callback) {
var cmd = new Buffer([
C.CMD_SET_PARAMETER,
param, value
]);
this.sendCmd(cmd, function (error) {
var error = error ? new Error('Failed to set parameter: programmer return status was not OK.') : null;
callback(error);
});
};
avrgirlStk500v2.prototype.getParameter = function (param, callback) {
var self = this;
var cmd = new Buffer([
C.CMD_GET_PARAMETER,
param
]);
this.write(cmd, function (error) {
var error = error ? new Error('Failed to get parameter: programmer return status was not OK.') : null;
if (error) { return callback(error, null); }
self.read(8, function(error, data) {
var error = error ? new Error('Failed to get parameter: programmer return status was not OK.') : null;
callback(error, data);
});
});
};
module.exports = avrgirlStk500v2;