/* mcTest.c * * Reads MC data from disk file and ships it to the EB * * EJW, 7-Jan-2014 * */ /* Event Buffer definitions */ #define MAX_EVENT_POOL 10 #define MAX_EVENT_LENGTH 1024*2000 /* Size in Bytes */ /* Define Interrupt source and address */ #define TI_MASTER #define TI_READOUT TI_READOUT_EXT_POLL /* Poll for available data, from TS link */ #define TI_ADDR (21<<19) /* GEO slot 21 */ // Decision on whether or not to readout the TI for each block // Comment out to disable readout #define TI_DATA_READOUT // for TI #define FIBER_LATENCY_OFFSET 0x4A /* measured longest fiber length */ // misc includes #include "dmaBankTools.h" #include "tiprimary_list.c" /* source required for CODA */ #include "evio.h" /* Global Blocklevel (Number of events per block) */ #define BLOCKLEVEL 1 #define BUFFERLEVEL 10 /* Redefine tsCrate according to TI_MASTER or TI_SLAVE */ #ifdef TI_SLAVE int tsCrate=0; #else #ifdef TI_MASTER int tsCrate=1; #endif #endif // evio file name, handle, etc static char *evioFileName = "/dev/shm/rocData.evio"; static int handle; #define MCBUFSIZE 10000 static uint32_t mcbuffer[MCBUFSIZE]; // defined someplace else extern unsigned int tiTriggerSource; extern DMANODE *the_event; // prototypes void rocTrigger(int arg); /*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/ /**************************************** * DOWNLOAD ****************************************/ void rocDownload() { blockLevel = BLOCKLEVEL; /***************** * TI SETUP *****************/ int overall_offset=0x80; #ifndef TI_DATA_READOUT /* Disable data readout */ tiDisableDataReadout(); /* Disable A32... where that data would have been stored on the TI */ tiDisableA32(); #endif /* Set crate ID */ tiSetCrateID(0x01); /* ROC 1 */ /* Set Trigger source to TS fiber input */ // tiSetTriggerSource(TI_TRIGGER_TSINPUTS); tiSetTriggerSource(TI_TRIGGER_PULSER); /* Set needed TS input bits */ tiEnableTSInput( TI_TSINPUT_1 ); /* Load the trigger table that associates pins 21/22 | 23/24 | 25/26 : trigger1 pins 29/30 | 31/32 | 33/34 : trigger2 */ tiLoadTriggerTable(0); tiSetTriggerHoldoff(1,10,0); tiSetTriggerHoldoff(2,10,0); /* /\* Set the sync delay width to 0x40*32 = 2.048us *\/ */ tiSetSyncDelayWidth(0x54, 0x40, 1); /* Set the busy source to non-default value (no Switch Slot B busy) */ /* tiSetBusySource(TI_BUSY_LOOPBACK,1); */ /* tiSetFiberDelay(10,0xcf); */ /* Set number of events per block */ tiSetBlockLevel(BLOCKLEVEL); /* Set timestamp format 48 bits */ tiSetEventFormat(3); tiSetBlockBufferLevel(BUFFERLEVEL); tiStatus(); printf("rocDownload: User Download Executed\n"); } /*----------------------------------------------------------------------------*/ /**************************************** * PRESTART ****************************************/ void rocPrestart() { /* TI Status */ tiStatus(); /* Unlock the VME Mutex */ vmeBusUnlock(); // open evio file if(evOpen(evioFileName,"r",&handle)!=S_SUCCESS) { printf("?unable to open file %s\n",evioFileName); } else { printf("successfully opened file %s\n",evioFileName); } printf("rocPrestart: Block Level set to %d\n",blockLevel); printf("rocPrestart: User Prestart Executed\n"); } /*----------------------------------------------------------------------------*/ /**************************************** * GO ****************************************/ void rocGo() { tiSetRandomTrigger(1,15); /* (trigger type, prescale factor) physics or playback trigger, prescale factor is exponent of dividing by 2 */ } /*----------------------------------------------------------------------------*/ /**************************************** * END ****************************************/ void rocEnd() { // close evio file evClose(handle); tiSetRandomTrigger(1,0); tiStatus(); printf("rocEnd: Ended after %d blocks\n",tiGetIntCount()); } /*----------------------------------------------------------------------------*/ /**************************************** * TRIGGER ****************************************/ void rocTrigger(int arg) { int dCnt,stat,nbytesLeft,nword; static unsigned int roEventNumber=0; roEventNumber++; syncFlag = tiGetSyncEventFlag(); if(tiGetSyncEventReceived()) { printf("%s: Sync Event received at readout event %d\n", __FUNCTION__,roEventNumber); } if(syncFlag) { printf("%s: Sync Flag Received at readout event %d\n", __FUNCTION__,roEventNumber); /* printf(" Sleeping for 10 seconds... \n"); */ /* sleep(10); */ /* printf(" ... Done\n"); */ } /* Set high, the first output port */ tiSetOutputPort(1,0,0,0); #ifdef TI_DATA_READOUT vmeDmaConfig(2,5,1); dCnt = tiReadTriggerBlock(dma_dabufp,8+(3*BLOCKLEVEL),1); /*MMD: Aparently this is an important differnce in that it make sure that the ti readout is formatted correctly*/ if(dCnt<=0) { printf("No data or error. dCnt = %d\n",dCnt); } else { /* Create the proper trigger bank */ dma_dabufp += dCnt; } #endif // open data bank BANKOPEN(3,BT_UI4,BLOCKLEVEL); // determine how much space is left in the output buffer nbytesLeft = the_event->part->size - sizeof(DMANODE); nbytesLeft = nbytesLeft - ((long)dma_dabufp - (long)&(the_event->length)); // read mc data from file, note that data is little-endian stat=evRead(handle,mcbuffer,MCBUFSIZE); printf("first mc data word in local buffer: 0x%x\n",mcbuffer[2]); if(stat!=S_SUCCESS) { evClose(handle); if(evOpen(evioFileName,"r",&handle)!=S_SUCCESS) { printf("?unable to reopen file %s\n",evioFileName); } else { printf("successfully reopened file %s\n",evioFileName); stat=evRead(handle,mcbuffer,MCBUFSIZE); } } nword = mcbuffer[0]-1; // now done in evioSplitRoc, ejw, 9-jan-2014 // convert data to big endian // swap_int32_t(&(mcbuffer[2]),nword, NULL); // copy from local to output buffer memcpy((char*)dma_dabufp,(char*)&(mcbuffer[2]),nword*sizeof(uint32_t)); // update word count if(stat==S_SUCCESS) { dCnt=nword; printf("dCnt: %i\n\n",dCnt); dma_dabufp += dCnt; } // close data bank BANKCLOSE; /* Turn off all output ports */ tiSetOutputPort(0,0,0,0); } /*----------------------------------------------------------------------------*/ /* * rocCleanup * - Routine called just before the library is unloaded. */ void rocCleanup() { remexClose(); } /*----------------------------------------------------------------------------*/