#include #include #include #include //-------------- my vme ---------------------- #include "shmem_roc.h" #include "rc_vme.h" //-------------------------------------------- #define FADC_ADDR (3<<19) #include "jvme.h" #include "fadcLib.h" #include "tiLib.h" #include "fa125Lib.h" #include "f1tdcLib.h" #include "ctpLib.h" #include "sdLib.h" //#include "tdLib.h" #include "tsLib.h" //#define USE_vmeDSC 1 #ifdef USE_vmeDSC #include #else #include "dsc2Lib.h" #endif extern int Debug ; //-- from shmem -- extern int sig_hup; void printlog(const char* msg, int var); //-------------------------------------------- // stdout logger //-------------------------------------------- char *printf_buffer; static char *printf_log(int size) { int shmid = shmget(IPC_PRIVATE, size + 1, 0660 | IPC_CREAT); int pipe_fds[2]; pipe(pipe_fds); putchar(0); fflush(stdout); usleep(1000); switch (fork()) { case -1: // = error perror("fork"); exit(EXIT_FAILURE); case 0: { // = child char *out = shmat(shmid, 0, 0), c; int i = 0; out[0] = 0; dup2(pipe_fds[0], 0); // redirect pipe to child's stdin close(pipe_fds[1]); setvbuf(stdout, 0, _IONBF, 0); while (read(0, &c, 1) == 1 && i < size) { printf("%c", c); // pass parent's stdout to real stdout, if (c==0) { printf("[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ printf_log():: buf=%p Reset pointer !!! out[%d]=%d len=%d buf::%.32s ]]\n",out,i,out[i],strlen(out),out); i=0; } else out[i++] = c; // and then buffer in mycapture buffer if (i==size) i=0; out[i] = 0; // (the extra <> are just for clarity) if (c==0) { i=0; printf("[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ printf_log():: buf=%p out[%d]=%d len=%d buf::%.32s ]]\n",out,i,out[i],strlen(out),out); } } _exit(EXIT_SUCCESS); } default: // = parent dup2(pipe_fds[1], 1); // replace stdout with output to child close(pipe_fds[0]); setvbuf(stdout, 0, _IONBF, 0); return shmat(shmid, 0, 0); // return the child's capture buffer } } //-------------------------------------------- //--- Global VARs from fadc250 lib: //-------------------------------------------- //#define FA_MAX_BOARDS 20 //#define FA_ADC_NSB_READBACK_MASK 0x000001FF //#define FA_ADC_NSB_TRIGGERPATH_MASK 0x00001E00 extern int fadcID[FA_MAX_BOARDS]; /* array of slot numbers for FADCs */ extern int fadcA24Offset; /* Difference in CPU A24 Base - VME A24 Base */ extern int nfadc; int fadcInited; /* LOCAL !! >0 if Library has been Initialized before */ //extern int fadcInited; /* >0 if Library has been Initialized before */ extern struct fadc_struct *FAp[(FA_MAX_BOARDS+1)]; extern volatile unsigned int *FApd[(FA_MAX_BOARDS+1)]; /* pointers to FADC FIFO memory */ extern int fadcUseSDC; /* If > 0 then Use Signal Distribution board */ extern int fadcProcRev[(FA_MAX_BOARDS+1)]; /* Processing FPGA Revision Info for each module */ extern pthread_mutex_t faMutex; #define FALOCK if(pthread_mutex_lock(&faMutex)<0) perror("pthread_mutex_lock"); #define FAUNLOCK if(pthread_mutex_unlock(&faMutex)<0) perror("pthread_mutex_unlock"); //-------------------------------------------- //--- Global VARs from tiLib: //-------------------------------------------- volatile struct TI_A24RegStruct *TIp=NULL; /* pointer to TI memory map */ volatile unsigned int *TIpd=NULL; /* pointer to TI data FIFO */ pthread_mutex_t tiMutex = PTHREAD_MUTEX_INITIALIZER; #define TILOCK if(pthread_mutex_lock(&tiMutex)<0) perror("pthread_mutex_lock"); #define TIUNLOCK if(pthread_mutex_unlock(&tiMutex)<0) perror("pthread_mutex_unlock"); int tiA24Offset=0; /* Difference in CPU A24 Base and VME A24 Base */ int tiA32Offset=0; /* Difference in CPU A32 Base and VME A32 Base */ unsigned int tiIntCount = 0; int tiMaster=1; /* Whether or not this TI is the Master */ unsigned int tiAckCount = 0; int tiBlockLevel=0; /* Current Block level for TI */ int tiNextBlockLevel=0; /* Next Block level for TI */ unsigned int tiSlaveMask = 0; /* TI Slaves (mask) to be used with TI Master */ unsigned int tiTriggerSource = 0; /* Set with tiSetTriggerSource(...) */ //-------------------------------------------- //--- Global VARs from fa125 Lib: //-------------------------------------------- extern int fa125ID[FA125_MAX_BOARDS]; /* array of slot numbers for FA125s */ extern volatile struct fa125_a24 *fa125p[(FA125_MAX_BOARDS+1)]; /* pointers to FA125 memory map */ extern volatile struct fa125_a32 *fa125pd[(FA125_MAX_BOARDS+1)]; /* pointers to FA125 FIFO memory */ #define FA125LOCK if(pthread_mutex_lock(&fa125Mutex)<0) perror("pthread_mutex_lock"); #define FA125UNLOCK if(pthread_mutex_unlock(&fa125Mutex)<0) perror("pthread_mutex_unlock"); pthread_mutex_t fa125Mutex = PTHREAD_MUTEX_INITIALIZER; int fa125A24Offset=0; /* Difference in CPU A24 Base and VME A24 Base */ extern int nfa125; //-------------------------------------------- //--- Global VARs from f1tdc Lib: //-------------------------------------------- #define F1TDC_ADDR (3<<19) extern int f1tdcA32Base; extern int f1tdcA32Offset; extern int nf1tdc; extern int f1ID[F1_MAX_BOARDS]; /* array of slot numbers for TDCs */ extern volatile struct f1tdc_struct *f1p[(F1_MAX_BOARDS+1)]; /* pointers to TDC memory map */ extern volatile unsigned int *f1pd[(F1_MAX_BOARDS+1)]; /* pointers to TDC FIFO memory */ #define F1LOCK if(pthread_mutex_lock(&f1Mutex)<0) perror("pthread_mutex_lock"); #define F1UNLOCK if(pthread_mutex_unlock(&f1Mutex)<0) perror("pthread_mutex_unlock"); pthread_mutex_t f1Mutex = PTHREAD_MUTEX_INITIALIZER; int f1tdcA24Offset = 0; /* Difference in CPU A24 Base - VME A24 Base */ extern int f1Nchips[(F1_MAX_BOARDS+1)]; /* Number of f1 chips on each module */ //-------------------------------------------- //--- Global VARs from CTP Lib: //-------------------------------------------- /* FPGA Channel number to Payload Port Map */ #define CTPBASE 0x30000 #define NUM_CTP_FPGA 3 #define NUM_FADC_CHANNELS 6 /* 5 for VLX50, 6 for VLX110 */ unsigned int ctpPayloadPort[NUM_CTP_FPGA][NUM_FADC_CHANNELS] = { /* U1 */ { 7, 9, 11, 13, 15, 0}, /* U3 */ { 8, 10, 12, 14, 16, 0}, /* U24 */ { 3, 1, 5, 2, 4, 6} }; enum ifpga {U1, U3, U24, NFPGA}; volatile struct CTPStruct *CTPp=NULL; /* pointer to CTP memory map */ pthread_mutex_t ctpMutex = PTHREAD_MUTEX_INITIALIZER; #define CTPLOCK if(pthread_mutex_lock(&ctpMutex)<0) perror("pthread_mutex_lock"); #define CTPUNLOCK if(pthread_mutex_unlock(&ctpMutex)<0) perror("pthread_mutex_unlock"); static int u24FirmwareVersion=0; unsigned int CTP_version[NFPGA]={0,0,0}; //-------------------------------------------- //--- Global VARs vmeDSC + dsc2 //-------------------------------------------- DMA_MEM_ID vmeIN,vmeOUT; extern DMANODE *the_event; extern unsigned int *dma_dabufp; #ifdef USE_vmeDSC //-------------------------------------------- //--- Global VARs vmeDSC Lib: //-------------------------------------------- extern int Ndsc; //--- for status --- extern unsigned int dscA24Offset; // Offset between VME A24 and Local address space extern volatile struct dsc_struct *dscp[DSC_MAX_SLOTS+1]; // pointers to DSC A24 memory map extern volatile unsigned int *dscpd[DSC_MAX_SLOTS+1]; // pointers to DSC A32 memory map // Mutex to guard flexio read/writes extern pthread_mutex_t dscMutex; // = PTHREAD_MUTEX_INITIALIZER; #define DSCLOCK if(pthread_mutex_lock(&dscMutex)<0) perror("pthread_mutex_lock"); #define DSCUNLOCK if(pthread_mutex_unlock(&dscMutex)<0) perror("pthread_mutex_unlock"); // Macro to check for module initialization int dscID[DSC_MAX_BOARDS+1]; // array of slot numbers for DSCs #define CHECKID(id) { \ if(id==0) id=dscID[0]; \ if((id<0) || (id>21) || (dscp[id] == NULL)) \ { \ logMsg("%s: ERROR: DSC in slot %d is not initialized \n", \ __FUNCTION__,id,3,4,5,6); \ return ERROR; \ } \ } #else //-------------------------------------------- //--- Global VARs dsc2 Lib: //-------------------------------------------- int Ndsc; #endif //-------------------------------------------- // Global Variables tdLib //-------------------------------------------- extern int nTD; /* Number of TDs found with tdInit() */ //-------------------------------------------- // //-------------------------------------------- int VME_INIT=0; int FA250_INIT=0; int TI_INIT=0; int CTP_INIT=0; int SD_INIT=0; int TD_INIT=0; int TS_INIT=0; int F1TDC_INIT=0; int FA125_INIT=0; //-------------------------------------------- // misc global vars //-------------------------------------------- struct timeval tv0, tv1, tv2; static unsigned int cnt1, cnt2 ; static time_t tm0, tm1, tm2; //-------------------------------------------- void faStatus1(int sflag); int fa125GetSerial(struct FADC250 *fadc250, int id); //int ctpStatus0(char *sndstr,int lenstr,int pflag); //int ctpInit0(char *sndstr,int lenstr,int flag); //int vmeDSCStatus0(char *sndstr,UINT32 id, int pflag); int faPrintScalers0(char *sndstr,int id, int rflag); //------------------------------------------------------------------------- // //------------------------------------------------------------------------- int vme_init(int flag) { int i,FA_SLOT,iFlag=0; GEF_STATUS status; printf("----> VME INIT %d -----\n",flag); printf_buffer = printf_log(128000); // capture first 128k bytes printf("----> print_buffer is created \n"); if (flag>0) { GEF_STATUS status = vmeOpenDefaultWindows(); if (status != GEF_SUCCESS) { printf("vmeOpenDefaultWindows failed: code 0x%08x\n",status); //goto CLOSE; flag=0; } else { printf("vmeOpenDefaultWindows done: code 0x%08x\n",status); VME_INIT=1; vmeCheckMutexHealth(10); vmeBusLock(); //------------------------------------------------------------------------------ /* Setup Address and data modes for DMA transfers * * vmeDmaConfig(addrType, dataType, sstMode); * * addrType = 0 (A16) 1 (A24) 2 (A32) * dataType = 0 (D16) 1 (D32) 2 (BLK32) 3 (MBLK) 4 (2eVME) 5 (2eSST) * sstMode = 0 (SST160) 1 (SST267) 2 (SST320) */ vmeDmaConfig(2,5,1); /* INIT dmaPList */ dmaPFreeAll(); //vmeIN = dmaPCreate("vmeIN",10244,500,0); vmeIN = dmaPCreate("vmeIN",11244,500,0); vmeOUT = dmaPCreate("vmeOUT",0,0,0); dmaPStatsAll(); dmaPReInitAll(); fflush(stdout); usleep(1000); fprintf(stderr, "\n-----------------------> Captured: <%s>\n", printf_buffer); putchar(0); fflush(stdout); while(printf_buffer[0]) usleep(1000); //------------------------------------------------------------------------------ //---- fa250 ---- /* Skip initialization and firmware/version check */ iFlag = FA_INIT_SKIP | FA_INIT_SKIP_FIRMWARE_CHECK; /* Set the FADC structure pointer */ int stat250 = faInit((unsigned int)(FADC_ADDR),(1<<19),18,iFlag); printf(" fa250 init status=%d fadcInited=%d nfadc=%d\n",stat250,fadcInited,nfadc); fadcInited=nfadc; for(i = 0; i Enable Scaler %d\n",FA_SLOT); faEnableScalers(FA_SLOT); } //---- fa125 ---- if (nfa125==0) { int nmods=3; //--- dummy here iFlag=FA125_INIT_SKIP | FA125_INIT_SKIP_FIRMWARE_CHECK; int stat = fa125Init(0,0,nmods,iFlag); printf("fa125 init status=%d \n",stat); if (stat != OK) printf("ERROR: fa125Init failed \n"); } //---- TS ---- printf("-> TS init .... \n"); if (tsInit((21<<19),0,TS_INIT_NO_INIT)==OK) { /* Set the TS structure pointer */ // if(tsCheckAddresses()==ERROR) tsStatus(1); TS_INIT=1; } else { printf("TS init error \n"); TS_INIT=0; } //---- TI ---- int slot=21; if (tiInit(slot,TI_READOUT_EXT_POLL,0)==OK) { //-- bit 0: Do not initialize the board, just setup the pointers tiCheckAddresses(); printf("Firmware version = 0x%x\n",tiGetFirmwareVersion()); TI_INIT=1; } else { printf("TI not found \n"); TI_INIT=0; } //---- CTP ---- slot=11; if (TI_INIT>0 && ctpInit(1)==OK) { CTP_INIT=1; printf("CTP init done \n"); } else { printf("CTP not found \n"); CTP_INIT=0; } //---- SD ---- printf("SD init .... \n"); if (sdInit(0)==OK) { sdStatus(1); SD_INIT=1; } else { printf("SD init error \n"); SD_INIT=0; } //---- TD ---- printf("TD init .... \n"); iFlag=0; iFlag |= (1<<18); /* Skip firmware check*/ if (tdInit(0,0,0,iFlag)==OK) { printf("found TDs: %d\n",nTD); tdStatus(0, 1); TD_INIT=1; } else { printf("TD init error \n"); TD_INIT=0; } //---- F1TDC ---- printf("F1TDC init .... \n"); f1Init(F1TDC_ADDR,(1<<19),18,F1_IFLAG_NOINIT | F1_IFLAG_NOFWCHECK); printf("found f1tdc: %d\n",nf1tdc); //---- vmeDSC / dsc2 ---- #ifdef USE_vmeDSC vmeDSCInit((3<<19),(1<<19),20,0); #else iFlag=0; //iFlag = (1<<16); // skip initialization dsc2Init(0x100000,0x80000,20,iFlag); Ndsc = dsc2GetNdsc(); #endif printf("found vmeDSC/dsc2: %d\n",Ndsc); //------------------------------------- vmeBusUnlock(); if (fadcInited) { printf("slot_info():: Skip init fadc, fadcInited=%d \n",fadcInited); FA250_INIT=1; } } } //-- close --- if (flag==0) { status = vmeCloseDefaultWindows(); printf("vmeCloseDefaultWindows : code 0x%08x\n",status); if (status != GEF_SUCCESS) { printf("vmeCloseDefaultWindows failed: code 0x%08x\n",status); return -1; } } return 0; } //------------------------------------------------------------------------- // //------------------------------------------------------------------------- int vme_fadc_scaler_print(char *sndstr,int slot, int rflag) { unsigned int data[17]; printf("----> Enter Print Scaler %d -----\n",slot); if (VME_INIT==0) vme_init(2); if ((FA250_INIT==0) || fadcInited==0 || (slot<=0) || (slot>21) || (FAp[slot] == NULL) ) return 1; /* Lock the vmeBus Mutex so as to not interfere with CODA */ printf("vme_fadc_scaler_print():: try vmeBusLock() ... \n"); vmeBusLock(); //--------------------------------------------------------------------------------------- //faPrintScalers(slot, 1); faPrintScalers0(sndstr,slot, rflag); gettimeofday(&tv1, NULL); tm1=tv1.tv_usec+1000000*tv1.tv_sec; tm2=tm1-tm0; cnt2=data[16]; sprintf(&sndstr[strlen(sndstr)],"timer: cnt=%u, t0=%lu t1=%lu, dc=%u dt=%lu r=%f\n" ,cnt1,tm0,tm1,cnt2,tm2, (double)tm2/(double)cnt2); tm0=tm1; /* int ichan; faReadScalers(slot, data, 0xFFFF, 1); double tm = (double)data[16]/1e6*2.; // sec., 1 clock = 2 us ; double rate=-1; for(ichan=0; ichan<16; ichan++) { if( (ichan%4) == 0 ) sprintf(&sndstr[strlen(sndstr)],"\n"); if (tm>0) rate=(double)data[ichan]/tm; sprintf(&sndstr[strlen(sndstr)],"| %2d: %10.3f Hz ",ichan,rate); } sprintf(&sndstr[strlen(sndstr)],"\n timer: %.3f sec. \n",tm); */ //--------------------------------------------------------------------------------------- /* Unlock the vmeBus Mutex */ vmeBusUnlock(); return 0; } //------------------------------------------------------------------------- int vme_fadc_scaler_getrates(double *rates, unsigned int *counters, int slot, int rflag) { unsigned int data[17], data_new[17], ichan; if (VME_INIT==0) vme_init(3); if (Debug>1 && fadcInited>0) printf("VME_INIT=%d slot=%d fadcInited=%d \n",VME_INIT, slot, fadcInited); printlog("vme_fadc_scaler_getrates:: slot=",slot); if ((FA250_INIT==0) || fadcInited==0 || (slot<=0) || (slot>21) || (FAp[slot] == NULL) ) return 1; /* Lock the vmeBus Mutex so as to not interfere with CODA */ if (Debug>1) printf("vme_fadc_scaler_getrates()::try vmeBusLock() ... \n"); printlog("vme_fadc_scaler_getrates:: vmeBusLock, slot=",slot); vmeBusLock(); //--------------------------------------------------------------------------------------- if (Debug>1) printf("----> get rates for Scaler %d -----\n",slot); faReadScalers(slot, data_new, 0xFFFF, rflag); //-- reset --- for(ichan=0; ichan<(16+1); ichan++) { data[ichan]=data_new[ichan]-counters[ichan]; counters[ichan]=data_new[ichan];} double tm = (double)data[16]/1e6*2.; // sec., 1 clock = 2 us ; double rate=-1; //---- if (Debug>1) { for(ichan=0; ichan<16; ichan++) { if( (ichan%4) == 0 ) printf("\n"); printf("| %2d: %08u ",ichan,counters[ichan]); printlog("vme_fadc_scaler_getrates:: counters=",counters[ichan]); } printf("\n counter: %lu %f sec.\n",counters[16],tm); } //---- rates[16]=tm; for(ichan=0; ichan<16; ichan++) { if (tm>0) rate=(double)data[ichan]/tm; rates[ichan]=rate; } //--------------------------------------------------------------------------------------- /* Unlock the vmeBus Mutex */ vmeBusUnlock(); printlog("vme_fadc_scaler_getrates:: vmeBusUlock, slot=",slot); return 0; } //------------------------------------------------------------------------- /** * @ingroup Readout * @brief Scaler Print Out routine * * Print out the scalers as well as the timer counter. * * @param id Slot number * @param rflag - Printout Flag * - bit 0: Latch Scalers before read * - bit 1: Clear Scalers after read * @return OK if successful, otherwise ERROR. */ //------------------------------------------------------------------------- int faPrintScalers0(char *sndstr,int id, int rflag) { int doLatch=0, doClear=0, ichan=0; unsigned int counts, counts_old, data[16], time_count; static unsigned int time_count_old, data_old[16]; if(id==0) id=fadcID[0]; if((id<=0) || (id>21) || (FAp[id] == NULL)) { logMsg("faPrintScalers: ERROR : ADC in slot %d is not initialized \n", id,0,0,0,0,0); return ERROR; } if(rflag & ~(FA_SCALER_CTRL_MASK)) { logMsg("faPrintScalers: WARN : rflag (0x%x) has undefined bits \n", rflag,0,0,0,0,0); } doLatch = rflag&(1<<0); doClear = rflag&(1<<1); FALOCK; if(doLatch) vmeWrite32(&FAp[id]->scaler_ctrl, FA_SCALER_CTRL_ENABLE | FA_SCALER_CTRL_LATCH); for(ichan=0; ichan<16; ichan++) { data[ichan] = vmeRead32(&FAp[id]->scaler[ichan]); // printf("faPrintScaler[%d]=%d, addr=%p, fadcAddr=0x%08x(0x%08x), csr=%p\n" // ,ichan,data[ichan], &FAp[id]->scaler[ichan], (UINT32) FAp[id], (UINT32)FAp[id]-fadcA24Offset // , &FAp[id]->csr); } time_count = vmeRead32(&FAp[id]->time_count); //printf("faPrintScaler:: timer=%d \n",time_count); if(doClear) vmeWrite32(&FAp[id]->scaler_ctrl, FA_SCALER_CTRL_ENABLE | FA_SCALER_CTRL_RESET); FAUNLOCK; sprintf(&sndstr[strlen(sndstr)],"\n %s: Slot:%d, flags:(C:%d,L:%d) Scaler Counts: ",__FUNCTION__, id,doClear,doLatch); for(ichan=0; ichan<16; ichan++) { counts=data[ichan]; counts_old=data_old[ichan]; data_old[ichan]=data[ichan]; data[ichan]-=counts_old; if( (ichan%4) == 0 ) sprintf(&sndstr[strlen(sndstr)],"\n"); sprintf(&sndstr[strlen(sndstr)],"| %2d: %10u ",ichan,counts); } sprintf(&sndstr[strlen(sndstr)],"\n timer: %10u\n",time_count); double tm = (double)(time_count-time_count_old)/1e6*2.; // sec., 1 clock = 2 us ; time_count_old=time_count; double rate=-1; for(ichan=0; ichan<16; ichan++) { if( (ichan%4) == 0 ) sprintf(&sndstr[strlen(sndstr)],"\n"); if (tm>0) rate=(double)data[ichan]/tm; sprintf(&sndstr[strlen(sndstr)],"| %2d: %10.3f Hz ",ichan,rate); } sprintf(&sndstr[strlen(sndstr)],"\n timer: %.3f sec. \n",tm); return OK; } //------------------------------------------------------------------------- // //------------------------------------------------------------------------- int slot_info(char *sndstr,int lenstr,int slot) { int iFlag=0; // test FADC slot if (VME_INIT==0) vme_init(4); /* Lock the vmeBus Mutex so as to not interfere with CODA */ printf("slot_info():: try vmeBusLock(); slot=%d lenstr=%d \n",slot,lenstr); vmeBusLock(); printf("slot_info():: Loop.....count: fadc250=%d fadc125=%d \n",nfadc,nfa125); int ii, nb250=0; for (ii=0;ii Print Scaler %d -----\n",slot); //faPrintScalers(slot, 1); //faPrintScalers0(sndstr,slot, 3); //-- reset faPrintScalers0(sndstr,slot, 1); //-- NO reset //--------------------------------------------------------------------------------------- nb250++; } } if (nb250!=1) { sprintf(&sndstr[strlen(sndstr)],"\n===> Slot %d :: ERROR reading FADC250 nb250=%d ",slot,nb250); //---------------- fa125 ------------- printf("------------ check fa125 -----------------\n"); int nb125=0; int iadc=0, faslot=0; for(iadc=0; iadc Slot %d :: ERROR reading FADC125 nb125=%d \n",slot,nb125); } printf("------------ check TD -----------------\n"); for (ii=0;ii0 ) { //--- TI ---- printf(" TI_INIT_NO_INIT=%d\n",TI_INIT_NO_INIT); printf("Firmware version = 0x%x\n",tiGetFirmwareVersion()); putchar(0); fflush(stdout); while(printf_buffer[0]) usleep(1000); //tiStatus0(sndstr,lenstr,1); tiStatus(1); fflush(stdout); usleep(10000); fflush(stdout); strncpy(sndstr,printf_buffer,lenstr); } else if (slot==21 && TS_INIT>0 ) { //--- TS ---- putchar(0); fflush(stdout); while(printf_buffer[0]) usleep(1000); tsStatus(1); fflush(stdout); usleep(10000); fflush(stdout); strncpy(sndstr,printf_buffer,lenstr); fprintf(stderr, "\n----------- len=%d ------------> Captured: <%s>\n", lenstr, printf_buffer); } printf("------------ check CTP -----------------\n"); if (slot==11 && CTP_INIT>0) { printf(" TI_INIT_NO_INIT=%d\n",TI_INIT_NO_INIT); //tiInit(21,TI_READOUT_EXT_POLL,1); //-- bit 0: Do not initialize the board, just setup the pointers //if (ctpInit0(sndstr,lenstr,1)==OK) //ctpStatus0(sndstr,lenstr,3); putchar(0); fflush(stdout); while(printf_buffer[0]) usleep(1000); ctpStatus(3); fflush(stdout); usleep(10000); fflush(stdout); strncpy(sndstr,printf_buffer,lenstr); fprintf(stderr, "\n----------- len=%d ------------> Captured: <%s>\n", lenstr, printf_buffer); } printf("------------ check SD -----------------\n"); if (slot==12 && SD_INIT>0) { putchar(0); fflush(stdout); while(printf_buffer[0]) usleep(1000); sdStatus(1); sdPrintBusyoutCounters(); sdPrintTrigoutCounters(); fflush(stdout); usleep(10000); fflush(stdout); strncpy(sndstr,printf_buffer,lenstr); fprintf(stderr, "\n----------- len=%d ------------> Captured: <%s>\n", lenstr, printf_buffer); } ////////////////////////////// // F1TDC init ////////////////////////////// printf("------------ check F1TDC -----------------\n"); //f1Init(F1TDC_ADDR,(1<<19),18,F1_IFLAG_NOINIT | F1_IFLAG_NOFWCHECK); //printf("found f1tdc: %d\n",nf1tdc); for(ii=0; ii fadcInited=%d\n",fadcInited); if (VME_INIT==0) vme_init(5); /* Lock the vmeBus Mutex so as to not interfere with CODA */ printf("crate_info():: try vmeBusLock() ... \n"); vmeBusLock(); printf("crate_info():: Loop.....count %d fadc, fadcInited=%d\n",nfadc,fadcInited); //------- reset info ------ int ic=0; for(ic = 0; ic<=Nfa250; ic++) { sprintf(fadc250[ic].SerNum,"------- \0"); fadc250[ic].TYPE=0; } for(i = 0; icsr,&FAp[FA_SLOT]->csr); //faCheckAddresses(FA_SLOT); //fadc250[FA_SLOT].SerNum[0]=0; sprintf(fadc250[FA_SLOT].SerNum,"-error-"); /* Get the serial number */ if(faGetSerialNumber(FA_SLOT,(char **)&sn,0)>0) { printf("%2d: Serial Number = %s\n", FA_SLOT,sn); sprintf(fadc250[FA_SLOT].SerNum,"%s",sn); } else { printf("ERROR READING SERIAL NUMBER\n"); } /* Get the firmware versions */ firmware_versions = faGetFirmwareVersions(FA_SLOT, 1); fadc250[FA_SLOT].F_REV=firmware_versions&0xFFFF; fadc250[FA_SLOT].B_REV=firmware_versions>>16; fadc250[FA_SLOT].TYPE=1; printf("----> enable Scaler %d\n",FA_SLOT); faEnableScalers(FA_SLOT); //faPrintScalers(FA_SLOT, 1); /* More adventurous? Get the raw register. (Careful... this won't be thread safe, unless used in conjuction with vmeBusLock()) */ raw_csr = vmeRead32(&FAp[FA_SLOT]->csr); } if (nfadc>0) { putchar(0); fflush(stdout); while(printf_buffer[0]) usleep(1000); faGStatus(0); fflush(stdout); usleep(50000); fflush(stdout); if (lenstr0) { putchar(0); fflush(stdout); while(printf_buffer[0]) usleep(1000); tdGStatus(0); fflush(stdout); usleep(100000); fflush(stdout); if (lenstr0) { //--- TI ---- printf("Firmware version = 0x%x\n",tiGetFirmwareVersion()); char mySN[20]; printf("0x%08x\n",tiGetSerialNumber((char **)&mySN)); printf("mySN = %s\n",mySN); sprintf(fadc250[slot].SerNum,"%s",mySN); fadc250[slot].F_REV=tiGetFirmwareVersion(); fadc250[slot].B_REV=0; fadc250[slot].TYPE=4; //tiStatus(1); } else if (TS_INIT>0 ) { //--- TS ---- printf("Firmware version = 0x%x\n",tsGetFirmwareVersion()); char mySN[20]; printf("0x%08x\n",tsGetSerialNumber((char **)&mySN)); sprintf(fadc250[slot].SerNum,"%s",mySN); fadc250[slot].F_REV=tsGetFirmwareVersion(); fadc250[slot].B_REV=0; fadc250[slot].TYPE=8; // TS tsGetSerialNumber(char **rSN) } else { printf("TI/TS not found \n"); } printf("------------ check CTP -----------------\n"); //-- check CTP ---- slot=11; if (CTP_INIT>0) { sprintf(fadc250[slot].SerNum,"%s","CTP"); fadc250[slot].F_REV=CTP_version[U1]; fadc250[slot].B_REV=CTP_version[U3]; fadc250[slot].TYPE=5; //-- CTP } //-- check SD ---- slot=12; if (SD_INIT>0) { char rSN[20]; sdGetSerialNumber(rSN); sprintf(fadc250[slot].SerNum,"%s",rSN); fadc250[slot].F_REV=sdGetFirmwareVersion(1); fadc250[slot].B_REV=0x0; fadc250[slot].TYPE=7; //-- SD } //---------------- fa125 ------------- printf("------------ check FA125 -------------------------\n"); int iadc=0, faslot=0; printf("crate_info():: fa125 info, nfa125=%d \n",nfa125); for(iadc=0; iadc0) { //printf("++++++++++++++++++++++++++>>>>>>> printf_buffer:: p=%p len=%d [0]=%d BEFORE 0 :: %.32s \n",printf_buffer, strlen(printf_buffer), printf_buffer[0], printf_buffer); putchar(0); fflush(stdout); while(printf_buffer[0]) usleep(1000); //printf("++++++++++++++++++++++++++>>>>>>> printf_buffer:: p=%p len=%d [0]=%d AFTER 0 :: %.32s \n",printf_buffer, strlen(printf_buffer), printf_buffer[0], printf_buffer); fa125GStatus(0); fflush(stdout); usleep(100000); fflush(stdout); if (lenstr0) { putchar(0); fflush(stdout); while(printf_buffer[0]) usleep(1000); f1GStatus(0); fflush(stdout); usleep(50000); fflush(stdout); if (lenstr%s<\n",f1Slot(ii),sn); sprintf(fadc250[slot].SerNum,"%s",sn); fadc250[slot].F_REV=f1GetFirmwareVersion(slot,1); fadc250[slot].B_REV=0; fadc250[slot].TYPE=3; } //f1GStatus(0); ////////////////////////////// // vmeDSC ////////////////////////////// printf("------------ check vmeDSC/dsc2 -----------------\n"); int idsc=0, ichan=0, DSC_SLOT=0; // Configure modules for(idsc=0; idsc21) || (fa125p[id] == NULL)) { printf("%s: ERROR : FA125 in slot %d is not initialized \n",__FUNCTION__,id); return ERROR; } FA125LOCK; m.id = vmeRead32(&fa125p[id]->main.id); m.swapctl = vmeRead32(&fa125p[id]->main.swapctl); m.version = vmeRead32(&fa125p[id]->main.version); m.pwrctl = vmeRead32(&fa125p[id]->main.pwrctl); m.slot_ga = vmeRead32(&fa125p[id]->main.slot_ga); m.clock = vmeRead32(&fa125p[id]->main.clock); for(i=0; i<4; i++) m.serial[i] = vmeRead32(&fa125p[id]->main.serial[i]); f[0].version = vmeRead32(&fa125p[id]->fe[0].version); if(f[0].version==0xffffffff) { f[0].version = vmeRead32(&fa125p[id]->fe[0].version); } p.version = vmeRead32(&fa125p[id]->proc.version); p.csr = vmeRead32(&fa125p[id]->proc.csr); p.trigsrc = vmeRead32(&fa125p[id]->proc.trigsrc); p.ctrl2 = vmeRead32(&fa125p[id]->proc.ctrl2); /* m.adr32 = vmeRead32(&fa125p[id]->main.adr32); m.adr_mb = vmeRead32(&fa125p[id]->main.adr_mb); m.ctrl1 = vmeRead32(&fa125p[id]->main.ctrl1); m.block_count = vmeRead32(&fa125p[id]->main.block_count); p.trig_count = vmeRead32(&fa125p[id]->proc.trig_count); p.ev_count = vmeRead32(&fa125p[id]->proc.ev_count); m.blockCSR = vmeRead32(&fa125p[id]->main.blockCSR); f[0].config1 = vmeRead32(&fa125p[id]->fe[0].config1); f[0].ptw = vmeRead32(&fa125p[id]->fe[0].ptw); f[0].pl = vmeRead32(&fa125p[id]->fe[0].pl); f[0].nsb = vmeRead32(&fa125p[id]->fe[0].nsb); f[0].nsa = vmeRead32(&fa125p[id]->fe[0].nsa); for(i=0; i<12; i++) { f[i].test = vmeRead32(&fa125p[id]->fe[i].test); } */ double temp1=0, temp2=0; temp1 = 0.0625*((int) vmeRead32(&fa125p[id]->main.temperature[0])); temp2 = 0.0625*((int) vmeRead32(&fa125p[id]->main.temperature[1])); FA125UNLOCK; printf("\nSTATUS for FA125 in slot %d at VME (Local) base address 0x%x (0x%x)\n", id, (UINT32) fa125p[id] - fa125A24Offset, (UINT32) fa125p[id]); printf("------------------------------------------------------------------------------- \n"); printf(" Main Firmware Revision = 0x%08x\n", m.version); printf(" FrontEnd Firmware Revision = 0x%08x\n", f[0].version); printf(" Processing Revision = 0x%08x\n", p.version); printf(" Main SN = 0x%04x%08x\n",m.serial[0], m.serial[1]); printf(" Mezzanine SN = 0x%04x%08x\n",m.serial[2], m.serial[3]); printf("%s: Main board temperature: %5.2lf \tMezzanine board temperature: %5.2lf\n", __FUNCTION__, temp1, temp2); //----------------------------- FILL STRUCT ----------------- int slot=id; printf("Before:: fa125 SN = %s\n",fadc250[slot].SerNum); sprintf(fadc250[slot].SerNum,"%x:%x",m.serial[0], m.serial[1]); fadc250[slot].F_REV=m.version; fadc250[slot].B_REV=p.version; //----------------------------------------------------------------- printf("\n"); return OK; } //------------------------------------------------------------------------- // f1tdc //------------------------------------------------------------------------- //------------------------------------------------------------------------- // f1tdc //------------------------------------------------------------------------- //------------------------------------------------------------------------- // CTP init //------------------------------------------------------------------------- //------------------------------------------------------------------------- // vmeDISC lib //------------------------------------------------------------------------- int DecodeData(DMANODE *outEvent); int SLOTID; int TRIGGERNUMBER; int IN2; int IN1; int BUILDERFLAG; int SCALERLENGTH; int DATANOTVALID; int CHANID; int NDISC = 20; int START_SLOT = 3; int EventCounter = 0; unsigned int ScalerData[MAX_SLOT+1][2*MAX_CHAN+1]; //------------------------------------------------------------------------- int DecodeData(DMANODE *outEvent){ int NumberOfChannels = 0; int idata; if (Debug>8) printf("-----------> Enter DSC DecodeData():\n"); for( idata=0;idatalength;idata++){ unsigned int wrd = LSWAP(outEvent->data[idata]); if( wrd & 0x80000000 ){ /* data type defining word */ unsigned int dataType = (wrd & 0x78000000) >> 27; switch(dataType){ case 0: /*BLOCK HEADER*/ break; case 1:/* BLOCK TRAILER */ break; case 2:/* Event Header */ SLOTID = (wrd & 0x07c00000) >> 22; TRIGGERNUMBER = (wrd & 0x3FFFFF); CHANID=0; break; case 4:/* Scaler Header */ IN2 = (wrd & 0x20000)>> 8; IN1 = (wrd & 0x10000)>> 8; BUILDERFLAG = (wrd & 0x3F00)>> 8; SCALERLENGTH = (wrd & 0xFF); if (Debug>8) printf("SLOT %d : BuilderFlag is 0x%x \n",SLOTID,BUILDERFLAG); break; case 14:/* Data Not Valid */ DATANOTVALID = 1; break; case 15:/* Filler Word */ DATANOTVALID = 1; break; } } else{ /* data word*/ //printf("%d %x\n",NumberOfChannels,wrd); int idx = SLOTID; int ch = CHANID++; if (Debug>8) printf("data from sl=%d : nch=%d idx=%d ch=%d rate=%d\n", SLOTID,NumberOfChannels,idx,ch,wrd); if ( 0>idx || idx>MAX_SLOT || 0>ch || ch>=2*MAX_CHAN ) continue; // >MAX_SLOT !! 21 ScalerData[idx][ch] = wrd; NumberOfChannels++; } } // --- data loop --- return NumberOfChannels; } //------------------------------------------------------------------------- int vme_discr_init(UINT16 THR1, UINT16 THR2, UINT16 pWidth, int *nslots, int *slots, int maxslots) { if (VME_INIT==0) vme_init(7); *nslots=Ndsc; printf("--- SET UP VME DISCRIMINATOR --- Ndiscr=%d THR=(%d,%d) width=%d\n",Ndsc, THR1,THR2,pWidth); if (Ndsc==0) return 1; vmeBusLock(); #ifdef USE_vmeDSC vmeDSCSetThresholdAll(THR1, THR2); #else dsc2SetThresholdAll(THR1, THR2); #endif int idsc; for(idsc=0; idsc0) { //printf("%2d: dCnt = %d\n",DSC_SLOT,dCnt); dma_dabufp += dCnt; vmeBusUnlock(); } else{ printf("ERROR: No data from DSC ID %02d\n",DSC_SLOT); } } else { printf("ERROR: Data not ready from DSC ID %02d\n",DSC_SLOT); } } PUTEVENT(vmeOUT); DMANODE *outEvent = dmaPGetItem(vmeOUT); printf("Event %d: outevent length = %d\n",EventCounter,outEvent->length); int nchan = DecodeData(outEvent); printf(" channel ScalerSlot1 ScalerSlot2\n"); for (k=0;k<16;k++) { printf(" %2d %d %d \n", k,ScalerData[4][k],ScalerData[5][k]); } printf(" 16 0x%x 0x%x \n", ScalerData[4][16],ScalerData[5][16]); dmaPFreeItem(outEvent); } return 0; } //------------------------------------------------------------------------- int vme_discr_set_thresholds(uint32_t *thresholds, int type, int slot) { if (VME_INIT==0) vme_init(6); printf("--- SET THR VME DISCRIMINATOR --- Ndsc=%d \n",Ndsc); if (Ndsc==0) return 1; vmeBusLock(); int chan; for (chan=0; chan1) printf("vme_discr_getrates():: found slot=%d\n",slot); int ready=10000; #ifdef USE_vmeDSC vmeDSCSoftTrigger(DSC_SLOT); #else dsc2SoftTrigger(DSC_SLOT); #endif /* Check for data ready */ int i; for(i=0; i<100; i++) { #ifdef USE_vmeDSC ready = vmeDSCDReady(DSC_SLOT); #else ready = dsc2DReady(DSC_SLOT); #endif if(ready) break; } if(ready) { //printf("%02d: Ready = %d\n",DSC_SLOT,ready); vmeBusLock(); #ifdef USE_vmeDSC dCnt = vmeDSCReadBlock(DSC_SLOT, dma_dabufp, 100, 1); #else dCnt = dsc2ReadBlock(DSC_SLOT, dma_dabufp, 100, 1); #endif //dCnt = vmeDSCReadScalers(DSC_SLOT, dma_dabufp, 100, 2, 1); if (dCnt>0) { //printf("%2d: dCnt = %d\n",DSC_SLOT,dCnt); dma_dabufp += dCnt; nsl++; } else { printf("ERROR: No data from DSC ID %02d\n",DSC_SLOT); } vmeBusUnlock(); } else { printf("ERROR: Data not ready from DSC ID %02d\n",DSC_SLOT); } } PUTEVENT(vmeOUT); DMANODE *outEvent = dmaPGetItem(vmeOUT); if (nsl==1) { //printf("Event %d: outevent length = %d\n",EventCounter,outEvent->length); int nchan = DecodeData(outEvent); //printf(" channel ScalerSlot1 ScalerSlot2\n"); for (k=0;k<16;k++) { counters[k]=ScalerData[slot][k+16]; //-- TDC_GRP1 (DAQ) //printf(" %2d",k); } //printf("\n"); //for (k=0;k<16;k++) printf("%5d",ScalerData[slot][k]); printf("\n"); for (k=0;k<16;k++) { counters2[k]=ScalerData[slot][k]; //-- TRG_GRP1 (EPICS) //printf(" %2d",k); } //printf("\n"); } dmaPFreeItem(outEvent); if (nsl!=1) return 1; return 0; } //----------------------------------------------------------------------------------- // //-----------------------------------------------------------------------------------