|
@@ -2,7 +2,10 @@
|
|
|
|
|
|
const fs = require("fs");
|
|
const fs = require("fs");
|
|
const SerialPort = require("serialport").SerialPort;
|
|
const SerialPort = require("serialport").SerialPort;
|
|
|
|
+const log = require("./logger");
|
|
const slip = require("./streams/slip");
|
|
const slip = require("./streams/slip");
|
|
|
|
+const delay = require("./utilities").delay;
|
|
|
|
+const repeatPromise = require("./utilities").repeatPromise;
|
|
|
|
|
|
|
|
|
|
// ../esptool.py --port /dev/cu.SLAB_USBtoUART --baud 115200 \
|
|
// ../esptool.py --port /dev/cu.SLAB_USBtoUART --baud 115200 \
|
|
@@ -40,24 +43,6 @@ const SYNC_FRAME = new Buffer([0x07, 0x07, 0x12, 0x20,
|
|
const FLASH_BLOCK_SIZE = 0x400;
|
|
const FLASH_BLOCK_SIZE = 0x400;
|
|
const SUCCESS = [0x01, 0x01];
|
|
const SUCCESS = [0x01, 0x01];
|
|
|
|
|
|
-var debug = function() {};
|
|
|
|
-
|
|
|
|
-function delay(time) {
|
|
|
|
- return new Promise((resolve) => {
|
|
|
|
- debug("Delaying for", time);
|
|
|
|
- setTimeout(resolve, time);
|
|
|
|
- });
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-function repeatPromise(times, callback) {
|
|
|
|
- let chain = Promise.resolve();
|
|
|
|
- for (let i = 0; i < times; i++) {
|
|
|
|
- chain = chain.then(() => callback());
|
|
|
|
- }
|
|
|
|
- return chain;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
class EspBoard {
|
|
class EspBoard {
|
|
constructor(port) {
|
|
constructor(port) {
|
|
this.port = port;
|
|
this.port = port;
|
|
@@ -65,7 +50,7 @@ class EspBoard {
|
|
|
|
|
|
portSet(options) {
|
|
portSet(options) {
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
- debug("Setting port", options);
|
|
|
|
|
|
+ log.info("Setting port", options);
|
|
this.port.set(options, (err, result) => {
|
|
this.port.set(options, (err, result) => {
|
|
if (err) {
|
|
if (err) {
|
|
reject(err);
|
|
reject(err);
|
|
@@ -78,7 +63,7 @@ class EspBoard {
|
|
resetIntoBootLoader() {
|
|
resetIntoBootLoader() {
|
|
// RTS - Request To Send
|
|
// RTS - Request To Send
|
|
// DTR - Data Terminal Ready
|
|
// DTR - Data Terminal Ready
|
|
- debug("Resetting board");
|
|
|
|
|
|
+ log.info("Resetting board");
|
|
return this.portSet({rts: true, dtr:false})
|
|
return this.portSet({rts: true, dtr:false})
|
|
.then(() => delay(5))
|
|
.then(() => delay(5))
|
|
.then(() => this.portSet({rts: false, dtr: true}))
|
|
.then(() => this.portSet({rts: false, dtr: true}))
|
|
@@ -90,9 +75,6 @@ class EspBoard {
|
|
|
|
|
|
class RomComm {
|
|
class RomComm {
|
|
constructor(config) {
|
|
constructor(config) {
|
|
- if (config.debug) {
|
|
|
|
- debug = config.debug;
|
|
|
|
- }
|
|
|
|
this._port = new SerialPort(config.portName, {
|
|
this._port = new SerialPort(config.portName, {
|
|
baudRate: config.baudRate,
|
|
baudRate: config.baudRate,
|
|
parity: 'none',
|
|
parity: 'none',
|
|
@@ -110,26 +92,26 @@ class RomComm {
|
|
}
|
|
}
|
|
|
|
|
|
bindPort() {
|
|
bindPort() {
|
|
- this._port.on('error', (error) => debug("PORT ERROR", error));
|
|
|
|
- this.in = new slip.SlipDecoder({debug: debug});
|
|
|
|
- this.out = new slip.SlipEncoder({debug: debug});
|
|
|
|
|
|
+ this._port.on('error', error => log.error("PORT ERROR", error));
|
|
|
|
+ this.in = new slip.SlipDecoder();
|
|
|
|
+ this.out = new slip.SlipEncoder();
|
|
this._port.pipe(this.in);
|
|
this._port.pipe(this.in);
|
|
this.out.pipe(this._port);
|
|
this.out.pipe(this._port);
|
|
this.in.on("data", (data) => {
|
|
this.in.on("data", (data) => {
|
|
if (data.length < 8) {
|
|
if (data.length < 8) {
|
|
- debug("Missing header");
|
|
|
|
|
|
+ log.error("Missing header");
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
let headerBytes = data.slice(0, 8);
|
|
let headerBytes = data.slice(0, 8);
|
|
let header = this.headerPacketFrom(headerBytes);
|
|
let header = this.headerPacketFrom(headerBytes);
|
|
if (header.direction != 0x01) {
|
|
if (header.direction != 0x01) {
|
|
- debug("Invaid direction", header.direction);
|
|
|
|
|
|
+ log.error("Invaid direction", header.direction);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
let commandName = commandToKey(header.command);
|
|
let commandName = commandToKey(header.command);
|
|
let body = data.slice(8, header.size);
|
|
let body = data.slice(8, header.size);
|
|
|
|
|
|
- debug("Emitting", commandName, body);
|
|
|
|
|
|
+ log.info("Emitting", commandName, body);
|
|
this.in.emit(commandName, body);
|
|
this.in.emit(commandName, body);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -137,7 +119,7 @@ class RomComm {
|
|
open() {
|
|
open() {
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
this._port.open((error) => {
|
|
this._port.open((error) => {
|
|
- debug("Opening port...", this._port);
|
|
|
|
|
|
+ log.info("Opening port...", this._port);
|
|
if (error) {
|
|
if (error) {
|
|
reject(error);
|
|
reject(error);
|
|
} else {
|
|
} else {
|
|
@@ -163,11 +145,11 @@ class RomComm {
|
|
|
|
|
|
|
|
|
|
sync(ignoreResponse) {
|
|
sync(ignoreResponse) {
|
|
- debug("Syncing");
|
|
|
|
|
|
+ log.info("Syncing");
|
|
return this.sendCommand(commands.SYNC_FRAME, SYNC_FRAME, ignoreResponse)
|
|
return this.sendCommand(commands.SYNC_FRAME, SYNC_FRAME, ignoreResponse)
|
|
.then((response) => {
|
|
.then((response) => {
|
|
if (!ignoreResponse) {
|
|
if (!ignoreResponse) {
|
|
- debug("Sync response completed!", response);
|
|
|
|
|
|
+ log.info("Sync response completed!", response);
|
|
this.isInBootLoader = true;
|
|
this.isInBootLoader = true;
|
|
}
|
|
}
|
|
});
|
|
});
|
|
@@ -192,7 +174,7 @@ class RomComm {
|
|
if (error) {
|
|
if (error) {
|
|
reject(error);
|
|
reject(error);
|
|
}
|
|
}
|
|
- debug("Port flushed");
|
|
|
|
|
|
+ log.info("Port flushed");
|
|
|
|
|
|
resolve();
|
|
resolve();
|
|
});
|
|
});
|
|
@@ -313,7 +295,7 @@ class RomComm {
|
|
this.out.write(message, 'buffer', (err, res) => {
|
|
this.out.write(message, 'buffer', (err, res) => {
|
|
delay(5).then(() => {
|
|
delay(5).then(() => {
|
|
this._port.drain((drainErr, results) => {
|
|
this._port.drain((drainErr, results) => {
|
|
- debug("Draining", drainErr, results);
|
|
|
|
|
|
+ log.info("Draining", drainErr, results);
|
|
if (ignoreResponse) {
|
|
if (ignoreResponse) {
|
|
resolve("Response was ignored");
|
|
resolve("Response was ignored");
|
|
}
|
|
}
|
|
@@ -324,12 +306,12 @@ class RomComm {
|
|
if (!ignoreResponse) {
|
|
if (!ignoreResponse) {
|
|
let commandName = commandToKey(command);
|
|
let commandName = commandToKey(command);
|
|
if (this.in.listeners(commandName).length === 0) {
|
|
if (this.in.listeners(commandName).length === 0) {
|
|
- debug("Listening once", commandName);
|
|
|
|
|
|
+ log.info("Listening once", commandName);
|
|
this.in.once(commandName, (response) => {
|
|
this.in.once(commandName, (response) => {
|
|
resolve(response);
|
|
resolve(response);
|
|
});
|
|
});
|
|
} else {
|
|
} else {
|
|
- debug("Someone is already awaiting", commandName);
|
|
|
|
|
|
+ log.info("Someone is already awaiting", commandName);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|