-
Notifications
You must be signed in to change notification settings - Fork 7
/
V1742_lib.c
1209 lines (1030 loc) · 37.7 KB
/
V1742_lib.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
/* #define WaveDump_Release "3.5.3_20130419" */
/* #define WaveDump_Release_Date "Apr 2013" */
/* #define DBG_TIME */
#include "V1742_lib.h"
#include "vme_bridge.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "X742CorrectionRoutines.h"
#define DEFAULT_CONFIG_FILE "/home/cmsdaq/DAQ/VMEDAQ/V1742_config.txt"
#define MAX_CH 64 /* max. number of channels */
#define MAX_SET 8 /* max. number of independent settings */
#define MAX_GW 1000 /* max. number of generic write commads */
#define VME_INTERRUPT_LEVEL 1
#define VME_INTERRUPT_STATUS_ID 0xAAAA
#define INTERRUPT_MODE CAEN_DGTZ_IRQ_MODE_ROAK
#define INTERRUPT_TIMEOUT 200 // ms
/* ###########################################################################
Typedefs
###########################################################################
*/
typedef struct WaveDumpConfig_t {
int LinkType;
int LinkNum;
int ConetNode;
uint32_t BaseAddress;
int Nch;
int Nbit;
float Ts;
int NumEvents;
int RecordLength;
unsigned int PostTrigger;
int InterruptNumEvents;
int TestPattern;
int DesMode;
int TriggerEdge;
int FPIOtype;
CAEN_DGTZ_TriggerMode_t ExtTriggerMode;
uint8_t EnableMask;
CAEN_DGTZ_TriggerMode_t ChannelTriggerMode[MAX_SET];
uint32_t DCoffset[MAX_SET];
int32_t DCoffsetGrpCh[MAX_SET][MAX_SET];
uint32_t Threshold[MAX_SET];
uint8_t GroupTrgEnableMask[MAX_SET];
uint32_t FTDCoffset[MAX_SET];
uint32_t FTThreshold[MAX_SET];
CAEN_DGTZ_TriggerMode_t FastTriggerMode;
uint32_t FastTriggerEnabled;
int GWn;
uint32_t GWaddr[MAX_GW];
uint32_t GWdata[MAX_GW];
/* OUTFILE_FLAGS OutFileFlags; */
int useCorrections;
} WaveDumpConfig_t;
/* ###########################################################################
Statics
###########################################################################
*/
static WaveDumpConfig_t WDcfg;
/* static int v1742_handle=0; */
static CAEN_DGTZ_BoardInfo_t BoardInfo;
static DataCorrection_t Table_gr0;
static DataCorrection_t Table_gr1;
/* ###########################################################################
Error messages
###########################################################################
*/
typedef enum {
ERR_NONE= 0,
ERR_CONF_FILE_NOT_FOUND,
ERR_DGZ_OPEN,
ERR_BOARD_INFO_READ,
ERR_INVALID_BOARD_TYPE,
ERR_DGZ_PROGRAM,
ERR_MALLOC,
ERR_RESTART,
ERR_INTERRUPT,
ERR_READOUT,
ERR_EVENT_BUILD,
ERR_HISTO_MALLOC,
ERR_UNHANDLED_BOARD,
ERR_MISMATCH_EVENTS,
ERR_FREE_BUFFER,
/* ERR_OUTFILE_WRITE, */
ERR_DUMMY_LAST,
} ERROR_CODES;
/* ###########################################################################
* Macros
* ########################################################################### */
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
/* ###########################################################################
* Functions
* ########################################################################### */
/*! \fn static long get_time()
* \brief Get time in milliseconds
*
* \return time in msec
*/
static long get_time()
{
long time_ms;
struct timeval t1;
struct timezone tz;
gettimeofday(&t1, &tz);
time_ms = (t1.tv_sec) * 1000 + t1.tv_usec / 1000;
return time_ms;
}
/*! \fn int GetMoreBoardInfo(CAEN_DGTZ_BoardInfo_t BoardInfo, WaveDumpConfig_t *WDcfg)
* \brief calculate num of channels, num of bit and sampl period according to the board type
*
* \param BoardInfo Board Type
* \param WDcfg pointer to the config. struct
* \return 0 = Success; -1 = unknown board type
*/
int GetMoreBoardInfo(int handle)
{
CAEN_DGTZ_DRS4Frequency_t freq;
int ret;
switch(BoardInfo.FamilyCode) {
case CAEN_DGTZ_XX724_FAMILY_CODE: WDcfg.Nbit = 14; WDcfg.Ts = 10.0; break;
case CAEN_DGTZ_XX720_FAMILY_CODE: WDcfg.Nbit = 12; WDcfg.Ts = 4.0; break;
case CAEN_DGTZ_XX721_FAMILY_CODE: WDcfg.Nbit = 8; WDcfg.Ts = 2.0; break;
case CAEN_DGTZ_XX731_FAMILY_CODE: WDcfg.Nbit = 8; WDcfg.Ts = 2.0; break;
case CAEN_DGTZ_XX751_FAMILY_CODE: WDcfg.Nbit = 10; WDcfg.Ts = 1.0; break;
case CAEN_DGTZ_XX761_FAMILY_CODE: WDcfg.Nbit = 10; WDcfg.Ts = 0.25; break;
case CAEN_DGTZ_XX740_FAMILY_CODE: WDcfg.Nbit = 12; WDcfg.Ts = 16.0; break;
case CAEN_DGTZ_XX742_FAMILY_CODE:
WDcfg.Nbit = 12;
if ((ret = CAEN_DGTZ_GetDRS4SamplingFrequency(handle, &freq)) != CAEN_DGTZ_Success) return CAEN_DGTZ_CommError;
switch (freq) {
case CAEN_DGTZ_DRS4_1GHz:
WDcfg.Ts = 1.0;
break;
case CAEN_DGTZ_DRS4_2_5GHz:
WDcfg.Ts = (float)0.4;
break;
case CAEN_DGTZ_DRS4_5GHz:
WDcfg.Ts = (float)0.2;
break;
}
break;
default: return -1;
}
if (((BoardInfo.FamilyCode == CAEN_DGTZ_XX751_FAMILY_CODE) ||
(BoardInfo.FamilyCode == CAEN_DGTZ_XX731_FAMILY_CODE) ) && WDcfg.DesMode)
WDcfg.Ts /= 2;
switch(BoardInfo.FamilyCode) {
case CAEN_DGTZ_XX724_FAMILY_CODE:
case CAEN_DGTZ_XX720_FAMILY_CODE:
case CAEN_DGTZ_XX721_FAMILY_CODE:
case CAEN_DGTZ_XX751_FAMILY_CODE:
case CAEN_DGTZ_XX761_FAMILY_CODE:
case CAEN_DGTZ_XX731_FAMILY_CODE:
switch(BoardInfo.FormFactor) {
case CAEN_DGTZ_VME64_FORM_FACTOR:
case CAEN_DGTZ_VME64X_FORM_FACTOR:
WDcfg.Nch = 8;
break;
case CAEN_DGTZ_DESKTOP_FORM_FACTOR:
case CAEN_DGTZ_NIM_FORM_FACTOR:
WDcfg.Nch = 4;
break;
}
break;
case CAEN_DGTZ_XX740_FAMILY_CODE:
switch( BoardInfo.FormFactor) {
case CAEN_DGTZ_VME64_FORM_FACTOR:
case CAEN_DGTZ_VME64X_FORM_FACTOR:
WDcfg.Nch = 64;
break;
case CAEN_DGTZ_DESKTOP_FORM_FACTOR:
case CAEN_DGTZ_NIM_FORM_FACTOR:
WDcfg.Nch = 32;
break;
}
break;
case CAEN_DGTZ_XX742_FAMILY_CODE:
switch( BoardInfo.FormFactor) {
case CAEN_DGTZ_VME64_FORM_FACTOR:
case CAEN_DGTZ_VME64X_FORM_FACTOR:
WDcfg.Nch = 36;
break;
case CAEN_DGTZ_DESKTOP_FORM_FACTOR:
case CAEN_DGTZ_NIM_FORM_FACTOR:
WDcfg.Nch = 16;
break;
}
break;
default:
return -1;
}
return 0;
}
/*! \fn int ProgramDigitizer(int handle, WaveDumpConfig_t WDcfg)
* \brief configure the digitizer according to the parameters read from
* the cofiguration file and saved in the WDcfg data structure
*
* \param handle Digitizer handle
* \param WDcfg: WaveDumpConfig data structure
* \return 0 = Success; negative numbers are error codes
*/
int ProgramDigitizer(int handle)
{
int i,j, ret = 0;
/* reset the digitizer */
ret |= CAEN_DGTZ_Reset(handle);
if (ret != 0) {
printf("Error: Unable to reset digitizer.\nPlease reset digitizer manually then restart the program\n");
return -1;
}
/* execute generic write commands */
for(i=0; i<WDcfg.GWn; i++)
ret |= CAEN_DGTZ_WriteRegister(handle, WDcfg.GWaddr[i], WDcfg.GWdata[i]);
// Set the waveform test bit for debugging
if (WDcfg.TestPattern)
ret |= CAEN_DGTZ_WriteRegister(handle, CAEN_DGTZ_BROAD_CH_CONFIGBIT_SET_ADD, 1<<3);
// custom setting for X742 boards
if (BoardInfo.FamilyCode == CAEN_DGTZ_XX742_FAMILY_CODE) {
ret |= CAEN_DGTZ_SetFastTriggerDigitizing(handle,(CAEN_DGTZ_EnaDis_t)WDcfg.FastTriggerEnabled);
ret |= CAEN_DGTZ_SetFastTriggerMode(handle,(CAEN_DGTZ_TriggerMode_t)WDcfg.FastTriggerMode);
}
if ((BoardInfo.FamilyCode == CAEN_DGTZ_XX751_FAMILY_CODE) || (BoardInfo.FamilyCode == CAEN_DGTZ_XX731_FAMILY_CODE)) {
ret |= CAEN_DGTZ_SetDESMode(handle, ( CAEN_DGTZ_EnaDis_t) WDcfg.DesMode);
}
ret |= CAEN_DGTZ_SetRecordLength(handle, WDcfg.RecordLength);
ret |= CAEN_DGTZ_SetPostTriggerSize(handle, (uint32_t) WDcfg.PostTrigger);
if(BoardInfo.FamilyCode != CAEN_DGTZ_XX742_FAMILY_CODE)
ret |= CAEN_DGTZ_GetPostTriggerSize(handle, &WDcfg.PostTrigger);
ret |= CAEN_DGTZ_SetIOLevel(handle, (CAEN_DGTZ_IOLevel_t) WDcfg.FPIOtype);
if( WDcfg.InterruptNumEvents > 0) {
// Interrupt handling
if( ret |= CAEN_DGTZ_SetInterruptConfig( handle, CAEN_DGTZ_ENABLE,
VME_INTERRUPT_LEVEL, VME_INTERRUPT_STATUS_ID,
WDcfg.InterruptNumEvents, INTERRUPT_MODE)!= CAEN_DGTZ_Success) {
printf( "\nError configuring interrupts. Interrupts disabled\n\n");
WDcfg.InterruptNumEvents = 0;
}
printf ("Interrupt enabled\n");
}
ret |= CAEN_DGTZ_SetMaxNumEventsBLT(handle, WDcfg.NumEvents);
ret |= CAEN_DGTZ_SetAcquisitionMode(handle, CAEN_DGTZ_SW_CONTROLLED);
ret |= CAEN_DGTZ_SetExtTriggerInputMode(handle, WDcfg.ExtTriggerMode);
if ((BoardInfo.FamilyCode == CAEN_DGTZ_XX740_FAMILY_CODE) || (BoardInfo.FamilyCode == CAEN_DGTZ_XX742_FAMILY_CODE)){
ret |= CAEN_DGTZ_SetGroupEnableMask(handle, WDcfg.EnableMask);
for(i=0; i<(WDcfg.Nch/8); i++) {
if (WDcfg.EnableMask & (1<<i)) {
if (BoardInfo.FamilyCode == CAEN_DGTZ_XX742_FAMILY_CODE) {
for(j=0; j<8; j++) {
if (WDcfg.DCoffsetGrpCh[i][j] != -1)
ret |= CAEN_DGTZ_SetChannelDCOffset(handle,(i*8)+j, WDcfg.DCoffsetGrpCh[i][j]);
else
ret |= CAEN_DGTZ_SetChannelDCOffset(handle,(i*8)+j, WDcfg.DCoffset[i]);
}
}
else {
ret |= CAEN_DGTZ_SetGroupDCOffset(handle, i, WDcfg.DCoffset[i]);
ret |= CAEN_DGTZ_SetGroupSelfTrigger(handle, WDcfg.ChannelTriggerMode[i], (1<<i));
ret |= CAEN_DGTZ_SetGroupTriggerThreshold(handle, i, WDcfg.Threshold[i]);
ret |= CAEN_DGTZ_SetChannelGroupMask(handle, i, WDcfg.GroupTrgEnableMask[i]);
}
ret |= CAEN_DGTZ_SetTriggerPolarity(handle, i, (CAEN_DGTZ_TriggerPolarity_t) WDcfg.TriggerEdge);
}
}
} else {
ret |= CAEN_DGTZ_SetChannelEnableMask(handle, WDcfg.EnableMask);
for(i=0; i<WDcfg.Nch; i++) {
if (WDcfg.EnableMask & (1<<i)) {
ret |= CAEN_DGTZ_SetChannelDCOffset(handle, i, WDcfg.DCoffset[i]);
ret |= CAEN_DGTZ_SetChannelSelfTrigger(handle, WDcfg.ChannelTriggerMode[i], (1<<i));
ret |= CAEN_DGTZ_SetChannelTriggerThreshold(handle, i, WDcfg.Threshold[i]);
ret |= CAEN_DGTZ_SetTriggerPolarity(handle, i, (CAEN_DGTZ_TriggerPolarity_t) WDcfg.TriggerEdge);
}
}
}
if (BoardInfo.FamilyCode == CAEN_DGTZ_XX742_FAMILY_CODE) {
for(i=0; i<(WDcfg.Nch/8); i++) {
ret |= CAEN_DGTZ_SetGroupFastTriggerDCOffset(handle,i,WDcfg.FTDCoffset[i]);
ret |= CAEN_DGTZ_SetGroupFastTriggerThreshold(handle,i,WDcfg.FTThreshold[i]);
}
}
if (ret)
printf("Warning: errors found during the programming of the digitizer.\nSome settings may not be executed\n");
return 0;
}
/*! \brief Write the correction table of a x742 boards into the output files
*
* \param Filename of output file
* \param DataCorrection table
*/
void SaveCorrectionTable(char *outputFileName, DataCorrection_t tb) {
char fnStr[1000];
int ch,i,j;
FILE *outputfile;
strcpy(fnStr, outputFileName);
strcat(fnStr, "_cell.txt");
printf("Saving correction table cell values to %s\n", fnStr);
outputfile = fopen(fnStr, "w");
for(ch=0; ch<MAX_X742_CHANNELS+1; ch++) {
fprintf(outputfile, "Calibration values from cell 0 to 1024 for channel %d:\n\n", ch);
for(i=0; i<1024; i+=8) {
for(j=0; j<8; j++)
fprintf(outputfile, "%d\t", tb.cell[ch][i+j]);
fprintf(outputfile, "cell = %d to %d\n", i, i+7);
}
}
fclose(outputfile);
strcpy(fnStr, outputFileName);
strcat(fnStr, "_nsample.txt");
printf("Saving correction table nsamples values to %s\n", fnStr);
outputfile = fopen(fnStr, "w");
for(ch=0; ch<MAX_X742_CHANNELS+1; ch++) {
fprintf(outputfile, "Calibration values from cell 0 to 1024 for channel %d:\n\n", ch);
for(i=0; i<1024; i+=8) {
for(j=0; j<8; j++)
fprintf(outputfile, "%d\t", tb.nsample[ch][i+j]);
fprintf(outputfile, "cell = %d to %d\n", i, i+7);
}
}
fclose(outputfile);
strcpy(fnStr, outputFileName);
strcat(fnStr, "_time.txt");
printf("Saving correction table time values to %s\n", fnStr);
outputfile = fopen(fnStr, "w");
fprintf(outputfile, "Calibration values (ps) from cell 0 to 1024 :\n\n");
for(i=0; i<1024; i+=8) {
for(ch=0; ch<8; ch++)
fprintf(outputfile, "%09.3f\t", tb.time[i+ch]);
fprintf(outputfile, "cell = %d to %d\n", i, i+7);
}
fclose(outputfile);
}
int writeEventToOutputBuffer_V1742(std::vector<unsigned int> *eventBuffer, CAEN_DGTZ_EventInfo_t *EventInfo, CAEN_DGTZ_X742_EVENT_t *Event)
{
int gr,ch;
// ====================================================
// | V1742 Raw Event Data Format |
// ====================================================
// 31 - 28 27 - 16 15 - 0
// Word[0] = [ 1010 ] [Event Tot #Words ] //Event Header (5 words)
// Word[1] = [ Board Id ] [ Pattern]
// Word[2] = [ #channels readout ]
// Word[3] = [ Event counter ]
// Word[4] = [ Trigger Time Tag ]
// Word[5] = [ 1000 ] [ Ch0 #Words ] // Ch0 Data (2 + #samples words)
// Word[6] = [ Ch0 #Gr ] [ Ch0 #Ch ]
// Word[7] = [ Ch0 Corr. samples (float) ]
// .. = [ Ch0 Corr. samples (float) ]
// Word[5+Ch0 #Words] = [ 1000 ] [ Ch1 #Words ] // Ch1 Data (2 + #samples words)
// Word[6+Ch0 #Words] = [ Ch1 #Gr ] [ Ch1 #Ch ]
// Word[7+Ch0 #Words] = [ Ch1 Corr. samples (float) ]
// ... = [ ..... ]
eventBuffer->clear();
eventBuffer->resize(5);
(*eventBuffer)[0]=0xA0000005;
(*eventBuffer)[1]=((EventInfo->BoardId)<<26)+EventInfo->Pattern;
(*eventBuffer)[2]=0;
(*eventBuffer)[3]=EventInfo->EventCounter;
(*eventBuffer)[4]=EventInfo->TriggerTimeTag;
// printf("EVENT 1742 %d %d\n",EventInfo->EventCounter,EventInfo->TriggerTimeTag);
for (gr=0;gr<(WDcfg.Nch/8);gr++) {
if (Event->GrPresent[gr]) {
for(ch=0; ch<9; ch++) {
int Size = Event->DataGroup[gr].ChSize[ch];
if (Size <= 0) {
continue;
}
// Channel Header for this event
uint32_t ChHeader[2];
ChHeader[0] = (8<<28) + (2 + Size); //Number of words written for this channel
ChHeader[1] = (gr<<16)+ch;
//Starting pointer
int start_ptr=eventBuffer->size();
//Allocating necessary space for this channel
eventBuffer->resize(eventBuffer->size() + 2 + Size);
memcpy(&((*eventBuffer)[start_ptr]), &ChHeader[0], 2 * sizeof(unsigned int));
//Beware the datas are float (because they are corrected...) but copying them here bit by bit. Should remember this for reading them out
memcpy(&((*eventBuffer)[start_ptr+2]), Event->DataGroup[gr].DataChannel[ch], Size * sizeof(unsigned int));
//Update event size and #channels
(*eventBuffer)[0]+=(Size+2);
(*eventBuffer)[2]++;
}
}
}
return 0;
}
/*! \fn int ParseConfigFile(FILE *f_ini, WaveDumpConfig_t *WDcfg)
* \brief Read the configuration file and set the WaveDump paremeters
*
* \param f_ini Pointer to the config file
* \param WDcfg: Pointer to the WaveDumpConfig data structure
* \return 0 = Success; negative numbers are error codes
*/
int ParseConfigFile(FILE *f_ini)
{
char str[1000], str1[1000];
int i,j, ch=-1, val, Off=0, tr = -1;
/* Default settings */
WDcfg.RecordLength = (1024*16);
WDcfg.PostTrigger = 80;
WDcfg.NumEvents = 1023;
WDcfg.EnableMask = 0xFF;
WDcfg.GWn = 0;
WDcfg.ExtTriggerMode = CAEN_DGTZ_TRGMODE_ACQ_ONLY;
WDcfg.InterruptNumEvents = 0;
WDcfg.TestPattern = 0;
WDcfg.TriggerEdge = 0;
WDcfg.DesMode = 0;
WDcfg.FastTriggerMode = CAEN_DGTZ_TRGMODE_DISABLED;
WDcfg.FastTriggerEnabled = 0;
WDcfg.FPIOtype = 0;
/* strcpy(WDcfg.GnuPlotPath, GNUPLOT_DEFAULT_PATH); */
for(i=0; i<MAX_SET; i++) {
WDcfg.DCoffset[i] = 0;
WDcfg.Threshold[i] = 0;
WDcfg.ChannelTriggerMode[i] = CAEN_DGTZ_TRGMODE_DISABLED;
WDcfg.GroupTrgEnableMask[i] = 0;
for(j=0; j<MAX_SET; j++) WDcfg.DCoffsetGrpCh[i][j] = -1;
WDcfg.FTThreshold[i] = 0;
WDcfg.FTDCoffset[i] =0;
}
WDcfg.useCorrections = -1;
/* read config file and assign parameters */
while(!feof(f_ini)) {
int read;
char *res;
// read a word from the file
read = fscanf(f_ini, "%s", str);
if( !read || (read == EOF) || !strlen(str))
continue;
// skip comments
if(str[0] == '#') {
res = fgets(str, 1000, f_ini);
continue;
}
if (strcmp(str, "@ON")==0) {
Off = 0;
continue;
}
if (strcmp(str, "@OFF")==0)
Off = 1;
if (Off)
continue;
// Section (COMMON or individual channel)
if (str[0] == '[') {
if (strstr(str, "COMMON")) {
ch = -1;
continue;
}
if (strstr(str, "TR")) {
sscanf(str+1, "TR%d", &val);
if (val < 0 || val >= MAX_SET) {
printf("%s: Invalid channel number\n", str);
} else {
tr = val;
}
} else {
sscanf(str+1, "%d", &val);
if (val < 0 || val >= MAX_SET) {
printf("%s: Invalid channel number\n", str);
} else {
ch = val;
}
}
continue;
}
// OPEN: read the details of physical path to the digitizer
if (strstr(str, "OPEN")!=NULL) {
read = fscanf(f_ini, "%s", str1);
if (strcmp(str1, "USB")==0)
WDcfg.LinkType = CAEN_DGTZ_USB;
else if (strcmp(str1, "PCI")==0)
WDcfg.LinkType = CAEN_DGTZ_PCI_OpticalLink;
else {
printf("%s %s: Invalid connection type\n", str, str1);
return -1;
}
read = fscanf(f_ini, "%d", &WDcfg.LinkNum);
if (WDcfg.LinkType == CAEN_DGTZ_USB)
WDcfg.ConetNode = 0;
else
read = fscanf(f_ini, "%d", &WDcfg.ConetNode);
read = fscanf(f_ini, "%x", &WDcfg.BaseAddress);
continue;
}
// Generic VME Write (address offset + data, both exadecimal)
if ((strstr(str, "WRITE_REGISTER")!=NULL) && (WDcfg.GWn < MAX_GW)) {
read = fscanf(f_ini, "%x", (int *)&WDcfg.GWaddr[WDcfg.GWn]);
read = fscanf(f_ini, "%x", (int *)&WDcfg.GWdata[WDcfg.GWn]);
WDcfg.GWn++;
continue;
}
// Acquisition Record Length (number of samples)
if (strstr(str, "RECORD_LENGTH")!=NULL) {
read = fscanf(f_ini, "%d", &WDcfg.RecordLength);
continue;
}
// Correction Level (mask)
if (strstr(str, "CORRECTION_LEVEL")!=NULL) {
read = fscanf(f_ini, "%s", str1);
if( strcmp(str1, "AUTO") == 0 )
WDcfg.useCorrections = -1;
else
WDcfg.useCorrections = atoi(str1);
continue;
}
// Test Pattern
if (strstr(str, "TEST_PATTERN")!=NULL) {
read = fscanf(f_ini, "%s", str1);
if (strcmp(str1, "YES")==0)
WDcfg.TestPattern = 1;
else if (strcmp(str1, "NO")!=0)
printf("%s: invalid option\n", str);
continue;
}
// Trigger Edge
if (strstr(str, "TRIGGER_EDGE")!=NULL) {
read = fscanf(f_ini, "%s", str1);
if (strcmp(str1, "FALLING")==0)
WDcfg.TriggerEdge = 1;
else if (strcmp(str1, "RISING")!=0)
printf("%s: invalid option\n", str);
continue;
}
// External Trigger (DISABLED, ACQUISITION_ONLY, ACQUISITION_AND_TRGOUT)
if (strstr(str, "EXTERNAL_TRIGGER")!=NULL) {
read = fscanf(f_ini, "%s", str1);
if (strcmp(str1, "DISABLED")==0)
WDcfg.ExtTriggerMode = CAEN_DGTZ_TRGMODE_DISABLED;
else if (strcmp(str1, "ACQUISITION_ONLY")==0)
WDcfg.ExtTriggerMode = CAEN_DGTZ_TRGMODE_ACQ_ONLY;
else if (strcmp(str1, "ACQUISITION_AND_TRGOUT")==0)
WDcfg.ExtTriggerMode = CAEN_DGTZ_TRGMODE_ACQ_AND_EXTOUT;
else
printf("%s: Invalid Parameter\n", str);
continue;
}
// Max. number of events for a block transfer (0 to 1023)
if (strstr(str, "MAX_NUM_EVENTS_BLT")!=NULL) {
read = fscanf(f_ini, "%d", &WDcfg.NumEvents);
continue;
}
/* // GNUplot path */
/* if (strstr(str, "GNUPLOT_PATH")!=NULL) { */
/* read = fscanf(f_ini, "%s", WDcfg.GnuPlotPath); */
/* continue; */
/* } */
// Post Trigger (percent of the acquisition window)
if (strstr(str, "POST_TRIGGER")!=NULL) {
read = fscanf(f_ini, "%d", &WDcfg.PostTrigger);
continue;
}
// DesMode (Double sampling frequency for the Mod 731 and 751)
if (strstr(str, "ENABLE_DES_MODE")!=NULL) {
read = fscanf(f_ini, "%s", str1);
if (strcmp(str1, "YES")==0)
WDcfg.DesMode = 1;
else if (strcmp(str1, "NO")!=0)
printf("%s: invalid option\n", str);
continue;
}
/* // Output file format (BINARY or ASCII) */
/* if (strstr(str, "OUTPUT_FILE_FORMAT")!=NULL) { */
/* read = fscanf(f_ini, "%s", str1); */
/* if (strcmp(str1, "BINARY")==0) */
/* WDcfg.OutFileFlags|= OFF_BINARY; */
/* else if (strcmp(str1, "ASCII")!=0) */
/* printf("%s: invalid output file format\n", str1); */
/* continue; */
/* } */
/* // Header into output file (YES or NO) */
/* if (strstr(str, "OUTPUT_FILE_HEADER")!=NULL) { */
/* read = fscanf(f_ini, "%s", str1); */
/* if (strcmp(str1, "YES")==0) */
/* WDcfg.OutFileFlags|= OFF_HEADER; */
/* else if (strcmp(str1, "NO")!=0) */
/* printf("%s: invalid option\n", str); */
/* continue; */
/* } */
// Interrupt settings (request interrupt when there are at least N events to read; 0=disable interrupts (polling mode))
if (strstr(str, "USE_INTERRUPT")!=NULL) {
read = fscanf(f_ini, "%d", &WDcfg.InterruptNumEvents);
continue;
}
if (!strcmp(str, "FAST_TRIGGER")) {
read = fscanf(f_ini, "%s", str1);
if (strcmp(str1, "DISABLED")==0)
WDcfg.FastTriggerMode = CAEN_DGTZ_TRGMODE_DISABLED;
else if (strcmp(str1, "ACQUISITION_ONLY")==0)
WDcfg.FastTriggerMode = CAEN_DGTZ_TRGMODE_ACQ_ONLY;
else
printf("%s: Invalid Parameter\n", str);
continue;
}
if (strstr(str, "ENABLED_FAST_TRIGGER_DIGITIZING")!=NULL) {
read = fscanf(f_ini, "%s", str1);
if (strcmp(str1, "YES")==0)
WDcfg.FastTriggerEnabled= 1;
else if (strcmp(str1, "NO")!=0)
printf("%s: invalid option\n", str);
continue;
}
// DC offset (percent of the dynamic range, -50 to 50)
if (!strcmp(str, "DC_OFFSET")) {
float dc;
read = fscanf(f_ini, "%f", &dc);
if (tr != -1) {
// WDcfg.FTDCoffset[tr] = dc;
WDcfg.FTDCoffset[tr*2] = (uint32_t)dc;
WDcfg.FTDCoffset[tr*2+1] = (uint32_t)dc;
continue;
}
val = (int)((dc+50) * 65535 / 100);
if (ch == -1)
for(i=0; i<MAX_SET; i++)
WDcfg.DCoffset[i] = val;
else
WDcfg.DCoffset[ch] = val;
continue;
}
if (strstr(str, "GRP_CH_DC_OFFSET")!=NULL) {
float dc[8];
read = fscanf(f_ini, "%f,%f,%f,%f,%f,%f,%f,%f", &dc[0], &dc[1], &dc[2], &dc[3], &dc[4], &dc[5], &dc[6], &dc[7]);
for(i=0; i<MAX_SET; i++) {
val = (int)((dc[i]+50) * 65535 / 100);
WDcfg.DCoffsetGrpCh[ch][i] = val;
}
continue;
}
// Threshold
if (strstr(str, "TRIGGER_THRESHOLD")!=NULL) {
read = fscanf(f_ini, "%d", &val);
if (tr != -1) {
// WDcfg.FTThreshold[tr] = val;
WDcfg.FTThreshold[tr*2] = val;
WDcfg.FTThreshold[tr*2+1] = val;
continue;
}
if (ch == -1)
for(i=0; i<MAX_SET; i++)
WDcfg.Threshold[i] = val;
else
WDcfg.Threshold[ch] = val;
continue;
}
// Group Trigger Enable Mask (hex 8 bit)
if (strstr(str, "GROUP_TRG_ENABLE_MASK")!=NULL) {
read = fscanf(f_ini, "%x", &val);
if (ch == -1)
for(i=0; i<MAX_SET; i++)
WDcfg.GroupTrgEnableMask[i] = val & 0xFF;
else
WDcfg.GroupTrgEnableMask[ch] = val & 0xFF;
continue;
}
// Channel Auto trigger (DISABLED, ACQUISITION_ONLY, ACQUISITION_AND_TRGOUT)
if (strstr(str, "CHANNEL_TRIGGER")!=NULL) {
CAEN_DGTZ_TriggerMode_t tm;
read = fscanf(f_ini, "%s", str1);
if (strcmp(str1, "DISABLED")==0)
tm = CAEN_DGTZ_TRGMODE_DISABLED;
else if (strcmp(str1, "ACQUISITION_ONLY")==0)
tm = CAEN_DGTZ_TRGMODE_ACQ_ONLY;
else if (strcmp(str1, "ACQUISITION_AND_TRGOUT")==0)
tm = CAEN_DGTZ_TRGMODE_ACQ_AND_EXTOUT;
else {
printf("%s: Invalid Parameter\n", str);
continue;
}
if (ch == -1)
for(i=0; i<MAX_SET; i++)
WDcfg.ChannelTriggerMode[i] = tm;
else
WDcfg.ChannelTriggerMode[ch] = tm;
continue;
}
// Front Panel LEMO I/O level (NIM, TTL)
if (strstr(str, "FPIO_LEVEL")!=NULL) {
read = fscanf(f_ini, "%s", str1);
if (strcmp(str1, "TTL")==0)
WDcfg.FPIOtype = 1;
else if (strcmp(str1, "NIM")!=0)
printf("%s: invalid option\n", str);
continue;
}
// Channel Enable (or Group enable for the V1740) (YES/NO)
if (strstr(str, "ENABLE_INPUT")!=NULL) {
read = fscanf(f_ini, "%s", str1);
if (strcmp(str1, "YES")==0) {
if (ch == -1)
WDcfg.EnableMask = 0xFF;
else
WDcfg.EnableMask |= (1 << ch);
continue;
} else if (strcmp(str1, "NO")==0) {
if (ch == -1)
WDcfg.EnableMask = 0x00;
else
WDcfg.EnableMask &= ~(1 << ch);
continue;
} else {
printf("%s: invalid option\n", str);
}
continue;
}
printf("%s: invalid setting\n", str);
}
return 0;
}
int init_V1742(int handle)
{
/* WaveDumpRun_t WDrun; */
int ret=CAEN_DGTZ_Success;
/* int handle; */
ERROR_CODES ErrCode= ERR_NONE;
char ConfigFileName[100];
/* int isVMEDevice= 0; */
int MajorNumber;
/* v1742_eventPtr=NULL; */
/* int nCycles= 0; */
FILE *f_ini;
printf("**************************************************************\n");
printf(" Initialise V1742\n");
printf("**************************************************************\n");
/* *************************************************************************************** */
/* Open and parse configuration file */
/* *************************************************************************************** */
strcpy(ConfigFileName, DEFAULT_CONFIG_FILE);
printf("Opening Configuration File %s\n", ConfigFileName);
f_ini = fopen(ConfigFileName, "r");
if (f_ini == NULL ) {
ErrCode = ERR_CONF_FILE_NOT_FOUND;
return ErrCode;
}
ParseConfigFile(f_ini);
fclose(f_ini);
/* /\* *************************************************************************************** *\/ */
/* /\* Open the digitizer and read the board information *\/ */
/* /\* *************************************************************************************** *\/ */
/* isVMEDevice = WDcfg.BaseAddress ? 1 : 0; */
/* /\* HACK, the function to load the correction table is a CAENComm function, so we first open the */
/* device with CAENComm lib, read the the correction table and suddenly close the device. *\/ */
/* if(WDcfg.useCorrections != -1) { // use Corrections Manually */
/* //Beware: Get Device ID from the vme_bridge.h and not from config file */
/* if (ret = (CAEN_DGTZ_ErrorCode) CAENComm_OpenDevice((CAENComm_ConnectionType)WDcfg.LinkType,VME_DEVICE_ID,WDcfg.ConetNode,WDcfg.BaseAddress,&handle)) { */
/* ErrCode = ERR_DGZ_OPEN; */
/* return ErrCode; */
/* } */
/* if (ret = (CAEN_DGTZ_ErrorCode) LoadCorrectionTables(handle, &Table_gr0, 0, CAEN_DGTZ_DRS4_5GHz)) */
/* return -1; */
/* if (ret = (CAEN_DGTZ_ErrorCode) LoadCorrectionTables(handle, &Table_gr1, 1, CAEN_DGTZ_DRS4_5GHz)) */
/* return -1; */
/* if (ret = (CAEN_DGTZ_ErrorCode) CAENComm_CloseDevice(handle)) */
/* return -1; */
/* SaveCorrectionTable("table0", Table_gr0); */
/* SaveCorrectionTable("table1", Table_gr1); */
/* // write tables to file */
/* } */
/* printf("Opening device %d %d %d %X %d\n",(CAEN_DGTZ_ConnectionType) WDcfg.LinkType,VME_DEVICE_ID, WDcfg.ConetNode, WDcfg.BaseAddress, handle); */
/* //Beware: Get Device ID from the vme_bridge.h and not from config file */
/* //ret = CAEN_DGTZ_OpenDigitizer( (CAEN_DGTZ_ConnectionType) WDcfg.LinkType, VME_DEVICE_ID, WDcfg.ConetNode, WDcfg.BaseAddress, &handle); */
/* // ret = CAEN_DGTZ_OpenDigitizer( (CAEN_DGTZ_ConnectionType) WDcfg.LinkType, VME_DEVICE_ID, WDcfg.ConetNode, WDcfg.BaseAddress, &handle); */
/* ret = CAEN_DGTZ_OpenDigitizer( (CAEN_DGTZ_ConnectionType) 0, 2, 0, 0x500000, &handle); */
/* printf("Open status %d\n",ret); */
/* if (ret) { */
/* ErrCode = ERR_DGZ_OPEN; */
/* return ErrCode; */
/* } */
ret = CAEN_DGTZ_GetInfo(handle, &BoardInfo);
if (ret) {
ErrCode = ERR_BOARD_INFO_READ;
return ErrCode;
}
printf("Connected to CAEN Digitizer Model %s\n", BoardInfo.ModelName);
printf("ROC FPGA Release is %s\n", BoardInfo.ROC_FirmwareRel);
printf("AMC FPGA Release is %s\n", BoardInfo.AMC_FirmwareRel);
// Check firmware rivision (DPP firmwares cannot be used with WaveDump */
sscanf(BoardInfo.AMC_FirmwareRel, "%d", &MajorNumber);
if (MajorNumber >= 128) {
printf("This digitizer has a DPP firmware\n");
ErrCode = ERR_INVALID_BOARD_TYPE;
return ErrCode;
}
// get num of channels, num of bit, num of group of the board */
ret = (CAEN_DGTZ_ErrorCode) GetMoreBoardInfo(handle);
if (ret) {
ErrCode = ERR_INVALID_BOARD_TYPE;
return ErrCode;
}
if( WDcfg.useCorrections == -1 ) { // use automatic corrections
ret = CAEN_DGTZ_LoadDRS4CorrectionData(handle,CAEN_DGTZ_DRS4_5GHz);
ret = CAEN_DGTZ_EnableDRS4Correction(handle);
}
// mask the channels not available for this model
if ((BoardInfo.FamilyCode != CAEN_DGTZ_XX740_FAMILY_CODE) && (BoardInfo.FamilyCode != CAEN_DGTZ_XX742_FAMILY_CODE)){
WDcfg.EnableMask &= (1<<WDcfg.Nch)-1;
} else {
WDcfg.EnableMask &= (1<<(WDcfg.Nch/8))-1;
}
if ((BoardInfo.FamilyCode == CAEN_DGTZ_XX751_FAMILY_CODE) && WDcfg.DesMode) {
WDcfg.EnableMask &= 0xAA;
}
if ((BoardInfo.FamilyCode == CAEN_DGTZ_XX731_FAMILY_CODE) && WDcfg.DesMode) {
WDcfg.EnableMask &= 0x55;
}
/* // Set plot mask */
/* if ((BoardInfo.FamilyCode != CAEN_DGTZ_XX740_FAMILY_CODE) && (BoardInfo.FamilyCode != CAEN_DGTZ_XX742_FAMILY_CODE)){ */
/* WDrun.ChannelPlotMask = WDcfg.EnableMask; */
/* } else { */
/* WDrun.ChannelPlotMask = (WDcfg.FastTriggerEnabled == 0) ? 0xFF: 0x1FF; */
/* } */
/* *************************************************************************************** */
/* program the digitizer */
/* *************************************************************************************** */
ret = (CAEN_DGTZ_ErrorCode) ProgramDigitizer(handle);
if (ret) {
ErrCode = ERR_DGZ_PROGRAM;
return ErrCode;
}
/* printf("%d\n",ret); */
/* // Select the next enabled group for plotting */
/* if ((WDcfg.EnableMask) && (WDcfg.Nch>8)) */
/* if( ((WDcfg.EnableMask>>WDrun.GroupPlotIndex)&0x1)==0 ) */
/* GoToNextEnabledGroup(&WDrun, WDcfg); */
// Read again the board infos, just in case some of them were changed by the programming
// (like, for example, the TSample and the number of channels if DES mode is changed)
ret = CAEN_DGTZ_GetInfo(handle, &BoardInfo);
if (ret) {
ErrCode = ERR_BOARD_INFO_READ;
return ErrCode;
}
ret = (CAEN_DGTZ_ErrorCode) GetMoreBoardInfo(handle);
if (ret) {
ErrCode = ERR_INVALID_BOARD_TYPE;
return ErrCode;
}
//if (WDcfg.TestPattern) CAEN_DGTZ_DisableDRS4Correction(handle);
//else CAEN_DGTZ_EnableDRS4Correction(handle);
/* if (WDrun.Restart && WDrun.AcqRun) */
CAEN_DGTZ_SWStartAcquisition(handle);
printf("**************************************************************\n");
printf(" Initialise V1742 completed. Starting run \n");
printf("**************************************************************\n");
return 0;
}
/* else */
/* printf("[s] start/stop the acquisition, [q] quit, [SPACE] help\n"); */
/* WDrun.Restart = 0; */
/* PrevRateTime = get_time(); */
/* *************************************************************************************** */
/* Readout Loop */
/* *************************************************************************************** */
/* while(!WDrun.Quit) { */
/* // Check for keyboard commands (key pressed) */
/* CheckKeyboardCommands(handle, &WDrun, WDcfg, BoardInfo); */
/* if (WDrun.Restart) { */
/* CAEN_DGTZ_SWStopAcquisition(handle); */
/* CAEN_DGTZ_FreeReadoutBuffer(&v1742_buffer); */
/* ClosePlotter(); */
/* PlotVar = NULL; */