Skip to content

Commit

Permalink
some very rough serialport support
Browse files Browse the repository at this point in the history
  • Loading branch information
noopkat committed Aug 8, 2015
1 parent 6dae597 commit 10554d5
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 10 deletions.
30 changes: 20 additions & 10 deletions avrgirl-stk500v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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');

function avrgirlStk500v2(options) {
this.options = {
Expand All @@ -13,15 +14,19 @@ function avrgirlStk500v2(options) {
frameless: options.frameless || false
};

this.device;
this.seq = 0;
this.debug = this.options.debug ? console.log : function() {};

// if (this.options.comm.deviceDescriptor && typeof this.options.comm.deviceDescriptor === 'object') {
// this.commType = 'libusb';
// }

// evenutally wrap the following line with the conditional above
this.device = new libusb(this.options.comm);
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();
Expand Down Expand Up @@ -80,6 +85,8 @@ avrgirlStk500v2.prototype.write = function (buffer, callback) {

var data = (this.options.frameless) ? buffer : this.frame(buffer);

this.debug('writing', data);

this.device.write(data, function (error) {
callback(error);
});
Expand All @@ -90,7 +97,9 @@ avrgirlStk500v2.prototype.read = function (length, callback) {
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);
callback(error, data);
var buffer = data || [];
self.debug('read', buffer);
callback(error, buffer);
});
};

Expand All @@ -103,7 +112,7 @@ avrgirlStk500v2.prototype.sendCmd = function(cmd, callback) {
this.write(cmd, function (error) {
if (error) { return callback(error); }
self.read(readLen, function (error, data) {
if (!error && data.length > 0 && data[statusPos] !== C.STATUS_CMD_OK) {
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);
Expand All @@ -127,7 +136,7 @@ avrgirlStk500v2.prototype.getSignature = function (callback) {
error = new Error('Failed to verify: programmer return status was not OK.');
}
var signature = data.slice(sigPos, data.length - foot);
callback(error, signature);
callback(null, signature);
});
});
};
Expand Down Expand Up @@ -238,7 +247,8 @@ avrgirlStk500v2.prototype.enterProgrammingMode = function (callback) {
]);

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('Failed to enter prog mode: programmer return status was not OK.') : null;
var error = error ? new Error(error) : null;
callback(error);
});
};
Expand Down
90 changes: 90 additions & 0 deletions lib/serialport-comms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
var async = require('async');

function serialCom (port) {
var self = this;
this.device = port;
this.responses = [];
var devMode = true;
this.debug = devMode ? console.log : function() {};

};

serialCom.prototype.open = function() {
this.device.open();
};

serialCom.prototype.setUpInterface = function(callback) {
var self = this;
this.device.open(function() {
self.device.on('data', function(data) {
self.debug('data', data)
self.responses.push(data);
});
self.sync(function(error) {
self.debug('sync success', error);
return callback(error);
});
});
};

serialCom.prototype.close = function() {
this.responses = [];
this.device.close();
};

serialCom.prototype.write = function(data, callback) {
var self = this;
// this will need to swap between 200 and 1000 depending on the command
var drainDelay = 200;

this.device.write(data, function(error, results) {
if (error) { return callback(new Error(error)); }
self.device.drain(function() {
setTimeout(callback, drainDelay);
});
});
};

serialCom.prototype.read = function(length, callback) {
//this.debug(this.responses);
var packet = this.responses.splice(0, length);
return callback(null, packet[0]);
};

serialCom.prototype.sync = function(callback) {
var self = this;
var attempts = 0;
var signon = new Buffer([0x1b, 0x03, 0x00, 0x0b, 0x0e, 0x01, 0x00, 0x08, 0x41, 0x56, 0x52, 0x49, 0x53, 0x50, 0x5f, 0x32, 0x76]);
var drainDelay = 1000;

function check() {
self.read(17, function(error, data) {
attempts += 1;
if (!data) {
if (attempts < 10) {
self.debug("trying again")
trySync();
} else {
self.debug('failure')
callback(new Error('attempt to sync with programmer failed.'));
}
} else {
self.debug('success');
callback(null, data);
}
});
}

function trySync() {
self.device.write(signon, function(error, results) {
if (error) { return callback(new Error(error)); }
self.device.drain(function() {
setTimeout(check, 10);
});
});
};

trySync();
};

module.exports = serialCom;

0 comments on commit 10554d5

Please sign in to comment.