forked from vectorgrp/XCPlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcpLite.c
1842 lines (1538 loc) · 65.5 KB
/
xcpLite.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
/*****************************************************************************
| File:
| xcpLite.c
|
| Description:
| Implementation of the ASAM XCP Protocol Layer V1.4
| Lite Version (see feature list and limitations)
|
|
| Supported commands:
| GET_COMM_MODE_INFO GET_ID GET_VERSION
| SET_MTA UPLOAD SHORT_UPLOAD DOWNLOAD SHORT_DOWNLOAD
| GET_CAL_PAGE SET_CAL_PAGE BUILD_CHECKSUM
| GET_DAQ_RESOLUTION_INFO GET_DAQ_PROCESSOR_INFO GET_DAQ_EVENT_INFO GET_DAQ_LIST_INFO
| FREE_DAQ ALLOC_DAQ ALLOC_ODT ALLOC_ODT_ENTRY SET_DAQ_PTR WRITE_DAQ WRITE_DAQ_MULTIPLE
| GET_DAQ_LIST_MODE SET_DAQ_LIST_MODE START_STOP_SYNCH START_STOP_DAQ_LIST
| GET_DAQ_CLOCK GET_DAQ_CLOCK_MULTICAST TIME_CORRELATION_PROPERTIES
|
| Limitations:
| - Testet on 32 bit or 64 bit Linux and Windows platforms
| - 8 bit and 16 bit CPUs are not supported
| - No Motorola byte sex
| - No misra compliance
| - Overall number of ODTs limited to 64K
| - Overall number of ODT entries is limited to 64K
| - Fixed DAQ+ODT 2 byte DTO header
| - Fixed 32 bit time stamp
| - Only dynamic DAQ list allocation supported
| - Resume is not supported
| - Overload indication by event is not supported
| - DAQ does not support address extensions and prescaler
| - DAQ list and event channel prioritization is not supported
| - ODT optimization not supported
| - Interleaved communication mode is not supported
| - Seed & key is not supported
| - Flash programming is not supported
|
| More features, more transport layer (CAN, FlexRay) and platform support, misra compliance
| by the free XCP basic version available from Vector Informatik GmbH at www.vector.com
|
| Limitations of the XCP basic version:
| - Stimulation (Bypassing) is not available|
| - Bit stimulation is not available
| - SHORT_DOWNLOAD is not implemented
| - MODIFY_BITS is not available|
| - FLASH and EEPROM Programming is not available|
| - Block mode for UPLOAD, DOWNLOAD and PROGRAM is not available
| - Resume mode is not available|
| - Memory write and read protection is not supported
| - Checksum calculation with AUTOSAR CRC module is not supported
|
|
| Copyright (c) Vector Informatik GmbH. All rights reserved.
| Licensed under the MIT license. See LICENSE file in the project root for details.
|
| No limitations and full compliance are available with the commercial version
| from Vector Informatik GmbH, please contact Vector
|***************************************************************************/
#include "platform.h"
#include "main_cfg.h"
#ifdef APP_ENABLE_A2L_GEN
#include "A2L.h"
#endif
#include "xcpLite.h" // Protocol layer interface
/****************************************************************************/
/* Defaults and checks */
/****************************************************************************/
/* Check limits of the XCP imnplementation */
#if defined( XCPTL_CTO_SIZE )
#if ( XCPTL_MAX_CTO > 255 )
#error "XCPTL_CTO_SIZE must be <= 255"
#endif
#if ( XCPTL_CTO_SIZE < 0x08 )
#error "XCPTL_CTO_SIZE must be > 7"
#endif
#else
#error "Please define XCPTL_CTO_SIZE"
#endif
#if defined( XCPTL_DTO_SIZE )
#if ( XCPTL_DTO_SIZE < 0x08 )
#error "XCPTL_DTO_SIZE must be > 7"
#endif
#else
#error "Please define XCPTL_DTO_SIZE"
#endif
/* Max. size of an object referenced by an ODT entry XCP_MAX_ODT_ENTRY_SIZE may be limited */
#if defined ( XCP_MAX_ODT_ENTRY_SIZE )
#if ( XCP_MAX_DTO_ENTRY_SIZE > 255 )
#error "XCP_MAX_ODT_ENTRY_SIZE too large"
#endif
#else
#define XCP_MAX_ODT_ENTRY_SIZE 248 // mod 4 = 0 to optimize DAQ copy granularity
#endif
/* Check XCP_DAQ_MEM_SIZE */
#if defined ( XCP_DAQ_MEM_SIZE )
#if ( XCP_DAQ_MEM_SIZE > 0xFFFFFFFF )
#error "XCP_DAQ_MEM_SIZE must be <= 0xFFFFFFFF"
#endif
#else
#error "Please define XCP_DAQ_MEM_SIZE"
#endif
/* Check configuration of XCP_TIMESTAMP_UNIT. */
#if defined ( XCP_TIMESTAMP_UNIT )
#if ( (XCP_TIMESTAMP_UNIT >> 4) > 9 ) || ( (XCP_TIMESTAMP_UNIT & 0x0F) > 0 )
#error "XCP_TIMESTAMP_UNIT is not valid."
#endif
#else
#error "Please define XCP_TIMESTAMP_UNIT"
#endif
/* Check configuration of XCP_TIMESTAMP_TICKS. */
#if defined ( XCP_TIMESTAMP_TICKS )
#if ( (XCP_TIMESTAMP_TICKS > 0xFFFF) || (XCP_TIMESTAMP_TICKS == 0) )
#error "Iillegal range of XCP_TIMESTAMP_TICKS"
#endif
#else
#error "Please define XCP_TIMESTAMP_TICKS"
#endif
/****************************************************************************/
/* DAQ Type Definition */
/****************************************************************************/
/* ODT */
/* Size must be even !!! */
typedef struct {
uint16_t firstOdtEntry; /* Absolute odt entry number */
uint16_t lastOdtEntry; /* Absolute odt entry number */
uint16_t size; /* Number of bytes */
} tXcpOdt;
/* DAQ list */
typedef struct {
uint16_t lastOdt; /* Absolute odt number */
uint16_t firstOdt; /* Absolute odt number */
uint16_t eventChannel;
#ifdef XCP_ENABLE_PACKED_MODE
uint16_t sampleCount; /* Packed mode */
#endif
uint8_t flags;
uint8_t res;
} tXcpDaqList;
/* Dynamic DAQ list structures */
typedef struct {
uint16_t DaqCount;
uint16_t OdtCount; /* Absolute */
uint16_t OdtEntryCount; /* Absolute */
union {
uint8_t b[XCP_DAQ_MEM_SIZE];
tXcpDaqList DaqList[XCP_DAQ_MEM_SIZE / sizeof(tXcpDaqList)];
} u;
} tXcpDaq;
/* Shortcuts */
/* j is absolute odt number */
#define DaqListOdtEntryCount(j) ((gXcp.pOdt[j].lastOdtEntry-gXcp.pOdt[j].firstOdtEntry)+1)
#define DaqListOdtLastEntry(j) (gXcp.pOdt[j].lastOdtEntry)
#define DaqListOdtFirstEntry(j) (gXcp.pOdt[j].firstOdtEntry)
#define DaqListOdtSize(j) (gXcp.pOdt[j].size)
/* n is absolute odtEntry number */
#define OdtEntrySize(n) (gXcp.pOdtEntrySize[n])
#define OdtEntryAddr(n) (gXcp.pOdtEntryAddr[n])
/* i is daq number */
#define DaqListOdtCount(i) ((gXcp.Daq.u.DaqList[i].lastOdt-gXcp.Daq.u.DaqList[i].firstOdt)+1)
#define DaqListLastOdt(i) gXcp.Daq.u.DaqList[i].lastOdt
#define DaqListFirstOdt(i) gXcp.Daq.u.DaqList[i].firstOdt
#define DaqListFlags(i) gXcp.Daq.u.DaqList[i].flags
#define DaqListEventChannel(i) gXcp.Daq.u.DaqList[i].eventChannel
#define DaqListSampleCount(i) gXcp.Daq.u.DaqList[i].sampleCount
/****************************************************************************/
/* Protocol layer data */
/****************************************************************************/
typedef struct {
/* Crm has to be the first object of this structure !! (refer to XcpInit()) */
tXcpCto Crm; /* RES,ERR Message buffer */
uint8_t CrmLen; /* RES,ERR Message length */
uint8_t SessionStatus;
uint8_t SessionStarted;
uint8_t reserved1;
uint8_t* Mta; /* Memory Transfer Address */
/*
Dynamic DAQ list structures
This structure should be stored in resume mode
*/
tXcpDaq Daq;
tXcpOdt* pOdt;
uint32_t* pOdtEntryAddr;
uint8_t* pOdtEntrySize;
uint64_t DaqStartClock64;
uint32_t DaqOverflowCount;
/* State info from SET_DAQ_PTR for WRITE_DAQ and WRITE_DAQ_MULTIPLE */
uint16_t WriteDaqOdtEntry;
uint16_t WriteDaqOdt;
uint16_t WriteDaqDaq;
/* Optional event list */
#ifdef XCP_ENABLE_DAQ_EVENT_LIST
uint16_t EventCount;
tXcpEvent EventList[XCP_MAX_EVENT];
#endif
#if XCP_PROTOCOL_LAYER_VERSION >= 0x0103
#ifdef XCP_ENABLE_DAQ_CLOCK_MULTICAST
uint16_t ClusterId;
#endif
T_CLOCK_INFO_SLAVE SlaveClockInfo;
#ifdef XCP_ENABLE_GRANDMASTER_CLOCK_INFO
T_CLOCK_INFO_GRANDMASTER GrandmasterClockInfo;
T_CLOCK_INFO_RELATION ClockRelationInfo;
#endif
#endif
} tXcpData;
static tXcpData gXcp;
/****************************************************************************/
/* Macros */
/****************************************************************************/
#define error(e) { err=(e); goto negative_response; }
#define check_error(e) { err=(e); if (err!=0) { goto negative_response; } }
#define isConnected() (gXcp.SessionStatus & SS_CONNECTED)
#define isDaqRunning() (gXcp.SessionStatus & SS_DAQ)
/****************************************************************************/
/* Test */
/****************************************************************************/
#ifdef XCP_ENABLE_TESTMODE
static void XcpPrintCmd(const tXcpCto *pCmd);
static void XcpPrintRes(const tXcpCto *pCmd);
static void XcpPrintDaqList(uint16_t daq);
#endif
// Test instrumentation
#ifdef APP_ENABLE_A2L_GEN
void XcpCreateA2lDescription() {
A2lCreateMeasurement(gXcp.Daq.DaqCount, "Number of DAQ lists");
A2lCreateMeasurement(gXcp.Daq.OdtEntryCount, "Number of ODT entries");
A2lCreateMeasurement(gXcp.DaqOverflowCount, "Number of DAQ overflows");
}
#endif
/****************************************************************************/
/* Status */
/****************************************************************************/
uint8_t XcpIsStarted() {
return gXcp.SessionStarted;
}
uint8_t XcpIsConnected() {
return isConnected();
}
uint8_t XcpIsDaqRunning() {
return isDaqRunning();
}
#ifdef XCP_ENABLE_DAQ_CLOCK_MULTICAST
uint16_t XcpGetClusterId() {
return gXcp.ClusterId;
}
#endif
uint8_t XcpIsDaqPacked() {
#ifdef XCP_ENABLE_PACKED_MODE
for (uint16_t daq = 0; daq < gXcp.Daq.DaqCount; daq++) {
if (DaqListSampleCount(daq) > 1) return 1;
}
#endif
return 0;
}
uint64_t XcpGetDaqStartTime() {
return gXcp.DaqStartClock64;
}
uint32_t XcpGetDaqOverflowCount() {
return gXcp.DaqOverflowCount;
}
/****************************************************************************/
/* Calibration */
/****************************************************************************/
// Write n bytes. Copying of size bytes from data to gXcp.Mta
static uint8_t XcpWriteMta( uint8_t size, const uint8_t* data )
{
/* Standard RAM memory write access */
while ( size > 0 ) {
*gXcp.Mta = *data;
gXcp.Mta++;
data++;
size--;
}
return XCP_CMD_OK;
}
// Read n bytes. Copying of size bytes from data to gXcp.Mta
static uint8_t XcpReadMta( uint8_t size, uint8_t* data )
{
/* Standard RAM memory read access */
while (size > 0) {
*data = *gXcp.Mta;
data++;
gXcp.Mta++;
size--;
}
return XCP_CMD_OK;
}
/****************************************************************************/
/* Data Aquisition Setup */
/****************************************************************************/
// Free all dynamic DAQ lists
static void XcpFreeDaq( void )
{
XcpTlInitTransmitQueue();
gXcp.SessionStatus &= (uint8_t)(~SS_DAQ);
gXcp.Daq.DaqCount = 0;
gXcp.Daq.OdtCount = 0;
gXcp.Daq.OdtEntryCount = 0;
gXcp.pOdt = (tXcpOdt*)0;
gXcp.pOdtEntryAddr = 0;
gXcp.pOdtEntrySize = 0;
memset((uint8_t*)&gXcp.Daq.u.b[0], 0, XCP_DAQ_MEM_SIZE);
}
// Allocate Memory for daq,odt,odtEntries and Queue according to DaqCount, OdtCount and OdtEntryCount
static uint8_t XcpAllocMemory( void )
{
uint32_t s;
/* Check memory overflow */
s = ( gXcp.Daq.DaqCount * (uint32_t)sizeof(tXcpDaqList ) +
( gXcp.Daq.OdtCount * (uint32_t)sizeof(tXcpOdt) ) +
( gXcp.Daq.OdtEntryCount * ((uint32_t)sizeof(uint8_t*) + (uint32_t)sizeof(uint8_t) ) ) );
if (s>=XCP_DAQ_MEM_SIZE) return CRC_MEMORY_OVERFLOW;
gXcp.pOdt = (tXcpOdt*)&gXcp.Daq.u.DaqList[gXcp.Daq.DaqCount];
gXcp.pOdtEntryAddr = (uint32_t*)&gXcp.pOdt[gXcp.Daq.OdtCount];
gXcp.pOdtEntrySize = (uint8_t*)&gXcp.pOdtEntryAddr[gXcp.Daq.OdtEntryCount];
#ifdef XCP_ENABLE_TESTMODE
if ( ApplXcpGetDebugLevel() >= 3) printf("[XcpAllocMemory] %u of %u Bytes used\n",s,XCP_DAQ_MEM_SIZE );
#endif
return 0;
}
// Allocate daqCount DAQ lists
static uint8_t XcpAllocDaq( uint16_t daqCount )
{
if ( (gXcp.Daq.OdtCount!=0) || (gXcp.Daq.OdtEntryCount!=0) ) {
return CRC_SEQUENCE;
}
if( daqCount == 0 || daqCount>255) {
return CRC_OUT_OF_RANGE;
}
gXcp.Daq.DaqCount = (uint8_t)daqCount;
return XcpAllocMemory();
}
// Allocate odtCount ODTs in a DAQ list
static uint8_t XcpAllocOdt( uint16_t daq, uint8_t odtCount )
{
uint32_t n;
if ( (gXcp.Daq.DaqCount==0) || (gXcp.Daq.OdtEntryCount!=0) ) {
return CRC_SEQUENCE;
}
if( odtCount == 0 ) {
return CRC_OUT_OF_RANGE;
}
n = (uint32_t)gXcp.Daq.OdtCount + (uint32_t)odtCount;
if (n > 0xFFFF) return CRC_OUT_OF_RANGE; // Overall number of ODTs limited to 64K
gXcp.Daq.u.DaqList[daq].firstOdt = gXcp.Daq.OdtCount;
gXcp.Daq.OdtCount = (uint16_t)n;
gXcp.Daq.u.DaqList[daq].lastOdt = (uint16_t)(gXcp.Daq.OdtCount-1);
return XcpAllocMemory();
}
// Adjust ODT size by size
static void XcpAdjustOdtSize(uint16_t daq, uint16_t odt, uint8_t size) {
#ifdef XCP_ENABLE_PACKED_MODE
uint16_t sc = DaqListSampleCount(daq);
if (sc == 0) sc = 1;
DaqListOdtSize(odt) = (uint16_t)(DaqListOdtSize(odt) + size*sc);
#else
DaqListOdtSize(odt) = (uint16_t)(DaqListOdtSize(odt) + size);
#endif
}
// Allocate all ODT entries, Parameter odt is relative odt number
static uint8_t XcpAllocOdtEntry( uint16_t daq, uint8_t odt, uint8_t odtEntryCount )
{
int xcpFirstOdt;
uint32_t n;
if ( (gXcp.Daq.DaqCount==0) || (gXcp.Daq.OdtCount==0) ) {
return (uint8_t)CRC_SEQUENCE;
}
if (odtEntryCount==0) {
return (uint8_t)CRC_OUT_OF_RANGE;
}
/* Absolute ODT entry count is limited to 64K */
n = (uint32_t)gXcp.Daq.OdtEntryCount + (uint32_t)odtEntryCount;
if (n>0xFFFF) return CRC_MEMORY_OVERFLOW;
xcpFirstOdt = gXcp.Daq.u.DaqList[daq].firstOdt;
gXcp.pOdt[xcpFirstOdt+odt].firstOdtEntry = gXcp.Daq.OdtEntryCount;
gXcp.Daq.OdtEntryCount = (uint16_t)n;
gXcp.pOdt[xcpFirstOdt + odt].lastOdtEntry = (uint16_t)(gXcp.Daq.OdtEntryCount - 1);
gXcp.pOdt[xcpFirstOdt + odt].size = 0;
return XcpAllocMemory();
}
// Set ODT entry pointer
static uint8_t XcpSetDaqPtr(uint16_t daq, uint8_t odt, uint8_t idx) {
uint16_t odt0 = (uint16_t)(DaqListFirstOdt(daq) + odt); // Absolute odt index
if ((daq >= gXcp.Daq.DaqCount) || (odt >= DaqListOdtCount(daq)) || (idx >= DaqListOdtEntryCount(odt0))) return CRC_OUT_OF_RANGE;
// Save info for XcpAddOdtEntry from WRITE_DAQ and WRITE_DAQ_MULTIPLE
gXcp.WriteDaqOdtEntry = (uint16_t)(DaqListOdtFirstEntry(odt0) + idx); // Absolute odt entry index
gXcp.WriteDaqOdt = odt0; // Absolute odt index
gXcp.WriteDaqDaq = daq;
return 0;
}
// Add an ODT entry to current DAQ/ODT
static uint8_t XcpAddOdtEntry(uint32_t addr, uint8_t ext, uint8_t size) {
if ((size == 0) || size > XCP_MAX_ODT_ENTRY_SIZE) return CRC_OUT_OF_RANGE;
if (0 == gXcp.Daq.DaqCount || 0 == gXcp.Daq.OdtCount || 0 == gXcp.Daq.OdtEntryCount) return CRC_DAQ_CONFIG;
OdtEntrySize(gXcp.WriteDaqOdtEntry) = size;
OdtEntryAddr(gXcp.WriteDaqOdtEntry) = addr; // Holds A2L/XCP address
XcpAdjustOdtSize(gXcp.WriteDaqDaq, gXcp.WriteDaqOdt, size);
gXcp.WriteDaqOdtEntry++; // Autoincrement to next ODT entry, no autoincrementing over ODTs
return 0;
}
// Set DAQ list mode
static void XcpSetDaqListMode(uint16_t daq, uint16_t event, uint8_t mode ) {
DaqListEventChannel(daq) = event;
DaqListFlags(daq) = mode;
}
// Start DAQ
static void XcpStartDaq( uint16_t daq )
{
gXcp.DaqStartClock64 = ApplXcpGetClock64();
gXcp.DaqOverflowCount = 0;
DaqListFlags(daq) |= (uint8_t)DAQ_FLAG_RUNNING;
XcpTlInitTransmitQueue();
gXcp.SessionStatus |= (uint8_t)SS_DAQ;
}
// Start all selected DAQs
static void XcpStartAllSelectedDaq()
{
uint16_t daq;
/* Start all selected DAQs */
gXcp.DaqStartClock64 = ApplXcpGetClock64();
gXcp.DaqOverflowCount = 0;
for (daq=0;daq<gXcp.Daq.DaqCount;daq++) {
if ( (DaqListFlags(daq) & (uint8_t)DAQ_FLAG_SELECTED) != 0 ) {
DaqListFlags(daq) |= (uint8_t)DAQ_FLAG_RUNNING;
DaqListFlags(daq) &= (uint8_t)(~DAQ_FLAG_SELECTED);
#ifdef XCP_ENABLE_TESTMODE
if (ApplXcpGetDebugLevel() >= 2) {
XcpPrintDaqList(daq);
}
#endif
}
}
XcpTlInitTransmitQueue();
gXcp.SessionStatus |= (uint8_t)SS_DAQ;
}
// Stop DAQ
static void XcpStopDaq( uint16_t daq )
{
uint8_t i;
DaqListFlags(daq) &= (uint8_t)(DAQ_FLAG_DIRECTION|DAQ_FLAG_TIMESTAMP|DAQ_FLAG_NO_PID);
/* Check if all DAQ lists are stopped */
for (i=0;i<gXcp.Daq.DaqCount;i++) {
if ( (DaqListFlags(i) & (uint8_t)DAQ_FLAG_RUNNING) != 0 ) {
return;
}
}
XcpTlInitTransmitQueue();
gXcp.SessionStatus &= (uint8_t)(~SS_DAQ);
}
// Stop all selected DAQs
static void XcpStopAllSelectedDaq()
{
uint16_t daq;
for (daq=0;daq<gXcp.Daq.DaqCount;daq++) {
if ( (DaqListFlags(daq) & (uint8_t)DAQ_FLAG_SELECTED) != 0 ) {
XcpStopDaq(daq);
DaqListFlags(daq) &= (uint8_t)(~DAQ_FLAG_SELECTED);
}
}
}
// Stop all DAQs
static void XcpStopAllDaq( void )
{
for (uint8_t daq=0; daq<gXcp.Daq.DaqCount; daq++) {
DaqListFlags(daq) &= (uint8_t)(DAQ_FLAG_DIRECTION|DAQ_FLAG_TIMESTAMP|DAQ_FLAG_NO_PID);
}
XcpTlInitTransmitQueue();
gXcp.SessionStatus &= (uint8_t)(~SS_DAQ);
}
/****************************************************************************/
/* Data Aquisition Processor */
/****************************************************************************/
// Measurement data acquisition, sample and transmit measurement date associated to event
static void XcpEvent_(uint16_t event, uint8_t* base, uint64_t clock)
{
uint8_t* d;
uint8_t* d0;
void* p0;
uint32_t e, el, odt, daq, hs, n;
#ifdef XCP_ENABLE_PACKED_MODE
uint32_t sc;
#endif
for (daq=0; daq<gXcp.Daq.DaqCount; daq++) {
if ((DaqListFlags(daq) & (uint8_t)DAQ_FLAG_RUNNING) == 0) continue; // DAQ list not active
if ( DaqListEventChannel(daq) != event ) continue; // DAQ list not associated with this event
#ifdef XCP_ENABLE_PACKED_MODE
sc = DaqListSampleCount(daq); // Packed mode sample count, 0 if not packed
#endif
for (hs=2+XCP_TIMESTAMP_SIZE,odt=DaqListFirstOdt(daq);odt<=DaqListLastOdt(daq);hs=2,odt++) {
// Get DTO buffer, overrun if not available
if ((d0 = XcpTlGetDtoBuffer(&p0, (uint16_t)(DaqListOdtSize(odt)+hs))) == 0) {
#ifdef XCP_ENABLE_TESTMODE
if (ApplXcpGetDebugLevel() >= 2) printf("DAQ queue overflow! Event %u skipped\n", event);
#endif
gXcp.DaqOverflowCount++;
DaqListFlags(daq) |= DAQ_FLAG_OVERRUN;
return; // Skip rest of this event on queue overrun
}
/* ODT,DAQ header */
d0[0] = (uint8_t)(odt-DaqListFirstOdt(daq)); /* Relative odt number */
d0[1] = (uint8_t)daq;
/* Use BIT7 of PID or ODT to indicate overruns */
if ( (DaqListFlags(daq) & DAQ_FLAG_OVERRUN) != 0 ) {
d0[0] |= 0x80;
DaqListFlags(daq) &= (uint8_t)(~DAQ_FLAG_OVERRUN);
}
/* Timestamp */
#if (XCP_TIMESTAMP_SIZE==8) // @@@@ XCP V1.6
if (hs==10) *((uint64_t*)&d0[2]) = clock;
#else
if (hs==6) *((uint32_t*)&d0[2]) = (uint32_t)clock;
#endif
/* Copy data */
/* This is the inner loop, optimize here */
e = DaqListOdtFirstEntry(odt);
if (OdtEntrySize(e) != 0) {
el = DaqListOdtLastEntry(odt);
d = &d0[hs];
while (e <= el) { // inner DAQ loop
n = OdtEntrySize(e);
if (n == 0) break;
#ifdef XCP_ENABLE_PACKED_MODE
if (sc>1) n *= sc; // packed mode
#endif
memcpy((uint8_t*)d, &base[OdtEntryAddr(e)], n);
d += n;
e++;
} // ODT entry
}
XcpTlCommitDtoBuffer(p0);
} /* odt */
} /* daq */
}
void XcpEventAt(uint16_t event, uint64_t clock) {
if ((gXcp.SessionStatus & (uint8_t)SS_DAQ) == 0) return; // DAQ not running
XcpEvent_(event, ApplXcpGetBaseAddr(), clock);
}
void XcpEventExt(uint16_t event, uint8_t* base) {
if ((gXcp.SessionStatus & (uint8_t)SS_DAQ) == 0) return; // DAQ not running
XcpEvent_(event, base, ApplXcpGetClock64());
}
void XcpEvent(uint16_t event) {
if ((gXcp.SessionStatus & (uint8_t)SS_DAQ) == 0) return; // DAQ not running
XcpEvent_(event, ApplXcpGetBaseAddr(), ApplXcpGetClock64());
}
/****************************************************************************/
/* Command Processor */
/****************************************************************************/
// Stops DAQ and goes to disconnected state
void XcpDisconnect( void )
{
if (!gXcp.SessionStarted) return;
gXcp.SessionStatus &= (uint8_t)(~SS_CONNECTED);
XcpStopAllDaq();
}
// Handles incoming XCP commands
void XcpCommand( const uint32_t* pCommand )
{
if (!gXcp.SessionStarted) return;
const tXcpCto* pCmd = (const tXcpCto*) pCommand;
uint8_t err = 0;
// Prepare the default response
CRM_CMD = PID_RES; /* Response, no error */
gXcp.CrmLen = 1; /* Length = 1 */
// CONNECT ?
if (CRO_CMD==CC_CONNECT)
{
#ifdef XCP_ENABLE_TESTMODE
if (ApplXcpGetDebugLevel() >= 1) {
printf("CONNECT mode=%u\n", CRO_CONNECT_MODE);
if (gXcp.SessionStatus & SS_CONNECTED) printf(" Already connected! DAQ setup cleared! Legacy mode activated!\n");
}
#endif
// Set Session Status
gXcp.SessionStatus = (uint8_t)(SS_CONNECTED | SS_LEGACY_MODE);
/* Reset DAQ */
XcpFreeDaq();
// Response
gXcp.CrmLen = CRM_CONNECT_LEN;
CRM_CONNECT_TRANSPORT_VERSION = (uint8_t)( (uint16_t)XCP_TRANSPORT_LAYER_VERSION >> 8 ); /* Major versions of the XCP Protocol Layer and Transport Layer Specifications. */
CRM_CONNECT_PROTOCOL_VERSION = (uint8_t)( (uint16_t)XCP_PROTOCOL_LAYER_VERSION >> 8 );
CRM_CONNECT_MAX_CTO_SIZE = XCPTL_CTO_SIZE;
CRM_CONNECT_MAX_DTO_SIZE = XCPTL_DTO_SIZE;
CRM_CONNECT_RESOURCE = 0x00; /* Reset resource mask */
CRM_CONNECT_RESOURCE |= (uint8_t)RM_DAQ; /* Data Acquisition */
CRM_CONNECT_COMM_BASIC = 0;
CRM_CONNECT_COMM_BASIC |= (uint8_t)CMB_OPTIONAL;
#if defined ( XCP_CPUTYPE_BIGENDIAN )
CRM_CONNECT_COMM_BASIC |= (uint8_t)PI_MOTOROLA;
#endif
}
// Handle other all other commands
else {
#ifdef XCP_ENABLE_TESTMODE
if (ApplXcpGetDebugLevel() >= 1) XcpPrintCmd(pCmd);
#endif
if (!(gXcp.SessionStatus & SS_CONNECTED)) { // Must be connected
#ifdef XCP_ENABLE_TESTMODE
if (ApplXcpGetDebugLevel() >= 1) printf("Command ignored because not in connected state, no response sent!\n");
#endif
return;
}
switch (CRO_CMD)
{
case CC_SYNC:
{
/* Always return a negative response with the error code ERR_CMD_SYNCH. */
gXcp.CrmLen = CRM_SYNCH_LEN;
CRM_CMD = PID_ERR;
CRM_ERR = CRC_CMD_SYNCH;
}
break;
case CC_GET_COMM_MODE_INFO:
{
gXcp.CrmLen = CRM_GET_COMM_MODE_INFO_LEN;
CRM_GET_COMM_MODE_INFO_DRIVER_VERSION = XCP_DRIVER_VERSION;
#ifdef XCP_ENABLE_INTERLEAVED
CRM_GET_COMM_MODE_INFO_COMM_OPTIONAL = CMO_INTERLEAVED_MODE;
CRM_GET_COMM_MODE_INFO_QUEUE_SIZE = XCP_INTERLEAVED_QUEUE_SIZE;
#else
CRM_GET_COMM_MODE_INFO_COMM_OPTIONAL = 0;
CRM_GET_COMM_MODE_INFO_QUEUE_SIZE = 0;
#endif
CRM_GET_COMM_MODE_INFO_MAX_BS = 0;
CRM_GET_COMM_MODE_INFO_MIN_ST = 0;
}
break;
case CC_DISCONNECT:
{
XcpDisconnect();
}
break;
case CC_GET_ID:
{
gXcp.CrmLen = CRM_GET_ID_LEN;
CRM_GET_ID_MODE = 0;
CRM_GET_ID_LENGTH = 0;
switch (CRO_GET_ID_TYPE) {
case IDT_ASCII:
if (!ApplXcpGetSlaveId((char**)&gXcp.Mta, &CRM_GET_ID_LENGTH)) error(CRC_OUT_OF_RANGE);
break;
#ifdef XCP_ENABLE_A2L_NAME
case IDT_ASAM_NAME:
if (!ApplXcpGetA2LFilename((char**)&gXcp.Mta, &CRM_GET_ID_LENGTH, 0)) error(CRC_OUT_OF_RANGE);
break;
#endif
#ifdef XCP_ENABLE_FILE_UPLOAD
case IDT_VECTOR_MDI:
case IDT_ASAM_UPLOAD:
if (!ApplXcpReadFile(CRO_GET_ID_TYPE, &gXcp.Mta, &CRM_GET_ID_LENGTH)) error(CRC_ACCESS_DENIED);
break;
#endif
case IDT_ASAM_PATH:
case IDT_ASAM_URL:
CRM_GET_ID_LENGTH = 0; // No error, just return length=0, INCA always polls ID_TYPE 0-4
break;
default:
error(CRC_OUT_OF_RANGE);
}
}
break;
case CC_GET_STATUS:
{
gXcp.CrmLen = CRM_GET_STATUS_LEN;
CRM_GET_STATUS_STATUS = (uint8_t)gXcp.SessionStatus;
CRM_GET_STATUS_PROTECTION = 0;
CRM_GET_STATUS_CONFIG_ID = 0; /* Session configuration ID not available. */
}
break;
case CC_SET_MTA:
{
gXcp.Mta = ApplXcpGetPointer(CRO_SET_MTA_EXT,CRO_SET_MTA_ADDR);
}
break;
case CC_DOWNLOAD:
{
uint8_t size;
size = CRO_DOWNLOAD_SIZE;
if (size > CRO_DOWNLOAD_MAX_SIZE) error(CRC_OUT_OF_RANGE)
err = XcpWriteMta(size, CRO_DOWNLOAD_DATA);
if (err == XCP_CMD_DENIED) error(CRC_WRITE_PROTECTED);
}
break;
case CC_SHORT_DOWNLOAD:
{
gXcp.Mta = ApplXcpGetPointer(CRO_SHORT_DOWNLOAD_EXT, CRO_SHORT_DOWNLOAD_ADDR);
uint8_t size;
size = CRO_SHORT_DOWNLOAD_SIZE;
if (size > CRO_SHORT_DOWNLOAD_MAX_SIZE) error(CRC_OUT_OF_RANGE)
err = XcpWriteMta(size, CRO_SHORT_DOWNLOAD_DATA);
if (err == XCP_CMD_DENIED) error(CRC_WRITE_PROTECTED);
}
break;
case CC_UPLOAD:
{
uint8_t size = CRO_UPLOAD_SIZE;
if (size > (uint8_t)CRM_UPLOAD_MAX_SIZE) error(CRC_OUT_OF_RANGE);
err = XcpReadMta(size,CRM_UPLOAD_DATA);
gXcp.CrmLen = (uint8_t)(CRM_UPLOAD_LEN+size);
if (err == XCP_CMD_PENDING) return; // No response
if (err == XCP_CMD_DENIED) error(CRC_ACCESS_DENIED);
}
break;
case CC_SHORT_UPLOAD:
{
if (CRO_SHORT_UPLOAD_SIZE > (uint8_t)CRM_SHORT_UPLOAD_MAX_SIZE) error(CRC_OUT_OF_RANGE);
gXcp.Mta = ApplXcpGetPointer(CRO_SHORT_UPLOAD_EXT,CRO_SHORT_UPLOAD_ADDR);
err = XcpReadMta(CRO_SHORT_UPLOAD_SIZE,CRM_SHORT_UPLOAD_DATA);
gXcp.CrmLen = (uint8_t)(CRM_SHORT_UPLOAD_LEN+CRO_SHORT_UPLOAD_SIZE);
if (err == XCP_CMD_PENDING) return; // No response
if (err == XCP_CMD_DENIED) error(CRC_ACCESS_DENIED);
}
break;
#ifdef XCP_ENABLE_CAL_PAGE
case CC_SET_CAL_PAGE:
{
check_error(ApplXcpSetCalPage(CRO_SET_CAL_PAGE_SEGMENT, CRO_SET_CAL_PAGE_PAGE, CRO_SET_CAL_PAGE_MODE));
}
break;
case CC_GET_CAL_PAGE:
{
gXcp.CrmLen = CRM_GET_CAL_PAGE_LEN;
CRM_GET_CAL_PAGE_PAGE = ApplXcpGetCalPage(CRO_GET_CAL_PAGE_SEGMENT, CRO_GET_CAL_PAGE_MODE);
}
break;
#endif
#if defined ( XCP_ENABLE_CHECKSUM )
case CC_BUILD_CHECKSUM: /* Build Checksum */
{
uint32_t n = CRO_BUILD_CHECKSUM_SIZE;
uint32_t s = 0;
uint32_t d,i;
if (n % 4 != 0) error(CRC_OUT_OF_RANGE)
n = n / 4;
for (i = 0; i < n; i++) { XcpReadMta(4, (uint8_t*)&d); s += d; }
CRM_BUILD_CHECKSUM_RESULT = s;
CRM_BUILD_CHECKSUM_TYPE = XCP_CHECKSUM_TYPE_ADD44;
gXcp.CrmLen = CRM_BUILD_CHECKSUM_LEN;
}
break;
#endif
case CC_GET_DAQ_PROCESSOR_INFO:
{
gXcp.CrmLen = CRM_GET_DAQ_PROCESSOR_INFO_LEN;
CRM_GET_DAQ_PROCESSOR_INFO_MIN_DAQ = 0;
CRM_GET_DAQ_PROCESSOR_INFO_MAX_DAQ = (gXcp.Daq.DaqCount); /* dynamic or */
#if defined ( XCP_ENABLE_DAQ_EVENT_INFO )
CRM_GET_DAQ_PROCESSOR_INFO_MAX_EVENT = ApplXcpEventCount;
#else
CRM_GET_DAQ_PROCESSOR_INFO_MAX_EVENT = 0; /* Unknown */
#endif
CRM_GET_DAQ_PROCESSOR_INFO_DAQ_KEY_BYTE = (uint8_t)DAQ_HDR_ODT_DAQB; /* DTO identification field type: Relative ODT number, absolute list number (BYTE) */
CRM_GET_DAQ_PROCESSOR_INFO_PROPERTIES = (uint8_t)( DAQ_PROPERTY_CONFIG_TYPE | DAQ_PROPERTY_TIMESTAMP | DAQ_OVERLOAD_INDICATION_PID );
}
break;
case CC_GET_DAQ_RESOLUTION_INFO:
{
gXcp.CrmLen = CRM_GET_DAQ_RESOLUTION_INFO_LEN;
CRM_GET_DAQ_RESOLUTION_INFO_GRANULARITY_DAQ = 1;
CRM_GET_DAQ_RESOLUTION_INFO_GRANULARITY_STIM = 1;
CRM_GET_DAQ_RESOLUTION_INFO_MAX_SIZE_DAQ = (uint8_t)XCP_MAX_ODT_ENTRY_SIZE;
CRM_GET_DAQ_RESOLUTION_INFO_MAX_SIZE_STIM = (uint8_t)XCP_MAX_ODT_ENTRY_SIZE;
#if (XCP_TIMESTAMP_SIZE==8) // @@@@ XCP V1.6
CRM_GET_DAQ_RESOLUTION_INFO_TIMESTAMP_MODE = XCP_TIMESTAMP_UNIT | DAQ_TIMESTAMP_FIXED | DAQ_TIMESTAMP_QWORD;
#else
CRM_GET_DAQ_RESOLUTION_INFO_TIMESTAMP_MODE = XCP_TIMESTAMP_UNIT | DAQ_TIMESTAMP_FIXED | DAQ_TIMESTAMP_DWORD;
#endif
CRM_GET_DAQ_RESOLUTION_INFO_TIMESTAMP_TICKS = (XCP_TIMESTAMP_TICKS);
}
break;
#ifdef XCP_ENABLE_DAQ_EVENT_INFO
case CC_GET_DAQ_EVENT_INFO:
{
uint16_t event = CRO_GET_DAQ_EVENT_INFO_EVENT;
if (event >= (uint16_t)ApplXcpEventCount) error(CRC_OUT_OF_RANGE);
gXcp.CrmLen = CRM_GET_DAQ_EVENT_INFO_LEN;
CRM_GET_DAQ_EVENT_INFO_PROPERTIES = DAQ_EVENT_PROPERTIES_DAQ | DAQ_EVENT_PROPERTIES_EVENT_CONSISTENCY;
#ifdef XCP_ENABLE_PACKED_MODE
if (ApplXcpEventList[event].sampleCount) CRM_GET_DAQ_EVENT_INFO_PROPERTIES |= DAQ_EVENT_PROPERTIES_PACKED;
#endif
if (ApplXcpEventList[event].size) CRM_GET_DAQ_EVENT_INFO_PROPERTIES |= DAQ_EVENT_PROPERTIES_EXT;
CRM_GET_DAQ_EVENT_INFO_MAX_DAQ_LIST = 0xFF;
CRM_GET_DAQ_EVENT_INFO_NAME_LENGTH = (uint8_t)strlen(ApplXcpEventList[event].name);
CRM_GET_DAQ_EVENT_INFO_TIME_CYCLE = ApplXcpEventList[event].timeCycle;
CRM_GET_DAQ_EVENT_INFO_TIME_UNIT = ApplXcpEventList[event].timeUnit;
CRM_GET_DAQ_EVENT_INFO_PRIORITY = 0;
#if XCP_PROTOCOL_LAYER_VERSION >= 0x0106
CRM_GET_DAQ_EVENT_INFO_SAMPLECOUNT = ApplXcpEventList[event].sampleCount;
CRM_GET_DAQ_EVENT_INFO_SIZE = ApplXcpEventList[event].size;
#endif
gXcp.Mta = (uint8_t*)ApplXcpEventList[event].name;
}
break;
#endif
case CC_FREE_DAQ:
{
XcpFreeDaq();
}
break;
case CC_ALLOC_DAQ:
{
uint16_t count = CRO_ALLOC_DAQ_COUNT;
check_error(XcpAllocDaq(count));
}
break;
case CC_ALLOC_ODT:
{
uint16_t daq = CRO_ALLOC_ODT_DAQ;
uint8_t count = CRO_ALLOC_ODT_COUNT;
if (daq >= gXcp.Daq.DaqCount) error(CRC_OUT_OF_RANGE);
check_error( XcpAllocOdt(daq, count) )
}
break;
case CC_ALLOC_ODT_ENTRY:
{
uint16_t daq = CRO_ALLOC_ODT_ENTRY_DAQ;
uint8_t odt = CRO_ALLOC_ODT_ENTRY_ODT;
uint8_t count = CRO_ALLOC_ODT_ENTRY_COUNT;
if ((daq >= gXcp.Daq.DaqCount) || (odt >= DaqListOdtCount(daq))) error(CRC_OUT_OF_RANGE);
check_error( XcpAllocOdtEntry(daq, odt, count) )
}