#include "caen1190.h" #define SLOT_ADDR 0x11300000 #define SLOT_INDX 0 #define ALMOSTFULL 1024 //#define ALMOSTFULL 4096 #define TDC_RESOLUTION 100 // Timeout time in microseconds to abandon the reading #define TIMEOUT_TIME 2000000 #define MICROSEC(tv1,tv2) \ (1e6*(tv2.tv_sec-tv1.tv_sec)+(tv2.tv_usec-tv1.tv_usec)) extern struct v1190_struct* c1190p[V1190_MAX_MODULES]; unsigned int *TDCBUF; unsigned int IPHYS,IUSER,ISIZE,IDMA; volatile unsigned tdcTime[NCHANS][MAXHITSPERCHAN] ; int caen1190_init() { int ntdc,ret; vmeOpenDefaultWindows(); //vmeCheckMutexHealth(1); // this may kill DiagGuiServer? usrVmeDmaSetMemSize(0x200000); usrVmeDmaInit(); usrVmeDmaSetConfig(2,3,0); // A32,MBLT usrVmeDmaMemory(&IPHYS, &IUSER, &ISIZE); // some sort of address bounday conditions, maybe not necessary: IDMA = IUSER; IDMA = IDMA & 0xFFFFFFF0; IDMA = IDMA + 0x10; TDCBUF = (unsigned int*)IDMA; ntdc=tdc1190Init(SLOT_ADDR,0x00000,1,0); printf("Init %d: %d\n",SLOT_INDX,ntdc); // probably not necssary, but might as well: ret=tdc1190Reset(SLOT_INDX); printf("Reset: %d\n",ret); // this reads from $CLON_PARMS/tdc1190/hostname.cnf: if (ntdc) tdc1190Config(""); ret=tdc1190SetContinuousStorageMode(SLOT_INDX); printf("Set Continuous Storage %d: %d\n",SLOT_INDX,ret); ret=tdc1190ReadAcquisitionMode(SLOT_INDX); printf("Read Acquisition Mode %d: %d\n",SLOT_INDX,ret); ret=tdc1190SetEdgeResolution(SLOT_INDX,TDC_RESOLUTION); printf("Set Edge Resolution: %d: %d\n",SLOT_INDX,ret); ret=tdc1190GetEdgeResolution(SLOT_INDX); printf("Get Edge Resolution: %d: %d\n",SLOT_INDX,ret); ret=tdc1190SetEdgeDetectionConfig(SLOT_INDX,2); printf("Set Edge Detection: %d: %d\n",SLOT_INDX,ret); ret=tdc1190ReadEdgeDetectionConfig(SLOT_INDX); printf("Get Edge Detection: %d: %d\n",SLOT_INDX,ret); ret=tdc1190SetAlmostFullLevel(SLOT_INDX,ALMOSTFULL); printf("Set Almost Full: %d: %d\n",SLOT_INDX,ret); ret=tdc1190GetAlmostFullLevel(SLOT_INDX); printf("Get Almost Full: %d: %d\n",SLOT_INDX,ret); if (ret!=ALMOSTFULL) { printf("Error setting almostfull."); return 1; } printf("caen1190 Initialization complete.\n\n"); return 0; //ret=tdc1190SetMaxNumberOfHitsPerEvent(SLOT_INDX,128); //ret=tdc1190GetMaxNumberOfHitsPerEvent(SLOT_INDX); //printf("Read Max Hits Per Event %d: %d\n",SLOT_INDX,ret); //tdc1190SetWindowWidth(SLOT_INDX,51175); //printf("Set Window Width %d: %d\n",SLOT_INDX,ret); //ret=tdc1190GetWindowWidth(SLOT_INDX); //printf("Set Window Width %d: %d\n",SLOT_INDX,ret); //tdc1190SetWindowOffset(SLOT_INDX,0); //ret=tdc1190EnableTDCErrorBypass(SLOT_INDX); //printf("Set Error Bypass %d: %d\n",SLOT_INDX,ret); //ret=tdc1190DisableChannel(SLOT_INDX,0); //printf("Set Disable Channel %d: %d\n",SLOT_INDX,ret); //ret=tdc1190EnableChannel(SLOT_INDX,0); //printf("Set Enable Channel %d: %d\n",SLOT_INDX,ret); // DREADY works, but don't need it, use AlmostFull //statReg = vmeRead16(&(c1190p[SLOT_INDX]->status)); //if (statReg & V1190_STATUS_DATA_READY) //tdc1190Status(SLOT_INDX); //tdc1190Mon(SLOT_INDX); } int caen1190_readOne(unsigned int *data,unsigned int *len,unsigned int *complete) { int jj,ii,ret,itdc; unsigned int word,chan,tdc,zeroes,trailing; unsigned int tdc_prev[NCHANS]; unsigned int ready=0; struct timeval tv_start,tv_now; unsigned int rolledOver = 0; unsigned channelShift = 32; // initialize status counters: int errors=0,fulls=0; // initialize times and output lengths: gettimeofday(&tv_start,NULL); for (jj=0; jj TIMEOUT_TIME) { printf("Waited too long, returning \n"); return errors+fulls; } // clear if 1190's buffer is full: if (tdc1190StatusFull(SLOT_INDX) > 0) { fulls++; ret=tdc1190Clear(SLOT_INDX); printf ("ReadOne got buffer Full, Stop reading and clearing\n"); break; } // read data if 1190's buffer is almost full: if (tdc1190StatusAlmostFull(SLOT_INDX) > 0) { vmeBusLock(); if (usrVme2MemDmaStart(SLOT_ADDR, (unsigned int)&TDCBUF[0], ALMOSTFULL*4) < 0) { errors++; printf("DMA ERROR: %d",ret); continue; } if (tdc1190ReadBoardDmaDone(SLOT_INDX) != ALMOSTFULL) { errors++; printf("DMA READ BYTES ERROR: %d/%d\n",ALMOSTFULL,ret); continue; } vmeBusUnlock(); for (jj=0; (jj>19) & 0x7F; tdc = (word) & 0x7FFFF; zeroes = (word>>27) & 0x1F; trailing = (word>>26) & 1; if (zeroes!=0) { // this can be due to filler words continue; } itdc = chan - channelShift; if( (channelShift + NCHANS -1 < chan) || chan < channelShift ) { continue; } // check for clock rollover: if (tdc_prev[itdc] > tdc) { // printf( "Rollover for channel %d , previous %d, current %d \n", itdc, tdc_prev[itdc], tdc ); rolledOver = 1; } // printf( "Check for channel %d , previous %d, current %d , Rollover is %d \n", itdc, tdc_prev[itdc], tdc, rolledOver ); // store in output buffer if ready and not complete: if ( ! rolledOver ) { // printf("Saving %d for %d channel in position %d \n", tdc, itdc, itdc*MAXHITSPERCHAN + tdc ); *(data + itdc*MAXHITSPERCHAN + tdc) = 1; } tdc_prev[itdc]=tdc; } } } printf("Exiting Read one\n"); return errors+fulls; } int caen1190_readDifferenceWithRF(float *histo, float* histo_short, int short_size, double short_scale ) { int dt, dt_short, jj,ii,ret,itdc; unsigned int word,chan,tdc,zeroes,trailing; unsigned int tdc_prev[NCHANS]; struct timeval tv_t0[NCHANS]; struct timeval tv_start,tv_now; // Number of the TDC assigned to RF int rfTDCNumber = 18; int tdcNumber, hitNumber, rfHitNumber, binNumber; unsigned hitNumberInBuffer[NCHANS]; unsigned rolledOver = 0; unsigned timedOut = 0; unsigned absMinimumTime; int minimumTime; int minRfIndex = 0; unsigned channelShift = 32; // initialize status counters: int errors=0,fulls=0,gaps=0; // initialize times and output lengths: gettimeofday(&tv_start,NULL); for (jj=0; jj TIMEOUT_TIME) { printf( "Timed out in RF difference \n"); timedOut = 1; } // clear if 1190's buffer is full: if (tdc1190StatusFull(SLOT_INDX) > 0) { fulls++; printf ("ReadDeltaRF got buffer Full, Stop reading and clearing\n"); ret=tdc1190Clear(SLOT_INDX); continue; } // read data if 1190's buffer is almost full: if (tdc1190StatusAlmostFull(SLOT_INDX) > 0) { vmeBusLock(); if (usrVme2MemDmaStart(SLOT_ADDR, (unsigned int)&TDCBUF[0], ALMOSTFULL*4) < 0) { errors++; printf("DMA ERROR: %d",ret); continue; } if (tdc1190ReadBoardDmaDone(SLOT_INDX) != ALMOSTFULL) { errors++; printf("DMA READ BYTES ERROR: %d/%d\n",ALMOSTFULL,ret); continue; } vmeBusUnlock(); for( tdcNumber=0; tdcNumber>19) & 0x7F; tdc = (word) & 0x7FFFF; zeroes = (word>>27) & 0x1F; trailing = (word>>26) & 1; if (zeroes!=0) { // this can be due to filler words continue; } itdc = chan - channelShift; if( (channelShift + NCHANS -1 < chan) || chan < channelShift ) { continue; } // check for clock rollover: if (tdc_prev[itdc] > tdc) { // printf( "Rollover for channel %d , previous %d, current %d \n", itdc, tdc_prev[itdc], tdc ); rolledOver = 1; for (jj=0; jj= 0 ) && (dt 0 ) { // dt = tdcTime[tdcNumber][hitNumber] - tdcTime[rfTDCNumber][0]; // } // binNumber = dt; // if( binNumber>=0 && binNumber < MAXHITSPERCHAN ) (*(histo+tdcNumber*MAXHITSPERCHAN + binNumber))++; dt_short = (int)fmod( dt , short_scale); // printf( "Short time from %d wrt %f is %d\n", dt, short_scale, dt_short); if( dt_short >=0 && dt_short < short_size ) (*(histo_short+tdcNumber*short_size + dt_short))++; } } } // for( tdcNumber = 0; tdcNumber < NCHANS; tdcNumber++ ) { // printf("Length for TDC %d is %d \n", tdcNumber, len[tdcNumber]); // } } printf("Exiting Read RF Differences \n"); return errors+fulls; } /* int main(int argc,char *argv[]) { unsigned int ii,jj; unsigned int data[NCHANS][MAXHITSPERCHAN]; unsigned int len[NCHANS]; caen1190_init(); printf("READING ...\n"); caen1190_read((unsigned int*)data,len); for (ii=0; ii