/************************************************************************* * * vme_list.c - Library of routines for readout and buffering of * events using a JLAB Trigger Interface V3 (TI) with * a Linux VME controller. * */ /* Event Buffer definitions */ #define MAX_EVENT_POOL 20 #define MAX_EVENT_LENGTH 2046*64 /* Size in Bytes */ /* Define Interrupt source and address */ #define TI_MASTER #define TI_READOUT TI_READOUT_EXT_POLL /* Poll for available data, external triggers */ #define TI_ADDR 0 #define TI_DATA_READOUT /* Decision on whether or not to readout the TI for each block - Comment out to disable readout */ // #define FIBER_LATENCY_OFFSET 0xC7 #define FIBER_LATENCY_OFFSET 0x4A #include "dmaBankTools.h" #include "tiprimary_list.c" /* source required for CODA */ #include "conf_utils.h" #include "conf_utils.c" int NFADC; int FA_SLOT; #include "fa250_init.c" extern int fadcA32Base; #define BLOCKLEVEL 1 #define BUFFERLEVEL 1 bd_type board_type = bd_unknown; int use_playback = 0; void rocTrigger(int arg); /**************************************** * DOWNLOAD ****************************************/ void rocDownload() { int rc = -1; int stat; int opt_port, port; memset(fa250, 0, sizeof(fa250)); memset(&ti_bd, 0, sizeof(ti_bd)); gethostname(roc_name,ROCLEN); /* Global config file */ strcpy(filename,rol->usrConfig); printf(BOLDMAGENTA " Global Config File Name %s \n" RESET ,filename); rc = read_conf_global(filename,roc_name); if(rc != 0){ printf(BOLDRED " FATAL: Fail to read global configuration file %d \n" RESET, rc); daLogMsg(" FATAL: Fail to read global configuration file"); return; } rc = read_conf_common(roc_name); if(rc != 0){ printf(BOLDRED " FATAL: Fail to read file with crate specific parameters %d \n" RESET, rc); daLogMsg(" FATAL: Fail to read common configuration file"); return; } rc = read_conf_user(roc_name); if(rc != 0){ printf(BOLDRED " FATAL: Fail to read user config file %d \n" RESET, rc); daLogMsg(" FATAL: Fail to read user configuration file"); return; } board_type = get_board_type(roc_name); /* 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); fadcA32Base=0x09000000; blockLevel = BLOCKLEVEL; print_ti_conf(); /***************** * TI SETUP *****************/ /* * Set Trigger source * For the TI-Master, valid sources: * TI_TRIGGER_FPTRG 2 Front Panel "TRG" Input * TI_TRIGGER_TSINPUTS 3 Front Panel "TS" Inputs * TI_TRIGGER_TSREV2 4 Ribbon cable from Legacy TS module * TI_TRIGGER_PULSER 5 TI Internal Pulser (Fixed rate and/or random) */ int overall_offset=0x80; // Disable Soft and Random triggers tiDisableRandomTrigger(); tiSoftTrig(1,0,700,0); tiSoftTrig(2,0,700,0); /* Pulser */ if(ti_bd.ti_master_trig == 1){ tiSetTriggerSource(TI_TRIGGER_PULSER); /* External Front Pannel */ } else if(ti_bd.ti_master_trig == 2){ tiSetTriggerSource(TI_TRIGGER_TSINPUTS); /* Play Back */ } else if(ti_bd.ti_master_trig == 3){ // tiSetTriggerSource(TI_TRIGGER_PULSER | TI_TRIGGER_TSINPUTS); printf(BOLDRED " ENABLE TRIGEGR SOURCES \n" RESET); tiSetTriggerSource(TI_TRIGGER_TSINPUTS); use_playback = 1; } else { printf(BOLDRED " Unknown trigger type %d \n", ti_bd.ti_master_trig); return(-2); // Return will be replaced by a function provided by Dave A. } /* Enable/Disable specific inputs */ if( ((ti_bd.ti_master_trig == 2) || (ti_bd.ti_master_trig == 3)) && ti_bd.ti_fp_inputs != 0){ tiEnableTSInput( ti_bd.ti_fp_inputs ); printf(BOLDBLUE "Enable TI_FP_INPUTS = " RESET); for(port = 0; port < 16; port++){ if( ti_bd.ti_fp_inputs & (1 << port)) printf(" %d", port+1); } printf("\n"); printf(" 0x%x \n",ti_bd.ti_fp_inputs); } /* Load the trigger table that associates pins 21/22 | 23/24 | 25/26 : trigger1 pins 29/30 | 31/32 | 33/34 : trigger2 */ tiLoadTriggerTable(2); // tiSetTriggerHoldoff(1,40,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); /* Set number of events per block */ blockLevel = BLOCKLEVEL; tiSetBlockLevel(blockLevel); /* Set Trigger Buffer Level */ tiSetBlockBufferLevel(BUFFERLEVEL); /* Set timestamp format 48 bits */ tiSetEventFormat(3); for(opt_port = 2; opt_port < 7; opt_port++){ if(ti_bd.ti_fiber_en & (1 << (opt_port - 1))){ tiAddSlave(opt_port); printf(BOLDBLUE "TI Add Slave for optical port = %d \n" RESET, opt_port); } } // tiAddSlave(1); // tiAddSlave(2); // tiAddSlave(3); // tiAddSlave(4); // tiAddSlave(5); tiStatus(0); if(board_type == fadc250) rc = fa250_download(); if(use_playback == 1){ unsigned short adc_playback[32]; unsigned short adc_playback_allchannels[512]; int sample, ichan; int slot; for(slot = 0;slot < NFADC; slot++){ FA_SLOT = fadcID[slot]; if( FA_SLOT == 10){ for(sample = 0; sample < 32; sample++){ adc_playback[sample] = 200; } adc_playback[1] = 1200; adc_playback[2] = 1200; adc_playback[3] = 1200; adc_playback[4] = 1200; adc_playback[5] = 1200; adc_playback[6] = 1200; for(ichan = 0; ichan < 16; ichan++) { for(sample = 0; sample < 32; sample++) { adc_playback_allchannels[ichan*32+sample] = adc_playback[sample]; // if(sample-ichan > 0) // adc_playback_allchannels[ichan*32+sample] = adc_playback[sample-ichan]; // else // adc_playback_allchannels[ichan*32+sample] = adc_playback[0]; } } faSetPPG(FA_SLOT,0,adc_playback_allchannels,512); taskDelay(5); int jj = 0; for(jj = 0; jj < 512; jj++){ printf(" SSS = %d \n", adc_playback_allchannels[jj]); } } else { // Other boards memset(adc_playback_allchannels, 0, sizeof(adc_playback_allchannels)); faSetPPG(FA_SLOT,0,adc_playback_allchannels,512); taskDelay(5); } } } // Use playback if(board_type == fadc250){ ctpInit(1); ctpSetFinalSumThreshold(20,0); printf(" CTP final sum threshold %d \n",ctpGetFinalSumThreshold()); // Single crate cosmic test // unsigned int ctp_vmemask = (1<<9) | (1<<10); unsigned int ctp_vmemask = fadcSlotMask; printf(" TEST TEST TEST 0x%x \n",fadcSlotMask); /* BCAL cosmic settings */ ctpSetBCALWindowWidth(20); ctpSetBCALThreshold(400); ctpSetVmeSlotEnableMask(ctp_vmemask); printf("----------------------------- \n"); ctpStatus(); printf("----------------------------- \n"); int iwait = 0; int allchanup = 0; while(allchanup != (0x7) ) { iwait++; allchanup = ctpGetAllChanUp(); if(iwait>1000) { printf("iwait timeout allchup - 0x%x\n",allchanup); break; } } } printf("rocDownload: User Download Executed\n"); } /**************************************** * PRESTART ****************************************/ void rocPrestart() { unsigned short iflag; int stat; int islot; int rc = -1; /* Set number of events per block */ tiSetBlockLevel(blockLevel); printf(BOLDBLUE "rocPrestart: Block Level set to %d\n" RESET,blockLevel); if(board_type == fadc250){ rc = fa250_prestart(); for(islot = 0;islot < NFADC;islot++) { FA_SLOT = fadcID[islot]; faDisable(FA_SLOT,0); faChanDisable(FA_SLOT,0xffff); faSetMGTTestMode(FA_SLOT,0); faEnable(FA_SLOT,0,0); faStatus(FA_SLOT,0); } ctpAlignAtSyncReset(1); ctpStatus(0xFFFF); ctpResetScalers(); printf("CTP sync counter = %d\n",ctpGetSyncScaler()); } tiStatus(1); sdStatus(); printf("rocPrestart: User Prestart Executed\n"); /* SYNC is issued after this routine is executed */ } /**************************************** * GO ****************************************/ void rocGo() { int iwait = 0; int allchanup = 0; int islot; tiStatus(1); if(board_type == fadc250){ for(islot = 0;islot < NFADC;islot++) { FA_SLOT = faSlot(islot); faChanDisable(FA_SLOT,0x0); faSetMGTTestMode(FA_SLOT,1); } ctpAlignAtSyncReset(0); ctpGetAlignmentStatus(1,10); } if(use_playback){ for(islot = 0;islot < NFADC;islot++) { FA_SLOT = fadcID[islot]; faPPGEnable(FA_SLOT); } } /* Enable modules, if needed, here */ printf(" NUMBER OF SOFT TRIGEGRS = 40 \n"); /* Enable Soft Trigger */ if(ti_bd.ti_master_trig == 1 || ti_bd.ti_master_trig == 3 ){ tiSoftTrig(ti_bd.ti_soft_trig[0], ti_bd.ti_soft_trig[1], ti_bd.ti_soft_trig[2], ti_bd.ti_soft_trig[3]); // tiSoftTrig(2, 10000, 0x7FFF, 1); printf(BOLDBLUE " SOFT TRIGGER ENABLED %d %d %d %d \n" RESET, ti_bd.ti_soft_trig[0], ti_bd.ti_soft_trig[1], ti_bd.ti_soft_trig[2], ti_bd.ti_soft_trig[3]); } } /**************************************** * END ****************************************/ void rocEnd() { int rc = -1; if(board_type == fadc250) rc = fa250_end(); /* Disable Fixed Rate trigger */ if(ti_bd.ti_master_trig == 1 ){ tiSoftTrig(1,0,700,0); } if(use_playback){ int islot; for(islot = 0;islot < NFADC;islot++) { FA_SLOT = fadcID[islot]; faPPGDisable(FA_SLOT); } } sdStatus(); tiStatus(0); printf("rocEnd: Ended after %d blocks\n",tiGetIntCount()); } /**************************************** * TRIGGER ****************************************/ void rocTrigger(int arg) { int ii, islot; int stat = 0, dCnt, len = 0, idata; int itime, gbready; int roflag=1; unsigned int val; unsigned int *start; // tiStatus(1); // printf(" Inside rocTrigger \n"); /* Set TI output 1 high for diagnostics */ tiSetOutputPort(1,0,0,0); // vmeDmaConfig(2,3,0); /* Readout the trigger block from the TI Trigger Block MUST be reaodut first */ dCnt = tiReadTriggerBlock(dma_dabufp); if(dCnt<=0) { printf("No TI Trigger data or error. dCnt = %d\n",dCnt); } else { /* TI Data is already in a bank structure. Bump the pointer */ dma_dabufp += dCnt; printf("Read TI block %d\n",dCnt); } // printf(" Before reading out FADC \n"); printf("Sascha %d \n",blockLevel); printf("Sascha %d \n",blockLevel); BANKOPEN(6,BT_UI4,blockLevel); /* Readout FADC */ /* Configure Block Type... temp fix for 2eSST trouble with token passing */ if(NFADC!=0) { FA_SLOT = fadcID[0]; for(itime=0;itime<100;itime++) { gbready = faGBready(); stat = (gbready == fadcSlotMask); if (stat>0) { break; } } if(stat>0) { if(NFADC>1) roflag=2; /* Use token passing scheme to readout all modules */ // printf("Read out FADC250 \n"); dCnt = faReadBlock(FA_SLOT,dma_dabufp,MAXFADCWORDS,roflag); printf(" FADC words %d\n",dCnt); if(dCnt<=0) { printf("FADC%d: No data or error. dCnt = %d\n",FA_SLOT,dCnt); } else { dma_dabufp += dCnt; } } else { printf ("FADC%d: no events stat=%d intcount = %d gbready = 0x%08x fadcSlotMask = 0x%08x\n", FA_SLOT,stat,tiGetIntCount(),gbready,fadcSlotMask); } /* Reset the Token */ if(roflag==2) { for(islot=0; islot