Created
November 2, 2016 03:58
-
-
Save ril3y/cfad8354105e433263d8ed7c3dd55ed5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var serialport = require("serialport") | |
| var color = require("colors") | |
| var EventEmitter = require('events').EventEmitter; | |
| var util = require('util'); | |
| var q = require('q'); | |
| var fs = require('fs'); | |
| const commandLineArgs = require("command-line-args"); | |
| /* | |
| on("read") can then call needsWrite(), and needsWrite(), and on('written') can call read() | |
| I'm in a call too | |
| IOW: read(), later on('readDone') calls write(), later on('writeDone') calls read() | |
| eventually write() calls promise.fulfill() | |
| you don't *need* promises. events are good for what events do. | |
| promise to open port | |
| promise to flash board | |
| use events (functions) inside of the flash_board | |
| */ | |
| CHIP_TABLE = { | |
| // SAM3X | |
| 0x286e0a60: { | |
| 'name': "ATSAM3X8H", | |
| 'address': 0x80000, | |
| 'pages': 2048, | |
| 'size': 256, | |
| 'planes': 2, | |
| 'lock_regions': 32, | |
| 'user': 0x20001000, | |
| 'stack': 0x20010000, | |
| 'registers_addr': 0x400e0a00, | |
| 'can_brownout': false | |
| }, | |
| 0x285e0a60: { | |
| 'name': "ATSAM3X8E", | |
| 'address': 0x80000, | |
| 'pages': 2048, | |
| 'size': 256, | |
| 'planes': 2, | |
| 'lock_regions': 32, | |
| 'user': 0x20001000, | |
| 'stack': 0x20010000, | |
| 'registers_addr': 0x400e0a00, | |
| 'can_brownout': false | |
| }, | |
| 0x284e0a60: { | |
| 'name': "ATSAM3X8C", | |
| 'address': 0x80000, | |
| 'pages': 2048, | |
| 'size': 256, | |
| 'planes': 2, | |
| 'lock_regions': 32, | |
| 'user': 0x20001000, | |
| 'stack': 0x20010000, | |
| 'registers_addr': 0x400e0a00, | |
| 'can_brownout': false | |
| } | |
| } | |
| var optionDefinitions = [ | |
| { name: 'port', alias:"p", | |
| file: 'file', alias: "f"} | |
| //{ name: 'src', type: String, multiple: true, defaultOption: true }, | |
| //{ name: 'timeout', alias: 't', type: Number } | |
| ] | |
| var options = commandLineArgs(optionDefinitions); | |
| var atmel_bootloader_Options = { | |
| baudRate: 1200, | |
| flowcontrol: ['RTSCTS'], | |
| // Provide our own custom parser: | |
| timedSendsOnly: false | |
| }; | |
| var atmel_flashing_Options = { | |
| baudRate: 115200, | |
| flowcontrol: ['RTSCTS'], | |
| // Provide our own custom parser: | |
| timedSendsOnly: false | |
| }; | |
| // var PORT = "/dev/cu.usbmodem14221" | |
| var PORT = options.port; | |
| var FILE = options.file; | |
| if(PORT == true) //dumb hack needed for vsc | |
| PORT = '/dev/cu.usbmodem1421'; | |
| FILE = 'tinyg2.bin'; | |
| var enterBootloaderMode = function(){ | |
| return q.Promise(function(resolve, reject, notify){ | |
| var _p = new serialport(PORT, atmel_bootloader_Options) | |
| _p.on('open',function(){ | |
| //Close when we open to kick board into bootloader | |
| console.log("1200 baud Port Opened"); | |
| _p.close(); | |
| }) | |
| _p.on('close', function(){ | |
| console.log("Closed 1200 baud"); | |
| resolve(); | |
| }) | |
| }) | |
| } | |
| var getPort = function(){ | |
| return q.Promise(function(resolve, reject, notify){ | |
| var sProm = enterBootloaderMode().then(function(){ | |
| var s = new serialport(PORT, atmel_flashing_Options) | |
| s.on('open', function(s){ | |
| console.log("Port Opened"); | |
| resolve(this); | |
| }); //end s.on() | |
| }); //then end | |
| }); //end promise | |
| } | |
| var startFlash = function(port){ | |
| var port = port; | |
| var CHIP = CHIP_TABLE[0x284e0a60] | |
| var writeWithPromise = function(port, data){ | |
| self = this; | |
| console.log("WriteWithPromise"); | |
| var deferred = q.defer(); | |
| var _onData = function(data, error){ | |
| if(error){ | |
| deferred.reject(error); | |
| }else{ | |
| console.log("DATA: " + data.toString('hex')); | |
| deferred.resolve(data); | |
| } | |
| } | |
| port.on('data', _onData); | |
| port.write(data); | |
| return deferred.promise; | |
| }// end promise | |
| function pad(num, size) { | |
| var s = "000" + num; | |
| return s.substr(s.length-size); | |
| } | |
| writeWithPromise(port, "N#"); | |
| flag = false; | |
| for(i=0; i<100; i++){ | |
| var line = "o8"+pad(i,4)+",#"; | |
| console.log("Writing: " + line) | |
| var b = Buffer(line); | |
| writeWithPromise(port,b).then(function(){ | |
| console.log("TST"); | |
| flag = true; | |
| }); | |
| } | |
| var _getAddressOfIAP = function(){ | |
| this._writeByte(IAP_POINTER, "IAP_ADDRESS"); | |
| } | |
| } | |
| var _write = function(data, callback){ | |
| fs.write | |
| } | |
| var self = this; | |
| IAP_POINTER = 'w00800008,#'; | |
| // commands = ["N#", get_IAP] | |
| // write_flag = true; | |
| var s = getPort() | |
| s.then(function(val){ | |
| // this.val = port; | |
| // var file = fs.readFile(FILE, function(data){ | |
| // // console.log(data); | |
| // return data; | |
| // }); | |
| startFlash(val); | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment