-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcmos_m_gtvalid.c
663 lines (591 loc) · 28.3 KB
/
cmos_m_gtvalid.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
/*
*cmos_m_gtvalid.c
* Main program to determine cmos gtvalid timing.
* Enable secondary current source and adjust bits
*
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "include/Record_Info.h"
#include "include/xl_regs.h"
#include "penn_daq.h"
#include "fec_util.h"
#include "mtc_util.h"
#include "net_util.h"
#define PED_WIDTH 25
#define GT_FINE_DEL 0
#define NGTVALID 20 // number of events per iteration
#define GTMAX 1000 // maximum GTVALID width
#define HOWMANY 350
#define TRUE 1
#define FALSE 0
#define GTPED_DELAY 20 // delay between GT and PED due to disc delay
#define TDELAY_EXTRA 0 // extra delay to make target and scope measured GTVALID width equal to each other
//initial values for TAC reference voltages
#define VMAX 203 // upper TAC rail
#define TACREF 72 // lower TAC rail
#define ISETM_START 147 // start value primary current source
#define ISETM 138 // primary current source
#define ISETA 70 // secondary current source, using 75 is aggressive
static int setup_crate(int cn, uint16_t slot_mask),
setup_mtc(uint32_t freq, uint16_t crate_num);
int get_gtdelay(uint16_t crate_num, int wt, float *get_gtchan, uint16_t isetm0, uint16_t isetm1, uint32_t select_reg);
/*
* We can change the delay of the GT on the MTCD. Then on the FECs, we can change the length
* of the GTVALID window for one of the two tacs by setting the ISETM dac. Then channel by
* channel we can change the twiddle bits to further refine the widths. In this code we find the
* length of the GTVALID window by changing the delay until a channel no longer sees triggers.
* We first go channel by channel and determine the delay with the maximum gtvalid width. If
* we are given a cutoff GTVALID delay, we then go back and change the ISETM dac until the
* maximum delay where the channels can still see the trigger is less than our cutoff. We
* then go back and change the twiddle bits until each channel is just over the cutoff.
*
*/
int cmos_m_gtvalid(char *buffer)
{
int freq,result,done, i,j,k,n,ic,ip,ot,wt,error;
int slot,chan;
uint16_t ncrates, db_new;
uint16_t chan_max, chan_max_sec, chan_min;
uint16_t isetm_new[2], iseta, tacbits[32], tacbits_new[2][32], gtflag[2][32];
uint16_t isetm_save[2], tacbits_save[2][32], dac_isetm[2], dac_iseta[2], isetm_start[2];
uint32_t nfifo, nget;
uint16_t chan_max_set[2],chan_min_set[2],cmax[2],cmin[2];;
float gtchan[32], gtchan_set[2][32];
float gtdelay, gtdelta, gtstart, corr_gtdelta, gt_max;
float gt_temp, gt_max_sec, gt_min, gt_other;
float gt_max_set[2], gt_min_set[2], gt_start[2][32];
float ratio, best[32], gmax[2],gmin[2];
float a=10.1, b=5.1, twait;
FILE *fp;
XL3_Packet packet;
uint32_t *pl;
pl = (uint32_t *) packet.payload;
//SNO_seterr_level(7,MTC_FAC);
//SNO_seterr_level(7,DAQ_FAC);
freq = 0;
// DB initialization
hware_vals_t theHWconf[16];
int update_db = 0;
int final_test = 0;
char ft_ids[16][50];
//read in from command line
uint16_t cn = 2;
uint16_t slot_mask = 0xFFFF;
uint32_t chan_mask = 0xFFFFFFFF;
float gt_cutoff = 0;
int do_twiddle = 1;
char *words,*words2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c'){
words2 = strtok(NULL, " ");
cn = atoi(words2);
}else if (words[1] == 'g'){
words2 = strtok(NULL, " ");
gt_cutoff = atof(words2);
}else if (words[1] == 'l'){
words2 = strtok(NULL, " ");
chan_mask = strtoul(words2,(char **)NULL,16);
}else if (words[1] == 'n'){
do_twiddle = 0;
}else if (words[1] == 'd'){
update_db = 1;
}else if (words[1] == '#'){
final_test = 1;
for (i=0;i<16;i++){
if ((0x1<<i) & slot_mask){
words2 = strtok(NULL, " ");
sprintf(ft_ids[i],"%s",words2);
}
}
}else if (words[1] == 's'){
words2 = strtok(NULL, " ");
slot_mask = strtoul(words2,(char**)NULL,16);
}else if (words[1] == 'h'){
printsend("Usage: cmos_m_gtvalid -c [crate num] -s [slot mask (hex)]"
" -g [gt cutoff] -l [channel mask (hex)] -n (no twiddle) -d (write to db)\n");
return -1;
}
}
words = strtok(NULL, " ");
}
//setup crate
setup_crate(cn, slot_mask);
prepare_mtc_pedestals(freq,PED_WIDTH,10,0);
enable_pulser();
unset_gt_crate_mask(MASKALL);
unset_ped_crate_mask(MASKALL);
set_gt_crate_mask(0x1<<cn);
set_ped_crate_mask(0x1<<cn);
//set_gt_crate_mask(MASKALL);
//set_ped_crate_mask(MASKALL);
;
//initialize some constants, DAC address
dac_isetm[0] = 132;
dac_isetm[1] = 131;
dac_iseta[0] = 130;
dac_iseta[1] = 129;
uint32_t select_reg, temp;
// select fec
for (slot=0;slot<16;slot++){
if (slot_mask & (0x1<<slot)){
select_reg = FEC_SEL*slot;
//select which tac to work on
for (wt=0;wt<2;wt++){
if (wt==0)
ot = 1;
else
ot = 0;
//initialize some stuff
for (i=0;i<32;i++)
gtchan[i] = 9999;
//only set DAC's if request setting GTVALID
if (gt_cutoff != 0){
//set TAC reference voltages
error = loadsDac(134,(u_short)VMAX,cn,select_reg);
error+= loadsDac(133,(u_short)TACREF,cn,select_reg);
error+= loadsDac(131,(u_short)ISETM_START,cn,select_reg);
error+= loadsDac(132,(u_short)ISETM_START,cn,select_reg);
//enable TAC secondary current source and adjust bits for
//all channels, the reason for this is so we can turn bits
//off to shorten the TAC time
error+= loadsDac(129,(u_short)ISETA,cn,select_reg);
error+= loadsDac(130,(u_short)ISETA,cn,select_reg);
if (error != 0){
printsend("Error in setting up TAC voltages, exiting...\n");
return 1;
}
printsend("Dacs loaded.\n");
//load cmos shift register to enable twiddle bits
packet.cmdHeader.packet_type = LOADTACBITS_ID;
*pl = cn;
*(pl+1) = select_reg;
for (j=0;j<32;j++){
tacbits[j] = 0x77;
*(uint16_t *)(packet.payload+8+j*2) = tacbits[j];
}
SwapLongBlock(packet.payload,2);
SwapShortBlock(packet.payload+8,32);
do_xl3_cmd(&packet,cn);
printsend("Tac bits loaded.\n");
}
// some board level initialization
isetm_new[0] = ISETM;
isetm_new[1] = ISETM;
for (k=0;k<32;k++){
if (do_twiddle){
tacbits_new[0][k] = 0x7;
tacbits_new[1][k] = 0x7; // enable all tac bits
}else{
tacbits_new[0][k] = 0x0;
tacbits_new[1][k] = 0x0; // disable all tac bits
}
}
isetm_start[0] = ISETM_START;
isetm_start[1] = ISETM_START;
//main loop over channels
printsend("Measuring GTVALID for crate,slot,TAC: %d %d %d\n",cn,slot,wt);
//loop over channels to measure initial GTVALID
for (chan=0;chan<32;chan++){
if (chan_mask & (0x1<<chan)){
//set pedestal enable for channel
xl3_rw(PED_ENABLE_R + select_reg + WRITE_REG,1<<chan,&temp,cn);
error = get_gtdelay(cn, wt, >_temp,isetm_start[0],isetm_start[1],select_reg);
gtchan[chan] = gt_temp;
gt_start[wt][chan] = gtchan[chan];
if (error != 0){
printsend("Error at slot, chan = %d %d\n",slot,chan);
return -1;
}
}// vv
}// end loop over channels
printsend("Measured initial GTVALIDS\n");
//find maximum gtvalid time
gmax[wt] = 0.0;
cmax[wt] = 0;
for (chan=0;chan<32;chan++){
if (chan_mask & (0x1<<chan)){
if (gtchan[chan] > gmax[wt]){
gmax[wt] = gtchan[chan];
cmax[wt] = chan;
}
}
}
//find minimum gtvalid time
gmin[wt] = 9999.0;
cmin[wt] = 0;
for (chan=0;chan<32;chan++){
if (chan_mask & (0x1<<chan)){
if (gtchan[chan] < gmin[wt]){
gmin[wt] = gtchan[chan];
cmin[wt] = chan;
}
}
}
//print out results
if (wt == 1){
printsend("GTVALID measure results, time in nsec: \n");
for (chan=0;chan<32;chan++){
printsend("Crate, slot, chan, GTDELAY0/1: %d %d %d %f %f\n",
cn, slot, chan, gt_start[0][chan],gt_start[1][chan]);
}
printsend("TAC0 max-chan, max-gtvalid: %d %f\n",cmax[0],gmax[0]);
printsend("TAC1 max-chan, max-gtvalid: %d %f\n",cmax[1],gmax[1]);
printsend("TAC0 min-chan, min-gtvalid: %d %f\n",cmin[0],gmin[0]);
printsend("TAC1 min-chan, min-gtvalid: %d %f\n",cmin[1],gmin[1]);
// if measuring only, write output to standard paw format file
if (gt_cutoff == 0){
fp = fopen("get_tcmos.dat","a");
for (chan=0;chan<32;chan++){
//fprintf(fp,"%d %d %d %f %f \n",cn,slot,chan,gt_start[0][chan],gt_start[1][chan]);
}
fclose(fp);
}
}
// if gt_cutoff is set, we are going to change the ISETM dacs until all the
// channels are just below it.
if (gt_cutoff != 0){
printsend("Finding ISETM values for crate,slot,TAC: %d %d %d\n",cn,slot,wt);
isetm_new[0] = ISETM;
isetm_new[1] = ISETM;
done = FALSE;
gt_temp = gmax[wt];
while (gt_temp > gt_cutoff){
isetm_new[wt]++;
error = loadsDac(dac_isetm[wt],isetm_new[wt],cn,select_reg);
if (error != 0){
printsend("Error loading Dac's', stopping...\n");
return 1;
}
// get new measurement of gtvalid for this channel
xl3_rw(PED_ENABLE_R + select_reg + WRITE_REG,0x1<<cmax[wt],&temp,cn);
error = get_gtdelay(cn, wt, >_temp,isetm_new[0],isetm_new[1],select_reg);
//printsend("- was %f, %f - \n",gt_temp,gt_cutoff);
}
// check that we still have the max channel
for (chan=0;chan<32;chan++){
if (chan_mask & (0x1<<chan)){
xl3_rw(PED_ENABLE_R + select_reg + WRITE_REG,0x1<<chan,&temp,cn);
error = get_gtdelay(cn,wt,>_temp,isetm_new[0],isetm_new[1],select_reg);
gtchan[chan] = gt_temp;
}
}
// find maximum gtvalid time
gt_max_sec = 0.0;
chan_max_sec = 0;
for (chan=0;chan<32;chan++){
if (chan_mask & (0x1<<chan)){
gt_max_sec = gtchan[chan];
chan_max_sec = chan;
}
}
if (chan_max_sec != cmax[wt]){
printsend("Warning, second chan_max not same as first.\n");
cmax[wt] = chan_max_sec;
gmax[wt] = gt_max_sec;
gt_temp = gmax[wt];
while (gt_temp > gt_cutoff){
isetm_new[wt]++;
error = loadsDac(dac_isetm[wt],isetm_new[wt],cn,select_reg);
xl3_rw(PED_ENABLE_R + select_reg + WRITE_REG,0x1<<cmax[wt],&temp,cn);
error = get_gtdelay(cn,wt,>_temp,isetm_new[0],isetm_new[1],
select_reg);
//printsend("- was %f, %f - \n",gt_temp,gt_cutoff);
}
}
// ok now we think that max channel is at gtcutoff
// now we change twiddle bits to get other channels close too
if (do_twiddle){
for (i=0;i<32;i++)
gtflag[wt][i]=0;
gtflag[wt][chan_max_sec] = 1; // we dont need to change max chan
done = FALSE;
while (!done){
for (chan=0;chan<32;chan++){
if (chan_mask & (0x1<<chan) && gtflag[wt][chan] == 0){
xl3_rw(PED_ENABLE_R + select_reg + WRITE_REG,0x1<<chan,&temp,cn);
error = get_gtdelay(cn,wt,>_temp,isetm_new[0],isetm_new[1],
select_reg);
gtchan_set[wt][chan] = gt_temp;
}
}
done = TRUE;
// successively turn off twiddle bits
for (chan=0;chan<32;chan++){
if (chan_mask & (0x1<<chan)){
if (gtchan_set[wt][chan] <= gt_cutoff && gtflag[wt][chan] == 0
&& tacbits_new[wt][chan] != 0x0){
tacbits_new[wt][chan] -= 0x1; // decrement twiddle by 1
done = FALSE;
printsend("channel %d, %f, tacbits at %01x\n",chan,gtchan_set[wt][chan],tacbits_new[wt][chan]);
}else if (gtchan_set[wt][chan] > gt_cutoff && gtflag[wt][chan] == 0){
tacbits_new[wt][chan] += 0x1; // go up just one
if (tacbits_new[wt][chan] > 0x7)
tacbits_new[wt][chan] = 0x7; //max
gtflag[wt][chan] = 1; // is as close to gt_cutoff as possible
printsend("channel %d ok\n",chan);
}
}
}
// now load the tacbits
for (k=0;k<32;k++) // build twiddle word
tacbits[k] = tacbits_new[1][k]*16 + tacbits_new[0][k];
packet.cmdHeader.packet_type = LOADTACBITS_ID;
*pl = cn;
*(pl+1) = select_reg;
for (j=0;j<32;j++)
*(uint16_t *)(packet.payload+8+j*2) = tacbits[j];
SwapLongBlock(packet.payload,2);
SwapShortBlock(packet.payload+8,32);
do_xl3_cmd(&packet,cn);
printsend("Loaded tacbits\n");
} // end while(!done)
}
//we are done, save setup
isetm_save[wt] = isetm_new[wt];
for (k=0;k<32;k++)
tacbits_save[wt][k] = tacbits_new[wt][k];
// remeasure gtvalid
for (i=0;i<32;i++)
gtchan_set[wt][i] = 9999;
for (chan=0;chan<32;chan++){
if (chan_mask & (0x1<<chan)){
xl3_rw(PED_ENABLE_R + select_reg + WRITE_REG,1<<chan,&temp,cn);
error = get_gtdelay(cn,wt,>_temp,isetm_new[0],isetm_new[1],
select_reg);
gtchan_set[wt][chan] = gt_temp;
}
}
//find maximum
gt_max_set[wt] = 0.0;
chan_max_set[wt] = 0;
for (i=0;i<32;i++){
if (gtchan_set[wt][i] > gt_max_set[wt]){
gt_max_set[wt] = gtchan_set[wt][i];
chan_max_set[wt] = i;
}
}
//find minimum
gt_min_set[wt] = 9999.0;
chan_min_set[wt] = 0;
for (i=0;i<32;i++){
if (gtchan_set[wt][i] < gt_min_set[wt]){
gt_min_set[wt] = gtchan_set[wt][i];
chan_min_set[wt] = i;
}
}
if (wt == 1){ // only do this once, after measuring TAC1
// print out results
if (!do_twiddle){
printsend(">>>ISETA0/1 = 0, no TAC twiddle bits set.\n");
}
printsend( "GTVALID setup results for Crate/Slot %d %d: \n", cn, j);
printsend( "VMAX, TACREF, ISETA = %hu %hu %hu\n",VMAX,TACREF,ISETA);
printsend( "CrCaCh, ISETM0/1, TacBits, GTValid0/1:\n");
for (i=0;i<32;i++){
printsend( "%d %d %d %d %d 0x%hx %f %f",
cn,j,i,isetm_save[0],isetm_save[1],
tacbits_save[1][i]*16 + tacbits_save[0][i],
gtchan_set[0][i],gtchan_set[1][i]);
if (isetm_save[0] == ISETM || isetm_save[1] == ISETM)
printsend( ">>> Warning: isetm not adjusted\n");
else
printsend( "\n");
}
printsend( ">>>Maximum Chan/GTValid TAC0: %d %f \n", chan_max_set[0],gt_max_set[0]);
printsend( ">>>Minimum Chan/GTValid TAC0: %d %f \n", chan_min_set[0],gt_min_set[0]);
printsend( ">>>Maximum Chan/GTValid TAC1: %d %f \n", chan_max_set[1],gt_max_set[1]);
printsend( ">>>Minimum Chan/GTValid TAC1: %d %f \n", chan_min_set[1],gt_min_set[1]);
printsend( "********************************************\n");
}// end if wt==1
}// end if gtvalid != 0
}// end loop over TACS
xl3_rw(PED_ENABLE_R + select_reg + WRITE_REG,0x0,&temp,cn); // reset pedestals
for (i=0;i<32;i++){
printsend( "%d %d %d %d %d 0x%hx %f %f",
cn,j,i,isetm_save[0],isetm_save[1],
tacbits_save[1][i]*16 + tacbits_save[0][i],
gtchan_set[0][i],gtchan_set[1][i]);
if (isetm_save[0] == ISETM || isetm_save[1] == ISETM)
printsend( ">>> Warning: isetm not adjusted\n");
else
printsend( "\n");
}
printsend( ">>>Maximum Chan/GTValid TAC0: %d %f \n", chan_max_set[0],gt_max_set[0]);
printsend( ">>>Minimum Chan/GTValid TAC0: %d %f \n", chan_min_set[0],gt_min_set[0]);
printsend( ">>>Maximum Chan/GTValid TAC1: %d %f \n", chan_max_set[1],gt_max_set[1]);
printsend( ">>>Minimum Chan/GTValid TAC1: %d %f \n", chan_min_set[1],gt_min_set[1]);
printsend( "********************************************\n");
//store in DB
if (update_db){
printsend("updating the database\n");
char hextostr[50];
JsonNode *newdoc = json_mkobject();
json_append_member(newdoc,"type",json_mkstring("cmos_m_gtvalid"));
json_append_member(newdoc,"vmax",json_mknumber((double)VMAX));
json_append_member(newdoc,"TACREF",json_mknumber((double)TACREF));
JsonNode* isetm_new = json_mkarray();
JsonNode* iseta_new = json_mkarray();
json_append_element(isetm_new,json_mknumber((double)isetm_save[0]));
json_append_element(isetm_new,json_mknumber((double)isetm_save[1]));
json_append_element(iseta_new,json_mknumber((double)ISETA));
json_append_element(iseta_new,json_mknumber((double)ISETA));
json_append_member(newdoc,"isetm",isetm_new);
json_append_member(newdoc,"iseta",iseta_new);
JsonNode* tac_shift_new = json_mkarray();
JsonNode* gtchan0 = json_mkarray();
JsonNode* gtchan1 = json_mkarray();
JsonNode* errors = json_mkarray();
for (i=0;i<32;i++){
json_append_element(tac_shift_new,json_mknumber((double)(byte) (tacbits_save[1][i]*16+tacbits_save[0][1])));
json_append_element(gtchan0,json_mknumber((double)(byte) (gtchan_set[0][i])));
json_append_element(gtchan1,json_mknumber((double)(byte) (gtchan_set[1][i])));
json_append_element(errors,json_mknumber((double)0));//FIXME
}
json_append_member(newdoc,"tac_shift",tac_shift_new);
json_append_member(newdoc,"GTValid_tac0",gtchan0);
json_append_member(newdoc,"GTValid_tac1",gtchan1);
json_append_member(newdoc,"errors",errors);
json_append_member(newdoc,"pass",json_mkstring("yes"));//FIXME
if (final_test)
json_append_member(newdoc,"final_test_id",json_mkstring(ft_ids[slot]));
post_debug_doc(cn,slot,newdoc);
json_delete(newdoc); // only delete the head
}
}// vv
}// end loop over slots
deselect_fecs(cn);
return 0;
}
int get_gtdelay(uint16_t crate_num, int wt, float *get_gtchan, uint16_t isetm0, uint16_t isetm1, uint32_t select_reg)
{
printsend(".");
fflush(stdout);
float upper_limit, lower_limit, current_delay;
int error, done, i;
uint32_t temp, num_read;
XL3_Packet packet;
FECCommand *command;
done = 0;
upper_limit = GTMAX;
lower_limit = 250;
// set unmeasured TAC GTVALID to long window time
if (wt == 0)
error = loadsDac(131,(u_short)125,crate_num,select_reg);
else
error = loadsDac(132,(u_short)125,crate_num,select_reg);
// find the time that the tac stops firing
while (done == 0){
//reset fifo
xl3_rw(GENERAL_CSR_R + select_reg + WRITE_REG,0x0 | (crate_num << FEC_CSR_CRATE_OFFSET) | 0x2,&temp,crate_num);
xl3_rw(GENERAL_CSR_R + select_reg + WRITE_REG,0x0 | (crate_num << FEC_CSR_CRATE_OFFSET),&temp,crate_num);
//binary search for GT delay
current_delay = (upper_limit - lower_limit)/2+lower_limit;
current_delay = set_gt_delay(current_delay+GTPED_DELAY+TDELAY_EXTRA)-GTPED_DELAY-TDELAY_EXTRA;
//for (i=0;i<NGTVALID;i++){
//send_softgt();
//}
multi_softgt(NGTVALID);
//mtc_multi_write(0xC,0x0,NGTVALID);
//packet.cmdHeader.packet_type = CMOSGTVALID_ID;
//*(uint32_t *) packet.payload = select_reg;
//SwapLongBlock(packet.payload,1);
//do_xl3_cmd(&packet,crate_num);
//SwapLongBlock(packet.payload,2);
//error = *(uint32_t *) packet.payload;
//num_read = *(uint32_t *) (packet.payload+4);
xl3_rw(FIFO_WRITE_PTR_R + select_reg + READ_REG,0x0,&temp,crate_num);
//packet.cmdHeader.packet_type = CMOSGTVALID_ID;
//command = (FECCommand *) packet.payload;
//command->flags = 0x0;
//command->cmd_num = 0x0;
//command->packet_num = 0x0;
//command->address = FIFO_WRITE_PTR_R + select_reg + READ_REG;
//command->data = 0x0;
//SwapLongBlock(&(command->data),1);
//SwapLongBlock(&(command->address),1);
//do_xl3_cmd(&packet, crate_num);
//temp = command->data;
//SwapLongBlock(&temp,1);
//*(uint32_t *)
num_read = (temp & 0x000FFFFF)/3UL;
//if (error != 0)
// return -1;
//printsend("delay %f, num %d\n",current_delay,num_read);
// now check to see if we saw the right number of events
if (num_read < (NGTVALID)*0.75)
upper_limit = current_delay;
else
lower_limit = current_delay;
if (upper_limit-lower_limit <= 1)
done = 1;
}
if (upper_limit == GTMAX){
*get_gtchan = 999;
}else{
// ok we know that lower limit is within the window, upper limit is outside
// lets make sure its the right TAC failing by making the window longer
// and seeing if the events show back up
if (wt == 0)
error = loadsDac(132,(u_short)125,crate_num,select_reg);
else
error = loadsDac(131,(u_short)125,crate_num,select_reg);
//reset fifo
xl3_rw(GENERAL_CSR_R + select_reg + WRITE_REG,0x0 | (crate_num << FEC_CSR_CRATE_OFFSET) | 0x2,&temp,crate_num);
xl3_rw(GENERAL_CSR_R + select_reg + WRITE_REG,0x0 | (crate_num << FEC_CSR_CRATE_OFFSET),&temp,crate_num);
current_delay = set_gt_delay(upper_limit+GTPED_DELAY+TDELAY_EXTRA)-GTPED_DELAY-TDELAY_EXTRA;
multi_softgt(NGTVALID);
//for (i=0;i<NGTVALID;i++){
// send_softgt();
//}
//packet.cmdHeader.packet_type = CMOSGTVALID_ID;
//*(uint32_t *) packet.payload = select_reg;
//SwapLongBlock(packet.payload,1);
//do_xl3_cmd(&packet,crate_num);
//SwapLongBlock(packet.payload,2);
//error = *(uint32_t *) packet.payload;
//num_read = *(uint32_t *) (packet.payload+4);
xl3_rw(FIFO_WRITE_PTR_R + select_reg + READ_REG,0x0,&temp,crate_num);
num_read = (temp & 0x000FFFFF)/3UL;
//printsend("check, num %d\n",num_read);
//if (error != 0)
// return -1;
// now check to see if we saw the right number of events
if (num_read < (NGTVALID)*0.75){
printsend("Uh oh, still not all the events\n");
//return -1;
}
*get_gtchan = upper_limit;
}
//set TACS back to original time
error = loadsDac(131,(u_short) isetm1, crate_num, select_reg);
error = loadsDac(132,(u_short) isetm0, crate_num, select_reg);
return 0;
}
static int setup_crate(int cn, uint16_t slot_mask)
{
int i;
uint32_t select_reg, result,temp;
printsend("Resetting fifo and pedestals.\n");
for (i=0;i<16;i++){
if (((0x1<<i)&slot_mask) != 0x0){
select_reg = FEC_SEL * i;
// disable pedestals
xl3_rw(PED_ENABLE_R + select_reg + WRITE_REG,0x0,&result,cn);
// reset fifo
xl3_rw(CMOS_CHIP_DISABLE_R + select_reg + WRITE_REG,0xFFFFFFFF,&result,cn);
xl3_rw(GENERAL_CSR_R + select_reg + READ_REG,0x0,&temp,cn);
// mask in crate_address and fifo reset
xl3_rw(GENERAL_CSR_R + select_reg + WRITE_REG,temp | (cn << FEC_CSR_CRATE_OFFSET) | 0x6,&result,cn);
xl3_rw(GENERAL_CSR_R + select_reg + WRITE_REG,0x0 | (cn << FEC_CSR_CRATE_OFFSET),&result,cn);
xl3_rw(CMOS_CHIP_DISABLE_R + select_reg + WRITE_REG,0x0,&result,cn);
}
}
deselect_fecs(cn);
return 0;
}