-
Notifications
You must be signed in to change notification settings - Fork 3
/
ttot.c
391 lines (361 loc) · 14.4 KB
/
ttot.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#include "include/xl_regs.h"
#include "include/dacNumber.h"
#include "penn_daq.h"
#include "fec_util.h"
#include "mtc_util.h"
#include "net_util.h"
//#include "pouch.h"
//#include "json.h"
#include "ttot.h"
int get_ttot(char * buffer)
{
if (sbc_is_connected == 0){
printsend("SBC not connected.\n");
return -1;
}
int errors;
int targettime = 400;
int crate = 2;
uint32_t slot_mask = 0x2000;
uint16_t times[32*16];
int tot_errors[16][32];
int result;
int i,j,slot,passflag;
int update_db = 0;
int final_test = 0;
char ft_ids[16][50];
char *words,*words2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c'){
words2 = strtok(NULL, " ");
crate = atoi(words2);
}else if (words[1] == 's'){
words2 = strtok(NULL, " ");
slot_mask = strtoul(words2,(char**)NULL,16);
}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] == 't'){
words2 = strtok(NULL, " ");
targettime = atoi(words2);
}else if (words[1] == 'h'){
printsend("Usage: get_ttot -c"
" [crate_num] -s [slot_mask (hex)] -t [target time] -d (update debug db)\n");
return 0;
}
}
words = strtok(NULL, " ");
}
unset_gt_mask(MASKALL);
errors = setup_pedestals(0.0,60,100,0); //freq=0,width=60,delay=100+0
unset_gt_crate_mask(MASKALL);
set_gt_crate_mask(MSK_CRATE21); //turn on the TUB
set_ped_crate_mask(MSK_CRATE21); //turn on the TUB
unset_ped_crate_mask(MASKALL);
set_ped_crate_mask(0x1<<crate | MSK_CRATE21); // leave TUB masked in
result = disc_m_ttot(crate,slot_mask,150,times);
printsend("crate\t slot\t channel\t time\n");
for (i=0;i<16;i++){
if ((0x1<<i) & slot_mask){
for(j=0;j<32;j++){
tot_errors[i][j] = 0;
printsend("%d\t %d\t %d\t %d",crate,i,j,times[i*32+j]);
if (targettime > times[i*32+j]){
printsend(">>>Warning: time less than %d nsec",targettime);
tot_errors[i][j] = 1;
}
printsend("\n");
}
}
}
if (update_db){
printsend("updating the database\n");
;
for (slot=0;slot<16;slot++){
if ((0x1<<slot) & slot_mask){
JsonNode *newdoc = json_mkobject();
json_append_member(newdoc,"type",json_mkstring("get_ttot"));
json_append_member(newdoc,"targettime",json_mknumber((double)targettime));
JsonNode *times_node = json_mkarray();
JsonNode *error_node = json_mkarray();
passflag = 0;
for (i=0;i<32;i++){
if (tot_errors[slot][i] == 1)
passflag = 1;
json_append_element(error_node,json_mknumber((double)tot_errors[slot][i]));
json_append_element(times_node,json_mknumber((double)times[slot*32+i]));
}
json_append_member(newdoc,"times",times_node);
json_append_member(newdoc,"errors",error_node);
if (passflag == 0){
json_append_member(newdoc,"pass",json_mkstring("yes"));
}else{
json_append_member(newdoc,"pass",json_mkstring("no"));
}
if (final_test)
json_append_member(newdoc,"final_test_id",json_mkstring(ft_ids[slot]));
post_debug_doc(crate,slot,newdoc);
json_delete(newdoc); // delete the head ndoe
}
}
}
return 0;
}
int set_ttot(char * buffer)
{
printsend(".%s.\n",buffer);
if (sbc_is_connected == 0){
printsend("SBC not connected.\n");
return -1;
}
int errors;
int targettime = 400;
int crate = 2;
uint32_t slot_mask = 0x2000;
int i,j;
int slot, passflag;
int update_db = 0;
int final_test = 0;
char ft_ids[16][50];
char *words,*words2;
words = strtok(buffer, " ");
while (words != NULL){
if (words[0] == '-'){
if (words[1] == 'c'){
words2 = strtok(NULL, " ");
crate = atoi(words2);
}else if (words[1] == 's'){
words2 = strtok(NULL, " ");
slot_mask = strtoul(words2,(char**)NULL,16);
}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] == 't'){
words2 = strtok(NULL, " ");
targettime = atoi(words2);
}else if (words[1] == 'h'){
printsend("Usage: set_ttot -c"
" [crate_num] -s [slot_mask (hex)] -t [target time] -d (update debug db)\n");
return 0;
}
}
words = strtok(NULL, " ");
}
unset_gt_mask(MASKALL);
errors = setup_pedestals(0.0,60,100,0); //freq=0,width=60,delay=100+0
unset_gt_crate_mask(MASKALL);
set_gt_crate_mask(MSK_CRATE21); //turn on the TUB
set_ped_crate_mask(MSK_CRATE21); //turn on the TUB
unset_ped_crate_mask(MASKALL);
set_ped_crate_mask(0x1<<crate | MSK_CRATE21); // leave TUB masked in
uint16_t allrmps[16*8],allvsis[16*8],alltimes[16*32];
int tot_errors[16*8];
disc_s_ttot(crate,slot_mask,targettime,allrmps,allvsis,alltimes,tot_errors);
if (update_db){
printsend("updating the database\n");
;
for (slot=0;slot<16;slot++){
if ((0x1<<slot) & slot_mask){
JsonNode *newdoc = json_mkobject();
json_append_member(newdoc,"type",json_mkstring("set_ttot"));
json_append_member(newdoc,"targettime",json_mknumber((double)targettime));
JsonNode *rmp = json_mkarray();
JsonNode *vsi = json_mkarray();
JsonNode *times = json_mkarray();
JsonNode *error_node = json_mkarray();
passflag = 0;
for (i=0;i<8;i++){
if (tot_errors[slot*8+i] == 1)
passflag = 1;
json_append_element(rmp,json_mknumber((double)allrmps[slot*8+i]));
json_append_element(vsi,json_mknumber((double)allvsis[slot*8+i]));
json_append_element(error_node,json_mknumber((double)tot_errors[slot*8+i]));
JsonNode *times_temp = json_mkarray();
for (j=0;j<4;j++){
json_append_element(times_temp,json_mknumber((double)alltimes[slot*32+i*4+j]));
}
json_append_element(times,times_temp);
}
json_append_member(newdoc,"rmp",rmp);
json_append_member(newdoc,"vsi",vsi);
json_append_member(newdoc,"times",times);
json_append_member(newdoc,"errors",error_node);
if (passflag == 0){
json_append_member(newdoc,"pass",json_mkstring("yes"));
}else{
json_append_member(newdoc,"pass",json_mkstring("no"));
}
if (final_test)
json_append_member(newdoc,"final_test_id",json_mkstring(ft_ids[slot]));
post_debug_doc(crate,slot,newdoc);
json_delete(newdoc); // head node needs deleting
}
}
}
return 0;
}
int disc_s_ttot(int crate, uint32_t slot_mask, int goal_time, uint16_t *allrmps,uint16_t *allvsis, uint16_t *times, int *errors)
{
int i,j,k,l;
uint16_t rmp[8];
uint16_t vsi[8];
uint16_t rmpup[8],vli[8];
uint16_t chips_not_finished;
int32_t diff[32];
uint32_t theDACs[50];
uint32_t theDAC_Values[50];
uint32_t select_reg;
int num_dacs;
int update_db = 0;
int result;
for (i=0;i<16;i++){
if ((0x1<<i) & slot_mask){
// set default values
for (j=0;j<8;j++){
rmpup[j] = RMPUPDEFAULT;
rmp[j] = RMPDEFAULT-10;
vsi[j] = VSIDEFAULT;
vli[j] = VLIDEFAULT;
}
// load default values
num_dacs = 0;
select_reg = FEC_SEL*i;
for (j=0;j<8;j++){
errors[i*8+j] = 0;
theDACs[j*4+0] = d_rmpup[j];
theDAC_Values[j*4+0] = rmpup[j];
theDACs[j*4+1] = d_rmp[j];
theDAC_Values[j*4+1] = rmp[j];
theDACs[j*4+2] = d_vsi[j];
theDAC_Values[j*4+2] = vsi[j];
theDACs[j*4+3] = d_vli[j];
theDAC_Values[j*4+3] = vli[j];
num_dacs += 4;
}
multiloadsDac(num_dacs,theDACs,theDAC_Values,crate,FEC_SEL*i);
printsend("Working on crate/board %d %d, target time %d\n",crate,i,goal_time);
chips_not_finished = 0xFF; // none finished
while (chips_not_finished){
// measure timing
result = disc_m_ttot(crate,0x1<<i,150,times);
for (j=0;j<8;j++){ // loop over disc chips
for (k=0;k<4;k++){ // loop over channels in chip
diff[4*j+k] = times[i*32+j*4+k] - goal_time;
} // end loop over channels in chip
if ((diff[4*j+0] > 0) && (diff[4*j+1] > 0) && (diff[4*j+2] > 0) && (diff[4*j+3] > 0)
&& (chips_not_finished & (0x1<<j))){ // if diffs all positive
chips_not_finished &= ~(0x1<<j); // that chip is finished
printsend("Fit Ch(%d) (RMP/VSI %d %d) Times:\t%d\t%d\t%d\t%d\n",j,rmp[j],vsi[j],times[i*32+j*4+0],times[i*32+j*4+1],times[i*32+j*4+2],times[i*32+j*4+3]);
allrmps[i*8+j] = rmp[j];
allvsis[i*8+j] = vsi[j];
}else{ // if not done, adjust DACs
if ((rmp[j] <= MAX_RMP_VALUE) && (vsi[j] > MIN_VSI_VALUE) && (chips_not_finished & (0x1<<j))){
rmp[j]++;
}
if ((rmp[j] > MAX_RMP_VALUE) && (vsi[j] > MIN_VSI_VALUE) && (chips_not_finished & (0x1<<j))){
rmp[j] = RMPDEFAULT-10;
vsi[j] -=2;
}
if ((vsi[j] <= MIN_VSI_VALUE) && (chips_not_finished & (0x1<<j))){
// out of bounds, end loop with error
printsend("RMP/VSI is too big for disc chip %d! (%d %d)\n",j,rmp[j],vsi[j]);
printsend("Aborting slot %d setup.\n",i);
errors[i*8+j] = 1;
for (l=0;l<8;l++){
if (chips_not_finished & (0x1<<l)){
printsend("Slot %d Chip %d\tRMP/VSI: %d %d <- unfinished\n",i,j,rmp[l],vsi[l]);
}
}
goto end; // oh the horror!
}
}
} // end loop over disc chips
// load new values
num_dacs = 0;
select_reg = FEC_SEL*i;
for (j=0;j<8;j++){
theDACs[j*2+0] = d_rmp[j];
theDAC_Values[j*2+0] = rmp[j];
theDACs[j*2+1] = d_vsi[j];
theDAC_Values[j*2+1] = vsi[j];
num_dacs += 2;
}
multiloadsDac(num_dacs,theDACs,theDAC_Values,crate,select_reg);
} // end while (chips not finished)
end:
continue;
if (update_db){
}
} // end if slot mask
} // end loop over slots
return 0;
}
int disc_m_ttot(int crate, uint32_t slot_mask, int start_time, uint16_t *disc_times)
{
int i,j,k;
int result;
int increment = 1;
int time;
uint32_t init[32],fin[32];
uint32_t chan_done_mask;
float real_delay;
for (i=0;i<16;i++){
if ((0x1<<i) & slot_mask){
result = set_crate_pedestals(crate,0x1<<i,0xFFFFFFFF);
chan_done_mask = 0x0;
for (time = start_time;time<=MAXTIME;time+=increment){
// setup gt delay
real_delay = set_gt_delay((float) time);
result = get_cmos_total_count2(crate,i,init);
for (j=0;j<32;j++){ // loop over channels
//initial conditions
//result = get_cmos_total_count(crate,i,j,&init[j]);
}
// send 20 soft gts
mtc_multi_write(0xC,0x0,NUM_PEDS);
// now read out the chips
result = get_cmos_total_count2(crate,i,fin);
for (j=0;j<32;j++){
//result = get_cmos_total_count(crate,i,j,&fin[j]);
fin[j] -= init[j];
// check if we got all the pedestals from the TUB too
if ((fin[j] >= 2*NUM_PEDS) & !(chan_done_mask & (0x1<<j))){
chan_done_mask |= 0x1<<j;
disc_times[i*32+j] = time+TUB_DELAY;
}
} // end loop over channels
if (chan_done_mask == 0xFFFFFFFF)
break; // no need to keep incrementing time
if (time == MAXTIME){
for(j=0;j<32;j++){
if ((0x1<<j) & !chan_done_mask){
disc_times[i*32+j] = time+TUB_DELAY;
}
}
}
}// for time<=MAXTIME
} // end if slot mask
} // end loop over slots
return 0;
}