-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmtc_util.c
1374 lines (1201 loc) · 37.9 KB
/
mtc_util.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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "include/xl_regs.h"
#include "penn_daq.h"
#include "fec_util.h"
#include "mtc_util.h"
#include "net_util.h"
static char* getXilinxData(long *howManyBits);
static char xilinxfilename[] = MTC_XILINX_LOCATION;
SBC_Packet aPacket;
int trigger_scan(char *buffer)
{
int trigger = 13;
int crate_mask = 0x4;
int min_thresh = 0;
int thresh_dac = 0;
int quick_mode = 0;
int total_nhit = -1;
uint32_t slot_mask[20];
int i,j,icrate,ifec,ithresh,inhit;
for (i=0;i<20;i++){
slot_mask[i] = 0xFFFF;
}
char filename[100];
sprintf(filename,"trigger_scan.dat");
FILE *file;
char *words,*words2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c'){
words2 = strtok(NULL, " ");
crate_mask = strtoul(words2,(char**)NULL,16);
}else if (words[1] == 's'){
words2 = strtok(NULL, " ");
for (i=0;i<20;i++){
slot_mask[i] = strtoul(words2,(char**)NULL,16);
}
}else if (words[1] == 't'){
words2 = strtok(NULL, " ");
trigger = atoi(words2);
if (trigger > 13 || trigger < 0){
printsend("Invalid trigger, resetting to default (13)\n");
trigger = 13;
}
}else if (words[1] == 'f'){
words2 = strtok(NULL, " ");
sprintf(filename,"%s",words2);
}else if (words[1] == 'n'){
words2 = strtok(NULL, " ");
total_nhit = atoi(words2);
}else if (words[1] == 'm'){
words2 = strtok(NULL, " ");
min_thresh = atoi(words2);
}else if (words[1] == 'd'){
words2 = strtok(NULL, " ");
thresh_dac = atoi(words2);
}else if (words[1] == 'q'){
quick_mode = 1;
}else if (words[1] == 'h'){
printsend("Usage: trigger_scan -c [crate mask (hex)]"
" -t [trigger id to enable in mask (0-13)]"
" -s [slot mask for all crates (hex)] -(00 - 18) [slot mask for crate (00 - 18) (hex)] -f [output file name]"
" -n [max nhit to scan up to. defaults to maximum for number of slots masked in (int)]"
" -m [minimum threshold to scan down to in adc counts. defaults to 0.]"
" -d [threshold dac to program (defaults to correct one for chosen trigger id) (int)"
" -q (enables quick mode; only samples every 10th dac count)\n");
return 0;
}else if (words[1] == '0'){
if (words[2] == '0'){
words2 = strtok(NULL, " ");
slot_mask[0] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '1'){
words2 = strtok(NULL, " ");
slot_mask[1] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '2'){
words2 = strtok(NULL, " ");
slot_mask[2] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '3'){
words2 = strtok(NULL, " ");
slot_mask[3] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '4'){
words2 = strtok(NULL, " ");
slot_mask[4] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '5'){
words2 = strtok(NULL, " ");
slot_mask[5] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '6'){
words2 = strtok(NULL, " ");
slot_mask[6] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '7'){
words2 = strtok(NULL, " ");
slot_mask[7] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '8'){
words2 = strtok(NULL, " ");
slot_mask[8] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '9'){
words2 = strtok(NULL, " ");
slot_mask[9] = strtoul(words2, (char **) NULL, 16);
}
}else if (words[1] == '1'){
if (words[2] == '0'){
words2 = strtok(NULL, " ");
slot_mask[10] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '1'){
words2 = strtok(NULL, " ");
slot_mask[11] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '2'){
words2 = strtok(NULL, " ");
slot_mask[12] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '3'){
words2 = strtok(NULL, " ");
slot_mask[13] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '4'){
words2 = strtok(NULL, " ");
slot_mask[14] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '5'){
words2 = strtok(NULL, " ");
slot_mask[15] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '6'){
words2 = strtok(NULL, " ");
slot_mask[16] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '7'){
words2 = strtok(NULL, " ");
slot_mask[17] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '8'){
words2 = strtok(NULL, " ");
slot_mask[18] = strtoul(words2, (char **) NULL, 16);
}else if (words[2] == '9'){
words2 = strtok(NULL, " ");
slot_mask[19] = strtoul(words2, (char **) NULL, 16);
}
}
}
words = strtok(NULL, " ");
}
if (sbc_is_connected == 0){
printsend("SBC not connected.\n");
return -1;
}
file = fopen(filename,"w");
printsend("starting a trigger scan\n");
uint32_t select_reg,result,beforegt,aftergt;
uint32_t gtdelay = 150;
uint16_t ped_width = 25;
int slot_num = 14;
int counts[14];
for (i=0;i<14;i++){
counts[i] = 10;
}
// set up the mtcd to send out softgts
int errors = setup_pedestals(0,ped_width,gtdelay,0);
if (errors){
printsend("Error setting up MTC for pedestals. Exiting\n");
unset_ped_crate_mask(MASKALL);
unset_gt_crate_mask(MASKALL);
return -1;
}
//enable GT/PED only for selected crate
unset_ped_crate_mask(MASKALL);
unset_gt_crate_mask(MASKALL);
set_ped_crate_mask(crate_mask);
set_gt_crate_mask(crate_mask);
float values[10000];
uint32_t pedestals[19][16];
int num_fecs = 0;
for (i=0;i<19;i++){
if ((0x1<<i) & crate_mask){
for (j=0;j<16;j++){
if ((0x1<<j) & slot_mask[i]){
xl3_rw(PED_ENABLE_R + FEC_SEL*j + WRITE_REG,0x0,&result,i);
num_fecs++;
pedestals[i][j] = 0x0;
}
}
}
}
// now we see our max number of nhit
if (total_nhit < 0){
total_nhit = num_fecs*32;
}else if(total_nhit > num_fecs*32){
printf("you dont have enough fecs to test that many nhit\n");
total_nhit = num_fecs*32;
}
printf("Testing nhit up to %d\n",total_nhit);
int current_nhit = 0;
int min_nhit = 0;
int last_zero = 0;
int one_count,noise_count;
int nhit_status;
// we loop over threshold, coming down from 4095
for (ithresh=0;ithresh<4095-min_thresh;ithresh++){
if(ithresh>50 && quick_mode==1) ithresh += 9;
if(thresh_dac != 0)
counts[thresh_dac] = 4095-ithresh;
else
counts[trigger-1] = 4095-ithresh;
// disable triggers while programming dacs due to noise
unset_gt_mask(0xFFFFFFFF);
load_mtc_dacs_counts(counts);
set_gt_mask(1<<(trigger-1));
for (i=0;i<10000;i++)
values[i] = -1.;
one_count = 0;
noise_count = 0;
last_zero = 0;
// now we want to loop over nhit
// we loop over the small subset of nhit that interests us
for (inhit=min_nhit;inhit<total_nhit;inhit++){
// we need to get our pedestals set right
// first we set up all the fully enabled fecs
int full_fecs = inhit/32;
int unfull_fec = inhit%32;
uint32_t unfull_pedestal = 0x0;
for (i=0;i<unfull_fec;i++){
unfull_pedestal |= 0x1<<i;
}
for (icrate=0;icrate<19;icrate++){
if (((0x1<<icrate) & crate_mask)){
for (ifec=0;ifec<16;ifec++){
if (((0x1<<ifec) & slot_mask[icrate])){
if (pedestals[icrate][ifec] == 0xFFFFFFFF){
if (full_fecs > 0){
// we should keep this one fully on
full_fecs--;
}else if (full_fecs == 0){
// this one should have the leftovers
full_fecs--;
pedestals[icrate][ifec] = unfull_pedestal;
xl3_rw(PED_ENABLE_R + ifec*FEC_SEL + WRITE_REG,pedestals[icrate][ifec],&result,icrate);
}else{
// turn this one back off
pedestals[icrate][ifec] = 0x0;
xl3_rw(PED_ENABLE_R + ifec*FEC_SEL + WRITE_REG,pedestals[icrate][ifec],&result,icrate);
}
}else{
if (full_fecs > 0){
// turn this one back fully on
pedestals[icrate][ifec] = 0xFFFFFFFF;
xl3_rw(PED_ENABLE_R + ifec*FEC_SEL + WRITE_REG,pedestals[icrate][ifec],&result,icrate);
full_fecs--;
}else if (full_fecs == 0){
// this one should have the leftovers
full_fecs--;
pedestals[icrate][ifec] = unfull_pedestal;
xl3_rw(PED_ENABLE_R + ifec*FEC_SEL + WRITE_REG,pedestals[icrate][ifec],&result,icrate);
}else if (pedestals[icrate][ifec] == 0x0){
// this one can stay off
}else{
// turn this one fully off
pedestals[icrate][ifec] = 0x0;
xl3_rw(PED_ENABLE_R + ifec*FEC_SEL + WRITE_REG,pedestals[icrate][ifec],&result,icrate);
}
}
}
}
}
}
// we are now sitting at the correct nhit
// and we have the right threshold set
// lets do this trigger check thing
// initial gt count
mtc_reg_read(MTCOcGtReg,&beforegt);
// send 500 pulses
multi_softgt(500);
// now get final gt count
mtc_reg_read(MTCOcGtReg,&aftergt);
// top bits are junk
uint32_t diff = (aftergt & 0x00ffffff) - (beforegt & 0x00ffffff);
values[inhit] = (float) diff / 500.0;
// we will start at an nhit based on where we
// start seeing more than zero triggers
if (values[inhit] == 0.){
last_zero = inhit;
}
// we will stop at an nhit based on where we
// hit the triangle of bliss
if (values[inhit] > 0.9 && values[inhit] < 1.1){
one_count++;
}
// we will also stop if we are stuck in the noise
// for too long, meaning we arent at a high enough
// threshold to see the triangle of bliss
if (values[inhit] > 1.2){
noise_count++;
}
if (one_count > 5 || noise_count > 25){
// we are done with this threshold
min_nhit = last_zero < 5 ? 0 : last_zero-5;
break; // break out of nhit loop
}
} // loop over nhit
// now write out this thresholds worth of results to file
for (i=1;i<10000;i++){
// only print out nhit we tested
if (values[i] >= 0){
fprintf(file,"%d %d %f\n",ithresh,i,values[i]);
printf("Finished %d\n",ithresh);
}
}
} // end loop over threshold
unset_gt_mask(MASKALL);
fclose(file);
return 0;
}
int mtc_xilinxload(void)
{
char *data;
long howManybits;
long bitIter;
uint32_t word;
uint32_t dp;
uint32_t temp;
printsend("loading xilinx\n");
data = getXilinxData(&howManybits);
if ((data == NULL) || (howManybits == 0)){
printsend("error getting xilinx data\n");
return -1;
}
aPacket.cmdHeader.destination = 0x3;
aPacket.cmdHeader.cmdID = 0x1;
aPacket.cmdHeader.numberBytesinPayload = sizeof(SNOMtc_XilinxLoadStruct) + howManybits;
printsend("numbytes is %d, size is %d\n",(int)aPacket.cmdHeader.numberBytesinPayload,(int)sizeof(SNOMtc_XilinxLoadStruct));
aPacket.numBytes = aPacket.cmdHeader.numberBytesinPayload+256+16;
SNOMtc_XilinxLoadStruct *payloadPtr = (SNOMtc_XilinxLoadStruct *)aPacket.payload;
payloadPtr->baseAddress = 0x7000;
payloadPtr->addressModifier = 0x29;
payloadPtr->errorCode = 0;
payloadPtr->programRegOffset = MTCXilProgReg;
payloadPtr->fileSize = howManybits;
char *p = (char *)payloadPtr + sizeof(SNOMtc_XilinxLoadStruct);
strncpy(p, data, howManybits);
do_mtc_xilinx_cmd(&aPacket);
long errorCode = payloadPtr->errorCode;
if (errorCode){
printsend( "Error code: %d \n",(int)errorCode);
}
printsend("Xilinx loading complete\n");
free(data);
data = (char *) NULL;
return 0;
}
/* return a structure of chars that sets up the data structure with
* all the data for the xilinx chips. function to read in
* bitstream. filename hard-coded for now.
*
* PW 8/21/97
*/
static char* getXilinxData(long *howManyBits)
{
char c;
FILE *fp;
char *data = NULL;
if ((fp = fopen(xilinxfilename, "r")) == NULL ) {
printsend( "getXilinxData: cannot open file %s\n", xilinxfilename);
return (char*) NULL;
}
if ((data = (char *) malloc(MAX_DATA_SIZE)) == NULL) {
//perror("GetXilinxData: ");
printsend("GetXilinxData: malloc error\n");
return (char*) NULL;
}
/* skip header -- delimited by two slashes.
if ( (c = getc(fp)) != '/') {
fprintf(stderr, "Invalid file format Xilinx file.\n");
return (char*) NULL;
}
while (( (c = getc(fp)) != EOF ) && ( c != '/'))
;
*/
/* get real data now. */
*howManyBits = 0;
while (( (data[*howManyBits] = getc(fp)) != EOF)
&& ( *howManyBits < MAX_DATA_SIZE)) {
/* skip newlines, tabs, carriage returns */
if ((data[*howManyBits] != '\n') &&
(data[*howManyBits] != '\r') &&
(data[*howManyBits] != '\t') ) {
(*howManyBits)++;
}
}
fclose(fp);
return data;
}
/*
* unset_gt_mask(raw_trig_types)
* unset bit(s) in the global trigger mask, according to mnemonics
* M. Neubauer 2 Sept 1997
*/
int unset_gt_mask_cmd(char *buffer){
uint32_t type = 0xFFFFFFFF;
char *words,*words2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 't'){
words2 = strtok(NULL, " ");
type = strtoul(words2,(char **) NULL,16);
}
if (words[1] == 'h'){
printsend("Usage: unset_gt_mask -t [raw trigs to remove (hex)]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
unset_gt_mask(type);
return 0;
}
int set_gt_mask_cmd(char *buffer){
uint32_t type = 0x0;
int clear = 0;
char *words,*words2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 't'){
words2 = strtok(NULL, " ");
type = strtoul(words2,(char **) NULL,16);
}
if (words[1] == 'c'){
clear = 1;
}
if (words[1] == 'h'){
printsend("Usage: set_gt_mask -t [raw trigs to add (hex)]"
" -c (clear gt mask first)\n");
return 0;
}
}
words = strtok(NULL, " ");
}
if (clear == 1)
unset_gt_mask(0xFFFFFFFF);
set_gt_mask(type);
return 0;
}
int mtc_read(char *buffer){
uint32_t address = 0x0;
uint32_t data = 0x0;
char *words,*words2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'a'){
words2 = strtok(NULL, " ");
address = strtoul(words2,(char **) NULL,16);
}
if (words[1] == 'h'){
printsend("Usage: mtc_read -a [address (hex)]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
mtc_reg_read(address, &data);
printsend("Received %08x\n",data);
return 0;
}
int mtc_write(char *buffer){
uint32_t address = 0x0;
uint32_t data = 0x0;
char *words,*words2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'd'){
words2 = strtok(NULL, " ");
data = strtoul(words2,(char **) NULL,16);
}
if (words[1] == 'a'){
words2 = strtok(NULL, " ");
address = strtoul(words2,(char **) NULL,16);
}
if (words[1] == 'h'){
printsend("Usage: mtc_write -d [data (hex)] -a [address (hex)]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
mtc_reg_write(address, data);
printsend("wrote %08x\n",data);
return 0;
}
void unset_gt_mask(unsigned long raw_trig_types) {
uint32_t temp;
mtc_reg_read(MTCMaskReg, &temp);
mtc_reg_write(MTCMaskReg, temp & ~raw_trig_types);
printsend("Triggers have been removed from the GT Mask\n");
}
void set_gt_mask(uint32_t raw_trig_types){
uint32_t temp;
mtc_reg_read(MTCMaskReg, &temp);
mtc_reg_write(MTCMaskReg, temp | raw_trig_types);
//printsend("Triggers have been added to the GT Mask\n");
}
/*
* Mask out crates for pedestal lines, according to mnemonics.
*
* jrk 1 Sept. 1997 (modified 8 Sept 1997 M. Neubauer)
*/
int unset_ped_crate_mask_cmd(char *buffer){
uint32_t crates = 0xFFFFF;
char *words,*words2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c'){
words2 = strtok(NULL, " ");
crates = strtoul(words2,(char **) NULL,16);
}
if (words[1] == 'h'){
printsend("Usage: unset_ped_crate_mask -c [crate_mask (hex)]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
unset_ped_crate_mask(crates);
}
int set_ped_crate_mask_cmd(char *buffer){
uint32_t crates = 0x0;
char *words,*words2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c'){
words2 = strtok(NULL, " ");
crates = strtoul(words2,(char **) NULL,16);
}
if (words[1] == 'h'){
printsend("Usage: set_ped_crate_mask -c [crate_mask (hex)]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
set_ped_crate_mask(crates);
}
void unset_ped_crate_mask(unsigned long crates) {
uint32_t temp;
mtc_reg_read(MTCPmskReg, &temp);
mtc_reg_write(MTCPmskReg, temp & ~crates);
//printsend("Crates have been removed from the Pedestal Crate Mask\n");
}
uint32_t set_ped_crate_mask(uint32_t crates){
uint32_t old_ped_crate_mask;
mtc_reg_read(MTCPmskReg, &old_ped_crate_mask);
mtc_reg_write(MTCPmskReg, old_ped_crate_mask | crates);
//printsend("Crates have been added to the Pedestal Crate Mask\n");
return old_ped_crate_mask;
}
/*
* Mask out crates for global trigger lines, according to mnemonics.
*
*/
int unset_gt_crate_mask_cmd(char *buffer){
uint32_t crates = 0xFFFFF;
char *words,*words2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c'){
words2 = strtok(NULL, " ");
crates = strtoul(words2,(char **) NULL,16);
}
if (words[1] == 'h'){
printsend("Usage: unset_gt_crate_mask -c [crate_mask (hex)]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
unset_gt_crate_mask(crates);
}
int set_gt_crate_mask_cmd(char *buffer){
uint32_t crates = 0x0;
char *words,*words2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c'){
words2 = strtok(NULL, " ");
crates = strtoul(words2,(char **) NULL,16);
}
if (words[1] == 'h'){
printsend("Usage: set_gt_crate_mask -c [crate_mask (hex)]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
set_gt_crate_mask(crates);
}
void unset_gt_crate_mask(unsigned long crates) {
uint32_t temp;
mtc_reg_read(MTCGmskReg, &temp);
mtc_reg_write(MTCGmskReg, temp & ~crates);
//printsend("Crates have been removed from the GT Crate Mask\n");
}
void set_gt_crate_mask(uint32_t crates){
uint32_t temp;
mtc_reg_read(MTCGmskReg, &temp);
mtc_reg_write(MTCGmskReg, temp | crates);
//printsend("Crates have been added to the GT Crate Mask\n");
}
int set_thresholds(char *buffer){
mtc_cons thresholds;
int i;
for (i=0;i<14;i++){
thresholds.mtca_dac_values[i] = -4900;
}
char *words,*words2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == '0'){
words2 = strtok(NULL, " ");
thresholds.mtca_dac_values[0] = (float) strtod(words2,(char**)NULL)*1000;
}
if (words[1] == '1'){
words2 = strtok(NULL, " ");
thresholds.mtca_dac_values[1] = (float) strtod(words2,(char**)NULL)*1000;
}
if (words[1] == '2'){
words2 = strtok(NULL, " ");
thresholds.mtca_dac_values[2] = (float) strtod(words2,(char**)NULL)*1000;
}
if (words[1] == '3'){
words2 = strtok(NULL, " ");
thresholds.mtca_dac_values[3] = (float) strtod(words2,(char**)NULL)*1000;
}
if (words[1] == '4'){
words2 = strtok(NULL, " ");
thresholds.mtca_dac_values[4] = (float) strtod(words2,(char**)NULL)*1000;
}
if (words[1] == '5'){
words2 = strtok(NULL, " ");
thresholds.mtca_dac_values[5] = (float) strtod(words2,(char**)NULL)*1000;
}
if (words[1] == '6'){
words2 = strtok(NULL, " ");
thresholds.mtca_dac_values[6] = (float) strtod(words2,(char**)NULL)*1000;
}
if (words[1] == '7'){
words2 = strtok(NULL, " ");
thresholds.mtca_dac_values[7] = (float) strtod(words2,(char**)NULL)*1000;
}
if (words[1] == '8'){
words2 = strtok(NULL, " ");
thresholds.mtca_dac_values[8] = (float) strtod(words2,(char**)NULL)*1000;
}
if (words[1] == '9'){
words2 = strtok(NULL, " ");
thresholds.mtca_dac_values[9] = (float) strtod(words2,(char**)NULL)*1000;
}
if (words[1] == 'a'){
words2 = strtok(NULL, " ");
thresholds.mtca_dac_values[10] = (float) strtod(words2,(char**)NULL)*1000;
}
if (words[1] == 'b'){
words2 = strtok(NULL, " ");
thresholds.mtca_dac_values[11] = (float) strtod(words2,(char**)NULL)*1000;
}
if (words[1] == 'c'){
words2 = strtok(NULL, " ");
thresholds.mtca_dac_values[12] = (float) strtod(words2,(char**)NULL)*1000;
}
if (words[1] == 'd'){
words2 = strtok(NULL, " ");
thresholds.mtca_dac_values[13] = (float) strtod(words2,(char**)NULL)*1000;
}
if (words[1] == 'h'){
printsend("Usage: set_thresholds -(0..d) [level (float)]\n");
return 0;
}
}
words = strtok(NULL, " ");
}
load_mtc_dacs(&thresholds);
return 0;
}
/* load_mtc_dacs()
* Loads all the mtc/a dac thresholds. Values are taken from a file for now.
*
* M. Neubauer 4 Sept 1997
*/
int load_mtc_dacs(mtc_cons *mtc_cons_ptr) {
unsigned long shift_value;
unsigned short raw_dacs[14];
char dac_names[][14]={"N100LO","N100MED","N100HI","NHIT20","NH20LB","ESUMHI",
"ESUMLO","OWLEHI","OWLELO","OWLN","SPARE1","SPARE2",
"SPARE3","SPARE4"};
short rdbuf;
int i, j, bi, di;
float mV_dacs;
printsend("Loading MTC/A threshold DACs...\n");
/* convert each threshold from mVolts to raw value and load into
raw_dacs array */
for (i = 0; i < 14; i++) {
rdbuf = mtc_cons_ptr->mtca_dac_values[i];
//raw_dacs[i] = ((2048 * rdbuf)/5000) + 2048;
raw_dacs[i] = MTCA_DAC_SLOPE * rdbuf + MTCA_DAC_OFFSET;
mV_dacs = (((float)raw_dacs[i]/2048) * 5000.0) - 5000.0;
printsend( "\t%s\t threshold set to %6.2f mVolts\n", dac_names[i],
mV_dacs);
}
/* set DACSEL */
mtc_reg_write(MTCDacCntReg,DACSEL);
/* shift in raw DAC values */
for (i = 0; i < 4 ; i++) {
mtc_reg_write(MTCDacCntReg,DACSEL | DACCLK); /* shift in 0 to first 4 dummy bits */
mtc_reg_write(MTCDacCntReg,DACSEL);
}
shift_value = 0UL;
for (bi = 11; bi >= 0; bi--) { /* shift in 12 bit word for each DAC */
for (di = 0; di < 14 ; di++){
if (raw_dacs[di] & (1UL << bi))
shift_value |= (1UL << di);
else
shift_value &= ~(1UL << di);
}
mtc_reg_write(MTCDacCntReg,shift_value | DACSEL);
mtc_reg_write(MTCDacCntReg,shift_value | DACSEL | DACCLK);
mtc_reg_write(MTCDacCntReg,shift_value | DACSEL);
}
/* unset DASEL */
mtc_reg_write(MTCDacCntReg,0x0);
printsend("DAC loading complete\n");
return 0;
}
int load_mtc_dacs_counts(int *counts)
{
printsend("Loading MTC/A threshold DACs...\n");
int i,bi,di;
uint32_t shift_value;
float mv_dacs;
char dac_names[][14]={"N100LO","N100MED","N100HI","NHIT20","NH20LB","ESUMHI",
"ESUMLO","OWLEHI","OWLELO","OWLN","SPARE1","SPARE2",
"SPARE3","SPARE4"};
for (i=0;i<14;i++){
mv_dacs = ((float) counts[i]/2048)*5000.0-5000.0;
printsend( "\t%s\t threshold set to %6.2f mVolts (%d counts)\n",dac_names[i],mv_dacs,counts[i]);
}
/* set DACSEL */
mtc_reg_write(MTCDacCntReg,DACSEL);
/* shift in raw DAC values */
for (i = 0; i < 4 ; i++) {
mtc_reg_write(MTCDacCntReg,DACSEL | DACCLK); /* shift in 0 to first 4 dummy bits */
mtc_reg_write(MTCDacCntReg,DACSEL);
}
shift_value = 0UL;
for (bi = 11; bi >= 0; bi--) { /* shift in 12 bit word for each DAC */
for (di = 0; di < 14 ; di++){
if (counts[di] & (1UL << bi))
shift_value |= (1UL << di);
else
shift_value &= ~(1UL << di);
}
mtc_reg_write(MTCDacCntReg,shift_value | DACSEL);
mtc_reg_write(MTCDacCntReg,shift_value | DACSEL | DACCLK);
mtc_reg_write(MTCDacCntReg,shift_value | DACSEL);
}
/* unset DASEL */
mtc_reg_write(MTCDacCntReg,0x0);
printsend("DAC loading complete\n");
return 0;
}
/*
* set_lockout_width(width)
* sets the width of the global trigger lockout time
* width is in ns
* M. Neubauer 2 Sept 1997
*/
int set_lockout_width(unsigned short width) {
unsigned long gtlock_value;
if ((width < 20) || (width > 5100)) {
printsend("Lockout width out of range\n");
return -1;
}
gtlock_value = ~(width / 20);
uint32_t temp;
mtc_reg_write(MTCGtLockReg,gtlock_value);
mtc_reg_read(MTCControlReg,&temp);
mtc_reg_write(MTCControlReg,temp | LOAD_ENLK); /* write GTLOCK value */
mtc_reg_read(MTCControlReg,&temp);
mtc_reg_write(MTCControlReg,temp & ~LOAD_ENLK); /* toggle load enable */
//printsend( "Lockout width is set to %u ns\n", width);
return 0;
}
/* set_gt_counter(count)
* load a count into the global trigger counter
*
* M. Neubauer 2 Sept 1997
*/
int set_gt_counter(unsigned long count) {
unsigned long shift_value;
short j;
uint32_t temp;
for (j = 23; j >= 0; j--){
shift_value = ((count >> j) & 0x01) == 1 ? SERDAT | SEN : SEN ;
mtc_reg_write(MTCSerialReg,shift_value);
mtc_reg_read(MTCSerialReg,&temp);
mtc_reg_write(MTCSerialReg,temp | SHFTCLKGT); /* clock in SERDAT */
}
mtc_reg_read(MTCControlReg,&temp);
mtc_reg_write(MTCControlReg,temp | LOAD_ENGT); /* toggle load enable */
mtc_reg_read(MTCControlReg,&temp);
mtc_reg_write(MTCControlReg,temp & ~LOAD_ENGT); /* toggle load enable */
printsend("The GT counter has been loaded\n");
return 0;
}
/*
* set_prescale(scale)-
* Set up prescaler for desired number of NHIT_100_LO triggers per PRESCALE trigger
* scale is the number of NHIT_100_LO triggers per PRESCALE trigger
* M. Neubauer 29 Aug 1997
*/
int set_prescale(unsigned short scale) {
uint32_t temp;
if (scale < 2) {
printsend("Prescale value out of range\n");
return -1;
}
mtc_reg_write(MTCScaleReg,~(scale-1));
mtc_reg_read(MTCControlReg,&temp);
mtc_reg_write(MTCControlReg,temp | LOAD_ENPR);
mtc_reg_read(MTCControlReg,&temp);
mtc_reg_write(MTCControlReg,temp & ~LOAD_ENPR); /* toggle load enable */
printsend( "Prescaler set to %d NHIT_100_LO per PRESCALE\n", scale);
return 0;
}
/*
* set_pulser_frequency(freq)-
* Set up the pulser for the desired frequency using the 50 MHz clock or SOFT_GT are source
* freq is in Hz. Pass zero for SOFT_GT as source of the pulser
* M. Neubauer 29 Aug 1997
*/
int set_pulser_frequency(float freq) {
unsigned long pulser_value,
shift_value,
prog_freq;
short j;
uint32_t temp;
if (freq <= 1.0e-3) { /* SOFT_GTs as pulser */
pulser_value = 0;
printsend("SOFT_GT is set to source the pulser\n");
}
else {
pulser_value = (unsigned long)((781250 / freq) - 1); /* 50MHz counter as pulser */
prog_freq = (unsigned long)(781250/(pulser_value + 1));
if ((pulser_value < 1) || (pulser_value > 167772216)) {
printsend( "Pulser frequency out of range\n", prog_freq);
return -1;
}
}
for (j = 23; j >= 0; j--){
shift_value = ((pulser_value >> j) & 0x01) == 1 ? SERDAT|SEN : SEN;
mtc_reg_write(MTCSerialReg,shift_value);
mtc_reg_read(MTCSerialReg,&temp);
mtc_reg_write(MTCSerialReg,temp | SHFTCLKPS); /* clock in SERDAT */
}
mtc_reg_read(MTCControlReg,&temp);
mtc_reg_write(MTCControlReg,temp | LOAD_ENPS);
mtc_reg_read(MTCControlReg,&temp);
mtc_reg_write(MTCControlReg,temp & ~LOAD_ENPS); /* toggle load enable */
//printsend( "Pulser frequency is set to %u Hz\n", prog_freq);
return 0;
}
/*
* set_pedestal_width(width)
* set width of PED pulses
* width is in 5 ns increments from 5 to 1275
* M. Neubauer 1 Sept 1997
*/
int set_pedestal_width(unsigned short width) {
uint32_t temp;
unsigned long pwid_value;
if ((width < 5) || (width > 1275)) {
printsend("Pedestal width out of range\n");
return -1;
}
pwid_value = ~(width / 5);