-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathprotocol.c
More file actions
1246 lines (1162 loc) · 55 KB
/
protocol.c
File metadata and controls
1246 lines (1162 loc) · 55 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
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include "protocol.h"
#define FILEBUF_LENGTH 65536
#define TEST_LOOP_ACK 0
#define B_DOUBLE_SPEED 0x01
#define B_HALF_GAIN 0x10
#define B_P1_DOWN 0x04
#define NULL_MSG 0xFF
#define START_MSG 0x7F
#define INFO_MSG 0x01
#define DATA_MSG 0x00
#define ACK_MSG 0x80
#define ERR_MSG 0x81
#define END_MSG 0x82
#define RDY_MSG 0x84
#define CHGB_MSG 0x8E
#define CHKB_MSG 0x8F
#define XXX_MSG 0x52
#define NULL_STEP 0x00
#define INFO_STEP 0x01
#define CHKB_STEP 0x02
#define CHGB_STEP 0x03
#define UNKNOW_STEP 0x4
#define RDY_STEP 0x05
#define LSTDATA_STEP 0x06
#define FSTDATA_STEP 0x07
#define END_STEP 0x08
unsigned char f_double_speed = 0;
unsigned char f_half_gain = 0;
unsigned char f_p1_down = 0;
static unsigned char UNKNOW_STEP_COUNT = 0;
unsigned char target_info[] = {0x68, 0x00, 0x3B, 0x00, 0x16, 0xBA, 0x16, 0xBA, \
0x16, 0xBA, 0x16, 0xBA, 0x16, 0xBA, 0x16, 0xBA, \
0X16, 0xBA, 0x16, 0xBA, 0x42, 0x43, 0xFD, 0xF0, \
0x02, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, \
0x02, 0x07, 0x16 };
unsigned char target_info_head[] = {0x68, 0x00, 0x3B, 0x00 };
unsigned char package_head[] = {0x46, 0xB9 };
unsigned char package_end = 0x16;
unsigned char package_recflag = 0x68;
unsigned char package_senflag = 0x6A;
unsigned char package_data_baudrate_chk[] = {0xFE, 0xC8, 0x01, 0x70, 0x28, 0x81 };
unsigned char package_data_baudrate_chg[] = {0xFE, 0xC8, 0x01, 0x70, 0x26 };
unsigned char package_data_rdy[] = {0x02 };
static unsigned char baudrate_chk_24M_1200[] = {0x02, 0xE2, 0x28 };
static unsigned char baudrate_chg_24M_1200[] = {0x02, 0xE2, 0x26 };
static unsigned char baudrate_chk_24M_2400[] = {0x01, 0x72, 0x28 };
static unsigned char baudrate_chg_24M_2400[] = {0x01, 0x72, 0x23 };
static unsigned char baudrate_chk_24M_4800[] = {0x00, 0xFE, 0x28 };
static unsigned char baudrate_chg_24M_4800[] = {0x00, 0xFF, 0x1E };
static unsigned char baudrate_chk_24M_9600[] = {0x00, 0x9C, 0x28 };
static unsigned char baudrate_chg_24M_9600[] = {0x00, 0x9C, 0x17 };
static unsigned char baudrate_chk_24M_19200[] = {0x00, 0x4E, 0x28 };
static unsigned char baudrate_chg_24M_19200[] = {0x00, 0x4E, 0x17 };
static unsigned char baudrate_chk_24M_38400[] = {0x00, 0x28, 0x28 };
static unsigned char baudrate_chg_24M_38400[] = {0x00, 0x28, 0x17 };
static unsigned char baudrate_chk_24M_57600[] = {0x00, 0x1A, 0x28 };
static unsigned char baudrate_chg_24M_57600[] = {0x00, 0x1A, 0x17 };
static unsigned char baudrate_chk_24M_115200[] = { };
static unsigned char baudrate_chg_24M_115200[] = { };
static unsigned char baudrate_chk_22M_1200[] = {0x02, 0x80, 0x28 };
static unsigned char baudrate_chg_22M_1200[] = {0x02, 0x80, 0x26 };
static unsigned char baudrate_chk_22M_2400[] = {0x01, 0x40, 0x28 };
static unsigned char baudrate_chg_22M_2400[] = {0x01, 0x40, 0x23 };
static unsigned char baudrate_chk_22M_4800[] = {0x00, 0xEE, 0x28 };
static unsigned char baudrate_chg_22M_4800[] = {0x00, 0xFF, 0x1E };
static unsigned char baudrate_chk_22M_9600[] = {0x00, 0x90, 0x28 };
static unsigned char baudrate_chg_22M_9600[] = {0x00, 0x90, 0x17 };
static unsigned char baudrate_chk_22M_19200[] = {0x00, 0x48, 0x28 };
static unsigned char baudrate_chg_22M_19200[] = {0x00, 0x48, 0x17 };
static unsigned char baudrate_chk_22M_38400[] = {0x00, 0x24, 0x28 };
static unsigned char baudrate_chg_22M_38400[] = {0x00, 0x24, 0x17 };
static unsigned char baudrate_chk_22M_57600[] = {0x00, 0x18, 0x28 };
static unsigned char baudrate_chg_22M_57600[] = {0x00, 0x18, 0x17 };
static unsigned char baudrate_chk_22M_115200[] = {0x00, 0x0C, 0x28 };
static unsigned char baudrate_chg_22M_115200[] = {0x00, 0x0C, 0x17 };
static unsigned char baudrate_chk_16M_1200[] = {0x01, 0xFE, 0x28 };
static unsigned char baudrate_chg_16M_1200[] = {0x01, 0xFF, 0x26 };
static unsigned char baudrate_chk_16M_2400[] = {0x00, 0xFE, 0x28 };
static unsigned char baudrate_chg_16M_2400[] = {0x00, 0xFF, 0x23 };
static unsigned char baudrate_chk_16M_4800[] = {0x00, 0xD0, 0x28 };
static unsigned char baudrate_chg_16M_4800[] = {0x00, 0xD0, 0x1E };
static unsigned char baudrate_chk_16M_9600[] = {0x00, 0x68, 0x28 };
static unsigned char baudrate_chg_16M_9600[] = {0x00, 0x68, 0x17 };
static unsigned char baudrate_chk_16M_19200[] = {0x00, 0x34, 0x28 };
static unsigned char baudrate_chg_16M_19200[] = {0x00, 0x34, 0x17 };
static unsigned char baudrate_chk_16M_38400[] = {0x00, 0x1A, 0x28 };
static unsigned char baudrate_chg_16M_38400[] = {0x00, 0x1A, 0x17 };
static unsigned char baudrate_chk_16M_57600[] = { };
static unsigned char baudrate_chg_16M_57600[] = { };
static unsigned char baudrate_chk_16M_115200[] = { };
static unsigned char baudrate_chg_16M_115200[] = { };
static unsigned char baudrate_chk_12M_1200[] = {0x01, 0x70, 0x28 };
static unsigned char baudrate_chg_12M_1200[] = {0x01, 0x70, 0x26 };
static unsigned char baudrate_chk_12M_2400[] = {0x00, 0xFE, 0x28 };
static unsigned char baudrate_chg_12M_2400[] = {0x00, 0xFF, 0x23 };
static unsigned char baudrate_chk_12M_4800[] = {0x00, 0x9C, 0x28 };
static unsigned char baudrate_chg_12M_4800[] = {0x00, 0x9C, 0x1E };
static unsigned char baudrate_chk_12M_9600[] = {0x00, 0x4E, 0x28 };
static unsigned char baudrate_chg_12M_9600[] = {0x00, 0x4E, 0x17 };
static unsigned char baudrate_chk_12M_19200[] = {0x00, 0x28, 0x28 };
static unsigned char baudrate_chg_12M_19200[] = {0x00, 0x28, 0x17 };
static unsigned char baudrate_chk_12M_38400[] = {0x00, 0x14, 0x28 };
static unsigned char baudrate_chg_12M_38400[] = {0x00, 0x14, 0x17 };
static unsigned char baudrate_chk_12M_57600[] = { };
static unsigned char baudrate_chg_12M_57600[] = { };
static unsigned char baudrate_chk_12M_115200[] = { };
static unsigned char baudrate_chg_12M_115200[] = { };
static unsigned char baudrate_chk_11M_1200[] = {0x01, 0x40, 0x28 };
static unsigned char baudrate_chg_11M_1200[] = {0x01, 0x40, 0x26 };
static unsigned char baudrate_chk_11M_2400[] = {0x00, 0xFE, 0x28 };
static unsigned char baudrate_chg_11M_2400[] = {0x00, 0xFF, 0x23 };
static unsigned char baudrate_chk_11M_4800[] = {0x00, 0x90, 0x28 };
static unsigned char baudrate_chg_11M_4800[] = {0x00, 0x90, 0x1E };
static unsigned char baudrate_chk_11M_9600[] = {0x00, 0x48, 0x28 };
static unsigned char baudrate_chg_11M_9600[] = {0x00, 0x48, 0x17 };
static unsigned char baudrate_chk_11M_19200[] = {0x00, 0x24, 0x28 };
static unsigned char baudrate_chg_11M_19200[] = {0x00, 0x24, 0x17 };
static unsigned char baudrate_chk_11M_38400[] = {0x00, 0x12, 0x28 };
static unsigned char baudrate_chg_11M_38400[] = {0x00, 0x12, 0x17 };
static unsigned char baudrate_chk_11M_57600[] = {0x00, 0x0C, 0x28 };
static unsigned char baudrate_chg_11M_57600[] = {0x00, 0x0C, 0x17 };
static unsigned char baudrate_chk_11M_115200[] = {0x00, 0x06, 0x28 };
static unsigned char baudrate_chg_11M_115200[] = {0x00, 0x06, 0x17 };
static unsigned char baudrate_chk_8M_1200[] = {0x00, 0xFE, 0x28 };
static unsigned char baudrate_chg_8M_1200[] = {0x00, 0xFF, 0x26 };
static unsigned char baudrate_chk_8M_2400[] = {0x00, 0xD0, 0x28 };
static unsigned char baudrate_chg_8M_2400[] = {0x00, 0xD0, 0x23 };
static unsigned char baudrate_chk_8M_4800[] = {0x00, 0x68, 0x28 };
static unsigned char baudrate_chg_8M_4800[] = {0x00, 0x68, 0x1E };
static unsigned char baudrate_chk_8M_9600[] = {0x00, 0x34, 0x28 };
static unsigned char baudrate_chg_8M_9600[] = {0x00, 0x34, 0x17 };
static unsigned char baudrate_chk_8M_19200[] = {0x00, 0x1A, 0x28 };
static unsigned char baudrate_chg_8M_19200[] = {0x00, 0x1A, 0x17 };
static unsigned char baudrate_chk_8M_38400[] = { };
static unsigned char baudrate_chg_8M_38400[] = { };
static unsigned char baudrate_chk_8M_57600[] = { };
static unsigned char baudrate_chg_8M_57600[] = { };
static unsigned char baudrate_chk_8M_115200[] = { };
static unsigned char baudrate_chg_8M_115200[] = { };
static unsigned char baudrate_chk_6M_1200[] = {0x00, 0xFE, 0x28 };
static unsigned char baudrate_chg_6M_1200[] = {0x00, 0xFF, 0x26 };
static unsigned char baudrate_chk_6M_2400[] = {0x00, 0x9C, 0x28 };
static unsigned char baudrate_chg_6M_2400[] = {0x00, 0x9C, 0x23 };
static unsigned char baudrate_chk_6M_4800[] = {0x00, 0x4E, 0x28 };
static unsigned char baudrate_chg_6M_4800[] = {0x00, 0x4E, 0x1E };
static unsigned char baudrate_chk_6M_9600[] = {0x00, 0x28, 0x28 };
static unsigned char baudrate_chg_6M_9600[] = {0x00, 0x28, 0x17 };
static unsigned char baudrate_chk_6M_19200[] = {0x00, 0x14, 0x28 };
static unsigned char baudrate_chg_6M_19200[] = {0x00, 0x14, 0x17 };
static unsigned char baudrate_chk_6M_38400[] = {0x00, 0x0A, 0x28 };
static unsigned char baudrate_chg_6M_38400[] = {0x00, 0x0A, 0x17 };
static unsigned char baudrate_chk_6M_57600[] = { };
static unsigned char baudrate_chg_6M_57600[] = { };
static unsigned char baudrate_chk_6M_115200[] = { };
static unsigned char baudrate_chg_6M_115200[] = { };
static unsigned char baudrate_chk_4M_1200[] = {0x00, 0xD0, 0x28 };
static unsigned char baudrate_chg_4M_1200[] = {0x00, 0xD0, 0x26 };
static unsigned char baudrate_chk_4M_2400[] = {0x00, 0x68, 0x28 };
static unsigned char baudrate_chg_4M_2400[] = {0x00, 0x68, 0x23 };
static unsigned char baudrate_chk_4M_4800[] = {0x00, 0x34, 0x28 };
static unsigned char baudrate_chg_4M_4800[] = {0x00, 0x34, 0x1E };
static unsigned char baudrate_chk_4M_9600[] = {0x00, 0x1A, 0x28 };
static unsigned char baudrate_chg_4M_9600[] = {0x00, 0x1A, 0x17 };
static unsigned char baudrate_chk_4M_19200[] = { };
static unsigned char baudrate_chg_4M_19200[] = { };
static unsigned char baudrate_chk_4M_38400[] = { };
static unsigned char baudrate_chg_4M_38400[] = { };
static unsigned char baudrate_chk_4M_57600[] = { };
static unsigned char baudrate_chg_4M_57600[] = { };
static unsigned char baudrate_chk_4M_115200[] = { };
static unsigned char baudrate_chg_4M_115200[] = { };
static unsigned char databuf_msg[512];
static unsigned int datalen_msg = 0;
static unsigned char filebuf[FILEBUF_LENGTH];
static unsigned int filelen = 0;
static unsigned int fileoffset = 0;
unsigned long target_hz = 12000000;
unsigned int target_baudrate = 1200;
unsigned int down_baudrate = 1200;
static unsigned char step_msg = 0;
static int rec_count_msg = 0;
static unsigned char buf_msg[1024];
/*
***********************************************************
* printf_msg
* print the message receive or send
* buf_name : you can give some note of the message print out
* buf : point to the buf in which the message will be
* print out
* len : the length of the message to be pint out
* return : NULL
***********************************************************
*/
void printf_msg(unsigned char *buf_name, unsigned char *buf, unsigned int len)
{
int i = 0;
printf("%s", buf_name);
for (i = 0; i < len; i ++) {
if (! (i % 16)) {
printf("\n%04X: ", i);
}
printf("%02X ", buf[i]);
}
printf("\n");
}
/*
***********************************************************
* init_msg
* init the message buf
***********************************************************
*/
void init_msg()
{
rec_count_msg = 0;
step_msg = 0;
fileoffset = 0;
datalen_msg = 0;
}
/*
***********************************************************
* treat_receive_target_info_msg
* treat the target information message received from uart
* the message contain these information:
* 1.crystal frequency of the target system
* 2.the type of the target MPU
* 3.the version of the ISP
* 4.current settings of the MPU
* buf : point to the buf used to save the message
* received from the uart
* len : the length of the message in buf
* return: the type of the message, INFO_MSG if received
* complete target infomation message, or else
* return NULL_MSG
***********************************************************
*/
unsigned char treat_receive_target_info_msg(unsigned char *buf, int len)
{
unsigned int i = 0;
unsigned int j = 0;
unsigned long k = 0;
double l = 0;
unsigned char ret = NULL_MSG;
while (i < len) {
buf_msg [rec_count_msg ++] = buf [i ++];
switch (rec_count_msg) {
case 4 : {
if ((buf_msg[0] != target_info_head[0]) || (buf_msg[1] != target_info_head[1]) || \
(buf_msg[2] != target_info_head[2]) || (buf_msg[3] != target_info_head[3]) ) {
memset(buf_msg, 0, rec_count_msg);
rec_count_msg = 0;
}
break;
}
case 20 : {
k = 0;
for (j = 4; j < 20; j += 2) {
k += ((unsigned long)buf_msg[j] << 8);
k += (unsigned long)buf_msg[j + 1];
}
k = k >> 3;
k = k * target_baudrate;
k = k / 5816;
if ((k%100) >= 50) {
k = (k / 100) + 1;
} else {
k = k / 100;
}
if (k == 24) {
target_hz = 24000000;
}
else if (k == 22) {
target_hz = 22118400;
}
else if (k == 16) {
target_hz = 16000000;
}
else if (k == 12) {
target_hz = 12000000;
}
else if (k == 11) {
target_hz = 11059200;
}
else if (k == 8) {
target_hz = 8000000;
}
else if (k == 6) {
target_hz = 6000000;
}
else if (k == 4) {
target_hz = 4000000;
}
break;
}
case 23 : {
if (!(buf_msg[rec_count_msg - 1] & B_HALF_GAIN)) {
f_half_gain = 1; // 1/2 gain
printf("target is 1/2 gain \n");
}
else {
f_half_gain = 0; // full gain
printf("target is full gain \n");
}
if (!(buf_msg[rec_count_msg - 1] & B_DOUBLE_SPEED)) {
f_double_speed = 1; // 6T
printf("target is double speed(6T) \n");
}
else {
f_double_speed = 0; // 12T
printf("target is normal speed(12T) \n");
}
if (!(buf_msg[rec_count_msg - 1] & B_P1_DOWN)) {
f_p1_down = 1; // P1.1 P1.0 pull down
printf("target's P1.1&P1.0 pull down \n");
}
else {
f_p1_down = 0; // P1.1 P1.0 normal
printf("target's P1.1&P1.0 normal \n");
}
printf("\n");
break;
}
case 58 : {
k = 0;
for (j = 0; j < (rec_count_msg -1); j ++) {
k += buf_msg[j];
}
if ((unsigned char)k != buf_msg[rec_count_msg - 1]) {
printf("check target infomation error\n");
memset(buf_msg, 0, rec_count_msg);
rec_count_msg = 0;
}
break;
}
case 59 : {
if (buf_msg[rec_count_msg - 1] != 0x16) {
printf("target information has not end flag\n");
memset(buf_msg, 0, rec_count_msg);
rec_count_msg = 0;
} else {
//if (f_double_speed) {
// target_hz <<= 1;
//}
if (target_hz == 24000000) {
switch (down_baudrate) {
case 1200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_24M_1200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_24M_1200, 3);
break;
case 2400 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_24M_2400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_24M_2400, 3);
break;
case 4800 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_24M_4800, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_24M_4800, 3);
break;
case 9600 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_24M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_24M_9600, 3);
break;
case 19200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_24M_19200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_24M_19200, 3);
break;
case 38400 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_24M_38400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_24M_38400, 3);
break;
case 57600 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_24M_57600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_24M_57600, 3);
down_baudrate = 57600;
break;
case 115200 :
//memcpy(&package_data_baudrate_chk[2], baudrate_chk_24M_115200, 3);
//memcpy(&package_data_baudrate_chg[2], baudrate_chg_24M_115200, 3);
memcpy(&package_data_baudrate_chk[2], baudrate_chk_24M_57600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_24M_57600, 3);
down_baudrate = 57600;
break;
default :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_24M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_24M_9600, 3);
down_baudrate = 9600;
break;
}
}
else if (target_hz == 22118400) {
switch (down_baudrate) {
case 1200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_22M_1200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_22M_1200, 3);
break;
case 2400 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_22M_2400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_22M_2400, 3);
break;
case 4800 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_22M_4800, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_22M_4800, 3);
break;
case 9600 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_22M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_22M_9600, 3);
break;
case 19200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_22M_19200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_22M_19200, 3);
break;
case 38400 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_22M_38400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_22M_38400, 3);
break;
case 57600 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_22M_57600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_22M_57600, 3);
break;
case 115200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_22M_115200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_22M_115200, 3);
break;
default :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_22M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_22M_9600, 3);
down_baudrate = 9600;
break;
}
}
else if (target_hz == 16000000) {
switch (down_baudrate) {
case 1200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_16M_1200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_16M_1200, 3);
break;
case 2400 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_16M_2400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_16M_2400, 3);
break;
case 4800 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_16M_4800, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_16M_4800, 3);
break;
case 9600 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_16M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_16M_9600, 3);
break;
case 19200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_16M_19200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_16M_19200, 3);
break;
case 38400 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_16M_38400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_16M_38400, 3);
break;
case 57600 :
//memcpy(&package_data_baudrate_chk[2], baudrate_chk_16M_57600, 3);
//memcpy(&package_data_baudrate_chg[2], baudrate_chg_16M_57600, 3);
memcpy(&package_data_baudrate_chk[2], baudrate_chk_16M_38400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_16M_38400, 3);
down_baudrate = 38400;
break;
case 115200 :
//memcpy(&package_data_baudrate_chk[2], baudrate_chk_16M_115200, 3);
//memcpy(&package_data_baudrate_chg[2], baudrate_chg_16M_115200, 3);
memcpy(&package_data_baudrate_chk[2], baudrate_chk_16M_38400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_16M_38400, 3);
down_baudrate = 38400;
break;
default :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_16M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_16M_9600, 3);
down_baudrate = 9600;
break;
}
}
else if (target_hz == 12000000) {
switch (down_baudrate) {
case 1200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_12M_1200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_12M_1200, 3);
break;
case 2400 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_12M_2400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_12M_2400, 3);
break;
case 4800 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_12M_4800, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_12M_4800, 3);
break;
case 9600 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_12M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_12M_9600, 3);
break;
case 19200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_12M_19200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_12M_19200, 3);
break;
case 38400 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_12M_38400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_12M_38400, 3);
break;
case 57600 :
//memcpy(&package_data_baudrate_chk[2], baudrate_chk_12M_57600, 3);
//memcpy(&package_data_baudrate_chg[2], baudrate_chg_12M_57600, 3);
memcpy(&package_data_baudrate_chk[2], baudrate_chk_12M_38400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_12M_38400, 3);
down_baudrate = 38400;
break;
case 115200 :
//memcpy(&package_data_baudrate_chk[2], baudrate_chk_12M_115200, 3);
//memcpy(&package_data_baudrate_chg[2], baudrate_chg_12M_115200, 3);
memcpy(&package_data_baudrate_chk[2], baudrate_chk_12M_38400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_12M_38400, 3);
down_baudrate = 38400;
break;
default :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_12M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_12M_9600, 3);
down_baudrate = 9600;
break;
}
}
else if (target_hz == 11059200) {
switch (down_baudrate) {
case 1200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_11M_1200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_11M_1200, 3);
break;
case 2400 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_11M_2400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_11M_2400, 3);
break;
case 4800 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_11M_4800, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_11M_4800, 3);
break;
case 9600 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_11M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_11M_9600, 3);
break;
case 19200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_11M_19200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_11M_19200, 3);
break;
case 38400 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_11M_38400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_11M_38400, 3);
break;
case 57600 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_11M_57600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_11M_57600, 3);
break;
case 115200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_11M_115200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_11M_115200, 3);
break;
default :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_11M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_11M_9600, 3);
down_baudrate = 9600;
break;
}
}
else if (target_hz == 8000000) {
switch (down_baudrate) {
case 1200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_8M_1200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_8M_1200, 3);
break;
case 2400 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_8M_2400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_8M_2400, 3);
break;
case 4800 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_8M_4800, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_8M_4800, 3);
break;
case 9600 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_8M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_8M_9600, 3);
break;
case 19200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_8M_19200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_8M_19200, 3);
break;
case 38400 :
//memcpy(&package_data_baudrate_chk[2], baudrate_chk_8M_38400, 3);
//memcpy(&package_data_baudrate_chg[2], baudrate_chg_8M_38400, 3);
memcpy(&package_data_baudrate_chk[2], baudrate_chk_8M_19200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_8M_19200, 3);
down_baudrate = 19200;
break;
case 57600 :
//memcpy(&package_data_baudrate_chk[2], baudrate_chk_8M_57600, 3);
//memcpy(&package_data_baudrate_chg[2], baudrate_chg_8M_57600, 3);
memcpy(&package_data_baudrate_chk[2], baudrate_chk_8M_19200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_8M_19200, 3);
down_baudrate = 19200;
break;
case 115200 :
//memcpy(&package_data_baudrate_chk[2], baudrate_chk_8M_115200, 3);
//memcpy(&package_data_baudrate_chg[2], baudrate_chg_8M_115200, 3);
memcpy(&package_data_baudrate_chk[2], baudrate_chk_8M_19200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_8M_19200, 3);
down_baudrate = 19200;
break;
default :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_8M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_8M_9600, 3);
down_baudrate = 9600;
break;
}
}
else if (target_hz == 6000000) {
switch (down_baudrate) {
case 1200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_6M_1200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_6M_1200, 3);
break;
case 2400 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_6M_2400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_6M_2400, 3);
break;
case 4800 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_6M_4800, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_6M_4800, 3);
break;
case 9600 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_6M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_6M_9600, 3);
break;
case 19200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_6M_19200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_6M_19200, 3);
break;
case 38400 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_6M_38400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_6M_38400, 3);
break;
case 57600 :
//memcpy(&package_data_baudrate_chk[2], baudrate_chk_6M_57600, 3);
//memcpy(&package_data_baudrate_chg[2], baudrate_chg_6M_57600, 3);
memcpy(&package_data_baudrate_chk[2], baudrate_chk_6M_19200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_6M_19200, 3);
down_baudrate = 38400;
break;
case 115200 :
//memcpy(&package_data_baudrate_chk[2], baudrate_chk_6M_115200, 3);
//memcpy(&package_data_baudrate_chg[2], baudrate_chg_6M_115200, 3);
memcpy(&package_data_baudrate_chk[2], baudrate_chk_6M_38400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_6M_38400, 3);
down_baudrate = 38400;
break;
default :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_6M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_6M_9600, 3);
down_baudrate = 9600;
break;
}
}
else if (target_hz == 4000000) {
switch (down_baudrate) {
case 1200 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_4M_1200, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_6M_1200, 3);
break;
case 2400 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_4M_2400, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_4M_2400, 3);
break;
case 4800 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_4M_4800, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_4M_4800, 3);
break;
case 9600 :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_4M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_4M_9600, 3);
break;
case 19200 :
//memcpy(&package_data_baudrate_chk[2], baudrate_chk_4M_19200, 3);
//memcpy(&package_data_baudrate_chg[2], baudrate_chg_4M_19200, 3);
memcpy(&package_data_baudrate_chk[2], baudrate_chk_4M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_4M_9600, 3);
down_baudrate = 9600;
break;
case 38400 :
//memcpy(&package_data_baudrate_chk[2], baudrate_chk_4M_38400, 3);
//memcpy(&package_data_baudrate_chg[2], baudrate_chg_4M_38400, 3);
memcpy(&package_data_baudrate_chk[2], baudrate_chk_4M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_4M_9600, 3);
down_baudrate = 9600;
break;
case 57600 :
//memcpy(&package_data_baudrate_chk[2], baudrate_chk_4M_57600, 3);
//memcpy(&package_data_baudrate_chg[2], baudrate_chg_4M_57600, 3);
memcpy(&package_data_baudrate_chk[2], baudrate_chk_4M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_4M_9600, 3);
down_baudrate = 9600;
break;
case 115200 :
//memcpy(&package_data_baudrate_chk[2], baudrate_chk_4M_115200, 3);
//memcpy(&package_data_baudrate_chg[2], baudrate_chg_4M_115200, 3);
memcpy(&package_data_baudrate_chk[2], baudrate_chk_4M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_4M_9600, 3);
down_baudrate = 9600;
break;
default :
memcpy(&package_data_baudrate_chk[2], baudrate_chk_4M_9600, 3);
memcpy(&package_data_baudrate_chg[2], baudrate_chg_4M_9600, 3);
down_baudrate = 9600;
break;
}
}
else {
memset(&package_data_baudrate_chk[2], 0, 3);
memset(&package_data_baudrate_chg[2], 0, 3);
down_baudrate = 9600;
}
package_data_baudrate_chk[5] = 0x87;
if (target_hz > 0) package_data_baudrate_chk[5] = 0x83;
if (target_hz > 5000000) package_data_baudrate_chk[5] = 0x82;
if (target_hz > 10000000) package_data_baudrate_chk[5] = 0x81;
if (target_hz > 20000000) package_data_baudrate_chk[5] = 0x80;
l = 32 * down_baudrate;
l = target_hz / l;
l += 0.5;
k = 65536 - (long)l;
//k = 65536 - (target_hz / 32) / down_baudrate;
package_data_baudrate_chk[0] = (unsigned char)(k >> 8);
package_data_baudrate_chk[1] = (unsigned char)k;
package_data_baudrate_chg[0] = (unsigned char)(k >> 8);
package_data_baudrate_chg[1] = (unsigned char)k;
printf_msg("<<<<< Received target_info:", buf_msg, rec_count_msg);
printf("\ntarget_hz is %ld\n\n", target_hz);
ret = INFO_MSG;
memcpy(target_info, buf_msg, rec_count_msg);
rec_count_msg = 0;
}
break;
}
default : {
break;
}
}
}
return ret;
}
/*
***********************************************************
* treat_receive_normal_msg
* treat the message(not target infomation) received
* from uart
* buf : point to the buf used to save the message
* received from the uart
* len : the length of the message in buf
* return: the type of the message, such as
* DATA_MSG :data message
* ACK_MSG :ack message
* ERR_MSG :error message
* END_MSG :programm is complete
* RDY_MSG :ready to down load
* CHGB_MSG :change the baud rate
* CHKB_MSG :check the baud rate
***********************************************************
*/
unsigned char treat_receive_normal_msg(unsigned char *buf, int len)
{
unsigned int i = 0;
unsigned int j = 0;
unsigned int k = 0;
static unsigned char length = 0x06;
unsigned ret = NULL_MSG;
while (i < len) {
//printf("----------0x%02X------------\n", buf[i]);
buf_msg [rec_count_msg ++] = buf [i ++];
if (rec_count_msg == 2) { // head
//i = memcmp(buf_msg, package_head, 2);
if ((buf_msg[0] != package_head[0]) || (buf_msg[1] != package_head[1])) {
memset(buf_msg, 0, rec_count_msg);
rec_count_msg = 0;
length = 0x06;
}
}
else if (rec_count_msg == 3) { // receive flag
if (buf_msg[rec_count_msg - 1] != package_recflag) {
memset(buf_msg, 0, rec_count_msg);
rec_count_msg = 0;
length = 0x06;
}
}
else if (rec_count_msg == 5) { // length
if (buf_msg[rec_count_msg - 1] < 0x06) {
memset(buf_msg, 0, rec_count_msg);
rec_count_msg = 0;
length = 0x06;
} else {
length = buf_msg[rec_count_msg - 1];
}
}
else if (rec_count_msg == 6) { // type
if (buf_msg[rec_count_msg - 1] != DATA_MSG && buf_msg[rec_count_msg - 1] != ACK_MSG && \
buf_msg[rec_count_msg - 1] != ERR_MSG && buf_msg[rec_count_msg - 1] != END_MSG && \
buf_msg[rec_count_msg - 1] != RDY_MSG && buf_msg[rec_count_msg - 1] != CHGB_MSG && \
buf_msg[rec_count_msg - 1] != CHKB_MSG ) {
memset(buf_msg, 0, rec_count_msg);
rec_count_msg = 0;
length = 0x06;
}
}
else if ((rec_count_msg >= 7) && (rec_count_msg < (length + 1))) { // data
databuf_msg[rec_count_msg - 7] = buf_msg[rec_count_msg - 1];
datalen_msg = rec_count_msg - 6;
}
else if ((rec_count_msg - 1)== length) { // check
k = 0;
for (j = 0; j < length; j ++) {
k += buf_msg[j];
}
k ++;
if (buf_msg[rec_count_msg - 1] != (unsigned char)k) {
memset(buf_msg, 0, rec_count_msg);
rec_count_msg = 0;
memset(databuf_msg, 0, datalen_msg);
datalen_msg = 0;
length = 0x06;
}
}
else if (rec_count_msg == (length + 2)) { // end
if (buf_msg[rec_count_msg - 1] != package_end) {
memset(buf_msg, 0, rec_count_msg);
rec_count_msg = 0;
length = 0x06;
memset(databuf_msg, 0, datalen_msg);
datalen_msg = 0;
} else {
ret = buf_msg[5];
printf_msg("<<<<< Receive MSG:", buf_msg, rec_count_msg);
printf("\n");
memset(buf_msg, 0, rec_count_msg);
rec_count_msg = 0;
length = 0x06;
break;
}
}
}
return ret;
}
/*
***********************************************************
* treat_receive_msg
* treat the msg received
* buf : message in which
* len : the message's length
* return : the type of the message should be sent followed
***********************************************************
*/
unsigned char treat_receive_msg(unsigned char *buf, int len)
{
unsigned int i = 0;
unsigned char rec_msg_type = NULL_MSG;
unsigned char ret = NULL_MSG;
if (step_msg == NULL_STEP) {
rec_msg_type = treat_receive_target_info_msg(buf, len);
} else {
rec_msg_type = treat_receive_normal_msg(buf, len);
}
switch (rec_msg_type) {
case NULL_MSG : {
// ret = START_MSG;
break;
}
case INFO_MSG : { // receive targeg infomation package
if (step_msg == NULL_STEP) {
step_msg = INFO_STEP;
// send check burdrate package
ret = CHKB_MSG;
}
break;
}
case DATA_MSG : { // receive data package
ret = NULL_MSG;
break;
}
case ACK_MSG : {
if (step_msg == CHGB_STEP) {
step_msg = UNKNOW_STEP;
UNKNOW_STEP_COUNT = 0;
// send ack package
ret = ACK_MSG;
} else if (step_msg == UNKNOW_STEP) {
if (UNKNOW_STEP_COUNT >= (5 - 2)) {
UNKNOW_STEP_COUNT = 0;
if (TEST_LOOP_ACK) {
// send ack package to loop (can not program the IC)
ret = ACK_MSG;
} else {
step_msg = RDY_STEP;
// send ready package
ret = RDY_MSG;
}
} else {
UNKNOW_STEP_COUNT ++;
// send ack package
ret = ACK_MSG;
}
} else if (step_msg == RDY_STEP) {
step_msg = LSTDATA_STEP;
// send last bank of data package
ret = DATA_MSG;
} else if (step_msg == LSTDATA_STEP) {
step_msg = FSTDATA_STEP;
// send first bank of data package
ret = DATA_MSG;
if ((fileoffset + 0x80) >= filelen) { // send banks of data package is end
step_msg = END_STEP;
fileoffset = 0;
// send end package
ret = END_MSG;
}
} else if (step_msg == FSTDATA_STEP) {
// send follow bank of data package
ret = DATA_MSG;
if ((fileoffset + 0x80) >= filelen) { // send banks of data package is end
step_msg = END_STEP;
fileoffset = 0;
// send end package
ret = END_MSG;
}