flashmagic.js
Version:
NXP LPC Microprocessor Programmer
54 lines (53 loc) • 2.12 kB
JavaScript
;
var Symbol = require('es6-symbol');
var Utilities = require('./Utilities');
var UUDecoder_1 = require('./UUDecoder');
var _addressSym = Symbol();
var MemoryReader = (function () {
function MemoryReader(isp) {
this.isp = isp;
}
MemoryReader.prototype.readFully = function (block) {
var _this = this;
var count = Utilities.alignCount(block.length);
return this.isp.sendCommand("R " + block.address + " " + count)
.then(function () { return _this.downloadChunk(block.length); });
};
MemoryReader.prototype.downloadChunk = function (length) {
var _this = this;
return new Promise(function (resolve, reject) {
var uud = new UUDecoder_1.UUDecoder();
var isp = _this.isp;
var lineCount = 0;
var index = 0;
var buffer = new Buffer(0);
(function loop() {
if (lineCount === Utilities.LINES_PER_UUENCODED_CHUNK || index >= length) {
isp.assert(new RegExp(uud.checksum.toString()))
.then(function () { return isp.sendLine('OK'); })
.then(function () {
uud.reset();
lineCount = 0;
if (index < length) {
process.nextTick(loop);
}
else {
resolve(buffer);
}
}).catch(function (error) { return reject(error); });
}
else {
isp.read().then(function (data) {
var b = uud.decode(data);
buffer = Buffer.concat([buffer, b], buffer.length + b.length);
index += b.length;
lineCount++;
process.nextTick(loop);
}).catch(function (error) { return reject(error); });
}
})();
});
};
return MemoryReader;
}());
exports.MemoryReader = MemoryReader;