11 #include <rtsSystems.h>
13 #include <DAQ1000/ddl_struct.h>
14 #include <DAQ1000/rdo_cmds.h>
16 #include <TPC/rowlen.h>
17 #include <TPX/tpx_altro_to_pad.h>
19 #include <DAQ_TPX/tpxCore.h>
20 #include <DAQ_TPX/tpxPed.h>
21 #include <DAQ_TPX/tpxGain.h>
23 #include <DAQ_ITPC/itpcPed.h>
27 #include <MISC_LIBS/thread_dbg.h>
39 static double mark(
void)
41 struct timeval tmval ;
43 gettimeofday(&tmval,0) ;
45 return ((
double)tmval.tv_sec*1000000.0 + (
double)tmval.tv_usec) ;
48 static double delta(
double v)
58 int tpx23::from22to23(
char *c_addr,
int words)
64 inline u_int tpx23::set_rdo(
int s,
int r)
83 get_token((
char *)d_start,words) ;
87 if(run_type==1 || run_type==5) {
88 int r0_logical = tpx36_from_real(subdet_id,sector1,rdo1) - 1 ;
91 peds->valid_evts[r0_logical]++ ;
92 peds->evts[r0_logical]++ ;
94 peds->sector = subdet_id ;
96 if(peds->evts[r0_logical]<=3)
goto done ;
97 if(peds->valid_evts[r0_logical]>1000)
goto done ;
108 if(log_level>0) LOG(TERR,
"%d: fee_scan",rdo1) ;
110 u_int *h_to_continue ;
117 while(h>(d_start+2)) {
130 int wc = ((hi&0x3F)<<4)|((lo&0xF0000)>>16) ;
133 int id = (lo&0xFF0) >> 4 ;
138 u_int a = (lo>>12)&0xF ;
141 if((aaa!= 0x2AAA)||(a!=0xA)||(wc>437)) {
144 if((online || mode) && retry==0) {
145 LOG(ERR,
"S%02d:%d: aid %d:%d, %d:%d: aaa 0x%X, a 0x%X, wc %d, %d",
146 sector1,rdo1,
id,ch,id_pre,ch_pre,aaa,a,wc,d_end-h) ;
168 u_int aa = ((h[1]&0xFFFFF)>>10) ;
175 if((online || mode) && retry==0) {
176 LOG(ERR,
"S%02d:%d: aid %d:%d, %d:%d: aaa 0x%X, a 0x%X, aa 0x%X, wc %d",
177 sector1,rdo1,
id,ch,id_pre,ch_pre,aaa,a,aa,wc) ;
200 LOG(WARN,
"S%02d:%d: aid %d:%d, %d:%d: aaa 0x%X, a 0x%X, wc %d, %d -- OK",
201 sector1,rdo1,
id,ch,id_pre,ch_pre,aaa,a,wc,d_end-h) ;
210 for(
int i=0;i<tpx_fee_override_cou;i++) {
211 if(sector1 == tpx_fee_override[i].sector) {
212 if(rdo1==tpx_fee_override[i].rdo) {
213 int fee =
id & 0xFE ;
215 if(fee == tpx_fee_override[i].curr_altro) {
216 int should = tpx_fee_override[i].orig_altro ;
218 if(
id & 1) should |= 1 ;
234 int flags = flags_row_pad(
id,ch,row,pad) ;
241 if(online) LOG(ERR,
"S%02d:%d: rp %d:%d (aid %d:%d, %d:%d) : wc %d",sector1,rdo1,row,pad,
242 id,ch,id_pre,ch_pre, wc) ;
256 if(flags && run_type==3) {
258 LOG(TERR,
"%d: rp %d:%d, flags 0x%X",rdo1,row,pad,flags) ;
266 altro_present[id][ch]++ ;
268 if(altro_present[
id][ch]>1) {
271 if(online) LOG(ERR,
"S%02:%d: AID %d:%d already present %d",sector1,rdo1,
id,ch,altro_present[
id][ch]) ;
278 u_short *d = s1_dta + last_ix ;
282 LOG(TERR,
"%d: rp %d:%d; last_ix %d %p",rdo1,row,pad,last_ix,d) ;
302 dd[0] = (hi>>10)&0x3FF ;
304 dd[2] = (lo>>10)&0x3FF ;
323 d[ix++] = (hi>>10)&0x3FF ;
324 d[ix++] = hi & 0x3FF ;
325 d[ix++] = (lo>>10)&0x3FF ;
326 d[ix++] = lo & 0x3FF ;
335 struct seq_t *seq = s1[row][pad].seq ;
354 u_short t_len = *dd++ - 2 ;
355 u_short t_hi = *dd++ ;
357 if(seq_ix>=(SEQ_MAX-1)) {
362 t_lo = t_hi - t_len + 1 ;
364 if(t_len>440 || t_hi>440 || t_lo>440) {
367 if(online||mode) LOG(ERR,
"S%02d:%d: rp %d:%d (aid %d:%d, %d:%d), t_len %d, t_lo %d, t_hi %d; wc %d, ix %d, seq %d, %d",
368 sector1,rdo1,row,pad,
370 t_len,t_lo,t_hi,wc,ix,seq_ix,d_end-h) ;
372 if(t_len>440 || t_hi>440 || t_lo>440) {
389 sseq[seq_ix].t_lo = t_lo ;
390 sseq[seq_ix].t_hi = t_hi ;
394 for(
int i=0;i<t_len;i++) {
396 sseq[seq_ix].d[i] = adc ;
407 if(run_type==1 || run_type==5) {
410 memset(&a,0,
sizeof(a)) ;
411 a.what = TPX_ALTRO_DO_ADC ;
423 for(
int i=(seq_ix-1);i>=0;i--) {
424 int t_len = sseq[i].t_hi - sseq[i].t_lo + 1 ;
427 for(
int j=(t_len-1);j>=0;j--) {
428 int adc = sseq[i].d[j] ;
430 a.tb[aix] = sseq[i].t_lo + ii ;
440 altro[altro_cou].row = row ;
441 altro[altro_cou].pad = pad ;
442 altro[altro_cou].ch = ch ;
443 altro[altro_cou].id = id ;
444 altro[altro_cou].count = 0 ;
448 for(
int i=(seq_ix-1);i>=0;i--) {
449 int t_len = sseq[i].t_hi - sseq[i].t_lo + 1 ;
452 for(
int j=(t_len-1);j>=0;j--) {
453 int adc = sseq[i].d[j] ;
454 altro[altro_cou].adc[aix] = adc ;
455 altro[altro_cou].tb[aix] = sseq[i].t_lo + ii ;
456 altro[altro_cou].count++ ;
465 tpx_d->sector = sector1 ;
471 LOG(NOTE,
"%d:%d %d:%d %d:%d",sector1,rdo1,row,pad,
id,ch) ;
473 tpx_d->ch_start(ch) ;
475 for(
int i=(seq_ix-1);i>=0;i--) {
476 int t_len = sseq[i].t_hi - sseq[i].t_lo + 1 ;
479 for(
int j=(t_len-1);j>=0;j--) {
480 int adc = sseq[i].d[j] ;
486 tb = sseq[i].t_lo + ii ;
491 tpx_d->accum(tb,adc) ;
507 seq = s1[row][pad].seq ;
514 for(
int i=(seq_ix-1);i>=0;i--) {
515 seq[s_cou].t_lo = sseq[i].t_lo;
516 seq[s_cou].t_hi = sseq[i].t_hi ;
517 seq[s_cou].dta_p = (dd-d) ;
518 seq[s_cou].blob_id = 0 ;
520 int t_len = sseq[i].t_hi - sseq[i].t_lo + 1 ;
524 for(
int j=(t_len-1);j>=0;j--) {
525 *dd++ = sseq[i].d[j] ;
537 sequence_cou += s_cou ;
539 seq[s_cou].t_hi = -1 ;
540 s1[row][pad].ix = last_ix ;
546 seq = s1[row][pad].seq ;
547 while(seq->t_hi>=0) {
548 int t_len = seq->t_hi - seq->t_lo + 1 ;
549 printf(
"rp %d:%d, t_lo %d, t_hi %d\n",row,pad,seq->t_lo,seq->t_hi) ;
551 dd = d + seq->dta_p ;
552 for(
int j=0;j<t_len;j++) {
553 printf(
" adc %d: %d\n",j,dd[j]) ;
585 u_int tpx23::get_token_s(
char *c_addr,
int wds)
587 u_int *d = (u_int *)c_addr ;
598 int type = (d[0]>>0)&0xF ;
599 int hdr_type = (d[0]>>24)&0xF ;
630 daq = (d[-2] >> 12)&0xF ;
631 trg = (d[-2] >> 16)&0xF ;
645 d -= trg_cou * (
sizeof(
struct trg_data)/4) ;
652 for(
int i=0;i<trg_cou;i++) {
655 switch(trg_d[i].csr & 0xFF000000) {
661 t = trg_d[i].data & 0xFFF ;
662 daq = (trg_d[i].data >> 12)&0xF ;
663 trg = (trg_d[i].data >> 16)&0xF ;
674 return (trg<<16)|(daq<<12)|t ;
677 u_int tpx23::get_token(
char *c_addr,
int wds)
679 u_int *d = (u_int *)c_addr ;
705 tdbg[0] = d_first[1] ;
714 token = d[-2] & 0xFFF ;
715 daq_cmd = (d[-2] >> 12)&0xF ;
716 trg_cmd = (d[-2] >> 16)&0xF ;
719 u_int evt_err = d[-1] ;
720 if(evt_err & 0xFF000000) {
723 if(wds>20) cou = 20 ;
729 LOG(ERR,
"evt_err %d:%d: 0x%08X: 0x%08X, wds %u",evt,rdo1,d_first[0],evt_err,wds) ;
730 for(
int i=0;i<cou;i++) {
731 LOG(TERR,
" %d: 0x%08X",i,d_first[i]) ;
738 printf(
" delta evt %d: all %d: %d %d %d %d - token %d, wds %d\n",evt,tdbg[1]-tdbg[0],
749 return (trg_cmd<<16)|(daq_cmd<<12)|token ;
759 d -= trg_cou * (
sizeof(
struct trg_data)/4) ;
764 for(
int i=0;i<trg_cou;i++) {
770 switch(trg[i].csr & 0xFF000000) {
776 token = trg[i].data & 0xFFF ;
777 daq_cmd = (trg[i].data >> 12)&0xF ;
778 trg_cmd = (trg[i].data >> 16)&0xF ;
791 return (trg_cmd<<16)|(daq_cmd<<12)|token ;
798 int tpx23::msc_dump(
char *c_addr,
int wds)
802 LOG(INFO,
"msc_dump: %d bytes, should be %d: Sreal%d:%d",wds*4,
sizeof(
struct tpx_rdo),
810 memcpy(&(
tpx_rdo[sector1-1][rdo1-1]),c_addr,
sizeof(
struct tpx_rdo)) ;
813 err = tpx_show_status(sector1,1<<(rdo1-1),0) ;
815 if(online) LOG(ERR,
"S%02d:%d: tpx_show_status %d",sector1,rdo1,err) ;
821 int tpx23::log_dump(
char *c_addr,
int wds)
831 u_int err_status = 0 ;
849 int max_cou = (words-4)*4 ;
850 rdobuff = (
char *)(c_addr+2*4) ;
853 len = strlen(rdobuff) ;
854 if(len>max_cou) len = max_cou ;
858 tmpbuff = (
char *) malloc(len+1) ;
859 memcpy(tmpbuff,rdobuff,len+1) ;
862 for(
int i=0;i<len;i++) {
863 if(tmpbuff[i]==
'\n') {
871 for(
int i=0;i<(len);) {
879 for(
int j=st;j<len;j++) {
880 if(tmpbuff[j] == 0) break ;
882 if(!isprint(tmpbuff[j])) {
883 if(tmpbuff[j] == 9) ;
885 LOG(WARN,
"---> [%d LOG] Unprintable character 0x%02X? -- powercycle",rdo,(u_char)tmpbuff[j]) ;
896 if(strstr(tmpbuff+st,
"RHIC clock: ")) {
897 if(strstr(tmpbuff+st,
"EXTERNAL")) {
905 if(strstr(tmpbuff+st,
"JTAG dev ")) {
913 ret = sscanf(tmpbuff+st,
"JTAG dev %d: ID 0x%X, USERcode 0x%X",&dev,&dev_id,&user) ;
914 LOG(WARN,
"JTAG: ret %d, dev %d, dev_id 0x%08X, user 0x%08X",ret, dev, dev_id,user) ;
916 if(ret==3 && dev>=0 && dev<5) {
917 fpga_usercode[dev] = user ;
922 if(strstr(tmpbuff+st,
"SPECIAL_0 code")) {
923 LOG(ERR,
"---> SPECIAL code: RDO %d",rdo) ;
927 if(strstr(tmpbuff+st,
"ERR")) {
928 if(strstr(tmpbuff+st,
"FLASH Id")) {
929 LOG(WARN,
"[S%02d:%d LOG]: contains ERR \"%s\"",s_real,r_real,tmpbuff+st) ;
938 if(strstr(tmpbuff+st,
"Status: Xil config")) {
939 if(strstr(tmpbuff+st,
"!")) {
941 LOG(WARN,
"---> [S%d:%d LOG] CPLD status has \"!\" -- ignored ",s_real,r_real) ;
945 if(strstr(tmpbuff+st,
"FEE power BAD")) {
953 if(strstr(tmpbuff+st,
"SRAM check failed")) {
956 LOG(ERR,
"---> [%d LOG] SRAM check failed -- powercycle",rdo) ;
959 if(strstr(tmpbuff+st,
"CPLD claims error")) {
961 LOG(ERR,
"---> [%d LOG] CPLD claims error -- reconfig 0x300",rdo) ;
964 if(strstr(tmpbuff+st,
"can't configure RDO!")) {
965 LOG(ERR,
"---> [%d LOG] \"can't configure RDO\" -- reconfig 0x300",rdo) ;
971 if(strstr(tmpbuff+st,
"lost RHIC")) {
972 LOG(ERR,
"---> [%d LOG] \"lost RHIC\" -- restart run",rdo) ;
975 if(strstr(tmpbuff+st,
"NO RHIC CLOCK")) {
976 LOG(ERR,
"---> [%d LOG] \"NO RHIC CLOCK\" -- restart run",rdo) ;
980 if(strstr(tmpbuff+st,
"DRIFT")) {
981 LOG(ERR,
"---> [%d LOG] \"DRIFT/clock problems\" -- restart run",rdo) ;
986 if(strstr(tmpbuff+st,
"CRIT")) {
988 LOG(ERR,
"---> [%d LOG] CRIT string in log -- restart run",rdo) ;
991 if(strstr(tmpbuff+st,
"altro error")) {
994 LOG(ERR,
"---> [%d LOG] altro error -- restart run",rdo) ;
998 if(strstr(tmpbuff+st,
"ERR ALTRO")) {
1003 if(strstr(tmpbuff+st,
"ERR: expired")) {
1009 LOG(ERR,
"[S%02d:%d %d]: %s",s_real,r_real,evt,tmpbuff+st) ;
1013 LOG(INFO,
"[S%02d:%d %d]: %s",s_real,r_real,evt,tmpbuff+st) ;
1032 tm[strlen(tm)-1] = 0 ;
1055 pthread_mutex_lock(&log_file_mutex) ;
1058 fprintf(log_file,
"%s[RDO %d] (%s) %s%s%s",ANSI_BLUE,rdo,
1061 term_char,ANSI_RESET) ;
1064 fprintf(log_file,
"%s",rdobuff) ;
1069 pthread_mutex_unlock(&log_file_mutex) ;
1077 int tpx23::rdo_scan(
char *c_addr,
int wds)
1081 u_int *d = (u_int *)c_addr ;
1085 int rdo = (d[0]>>8)&0xF ;
1086 int sec = (d[0]>>12)&0xFF ;
1088 subtype = (d[0]>>4)&0xF ;
1089 type = (d[0]>>0)&0xF ;
1091 hdr_version = (d[0]>>24)&0xF ;
1105 if((d[0]&0xF0000000)!=0xF0000000) {
1106 LOG(ERR,
"%d:%d: bad header 0x%08X",evt,rdo1,d[0]) ;
1109 if(rdo!=rdo1 || sec!=sector1) {
1110 LOG(ERR,
"%d:%d: wrong sec,rdo: rdo expect %d is %d; sector expect %d is %d [0x%08X]",evt,rdo1,
1111 rdo1,rdo,sector1,sec,d[0]) ;
1121 ret = log_dump(c_addr,wds) ;
1129 LOG(WARN,
"%d: MSC: event %d: S%02d:%d: type %d:%d, words %d",rdo1,d[1],sec,rdo,type,subtype,words) ;
1131 ret = msc_dump(c_addr, wds) ;
1154 if(rp_gain_tpx==0) {
1155 rp_gain_tpx = (row_pad_t (*)[ROW_MAX+1][PAD_MAX+1]) malloc(
sizeof(row_pad_t)*24*(ROW_MAX+1)*(PAD_MAX+1)) ;
1158 for(
int s=0;s<24;s++) {
1159 for(
int r=0;r<=ROW_MAX;r++) {
1160 for(
int p=0;p<=PAD_MAX;p++) {
1161 rp_gain_tpx[s][r][p].gain = 1.0 ;
1162 rp_gain_tpx[s][r][p].t0 = 0.0 ;
1163 rp_gain_tpx[s][r][p].flags = 0 ;
1168 rp_gain = rp_gain_tpx ;
1171 peds =
new class tpxPed ;
1176 for(
int row=1;row<=45;row++) rowlen[row] = tpc_rowlen[row] ;
1180 memset(fpga_usercode,0,
sizeof(fpga_usercode)) ;
1188 #include <DAQ_TPX/tpxCore.h>
1190 u_char tpx23::flags_row_pad(
int asic,
int channel,
int &row,
int &pad)
1195 if(rdo1<1||rdo1>6)
return 0xFF ;
1199 tpx_from_altro(rdo1-1,asic,channel,row,pad) ;
1200 if(row==0 || row==255)
return 0xFF ;
1201 if(pad==0 || pad==255)
return 0xFF ;
1205 return rp_gain[sector1-1][row][pad].flags ;
1209 int tpx23::run_start()