Commit 960adb28 authored by Ryan Day's avatar Ryan Day
Browse files

adding parsed response to CMD_SIGN_ON in sync

parent aebe9ea6
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line

var test = require('tape');
var stk500 = require('../');
var serialport = require('serialport');
var intel_hex = require('intel-hex');
@@ -9,11 +8,13 @@ var data = fs.readFileSync(__dirname+'/../../panda-attack/bootstrap.hex')+'';
var hex = intel_hex.parse(data).data;


var pageSize = 256;
//var pageSize = 256;
var baud = 115200;
var delay1 = 10; //minimum is 2.5us, so anything over 1 fine?
var delay2 = 1;
//var delay1 = 10;
//var delay2 = 1;
/*
var signature = new Buffer([0x1e, 0x98, 0x01]);

var options = {
  timeout:0xc8,
  stabDelay:0x64,
@@ -23,7 +24,7 @@ var options = {
  pollValue:0x53,
  pollIndex:0x03
};

*/

var comName = '/dev/ttyACM0';

@@ -46,6 +47,5 @@ flasher.parser.on('raw',function(buf){
})

flasher.sync(2,function(err,data){
  console.log('callback',err,data)
  console.log('callback',err," ",data)
})
//([0x10, 0xc8, 0x64, 0x19, 0x20, 0x00, 0x53, 0x03, ----  0xac, 0x53, 0x00, 0x00], function(err, resp) {
+4 −2
Original line number Diff line number Diff line
@@ -176,7 +176,7 @@ module.exports = function(serialPort){
        }
        ++this.state;
        // can stk500 send empty messages? probably not. avrdude doesnt support it.
        if(!pkt.len) ++state;
        if(!pkt.len) ++this.state;
        break;
      case 5:
        if(pkt.len === 0 && curByte === c.STATUS_CKSUM_ERROR){
@@ -206,11 +206,13 @@ module.exports = function(serialPort){
        pkt.message = new Buffer(pkt.message);
        this.emit('data',pkt);
        this.state++;// sets state to 7. the parser is not interested in any other bytes until a message is queued.
        pkt.len = pkt.message.length;
        delete pkt.raw;
        this._resolveCurrent(pkt.error?pkt.error:false,pkt);
        break;
      }

      pkt.raw.push(curByte);
      if(pkt.raw) pkt.raw.push(curByte);

    },
    _resolveCurrent:function(err,pkt){
+52 −28
Original line number Diff line number Diff line
@@ -36,6 +36,58 @@ function stk500(port) {
  
};

stk500.prototype.sync = function(attempts, done) {
  var self = this;
  var tries = 1;

  var cmd = new Buffer([CMD_SIGN_ON]);

  attempt();
  function attempt(){
  	tries=tries+1;

  	self.parser.send(cmd, function(error, pkt){


      var res;
      if(!error){
        // message response format for CMD_SIGN_ON
        // 1 CMD_SIGN_ON
        // 1 STATUS_CMD_OK
        // 1 8 - length of sig string
        // 8 the signature string - "STK500_2" or "AVRISP_2"
        var response = pkt.message;

        if(response[0] !== c.CMD_SIGN_ON){
          // something is wrong. look for error in constants. 
          error = new Error('command response was not CMD_SIGN_ON. '+response[0]); 
          error.code = "E_CMD_ERROR";
        } else if(response[1] !== c.STATUS_CMD_OK){
          // malformed. check command status constants and return error
          error = new Error('command status was incorrect. '+response[1]); 
          error.code = "E_CMD_STATUS";
        } else {
          var len = response[2];
          res = response.slice(3)+'';
          if(res.length != len) {
            // something is wroing but all signs point to right,
          }
        }
      }

      if(error && tries<=attempts){
        console.log("failed attempt again");
        return attempt();
      }

      done(error,res);
  	});
  }
};


////// BELOW HERE IS TODO i'm mid refactor.


stk500.prototype.reset = function(delay1, delay2, done){
  console.log("reset");
@@ -73,34 +125,6 @@ stk500.prototype.reset = function(delay1, delay2, done){
  );
};

stk500.prototype.sync = function(attempts, done) {
  console.log("sync");
  var self = this;
  var tries = 1;

  var cmd = new Buffer([CMD_SIGN_ON]);

  attempt();
  function attempt(){
  	tries=tries+1;

  	self.parser.send(cmd, function(error, results){
  		console.log("confirm sync");
      if(error) {
        if(tries<=attempts){
          console.log("failed attempt again");
          attempt();
        }else{
          done(error);
        }
      }else{
        console.log("confirmed sync");
        done(results);
      }
  	});
  }
};


stk500.prototype.verifySignature = function(signature, done) {
  console.log("verify signature");