-
Notifications
You must be signed in to change notification settings - Fork 4
/
pinoccio.js
597 lines (560 loc) · 16 KB
/
pinoccio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
var VENDOR_ID = 0x1d50;
var PRODUCT_ID = 0x6051;
// Program writing constants
var pageSize = 256;
var pageDelay = 10;
(function() {
function Device() {
this.port = "";
this.conn = undefined;
this.blSeq = 0; // Bootloader command sequence
}
Device.prototype.connect = function(port, cbDone) {
if (this.conn) setTimeout(cbDone, 0);
this.port = port;
console.log("Set port to ", this.port);
this.conn = new PinoccioSerial.SerialConnection();
this.conn.connect(this.port, function(err) {
cbDone(err);
});
};
Device.prototype.restart = function(cbDone) {
var self = this;
async.series([
function(cbStep) {
PinoccioSerial.closeAll(function() {
cbStep()
});
},
function(cbStep) {
self.conn = new PinoccioSerial.SerialConnection();
self.conn.connect(self.port, function(err) {
console.log("Setup the new connection to ", self.port, err);
cbStep(err);
});
},
function(cbStep) {
self.conn.setControlSignals({rts:true, dtr:true}, function() {
cbStep();
});
},
function(cbStep) {
setTimeout(cbStep, 250);
},
function(cbStep) {
self.conn.setControlSignals({rts:false, dtr:false}, function() {
cbStep();
});
},
function(cbStep) {
setTimeout(cbStep, 50);
}
], function(err) {
cbDone(err);
});
};
Device.prototype.drain = function(cbDone) {
var self = this;
self.conn.flush(function() {
self.conn.read(1000, function(readInfo) {
self.conn.flush(function() {
cbDone();
});
});
});
};
Device.prototype.sendBootloadCommand = function(msg, cbDone) {
var bufLen = 6 + msg.length;
var buffer = new ArrayBuffer(bufLen);
var dv = new DataView(buffer);
var checksum = 0;
dv.setUint8(0, 0x1b);
checksum ^= 0x1b;
dv.setUint8(1, this.blSeq);
checksum ^= this.blSeq;
dv.setUint16(2, msg.length, false);
checksum ^= dv.getUint8(2);
checksum ^= dv.getUint8(3);
dv.setUint8(4, 0x0e);
checksum ^= dv.getUint8(4);
for (var x = 0; x < msg.length; ++x) {
dv.setUint8(5 + x, msg[x]);
checksum ^= msg[x];
}
dv.setUint8(bufLen - 1, checksum);
++this.blSeq;
if (this.blSeq > 0xff) this.blSeq = 0;
this.conn.writeRaw(buffer, function() {
this.readBootloadCommand(cbDone);
}.bind(this));
/*
var conn = new SerialConnection();
conn.connect(port, function() {
conn.setControlSignals({rts:true, dtr:true}, function() {
setTimeout(function() {
conn.setControlSignals({rts:false, dtr:false}, function() {
setTimeout(function() {
conn.read(100, function(readInfo) {
//setTimeout(function() {
readCmd(conn, function() {
})
});
});
}, 50);
});
}, 250);
});
});
*/
}
var cmdReadStates = ["Start", "GetSequenceNumber", "GetMessageSize1", "GetMessageSize2", "GetToken", "GetData", "GetChecksum", "Done"];
Device.prototype.readBootloadCommand = function(timeout, cbDone) {
var self = this;
var state = 0;
var timedout = false;
var pkt = {
message : [],
messageLen : [],
checksum:0
};
if (typeof timeout === "function") {
cbDone = timeout;
timeout = undefined;
}
if (timeout === undefined) timeout = 2000;
setTimeout(function() { timedout = true; }, timeout);
async.whilst (function() {
return state < (cmdReadStates.length - 1) && !timedout
}, function(cbStep) {
self.conn.read(1, function(readInfo) {
if (readInfo.length > 0) {
var curByte = readInfo.charCodeAt(0);
debugLog("Read: state(%s) byte(%d) char(%s)", cmdReadStates[state], curByte, String.fromCharCode(curByte));
} else {
debugLog("There was no data yet, waiting for some");
return setTimeout(cbStep, 10);
}
pkt.checksum ^= curByte;
switch(state) {
case 0:
if (curByte != 0x1b) {
return cbStep("Invalid header byte expected 0x1b got -%s-", curByte)
}
++state;
break;
case 1:
var prevSeq = self.blSeq - 1;
if (prevSeq == -1) prevSeq = 255;
if (curByte != prevSeq) {
return cbStep("Invalid sequence number");
}
++state;
break;
case 2:
pkt.messageLen.push(curByte);
++state;
break;
case 3:
pkt.messageLen.push(curByte);
pkt.messageLen = (pkt.messageLen[0] << 8) | pkt.messageLen[1];
++state;
break;
case 4:
if (curByte != 0x0e) {
return cbStep("Invalid message marker byte");
}
++state;
break;
case 5:
if (--pkt.messageLen == 0) ++state;
pkt.message.push(curByte);
break;
case 6:
pkt.checksum ^= curByte;
pkt.checksum = (pkt.checksum == curByte) ? true : false;
++state;
break;
}
cbStep();
});
}, function(err) {
cbDone(err, pkt);
})
};
Device.prototype.signOn = function(cbDone) {
var self = this;
self.restart(function(err) {
console.log(err);
//self.drain(function() {
self.sendBootloadCommand([0x01], function(err, pkt) {
if (err) return cbDone(err);
console.log("Packet: ", pkt);
if (!pkt || !pkt.message || pkt.message.length == 1) return cbDone("Unable to sign on to device");
cbDone();
});
//});
});
}
// Save the given progrma to the chip
// The programData should be in intel hex format
Device.prototype.saveProgram = function(programData, cbDone) {
// Convert the programData into binary
var binaryData = [];
var curPos = 0;
var validProgram = false;
programData.split("\n").forEach(function(programLine) {
//console.log("Checking ", programLine);
if (validProgram) return;
if (programLine[0] != ":") {
throw new Error("Invalid program, data format incorrect");
}
// If it's the end we're good
if (programLine == ":00000001FF") {
validProgram = true;
return;
}
var recordType = parseInt(programLine.substring(8, 9), 10);
if (recordType == 2) {
// This is a extended segment address, let's reset and keep going, we put it all together
curPos = 0;
return;
} else if (recordType != 0) {
console.error("Unexpected record type %d", recordType);
throw new Error("Invalid program, could not parse unknown record type " + recordType);
return;
}
// Break apart the line
var linePos = parseInt(programLine.substring(3, 7), 16);
if (linePos != curPos) {
console.error("Got %d expected %d", linePos, curPos);
throw new Error("Invalid program, out of order.");
}
var lineLength = parseInt(programLine.substring(1, 3), 16);
// Parse the data
for (var i = 9; i < 9 + (lineLength * 2); i += 2) {
binaryData.push(parseInt(programLine.substring(i, i+2), 16));
}
curPos += lineLength;
});
if (!validProgram) {
throw new Error("The program is invalid, did not parse");
}
// var bytes = String.fromCharCode.apply(String, binaryData);
console.log("Parsed %d bytes", binaryData.length);
var self = this;
async.series([
function(cbStep) {
setTimeout(cbStep, 500);
},
function(cbStep) {
self.signOn(cbStep);
},
function(cbStep) {
// Enter programming mode
console.log("Entering programming mode");
self.sendBootloadCommand([0x10, 0xc8, 0x64, 0x19, 0x20, 0x00, 0x53, 0x03, 0xac, 0x53, 0x00, 0x00], function(err, resp) {
if (err) console.error(err);
console.log("Programming mode response: ", resp);
cbStep();
});
},
/*
function(cbStep) {
self.readBootloadCommand(5000, function(err, pkt) {
console.log("Entering programming mode", pkt);
cbStep();
});
},
*/
/*
function(cbStep) {
self.readBootloadCommand(5000, function(err, pkt) {
console.log("Load address ", pkt);
cbStep();
});
},
*/
// Actually do the paged write
function(cbStep) {
self.pagedWrite(binaryData, cbStep);
},
function(cbStep) {
console.log("Exiting the programmer");
// Exit programming mode
self.sendBootloadCommand([0x11, 0x01, 0x01], function() {
cbStep();
});
},
/*
function(cbStep) {
self.readBootloadCommand(5000, function(err, pkt) {
console.log("Signed out of the programmer", pkt);
cbStep();
});
},
*/
function(cbStep) {
setTimeout(function() {
self.conn.close(function() {
connectedDevice = null;
cbStep();
});
}, 1000);
}
], function(err) {
if (err) console.error(err);
console.log("Done programming");
cbDone(err);
});
}
Device.prototype.pagedWrite = function(bytes, cbDone) {
var self = this;
var pageaddr = 0;
async.whilst(
function() { return pageaddr < bytes.length; },
function(cbWhileStep) {
async.series([
// Set the program address
function(cbStep) {
var useaddr = pageaddr >> 1;
var cmdBuf = [0x06];
cmdBuf[1] = (useaddr >> 24) & 0xff | 0x80;
cmdBuf[2] = (useaddr >> 16) & 0xff;
cmdBuf[3] = (useaddr >> 8) & 0xff;
cmdBuf[4] = useaddr & 0xff;
self.sendBootloadCommand(cmdBuf, function(err, pkt) {
if (err) console.error(err);
cbStep();
});
},
function(cbStep) {
// Write the page
var writeBytes = bytes.slice(pageaddr, (bytes.length > pageSize ? (pageaddr + pageSize) : bytes.length - 1));
var cmdBuf = [0x13, 0x00, 0x00, 0xc1, 0x0a, 0x40, 0x4c, 0x20, 0x00, 0x00];
cmdBuf[1] = writeBytes.length >> 8;
cmdBuf[2] = writeBytes.length & 0xff;
if ((pageaddr + writeBytes.length) > 0xEF000) {
console.log("Trying to write past our valid space, bailing");
return cbStep(new Error("Trying to write into boot loader"));
}
//console.log(cmdBuf.concat(writeBytes));
self.sendBootloadCommand(cmdBuf.concat(writeBytes), function(err, resp) {
if (err) return cbStep(err);
pageaddr += writeBytes.length;
setTimeout(cbStep, 4);
});
},
/*
function(cbStep) {
setTimeout(cbStep, pageDelay);
}
*/
], function(err) {
cbWhileStep(err);
});
},
function(err) {
cbDone(err);
}
);
}
/* TODO: The chip doesn't like how avrdude does this so we're still researching
Device.prototype.erase = function(cbDone) {
self.sendBootloadCommand([], function(err, pkt) {
});
};
*/
var overallTimer;
function checkForDevice(timeout, onFound) {
console.log(arguments);
// Wrap the found callback to deal with timer triggers
var foundTriggered = false;
function foundWrapper(err, found) {
if (foundTriggered) return;
if (overallTimer !== undefined) clearTimeout(overallTimer);
if (curTimer !== undefined) clearTimeout(curTimer);
foundTriggered = true;
var args = Array.prototype.slice.call(arguments, 0);
onFound(err, found);
};
// Overall timeout
if (overallTimer === undefined) {
overallTimer = setTimeout(function() {
console.log("TIMEOUT");
overallTimer = undefined;
clearTimeout(curTimer);
return foundWrapper("Timeout waiting for device", false);
}, timeout);
}
console.log("Checking for USB device");
var curTimer;
chrome.usb.getDevices({"vendorId": VENDOR_ID, "productId": PRODUCT_ID}, function(devices) {
if (devices && devices.length > 0) {
console.log("our USB is plugged in");
foundWrapper(null, true);
} else {
// if (!foundTriggered) {
// curTimer = setTimeout(function() {
// curTimer = undefined;
// checkForDevice(0, foundWrapper);
// }, 500);
// }
// We didn't find it, let HQ tell us when to detect again
console.log("did not find our USB device");
foundWrapper(null, false);
}
});
}
var usbCheckTimer;
function cancelUSBPluggedIn() {
clearTimeout(usbCheckTimer);
}
function checkUSBPluggedIn(timeout, cbDone) {
chrome.usb.getDevices({"vendorId": VENDOR_ID, "productId": PRODUCT_ID}, function(devices) {
if (devices && devices.length > 0) {
usbCheckTimer = setTimeout(function() { checkUSBPluggedIn(timeout, cbDone); }, timeout);
} else {
cbDone();
}
});
}
var connectedDevice = undefined;
function forgetDevice(cbDone) {
connectedDevice = null;
console.log("Forgetting connected device.");
return cbDone(null, true);
}
function findSerial(cbDone) {
console.log("findSerial");
// If we're already connecte just short circuit
if (connectedDevice) {
console.log("Using already connected device.");
return cbDone(null, connectedDevice);
}
var usbttyRE = /(tty\.usb|ttyACM|COM\d+)/;
var port;
console.log("Going to close all");
PinoccioSerial.closeAll(function() {
PinoccioSerial.getDevices(function(devices) {
async.detect(devices, function(device, cbStep) {
console.log("Check ", device);
if (usbttyRE.test(device.path)) {
console.log("Actually trying ", device);
trySerial(device.path, function(conn) {
if (!conn) return cbStep(false);
console.log("This is the one: ", device.path);
port = device.path;
connectedDevice = new Device;
connectedDevice.port = port;
connectedDevice.conn = conn;
cbStep(true);
});
} else {
setTimeout(function() { cbStep(false); }, 0);
}
}, function(result) {
return cbDone(null, connectedDevice);
})
});
});
/*
chrome.serial.getPorts(function(ports) {
async.detect(ports, function(portName, cbStep) {
console.log("Trying ", portName);
if (usbttyRE.test(portName)) {
trySerial(portName, function(conn) {
if (!conn) return cbStep(false);
console.log("This is the one: ", portName);
port = portName;
connectedDevice = new Device;
connectedDevice.conn = conn;
cbStep(true);
});
} else {
cbStep(false);
}
}, function(result) {
if (!result) return cbDone("Could not find the device");
return cbDone(null, connectedDevice);
});
});
*/
}
function trySerial(port, cbDone) {
var conn = new PinoccioSerial.SerialConnection();
var foundIt = false;
async.series([
function(cbStep) {
console.log("Connecting");
conn.connect(port, cbStep);
},
function(cbStep) {
console.log("Waiting a bit");
setTimeout(cbStep, 1000);
},
/*
function(cbStep) {
console.log("Flushing");
conn.flush(function(result) {
if (!result) {
conn.close(function() {
cbStep("Could not flush");
});
} else {
cbStep();
}
});
},
*/
/*
function(cbStep) {
conn.unechoWrite("\n", function() {
cbStep();
});
},
function(cbStep) {
conn.waitForPrompt("\n> ", function() {
cbStep();
});
},
*/
/*
function(cbStep) {
console.log("running scout.report");
conn.unechoWrite("scout.report\n", function(writeInfo) {
cbStep();
});
},
*/
function(cbStep) {
console.log("Waiting for prompt");
conn.readUntilPrompt("> ", function(err, readData) {
if (err) return cbStep(err);
//console.log("Read -%s-", readData);
if (/Pinoccio/.test(readData)) {
console.log("Found it");
foundIt = true;
} else {
console.error("No scout ready");
}
cbStep();
});
}],
function(err) {
cbDone(foundIt ? conn : undefined);
}
);
}
function closeAll(cbDone) {
PinoccioSerial.closeAll(cbDone);
}
window.pinoccio = {
Device:Device,
checkForDevice:checkForDevice,
findSerial:findSerial,
forgetDevice:forgetDevice,
checkUSBPluggedIn:checkUSBPluggedIn,
cancelUSBPluggedIn:cancelUSBPluggedIn,
closeAll:closeAll
};
})(window);