-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbdmtrionic.cpp
More file actions
1363 lines (1208 loc) · 55.4 KB
/
bdmtrionic.cpp
File metadata and controls
1363 lines (1208 loc) · 55.4 KB
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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
bdmtrionic.cpp
(c) 2010 by Sophie Dexter
General purpose BDM functions for Just4Trionic by Just4pLeisure
A derivative work based on:
//-----------------------------------------------------------------------------
// CAN/BDM adapter firmware
// (C) Janis Silins, 2010
// $id$
//-----------------------------------------------------------------------------
********************************************************************************
WARNING: Use at your own risk, sadly this software comes with no guarantees.
This software is provided 'free' and in good faith, but the author does not
accept liability for any damage arising from its use.
*******************************************************************************/
#include "interfaces.h"
#include "bdmcpu32.h"
#include "bdmdriver.h"
#include "bdmtrionic.h"
// structure for command address/value pairs
struct mempair_t {
uint32_t addr; ///< target address
uint16_t val; ///< word value
};
// word write algorithm (29Fxxx)
static const struct mempair_t am29_write [] = {
{0xaaaa, 0xaaaa}, {0x5554, 0x5555}, {0xaaaa, 0xa0a0},
};
// chip erase algorithms
static const struct mempair_t am29_erase [] = {
{0xaaaa, 0xaaaa}, {0x5554, 0x5555}, {0xaaaa, 0x8080},
{0xaaaa, 0xaaaa}, {0x5554, 0x5555}, {0xaaaa, 0x1010}
};
// reset algorithms
//static const struct mempair_t am29_reset = {0xfffe, 0xf0f0};
static const struct mempair_t am29_reset [] = {
{0xaaaa, 0xaaaa}, {0x5554, 0x5555}, {0xaaaa, 0xf0f0},
};
// chip id algorithms
static const struct mempair_t am29_id [] = {
{0xaaaa, 0xaaaa}, {0x5554, 0x5555}, {0xaaaa, 0x9090},
};
// ;-)
static const struct mempair_t flash_tag [] = {
{0x7fe00, 0xFF4A}, {0x7fe02, 0x7573}, {0x7fe04, 0x7434}, {0x7fe06, 0x704C},
{0x7fe08, 0x6569}, {0x7fe0a, 0x7375}, {0x7fe0c, 0x7265}, {0x7fe0e, 0x3B29},
};
// local functions
bool erase_am29();
bool erase_am28(const uint32_t* start_addr, const uint32_t* end_addr);
bool get_flash_id(uint8_t* make, uint8_t* type);
bool run_bdm_driver(uint32_t addr, uint32_t maxtime);
//-----------------------------------------------------------------------------
/**
Dumps contents of a memory block from [start_addr] up to, but not including,
the [end_addr] as long words (word-aligned addresses). MCU must be in
background mode. The operation interrupts if the break character is
received.
@param start_addr block start address
@param end_addr block end address
@return status flag
*/
uint8_t dump_flash(const uint32_t* start_addr, const uint32_t* end_addr)
{
// check parametres
if (*start_addr > *end_addr) {
return TERM_ERR;
}
// dump memory contents
uint32_t curr_addr = *start_addr;
uint32_t value;
while ((curr_addr < *end_addr) && (pc.getc() != TERM_BREAK)) {
// read long word
if (curr_addr > *start_addr) {
if (memdump_long(&value) != TERM_OK) {
return TERM_ERR;
}
} else {
if (memread_long(&value, &curr_addr) != TERM_OK) {
return TERM_ERR;
}
}
// send memory value to host
printf("%08lX", value);
printf("\r\n");
// add the terminating character
if (curr_addr < *end_addr - 4) {
pc.putc(TERM_OK);
// light up the activity LED
ACTIVITYLEDON;
}
curr_addr += 4;
}
return TERM_OK;
}
//-----------------------------------------------------------------------------
/**
Dumps the contents of a T5 ECU to a BIN file on the mbed 'disk'
from [start_addr] up to, but not including, the [end_addr].
MCU must be in background mode.
@param start_addr block start address
@param end_addr block end address
@return status flag
*/
uint8_t dump_trionic()
{
// Configure the MC68332 register values to prepare for flashing
printf("I am trying to discover what type of Trionic ECU I am connected to...\r\n");
prep_t5_do();
// Work out what type of FLASH chips we want to make a dump file for
uint8_t make;
uint8_t type;
get_flash_id(&make, &type);
// set up chip-specific functions
bool (*reset_func)();
uint32_t flash_size;
switch (type) {
case AMD29BL802C:
printf("I have found AMD29BL802C type FLASH chips; I must be connected to a T8 ECU :-)\r\n");
reset_func = &reset_am29;
flash_size = T8FLASHSIZE;
break;
case AMD29F400B:
case AMD29F400T:
printf("I have found AMD29F400 type FLASH chips; I must be connected to a T7 ECU :-)\r\n");
reset_func = &reset_am29;
flash_size = T7FLASHSIZE;
break;
case AMD29F010:
case SST39SF010:
case AMICA29010L:
printf("I have found 29/39F010 type FLASH chips; I must be connected to a repaired T5.5 ECU :-)\r\n");
reset_func = &reset_am29;
flash_size = T55FLASHSIZE;
break;
case ATMEL29C010:
printf("I have found Atmel 29C010 type FLASH chips; I must be connected to a repaired T5.5 ECU :-)\r\n");
reset_func = &reset_am29;
flash_size = T55FLASHSIZE;
break;
case AMD28F010:
case INTEL28F010:
printf("I have found 28F010 type FLASH chips; I must be connected to a T5.5 ECU :-)\r\n");
reset_func = &reset_am28;
flash_size = T55FLASHSIZE;
break;
case AMD28F512:
case INTEL28F512:
printf("I have found 28F512 type FLASH chips; I must be connected to a T5.2 ECU :-)\r\n");
reset_func = &reset_am28;
flash_size = T52FLASHSIZE;
break;
case ATMEL29C512:
printf("I have found Atmel 29C512 type FLASH chips; I must be connected to a repaired T5.2 ECU :-)\r\n");
reset_func = &reset_am28;
flash_size = T52FLASHSIZE;
break;
default:
// unknown flash type
printf("I could not work out what FLASH chips or TRIONIC ECU I am connected to :-(\r\n");
return TERM_ERR;
}
// reset the FLASH chips
if (!reset_func()) return TERM_ERR;
printf("Creating FLASH dump file...\r\n");
FILE *fp = fopen("/local/original.bin", "w"); // Open "original.bin" on the local file system for writing
if (!fp) {
perror ("The following error occured");
return TERM_ERR;
}
// dump memory contents
uint32_t addr = 0x00;
uint32_t long_value;
// setup start address to dump from
if (memread_long_cmd(&addr) != TERM_OK) return TERM_ERR;
timer.reset();
timer.start();
printf(" 0.00 %% complete.\r");
while (addr < flash_size) {
uint16_t byte_count = 0;
while (byte_count < FILE_BUF_LENGTH) {
// get long word
if (memget_long(&long_value) != TERM_OK) return TERM_ERR;
// send memory value to file_buffer before saving to mbed 'disk'
file_buffer[byte_count++] = ((uint8_t)(long_value >> 24));
file_buffer[byte_count++] = ((uint8_t)(long_value >> 16));
file_buffer[byte_count++] = ((uint8_t)(long_value >> 8));
file_buffer[byte_count++] = ((uint8_t)long_value);
}
fwrite(file_buffer, 1, FILE_BUF_LENGTH, fp);
if (ferror (fp)) {
fclose (fp);
printf ("Error writing to the FLASH BIN file.\r\n");
return TERM_ERR;
}
printf("%6.2f\r", 100*(float)addr/(float)flash_size );
// make the activity led twinkle
ACTIVITYLEDON;
addr += FILE_BUF_LENGTH;
}
printf("100.00\r\n");
// should 'clear' the BDM connection here but bdm_clear won't compile from here
// instead do a memread (or anything really) but ignore the result because it's not needed for anything
memread_long(&long_value, &addr);
timer.stop();
printf("Getting the FLASH dump took %#.1f seconds.\r\n",timer.read());
fclose(fp);
return TERM_OK;
}
//-----------------------------------------------------------------------------
/**
Erases the flash memory chip starting from [start_addr] up to, but not
including [end_addr] and optionally verifies the result; MCU must be in
background mode.
@param flash_type type of flash chip
@param start_addr flash start address
@param end_addr flash end address
@return status flag
*/
uint8_t erase_flash(const char* flash_type, const uint32_t* start_addr,
const uint32_t* end_addr)
{
// AM29Fxxx chips (retrofitted to Trionic 5.x; original to T7)
if (strncmp(flash_type, "29f010", 6) == 0 ||
strncmp(flash_type, "29f400", 6) == 0) {
return erase_am29() ? TERM_OK : TERM_ERR;
}
// AM28F010 chip (Trionic 5.x original)
if (strncmp(flash_type, "28f010", 6) == 0) {
return erase_am28(start_addr, end_addr) ? TERM_OK : TERM_ERR;
}
return TERM_ERR;
}
//-----------------------------------------------------------------------------
/**
Writes a batch of long words to the flash starting from [start_addr]. The
operation interrupts if a break character is received. MCU must be in
background mode.
@param flash_type type of flash chip
@param start_addr block start address
@return status flag
*/
uint8_t write_flash(const char* flash_type, const uint32_t* start_addr)
{
// set up chip-specific functions
bool (*reset_func)(void);
bool (*flash_func)(const uint32_t*, uint16_t);
// AM29Fxxx chips (retrofitted to Trionic 5.x, original to T7)
if (strncmp(flash_type, "29f010", 6) == 0 ||
strncmp(flash_type, "29f400", 6) == 0) {
reset_func = &reset_am29;
flash_func = &flash_am29;
} else if (strncmp(flash_type, "28f010", 6) == 0) {
// AM28F010 chip (Trionic 5.x original)
reset_func = &reset_am28;
flash_func = &flash_am28;
} else {
// unknown flash type
return TERM_ERR;
}
// reset the flash
if (!reset_func()) {
return TERM_ERR;
}
uint32_t curr_addr = *start_addr;
if (strncmp(flash_type, "29f010", 6) == 0) {
curr_addr = 0;
}
int rx_char = 0;
char rx_buf[8];
char* rx_ptr;
uint32_t long_value;
bool ret = true;
// ready to receive data
pc.putc(TERM_OK);
while (true) {
// receive long words from USB
printf("receive long words from USB\r\n");
rx_ptr = rx_buf;
do {
rx_char = pc.getc();
if (rx_char != EOF) {
// have got all characters for one long word
if (rx_ptr > &rx_buf[7]) {
ret = (rx_char == TERM_OK);
break;
}
// save the character
*rx_ptr++ = (char)rx_char;
}
} while (rx_char != TERM_OK && rx_char != TERM_BREAK);
// end writing
printf("end writing\r\n");
if (!ret || rx_char == TERM_BREAK) {
break;
}
// convert value to long word
printf("convert value to long word\r\n");
if (!ascii2int(&long_value, rx_buf, 8)) {
ret = false;
break;
}
printf("long value %08lx \r\n", long_value);
// write the first word
printf("write the first word\r\n");
if (!flash_func(&curr_addr, (uint16_t)(long_value >> 16))) {
ret = false;
break;
}
curr_addr += 2;
// write the second word
printf("write the second word\r\n");
if (!flash_func(&curr_addr, (uint16_t)long_value)) {
ret = false;
break;
}
curr_addr += 2;
// light up the activity LED
ACTIVITYLEDON;
}
// reset flash
return (reset_func() && ret) ? TERM_OK : TERM_ERR;
}
//-----------------------------------------------------------------------------
/**
Writes a BIN file to the flash starting from [start_addr].
The operation ends when no more bytes can be read from the BIN file.
MCU must be in background mode.
@param flash_type type of flash chip
@param start_addr block start address
@return status flag
*/
uint8_t flash_trionic()
{
// Configure the MC68332 register values to prepare for flashing
printf("I am trying to discover what type of Trionic ECU I am connected to...\r\n");
prep_t5_do();
// Work out what type of FLASH chips we want to program
uint8_t make;
uint8_t type;
get_flash_id(&make, &type);
// set up chip-specific functions
bool (*reset_func)();
bool (*flash_func)(const uint32_t*, uint16_t);
uint32_t flash_size;
switch (type) {
case AMD29BL802C:
printf("I have found AMD29BL802C type FLASH chips; I must be connected to a T8 ECU :-)\r\n");
reset_func = &reset_am29;
flash_func = &flash_am29;
flash_size = T8FLASHSIZE;
break;
case AMD29F400B:
case AMD29F400T:
printf("I have found AMD29F400 type FLASH chips; I must be connected to a T7 ECU :-)\r\n");
reset_func = &reset_am29;
flash_func = &flash_am29;
flash_size = T7FLASHSIZE;
break;
case AMD29F010:
case SST39SF010:
case AMICA29010L:
printf("I have found 29/39F010 type FLASH chips; I must be connected to a repaired T5.5 ECU :-)\r\n");
reset_func = &reset_am29;
flash_func = &flash_am29;
flash_size = T55FLASHSIZE;
break;
case ATMEL29C010:
printf("I have found Atmel 29C010 type FLASH chips; I must be connected to a repaired T5.5 ECU :-)\r\n");
reset_func = &reset_am29;
flash_func = NULL;
flash_size = T55FLASHSIZE;
break;
case AMD28F010:
case INTEL28F010:
printf("I have found 28F010 type FLASH chips; I must be connected to a T5.5 ECU :-)\r\n");
reset_func = &reset_am28;
flash_func = &flash_am28;
flash_size = T55FLASHSIZE;
break;
case AMD28F512:
case INTEL28F512:
printf("I have found 28F512 type FLASH chips; I must be connected to a T5.2 ECU :-)\r\n");
reset_func = &reset_am28;
flash_func = &flash_am28;
flash_size = T52FLASHSIZE;
break;
case ATMEL29C512:
printf("I have found Atmel 29C512 type FLASH chips; I must be connected to a repaired T5.2 ECU :-)\r\n");
reset_func = &reset_am29;
flash_func = NULL;
flash_size = T52FLASHSIZE;
break;
default:
// unknown flash type
printf("I could not work out what FLASH chips or TRIONIC ECU I am connected to :-(\r\n");
return TERM_ERR;
}
// reset the FLASH chips
if (!reset_func()) return TERM_ERR;
printf("Checking the FLASH BIN file...\r\n");
FILE *fp = fopen("/local/modified.bin", "r"); // Open "modified.bin" on the local file system for reading
// FILE *fp = fopen("/local/original.bin", "r"); // Open "original.bin" on the local file system for reading
if (!fp) {
printf("Error: I could not find the BIN file MODIFIED.BIN\r\n");;
return TERM_ERR;
}
// obtain file size - it should match the size of the FLASH chips:
fseek (fp , 0 , SEEK_END);
uint32_t file_size = ftell (fp);
rewind (fp);
// read the initial stack pointer value in the BIN file - it should match the value expected for the type of ECU
uint8_t stack_bytes[4] = {0, 0, 0, 0};
uint32_t stack_long = 0;
if (!fread(&stack_bytes[0],1,4,fp)) return TERM_ERR;
rewind (fp);
for(uint32_t i=0; i<4; i++) {
(stack_long <<= 8) |= stack_bytes[i];
}
if (flash_size == T52FLASHSIZE && (file_size != T52FLASHSIZE || stack_long != T5POINTER)) {
fclose(fp);
printf("The BIN file does not appear to be for a T5.2 ECU :-(\r\n");
printf("BIN file size: %#10lx, FLASH chip size: %#010lx, Pointer: %#10lx.\r\n", file_size, flash_size, stack_long);
return TERM_ERR;
}
if (flash_size == T55FLASHSIZE && (file_size != T55FLASHSIZE || stack_long != T5POINTER)) {
fclose(fp);
printf("The BIN file does not appear to be for a T5.5 ECU :-(\r\n");
printf("BIN file size: %#10lx, FLASH chip size: %#010lx, Pointer: %#10lx.\r\n", file_size, flash_size, stack_long);
return TERM_ERR;
}
if (flash_size == T7FLASHSIZE && (file_size != T7FLASHSIZE || stack_long != T7POINTER)) {
fclose(fp);
printf("The BIN file does not appear to be for a T7 ECU :-(\r\n");
printf("BIN file size: %#10lx, FLASH chip size: %#010lx, Pointer: %#10lx.\r\n", file_size, flash_size, stack_long);
return TERM_ERR;
}
if (flash_size == T8FLASHSIZE && (file_size != T8FLASHSIZE || stack_long != T8POINTER)) {
fclose(fp);
printf("The BIN file does not appear to be for a T8 ECU :-(\r\n");
printf("BIN file size: %#10lx, FLASH chip size: %#010lx, Pointer: %#10lx.\r\n", file_size, flash_size, stack_long);
return TERM_ERR;
}
uint32_t curr_addr = 0;
switch (type) {
case AMD29BL802C:
case AMD29F400T:
case AMD29F010:
case SST39SF010:
case AMICA29010L:
case ATMEL29C010:
case AMD28F010:
case INTEL28F010:
case AMD28F512:
case INTEL28F512:
case ATMEL29C512: {
uint8_t flashDriver[] = {\
0x60,0x00,0x04,0x0C,\
0x7C,0x2F,0x2D,0x5C,0x2A,0x0D,0x00,0x00,\
0x02,0x03,0x00,0x03,0x41,0xFA,0xFF,0xF6,\
0x10,0xBB,0x30,0xEE,0x70,0x01,0x4A,0xFA,\
0x52,0x43,0x4E,0x75,\
0x20,0x7C,0x00,0xFF,0xFA,0x00,0x08,0x10,\
0x00,0x04,0x66,0x4E,0xD0,0xFC,0x00,0x04,\
0x10,0xFC,0x00,0x7F,0x08,0x10,0x00,0x03,\
0x67,0xFA,0xD0,0xFC,0x00,0x1C,0x42,0x10,\
0xD0,0xFC,0x00,0x23,0x30,0xBC,0x3F,0xFF,\
0xD0,0xFC,0x00,0x04,0x70,0x07,0x30,0xC0,\
0x30,0xBC,0x68,0x70,0xD0,0xFC,0x00,0x06,\
0x30,0xC0,0x30,0xFC,0x30,0x30,0x30,0xC0,\
0x30,0xBC,0x50,0x30,0xD0,0xFC,0x01,0xBE,\
0x70,0x40,0x30,0xC0,0x30,0x80,0x30,0x3C,\
0x55,0xF0,0x4E,0x71,0x51,0xC8,0xFF,0xFC,\
0x60,0x18,0xD0,0xFC,0x00,0x08,0x30,0xFC,\
0x69,0x08,0x08,0x10,0x00,0x09,0x67,0xFA,\
0x31,0x3C,0x68,0x08,0xD0,0xFC,0x00,0x48,\
0x42,0x50,0x4E,0x75,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x2C,0x3C,0x00,0x00,0x55,0x55,\
0x2E,0x3C,0x00,0x00,0xAA,0xAA,0x2A,0x46,\
0x53,0x8D,0x2C,0x47,0x45,0xF8,0x00,0x00,\
0x47,0xFA,0xFF,0xDE,0x3C,0x87,0x3A,0x86,\
0x3C,0xBC,0x90,0x90,0x36,0xDA,0x36,0x92,\
0x3C,0x87,0x3A,0x86,0x3C,0xBC,0xF0,0xF0,\
0x20,0x3A,0xFF,0xC6,0x72,0x02,0x48,0x41,\
0x74,0x01,0x0C,0x00,0x00,0x25,0x67,0x50,\
0x0C,0x00,0x00,0xB8,0x67,0x4A,0x74,0x04,\
0x0C,0x00,0x00,0x5D,0x67,0x42,0x74,0x01,\
0xE3,0x99,0x0C,0x00,0x00,0xA7,0x67,0x38,\
0x0C,0x00,0x00,0xB4,0x67,0x32,0x74,0x02,\
0x0C,0x00,0x00,0x20,0x67,0x2A,0x0C,0x00,\
0x00,0xA4,0x67,0x24,0x0C,0x00,0x00,0xB5,\
0x67,0x1E,0x74,0x04,0x0C,0x00,0x00,0xD5,\
0x67,0x16,0x74,0x03,0xE3,0x99,0x0C,0x00,\
0x00,0x23,0x67,0x0C,0xE3,0x99,0x0C,0x00,\
0x00,0x81,0x67,0x04,0x72,0x00,0x74,0x00,\
0x47,0xFA,0xFF,0x6A,0x26,0x81,0x47,0xFA,\
0xFF,0x68,0x16,0x82,0x4E,0x75,\
0x45,0x72,0x61,0x73,0x69,0x6E,0x67,0x20,\
0x46,0x4C,0x41,0x53,0x48,0x20,0x63,0x68,\
0x69,0x70,0x73,0x0D,0x0A,0x00,0x41,0xFA,\
0xFF,0xE8,0x70,0x01,0x4A,0xFA,0x12,0x3A,\
0xFF,0x42,0x53,0x01,0x67,0x16,0x53,0x01,\
0x67,0x00,0x00,0xB8,0x53,0x01,0x67,0x00,\
0x01,0x0A,0x53,0x01,0x67,0x00,0x01,0x3A,\
0x60,0x00,0x01,0x3A,0x4B,0xF8,0x00,0x00,\
0x24,0x3A,0xFF,0x1C,0x26,0x02,0x3A,0xBC,\
0xFF,0xFF,0x3A,0xBC,0xFF,0xFF,0x42,0x55,\
0x4A,0x35,0x28,0xFF,0x67,0x28,0x7A,0x19,\
0x1B,0xBC,0x00,0x40,0x28,0xFF,0x42,0x35,\
0x28,0xFF,0x72,0x15,0x4E,0x71,0x51,0xC9,\
0xFF,0xFC,0x1B,0xBC,0x00,0xC0,0x28,0xFF,\
0x72,0x0C,0x4E,0x71,0x51,0xC9,0xFF,0xFC,\
0x4A,0x35,0x28,0xFF,0x66,0x06,0x53,0x82,\
0x66,0xCC,0x60,0x04,0x53,0x45,0x66,0xD0,\
0x42,0x55,0x4A,0x55,0x4A,0x05,0x67,0x00,\
0x00,0xE4,0x24,0x03,0x50,0xC4,0x2A,0x3C,\
0x03,0xE8,0x03,0xE8,0x72,0x20,0x1B,0x81,\
0x28,0xFF,0x1B,0x81,0x28,0xFF,0x32,0x3C,\
0x55,0xF0,0x4E,0x71,0x51,0xC9,0xFF,0xFC,\
0x4E,0xBA,0xFE,0x20,0x1B,0xBC,0x00,0xA0,\
0x28,0xFF,0x72,0x0C,0x4E,0x71,0x51,0xC9,\
0xFF,0xFC,0xB8,0x35,0x28,0xFF,0x66,0x08,\
0x48,0x45,0x53,0x82,0x66,0xE6,0x60,0x04,\
0x53,0x45,0x66,0xC8,0x42,0x55,0x4A,0x55,\
0x4A,0x45,0x67,0x00,0x00,0x98,0x60,0x00,\
0x00,0x90,0x70,0x01,0x42,0x83,0x1D,0x87,\
0x08,0x00,0x1B,0x86,0x08,0x00,0x1D,0xBC,\
0x00,0x80,0x08,0x00,0x1D,0x87,0x08,0x00,\
0x1B,0x86,0x08,0x00,0x1D,0xBC,0x00,0x10,\
0x08,0x00,0x2A,0x00,0x4E,0xBA,0xFD,0xCC,\
0x20,0x05,0x1A,0x30,0x09,0x90,0x08,0x05,\
0x00,0x07,0x66,0x20,0x08,0x05,0x00,0x05,\
0x67,0xE8,0x1A,0x30,0x09,0x90,0x08,0x05,\
0x00,0x07,0x66,0x10,0x1D,0x87,0x08,0x00,\
0x1B,0x86,0x08,0x00,0x1D,0xBC,0x00,0xF0,\
0x08,0x00,0x60,0x40,0x53,0x80,0x67,0xAE,\
0x60,0x36,0x42,0x83,0x3C,0x87,0x3A,0x86,\
0x3C,0xBC,0x00,0x80,0x3C,0x87,0x3A,0x86,\
0x3C,0xBC,0x00,0x10,0x4E,0xBA,0xFD,0x84,\
0x3A,0x15,0x08,0x05,0x00,0x07,0x66,0x18,\
0x08,0x05,0x00,0x05,0x67,0xEE,0x3A,0x15,\
0x08,0x05,0x00,0x07,0x66,0x0A,0x3C,0x87,\
0x3A,0x86,0x3C,0xBC,0x00,0xF0,0x60,0x04,\
0x42,0x80,0x60,0x02,0x70,0x01,0x4E,0x75,\
0x47,0xFB,0x01,0x70,0x00,0x00,0x04,0x4C,\
0x28,0x49,0x24,0x3C,0x00,0x00,0x01,0x00,\
0x12,0x3A,0xFD,0xD8,0x53,0x01,0x67,0x14,\
0x53,0x01,0x67,0x5A,0x53,0x01,0x67,0x00,\
0x00,0xBC,0x53,0x01,0x67,0x00,0x01,0x00,\
0x60,0x00,0x01,0x2E,0x10,0x33,0x28,0xFF,\
0x0C,0x00,0x00,0xFF,0x67,0x28,0x7A,0x19,\
0x19,0xBC,0x00,0x40,0x28,0xFF,0x19,0x80,\
0x28,0xFF,0x72,0x15,0x4E,0x71,0x51,0xC9,\
0xFF,0xFC,0x19,0xBC,0x00,0xC0,0x28,0xFF,\
0x72,0x0C,0x4E,0x71,0x51,0xC9,0xFF,0xFC,\
0xB0,0x34,0x28,0xFF,0x66,0x06,0x53,0x82,\
0x66,0xCA,0x60,0x04,0x53,0x05,0x66,0xD0,\
0x42,0x55,0x4A,0x55,0x4A,0x05,0x67,0x00,\
0x00,0xE8,0x60,0x00,0x00,0xE0,0x20,0x0C,\
0xD0,0x82,0xC0,0xBC,0x00,0x00,0x00,0x01,\
0x08,0x40,0x00,0x00,0x16,0x33,0x28,0xFF,\
0x0C,0x03,0x00,0xFF,0x67,0x48,0x1D,0x87,\
0x08,0x00,0x1B,0x86,0x08,0x00,0x1D,0xBC,\
0x00,0xA0,0x08,0x00,0x19,0x83,0x28,0xFF,\
0xC6,0x3C,0x00,0x80,0x18,0x34,0x28,0xFF,\
0x1A,0x04,0xC8,0x3C,0x00,0x80,0xB8,0x03,\
0x67,0x24,0x08,0x05,0x00,0x05,0x67,0xEC,\
0x18,0x34,0x28,0xFF,0xC8,0x3C,0x00,0x80,\
0xB8,0x03,0x67,0x12,0x1D,0x87,0x08,0x00,\
0x1B,0x86,0x08,0x00,0x1D,0xBC,0x00,0xF0,\
0x08,0x00,0x60,0x00,0x00,0x84,0x53,0x82,\
0x66,0xA6,0x60,0x78,0x36,0x33,0x28,0xFE,\
0x0C,0x43,0xFF,0xFF,0x67,0x3A,0x3C,0x87,\
0x3A,0x86,0x3C,0xBC,0x00,0xA0,0x39,0x83,\
0x28,0xFE,0xC6,0x7C,0x00,0x80,0x38,0x34,\
0x28,0xFE,0x3A,0x04,0xC8,0x7C,0x00,0x80,\
0xB8,0x43,0x67,0x1C,0x08,0x05,0x00,0x05,\
0x67,0xEC,0x38,0x34,0x28,0xFE,0xC8,0x7C,\
0x00,0x80,0xB8,0x43,0x67,0x0A,0x3C,0x87,\
0x3A,0x86,0x3C,0xBC,0x00,0xF0,0x60,0x38,\
0x55,0x82,0x66,0xB8,0x60,0x2E,0x3C,0x87,\
0x3A,0x86,0x3C,0xBC,0xA0,0xA0,0x39,0xB3,\
0x28,0xFE,0x28,0xFE,0x55,0x82,0x66,0xF6,\
0x32,0x3C,0x55,0xF0,0x4E,0x71,0x51,0xC9,\
0xFF,0xFC,0x34,0x3C,0x01,0x00,0x36,0x33,\
0x28,0xFE,0xB6,0x74,0x28,0xFE,0x66,0x08,\
0x55,0x82,0x66,0xF2,0x42,0x80,0x60,0x02,\
0x70,0x01,0x4E,0x75,\
0x4F,0xFB,0x01,0x70,0x00,0x00,0x02,0xF0,\
0x4E,0xBA,0xFC,0x08,0x4E,0xBA,0xFC,0x82,\
0x4E,0xBA,0xFD,0x30,0x4A,0xFA,0x42,0x80,\
0x22,0x40,0x4E,0xBA,0xFE,0x88,0x4A,0xFA,\
0xD2,0xFC,0x01,0x00,0x60,0xF4
};
//if (prep_t5_do() != TERM_OK) return TERM_ERR;
// Set Program counter to start of BDM driver code
uint32_t driverAddress = 0x100000;
if (sysreg_write(0x0, &driverAddress) != TERM_OK) break;
for (uint32_t i = 0; i < sizeof(flashDriver); i++) {
if(memwrite_byte(&driverAddress, flashDriver[i]) != TERM_OK) return false;
driverAddress++;
}
// if (!bdmLoadMemory(flashDriver, driverAddress, sizeof(flashDriver))) break;
timer.reset();
timer.start();
printf("Erasing FLASH chips...\r\n");
printf("This can take up to a minute for a T8,\r\n");
printf("30s for a T7 or 15s for a T5 ECU.\r\n");
// execute the erase algorithm in the BDM driver
// write the buffer - should complete within 200 milliseconds
// Typical and Maximum Chip Programming times are 9 and 27 seconds for Am29BL802C
// Typical Chip erase time for Am29BL802C is 45 secinds, not including 0x00 programming prior to erasure.
// Allow for at least worst case 27 seconds programming to 0x00 + 3(?) * 45 typical erase time (162 seconds)
// Allow at least 200 seconds erase time 2,000 * (100ms + BDM memread time)
// NOTE: 29/39F010 and 29F400 erase times are considerably lower
// if (sysreg_write(0x0, &driverAddress) != TERM_OK) return TERM_ERR;
// break;
do {
if (!bdmRunDriver(0x0, 200000)) {
printf("WARNING: An error occured when I tried to erase the FLASH chips :-(\r\n");
return TERM_ERR;
}
} while (bdmProcessSyscall() == CONTINUE);
// if (!run_bdm_driver(0x0, 200000)) {
// printf("WARNING: An error occured when I tried to erase the FLASH chips :-(\r\n");
// return TERM_ERR;
// }
printf("Erasing took %#.1f seconds.\r\n",timer.read());
printf("Programming the FLASH chips...\r\n");
// ready to receive data
printf(" 0.00 %% complete.\r");
while (curr_addr < flash_size) {
// receive bytes from BIN file - break if no more bytes to get
if (!fread(file_buffer,1,0x100,fp)) {
fclose(fp);
printf("Error reading the BIN file MODIFIED.BIN");
break;
}
if (!bdmLoadMemory((uint8_t*)file_buffer, 0x100700, 0x100)) break;
// write the buffer - should complete within 200 milliseconds
if (!bdmRunDriver(0x0, 200)) break;
// if (!run_bdm_driver(0x0, 200)) break;
printf("%6.2f\r", 100*(float)curr_addr/(float)flash_size );
// make the activity LED twinkle
ACTIVITYLEDON;
curr_addr += 0x100;
}
break;
}
// johnc's original method
case AMD29F400B: /// a sort of dummy 'placeholder' as the 'B' chip isn't ever fitted to T7 ECUS
default: {
timer.reset();
timer.start();
// reset the FLASH chips
printf("Reset the FLASH chip(s) to prepare them for Erasing\r\n");
if (!reset_func()) return TERM_ERR;
switch (type) {
// AM29Fxxx chips (retrofitted to Trionic 5.x; original to T7)
case AMD29BL802C:
case AMD29F400B:
case AMD29F400T:
case AMD29F010:
case SST39SF010:
case AMICA29010L:
printf("Erasing 29BL802/F400/010 type FLASH chips...\r\n");
if (!erase_am29()) {
printf("WARNING: An error occured when I tried to erase the FLASH chips :-(\r\n");
return TERM_ERR;
}
break;
// AM28F010 chip (Trionic 5.x original)
case AMD28F010:
case INTEL28F010:
case AMD28F512:
case INTEL28F512:
printf("Erasing 28F010/512 type FLASH chips...\r\n");
if (!erase_am28(&curr_addr, &flash_size)) {
printf("WARNING: An error occured when I tried to erase the FLASH chips :-(\r\n");
return TERM_ERR;
}
break;
case ATMEL29C010:
case ATMEL29C512:
printf("Atmel FLASH chips do not require ERASEing :-)\r\n");
break;
default:
// unknown flash type - shouldn't get here hence "Strange!"
printf("Strange! I couldn't work out how to erase the FLASH chips in the TRIONIC ECU that I am connected to :-(\r\n");
return TERM_ERR;
}
timer.stop();
printf("Erasing took %#.1f seconds.\r\n",timer.read());
printf("Programming the FLASH chips...\r\n");
timer.reset();
timer.start();
uint16_t word_value = 0;
// ready to receive data
printf(" 0.00 %% complete.\r");
while (curr_addr < flash_size) {
// receive bytes from BIN file
//Get a byte - break if no more bytes to get
if (!fread(&file_buffer[0],1,0x2,fp)) {
fclose(fp);
printf("Error reading the BIN file MODIFIED.BIN");
break;
}
for(uint32_t i=0; i<2; i++) {
(word_value <<= 8) |= file_buffer[i];
}
// write the word if it is not 0xffff
if (word_value != 0xffff) {
if (!flash_func(&curr_addr, word_value)) break;
}
if (!(curr_addr % 0x80)) {
printf("%6.2f\r", 100*(float)curr_addr/(float)flash_size );
// make the activity LED twinkle
ACTIVITYLEDON;
}
curr_addr += 2;
}
}
}
timer.stop();
fclose(fp);
if (curr_addr == flash_size) {
printf("100.00\r\n");
printf("Programming took %#.1f seconds.\r\n",timer.read());
// "Just4pleisure;)" 'tag' in the empty space at the end of the FLASH chip
// Removed for now because it conflicts with some information that Dilemma places in this empty space
// and because it is unsafe for Trionic 8 ECUs which have much bigger BIN files and FLASH chips
// reset_func();
// for (uint8_t i = 0; i < 8; ++i) {
// memread_word(&word_value, &flash_tag[i].addr);
// flash_func(&flash_tag[i].addr, (flash_tag[i].val & word_value));
// }
} else {
printf("\r\n");
printf("Programming took %#.1f seconds.\r\n",timer.read());
printf("WARNING: Oh dear, I couldn't program the FLASH at address 0x%08lx.\r\n", curr_addr);
}
// reset flash
return (reset_func() && (curr_addr == flash_size)) ? TERM_OK : TERM_ERR;
}
//-----------------------------------------------------------------------------
/**
Resets an AM29Fxxx flash memory chip. MCU must be in background mode.
@param none
@return succ / fail
*/
bool reset_am29(void)
{
// execute the reset command
// uint32_t addr = 0xfffe;
// return (memwrite_word(&addr, 0xf0f0) == TERM_OK);
// execute the algorithm
for (uint8_t i = 0; i < 3; ++i) {
if (memwrite_word(&am29_reset[i].addr, am29_reset[i].val) != TERM_OK) return false;
}
return true;
}
//-----------------------------------------------------------------------------
/**
Erases an AM29Fxxx flash memory chip and verifies the result; MCU must be
in background mode.
@return succ / fail
*/
bool erase_am29()
{
// reset flash
if (!reset_am29()) {
return false;
}
printf("Erasing AMD 29Fxxx FLASH chips.\r\n");
printf("This can take up to a minute for a T8,\r\n");
printf("30s for a T7 or 15s for a T5 ECU.\r\n");
// execute the algorithm
for (uint8_t i = 0; i < 6; ++i) {
if (memwrite_word(&am29_erase[i].addr, am29_erase[i].val) != TERM_OK) {
reset_am29();
return false;
}
}
// verify the result
uint32_t addr = 0x0;
uint16_t verify_value;
printf(" 0.0 seconds.\r");
timeout.reset();
timeout.start();
while (timeout.read() < 200.0) {
// Typical and Maximum Chip Programming times are 9 and 27 seconds for Am29BL802C
// Typical Chip erase time for Am29BL802C is 45 secinds, not including 0x00 programming prior to erasure.
// Allow for at least worst case 27 seconds programming to 0x00 + 3(?) * 45 typical erase time (162 seconds)
// Allow at least 200 seconds erase time 2,000 * (100ms + BDM memread time)
// NOTE: 29/39F010 and 29F400 erase times are considerably lower
if (memread_word(&verify_value, &addr) == TERM_OK && verify_value == 0xffff) {
// erase completed normally
reset_am29();
printf("\n");
return true;
}
// make the activity LED twinkle
ACTIVITYLEDON;
printf("%5.1f\r", timeout.read());
}
// erase failed
printf("\n");
reset_am29();
return false;
}
//-----------------------------------------------------------------------------
/**
Writes a word to AM29Fxxx flash memory chip and optionally verifies the
result; MCU must be in background mode.
@param addr destination address
@param val value
@return succ / fail
*/
bool flash_am29(const uint32_t* addr, uint16_t value)
{
// execute the algorithm
for (uint8_t i = 0; i < 3; ++i) {
if (memwrite_word(&am29_write[i].addr, am29_write[i].val) != TERM_OK) {
reset_am29();
return false;
}
}
// write the value
if (memwrite_word(addr, value) != TERM_OK) {
reset_am29();
return false;
}
// verify the result
timeout.reset();
timeout.start();
while (timeout.read_us() < 500) {
// Typical and Maximum Word Programming times are 9us and 360us for Am29BL802C
// Allow at least 500 microseconds program time 500 * (1us + BDM memread time)
// NOTE: 29/39F010 and 29F400 programming times are considerably lower
uint16_t verify_value;
if ((memread_word(&verify_value, addr) == TERM_OK) &&
(verify_value == value)) {
// flashing successful
return true;
}
}
// writing failed
reset_am29();
return false;
}
//-----------------------------------------------------------------------------
/**
Writes a word to a FLASH memory chip and checks the result
MCU must be in background mode.
@param addr BDM driver address (0 to continue)
@param maxtime how long to allow driver to execute (milliseconds)
@return succ / fail
*/
bool run_bdm_driver(uint32_t addr, uint32_t maxtime)
{
// Start BDM driver and allow it up to 200 milliseconds to update 256 Bytes
// Upto 25 pulses per byte, 16us per pulse, 256 Bytes
// 25 * 16 * 256 = 102,400us plus overhead for driver code execution time
// Allowing up to 200 milliseconds seems like a good allowance.
uint32_t driverAddress = addr;
if (run_chip(&driverAddress) != TERM_OK) {
printf("Failed to start BDM driver.\r\n");
return false;
}
timeout.reset();
timeout.start();
// T5 ECUs' BDM interface seem to have problems when the running the CPU and
// sometimes shows the CPU briefly switching between showing BDM mode or that
// the CPU is running.
// I 'debounce' the interface state to workaround this erratic bahaviour
for (uint32_t debounce = 0; debounce < 5; debounce++) {
while (IS_RUNNING) {
debounce = 0;
if (timeout.read_ms() > maxtime) {
printf("Driver did not return to BDM mode.\r\n");
timeout.stop();
return false;
}
}
wait_us(1);
}
timeout.stop();
// Check return code in D0 register (0 - OK, 1 - FAILED)
uint32_t result = 1;
if (adreg_read(&result, 0x0) != TERM_OK) {
printf("Failed to read BDM register.\r\n");
return false;
}
return (result == 1) ? false : true;
}