Skip to content

Commit

Permalink
Convert flash to return a promise
Browse files Browse the repository at this point in the history
  • Loading branch information
majgis committed Aug 1, 2016
1 parent 9e79635 commit 3be9418
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 44 deletions.
76 changes: 40 additions & 36 deletions lib/flash.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function factoryPartition(mac1, mac2) {
);
}

function transaction(usbConnection, write, read, status_poll, wren, next) {
function transaction (usbConnection, write, read, status_poll, wren, next) {
read = read || 0;
status_poll = status_poll || false;
wren = wren || false;
Expand Down Expand Up @@ -69,8 +69,8 @@ function eraseChip(usbConnection, next) {
}

// Poll for the WIP bit in the status register to go low
function waitTransactionComplete(usbConnection, next) {
setTimeout(function onWait() {
function waitTransactionComplete (usbConnection, next) {
setTimeout(function onWait () {
getStatus(usbConnection, (err, status) => {
if (err) {
return next(err);
Expand Down Expand Up @@ -138,39 +138,43 @@ function randbyte() {
return randint(0, 255);
}

function asyncLog(msg, next) {
log.info(msg);
next();
function asyncLog(msg) {
return function onAsyncLog(next) {
log.info(msg);
next();
}
}

module.exports = function flashDevice(usbConnection, ubootBuffer, squashfsBuffer, next) {
var uid = [randbyte(), randbyte(), randbyte(), randbyte()];
var mac1 = [0x02, 0xa3].concat(uid);
var mac2 = [0x02, 0xa4].concat(uid);

async.waterfall([
asyncLog.bind(null, 'Checking the chip id...'),
readChipId.bind(null, usbConnection),
checkChipId,

asyncLog.bind(null, 'Erasing the chip...'),
setWriteEnabled.bind(null, usbConnection),
eraseChip.bind(null, usbConnection),
waitTransactionComplete.bind(null, usbConnection),

asyncLog.bind(null, 'Writing uboot...'),
write.bind(null, usbConnection, 0, ubootBuffer),
waitTransactionComplete.bind(null, usbConnection),

asyncLog.bind(null, 'Writing mediatek factory partition...'),
write.bind(null, usbConnection, 0x40000, factoryPartition(mac1, mac2)),
waitTransactionComplete.bind(null, usbConnection),

asyncLog.bind(null, 'Writing squashfs...'),
write.bind(null, usbConnection, 0x50000, squashfsBuffer),
waitTransactionComplete.bind(null, usbConnection),
asyncLog.bind(null, 'The update was successful.'),
asyncLog.bind(null, 'Please power cycle your Tessel.')

], next);
module.exports = function flashDevice(usbConnection, ubootBuffer, squashfsBuffer) {
return new Promise((resolve, reject)=> {
var uid = [randbyte(), randbyte(), randbyte(), randbyte()];
var mac1 = [0x02, 0xa3].concat(uid);
var mac2 = [0x02, 0xa4].concat(uid);

async.waterfall([
asyncLog('Checking the chip id...'),
readChipId.bind(null, usbConnection),
checkChipId,

asyncLog('Erasing the chip...'),
setWriteEnabled.bind(null, usbConnection),
eraseChip.bind(null, usbConnection),
waitTransactionComplete.bind(null, usbConnection),

asyncLog('Writing uboot...'),
write.bind(null, usbConnection, 0, ubootBuffer),
waitTransactionComplete.bind(null, usbConnection),

asyncLog('Writing mediatek factory partition...'),
write.bind(null, usbConnection, 0x40000, factoryPartition(mac1, mac2)),
waitTransactionComplete.bind(null, usbConnection),

asyncLog('Writing squashfs...'),
write.bind(null, usbConnection, 0x50000, squashfsBuffer),
waitTransactionComplete.bind(null, usbConnection),
asyncLog('The update was successful.'),
asyncLog('Please power cycle your Tessel.')

], (err) => err ? reject(err) : resolve());
})
};
14 changes: 6 additions & 8 deletions lib/tessel/restore.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@ var update = require('../update-fetch');
var log = require('../log');
var flash = require('../flash');

Tessel.prototype.restore = function restore() {
Tessel.prototype.restore = function restore () {
var usbConnection = this.connection;
return new Promise((resolve, reject) => {

log.info('Proceeding with updating OpenWrt...');

// download images
return update.fetchRestore()
return update
.fetchRestore()
.then((result) => {
flash(usbConnection, result.uboot, result.squashfs, (err) => {
if (err) {
return reject(err);
}
resolve();
});
flash(usbConnection, result.uboot, result.squashfs)
.then(resolve)
.catch(reject)
});
});
};

0 comments on commit 3be9418

Please sign in to comment.