/*============================================================================= Name: devWaveformBunchLengthCF.c Abs: Device support for LCLS Beam Charge 4 channel ADC module (Arcturus Coldfire 5282 processor with LTC2208 ADC) waveform records. Derived from $EPICS/base/src/rec/waveformRecord.c Auth: 16-nov-2005, Dayle Kotturi (dayle): Initial version for LLRF -----------------------------------------------------------------------------*/ #include "copyright_SLAC.h" /* SLAC copyright comments */ /*----------------------------------------------------------------------------- Mod: (newest to oldest) 11-aug-2008, Dayle Kotturi: Adapt for eth2 interface 23-jan-2007, Dayle Kotturi: Adapt for bunch length monitor =============================================================================*/ #include #include #include #include "alarm.h" #include "cvtTable.h" #include "dbDefs.h" #include "devLib.h" #include "dbAccess.h" #include "recSup.h" #include "callback.h" #include "devSup.h" #include "dbScan.h" /* for SCAN_IO_EVENT */ #include "link.h" #include "waveformRecord.h" /* for waveformRecord structure */ #include "epicsExport.h" /* for epicsExportAddress */ #include /* for DEBUGPRINT */ #include "BunchLengthCFMacros.h" #include "drvBunchLengthCF.h" /* for blmGetSampleSize, bccGetSignal, BLM_FIFO_SIZE */ /* key fields for waveformRecord epicsEnum16 ftvl; Field Type of Value short busy; Busy Indicator unsigned long nord; Number elements read void * bptr; Buffer Pointer */ typedef struct waveformBlc_s { unsigned long initialNelm; /* not useful. could be removed */ unsigned long inputType; /* raw = 0, scalar correction = 1, processed = 2 */ int channel; char filename[100]; } waveformBlc_t; typedef struct { long number; DEVSUPFUN report; DEVSUPFUN init; DEVSUPFUN init_record; DEVSUPFUN get_ioint_info; DEVSUPFUN read_write; } WFDSET; #ifdef DEBUG_PRINT int devWaveformBunchLengthCFFlag = 4; #endif int blmOverwriting = FALSE; int blmWF = 0; LOCAL long init_waveform(); LOCAL long waveform_ioinfo(); LOCAL long read_waveform(); #ifndef SCAN_IO short * blmFakeSignal(int n); short fakeSignal[BLM_FIFO_SIZE]; #endif WFDSET devWaveformBunchLengthCF = {5, NULL, NULL, init_waveform, waveform_ioinfo, read_waveform}; epicsExportAddress(dset, devWaveformBunchLengthCF); /* INFO: Generic case is to use init_waveform to: Using NELM and FTVL space for the array is allocated. The array address is stored in the record. This routine initializes SIMM with the value of SIML if SIML type is CONSTANT link or creates a channel access link if SIML type is PV_LINK. VAL is likewise initialized if SIOL is CONSTANT or PV_LINK. This routine next checks to see that device support is available and a device support read routine is defined. If either does not exist, an error message is issued and processing is terminated */ LOCAL long init_waveform(struct waveformRecord *pwaveform) { waveformBlc_t *pwaveformp; int rc; switch(pwaveform->inp.type) { case(INST_IO): /* Allocate the memory for this records private data structure */ pwaveformp = (waveformBlc_t *) malloc(sizeof(waveformBlc_t)); if (pwaveformp == NULL) { return S_dev_noMemory; } /* Tell the record about its private data structure */ pwaveform->dpvt = pwaveformp; /* The first value in "string" specifies what is to be done 0 : read the FIFO (this is the case for acquiring the raw ADC data) 1 : read from a file (this is the case for the scalar correction) */ sscanf(pwaveform->inp.value.instio.string, "%lu %d %s", &(pwaveformp->inputType), &(pwaveformp->channel), (pwaveformp->filename)); pwaveformp->initialNelm = pwaveform->nelm; pwaveform->nord = pwaveform->nelm; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devWaveformBunchLengthCFFlag, ("init_waveform %s:\n", pwaveform->name)); DEBUGPRINT(DP_INFO, devWaveformBunchLengthCFFlag, (" has instio.string: %s\n", pwaveform->inp.value.instio.string)); DEBUGPRINT(DP_INFO, devWaveformBunchLengthCFFlag, (" has inputType: %lu\n", pwaveformp->inputType)); DEBUGPRINT(DP_INFO, devWaveformBunchLengthCFFlag, (" has channel: %d\n", pwaveformp->channel)); DEBUGPRINT(DP_INFO, devWaveformBunchLengthCFFlag, (" has filename: %s\n", pwaveformp->filename)); DEBUGPRINT(DP_INFO, devWaveformBunchLengthCFFlag, (" has nelm: %lu\n", pwaveform->nelm)); DEBUGPRINT(DP_INFO, devWaveformBunchLengthCFFlag, (" has initialNelm: %lu\n", pwaveformp->initialNelm)); DEBUGPRINT(DP_INFO, devWaveformBunchLengthCFFlag, (" has nord: %lu\n", pwaveform->nord)); DEBUGPRINT(DP_INFO, devWaveformBunchLengthCFFlag, (" has val: 0x%08x\n", (unsigned int) pwaveform->val)); #endif /* Changes here. 3/24/2006. Reasons: 1) to avoid memcpy into bptr in the read (below), bptr is getting freed and set to blmSignal. 2) nelm is going to be changed on-the-fly via records so allocating bptr to an array of length NELM in iocInit no longer makes sense */ free(pwaveform->bptr); /* 11/17/2006: If data is to be read in from a file, do it now. The scalar correction arrays are examples of this. TO DO: make filenames embed the PRIM, LOCA, UNIT so that they can be distinguished by instance and co-located */ if (pwaveformp->inputType == BLM_SCALAR) { rc = blmInitialiseScalarFromFile(pwaveformp->channel, pwaveformp->filename); if (rc != OK) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, devWaveformBunchLengthCFFlag, ("init_waveform: %s: error returned by blmInitialseScalarFromFile: channel:%d, filename: %s", pwaveform->name, (int) pwaveformp->channel, pwaveformp->filename)); #endif return ERROR; } } pwaveform->udf = 0; break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, devWaveformBunchLengthCFFlag, ("init_waveform: %s: unrecognized link type: %d", pwaveform->name, (int) pwaveform->inp.type)); #endif break; } return OK; } LOCAL long waveform_ioinfo( int cmd, struct waveformRecord *pwaveform, IOSCANPVT *ppvt ) { #ifdef SCAN_IO *ppvt = blmGetIoScanPvt(); #endif return OK; } LOCAL long read_waveform(struct waveformRecord *pwaveform) { waveformBlc_t *pwaveformp; /* Facilitate access to device private structure */ pwaveformp = (waveformBlc_t *)pwaveform->dpvt; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devWaveformBunchLengthCFFlag, ("read_waveform %s:\n", pwaveform->name)); DEBUGPRINT(DP_INFO, devWaveformBunchLengthCFFlag, (" has inputType: %lu\n", pwaveformp->inputType)); DEBUGPRINT(DP_INFO, devWaveformBunchLengthCFFlag, (" has channel: %d\n", pwaveformp->channel)); #endif /* blmGetSignal returns array based on args passed in */ pwaveform->bptr = blmGetSignal(pwaveformp->channel, pwaveformp->inputType); switch (pwaveformp->inputType) { case BLM_RAW: blmWF++; pwaveform->nelm = blmGetSampleSize(pwaveformp->channel); /* If SCAN is "I/O Intr", use timestamp from EVR; otherwise leave alone if (pwaveform->scan == SCAN_IO_EVENT && pwaveform->tse == -2) { blmOverwriting = TRUE; pwaveform->time = ...; }*/ break; /* BLM_SCALAR waveform doesn't change its length after init, so don't update here */ /* BLM_PROCESSED waveform should update its length along with BLM_RAW, though - TO DO */ } pwaveform->val = pwaveform->bptr; /* from RRM: Device support must set nord when it completes. */ pwaveform->nord = pwaveform->nelm; pwaveform->udf = 0; return OK; } #ifndef SCAN_IO short * blmFakeSignal(int nelm) { int i; if (nelm > BLM_FIFO_SIZE) { nelm = BLM_FIFO_SIZE; } for (i=0; i