#include "ieee_fun_types.h" #include "moduleSetupLib.h" #include #include #include /* use below to get rid of warning message */ void usleep(unsigned long usec); /* needed to get date time at micro-second procission */ #include #define MILLION 1000000 unsigned getbits( unsigned x, unsigned p, unsigned n) { /* get n bits from position p */ return (( x >> (p+1-n)) & ~(~0 << n)); } int main() { /*int ret; */ /* return for CAMAC command*/ int mPointer1=0; /* holder for FERA memory pointer returned */ int top=0, top1=0, top2=0; int lam=0, lam1=0, lam2=0; int ext; int branch=18; int crate; int crate1=4; int slot,subad,function,qres; /* stuff for AD114 */ int virtID; int ad114Error; int lld=0, uld=0, dcOffset=0; int lecroy4302Error =0; int lecroy4300Error = 0; int pedestal = 0; int return_status = CAM_S_SUCCESS; /* data array returned by Camac Controler */ unsigned short dataw[2]={0,0}; unsigned short data16[32768]; unsigned short data16a[32768]; int dataw24[2]={0,0}; /* for 24bit reads of jorway 85a quad scaler */ /* Jorway spec say max 16bit word dma transfer is 16100 */ int cb[3]; /* camac control block 0=request_repeat_count 1=result_repeat_count 2=lam id(not used) 3=dma type 0=Q-stop 1=single_word */ int i=0; int evLoop=0,evLoopMax=0, iWait=0; /* unsigned short RAW; int numNoReads=25; float pulserDelay=0.0, pulserPeriod=0.0; */ /* stuff for time and date */ struct timeval tpstart; int realTimeClockLRcrate1; int realTimeClockHRcrate1; int timeOffset=0; /* Get Real time Clock information */ gettimeofday(&tpstart,NULL); timeOffset = tpstart.tv_sec; /***************** OPEN DATA FILE ********************/ /* File to save data */ FILE *data_file; char file[20] = ("data/run_001.txt"); char ctemp[4]; int r; int run=0, runType=0;; /* get configuration information from user or a 3): "); scanf("%d", &evLoopMax); if (evLoopMax == 0) evLoopMax=3; fprintf(stderr,"Enter wait on LAM time in msec(0=>100): "); scanf("%d", &iWait ); if (iWait == 0) iWait = 100; iWait = iWait*1000; */ /* convert to usec */ iWait=100000; /* default wait time is 100 msec */ evLoopMax=6000; /* maximum number of event loops is 10 minutes */ fprintf(stderr," Wait on LAM time =%d, Max # of loops = %d\n",iWait,evLoopMax); /* printf("Enter Number of NoReads allowed: "); scanf("%d",&numNoReads); printf("Enter pulserDelay: "); scanf("%f",&pulserDelay); printf("Enter pulserPeriod: "); scanf("%f",&pulserPeriod); printf("User requested numNoReads = %d\n", numNoReads); printf("User requested pulserDelay = %f\n", pulserDelay); printf("User requested pulserPeriod = %f\n", pulserPeriod); printf("TitleLine: CC-USB data file runNumb=%d numNoReads=%d pulserDelay=%f pulserPeriod=%f \n", run, numNoReads, pulserDelay, pulserPeriod); fprintf(data_file,"CC-USB data file runNumb=%d numNoReads=%d pulserDelay=%f pulserPeriod=%f \n", run, numNoReads, pulserDelay, pulserPeriod); */ /****************** OPEN Jorway CAMAC interface **********************/ printf ("\n Opening Jorway411 branch highway"); if ((return_status = cdchn(branch, 1, 0)) != CAM_S_SUCCESS) { printf("Error opening branch=%d\n",branch); exit(1); }; ccctype(branch,0,1); /* parallel - Jor 73A is considered parallel */ /**************************** Initialize CAMAC **************************/ printf("\n Initializing CAMAC\n"); slot=2; subad=0; /* encode the branch, crate, slot, and subad arguments into ext */ cdreg(&ext,branch,crate1,slot,subad); /* crate clear */ cccc(ext); /* crate initialize */ cccz(ext); /* remove crate inhibit */ ccci(ext,0); function=0; /****************** SET TRIGGER INHIBIT WITH 2323 GATE GENERATOR ***********************/ printf("\n Set up 2323 GG ch a as initial clr to 4302"); slot=18; subad = 0; cdreg(&ext,branch,crate1,slot,subad); cssa(1,ext,dataw,&qres); printf("\n Reading Channel A programming word: %X Q=%d", dataw[0], qres); subad = 0; cdreg(&ext,branch,crate1,slot,subad); dataw[0]=0x0090; /* set nonlatched 128 nsec pulse */ cssa(17,ext,dataw,&qres); subad = 0; cdreg(&ext,branch,crate1,slot,subad); cssa(1,ext,dataw,&qres); printf("\n Readback Channel A programming word: %X Q=%d", dataw[0], qres); printf("\n Set trigger inhibit using 2323 GG ch b"); slot=18; /* Clear 2323 GG module */ subad=1; cdreg(&ext,branch,crate1,slot,subad); dataw[0]=0x2000; /* Set 2323 channel B into latch mode */ cssa(17,ext,dataw,&qres); cssa(25,ext,dataw,&qres); /* Start channel B inhibit */ /****************** SET UP 1st Ortec AD411 ADC ***********************/ branch = 18; crate = 4; slot = 5; virtID = 1; lld = 0x00D9; uld = 0x001F; dcOffset= 0x0080; ad114Error = initAndConfigureAD114( branch, crate, slot, virtID, lld, uld, dcOffset); if ( ad114Error != 0) { fprintf(stderr," initAndConfigureAD114 at virtID=%d declared an error\n",virtID); return 1; } /****************** SET UP 2nd Ortec AD411 ADC ***********************/ slot = 8; virtID = 2; ad114Error = initAndConfigureAD114( branch, crate, slot, virtID, lld, uld, dcOffset); if ( ad114Error != 0) { fprintf(stderr," initAndConfigureAD114 at virtID=%d declared an error\n",virtID); return 1; } /****************** SET UP 3nd Ortec AD411 ADC ***********************/ slot = 11; virtID = 3; ad114Error = initAndConfigureAD114( branch, crate, slot, virtID, lld, uld, dcOffset); if ( ad114Error != 0) { fprintf(stderr," initAndConfigureAD114 at virtID=%d declared an error\n",virtID); return 1; } /***************** SET UP the LeCroy 4302 FERA memory ********************/ printf("\n Initializing two 4302 buffer memory"); crate = 4; slot = 15; lecroy4302Error = initAndConfigLeCroy4302Memory( branch, crate, slot ); crate = 5; slot = 15; lecroy4302Error = initAndConfigLeCroy4302Memory( branch, crate, slot ); /****************** SET UP Lecroy 4300B ADC **********************/ crate=4; slot=3; pedestal=0; virtID = 5; lecroy4300Error = initAndConfigLeCroy4300ADC( branch, crate, slot, virtID, pedestal ); printf("\n Issue an initial clear to 4302 via GG ch a"); slot=18; subad = 0; cdreg(&ext,branch,crate1,slot,subad); cssa(25,ext,dataw,&qres); printf("Issue pulse on Channel A Q=%d\n", qres); usleep(1000); /* ---------------- clear the Jorway 85a quad scaler -------------------------*/ for ( i=0; i<4; i++ ) { subad = i; slot = 21; cdreg(&ext,branch,crate1,slot,subad); cssa(9,ext,dataw,&qres); } printf("\n\n***** End of setup and configuration ******\n"); /***************************************************************/ /**************** START EVENT(buffer full) LOOP ***************/ /***************************************************************/ for ( evLoop=0; evLoop= 1 ) break; /* read the ch 0 of the camac scaler */ subad = 0; slot = 21; cdreg(&ext,branch,crate1,slot,subad); cssa(0,ext,dataw,&qres); if (dataw[0] != 0 ) break; /* if ( waitCounter%100 == 0) fprintf(stderr, " Waiting on LAM, sleep for %d msec\n", iWait/1000); */ usleep(iWait); /* sleep for iWait microseconds */ } fprintf(stdout, "Found LAM status as lam=%d \n",lam); fprintf(stderr, "Found LAM status as lam=%d at waitCounter=%d\n",lam,waitCounter); lam = 0; /* get ready for next loop */ /* set trigger inhibit */ printf("\n Set trigger inhibit"); fprintf(stderr,"Set trigger inhibit\n"); slot=18; subad=1; cdreg(&ext,branch,crate1,slot,subad); /* note this module only in crate1 */ cssa(25,ext,dataw,&qres); /* Start channel B inhibit */ /* Set 4302 to CAMAC mode*/ slot=15; subad=1; cdreg(&ext,branch,crate1,slot,subad); dataw[0]=0x1; cssa(17,ext,dataw,&qres); /* Set 4302 to CAMAC mode*/ /* Read 4302 memory address */ subad=0; cdreg(&ext,branch,crate1,slot,subad); dataw[0]=0x0; cssa(1,ext,&dataw[0],&qres); if ( (dataw[0] & 0xC000) == 0xC000 ) fprintf(stderr, "4302 memory address overflow signaled on read of memory address.\n"); if ( (dataw[0] & 0xC000) == 0xC000 ) printf("\n 4302 memory address overflow signaled on read of memory address.\n"); printf("\n Current pointer for 4302 = %X %d\n",dataw[0],dataw[0]); fprintf(stderr, "Read memory pointer in 4302 = %X %d\n",dataw[0],dataw[0]); mPointer1 = dataw[0]; /* save the memory point to set DMA size */ /* Set 4302 current memory address to 0 */ subad=0; cdreg(&ext,branch,crate1,slot,subad); dataw[0]=0x0; cssa(17,ext,dataw,&qres); /* Set 4302 current memory address to 0 */ fprintf(stderr, "Set memory pointer in 4302 to 0 \n"); for ( i=0; i< 32768; i++) data16[i]=0; /* clear buffer to receive data from dma */ for ( i=0; i< 32768; i++) data16a[i]=0; /* clear buffer to receive data from dma */ /* Get Real time Clock information */ gettimeofday(&tpstart,NULL); /* timedif= MILLION*(tpend.tv_sec -tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec; */ realTimeClockLRcrate1 = tpstart.tv_sec; realTimeClockHRcrate1 = tpstart.tv_usec; /* do a dma of 4302 memory */ top1 = 0; subad=0; cdreg(&ext,branch,crate1,slot,subad); if (mPointer1 <= 16100 ) { cb[0]=mPointer1; cb[1]=0; cb[2]=0; cb[3]=0; } else { cb[0]=16100; cb[1]=0; cb[2]=0; cb[3]=0; } fprintf(stderr, "Do 1st dma with cb = %d %d %d %d\n", cb[0],cb[1],cb[2],cb[3]); csubc(0, ext, data16a, cb); printf("\n dma returns cb[1]=%d", cb[1]); fprintf(stderr, "1st dma complete cb = %d %d %d %d\n", cb[0],cb[1],cb[2],cb[3]); top1 = cb[1]; if (top1 > 16384 ) { fprintf(stderr,"Bad max value reset top1 = 16384\n"); top1 = 16384; } /* ------------------------------------------------------------------------------------------------------------------------------*/ /* read and clear the quad scaler */ int scalerValue[4]; slot = 21; for ( i=0; i<4; i++) { subad = i; cdreg(&ext,branch,crate1,slot,subad); cfsa(0,ext,dataw24,&qres); /* 24 bit read */ scalerValue[i] = dataw24[0]; } /* clear all four channels */ for ( i=0; i<4; i++) { subad = i; cdreg(&ext,branch,crate1,slot,subad); cssa(9,ext,dataw,&qres); } top = top1; if ( top1 > top2) top=top1; if ( top2 > top1) top=top2; printf("\n %d\t%x\t%d\t%x\t%d\tScalerPointer", -7, 4, 4, 4, 4); for ( i=0; i<4; i++ ) printf("\n %d\t%x\t%d\t%x\t%d\tScalerValue", i, scalerValue[i], scalerValue[i], 0, 0); printf("\n %d\t%x\t%d\tRunTyper", -6, runType, runType ); printf("\n %d\t%x\t%d\tRunNumber", -5, run, run); printf("\n %d\t%x\t%d\tRealTime LR - Offset", -4, realTimeClockLRcrate1-timeOffset, realTimeClockLRcrate1-timeOffset); printf("\n %d\t%x\t%d\tRealTime HR", -3, realTimeClockHRcrate1, realTimeClockHRcrate1); printf("\n %d\t%x\t%d\tCrateID", -2, crate1, crate1); printf("\n %d\t%x\t%d\tmPointers", -1, mPointer1, mPointer1); for ( i=0; i 16384 ) top = 16384; for ( i=0; i