-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
flash-app.js
executable file
·895 lines (785 loc) · 27.7 KB
/
flash-app.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
#!/usr/bin/env node
/*
* MCP-CAN-Boot Flash-App
*
* Flash application for MCP-CAN-Boot, a CAN bus bootloader for
* AVR microcontrollers attached to an MCP2515 CAN controller.
*
* Copyright (C) 2020-2024 Peter Müller <[email protected]> (https://crycode.de)
* License: CC BY-NC-SA 4.0
*/
const fs = require('fs');
const yargs = require('yargs');
const socketcan = require('socketcan');
const MemoryMap = require('nrf-intel-hex');
const cliProgress = require('cli-progress');
const BOOTLOADER_CMD_VERSION = 0x01;
const CAN_DATA_BYTE_MCU_ID_MSB = 0;
const CAN_DATA_BYTE_MCU_ID_LSB = 1;
const CAN_DATA_BYTE_CMD = 2;
const CAN_DATA_BYTE_LEN_AND_ADDR = 3;
const CAN_ID_MCU_TO_REMOTE_DEFAULT = 0x1FFFFF01;
const CAN_ID_REMOTE_TO_MCU_DEFAULT = 0x1FFFFF02;
const CAN_PING_INTERVAL_DEFAULT = 75;
const CMD_PING = 0b00000000; // remote -> mcu
const CMD_BOOTLOADER_START = 0b00000010; // mcu -> remote
const CMD_FLASH_INIT = 0b00000110; // remote -> mcu
const CMD_FLASH_READY = 0b00000100; // mcu -> remote
const CMD_FLASH_SET_ADDRESS = 0b00001010; // remote -> mcu
const CMD_FLASH_ADDRESS_ERROR = 0b00001011; // mcu -> remote
const CMD_FLASH_DATA = 0b00001000; // remote -> mcu
const CMD_FLASH_DATA_ERROR = 0b00001101; // mcu -> remote
const CMD_FLASH_DONE = 0b00010000; // remote -> mcu
const CMD_FLASH_DONE_VERIFY = 0b01010000; // remote <-> mcu
const CMD_FLASH_ERASE = 0b00100000; // remote -> mcu
const CMD_FLASH_READ = 0b01000000; // remote -> mcu
const CMD_FLASH_READ_DATA = 0b01001000; // mcu -> remote
const CMD_FLASH_READ_ADDRESS_ERROR = 0b01001011; // mcu -> remote
const CMD_START_APP = 0b10000000; // mcu <-> remote
const STATE_INIT = 0;
const STATE_FLASHING = 1;
const STATE_READING = 2;
class FlashApp {
constructor () {
this.args = yargs
.locale('en')
.option('file', {
alias: 'f',
description: 'Hex file to flash',
type: 'string',
demandOption: true,
requiresArg: true
})
.option('iface', {
alias: 'i',
description: 'CAN interface to use',
type: 'string',
default: 'can0',
requiresArg: true
})
.option('partno', {
alias: 'p',
description: 'Specific AVR device like in avrdude',
type: 'string',
demandOption: true,
requiresArg: true
})
.option('mcuid', {
alias: 'm',
description: 'ID of the MCU bootloader',
type: 'string',
demandOption: true,
requiresArg: true,
coerce: this.parseNumber
})
.option('e', {
description: 'Erase whole flash before flashing new data',
type: 'boolean'
})
.option('V', {
description: 'Do not verify',
type: 'boolean'
})
.option('r', {
description: 'Read flash and save to given file (no flashing!), optional with maximum address to read until',
type: 'string',
coerce: this.parseNumber
})
.option('F', {
description: 'Force flashing, even if the bootloader version missmatched',
type: 'boolean'
})
.option('reset', {
alias: 'R',
description: 'CAN message to send on startup to reset the MCU (<can_id>#{hex_data})',
type: 'string',
requiresArg: true,
})
.option('can-id-mcu', {
description: 'CAN-ID for messages from MCU to remote',
type: 'string',
default: CAN_ID_MCU_TO_REMOTE_DEFAULT,
requiresArg: true,
coerce: this.parseNumber
})
.option('can-id-remote', {
description: 'CAN-ID for messages from remote to MCU',
type: 'string',
default: CAN_ID_REMOTE_TO_MCU_DEFAULT,
requiresArg: true,
coerce: this.parseNumber
})
.option('sff', {
description: 'Use Standad Frame Format (SFF) instead of the default Extended Frame Format (EFF) for the CAN-IDs',
type: 'boolean'
})
.option('ping', {
description: 'Send a ping in the given interval (ms) to keep the bus active (should be used if the bootloader uses bitrate detection)',
type: 'number'
})
.option('verbose', {
alias: 'v',
description: 'Enable verbose logging output',
type: 'boolean'
})
.help()
.version(false)
.alias('help', 'h')
.usage(`
= MCP-CAN-Boot Flash-App =
Flash application for MCP-CAN-Boot, a CAN bus bootloader for AVR microcontrollers attached to an MCP2515 CAN controller.
https://github.com/crycode-de/mcp-can-boot`)
.example('$0 -f firmware.hex -p m1284p -m 0x0042')
.example('$0 -f firmware.hex -p m1284p -m 0x0042 --reset 020040FF#4201FA')
.example('$0 -r -f - -p m328p -m 0x0042')
.argv;
// create a new progress bar instance and use legacy theme
this.progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.legacy);
this.doProgress = !this.args.verbose;
this.mcuId = [(this.args.mcuid >> 8) & 0xFF, this.args.mcuid & 0xFF];
this.doErase = !!this.args.e;
this.doRead = (this.args.r !== undefined) ? true : false;
this.doVerify = this.doRead ? false : !this.args.V; // if we are just reading, we cannot verify
// get default time for ping, if ping is set but without a time
if (Object.prototype.hasOwnProperty.call(this.args, 'ping') && typeof this.args.ping !== 'number') {
this.args.ping = CAN_PING_INTERVAL_DEFAULT;
}
this.state = STATE_INIT;
this.deviceSignature = null;
this.deviceFlashSize = 0;
this.loadDeviceInfo(this.args.partno);
if (!this.doRead) {
// load from file if we are not only reading the flash
if (!fs.existsSync(this.args.file)) {
console.log(`Input file ${this.args.file} does not exist!`);
this.exit(1);
}
const intelHexString = fs.readFileSync(this.args.file, 'latin1');
this.memMap = MemoryMap.fromHex(intelHexString);
} else {
// we are only reading the flash... init an empty memory map
this.memMap = new MemoryMap();
// check if output file exists
if (this.args.file !== '-' && fs.existsSync(this.args.file)) {
console.log(`Output file ${this.args.file} already exists!`);
this.exit(1);
}
}
this.memMapKeys = this.memMap.keys(); // load all keys of the memory map
this.memMapCurrentKey = null; // set current key to null to begin new key on flash ready
this.memMapCurrentDataIdx = 0;
this.memMapTotalBytes = 0;
// compute input file size in bytes
for (const block of this.memMap.values()) {
this.memMapTotalBytes += block.length;
}
this.curAddr = 0x0000; // current flash address
this.readDataArr = [];
this.can = socketcan.createRawChannel(this.args.iface, true);
this.can.addListener('onMessage', this.handleCanMsg.bind(this));
this.can.start();
// send can message to reset the mcu?
if (this.args.reset) {
const [canIdStr, dataStr] = this.args.reset.split('#');
const canId = parseInt(canIdStr, 16);
if ((canIdStr.length !== 3 && canIdStr.length !== 8) || isNaN(canId)) {
console.log(`Reset message format error!\nThe can_id is not valid. A three digits standard frame or eight digits extended frame hex id must be provided.`);
this.exit(1);
}
const data = dataStr ? dataStr.match(/../g).map((d) => {
const n = parseInt(d, 16);
if (isNaN(n)) {
console.log(`Reset message format error!\nThe data bytes must be provided as hex numbers.`);
this.exit(1);
}
return n;
}) : [];
this.can.send({
id: canId,
ext: (canIdStr.length > 3),
rtr: false,
data: Buffer.from(data)
});
console.log(`Reset message send to the MCU.`);
}
// send ping messages?
if (this.args.ping) {
console.log(`Sending a ping message every ${this.args.pings} ms.`);
this.pingInterval = setInterval(() => {
this.can.send({
id: this.args.canIdRemote,
ext: !this.args.sff,
rtr: false,
data: Buffer.from([
this.mcuId[0],
this.mcuId[1],
CMD_PING,
0x00,
0x00,
0x00,
0x00,
0x00
])
});
}, this.args.ping);
}
console.log(`Waiting for bootloader start message for MCU ID ${this.hexString(this.args.mcuid, 4)} ...`);
}
handleCanMsg (msg) {
if (msg.data.length !== 8) return;
if (msg.id !== this.args.canIdMcu) return;
const mcuid = msg.data[CAN_DATA_BYTE_MCU_ID_LSB] + (msg.data[CAN_DATA_BYTE_MCU_ID_MSB] << 8);
if (mcuid !== this.args.mcuid) return;
// the message is for this bootloader session
let byteCount, addrPart;
switch (this.state) {
case STATE_INIT:
switch (msg.data[CAN_DATA_BYTE_CMD]) {
case CMD_BOOTLOADER_START:
// check device signature
if (msg.data[4] !== this.deviceSignature[0] || msg.data[5] !== this.deviceSignature[1] || msg.data[6] !== this.deviceSignature[2]) {
console.log('Error: Got bootloader start message but device signature missmatched!');
console.log(`Expected ${this.hexString(this.deviceSignature[0])} ${this.hexString(this.deviceSignature[1])} ${this.hexString(this.deviceSignature[2])} for ${this.args.partno}, got ${this.hexString(msg.data[4])} ${this.hexString(msg.data[5])} ${this.hexString(msg.data[6])}`);
return;
}
// check bootloader version
if (msg.data[7] !== BOOTLOADER_CMD_VERSION) {
if (this.args.F) {
console.warn(`WARNING: Bootloader command version of MCU ${this.hexString(msg.data[7])} does not match the version expected by this flash app ${this.hexString(BOOTLOADER_CMD_VERSION)}. You forced flashing anyways. This may lead to a stupid result...`);
} else {
console.warn(`ERROR: Bootloader command version of MCU ${this.hexString(msg.data[7])} does not match the version expected by this flash app ${this.hexString(BOOTLOADER_CMD_VERSION)}. To force flashing use the -F argument.`);
return;
}
}
// enter flash mode
console.log('Got bootloader start, entering flash mode ...');
if (this.pingInterval) {
clearInterval(this.pingInterval);
this.pingInterval = undefined;
console.log(`Stopped sending of ping messages.`);
}
this.flashStartTs = Date.now();
this.can.send({
id: this.args.canIdRemote,
ext: !this.args.sff,
rtr: false,
data: Buffer.from([
this.mcuId[0],
this.mcuId[1],
CMD_FLASH_INIT,
0x00,
this.deviceSignature[0],
this.deviceSignature[1],
this.deviceSignature[2],
0x00
])
});
break;
case CMD_FLASH_READY:
// flash is ready for first data, read or erase...
if (this.doRead) {
console.log('Querying bootloader size ...');
// determine size of bootloader section by trying to set the
// flash address to 0xFFFFFFFF (huge address that's out of bounds).
// bootloader will repond with CMD_FLASH_ADDRESS_ERROR that will
// inform us of FLASHEND_BL (last address of the program space).
// we can use that value and the known size of the chip's flash
// memory to determine the bootloader size.
this.sendSetFlashAddress(0xFFFFFFFF);
} else if (this.doErase) {
console.log('Got flash ready message, erasing flash ...');
this.can.send({
id: this.args.canIdRemote,
ext: !this.args.sff,
rtr: false,
data: Buffer.from([
this.mcuId[0],
this.mcuId[1],
CMD_FLASH_ERASE,
0x00,
0x00,
0x00,
0x00,
0x00
])
});
this.doErase = false;
} else {
console.log('Got flash ready message, begin flashing ...');
this.state = STATE_FLASHING;
this.onFlashReady(msg.data);
}
break;
case CMD_FLASH_ADDRESS_ERROR:
if (this.doRead) {
// get FLASHEND_BL from error response (last address of program space).
// use it to recover the size of the program/bootloader sections.
const flashendBL =
(msg.data[4] << 24) |
(msg.data[5] << 16) |
(msg.data[6] << 8) |
msg.data[7];
const progSize = flashendBL + 1;
const blSize = this.deviceFlashSize - progSize;
console.log(`Bootloader size: ${blSize} bytes`);
// determine read size (default to full program memory size)
let readSizeBytes = progSize;
if (this.args.r) {
if (this.args.r >= progSize) {
console.warn(`WARNING: read size of ${this.args.r} exceeds program memory size of ${progSize}`);
} else {
readSizeBytes = this.args.r;// user specified max read address
}
}
this.progressStart(readSizeBytes, 0);
this.state = STATE_READING;
this.can.send({
id: this.args.canIdRemote,
ext: !this.args.sff,
rtr: false,
data: Buffer.from([
this.mcuId[0],
this.mcuId[1],
CMD_FLASH_READ,
0x00,
0x00,
0x00,
0x00,
0x00
])
});
} else {
console.warn('WARNING: unexpected CMD_FLASH_ADDRESS_ERROR in STATE_INIT');
}
break;
default:
// something wrong?
console.warn(`WARNING: Got unexpected message from MCU: ${this.hexString(msg.data[CAN_DATA_BYTE_CMD])}`);
}
break;
case STATE_FLASHING:
switch (msg.data[CAN_DATA_BYTE_CMD]) {
case CMD_FLASH_DATA_ERROR:
console.log('Flash data error!');
console.log('Maybe there are some CAN bus issues?');
break;
case CMD_FLASH_ADDRESS_ERROR:
console.log('Flash address error!');
console.log('Maybe the hex file is not for this MCU type or the application is too large to be used together with the bootloader?');
break;
case CMD_FLASH_READY:
byteCount = (msg.data[CAN_DATA_BYTE_LEN_AND_ADDR] >> 5);
// console.log(`${byteCount} bytes flashed`);
this.progressIncrement(byteCount);
this.curAddr += byteCount;
this.memMapCurrentDataIdx += byteCount;
this.onFlashReady(msg.data);
break;
case CMD_START_APP:
console.log(`Flash done in ${(Date.now() - this.flashStartTs)} ms.`);
console.log('MCU is starting the app. :-)');
this.exit(0);
break;
default:
// something wrong?
console.warn(`WARNING: Got unexpected message from MCU: ${this.hexString(msg.data[CAN_DATA_BYTE_CMD])}`);
}
break;
case STATE_READING:
switch (msg.data[CAN_DATA_BYTE_CMD]) {
case CMD_FLASH_DONE_VERIFY:
// start reading flash to verify
console.log('Start reading flash to verify ...');
this.progressStart(this.memMapTotalBytes, 0);
// TODO
this.memMapKeys = this.memMap.keys(); // load all keys of the memory map
this.memMapCurrentKey = null; // set current key to null to begin new key on flash read
this.memMapCurrentDataIdx = 0;
this.readForVerify ();
break;
case CMD_FLASH_READ_DATA:
byteCount = (msg.data[CAN_DATA_BYTE_LEN_AND_ADDR] >> 5);
addrPart = msg.data[CAN_DATA_BYTE_LEN_AND_ADDR] & 0b00011111;
if (this.curAddr & 0b00011111 !== addrPart) {
console.log('Got an unexpected address of read data from MCU!');
console.log('Will now abort and exit the bootloader ...');
this.sendStartApp();
return;
}
if (this.args.verbose) {
console.log(`Got flash data for ${this.hexString(this.curAddr, 4)} ...`);
}
this.progressIncrement(byteCount);
if (this.doVerify) {
// verify flash
for (let i = 0; i < byteCount; i++) {
if (this.memMap.get(this.memMapCurrentKey)[this.memMapCurrentDataIdx] !== undefined
&& this.memMap.get(this.memMapCurrentKey)[this.memMapCurrentDataIdx] !== msg.data[4+i]) {
console.log(`ERROR: Verify failed at ${this.hexString(this.curAddr)}!`);
console.log('Trying to start the app nevertheless ...');
this.sendStartApp();
return;
}
this.curAddr++;
this.memMapCurrentDataIdx++;
}
this.readForVerify();
} else {
// read whole flash
// cache the data
for (let i = 0; i < byteCount; i++) {
this.readDataArr.push(msg.data[4+i]);
this.curAddr++;
}
if (this.args.r > 0 && this.curAddr > this.args.r) {
// reached max read address...
this.readDone();
return;
}
// request next address
this.can.send({
id: this.args.canIdRemote,
ext: !this.args.sff,
rtr: false,
data: Buffer.from([
this.mcuId[0],
this.mcuId[1],
CMD_FLASH_READ,
0x00,
(this.curAddr >> 24) & 0xFF,
(this.curAddr >> 16) & 0xFF,
(this.curAddr >> 8) & 0xFF,
this.curAddr & 0xFF
])
});
}
break;
case CMD_FLASH_READ_ADDRESS_ERROR:
// we hit the end of the flash
if (this.doVerify) {
// hitting the end at verify must be an error...
console.log('ERROR: Reading flash failed during verify!');
this.sendStartApp();
return;
} else {
// when reading whole flash this is expected
this.readDone();
}
break;
case CMD_START_APP:
console.log('MCU is starting the app. :-)');
this.exit(0);
break;
default:
// something wrong?
console.warn(`WARNING: Got unexpected message from MCU: ${this.hexString(msg.data[CAN_DATA_BYTE_CMD])}`);
}
break;
}
}
readForVerify () {
// check memory map and get next map key if we reached the end
if (!this.memMap.get(this.memMapCurrentKey) || this.memMap.get(this.memMapCurrentKey)[this.memMapCurrentDataIdx] === undefined) {
// no more data... goto next memory map key...
const key = this.memMapKeys.next();
if (key.done) {
// all keys done... verify complete
this.progressStop();
console.log(`Flash and verify done in ${(Date.now() - this.flashStartTs)} ms.`);
this.sendStartApp();
return;
}
// apply new current address and set data index to 0
this.memMapCurrentKey = key.value;
this.memMapCurrentDataIdx = 0;
this.curAddr = key.value;
}
// request next address
this.can.send({
id: this.args.canIdRemote,
ext: !this.args.sff,
rtr: false,
data: Buffer.from([
this.mcuId[0],
this.mcuId[1],
CMD_FLASH_READ,
0x00,
(this.curAddr >> 24) & 0xFF,
(this.curAddr >> 16) & 0xFF,
(this.curAddr >> 8) & 0xFF,
this.curAddr & 0xFF
])
});
}
readDone () {
this.progressStop();
// create memory map
const memMap = new MemoryMap();
memMap.set(0x0000, Uint8Array.from(this.readDataArr));
const intelHexString = memMap.asHexString();
if (this.args.file === '-') {
// write to stdout
console.log('Read hex data:');
console.log(intelHexString);
} else {
fs.writeFileSync(this.args.file, intelHexString, 'latin1');
console.log(`Hex file written to ${this.args.file}.`);
}
console.log(`Reading flash done in ${Date.now() - this.flashStartTs} ms.`);
// start the main application at the MCU
this.sendStartApp();
}
sendStartApp () {
console.log('Starting the app on the MCU ...');
this.can.send({
id: this.args.canIdRemote,
ext: !this.args.sff,
rtr: false,
data: Buffer.from([
this.mcuId[0],
this.mcuId[1],
CMD_START_APP,
0x00,
0x00,
0x00,
0x00,
0x00
])
});
}
sendSetFlashAddress(addr) {
if (this.args.verbose) {
console.log(`Setting flash address to ${this.hexString(addr)} ...`);
}
this.can.send({
id: this.args.canIdRemote,
ext: !this.args.sff,
rtr: false,
data: Buffer.from([
this.mcuId[0],
this.mcuId[1],
CMD_FLASH_SET_ADDRESS,
0x00,
(addr >> 24) & 0xFF,
(addr >> 16) & 0xFF,
(addr >> 8) & 0xFF,
addr & 0xFF
])
});
}
onFlashReady (msgData) {
const curAddrRemote = msgData[7] + (msgData[6] << 8) + (msgData[5] << 16) + (msgData[4] << 24);
//console.log(`Remote flash address is ${this.hexString(curAddrRemote)}`);
if (!this.memMap.get(this.memMapCurrentKey) || this.memMap.get(this.memMapCurrentKey)[this.memMapCurrentDataIdx] === undefined) {
// no more data... goto next memory map key...
const key = this.memMapKeys.next();
if (key.done) {
// all keys done... flash complete
this.progressStop();
console.log('All data transmitted. Finalizing ...');
if (this.doVerify) {
// we want to verify... send flash done verify and set own state to read
this.state = STATE_READING;
this.can.send({
id: this.args.canIdRemote,
ext: !this.args.sff,
rtr: false,
data: Buffer.from([
this.mcuId[0],
this.mcuId[1],
CMD_FLASH_DONE_VERIFY,
0x00,
0x00,
0x00,
0x00,
0x00
])
});
} else {
// we don't want to verify... send flash done to start the app
this.can.send({
id: this.args.canIdRemote,
ext: !this.args.sff,
rtr: false,
data: Buffer.from([
this.mcuId[0],
this.mcuId[1],
CMD_FLASH_DONE,
0x00,
0x00,
0x00,
0x00,
0x00
])
});
}
return;
}
// initialize progress bar on first block
if (this.memMapCurrentKey == null) {
this.progressStart(this.memMapTotalBytes, 0);
}
// apply new current address and set data index to 0
this.memMapCurrentKey = key.value;
this.memMapCurrentDataIdx = 0;
this.curAddr = key.value;
}
if (this.curAddr !== curAddrRemote) {
// need to set the address to flash...
console.log(`Setting flash address to ${this.hexString(this.curAddr, 4)} ...`);
this.sendSetFlashAddress(this.curAddr);
return;
}
// send data to flash...
const data = Buffer.from([
this.mcuId[0],
this.mcuId[1],
CMD_FLASH_DATA,
0x00,
0x00,
0x00,
0x00,
0x00
]);
// add the 4 data bytes if available
let dataBytes = 0;
for (let i = 0; i < 4; i++) {
const byte = this.memMap.get(this.memMapCurrentKey)[this.memMapCurrentDataIdx+i];
if (byte === undefined) {
break;
}
data[4+i] = byte;
dataBytes++;
}
// set the number of bytes and address
data[CAN_DATA_BYTE_LEN_AND_ADDR] = (dataBytes << 5) | (this.curAddr & 0b00011111);
// send data
if (this.args.verbose) {
console.log(`Sending flash data ${this.hexString(this.curAddr, 4)} ...`);
}
this.can.send({
id: this.args.canIdRemote,
ext: !this.args.sff,
rtr: false,
data: data
});
}
hexString (num, minLength) {
let hex = num.toString(16);
if (hex.length % 2 !== 0) {
hex = '0' + hex;
}
if (minLength) {
while (hex.length < minLength) {
hex = '0' + hex;
}
}
return '0x' + hex.toUpperCase();
}
parseNumber (val) {
if (typeof(val) === 'string') {
val = parseInt(val, val.startsWith('0x') ? 16 : 10);
}
return val;
}
progressStart(total, startValue) {
if (this.doProgress) {
this.progressBar.start(total, startValue);
}
}
progressIncrement(incr = 1) {
if (this.doProgress) {
this.progressBar.increment(incr);
}
}
progressStop() {
if (this.doProgress) {
this.progressBar.stop();
}
}
loadDeviceInfo (partno) {
partno = partno.toLowerCase();
switch (partno) {
case 'm32':
case 'mega32':
case 'atmega32':
this.deviceSignature = [0x1E, 0x95, 0x02];
this.deviceFlashSize = 32 * 1024;
break;
case 'm32u4':
case 'mega32u4':
case 'atmega32u4':
this.deviceSignature = [0x1E, 0x95, 0x87];
this.deviceFlashSize = 32 * 1024;
break;
case 'm328':
case 'mega328':
case 'atmega328':
this.deviceSignature = [0x1E, 0x95, 0x14];
this.deviceFlashSize = 32 * 1024;
break;
case 'm328p':
case 'mega328p':
case 'atmega328p':
this.deviceSignature = [0x1E, 0x95, 0x0F];
this.deviceFlashSize = 32 * 1024;
break;
case 'm328pb':
case 'mega328pb':
case 'atmega328pb':
this.deviceSignature = [0x1E, 0x95, 0x16];
this.deviceFlashSize = 32 * 1024;
break;
case 'm64':
case 'mega64':
case 'atmega64':
this.deviceSignature = [0x1E, 0x96, 0x02];
this.deviceFlashSize = 64 * 1024;
break;
case 'm644p':
case 'mega644p':
case 'atmega644p':
this.deviceSignature = [0x1E, 0x96, 0x0A];
this.deviceFlashSize = 64 * 1024;
break;
case 'm128':
case 'mega128':
case 'atmega128':
this.deviceSignature = [0x1E, 0x97, 0x02];
this.deviceFlashSize = 128 * 1024;
break;
case 'm1284p':
case 'mega1284p':
case 'atmega1284p':
this.deviceSignature = [0x1E, 0x97, 0x05];
this.deviceFlashSize = 128 * 1024;
break;
case 'm2560':
case 'mega2560':
case 'atmega2560':
this.deviceSignature = [0x1E, 0x98, 0x01];
this.deviceFlashSize = 256 * 1024;
break;
default:
this.deviceSignature = [0, 0, 0];
this.deviceFlashSize = 0 * 1024;
}
}
/**
* Do a clean exit of the flash app.
*/
exit (code) {
try {
if (this.pingInterval) {
clearInterval(this.pingInterval);
}
if (this.can) {
this.can.stop();
}
} catch (e) {
console.warn('Error at exit cleanup:', e);
}
process.exit(code);
}
}
new FlashApp();