/
ECU_Flasher_UNO_EDC15.ino
1854 lines (1681 loc) · 43.9 KB
/
ECU_Flasher_UNO_EDC15.ino
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
/*
EDC15 Toolbox, made by Bi0H4z4rD V0.4
It can read/write flash on these ECUS and read their info.
Supports EDC15P/P+, EDC15V,EDC15VM+ (1MB flash EDC15VM is not supported, do NOT use the tool on this one!! Only 512KB EDC15VM is supported!!);
Improved boot time, now the first stage and bitbang are avoided.
Removed EEPROM operations to have space in flash (Will add on next release)
TODO:
-Need to add cloning function.
-Need to add retry for packets sent while writing to flash without ACK
-Remake the "read info" with the new automatic sw version detection--DONE
-Adapt the automatic sw version detection to all ecus (not only VAG)
-In the definitive hw version, the 33290 CS must be attached to a MCU pin and disabled for programming (LOW)--Pin D5
-Need to add specific errors
-Implement the file indexing--DONE
-Fix the iso_sendstring so it doesnt call the iso_send_byte and turn off/on rx for every byte
-Add support for EDC15VM 1MB flash (Do not use the tool on these yet!!!!)
Know bugs:
-After adding to DB, ECU will be detected as if recovery mode unless you power cycle it.
-After reading info from dead ECU, tool wont be able to detect files (need to check file.close()
*/
//*Trick to save RAM*////
#define flp(string) flashprint(PSTR(string));
#define RXENABLE 2
//****************LCD stuff****************
#include <Wire_400khz.h>
#include <LiquidCrystal_I2C.h>
//#include <MemoryFree.h>
LiquidCrystal_I2C lcd(0x27,20,4);
//This is the push button stuff
const byte OKbuttonPin = 6;
const byte BackbuttonPin = 7;
const byte LeftbuttonPin = 8;
const byte RightbuttonPin = 9;
//This is for the menus:
byte optionset=0;
/*SD stuff
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 10
*/
byte SDbuffer[258];//Buffer to be saved on SD
int SDcounter=0;//counter to know how many bytes will be written to the SD
#include <SdFat.h>
#include <SdFatUtil.h>
SdFat SD;
Sd2Card card;
SdVolume volume;
const uint8_t SD_CHIP_SELECT = 10;
SdFile myFile;
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
boolean fail=0;//Used to determine wether a proccess has failed, and therefor, return to main menu
#define K_IN 0 //RX
#define K_OUT 1 //TX
#define ISORequestByteDelay 7
byte b = 0;
word m=0;
word w=0;
int l=0;
byte Jobdone=0;
byte setspeed = 0;
boolean success = 0;
byte iscrc=0;
byte operation;
byte EcuType;//determines which ecu is connected
byte Specific=0;//used for specific ecu command sets
byte recovery=0;
//*****************************These bytes represent the replies that the ECU gives during common parts********************************
//EDC15 stuff
const char EDC15_ECUBytes[] PROGMEM = {
//0x83, 0x00,
//0x01, 0x7F, 0x00, 0x13,//These bytes are for the speed negotiation
0x83, 0xF1, 0x01, 0x7F, 0x10, 0x78,
0x83, 0xF1, 0x01, 0x50, 0x85, 0x2F,
0x81, 0xF1, 0x01, 0xC2,
0x83, 0xF1, 0x01, 0xC1, 0x6B, 0x8F,
0x03, 0x50, 0x85, 0x87,
0x03,0x67,0x42,0x34,//This is the reply for a correct key sent
0x02,0x74,0xFF
};
const char EDC15_Recovery_ECUBytes[] PROGMEM = {
//0x83, 0xF1,
//0x01, 0xC1, 0x6B, 0x8F,
0x83,0xF1,0x01, 0x50, 0x85, 0x87,
0x87,0xF1,0x01,0xC3,0x00,0x00,0x50,0x03,0xFF,0x00,
0x82,0xF1,0x01,0xC3,0x03,
0x03,0x67,0x42,0x34,//This is the reply for a correct key sent
0x02,0x74,0xFF
};
const char EDC15P_ECUBytes[] PROGMEM = {
0x03,0x7F,0x31,0x22, //This is the string that confirms that ECU got the loader running
0x11,0x36,0x0A,0x01,0x04,0x00,0x05,0x00,0x0B,0x00,0x0C,0x00,0x0D,0x00,0x0F,0x00,0x10,0x00,
0x11,0x36,0x0A,0x01,0x04,0x00,0x05,0x00,0x0B,0x00,0x0C,0x00,0x0D,0x00,0x0F,0x00,0x10,0x00,
0x01,0x7F
};
const char EDC15V_ECUBytes[] PROGMEM = {
0x03,0x7F,0x31,0x22, //This is the string that confirms that ECU got the loader running
};
const char EDC15VM_ECUBytes[] PROGMEM = {
0x03,0x7F,0x31,0x22, //This is the string that confirms that ECU got the loader running
0x11,0x36,0xDB,0x00,0xF2,0xF0,0x40,0x00,0x26,0xF0,0x08,0x00,0x84,0x00,0x72,0xFB,0xF6,0xF0,
0x11,0x36,0xDB,0x00,0xF2,0xF0,0x40,0x00,0x26,0xF0,0x08,0x00,0x84,0x00,0x72,0xFB,0xF6,0xF0,
0x01,0x7F
};
//********************************These bytes represent the replies that Arduino gives during common processes
//EDC15 suff
const char EDC15ArduBytes[] PROGMEM = {
0x82,0x01,0xF1,0x10,0x85,
0x81,0x01,0xF1,0x82,
0x03,0x10,0x85,0x87, //Request for 124800bps.
0x03,0x27,0x41,0x00,
0x08,0x34,0x40,0xE0,0x00,0x00,0x00,0x04,0x20,
//These are post-loader config strings
0x09,0x36,0xC3,0xC3,0x89,0xC5,0x45,0xDA,0x63,0x0B
};
const char EDC15_Recovery_ArduBytes[] PROGMEM = {
0x83,0x01,0xF1,0x10,0x85,0x87, //Request for 124800bps.
0x82,0x01,0xF1,0x83,0x00,
0x87,0x01,0xF1,0x83,0x03,0x00,0x50,0x03,0xFF,0x00,
0x03,0x27,0x41,0x00,
0x08,0x34,0x40,0xE0,0x00,0x00,0x00,0x04,0x20,
//These are post-loader config strings
0x09,0x36,0xC3,0xC3,0x89,0xC5,0x45,0xDA,0x63,0x0B
};
const char EDC15P_ArduBytes[] PROGMEM = {
0x08,0x31,0x02,0x10,0x00,0x00,0x0F,0xBF,0xFF,
0x05,0x23,0x08,0x00,0x00,0x10,
0x05,0x23,0x10,0x00,0x00,0x10,
0x08,0x31,0x02,0x08,0x00,0x00,0x0F,0xBF,0xFF,
};
const char EDC15V_ArduBytes[] PROGMEM = {
0x08,0x31,0x02,0x08,0x00,0x00,0x0F,0xBF,0xFF
};
const char EDC15VM_ArduBytes[] PROGMEM = {
0x08,0x31,0x02,0x10,0x00,0x00,0x0F,0xBF,0xFF,
0x05,0x23,0x08,0x00,0x00,0x10,
0x05,0x23,0x10,0x00,0x00,0x10,
0x08,0x31,0x02,0x08,0x00,0x00,0x0F,0xBF,0xFF,
};
//*******************************These are the loaders**************************
//THIS IS THE LOADER THAT IS BEING SENT WHEN WE WANT TO READ/WRITE THE FLASH ON EDC15
//THIS IS THE FIRST BLOCK. FIRST 0xFF MEANS THAT 255 BYTES WILL BE SENT.
const char EDC15_Loader[] PROGMEM = {
0x00, 0xFF, 0x36, 0xA5, 0xA5, 0x14, 0xE0, 0x00, 0x00, 0x3C, 0xE0, 0x00,
0x00, 0x42, 0xE0, 0x00, 0x00, 0xB0, 0xE3, 0x00, 0x00, 0x02, 0x47, 0x13,
0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00,
0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, 0x0A, 0x00, 0x0B, 0x00,
0x0C, 0x00, 0x0D, 0x00, 0x0E, 0x00, 0x0F, 0x80, 0x0F, 0xA0, 0x0F, 0xC0,
0x0F, 0x00, 0x10, 0x45, 0x2F, 0x7D, 0x64, 0x9B, 0xC3, 0xBE, 0x88, 0x4F,
0xD8, 0xE6, 0xF0, 0xFE, 0xE7, 0xDA, 0x00, 0x24, 0xE1, 0x48, 0x61, 0x2D,
0x39, 0x48, 0x62, 0x2D, 0x16, 0xE6, 0xF1, 0x00, 0xE6, 0xF4, 0x41, 0x01,
0x00, 0x47, 0xF4, 0x23, 0x00, 0x2D, 0x12, 0x47, 0xF4, 0xA2, 0x00, 0x2D,
0x12, 0x47, 0xF4, 0xA3, 0x00, 0x2D, 0x17, 0x47, 0xF4, 0xA4, 0x00, 0x2D,
0x19, 0x47, 0xF4, 0xA5, 0x00, 0x2D, 0x29, 0x47, 0xF4, 0x36, 0x00, 0x2D,
0x2D, 0xDA, 0x00, 0xE8, 0xE0, 0x0D, 0xE1, 0xDA, 0x00, 0x3E, 0xE2, 0x0D,
0xDE, 0xE7, 0xFE, 0x55, 0x00, 0xDA, 0x00, 0x88, 0xE1, 0xDA, 0x00, 0xBE,
0xE2, 0xB7, 0x48, 0xB7, 0xB7, 0xE7, 0xFE, 0x55, 0x00, 0xDA, 0x00, 0x88,
0xE1, 0x0D, 0xD1, 0xDA, 0x00, 0x06, 0xE1, 0xDA, 0x00, 0xBE, 0xE2, 0xE0,
0x02, 0xF4, 0x41, 0x02, 0x00, 0xF6, 0xF2, 0xB4, 0xFE, 0xE7, 0xFE, 0xAA,
0x00, 0xDA, 0x00, 0x88, 0xE1, 0x0D, 0xC3, 0xA9, 0xE1, 0x09, 0xE1, 0xDA,
0x00, 0x88, 0xE1, 0x0D, 0xBE, 0xDA, 0x00, 0x06, 0xE1, 0xDA, 0x00, 0xE6,
0xE2, 0xDA, 0x00, 0x06, 0xE1, 0x0D, 0xB7, 0xDA, 0x00, 0xC4, 0xE1, 0xDA,
0x00, 0x06, 0xE1, 0x0D, 0xB2, 0xDB, 0x00, 0x88, 0x10, 0x88, 0x20, 0xE6,
0xF1, 0x00, 0xE6, 0xE1, 0x14, 0xB9, 0x41, 0xE7, 0xF4, 0x7F, 0x00, 0xE4,
0x41, 0x01, 0x00, 0xDA, 0x00, //End of this block, checksum is 0x2D
//SECOND BLOCK
0x00, 0xFF, 0x36, 0x5E, 0xE1, 0x98, 0x20, 0x98, 0x10, 0xDB, 0x00, 0x88,
0x10, 0x88, 0x20, 0xE6, 0xF1, 0x00, 0xE6, 0xE1, 0x14, 0xB9, 0x41, 0xE7,
0xF4, 0x76, 0x00, 0xE4, 0x41, 0x01, 0x00, 0xDA, 0x00, 0x5E, 0xE1, 0x98,
0x20, 0x98, 0x10, 0xDB, 0x00, 0x88, 0x10, 0x88, 0x20, 0x88, 0x30, 0xE6,
0xF1, 0x00, 0xE6, 0xDA, 0x00, 0x92, 0xE1, 0xE0, 0x02, 0xF1, 0x4E, 0xF1,
0x6E, 0xB9, 0xE1, 0x08, 0x11, 0xDA, 0x00, 0xA2, 0xE1, 0x48, 0x60, 0x3D,
0x09, 0xB9, 0xE1, 0x01, 0x6E, 0x08, 0x11, 0xA0, 0x02, 0x3D, 0xF7, 0x21,
0x6E, 0x41, 0x6E, 0x2D, 0x01, 0xE0, 0x26, 0x98, 0x30, 0x98, 0x20, 0x98,
0x10, 0xDB, 0x00, 0x88, 0x10, 0x88, 0x20, 0x88, 0x30, 0xE6, 0xF1, 0x00,
0xE6, 0xE0, 0x02, 0xA9, 0x41, 0xE1, 0x06, 0x99, 0xE1, 0x01, 0x6E, 0xDA,
0x00, 0x88, 0xE1, 0xA0, 0x02, 0x3D, 0xFA, 0xF1, 0xE6, 0xDA, 0x00, 0x88,
0xE1, 0x98, 0x30, 0x98, 0x20, 0x98, 0x10, 0xDB, 0x00, 0xF7, 0xFE, 0xB0,
0xFE, 0xDA, 0x00, 0x92, 0xE1, 0xDB, 0x00, 0xA7, 0x58, 0xA7, 0xA7, 0x9A,
0xB7, 0xFC, 0x70, 0xF2, 0xF7, 0xB2, 0xFE, 0x7E, 0xB7, 0xDB, 0x00, 0x88,
0x10, 0xE6, 0xF1, 0xFF, 0xFF, 0xE0, 0x06, 0xA7, 0x58, 0xA7, 0xA7, 0x8A,
0xB7, 0x04, 0x70, 0xA0, 0x01, 0x3D, 0xFA, 0xE0, 0x16, 0x0D, 0x03, 0xF2,
0xF7, 0xB2, 0xFE, 0x7E, 0xB7, 0x98, 0x10, 0xDB, 0x00, 0x88, 0x10, 0x88,
0x20, 0x88, 0x30, 0x88, 0x40, 0xE6, 0xF3, 0x00, 0xE6, 0xE0, 0x02, 0xF4,
0x43, 0x02, 0x00, 0xF4, 0x33, 0x03, 0x00, 0xF4, 0x23, 0x04, 0x00, 0xE0,
0x04, 0xA9, 0x83, 0x28, 0x44, 0x7C, 0x14, 0x28, 0x41, 0x08, 0x35, 0xA7,
0x58, 0xA7, 0xA7, 0x99, 0xE3, 0x99, 0xF3, 0xDA, 0x00, 0x08, 0xE2, 0x08,
0x12, 0x18, 0x20, 0xA0, 0x04,
//END OF SECOND BLOCK
//BEGINNING OF THIRD BLOCK
0x00, 0xFF, 0x36, 0x3D, 0xF6, 0x98, 0x40, 0x98, 0x30, 0x98, 0x20, 0x98,
0x10, 0xDB, 0x00, 0x88, 0x10, 0x88, 0x70, 0xE6, 0xF1, 0xAA, 0xAA, 0xE7,
0xFE, 0xAA, 0x00, 0xDA, 0x00, 0x74, 0xE3, 0xE6, 0xF1, 0x55, 0x55, 0xE7,
0xFE, 0x55, 0x00, 0xDA, 0x00, 0x74, 0xE3, 0xE6, 0xF1, 0xAA, 0xAA, 0xE7,
0xFE, 0xA0, 0x00, 0xDA, 0x00, 0x74, 0xE3, 0x98, 0x70, 0x98, 0x10, 0xDA,
0x00, 0x84, 0xE3, 0xDA, 0x00, 0x54, 0xE3, 0xDB, 0x00, 0x88, 0x10, 0x88,
0x20, 0x88, 0x30, 0x88, 0x40, 0x88, 0x50, 0xE6, 0xF3, 0x00, 0xE6, 0xE0,
0x02, 0xF4, 0x43, 0x02, 0x00, 0xF4, 0x33, 0x03, 0x00, 0xF4, 0x23, 0x04,
0x00, 0xE0, 0x04, 0xF4, 0x83, 0x05, 0x00, 0x09, 0x81, 0xB9, 0x83, 0x08,
0x31, 0xE7, 0xFE, 0x36, 0x00, 0xB9, 0xE3, 0x08, 0x31, 0x29, 0x81, 0xDA,
0x00, 0x90, 0xE2, 0xB9, 0xE3, 0x08, 0x31, 0x08, 0x11, 0x18, 0x20, 0xA0,
0x04, 0x3D, 0xF8, 0xDA, 0x00, 0x5E, 0xE1, 0x98, 0x50, 0x98, 0x40, 0x98,
0x30, 0x98, 0x20, 0x98, 0x10, 0xDB, 0x00, 0x88, 0x10, 0xA7, 0x58, 0xA7,
0xA7, 0xDA, 0x00, 0xA4, 0xE2, 0x66, 0xF1, 0xFF, 0x3F, 0xA9, 0xE1, 0x98,
0x10, 0xDB, 0x00, 0x88, 0x10, 0x88, 0x20, 0x5C, 0x22, 0x66, 0xF2, 0xFC,
0x00, 0x1C, 0x21, 0x68, 0x13, 0x70, 0x21, 0xF6, 0xF2, 0x00, 0xFE, 0x98,
0x20, 0x98, 0x10, 0xDB, 0x00, 0x88, 0x30, 0xE0, 0x03, 0xA7, 0x58, 0xA7,
0xA7, 0x08, 0x31, 0x46, 0xF3, 0x00, 0x01, 0x3D, 0xFA, 0x98, 0x30, 0xDB,
0x00, 0x88, 0x30, 0xE0, 0x03, 0xA7, 0x58, 0xA7, 0xA7, 0x08, 0x31, 0x46,
0xF3, 0x10, 0x00, 0x3D, 0xFA, 0x98, 0x30, 0xDB, 0x00, 0x88, 0x10, 0x88,
0x20, 0x88, 0x30, 0x88, 0x70, 0xE6, 0xF3, 0x00, 0xE6, 0xE6, 0xF2, 0x20,
0x00, 0xE6, 0xF1, 0xAA, 0xAA,
//END OF THIRD BLOCK
//BEGINNING OF BLOCK FOUR
0x00, 0xFF, 0x36, 0xE7, 0xFE, 0xAA, 0x00, 0xDA, 0x00, 0x74, 0xE3, 0xE6,
0xF1, 0x55, 0x55, 0xE7, 0xFE, 0x55, 0x00, 0xDA, 0x00, 0x74, 0xE3, 0xE6,
0xF1, 0xAA, 0xAA, 0xE7, 0xFE, 0x80, 0x00, 0xDA, 0x00, 0x74, 0xE3, 0xE6,
0xF1, 0xAA, 0xAA, 0xE7, 0xFE, 0xAA, 0x00, 0xDA, 0x00, 0x74, 0xE3, 0xE6,
0xF1, 0x55, 0x55, 0xE7, 0xFE, 0x55, 0x00, 0xDA, 0x00, 0x74, 0xE3, 0xE6,
0xF1, 0xAA, 0xAA, 0xE7, 0xFE, 0x10, 0x00, 0xDA, 0x00, 0x74, 0xE3, 0xDA,
0x00, 0xBE, 0xE2, 0xE7, 0xFE, 0x80, 0x00, 0xDA, 0x00, 0x54, 0xE3, 0x98,
0x70, 0x98, 0x30, 0x98, 0x20, 0x98, 0x10, 0xDB, 0x00, 0x88, 0x20, 0x88,
0x70, 0x26, 0xF2, 0x18, 0x00, 0xF1, 0xFE, 0x67, 0xFF, 0x80, 0x00, 0xDA,
0x00, 0x90, 0xE2, 0x67, 0xFE, 0x80, 0x00, 0x41, 0xEF, 0x3D, 0xFA, 0x98,
0x70, 0x98, 0x20, 0xDB, 0x00, 0x88, 0x10, 0xDA, 0x00, 0xA4, 0xE2, 0x66,
0xF1, 0xFF, 0x3F, 0xB9, 0xE1, 0x98, 0x10, 0xDB, 0x00, 0x88, 0x10, 0xDA,
0x00, 0xA4, 0xE2, 0x66, 0xF1, 0xFF, 0x3F, 0xB8, 0x71, 0x98, 0x10, 0xDB,
0x00, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3,
0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x89,
0xC5, 0x12, 0xCE, 0xC7, 0xB3, 0xA5, 0xA5, 0x14, 0xE0, 0x00, 0x00, 0x3C,
0xE0, 0x00, 0x00, 0x42, 0xE0, 0x00, 0x00, 0x00, 0xE4, 0x00, 0x00, 0x02,
0x47, 0x13, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00,
0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, 0x0A, 0x00,
0x0B, 0x00, 0x0C, 0x00, 0x0D, 0x00, 0x0E, 0x00, 0x0F, 0x80, 0x0F, 0xA0,
0x0F, 0xC0, 0x0F, 0x00, 0x10, 0x45, 0x2F, 0x7D, 0x64, 0x9B, 0xC3, 0xC3,
0xC3, 0xC3, 0xC3, 0xC3, 0xC3
//END OF BLOCK FOUR
//END OF LOADER
};
void setup()
{
pinMode(10, OUTPUT);//CS for SD
pinMode(OKbuttonPin, INPUT);//Set the PB pins
pinMode(BackbuttonPin, INPUT);
pinMode(LeftbuttonPin, INPUT);
pinMode(RightbuttonPin, INPUT);
pinMode(RXENABLE, OUTPUT);
digitalWrite(RXENABLE,HIGH);
lcd.init();
lcd.backlight();
pinMode(K_OUT, OUTPUT);
pinMode(K_IN, INPUT);
lcd.setCursor(0,0);
flp("EDC15 tool V0.4");
lcd.setCursor(0,1);
flp("Checking SD...");
//SD card INIT
if (!SD.begin(SD_CHIP_SELECT, SPI_FULL_SPEED))
{
lcd.setCursor(0,3);
flp("SD Error...");
fail=1;
while (fail){}
}
lcd.setCursor(14,1);
flp("Done!");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
flp("Select ECU type: ");//if the family selected was EDC15, this menu will show
optionset = ShowMenuOptions(3,"EDC15P","EDC15V","EDC15VM+");
delay(500);
if (optionset == 1)
{
EcuType=0;
}
if (optionset == 2)
{
EcuType=1;
}
if (optionset == 3)
{
EcuType=2;
}
}
void loop()//Now that we know with which ECU we will work, we show the action menu
{
VariablesInit();
serial_rx_off();
serial_tx_off();
digitalWrite(K_OUT, HIGH);
lcd.clear();
lcd.setCursor(0,0);
flp("Select operation:");
byte optionset = ShowMenuOptions(3,"Info","Flash","Tuning");
if (optionset == 1)//This defines the info reading proccess
{
SelectInfo();
}
if (optionset ==2)//Flash operations menu
{
lcd.clear();
lcd.setCursor(0,0);
flp("Flash operations: ");
byte optionset = ShowMenuOptions(2,"Read Flash","Write File to flash","");
if (optionset == 0)//Return to main menu
{
return;
}
if (optionset == 1)//This defines the read proccess
{
lcd.clear();
SelectRead();
}
if (optionset == 2)//Write flash proccess
{
lcd.clear();
lcd.setCursor(0,0);
if (!myFile.open("edc15rd.skf", O_READ))
{
// if the file didn't open, print an error:
flp("File missing in SD");
delay(2000);
return;
}
SelectWrite();
}
}
if (optionset ==3)//Tuning operations menu
{
lcd.clear();
lcd.setCursor(0,0);
flp("Tuning operations: ");
byte optionset = ShowMenuOptions(3,"Add file to DB","Write ORI","Write MOD");
if (optionset == 0)//Return to main menu
{
return;
}
if (optionset == 1)//Add file to the DB
{
lcd.clear();
AddDB(0,0x72000,0x77000,"edc15rd.skf");
return;
}
if (optionset == 2)//Write ORI file
{
lcd.clear();
WriteORI();
}
if (optionset == 3)//Write MOD file
{
lcd.clear();
WriteMOD();
}
}
}
/***********************************Here goes all the operation routines for the menu actions***************/
//***************Read info operations**************//
void SelectInfo()
{
EDC15CommonStart();
if (fail)
{
lcd.setCursor(0,3);
flp("Operation failed...");
while (CheckButtonPressed() == 0)
{
}
return;
}
ReadEDC15Info();
}
//This part is done right after the iso_init. Kept only for future auto-detect ECU features.
void ReadEDC15Info()//Searches in flash for the appropiate data string.
{
ReadEDC15FlashBlock(0x72000,0x77000);
myFile.open("temp", O_READ);
long offset=StringSearch("EDC",0x00,0x5000,3);
if (offset==0)
{
offset=StringSearch("TDI",0x00,0x5000,3);
}
if (offset==0)
{
lcd.clear();
lcd.setCursor(0,0);
flp("No data found...");
lcd.setCursor(0,2);
delay(5000);
return ;
}
lcd.clear();
offset=offset-20;
myFile.seekSet(offset);
lcd.setCursor(0,0);
flp("SW: ");//Print ECU version
for (byte crap=0;crap<11;crap++)
{
char asciiconvert= myFile.read();
lcd.print(asciiconvert);
}
offset=offset+12;
myFile.seekSet(offset);
lcd.setCursor(0,1);
flp("Engine: ");
for (byte crap=0;crap<11;crap++)
{
char asciiconvert= myFile.read();
lcd.print(asciiconvert);
}
offset=offset+31;
myFile.seekSet(offset);
lcd.setCursor(0,2);
flp("HW: ");
for (byte crap=0;crap<11;crap++)
{
char asciiconvert= myFile.read();
lcd.print(asciiconvert);
}
lcd.setCursor(0,3);
flp("ECU SW date: ");
offset=offset+34;
myFile.seekSet(offset);
for (byte crap=0;crap<5;crap++)
{
char asciiconvert= myFile.read();
lcd.print(asciiconvert);
}
myFile.remove();
myFile.close();
while (CheckButtonPressed() == 0)
{
}
}
//*************Flash operations***************/
void SelectRead()
{
EDC15CommonStart();
if (fail)
{
lcd.setCursor(0,3);
flp("Operation failed...");
delay(2000);
return;
}
lcd.clear();
lcd.setCursor(0,0);
flp("Reading flash...");
ReadEDC15Flash();
flp("Done");
delay(1000);
}
void SelectWrite()
{
//We need to load the file we will write before getting here
EDC15CommonStart();
WriteEDC15Flash();
}
void WriteORI()
{
EDC15CommonStart();
for (byte crap=0;crap<1;crap++)
{
String peep=CheckInDB();
if (peep==0)
{
return;
}
String folder=peep+"/ORI";
char filename[11];
folder.toCharArray(filename,12);
myFile.open(filename, O_READ);
}
VariablesInit();
delay(2000);
SelectWrite();
}
void WriteMOD()
{
EDC15CommonStart();
for (byte crap=0;crap<1;crap++)
{
String peep=CheckInDB();
if (peep==0)
{
return;
}
String folder=peep+"/MOD";
char filename[11];
folder.toCharArray(filename,12);
if (!myFile.open(filename, O_READ))
{
lcd.setCursor(0,3);
flp("MOD file not found!")
delay(5000);
return;
}
VariablesInit();
delay(2000);
SelectWrite();
}
}
void ReadEDC15FlashBlock(long BlockStart, long BlockEnd)
{
if (myFile.open("temp", O_READ))
{
myFile.remove();
myFile.close();
}
if (!myFile.open("temp", O_CREAT | O_WRITE)){
// if the file didn't open, print an error:
lcd.clear();
lcd.setCursor(0,0);
flp("Error writing to SD");
lcd.setCursor(0,1);
flp("Check SD lock");
delay(5000);
software_Reset();
// return;
}
long tempstring;
byte ReadString[7];
operation=1;
ReadString[0]= 0x05;
ReadString[1]=0x23;
ReadString[5]=0xFE;//First byte indicates the total number of bytes excluding the checksum, and the second byte is the sender ID (the tool)
//Address is expressed in the third byte starting from 0x08(0x00), and the next two bytes are address too
boolean finished=0;
boolean ReadDone=0;
do{
tempstring=(BlockStart>>16)+0x08;
ReadString[2]=tempstring;
ReadString[4]=BlockStart&0xFFFF;
tempstring=BlockStart&0xFFFF;
ReadString[3]=tempstring>>8;
ReadString[6]=iso_checksum(ReadString,6);
iso_write_byte(7,ReadString);//
ReadEDC15FlashString();
BlockStart=BlockStart+0xFE;
if(BlockStart >= BlockEnd)
{
myFile.close();
finished=1;
}
}while(!finished);
CloseEDC15Ecu();
}
void ReadEDC15Flash()//Function to read the flash
{
byte before=0;
if (myFile.open("edc15rd.skf", O_READ))
{
myFile.remove();
myFile.close();
}
if (!myFile.open("edc15rd.skf", O_CREAT | O_WRITE)){
// if the file didn't open, print an error:
lcd.clear();
lcd.setCursor(0,0);
flp("Error writing to SD");
lcd.setCursor(0,1);
flp("Check SD lock");
delay(5000);
software_Reset();
//return;
}
long ReadStart =0x00;//Last byte before CRC is the lengh of the string you want to receive
long ReadEnd =0x7FEE2;
long tempstring;
byte ReadString[7];
operation=1;
ReadString[0]= 0x05;
ReadString[1]=0x23;
ReadString[5]=0xFE;//First byte indicates the total number of bytes excluding the checksum, and the second byte is the sender ID (the tool)
//Address is expressed in the third byte starting from 0x08(0x00), and the next two bytes are address too
boolean finished=0;
boolean ReadDone=0;
do{
tempstring=(ReadStart>>16)+0x08;
ReadString[2]=tempstring;
ReadString[4]=ReadStart&0xFFFF;
tempstring=ReadStart&0xFFFF;
ReadString[3]=tempstring>>8;
ReadString[6]=iso_checksum(ReadString,6);
iso_write_byte(7,ReadString);//
ReadEDC15FlashString();
if(ReadDone)
{
//Here goes the final string
ReadString[3] = 0xFF;
ReadString[4] = 0xE0;
ReadString[5] = 0x20;
ReadString[6]=iso_checksum(ReadString,6);
iso_write_byte(7,ReadString);//
ReadEDC15FlashString();
myFile.close();
finished=1;
}
ReadStart=ReadStart+0xFE;
if (ReadStart == ReadEnd)
{
ReadDone=1;
}
byte percent=(ReadStart*100)/ReadEnd;
if (percent != before)
{
lcd.setCursor(16,0);
lcd.print(percent);
flp("%");
before=percent;
}
}while(!finished);
CloseEDC15Ecu();
lcd.setCursor(16,0);
}
void ReadEDC15FlashString()//Reads a string of memory sent by the ECU. First byte indicates the total number of bytes, excluding the checksum, that will be received, and the second one is the sender ID(ECU)
{
byte readbyte[2];
iso_read_byte();
readbyte[0]=b;
CheckRec(0x36);
readbyte[1]=b;
SDcounter=0;
while (SDcounter < (readbyte[0])-1)
{
iso_read_byte();
SDbuffer[SDcounter]=b;
SDcounter++;
}
//Check that the checksum is correct
iso_read_byte();
byte checkCRC;
checkCRC=iso_checksum(readbyte,2);
checkCRC=checkCRC+iso_checksum(SDbuffer,SDcounter);
if (checkCRC != b){
lcd.clear();
lcd.setCursor(0,0);
flp("Error: CRC mismatch");
lcd.setCursor(0,1);
flp("Expected ");
lcd.print(checkCRC,HEX);
lcd.setCursor(0,2);
flp("Got ");
lcd.print(b,HEX);
myFile.close();
myFile.open("rec.log", O_CREAT | O_WRITE);
myFile.write(SDbuffer,258);
myFile.close();
fail=1;
while(fail){}
}
if (operation==1)
{
myFile.write(SDbuffer, SDcounter);
}
}
void WriteEDC15Flash()
{
delay(250);
lcd.clear();
lcd.setCursor(0,0);
flp("Erasing....");
for (byte m=0; m<1; m++)//Send the string, made inside a for to save ram
{
byte WriteString[4]={0x02, 0xA5, 0x0F, 0xB6};
iso_write_byte(4,WriteString);//
}
CheckAck();
//Wait for flash to be erased...
delay(4000);
CheckAck();
flp("Done!");
lcd.setCursor(0,1);
flp("Writing boot...");
WriteEDC15FlashBlock(0x7C000,0x7FFFF);
flp("Done!");
lcd.setCursor(0,2);
flp("Writing flash...");
WriteEDC15FlashBlock(0x00000,0x7BFFF);
lcd.setCursor(16,2);
flp("Done");
myFile.close();
CloseEDC15Ecu();
delay(2000);
}
//Writes a block of data for a range of address
void WriteEDC15FlashBlock(long BlockStart, long BlockEnd)
{
byte before=0;
long tempstring;
myFile.seekSet(BlockStart);
byte Writebyte[6];
Writebyte[1]=0x36;
while (BlockEnd>=BlockStart)
{
//extract the numbers from the address counter
tempstring=(BlockStart>>16)+0x20;
Writebyte[2]=tempstring;
tempstring=BlockStart&0xFFFF;
Writebyte[4]=tempstring;
Writebyte[3]=tempstring>>8;
//build the message to be sent
byte stringcount=0;
while (BlockEnd>=BlockStart && stringcount <= 0xF9)
{
BlockStart++;
stringcount++;
}
myFile.read(SDbuffer,stringcount);
Writebyte[0]=stringcount+4;//set the header with the length
Writebyte[5]=iso_checksum(Writebyte,5);//calculate checksumm
Writebyte[5]=Writebyte[5]+iso_checksum(SDbuffer,stringcount);
//Thread composed, now need to send it
iso_write_byte(5,Writebyte);//
iso_write_byte(stringcount, SDbuffer);//
byte chk[]={Writebyte[5]};
iso_write_byte(1,chk);//send the checksumm
CheckAck();
if (BlockEnd == 0x7BFFF)
{
byte percent=(BlockStart*100)/BlockEnd;
if (percent != before)
{
lcd.setCursor(16,2);
lcd.print(percent);
flp("%");
before=percent;
}
}
}
}
//***********************Loader operations************************//
void SendLoader()
{
lcd.setCursor(0,2);
flp("Send Loader...");
setspeed = 3;
delay(25);
SendLoaderString(9,3);
//Start sending the loader
for (byte r=0;r<4;r++)
{
Send_Loader(257);
}
//Loader sent
iso_sendstring(10);
CheckAck();
delayMicroseconds(4510);
if (EcuType == 0)
{
SendEDC15PLoader();
}
if (EcuType == 1)
{
SendEDC15VLoader();
}
if (EcuType == 2)
{
SendEDC15VMLoader();
}
flp("Done!");
}
//This part is for the specific replies that EDC15P ecu gives to the loader
void SendEDC15PLoader()
{
Specific=1;
m=0x00;
w=0x00;
SendLoaderString(9,4);
SendLoaderString(6,18);
SendLoaderString(6,18);
SendLoaderString(9,2);
}
//This part is for the specific replies that EDC15V ecu gives to the loader
void SendEDC15VLoader()
{
Specific=1;
m=0x00;
w=0x00;
SendLoaderString(9,4);
}
void SendEDC15VMLoader()
{
Specific=1;
m=0x00;
w=0x00;
SendLoaderString(9,4);
SendLoaderString(6,18);
SendLoaderString(6,18);
SendLoaderString(9,2);
}
void SendLoaderString(byte a, byte f)
{
iso_sendstring(a);
iso_readstring(f);
delayMicroseconds(4510);
}
//Sends the EDC15 loader
void Send_Loader(int leng)
{
int tmpc = 0;
leng=leng+l;
while (l<leng)
{
b=pgm_read_byte(&EDC15_Loader[l]);
SDbuffer[tmpc]=b;
l++;
tmpc++;
}
SDbuffer[tmpc]= 0;
SDbuffer[tmpc]=iso_checksum(SDbuffer,tmpc);
tmpc++;
iso_write_byte(tmpc,SDbuffer);//
CheckAck();
delayMicroseconds(4510);
}
////*****************Seed/Key operations**************//////
void ProcessKey()
{
long Key1;
long Key2;
long Key3 = 0x3800000;
long tempstring;
tempstring = SDbuffer[3];
tempstring = tempstring<<8;
long KeyRead1 = tempstring+SDbuffer[4];
tempstring = SDbuffer [5];
tempstring = tempstring<<8;
long KeyRead2 = tempstring+SDbuffer[6];
//Process the algorithm
if (EcuType == 0)
{
Key1=0xDA1C;//EDC15P keys
Key2=0xF781;
}
if (EcuType == 1)
{
Key1=0x508D;//EDC15V keys
Key2=0xA647;
}
if (EcuType == 2)
{
Key1=0xF25E;//EDC15VM+ keys
Key2=0x6533;
}
for (byte counter=0;counter<5;counter++)
{
long temp1;
long KeyTemp = KeyRead1;
KeyTemp = KeyTemp&0x8000;
KeyRead1 = KeyRead1 << 1;
temp1=KeyTemp&0x0FFFF;
if (temp1 == 0)
{
long temp2 = KeyRead2&0xFFFF;
long temp3 = KeyTemp&0xFFFF0000;
KeyTemp = temp2+temp3;
KeyRead1 = KeyRead1&0xFFFE;
temp2 = KeyTemp&0xFFFF;
temp2 = temp2 >> 0x0F;
KeyTemp = KeyTemp&0xFFFF0000;
KeyTemp = KeyTemp+temp2;
KeyRead1 = KeyRead1|KeyTemp;
KeyRead2 = KeyRead2 << 0x01;
}
else
{
long temp2;
long temp3;
long temp4;
KeyTemp = KeyRead2+KeyRead2;
KeyRead1 = KeyRead1&0xFFFE;
temp2=KeyTemp&0xFF;
temp2= temp2|1;
temp3=Key3&0xFFFFFF00;
Key3 = temp2+temp3;
Key3 = Key3&0xFFFF00FF;
Key3 = Key3|KeyTemp;
temp2 = KeyRead2&0xFFFF;
temp3 = KeyTemp&0xFFFF0000;
KeyTemp = temp2+temp3;
temp2 = KeyTemp&0xFFFF;
temp2 = temp2 >> 0x0F;
KeyTemp = KeyTemp&0xFFFF0000;
KeyTemp = KeyTemp+temp2;
KeyTemp = KeyTemp|KeyRead1;
Key3 = Key1^Key3;
KeyTemp = Key2^KeyTemp;
KeyRead2 = Key3;
KeyRead1 = KeyTemp;
}
}
//Done with the key generation
KeyRead2=KeyRead2&0xFFFF;//Clean first and secong word from garbage
KeyRead1=KeyRead1&0xFFFF;
lcd.setCursor(0,1);
flp("Bypass auth...");
SDbuffer[0]=0x06;
SDbuffer[1]=0x27;
SDbuffer[2]=0x42;
//Extract the key bytes
tempstring = KeyRead1;
SDbuffer[4]=KeyRead1;
tempstring = tempstring>>8;