-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathccp.c
1626 lines (1323 loc) · 47.2 KB
/
ccp.c
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
/*****************************************************************************
| Project Name: C C P - Driver
| File Name: CCP.C
|
| Description:
| Implements the CCP module .
| used by the CANape CAN Calibration Tool.
|
|-----------------------------------------------------------------------------
| C O P Y R I G H T
|-----------------------------------------------------------------------------
| Copyright (c) 2001-2003 by Vector Informatik GmbH. All rights reserved.
|-----------------------------------------------------------------------------
| A U T H O R I D E N T I T Y
|-----------------------------------------------------------------------------
| Initials Name Company
| -------- --------------------- -------------------------------------
| Bus Sabine B�cherl Vector Informatik GmbH
| Ds Sven Deckardt Vector Informatik GmbH
| Hp Armin Happel Vector Informatik GmbH
| Tri Frank Triem Vector Informatik GmbH
| Za Rainer Zaiser Vector Informatik GmbH
|-----------------------------------------------------------------------------
| R E V I S I O N H I S T O R Y
|-----------------------------------------------------------------------------
| Date Version Author Description
| ---------- ------- ------ -----------------------------------------------
| 2000-24-09 1.29.00 Za - New define CCP_CHECKSUM_BLOCKSIZE
| 2000-29-11 1.30.00 Za - #ifndef CCP_EXTERNAL_STATION_ID
| 2001-08-02 1.31.00 Za - new define CCP_DAQ_BASE_ADDR
| - new function ccpGetDaqPointer
| 2001-30-05 1.32.00 Za - Reserved word "data" in KEIL Compiler for C5x5
| - Prefix CCP_ for all #defines
| 2001-14-09 1.33.00 Za - #define CCP_ODT_ENTRY_SIZE
| - #define CCP_INTEL,CCP_MOTOROLA
| 2001-28-10 1.34.00 Za - ccpSend return value removed
| - Transmission error handling should be done by the user
| 2002-08-04 1.35.00 Za - #define CCP_CPUTYPE_32BIT
| - Max checksum block size is DWORD on 32 bit CPUs
| 2002-02-06 1.36.00 Za - #undef CCP_DAQ for drivers without DAQ fixed
| - double - float conversion for SHORT_UPLOAD, DNLOAD and DAQ
| 2002-17-07 1.37.00 Ds - Fixed the version nr. because the version was in
| the comment 1.36 but 135 was define.
| - Set #define CCP_DRIVER_VERSION to 137
| 2002-14-11 1.37.01 Hp - define CCP_MAX_DAQ only if CCP_DAQ is defined
| 2002-27-11 1.37.02 Ds - delete the query of extended id
| 2003-05-28 1.37.02 Bus - added V_MEMROM0
| 2003-08-11 1.37.03 Tri - implemented P_MEM_ROM and P_MEM_RAM to support M16C Mitsubishi.
| 2003-10-14 1.38.00 Tri - version skipped due to special version for TMS320
| 2003-10-14 1.39.00 Tri - version skipped due to special version for TMS320
| 2003-10-14 1.40.00 Tri - merge of versions: 1.37.03, 1.37.02
| 2003-10-16 1.41.00 Ds - minor bugfix set ROM to CCP_ROM
| 2003-10-16 1.41.01 Ds - change the position of CCP_ROM
| 2003-10-21 1.42.00 Tri - change the position of CCP_ROM
|***************************************************************************/
/* CCP Definitions and Parameters */
#include "ccp.h"
#ifndef C_DISABLE_CCP
/******************************************************************************/
/* Version check */
/******************************************************************************/
#if( CCP_DRIVER_VERSION != 142)
#error "Source and Header file of CCP-Module inconsistent!"
#endif
#if( CCP_DRIVER_BUGFIX_VERSION != 0)
#error "Source and Header file of CCP-Module inconsistent!"
#endif
#if( CCP_DRIVER_VERSION > 255)
#error "Version decreased in CCP-Module"
#endif
/*--------------------------------------------------------------------------*/
/* Performance measurements */
#ifndef CCP_PROFILE
#define SET_PORT_BIT(i)
#define RST_PORT_BIT(i)
#else
/*
t[0] - ccpCommand
t[1] - ccpCallBack
t[2] - ccpBackground
t[3] - ccpDaq
*/
unsigned int t0[4],t[4];
#define SET_PORT_BIT(i) t0[i-1]=ccpGetTimestamp();
#define RST_PORT_BIT(i) t[i-1]=T3-t0[i-1];
#endif
/*--------------------------------------------------------------------------*/
/* Test */
#ifdef CCP_TESTMODE
#include <stdio.h>
static void ccpPrintCANapeSettings( void );
static void ccpPrintDaqList( CCP_BYTE daq );
#endif
/*--------------------------------------------------------------------------*/
/* ROM */
/*--------------------------------------------------------------------------*/
/*
Identification
Must be 0 terminated !!
This string is used by CANape as the ASAP2 database filename
The extension .A2L or .DB is added by CANape
*/
#ifdef CCP_EXTERNAL_STATION_ID
CCP_MEMROM0 extern CCP_ROM CCP_BYTE ccpStationId[];
#else
CCP_MEMROM0 CCP_ROM CCP_BYTE ccpStationId[] = CCP_STATION_ID;
#endif
/*--------------------------------------------------------------------------*/
/* RAM */
/*--------------------------------------------------------------------------*/
/*
The following structure containes all RAM locations needed by the CCP driver
It has to be word aligned on a C167 !!!
*/
/* ##Hp - rename struct ccp */
CCP_RAM struct ccp ccp;
/*--------------------------------------------------------------------------*/
/* CODE */
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* Transmit */
/*--------------------------------------------------------------------------*/
/* Send a CRM, if no other message is pending */
void ccpSendCrm( void ) {
CCP_DISABLE_INTERRUPT;
if (ccp.SendStatus&CCP_SEND_PENDING) {
ccp.SendStatus |= CCP_CRM_REQUEST;
} else {
ccp.SendStatus |= CCP_CRM_PENDING;
ccpSend(ccp.Crm);
}
CCP_ENABLE_INTERRUPT;
}
/* Send a DTM, if no other message is pending */
#ifdef CCP_DAQ
#ifndef CCP_SEND_QUEUE
static void ccpSendDtm( void ) {
CCP_DISABLE_INTERRUPT;
if (ccp.SendStatus&CCP_SEND_PENDING) {
ccp.SendStatus |= CCP_DTM_REQUEST;
} else {
ccp.SendStatus |= CCP_DTM_PENDING;
ccpSend(ccp.Dtm);
}
CCP_ENABLE_INTERRUPT;
}
#endif
#endif
/*--------------------------------------------------------------------------*/
/* Transmit Queue */
/*--------------------------------------------------------------------------*/
#ifdef CCP_SEND_QUEUE
void ccpQueueInit(void) {
ccp.Queue.len = 0;
ccp.Queue.rp = 0;
}
CCP_BYTE ccpQueueWrite(ccpMsg_t *msg) {
if (ccp.Queue.len>=CCP_SEND_QUEUE_SIZE) return 0;
ccp.Queue.msg[(ccp.Queue.rp+ccp.Queue.len)%CCP_SEND_QUEUE_SIZE] = *msg;
ccp.Queue.len++;
return 1;
}
#endif
/*--------------------------------------------------------------------------*/
/* Handle MTAs (Memory-Transfer-Address) */
/*--------------------------------------------------------------------------*/
/* Assign a pointer to a MTA */
#define ccpSetMTA(n,p) ccp.MTA[n] = p;
/* Write n bytes */
static CCP_BYTE ccpWriteMTA( CCP_BYTE n, CCP_BYTE size, CCP_BYTEPTR d ) {
/* EEPROM write access */
#ifdef CCP_WRITE_EEPROM
CCP_BYTE r = ccpCheckWriteEEPROM(ccp.MTA[n],size,d);
if (r) { /* EEPROM write access */
ccp.MTA[n] += size;
return r;
}
#endif
/* Checked ram memory write access */
#ifdef CCP_WRITE_PROTECTION
if (!ccpCheckWriteAccess(ccp.MTA[n],size)) {
ccp.MTA[n] += size;
return CCP_WRITE_DENIED;
}
#endif
/* double conversion */
#ifdef CCP_DOUBLE_FLOAT
if (size==8) {
*(double*)ccp.MTA[n] = *(float*)d;
#ifdef CCP_TESTMODE
if (gDebugLevel>=2) CCPPRINT("[ccpWriteMTA] conversion -> double %g\n",*(double*)ccp.MTA[n]);
#endif
ccp.MTA[n] += 8;
return CCP_WRITE_OK;
}
#endif
while (size-->0) {
/* COSMIC Compiler Bug: *(ccp.MTA[n]++) = *(d++); */
*(ccp.MTA[n]) = *d;
ccp.MTA[n]++;
d++;
}
return CCP_WRITE_OK;
}
/* Read n bytes */
static void ccpReadMTA( CCP_BYTE n, CCP_BYTE size, CCP_BYTEPTR d ) {
/* EEPROM read access */
#ifdef CCP_READ_EEPROM
if (ccpCheckReadEEPROM(ccp.MTA[n],size,d)) {
ccp.MTA[n] += size;
return;
}
#endif
/* double conversion */
#ifdef CCP_DOUBLE_FLOAT
if (size==8) {
*(float*)d = (float)(*(double*)(ccp.MTA[n]));
#ifdef CCP_TESTMODE
if (gDebugLevel>=2) CCPPRINT("[ccpReadMTA] conversion -> float %g\n",(double)(*(float*)d));
#endif
return;
}
#endif
while (size-->0) {
*d = *(ccp.MTA[n]);
d++;
ccp.MTA[n]++;
}
}
/*--------------------------------------------------------------------------*/
/* Data Aquisition Setup */
/*--------------------------------------------------------------------------*/
#ifdef CCP_DAQ
/* Clear DAQ list */
static CCP_BYTE ccpClearDaqList( CCP_BYTE daq ) {
CCP_BYTEPTR p;
CCP_BYTEPTR pl;
if (daq>=CCP_MAX_DAQ) return 0;
/* Clear this daq list to zero */
p = (CCP_BYTEPTR)&ccp.DaqList[daq];
pl = p+sizeof(ccpDaqList_t);
while (p<pl) *p++ = 0;
/* @@@@ Not DAQ list specific */
ccp.SessionStatus |= SS_DAQ;
#ifdef CCP_SEND_SINGLE
ccp.CurrentDaq = 0;
ccp.CurrentOdt = 0;
#endif
#ifdef CCP_SEND_QUEUE
ccpQueueInit();
#endif
return CCP_MAX_ODT;
}
/* Prepare DAQ */
static CCP_BYTE ccpPrepareDaq( CCP_BYTE daq, CCP_BYTE last, CCP_BYTE eventChannel, CCP_WORD prescaler ) {
if (daq>=CCP_MAX_DAQ) return 0;
ccp.DaqList[daq].eventChannel = eventChannel;
if (prescaler==0) prescaler = 1;
ccp.DaqList[daq].prescaler = prescaler;
ccp.DaqList[daq].cycle = 1;
ccp.DaqList[daq].last = last;
ccp.DaqList[daq].flags = DAQ_FLAG_PREPARED;
return 1;
}
/* Start DAQ */
static CCP_BYTE ccpStartDaq( CCP_BYTE daq ) {
if (daq>=CCP_MAX_DAQ) return 0;
ccp.DaqList[daq].flags = DAQ_FLAG_START;
ccp.SessionStatus |= SS_RUN;
#ifdef CCP_TIMESTAMPING
ccpClearTimestamp();
#endif
return 1;
}
/* Start all prepared DAQs */
static void ccpStartAllPreparedDaq(void) {
CCP_BYTE q;
for (q=0;q<CCP_MAX_DAQ;q++) {
if (ccp.DaqList[q].flags==DAQ_FLAG_PREPARED) ccp.DaqList[q].flags = DAQ_FLAG_START;
}
ccp.SessionStatus |= SS_RUN;
#ifdef CCP_TIMESTAMPING
ccpClearTimestamp();
#endif
}
/* Stop DAQ */
static void ccpStopDaq ( CCP_BYTE daq ) {
CCP_BYTE i;
if (daq>=CCP_MAX_DAQ) return;
ccp.DaqList[daq].flags = 0;
/* check if all DAQ lists are stopped */
for (i=0;i<CCP_MAX_DAQ;i++) {
if (ccp.DaqList[i].flags&DAQ_FLAG_START) return;
}
ccp.SessionStatus &= ~SS_RUN;
}
/* Stop all DAQs */
static void ccpStopAllDaq( void ) {
CCP_BYTE q;
for (q=0;q<CCP_MAX_DAQ;q++) ccp.DaqList[q].flags = 0;
ccp.SessionStatus &= ~SS_RUN;
}
/*--------------------------------------------------------------------------*/
/* Data Aquisition Processor */
/*--------------------------------------------------------------------------*/
#ifndef CCP_SEND_SINGLE
/* Sample and transmit a DTM */
static int ccpSampleAndTransmitDtm( CCP_BYTE pid, CCP_BYTE daq, CCP_BYTE odt ) {
#ifdef CCP_SEND_QUEUE
CCP_BYTE dtm[8];
#else
#define dtm ccp.Dtm
#endif
#ifdef CCP_DAQ_BASE_ADDR
CCP_BYTEPTR p;
#else
CCP_DAQBYTEPTR p;
#endif
#ifdef CCP_ODT_ENTRY_SIZE
CCP_BYTE s;
CCP_BYTE *d,*dl;
ccpOdtEntry_t *e,*el;
#else
CCP_BYTE i;
ccpOdtEntry_t *e;
#endif
/* PID */
dtm[0] = pid;
/* Assure data consistency */
CCP_DISABLE_INTERRUPT;
/* Sample */
#ifdef CCP_ODT_ENTRY_SIZE
e = &ccp.DaqList[daq].odt[odt][0];
el = e+8;
d = &dtm[1];
dl = d+7;
while (d<dl && e<el && e->ptr) {
#ifdef CCP_DAQ_BASE_ADDR
p = (CCP_BYTEPTR)( e->ptr ) + CCP_DAQ_BASE_ADDR;
#else
p = e->ptr;
#endif
s = e->siz;
#ifdef CCP_DOUBLE_FLOAT
if (s==8) {
*(float*)d = (float)(*(double*)p);
s = 4;
#ifdef CCP_TESTMODE
if (gDebugLevel>=2) CCPPRINT("[ccpSampleAndTransmitDtm] conversion -> float %g\n",*(float*)d);
#endif
} else
#endif
if (s==4) {
*(CCP_DWORD*)d = *(CCP_DWORD*)p;
} else if (s==2) {
*(CCP_WORD*)d = *(CCP_WORD*)p;
} else {
*d = *p;
}
d += s;
e++;
}
#else
e = &ccp.DaqList[daq].odt[odt][0];
for (i=1;i<8;i++) {
#ifdef CCP_DAQ_BASE_ADDR
p = (CCP_BYTEPTR)( (e++)->ptr ) + CCP_DAQ_BASE_ADDR;
#else
p = (e++)->ptr;
#endif
if (p) dtm[i] = *p;
}
#endif
/* Optional for CANape: Put a timestamp in the first ODT (Byte6+7) of each DAQ */
#ifdef CCP_TIMESTAMPING
if (odt==0) {
*(CCP_WORD*)&dtm[6] = ccpGetTimestamp();
}
#endif
/* Queue or transmit the DTM */
#ifdef CCP_SEND_QUEUE
if (ccp.SendStatus&CCP_SEND_PENDING) {
if (!ccpQueueWrite((ccpMsg_t*)dtm)) {
/* Overun */
CCP_ENABLE_INTERRUPT;
return 0;
}
} else {
ccp.SendStatus |= CCP_DTM_PENDING;
ccpSend(dtm);
}
#else
if (ccp.SendStatus&CCP_DTM_REQUEST) {
/* Overun */
CCP_ENABLE_INTERRUPT;
return 0;
}
if (ccp.SendStatus&CCP_SEND_PENDING) {
ccp.SendStatus |= CCP_DTM_REQUEST;
} else {
ccp.SendStatus |= CCP_DTM_PENDING;
ccpSend(dtm);
}
#endif
CCP_ENABLE_INTERRUPT;
return 1;
}
#else
/* Sample and transmit the next DTM in SEND_SINGLE mode */
static void ccpSampleAndSendNextDtm( void ) {
CCP_BYTE i,j;
CCP_DAQBYTEPTR p;
ccpOdtEntry_t *e;
/* Request for DTM transmission pending */
if (ccp.SendStatus&CCP_DTM_REQUEST) return;
/* Find a DAQ list marked for transmission */
for (i=0;i<CCP_MAX_DAQ;i++) {
if (ccp.DaqList[ccp.CurrentDaq].flags&DAQ_FLAG_SEND) {
/* PID */
ccp.Dtm[0] = ccp.CurrentDaq*CCP_MAX_ODT+ccp.CurrentOdt;
/* Sample */
e = &ccp.DaqList[ccp.CurrentDaq].odt[ccp.CurrentOdt][0];
for (j=1;j<8;j++) {
p = (e++)->ptr;
if (p) ccp.Dtm[j] = *p;
}
/* Send */
ccpSendDtm();
/* Increment ODT */
if (++ccp.CurrentOdt>ccp.DaqList[ccp.CurrentDaq].last) {
/* DAQ list done */
ccp.CurrentOdt = 0;
ccp.DaqList[ccp.CurrentDaq].flags &= ~DAQ_FLAG_SEND;
/* Increment DAQ */
if (++ccp.CurrentDaq>=CCP_MAX_DAQ) ccp.CurrentDaq = 0;
}
break;
} else {
/* Increment DAQ */
if (++ccp.CurrentDaq>=CCP_MAX_DAQ) ccp.CurrentDaq = 0;
}
}
}
#endif
/* Data aquisition */
void ccpDaq( CCP_BYTE eventChannel )
{
CCP_BYTE q,o;
#ifndef CCP_SEND_SINGLE
CCP_BYTE j;
#endif
if (!(ccp.SessionStatus&SS_RUN)) return;
SET_PORT_BIT(4); /* Timingtest */
for (o=0,q=0; q<CCP_MAX_DAQ; o+=CCP_MAX_ODT,q++) {
if (!(ccp.DaqList[q].flags&DAQ_FLAG_START)) continue;
if (ccp.DaqList[q].eventChannel!=eventChannel) continue;
if (--ccp.DaqList[q].cycle!=0) continue;
ccp.DaqList[q].cycle = ccp.DaqList[q].prescaler;
#ifdef CCP_SEND_SINGLE
/* Just mark DAQ for transmission */
ccp.DaqList[q].flags |= DAQ_FLAG_SEND;
#else
/* Check that the current queue space fits a complete cycle */
#if defined(CCP_SEND_QUEUE) && defined(CCP_SEND_QUEUE_OVERRUN_INDICATION)
if (CCP_SEND_QUEUE_SIZE-ccp.Queue.len<=ccp.DaqList[q].last) {
ccp.DaqList[q].flags |= DAQ_FLAG_OVERRUN;
continue; /* Skip this DAQ list on overrun */
}
#endif
/* Use BIT7 of PID to indicate overruns (CANape special feature) */
#ifdef CCP_SEND_QUEUE_OVERRUN_INDICATION
for (j=0;j<=ccp.DaqList[q].last;j++) {
if (!ccpSampleAndTransmitDtm((o+j)|(ccp.DaqList[q].flags&DAQ_FLAG_OVERRUN),q,j)) {
ccp.DaqList[q].flags |= DAQ_FLAG_OVERRUN;
} else {
ccp.DaqList[q].flags &= ~DAQ_FLAG_OVERRUN;
}
} /* j */
#else
for (j=0;j<=ccp.DaqList[q].last;j++) {
ccpSampleAndTransmitDtm(o+j,q,j);
} /* j */
#endif
#endif
} /* q */
/* Check for the next ODT to send */
#ifdef CCP_SEND_SINGLE
ccpSampleAndSendNextDtm();
#endif
RST_PORT_BIT(4); /* Timingtest */
}
#endif /* CCP_DAQ */
/*--------------------------------------------------------------------------*/
/* Background Processing */
/* Used for Checksum Calculation */
/*--------------------------------------------------------------------------*/
/* Table for CCITT checksum calculation */
#ifdef CCP_CHECKSUM_CCITT
CCP_MEMROM0 CCP_ROM CCP_WORD CRC16CCITTtab[256] = {
0x0000,0x1189,0x2312,0x329B,0x4624,0x57AD,0x6536,0x74BF,
0x8C48,0x9DC1,0xAF5A,0xBED3,0xCA6C,0xDBE5,0xE97E,0xF8F7,
0x1081,0x0108,0x3393,0x221A,0x56A5,0x472C,0x75B7,0x643E,
0x9CC9,0x8D40,0xBFDB,0xAE52,0xDAED,0xCB64,0xF9FF,0xE876,
0x2102,0x308B,0x0210,0x1399,0x6726,0x76AF,0x4434,0x55BD,
0xAD4A,0xBCC3,0x8E58,0x9FD1,0xEB6E,0xFAE7,0xC87C,0xD9F5,
0x3183,0x200A,0x1291,0x0318,0x77A7,0x662E,0x54B5,0x453C,
0xBDCB,0xAC42,0x9ED9,0x8F50,0xFBEF,0xEA66,0xD8FD,0xC974,
0x4204,0x538D,0x6116,0x709F,0x0420,0x15A9,0x2732,0x36BB,
0xCE4C,0xDFC5,0xED5E,0xFCD7,0x8868,0x99E1,0xAB7A,0xBAF3,
0x5285,0x430C,0x7197,0x601E,0x14A1,0x0528,0x37B3,0x263A,
0xDECD,0xCF44,0xFDDF,0xEC56,0x98E9,0x8960,0xBBFB,0xAA72,
0x6306,0x728F,0x4014,0x519D,0x2522,0x34AB,0x0630,0x17B9,
0xEF4E,0xFEC7,0xCC5C,0xDDD5,0xA96A,0xB8E3,0x8A78,0x9BF1,
0x7387,0x620E,0x5095,0x411C,0x35A3,0x242A,0x16B1,0x0738,
0xFFCF,0xEE46,0xDCDD,0xCD54,0xB9EB,0xA862,0x9AF9,0x8B70,
0x8408,0x9581,0xA71A,0xB693,0xC22C,0xD3A5,0xE13E,0xF0B7,
0x0840,0x19C9,0x2B52,0x3ADB,0x4E64,0x5FED,0x6D76,0x7CFF,
0x9489,0x8500,0xB79B,0xA612,0xD2AD,0xC324,0xF1BF,0xE036,
0x18C1,0x0948,0x3BD3,0x2A5A,0x5EE5,0x4F6C,0x7DF7,0x6C7E,
0xA50A,0xB483,0x8618,0x9791,0xE32E,0xF2A7,0xC03C,0xD1B5,
0x2942,0x38CB,0x0A50,0x1BD9,0x6F66,0x7EEF,0x4C74,0x5DFD,
0xB58B,0xA402,0x9699,0x8710,0xF3AF,0xE226,0xD0BD,0xC134,
0x39C3,0x284A,0x1AD1,0x0B58,0x7FE7,0x6E6E,0x5CF5,0x4D7C,
0xC60C,0xD785,0xE51E,0xF497,0x8028,0x91A1,0xA33A,0xB2B3,
0x4A44,0x5BCD,0x6956,0x78DF,0x0C60,0x1DE9,0x2F72,0x3EFB,
0xD68D,0xC704,0xF59F,0xE416,0x90A9,0x8120,0xB3BB,0xA232,
0x5AC5,0x4B4C,0x79D7,0x685E,0x1CE1,0x0D68,0x3FF3,0x2E7A,
0xE70E,0xF687,0xC41C,0xD595,0xA12A,0xB0A3,0x8238,0x93B1,
0x6B46,0x7ACF,0x4854,0x59DD,0x2D62,0x3CEB,0x0E70,0x1FF9,
0xF78F,0xE606,0xD49D,0xC514,0xB1AB,0xA022,0x92B9,0x8330,
0x7BC7,0x6A4E,0x58D5,0x495C,0x3DE3,0x2C6A,0x1EF1,0x0F78
};
#endif
#ifndef CCP_CHECKSUM_BLOCKSIZE
#define CCP_CHECKSUM_BLOCKSIZE 256
#endif
CCP_BYTE ccpBackground( void ) {
SET_PORT_BIT(3); /* Timingtest */
/* CCP command pending */
#ifdef CCP_CMD_NOT_IN_INTERRUPT
CCP_DISABLE_INTERRUPT;
if (ccp.SendStatus&CCP_CMD_PENDING) {
ccp.SendStatus &= ~CCP_CMD_PENDING;
ccpCommand(CCP_RX_DATA_PTR);
}
CCP_ENABLE_INTERRUPT;
#endif
/* Call the user backgound function */
ccpUserBackground();
/* CCP checksum calculation */
#ifdef CCP_CHECKSUM
/*
Checksum algorithm is not defined by the standard
Type is defined by CCP_CHECKSUM_TYPE, Maximum blocksize is 64K
*/
/* Checksum calculation in progress */
if (ccp.CheckSumSize) {
register CCP_BYTE n;
#ifndef CCP_CHECKSUM_CCITT
register CCP_BYTE b;
#endif
if (ccp.CheckSumSize<=(CCP_CHECKSUM_BLOCKSIZE-1)) {
n = (CCP_BYTE)ccp.CheckSumSize;
ccp.CheckSumSize = 0;
} else {
n = 0;
ccp.CheckSumSize -= CCP_CHECKSUM_BLOCKSIZE;
}
do {
#ifdef CCP_CHECKSUM_CCITT
/* CRC */
#ifdef CCP_MOTOROLA
(*(CCP_WORD*)&ccp.Crm[4]) = CRC16CCITTtab[ccp.Crm[5] ^ *(ccp.MTA[CCP_INTERNAL_MTA]++)] ^ ccp.Crm[4];
#else
(*(CCP_WORD*)&ccp.Crm[4]) = CRC16CCITTtab[ccp.Crm[4] ^ *(ccp.MTA[CCP_INTERNAL_MTA]++)] ^ ccp.Crm[5];
#endif
#else
/* Compiler Error BSO Tasking 16x */
/* *(CCP_CHECKSUM_TYPE*)&ccp.Crm[4] += *(ccp.MTA[CCP_INTERNAL_MTA]++); does not work */
/* Sum */
b = *(ccp.MTA[CCP_INTERNAL_MTA]);
*(CCP_CHECKSUM_TYPE*)&ccp.Crm[4] += b;
ccp.MTA[CCP_INTERNAL_MTA]++;
#endif
} while (--n!=0);
/* Checksum calculation finished ? */
if (ccp.CheckSumSize) {
RST_PORT_BIT(3); /* Timingtest */
return 1; /* Still pending */
}
ccpSendCrm();
} /* ccp.CheckSumSize */
#endif
RST_PORT_BIT(3); /* Timingtest */
return 0;
}
/*--------------------------------------------------------------------------*/
/* Command Processor */
/*--------------------------------------------------------------------------*/
void ccpCommand( CCP_BYTEPTR com ) {
SET_PORT_BIT(1); /* Timingtest */
#define cmd com[0]
#define ctr com[1]
/* Handle CONNECT or TEST command */
if (cmd==CC_CONNECT||cmd==CC_TEST) {
#define stationAddr (*(CCP_WORD*)&com[2]) /* Has to be Intel-Format ! */
#ifdef CCP_TESTMODE
if (gDebugLevel) CCPPRINT("\n%u: %s addr=%u\n",ctr,cmd==CC_CONNECT?"CONNECT":"TEST",stationAddr);
#endif
/* This station */
if (stationAddr==CCP_STATION_ADDR||stationAddr==CCP_BROADCAST_STATION_ADDR) { /* This station */
if (cmd==CC_CONNECT) {
#ifdef CCP_DAQ
if (!(ccp.SessionStatus&SS_TMP_DISCONNECTED)) {
ccpStopAllDaq();
ccp.SendStatus = 0; /* Clear all transmission flags */
}
#endif
ccp.SessionStatus |= SS_CONNECTED;
ccp.SessionStatus &= ~SS_TMP_DISCONNECTED;
#ifdef CCP_TESTMODE
if (gDebugLevel) CCPPRINT("*** connected ***\n");
#endif
}
/* Responce */
/* Station addresses in Intel Format */
ccp.Crm[0] = 0xFF;
ccp.Crm[1] = CRC_OK;
ccp.Crm[2] = ctr;
ccp.Crm[3] = 0xFE;
*(CCP_WORD*)&ccp.Crm[4] = CCP_STATION_ADDR;
*(CCP_WORD*)&ccp.Crm[6] = CCP_BROADCAST_STATION_ADDR;
/* responce */
/* |||| */
}
/* Another station */
else {
/* If connected, temporary disconnect */
if (ccp.SessionStatus&SS_CONNECTED) {
ccp.SessionStatus &= ~SS_CONNECTED;
ccp.SessionStatus |= SS_TMP_DISCONNECTED;
#ifdef CCP_TESTMODE
if (gDebugLevel) CCPPRINT("*** temporary disconnect ***\n");
#endif
}
RST_PORT_BIT(1); /* Timingtest */
/* no responce */
return;
}
}
/* Handle other commands only if connected */
else if (ccp.SessionStatus&SS_CONNECTED) {
/* prepare the responce */
ccp.Crm[0] = 0xFF;
ccp.Crm[1] = CRC_OK;
ccp.Crm[2] = ctr;
switch (cmd) {
case CC_DISCONNECT:
{
#define disconnectCmd com[2]
#define disconnectStationAddr (*(CCP_WORD*)&com[4])
ccp.SessionStatus &= ~SS_CONNECTED;
#ifdef CCP_TESTMODE
if (gDebugLevel) CCPPRINT("%u: DISCONNECT cmd=%u\n",ctr,disconnectCmd);
#endif
if (disconnectCmd==0x00) { /* Temporary */
ccp.SessionStatus |= SS_TMP_DISCONNECTED;
#ifdef CCP_TESTMODE
if (gDebugLevel) CCPPRINT("*** temporary disconnect ***\n");
#endif
} else { /* End of session */
#ifdef CCP_DAQ
ccpStopAllDaq();
#endif
#ifdef CCP_SEED_KEY
ccp.ProtectionStatus = 0; /* Clear Protection Status */
#endif
#ifdef CCP_TESTMODE
if (gDebugLevel) CCPPRINT("*** end of session ***\n");
#endif
}
}
break;
case CC_EXCHANGE_ID: /* Exchange Station Identifications */
{
CCP_BYTE i;
#define masterId com[2]
for(i=0;ccpStationId[i]!=0;i++) ;
ccp.Crm[3] = i; /* Lenght of slave device identifier */
ccp.Crm[4] = 0;
/* Build the Resource Availability and Protection Mask */
ccp.Crm[5] = PL_CAL; /* Default: Calibration available */
ccp.Crm[6] = 0; /* Default: No Protection */
#ifdef CCP_SEED_KEY
ccp.Crm[6] |= PL_CAL; /* Protected Calibration */
#endif
#ifdef CCP_DAQ
ccp.Crm[5] |= PL_DAQ; /* Data Acquisition */
#ifdef CCP_SEED_KEY
ccp.Crm[6] |= PL_DAQ; /* Protected Data Acquisition */
#endif
#endif
#if defined(CCP_PROGRAM) || defined(CCP_BOOTLOADER_DOWNLOAD)
ccp.Crm[5] |= PL_PGM; /* Flash Programming */
#ifdef CCP_SEED_KEY
ccp.Crm[6] |= PL_PGM; /* Protected Flash Programming */
#endif
#endif
ccp.Crm[7] = CCP_DRIVER_VERSION; /* Driver version number */
ccpSetMTA(0,(CCP_MTABYTEPTR)ccpStationId);
#ifdef CCP_TESTMODE
if (gDebugLevel) CCPPRINT("%u: EXCANGE_ID master_id=%u\n",ctr,masterId);
#endif
}
break;
#ifdef CCP_SEED_KEY
case CC_GET_SEED: /* Get Seed for Key */
{
#define privilegeLevel com[2]
ccp.Crm[3] = 0; /* Protection Status: No key required */
*(CCP_DWORD*)&ccp.Crm[4] = 0;
#ifdef CCP_SEED_KEY
/* Keys required for CAL or PGM */
switch (privilegeLevel) {
case PL_CAL:
ccp.Crm[3] = (0==(ccp.ProtectionStatus&PL_CAL)); /* Protection Status */
*(CCP_DWORD*)&ccp.Crm[4] = ccpGetSeed(PL_CAL);
break;
case PL_PGM:
ccp.Crm[3] = (0==(ccp.ProtectionStatus&PL_PGM)); /* Protection Status */
*(CCP_DWORD*)&ccp.Crm[4] = ccpGetSeed(PL_PGM);
break;
case PL_DAQ:
ccp.Crm[3] = (0==(ccp.ProtectionStatus&PL_DAQ)); /* Protection Status */
*(CCP_DWORD*)&ccp.Crm[4] = ccpGetSeed(PL_DAQ);
break;
default:
ccp.Crm[1] = CRC_CMD_SYNTAX;
/* Error */
}
#endif
#ifdef CCP_TESTMODE
if (gDebugLevel) CCPPRINT("%u: GET_SEED resource=%02X, protection_status=%u\n",ctr,privilegeLevel,ccp.Crm[3]);
#endif
}
break;
case CC_UNLOCK: /* Unlock Protection */
{
#define key com[2] /* Key may be up to 6 Bytes */
/* Check key */
ccp.ProtectionStatus |= ccpUnlock(&com[2]); /* Reset the appropriate resource protection mask bit */
ccp.Crm[3] = ccp.ProtectionStatus; /* Current Protection Status */
#ifdef CCP_TESTMODE
if (gDebugLevel) CCPPRINT("%u: UNLOCK protection_status=%02X\n",ctr,ccp.ProtectionStatus);
#endif
}
break;
#endif /* CCP_SEED_KEY */
case CC_SET_MTA: /* Set transfer address */
{
#define mta com[2]
#define addrExt com[3]
#define addr (*(CCP_DWORD*)&com[4])
if (mta<CCP_MAX_MTA-1) {
ccpSetMTA(mta,ccpGetPointer(addrExt,addr));
} else {
ccp.Crm[1] = CRC_OUT_OF_RANGE;
}
#ifdef CCP_TESTMODE
if (gDebugLevel) CCPPRINT("%u: SET_MTA %u to %08lX,%u\n",ctr,mta,addr,addrExt);
#endif
#undef mta
#undef addrExt
#undef addr
}
break;
case CC_DNLOAD: /* Download */
{
CCP_BYTE r;
#define size com[2]
#ifdef CCP_SEED_KEY
if (!(ccp.ProtectionStatus&PL_CAL)) {
ccp.Crm[1] = CRC_ACCESS_DENIED;
r = 0;
}