Ver Fonte

More translations of C and Python into Node. No idea if it is working ;)

craigsdennis há 9 anos atrás
pai
commit
f17fee59a0
2 ficheiros alterados com 44 adições e 6 exclusões
  1. 2 1
      package.json
  2. 42 5
      spike/flasher.js

+ 2 - 1
package.json

@@ -25,7 +25,8 @@
   "homepage": "https://github.com/thingsSDK/especially-flasher#readme",
   "dependencies": {
     "node-binary": "^1.1.0",
-    "serialport": "^2.0.6"
+    "serialport": "^2.0.6",
+    "slip": "^1.0.2"
   },
   "devDependencies": {
     "electron-prebuilt": "^0.36.9",

+ 42 - 5
spike/flasher.js

@@ -1,5 +1,5 @@
 var SerialPort = require("serialport").SerialPort;
-var binary = require("binary");
+var bufferpack = require("bufferpack");
 var slip = require("slip");
 
 // /path/to/esptool/esptool.py --port /dev/ttyUSB0 --baud 115200 \
@@ -7,6 +7,27 @@ var slip = require("slip");
 //   0x0000 "boot_v1.4(b1).bin" 0x1000 espruino_esp8266_user1.bin \
 //   0x3FC000 esp_init_data_default.bin 0x3FE000 blank.bin
 
+// Marriage of ESPTOOL-CK and ESPTOOL.py
+const formats = {
+    bootloader_packet_header: "<B(direction)B(command)H(size)I(checksum)"
+};
+
+const commands = {
+    CMD0: 0x00,
+    CMD1: 0x01,
+    FLASH_DOWNLOAD_BEGIN: 0x02,
+    FLASH_DOWNLOAD_DATA: 0x03,
+    FLASH_DOWNLOAD_DONE: 0x04,
+    RAM_DOWNLOAD_BEGIN: 0x05,
+    RAM_DOWNLOAD_END: 0x06,
+    RAM_DOWNLOAD_DATA: 0x07,
+    SYNC_FRAME: 0x08,
+    WRITE_REGISTER: 0x09,
+    READ_REGISTER: 0x0A,
+    SET_FLASH_PARAMS: 0x0B,
+    NO_COMMAND: 0xFF
+};
+
 
 function slipReadParser(emitter, buffer) {
         // This is the pyramid of doom right?
@@ -18,7 +39,7 @@ function slipReadParser(emitter, buffer) {
         decoder.decode(buffer);
 }
 
-function EspROM(config) {
+function EspComm(config) {
     this.port = new SerialPort(config.portName, {
         baud: config.baud,
         parser: slipReadParser
@@ -28,10 +49,26 @@ function EspROM(config) {
 
 // TODO:csd - How to make the commands pretty?
 // https://github.com/themadinventor/esptool/blob/master/esptool.py#L108
-EspROM.prototype.request = function(req) {
+// https://github.com/igrr/esptool-ck/blob/master/espcomm/espcomm.c#L103
+EspComm.prototype.sendCommand = function(command, data) {
+    // ???:csd - Is this how you do OO anymore?
     var port = this.port;
     return new Promise(function(resolve, reject) {
-        port.write(slip.encode(req));
-        port.once('data', resolve);
+        var sendHeader = bufferpack.pack(formats.bootloader_packet_header, {
+            direction: 0x00,
+            command: command,
+            size: data.size(), 
+        });
+        port.write(slip.encode(sendHeader));
+        port.write(slip.encode(data));
+        port.once('data', function(buffer) {    
+            var receiveHeader = bufferpack.unpack(formats.bootloader_packet_header, buffer.readInt8(0));
+            // FIXME:csd - Sanity check here regarding things
+            resolve({
+                header: receiveHeader,
+                // Result follows the header
+                data: buffer.slice(8)
+            });
+        });
     });
 }