/* * testDataClass.cpp * * Created on: Nov 8, 2017 * Author: hovanes */ #include #include #include #include #include #include #include using namespace std; extern "C" { #include "jvme.h" #include "vetrocLib.h" } #include "VetrocBlockData.hh" #define VETROC_SLOT 2 /* global variables */ extern int nvetroc; /* Number of VETROCs in Crate */ static unsigned int vetrocSlotMask; //static unsigned int MAXVETROCWORDS = 10000; //static unsigned int MAXVETROCWORDS = 46; int block_level = 1; //double pulseFrequency = 10.0; extern double pulseFrequency ; double dutyCycle = 0.5; double coincidenceWindow = 31; //int block_level = 255; int read_stat = 0; //unsigned int *tdcbuf; extern DMANODE *the_event; extern unsigned int *dma_dabufp; //struct vetroc_data_struct vetroc_data; unsigned long NumberOfEvents = 20000; DMA_MEM_ID vetrocVME_IN; DMA_MEM_ID vetrocVME_OUT; void usage( int ix ); void setup(); void createTrigger(); VetrocBlockData* getData(); int main( int argc, char *argv[] ) { int vetroc_addr, iflag, nvetroc; int narg; printf( "argc %d \n", argc ); if ( argc < 2 ) { usage( 0 ); exit( 0 ); } narg = atoi( argv[1] ); printf( "narg = %d \n", narg ); if ( narg < 1 || narg > 3 ) { usage( 1 ); exit( 0 ); } vmeOpenDefaultWindows(); vmeBusLock(); if ( narg >= 1 ) setup(); for ( unsigned iEvent = 0; iEvent < NumberOfEvents; iEvent++ ) { if ( iEvent % 1000 == 0 ) { cout << "Working on readout event number " << dec << iEvent << endl; } if ( narg >= 2 ) createTrigger(); if ( narg == 3 ) { VetrocBlockData* data = getData(); delete data; } } dmaPFreeAll(); vmeBusUnlock(); vmeCloseDefaultWindows(); return 0; } void usage( int iwhich ) { if ( iwhich == 0 ) printf( "Need 1 arg, which tells what to do \n" ); if ( iwhich == 1 ) printf( "Arg must be within a range \n" ); printf( " usage: ./vetrocRatetest N \n" ); printf( " N >= 1, initialize then exit \n" ); printf( " N >= 2, look for a trigger \n" ); printf( " N = 3, read data into a buffer and print it \n" ); } void setupDMA() { vetrocVME_IN = dmaPCreate( "vmeIN", 40240, 500, 0 ); vetrocVME_OUT = dmaPCreate( "vmeOUT", 0, 0, 0 ); dmaPReInitAll(); dmaPStatsAll(); vmeDmaConfig( 2, 5, 1 ); /* Optimal DMA configuration for the FADC250 */ } void setup() { int vetroc_addr, iflag, ii; vetroc_addr = (VETROC_SLOT << 19); /* 0 = software synch-reset, FP input 1, internal clock */ iflag = 0x00; nvetroc = 0; nvetroc = vetrocInit( (VETROC_SLOT << 19), 0x80000, 1, iflag ); if ( nvetroc <= 0 ) { printf( "ERROR: no VETROC. Bye ! \n" ); return; } vetrocGSetProcMode( /*4000*/2000,/*4000*/2000 ); /* Maybe ? pick one of these 2 ? */ /* vetrocEnableMultiBlock(1); */ /* vetrocDisableMultiBlock(); */ /* Construct SlotMask Here you'd want to loop over slots if you had more than 1 slot, using vetrocSlot(index) to find slot numbers */ vetrocSlotMask = 0; vetrocSlotMask |= (1 << VETROC_SLOT); /* Add it to the mask */ printf( "vetrocSlotMask=0x%08x\n", vetrocSlotMask ); vetrocSetBlockLevel( VETROC_SLOT, block_level ); vetrocTriggerPulseWidth( VETROC_SLOT, 8000 ); vetrocLinkReset( VETROC_SLOT ); vetrocClear( VETROC_SLOT ); vetrocPulserSetup( VETROC_SLOT, pulseFrequency, dutyCycle, 0xffffffff ); vetrocSetTrigSource( VETROC_SLOT, VETROC_SD_MUX_PULSER ); vetrocSetCoincidenceWindow( VETROC_SLOT, coincidenceWindow ); setupDMA(); return; } void createTrigger() { // First trigger the board // vetrocTrig( VETROC_SLOT ); /* see if there are any triggers */ /* there wont be unless we create them */ int itime, gbready, i; read_stat = 0; for ( itime = 0; itime < 100000; itime++ ) { gbready = vetrocGBready(); read_stat = (gbready == vetrocSlotMask); /* printf("expected mask 0x%08x, got 0x%08x\n",vetrocSlotMask,gbready); */ if ( read_stat > 0 ) { // printf( "expected mask 0x%08x, got 0x%08x\n", vetrocSlotMask, gbready ); break; } } // if ( read_stat != 0 ) printf( "vetroc IS ready !!!\n" ); if ( read_stat == 0 ) printf( "vetroc not ready !!!\n" ); return; } VetrocBlockData* getData() { // cout << "Getting Event" << endl; unsigned int event_number = 1; // cout << "Preparing input queue" << endl; GETEVENT( vetrocVME_IN, event_number ); // cout << "Input queue ready" << endl; int len, i; if ( read_stat <= 0 ) { printf( "No data. Bye ! \n" ); return 0; } // cout << "Initiating DMA readout" << endl; len = vetrocReadBlock( VETROC_SLOT, dma_dabufp, 10000, 1 ); dma_dabufp += len; PUTEVENT( vetrocVME_OUT ); DMANODE *outEvent = dmaPGetItem( vetrocVME_OUT ); len = (outEvent->length); // if ( len != 8 ) { // cout << "The length read from the board is " << len << " words" << endl; // } // cout << "The length read from the board is " << dec << len << " words" << endl; if ( len == 0 ) printf( "VETROC: no data or error. Bye ! \n" ); // cout << "Creating data with length " << dec << len << endl; VetrocBlockData* vetrocEvent = new VetrocBlockData( len, outEvent->data ); dmaPFreeItem( outEvent ); return vetrocEvent; }