/*============================================================================= Name: drvPad.c Abs: Driver support for LCLS Phase and Amplitude Detector PAD module (Arcturus Coldfire 5282 processor with LTC2208 ADC) Refs: See list of docs under the heading "" on page: http://www.slac.stanford.edu/grp/lcls/controls/global/subsystems/llrf Compiler Flags: DEBUGPRINT - SCANIO - should only be undefined if you use ASYN to scan I/O Intr via callbacks Mod: 15-Jul-11 1. Change 'chan->avgS not within accepted range' to 'chan->avgOut not within accepted range' in the function padSimpReadSignal() 03-may-2011, Shantha Condamoor (scondam): 1. Added functions to support PEDAVG pv. This is the pedestal or threshold of avgOut for each of the 4 channels set by the user. When avgOut is below this threshold, fault signal is not generated. Useful during RF power ramping. 29-mar-2011, Shantha Condamoor (scondam): 1. Added prev_intrCount to aid in calculating the Trigger rate. 2. Add the following reset functions: padResetIntrCount() padResetScanIOCount() padResetOverflowICount() padResetState() 02-mar-2010, Shantha Condamoor (scondam): Initial version for Klystron Test Station 01 Interlocking with PAD. 1. Completely scratched out the existing code as it was causing troubles in sampling more than 220 points at higher trigger rates. IOC was hanging up. New code base is derived from the pad4App package and has been modified extensively for the TS01 test setup. This code has been tested and found to be capable of handling upto 512 sampling points at 60 Hz. 2. Added the scalar variables for scaling down raw averages. 28-jan-2010, Shantha Condamoor Modified to setup special test for TS01 in Klystron Lab. This test is described in the KLY_TS01_PAD_User_Manual.doc in TS01 windows machine in T:/Pad Information directory: 1. GPT PA0 (Pin 24) configured as an output pin to indicate the status. status = NORMAL (1) when ADC pulse value is within an expected range. = FAULT (0) when it exceed the range. 2. Added more members to rf_channel_s structure. 3. Added the compiler option 'PAD_SIMPLE_PROCESSING. It is used for compiling in the code specific to the Klystron Test Station TS01 test. 4. Added dump functions for Average Out, Faulting channel statistics etc. 5. Added Get and Set functions for the new PVs. 6. Added code in the ioc init function to initialize all the newly added data variables. Auth: 16-nov-2005, Dayle Kotturi (dayle): -----------------------------------------------------------------------------*/ #include "copyright_SLAC.h" /* SLAC copyright comments */ /*----------------------------------------------------------------------------- Mod: (newest to oldest) DD-MMM-YYYY, My Name: Changed such and such to so and so. etc. etc. DD-MMM-YYYY, Your Name: More changes ... The ordering of the revision history should be such that the NEWEST changes are at the HEAD of the list. =============================================================================*/ #include /* for printf */ #include /* for malloc */ #include /* for memcpy */ #include /* for errno */ #include /* for sqrt */ #include /* for strerror */ #include #ifdef USING_TIMER #include /* for benchmark_timer_initialize(), benchmark_timer_read() */ #endif #include /* for IOSCANPVT */ #include /* for DRVSUPFN */ #include /* for dev*routines, atVMEA16 */ #include /* for epicsEvent* */ #include /* for epicsExportAddress */ #include /* for epicsMessageQueueCreate, ... */ #include /* for epicsTimeStamp and assoc fns */ #include /* for epicsTimerQueue for watchdog */ #include /* for epicsThreadPriorityMax and to get confdefs.h indirectly inc'd */ /* #include for in_le32 */ #include /* for DEBUGPRINT */ #include "drvPad.h" /* Interrupt/General-Purpose I/O Pin Descriptions All pins default to general-purpose input pins at reset. The pin value is synchronized to the rising edge of CLKOUT when read from the EPORT pin data register (EPPDR). The values used in the edge/level detect logic are also synchronized to the rising edge of CLKOUT. These pins use Schmitt triggered input buffers which have built in hysteresiss designed to decrease the probability of generating false edge-triggered interrrupts for slow rising and falling input signals. When a pin is configured as an output, it is driven to a state whose level is determined by the corresponding bit in the EPORT data register(EPDR). All bits in the EPDR are high at reset. */ #define IPSBAR (0x40000000) /* Default base address for the Edgeport and the Chip Select Module */ /* Adding the base address here, defies the purpose of having the change base address routine */ /* Interrupt controller module */ #define ICR02 (IPSBAR+0xC42) /* Address of interrupt control register #2. 1 byte */ #define IMRL (IPSBAR+0xC0C) /* Address of interrupt mask register low. 4 bytes */ #define EPPAR (IPSBAR+0x00130001) /* Two bytes. Big endian. 0=bits 15-8, 1=bits 7-0 */ #define EPDDR (IPSBAR+0x00130002) /* EPORT Data Direction Register */ #define EPIER (IPSBAR+0x00130003) /* EPORT Interrupt Enable Register */ #define EPDR (IPSBAR+0x00130004) /* EPORT Data Register */ #define EPPDR (IPSBAR+0x00130005) /* EPORT Pin Data Register */ #define EPFR (IPSBAR+0x00130006) /* EPORT Flag Register */ /* Note: 0x00130007 is reserved */ /* Note about access: EPPAR, EPDDR, and EPIER are Supervisor access EPDR, EPPDR and EPRF are Supervisor/User access Writing to reserved address has no effect, reading returns zeroes */ #define CSAR1 (IPSBAR+0x00008C) /* 2 bytes */ #define CSMR1 (IPSBAR+0x000090) /* 4 bytes */ #define CSCR1 (IPSBAR+0x000096) /* 2 bytes */ #ifdef PAD_SIMPLE_PROCESSING /* General Purpose Timer Modules (GPTA and GPTB) setup */ /* The port should initialize as general purpose inputs with the timer functions disabled. */ /* The general purpose timer module can be configured as IO and appears in Connector J21 of the PAD board. */ /* Connector Port -Pin Name J21-11 PTA0 J21-12 PTA1 J21-9 PTA2 J21-10 PTA3 */ /* GPT Port Data Direction Register (GPTDDR) */ #define GPTDDR (IPSBAR+0x1A001E) /* GPT Port Data Direction Register. */ /* Setting Bits 0 to 3 at Byte address IPSBAR + 0x1A001E will cause the corresponding PTA0 to PTA3 to become outputs. */ /* Write 0x01 to IPSBAR + 0x1A001E to set PTA0 to an output pin. */ /* GPT Port Data Register (GPTPORT) */ /* Writing to bits 0 to 3 of byte address IPSBAR+0x1A001D will set corresponding output pins PTA0 to PTA3 to the value written. */ #define GPTPORT (IPSBAR+0x1A001D) /* GPT Port Data Register */ /* Set bit low write 0x00 to IPSBAR+0x1A001D */ /* Set bit low write 0x00 to IPSBAR+0x1A001D */ #endif #define DACQ_TASK_PRIO epicsThreadPriorityHigh #define DACQ_TASK_STACK (20000) /* task stack size */ #define PAD_NUM_WAVEFORMS_KEPT (10) #define PAD_MAX_INT (2147483647) /* 4 byte int - 1 bit for sign */ #define PAD_IRQ_VECTOR (66) /* DECIMAL! */ /*#define PAD_IRQ_LEVEL (?) done differently */ #define PAD_MAX_FILENAME (100) #define PAD_SCALED_DATA_NORMALIZATION (32767) /* 2nd dimension, data1 and yint* are only used by PAD_BPH app *S vars are only used by PAD_WF app sumSq* and sigmaSq* are only used by PAD_REF app */ /* scondam: 01.28.09: The compiler option 'PAD_SIMPLE_PROCESSING is used for compiling in the code specific to the Klystron Test Station TS01 test */ #ifdef PAD_SIMPLE_PROCESSING #define PAD_NUM_AVGS_TO_SAVE (256) #else #define PAD_NUM_AVGS_TO_SAVE (32768) #endif typedef struct rf_channel_s { long long sumQ[2]; long long sumI[2]; long long sumS; long long sumSqQ; /* used by pad{Ref,Ifq}ReadSignal */ long long sumSqI; /* used by pad{Ref,Ifq}ReadSignal */ long sigmaSqQ; /* used by pad{Ref,Ifq}ReadSignal */ long sigmaSqI; /* used by pad{Ref,Ifq}ReadSignal */ short* scalarI; /* array of scalar corrections used by padIfqReadSignal */ short* scalarQ; /* array of scalar corrections used by padIfqReadSignal */ long* procI; /* array of processed data used by padIfqReadSignal */ long* procQ; /* array of processed data used by padIfqReadSignal */ int ofst[2]; int sz[5]; /* 0=sample 0, 1=sample 1, 3=IFQ sample I, 4=IFQ sample Q */ int x[2]; /* <--- 8/14/7: may be able to do without this now */ int nToRead[2]; int nRead[2]; int numQ[2]; int numI[2]; int numS; short* data0; /* the first or only sample of interest in the FIFO */ short* data1; /* the second sample of interest in the FIFO (only padBph) */ short* scalar; /* array of scalar corrections used by padGenReadSignal */ long* proc0; long* proc1; long avgQ[2]; /* 1/22/2007 with scalar array ranging from -32767 to 32767, avg doesn't fit into 2 bytes */ long avgI[2]; /* 1/22/2007 with scalar array ranging from -32767 to 32767, avg doesn't fit into 2 bytes */ long avgS; short plsavg; /* number of pulses to average. Overwrites PAD_NUM_AVGS_TO_SAVE for a channel */ long avgOut; /* Average value of 'avgS' for 'plsavg' number of pulses */ long avgOut_ped; /* 5/02/2011 - add a user-settable pedestal value below which avgS will not be set to FAULT state */ short threshold; /* The upper fault threshold of a CH is set to this % above the avgOut and lower threshold to this% below avgOut */ short state; /* indicates the state of a channel, = NORMAL (1) when RAW value within range. = FAULT (0) otherwise. */ long padLogAvg[PAD_NUM_AVGS_TO_SAVE]; /* array of sets of avgS for 'plsavg' pulses */ short logindex; /* sliding window index. When logindex == plsavg, it rolls over to 0 */ } rf_channel_t; typedef struct iqcal_channel_s { int ofst; int sz; int nToRead; int nRead; short* data0; /* the sample of interest in the FIFO */ } iqcal_channel_t; rf_channel_t padCh0; rf_channel_t padCh1; rf_channel_t padCh2; rf_channel_t padCh3; iqcal_channel_t padCalCh0; iqcal_channel_t padCalCh1; iqcal_channel_t padCalCh2; iqcal_channel_t padCalCh3; /* Use this array of pointers to group the 4 like channel structures together */ rf_channel_t* pChannels[4]={&padCh0, &padCh1, &padCh2, &padCh3}; iqcal_channel_t* pCalChannels[4]={&padCalCh0, &padCalCh1, &padCalCh2, &padCalCh3}; short padLogIavg[PAD_NUM_AVGS_TO_SAVE]; short padLogQavg[PAD_NUM_AVGS_TO_SAVE]; int logIindex = 0; int logQindex = 0; int Idone = 0; int Qdone = 0; struct drvet drvPad = { 2, (DRVSUPFUN) padReport, /* subroutine defined in this file */ (DRVSUPFUN) padInitialise /* subroutine defined in this file */ }; epicsExportAddress(drvet, drvPad); epicsTimerQueueId padWdTimerQ = 0; /* scondam: 01.28.09: The compiler option 'PAD_SIMPLE_PROCESSING is used for compiling in the code specific to the Klystron Test Station TS01 test */ #ifdef PAD_SIMPLE_PROCESSING #define FAULT (0) #define NORMAL (1) int State = FAULT; int fault_chan = -1; typedef struct { short* data; int ofst; int sz; int nToRead; int nRead; long long sumS; int numS; long avgS; long avgOut; long thresh; long ulevel; long llevel; short state; long faults; } fail_state; fail_state fst0; fail_state fst1; fail_state fst2; fail_state fst3; fail_state* fst[4]={&fst0, &fst1, &fst2, &fst3}; #endif #ifdef USING_TIMER int timers[20]; int worst_case_timers[21]; /* has one more to hold the number of valid timers */ int timer_ii; int longest_time_at_done = 0; #endif /* ENUM with values: DP_FATAL, DP_ERROR, DP_WARN, DP_INFO, DP_DEBUG Anything less than or equal to drvPnetFlag gets printed out. Can be set to 0 here and altered via prompt or in st.cmd file */ int drvPadFlag = 4; int drvPadGetSigFlag = 4; int drvPadCfgFlag = 4; int drvPadScalFlag = 4; int drvPadIntrFlag = 4; int drvPadSSFlag = 4; int drvPadGenFlag = 4; int drvPadIfqFlag = 4; int drvPadRefFlag = 4; int drvPadBphFlag = 4; int drvPadSimpFlag = 4; /* Hardware registers for chip select and EPORT module */ LOCAL volatile unsigned char *icr02 = (unsigned char *)ICR02; LOCAL volatile unsigned long *imrl = (unsigned long *)IMRL; LOCAL volatile unsigned char *epddr = (unsigned char *)EPDDR; LOCAL volatile unsigned char *epier = (unsigned char *)EPIER; LOCAL volatile unsigned char *epdr = (unsigned char *)EPDR; LOCAL volatile unsigned char *eppar = (unsigned char *)EPPAR; LOCAL volatile unsigned char *eppdr = (unsigned char *)EPPDR; LOCAL volatile unsigned char *epfr = (unsigned char *)EPFR; LOCAL volatile short *csar1 = (short *)CSAR1; LOCAL volatile short *csar1_actual = (short *) 0x80000000; LOCAL volatile unsigned short *cscr1 = (unsigned short *)CSCR1; LOCAL volatile unsigned long *csmr1 = (unsigned long *)CSMR1; /* scondam: 01.28.09: General Purpose Timer and IO Port configuration */ LOCAL volatile unsigned char *gptddr = (unsigned char *)GPTDDR; LOCAL volatile unsigned char *gptport = (unsigned char *)GPTPORT; /* Driver globals */ LOCAL epicsEventId waitForData; LOCAL epicsTimeStamp timeAtBoot; LOCAL int intrCount = 0; LOCAL int prev_intrCount = 0; /* scondam: 03.29.11: added to calculate trigger rate */ LOCAL int numIntrRollovers = 0; LOCAL int overflowICount = 0; LOCAL int numOverflowIRollovers = 0; LOCAL int overflowQCount = 0; LOCAL int numOverflowQRollovers = 0; LOCAL int overflowSCount = 0; LOCAL int numOverflowSRollovers = 0; LOCAL char filename[PAD_MAX_FILENAME]; LOCAL char filenameStem[PAD_MAX_FILENAME]; #if defined PAD_SIMPLE_PROCESSING LOCAL int faultCount = 0; /* Number of times FAULT state was entered since boot*/ #endif #ifdef SCANIO LOCAL int scanIoCount = 0; LOCAL IOSCANPVT pad_ioscanpvt; /* single interrupt source for all pad recs */ LOCAL int numScanIoRollovers = 0; #endif long ch0data[7]; /* 8/14/7: new. used by padGetSignal */ long ch1data[7]; /* 8/14/7: new. used by padGetSignal */ long ch2data[7]; /* 8/14/7: new. used by padGetSignal */ long ch3data[7]; /* 8/14/7: new. used by padGetSignal */ /*============================================================================= Name: padDumpSignal(int channel) Abs: Print out latest raw data in channel "channel" Special handling for PAD_BPH app since it has two samples =============================================================================*/ int padDumpSignal(int channel) { int ii; rf_channel_t* pCh; iqcal_channel_t* pCCh; /* Channels 0-3 are samples used for calcs; channels 4-7 are full FIFOs of raw data */ switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; printf("Channel %d's raw data:\n", channel); printf(" sample 0: "); for (ii=0; ii<*(pCh->sz); ii++) { printf ("addr:0x%08x val:%hd ", (unsigned int)(pCh->data0+ii),*(pCh->data0+ii)); } printf("\n"); #ifdef PAD_BPH printf(" sample 1: "); for (ii=0; ii<*(pCh->sz); ii++) { printf ("addr:0x%08x val:%hd ", (unsigned int)(pCh->data0+ii),*(pCh->data0+ii)); } printf("\n"); #endif break; case 4: case 5: case 6: case 7: pCCh = pCalChannels[channel-4]; printf("Calibration channel's raw data:\n"); for (ii=0; iisz; ii++) { printf ("0x%08x: %d, ", (unsigned int)(pCCh->data0+ii), *(pCCh->data0+ii)); } printf("\n"); break; default: printf("padDumpSignal: invalid channel: %d\n", channel); return ERROR; } return OK; } /*============================================================================= Name: padDumpProcessedSignal(int channel) Abs: Print out latest processed data for channel "channel" Special handling for PAD_BPH app since it has two samples =============================================================================*/ int padDumpProcessedSignal(int channel) { int ii; rf_channel_t* pCh; pCh = pChannels[channel]; printf("Channel %d's processed data:\n", channel); printf(" sample 0: "); for (ii=0; ii<*(pCh->sz); ii++) { printf ("addr:0x%08x val:%ld ", (unsigned int)(pCh->proc0+ii),*(pCh->proc0+ii)); } printf("\n"); #ifdef PAD_BPH printf(" sample 1: "); for (ii=0; ii<*(pCh->sz); ii++) { printf ("addr:0x%08x val:%ld ", (unsigned int)(pCh->proc1+ii),*(pCh->proc1+ii)); } printf("\n"); #endif return OK; } /*============================================================================= Name: padDumpChannel Abs: Debug routine used to look at the contents of a channel's structure Ret: OK =============================================================================*/ int padDumpChannel(int channel ) { int ii; rf_channel_t* pCh; iqcal_channel_t* pCCh; switch (channel) { case 0: case 1: case 2: case 3: printf("Contents of RF channel %d, (range is 0-3), sample 0\n", channel); pCh = pChannels[channel]; printf("sz: %d\n", *(pCh->sz)); printf("ofst: %d\n", *(pCh->ofst)); printf("\n"); printf("nToRead: %d\n", *(pCh->nToRead)); printf("nRead: %d\n", *(pCh->nRead)); printf("sumQ: %lld\n", *(pCh->sumQ)); printf("numQ: %d\n", *(pCh->numQ)); printf("avgQ: %ld\n", *(pCh->avgQ)); printf("sumI: %lld\n", *(pCh->sumI)); printf("numI: %d\n", *(pCh->numI)); printf("avgI: %ld\n", *(pCh->avgI)); printf("x: %d\n", *(pCh->x)); printf("sumS: %lld\n", pCh->sumS); printf("numS: %d\n", pCh->numS); printf("avgS: %d\n", (int) pCh->avgS); printf("\nsample 1. valid for phase cavity channels\n"); printf("sz: %d\n", *(pCh->sz+1)); printf("ofst: %d\n", *(pCh->ofst+1)); printf("sumQ: %lld\n", *(pCh->sumQ+1)); printf("numQ: %d\n", *(pCh->numQ+1)); printf("avgQ: %ld\n", *(pCh->avgQ+1)); printf("sumI: %lld\n", *(pCh->sumI+1)); printf("x: %d\n", *(pCh->x+1)); printf("nToRead: %d\n", *(pCh->nToRead+1)); printf("nRead: %d\n", *(pCh->nRead+1)); printf("numI: %d\n", *(pCh->numI+1)); printf("avgI: %ld\n", *(pCh->avgI+1)); printf("\nVariance-related terms. valid for some reference dist'n and laser channels\n"); printf("sumSqQ: %lld\n", pCh->sumSqQ); printf("sumSqI: %lld\n", pCh->sumSqI); printf("sigmaSqQ: %ld\n", pCh->sigmaSqQ); printf("sigmaSqI: %ld\n", pCh->sigmaSqI); printf("First 10 values of scalarI...valid only where channel is not sampled mult of 8.5MHz\n"); for (ii=0; ii<10; ii++) { printf("addr:0x%08x val:%8hd\n", (unsigned int)(pCh->scalarI+ii), *(pCh->scalarI+ii)); } printf("First 10 values of scalarQ...valid same as scalarI\n"); for (ii=0; ii<10; ii++) { printf("addr:0x%08x val:%8hd\n", (unsigned int)(pCh->scalarQ+ii), *(pCh->scalarQ+ii)); } printf("First 10 values of scalar...valid where channel is sampled at mult of 8.5 MHz\n"); for (ii=0; ii<10; ii++) { printf("addr:0x%08x val:%8hd\n", (unsigned int)(pCh->scalar+ii), *(pCh->scalar+ii)); } printf("\n"); padDumpSignal(channel); /* does both samples if PAD_BPH */ padDumpProcessedSignal(channel); /* does both samples if PAD_BPH */ return OK; break; case 4: case 5: case 6: case 7: pCCh = pCalChannels[channel-4]; printf("Contents of CAL channel (access is to 4-7, but physically, it's 0-3 in the array)\n"); printf("ofst: %d\n", pCCh->ofst); printf("sz: %d\n", pCCh->sz); printf("nToRead: %d\n", pCCh->nToRead); printf("nRead: %d\n", pCCh->nRead); padDumpSignal(channel); break; default: printf("padDumpChannel: unrecognized channel: %d\n", channel); return ERROR; } return OK; } /*============================================================================= Name: padReport Ret: Driver support routine registered in DRVET structure =============================================================================*/ int padReport( int interest ) { printf("IOC booted at %d seconds from EPOCH\n", timeAtBoot.secPastEpoch); printf("Current epics time: %d seconds from EPOCH\n", 0); printf("Num interrupts since boot: %d\n", intrCount); printf("Num I overflow errors since boot: %d\n", overflowICount); printf("Num Q overflow errors since boot: %d\n", overflowQCount); #ifdef SCAN_IO printf("Num scan I/O requests since boot: %d\n", scanIoCount); #endif #ifdef DEBUG_PRINT int rc; rc = padDumpChannel(0); rc = padDumpChannel(1); rc = padDumpChannel(2); rc = padDumpChannel(3); #endif #ifdef PAD_PROCESS_DURING_READ /* Since processed data is not currently saved, skip this */ #endif return interest; } /*============================================================================= Name: padSetDataFileStem Abs: =============================================================================*/ /* Set the stem of the filename for the output data files */ int padSetDataFileStem(char * stem){ strcpy(filenameStem, stem); printf("padSetDataFileStem: setting filename to stem '%s'\n", filenameStem); return OK; } /*============================================================================= Name: padGetDataFileStem Abs: =============================================================================*/ char * padGetDataFileStem() { return filenameStem; } /*============================================================================= Name: padISR Abs: Executes every time the interrupt that indicates FIFO full arrives Rem: =============================================================================*/ void padISR () { if (intrCount < PAD_MAX_INT) { intrCount++; } else { intrCount = 0; numIntrRollovers++; } /* Announce that data is available for read */ epicsEventSignal(waitForData); /* Clear the interrupt by writing a "1" to "EPF2" */ /* Ref: p. 11-7 of EPORT manual. */ *epfr = 4; return; } #ifdef PAD_SIMPLE_PROCESSING /* scondam: 01.28.09: */ /*============================================================================= Name: padGetPA0 Abs: show the output port status (Pin 24 which is GPT PA0) =============================================================================*/ int padGetPA0() { return *gptport; } /*============================================================================= Name: padGetFaultCount Abs: show the number of times the Fault states were entered since boot =============================================================================*/ int padGetFaultCount() { return faultCount; } #endif /*============================================================================= Name: padGetIntrCount Abs: Return the number of interrupts that have been processed since boot =============================================================================*/ int padGetIntrCount() { return intrCount; } /*============================================================================= Name: padResetIntrCount Abs: Reset the intrCount to 0 =============================================================================*/ int padResetIntrCount() { intrCount = 0; return OK; } /*============================================================================= Name: padGetPreviousIntrCount Abs: Return the number of interrupts processed in the previous second =============================================================================*/ int padGetPreviousIntrCount() { int prev = prev_intrCount; prev_intrCount = intrCount; return prev; } /*============================================================================= Name: padGetOverflowQCount Abs: Return the number of Q data overflows that have been processed since boot =============================================================================*/ int padGetOverflowQCount() { return overflowQCount; } /*============================================================================= Name: padGetOverflowICount Abs: Return the number of I data overflows that have been processed since boot =============================================================================*/ int padGetOverflowICount() { return overflowICount; } /*============================================================================= Name: padResetOverflowICount Abs: Reset the number of I data overflows =============================================================================*/ int padResetOverflowICount() { overflowICount = 0; return OK; } #ifdef SCANIO /*============================================================================= Name: padGetScanIOCount Abs: =============================================================================*/ int padGetScanIOCount() { return scanIoCount; } /*============================================================================= Name: padResetScanIOCount Abs: Reset the number of Scan I/O counts =============================================================================*/ int padResetScanIOCount() { scanIoCount = 0; return OK; } #endif #ifdef PAD_SIMPLE_PROCESSING /* scondam: 01.28.09: */ /*============================================================================= Name: padGetState Abs: Return the combined state of all channels. Even if one channel is in FAULT, State is FAULT. All channels must be NORMAL, for State to be NORMAL. =============================================================================*/ int padGetState() { return State; } /*============================================================================= Name: padResetState Abs: Reset State to NORMAL and set gptport to HIGH. =============================================================================*/ int padResetState() { *gptport = 0x1; State = NORMAL; return OK; } int padGetFaultChannel() { return fault_chan; } /*============================================================================= Name: padGetThreshold Abs: Return the threshold % set by user for a channel 'x' in the PV TST_x_LEVEL =============================================================================*/ int padGetThreshold(int channel) { if (channel <= 3) return pChannels[channel]->threshold; DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetThreshold: unrecognized channel: %d\n", channel)); return 0; } /*============================================================================= Name: padSetThreshold Abs: Set the threshold in % for a channel 'x' in the PV TST_x_LEVEL =============================================================================*/ int padSetThreshold(int channel, int val) { if (channel <= 3) { if ((val >= 0) && (val <= 100)) { pChannels[channel]->threshold = val; } else { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padSetThreshold: unrecognized threshold: %d. Accepted values are 0-100\n", channel)); } } else { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padSetThreshold: unrecognized channel: %d\n", channel)); } return 0; } /*============================================================================= Name: padGetUpperThreshold Abs: Return the threshold % set by user for a channel 'x' in the PV TST_x_LEVEL =============================================================================*/ int padGetUpperThreshold(int channel) { if (channel <= 3) { long thresh = labs(pChannels[channel]->avgOut) * (pChannels[channel]->threshold / 100.0); return (pChannels[channel]->avgOut + thresh); } DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetUpperThreshold: unrecognized channel: %d\n", channel)); return 0; } /*============================================================================= Name: padGetLowerThreshold Abs: Return the threshold % set by user for a channel 'x' in the PV TST_x_LEVEL =============================================================================*/ int padGetLowerThreshold(int channel) { if (channel <= 3) { long thresh = labs(pChannels[channel]->avgOut) * (pChannels[channel]->threshold / 100.0); return (pChannels[channel]->avgOut - thresh); } DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetLowerThreshold: unrecognized channel: %d\n", channel)); return 0; } /*============================================================================= Name: padGetFaultingPulses Abs: Return the number of pulses in fault since boot =============================================================================*/ int padGetFaultingPulses(int channel) { if (channel <= 3) { fail_state* fSt; fSt = fst[channel]; return fSt->faults; } DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetFaultingPulses: unrecognized channel: %d\n", channel)); return 0; } #endif /* end of #ifdef PAD_SIMPLE_PROCESSING */ /*============================================================================= Name: padTurnOn/OffLED Abs: It should be obvious =============================================================================*/ int padTurnOnLED() { volatile unsigned char *place; place = (unsigned char *) EPDR; *place = 0xF7; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padTurnOnLED: %d\n", (int) *place)); #endif return OK; } int padTurnOffLED() { volatile unsigned char *place; place = (unsigned char *) EPDR; *place = 0xFF; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padTurnOffLED: %d\n", (int) *place)); #endif return OK; } /* It should take 2.5 millisec to write to the 256 Kword FIFO It should take 12.5 millisec to read from the 256 Kword FIFO NB: without the "while" stmt, you have to call this once to enable FIFO (which isn't yet full) and then call it again to see the full FIFO Args: Type Name Access Description --------------- ------------- ---------- ---------------------------- int startingPoint read-only first 40 nsec increment to read int num40SecIncs read-only number of (16 bit) shorts to read */ /*============================================================================= Name: padGetSampleOffset Abs: =============================================================================*/ int padGetSampleOffset(int channel, int sample) { int* addr = NULL; int val = 0; iqcal_channel_t* pCCh; rf_channel_t* pCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; addr = pCh->ofst; val = *(addr+sample); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleOffset: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleOffset: address to ofst[0] is 0x%08x\n", (unsigned int) addr)); if (sample > 0) { DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleOffset: address to ofst + sample %d is 0x%08x\n", sample, (unsigned int) (addr+sample))); } DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleOffset: ofst is %d\n", val)); #endif break; case 4: case 5: case 6: case 7: pCCh = pCalChannels[channel-4]; val = pCCh->ofst; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleOffset: address to channel structure is 0x%08x\n", (unsigned int) &pCCh)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleOffset: address to ofst is 0x%08x\n", (unsigned int) &(pCCh->ofst))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleOffset: ofst is %d\n", val)); #endif break; default: DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetSampleOffset: unrecognized channel: %d\n", channel)); return ERROR; } return val; } /*============================================================================= Name: padSetSampleOffset Abs: Set offset to reflect desired value + 3 clock cycles that we must wait for =============================================================================*/ int padSetSampleOffset(int channel, int sample, int ofst) { int* addr; rf_channel_t* pCh; iqcal_channel_t* pCCh; /* Range checking limit depends on the mode of operation. When CALIBRATING, channels 4-7 are used. The sample offset is __________. When RUNNING, channels 0-3 are used. The sample offset is __________. */ switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; addr = pCh->ofst; addr = addr + sample; /*if (sample == 0) { ofst += 3; do 3 garbage reads while FIFO fills }*/ *addr = ofst; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetSampleOffset: address to channel structure is 0x%08x\n", (unsigned int) pCh)); DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetSampleOffset: address to ofst[%d] is 0x%08x\n", sample, (unsigned int) (addr+sample))); DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetSampleOffset: ofst[%d] = %d\n", sample, *addr)); #endif break; case 4: case 5: case 6: case 7: pCCh = pCalChannels[channel-4]; pCCh->ofst = ofst /*+ 3*/; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetSampleOffset: address to channel structure is 0x%08x\n", (unsigned int) pCCh)); DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetSampleOffset: address to ofst is 0x%08x\n", (unsigned int) &(pCCh->ofst))); DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetSampleOffset: ofst = %d\n", pCCh->ofst)); #endif break; default: DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetSampleOffset: unrecognized channel: %d\n", channel)); return ERROR; } return OK; } /*============================================================================= Name: padGetSampleSize Abs: return sample size for any inputType, channel and sample =============================================================================*/ int padGetSampleSize(int channel, int sample) { int* addr; int val; rf_channel_t* pCh; iqcal_channel_t* pCCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; addr = pCh->sz; /* now that that has been dealt with, from this point on in this routine, sample indicates the sample number... */ val = *(addr+sample); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleSize: address to channel structure is 0x%08x\n", (unsigned int) &pCh)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleSize: address to sz[0] is 0x%08x\n", (unsigned int) addr)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleSize: address to sz[%d] is 0x%08x\n", sample, (unsigned int) (addr+sample))); #endif break; case 4: case 5: case 6: case 7: pCCh = pCalChannels[channel-4]; val = pCCh->sz; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleSize: address to channel structure is 0x%08x\n", (unsigned int) &pCCh)); #endif break; default: return ERROR; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleSize: sz is %d\n", val)); #endif return val; } /*============================================================================= Name: padSetSampleSize Abs: set sample size Inputs: type indicates interlaced vs non-interlaced processed data array channel is ADC channel sample is the window of interest in the FIFO, normally there is only one, which is numbered '0'. Phasing cavity PAD uses a second sample, which is numbered '1'. size is the size of the window of interest which must be no bigger than the allowed size. The allowed size depends on whether it is the calibration data or the operational data. =============================================================================*/ int padSetSampleSize(int type, int channel, int sample, int size) { int* addr=NULL; rf_channel_t* pCh; iqcal_channel_t* pCCh; int tmp; /* Range checking limit depends on the mode of operation. When CALIBRATING, channels 4-7 are used. The sample size is the PAD_FIFO_SIZE. When RUNNING, channels 0-3 are used. The sample size is PAD_SAMPLE_SIZE */ switch (channel) { case 0: case 1: case 2: case 3: if (size > PAD_SAMPLE_SIZE || size < 0) { DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetSampleSize: channel %d: requested size, %d, out of range [0,%d]. Using 24\n", channel, size, PAD_SAMPLE_SIZE)); /*return ERROR;*/ /* 2/26/2007. This is not enough of an error handler. If you enter a huge number by accident. I.e. you append 300 to initial 80 and end up with 80300, you see this msg go by, but system hangs waiting for mbuf!!! */ size = 24; } /* 4/24/7: TO DO: If raw data is interlaced, ensure that size is a mult of 4; if raw data is non-interlaced, ensure that size is a mult of 6 */ if ((type == PAD_NON_INTERLACED_RAW)) { /* size must be a multiple of 6 */ tmp=size; while (tmp >= 6) { tmp -= 6; } if (tmp !=0 ) { /* Reject choice */ DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetSampleSize: channel %d: requested size, %d, is not a multiple of 6. using 24\n", channel, size)); size = 24; } } else { if (type == PAD_RAW) { /* size must be a multiple of 4 */ tmp=size; while (tmp >= 4) { tmp -= 4; } if (tmp !=0 ) { /* Reject choice */ DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetSampleSize: channel %d: requested size, %d, is not a multiple of 4. using 24\n", channel, size)); size = 24; } } } pCh = pChannels[channel]; if (sample==1) { addr = pCh->sz; addr = addr + sample; *addr = size; } else { /* case for first sample, just one sample and for Ifq */ /* Pb here is that "sample" is being used for two diff purposes: to hold the sample size of the window of interest AND to indicate that it's a non-interlaced sample (3 indicates it's non-inter I, 4 indicates non- inter Q. */ *(pCh->sz) = size; break; } case 4: case 5: case 6: case 7: pCCh = pCalChannels[channel-4]; pCCh->sz = size; break; default: DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetSampleSize: unrecognized channel: %d\n", channel)); return ERROR; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetSampleSize: changing sample size for ch%d type %d sample %d to %d\n", channel, type, sample, size)); #endif return OK; } /*============================================================================= Name: padGetOperationMode Abs: return mode of operation =============================================================================*/ int padGetOperationMode() { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetOperationMode: %d\n", padOperationMode)); #endif return padOperationMode; } /*============================================================================= Name: padSetOperationMode Abs: set mode of operation =============================================================================*/ int padSetOperationMode(int mode) { /* range check on mode */ if ((mode < 0) || (mode> PAD_NUM_OP_MODES)) { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padSetOperationMode: mode %d is out of range [0,%d]\n", mode, PAD_NUM_OP_MODES)); return ERROR; } padOperationMode = mode; return OK; } /*============================================================================= Name: padGetCalibrationControl Abs: return mode of operation =============================================================================*/ int padGetCalibrationControl() { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetCalibrationControl: %d\n", padCalibrationControl)); #endif return padCalibrationControl; } /*============================================================================= Name: padSetCalibrationControl Abs: set calibration control =============================================================================*/ int padSetCalibrationControl(int calibrationControl) { /* range check on calibrationControl */ if ((calibrationControl < 0) || (calibrationControl > PAD_NUM_CAL_MODES)) { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padSetCalibrationControl: calibrationControl %d is out of range [0,%d]\n", calibrationControl, PAD_NUM_CAL_MODES)); return ERROR; } padCalibrationControl = calibrationControl; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padSetCalibrationControl: calibrationControl set to %d\n", calibrationControl)); #endif return OK; } /*============================================================================= Name: padSimpReadSignal Inp: Takes up to four rf_channel structures as input. Pass NULL ptrs as placeholders where you do NOT pass a structure. For eg., if you want to use this algorithm on channel '2' only, then call this routine with the signature: padSimpReadSignal(NULL, NULL, pChannels[2], NULL); What else must be set up first: Abs: What is set up after: =============================================================================*/ int padSimpReadSignal(rf_channel_t* chan0, rf_channel_t* chan1, rf_channel_t* chan2, rf_channel_t* chan3) { int ii; /* abs pos in the FIFO */ int jj; /* (ii-*ofst)th position in FIFO */ int mm = 0; /* channel */ #ifndef FAKE_DATA volatile short *pSrc; /* MUST be volatile to avoid read-only *pSrc line below from being optimized away by the compiler AND for the padChanneli[jj] = *pSrc from being optimized to be assigned */ #endif int srcOfst = 0; rf_channel_t* chan = NULL; int isOverflow; #ifndef FAKE_DATA pSrc = csar1_actual; #endif #ifdef PAD_SIMPLE_PROCESSING int all_state=NORMAL; /* check State for all channels */ #endif for (mm=0; mmnToRead))); #else DEBUGPRINT(DP_INFO, drvPadSimpFlag, ("padSimpReadSignal: faking channel %d data for %d words\n", mm, *(chan->sz))); #endif #endif *(chan->nToRead) = *(chan->ofst) + *(chan->sz); #ifdef PAD_PROCESS_DURING_READ chan->sumS = 0; chan->numS = 0; /* CHECK: set other, non-used vars in struct to zero? */ #endif #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadSimpFlag, ("padSimpReadSignal: guts of read for ch%d\n",mm)); #endif #ifndef FAKE_DATA /* Skip first three clock cycles to allow FIFO to fill */ *(pSrc+srcOfst); *(pSrc+srcOfst); *(pSrc+srcOfst); #endif jj=0; for (ii=0; ii<*(chan->nToRead); ii++) { /* Have we reached the starting point yet? Only copy data after that point*/ /* *pSrc will be refilled by hardware after read */ if (ii < *(chan->ofst)) { /* We've not yet reached the starting point. Read pSrc, but only for purpose of clearing chip select */ #ifndef FAKE_DATA *(pSrc+srcOfst); #endif } else { #ifndef FAKE_DATA *(chan->data0+jj) = *(pSrc+srcOfst); #else /* Make fake data */ printf("writing %d into addr 0x%08x (ch[%d]->data0[%d])\n",ii,(unsigned int)(chan->data0+jj),mm,jj); *(chan->data0+jj) = ii; #endif #ifdef PAD_PROCESS_DURING_READ /* New 6/7/7... */ isOverflow = *(chan->data0+jj) & 1; if (isOverflow) { if (overflowSCount < PAD_MAX_INT) { overflowSCount++; } else { overflowQCount = 0; numOverflowSRollovers++; } } else { /*... end New 6/7/7 */ /* Good data. Start by multiplying the signal by the constant */ *(chan->proc0+jj) = ((*(chan->scalar)) * (*(chan->data0+jj)) / 100); chan->sumS += *(chan->proc0+jj); chan->numS += 1; } /* New 6/7/7 */ #endif jj++; } } /* We've finished reading out the FIFO now. Do the operations on the full dataset, padChanneli[]... */ /* Save the number of 16-bit words read */ *(chan->nRead) = jj; /* Calculate the average of the sample S */ if (chan->numS == 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padKlyReadSignal: Channel %d: num pts is zero.\n", mm)); #endif return OK; } else { chan->avgS = chan->sumS/chan->numS; } #if defined PAD_SIMPLE_PROCESSING /* scondam: 01.28.09: This functions processes the raw data. Processing here means multiplying by a scalar value. Default value of this scalar is 1.*/ long sum = 0; int chan_num = mm; /* keep filling array until logXindex rollsovers */ chan->padLogAvg[chan->logindex++] = chan->avgS; if (chan->logindex >= chan->plsavg) { /* index rollover */ chan->logindex = 0; } for (ii=0; iiplsavg; ii++) {sum += chan->padLogAvg[ii];} chan->avgOut = sum / chan->plsavg; /* calculate higher and lower thresholds based on the last updated average value */ /* chan->avgOut will be updated once every chan->plsavg pulses */ /* take the absolute value - ignore the sign - and calculate the threshold in % */ long thresh = labs(chan->avgOut) * (chan->threshold / 100.0); /* now compare the current avg to seen if it is within the thresholds */ if ((chan->avgS <= (chan->avgOut + thresh)) && (chan->avgS >= (chan->avgOut - thresh))) { /* chan->avgS within accepted range */ chan->state = NORMAL; /* channel state is set to TRUE or NORMAL */ } else { /* scondam: 15-Jul-11 Change 'chan->avgS not within accepted range' to chan->avgOut not within accepted range */ /* while rf power is ramping up, do not trip until avg reaches a stable user-settable pedestal value */ if (chan->avgOut > chan->avgOut_ped) { /* fault state only if avg is above a user-settable pedestal value */ chan->state = FAULT; /* channel state is set to FALSE or FAULT */ } else { chan->state = NORMAL; /* channel state is set to TRUE or NORMAL */ } } if (chan->state == FAULT) { fault_chan = chan_num; fst[chan_num]->ofst = *(chan->ofst); fst[chan_num]->sumS = *(chan->sz); fst[chan_num]->nToRead = *(chan->nToRead); fst[chan_num]->nRead = *(chan->nRead); fst[chan_num]->sumS = chan->sumS; fst[chan_num]->numS = chan->numS; fst[chan_num]->avgS = chan->avgS; fst[chan_num]->avgOut = chan->avgOut; fst[chan_num]->thresh = thresh; fst[chan_num]->ulevel = (chan->avgOut + thresh); fst[chan_num]->llevel = (chan->avgOut - thresh); fst[chan_num]->state = chan->state; fst[chan_num]->faults++; /* for ( ii = 0; ii < padGetSampleSize(chan_num,0); ii++ ) { *(fst[chan_num]->data+ii) = *(chan->data0+ii);} */ /* padDumpFaultCountToFile(); */ } if (chan->state == FAULT) { /* even if one channel is faulty, set OUT line to FAULT */ all_state = FAULT; /* setPTA0ToLow(); */ /* OUT line is set to LOW */ *gptport = 0x0; State = FAULT; /* even if one channel is in FALSE, then State will become FAULT */ } #endif } /* mm loop */ #ifdef PAD_SIMPLE_PROCESSING if (all_state == NORMAL) { /* if all channels are NORMAL, then OUT line stays high */ /* setPTA0ToHigh(); */ /* OUT line is set to HIGH */ *gptport = 0x1; State = NORMAL; } #endif return OK; } /*============================================================================= Name: padCalReadSignal Abs: What makes this read unique is that no processing is done; i.e., there is no correction factor multiplied in to the each data point What else must be set up first:... What is set up after: if in PAD_CALIBRATING operational mode:... NB: Since this calibration channel is ALWAYS in channel 0, there is no need for a srcOfst term to be added to pSrc here 9/29/2006 TO DO: Since every PAD now can do calibration in all of its channels, this routine no longer needs to pass any args in. Just make a loop through all 4 padCalChannels where chan gets set to padCalChannels[mm]; for (mm=0; mmnToRead = chan->ofst + chan->sz; #ifndef FAKE_DATA printf("padCalReadSignal: initiating chip select read at 0x%08x for %d words\n", (unsigned int) csar1_actual, chan->nToRead); #else printf("padCalReadSignal: faking channel %d data for %d words\n", mm, chan->sz); #endif #ifndef FAKE_DATA /* Skip first three clock cycles to allow FIFO to fill */ *(pSrc+srcOfst); *(pSrc+srcOfst); *(pSrc+srcOfst); #endif /* Here are the guts of the read. This code was timed and found to take 250 ns to read one short */ printf("padCalReadSignal: guts of read for channel %d\n",mm); jj = 0; for (ii=0; iinToRead; ii++) { /* Have we reached the starting point yet? Only copy data after that point*/ /* *pSrc will be refilled by hardware after read */ if (ii < chan->ofst) { /* We've not yet reached the starting point. Read pSrc, but only for purpose of getting to the next value */ #ifndef FAKE_DATA *(pSrc+srcOfst); #endif } else { #ifndef FAKE_DATA *(chan->data0+jj) = *(pSrc+srcOfst); #else /* Make fake data */ *(chan->data0+jj) = ii; printf("padCalReadSignal: writing %d into addr 0x%08x (ch[%d]->data0[%d])\n",ii,(unsigned int)chan->data0+jj,mm,jj); #endif jj++; } } /* We've finished reading out the FIFO now */ /* Save the number of 16-bit words read */ chan->nRead = jj; printf("padCalReadSignal: number of words read is: %d\n", chan->nRead); } /* mm loop */ return OK; } /*============================================================================= Name: padGetYIntercept(y1, y2) Abs: from a pair of y-values (and globally-available, corresponding x-values) calculate first the slope, m=(y2-y1)/(x2-x1), and then the y-intercept, b=y-mx. Ret: y-intercept =============================================================================*/ long padGetYIntercept(int x1, long y1, int x2, long y2) { long m; long b; if (x2 != x1) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetYIntercept: points are: (%d,%ld) (%d,%ld)\n",x1,y1,x2,y2)); #endif m = (y2 - y1)/(long)(x2 - x1); b = y2 - m*x2; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetYIntercept: slope:%ld int:%ld\n",m,b)); #endif } else { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetYIntercept: vertical lines. slopes are inf\n")); return ERROR; } return b; } #ifdef NEVER /* Accessor functions for dev support */ long padGetInterceptI(int channel) { long val; rf_channel_t* pCh; pCh = pChannels[channel]; val = pCh->yintI; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetInterceptI: address to channel structure is 0x%08x\n", (unsigned int) &pCh)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetInterceptI: yintI is %ld\n", val)); #endif return val; } long padGetInterceptQ(int channel) { long val; rf_channel_t* pCh; pCh = pChannels[channel]; val = pCh->yintQ; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetInterceptQ: address to channel structure is 0x%08x\n", (unsigned int) &pCh)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetInterceptQ: yintQ is %ld\n", val)); #endif return val; } #endif /*============================================================================= Name: padGetSignal(int channel, int sample, int inputType) Abs: Ret: The data read from the FIFO for a specified channel and sample for any type: raw (0), scalar (1), processed (2), ... Here and other places: TO DO: range check on "channel" =============================================================================*/ void *padGetSignal(int channel, int sample, int inputType) { #ifdef DEBUGPRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: returning signal for ch:%d, samp:%d, inputType:%d\n", channel, sample, inputType)); #endif /* Determine if operational data or calibration data based on channel */ if (channel >= 0 && channel < PAD_NUM_CHANNELS_ON_BOARD) { /* Figure out what to do based on inputType */ switch (inputType) { case PAD_RAW: return pChannels[channel]->data0; break; case PAD_SCALAR: case PAD_CONSTANT: return pChannels[channel]->scalar; break; case PAD_PROCESSED: switch (sample) { case 0: return (void*)(pChannels[channel]->proc0); break; case 1: return (void*)(pChannels[channel]->proc1); break; default: DEBUGPRINT(DP_ERROR, drvPadGetSigFlag, ("padGetSignal: unrecognized sample: %d\n", sample)); return (void*) NULL; } case PAD_NON_INTERLACED_RAW: return pChannels[channel]->data0; break; case PAD_SCALAR_NON_INTERLACED_I: return pChannels[channel]->scalarI; break; case PAD_SCALAR_NON_INTERLACED_Q: return pChannels[channel]->scalarQ; break; case PAD_PROCESSED_NON_INTERLACED_I: #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: with inputType=PROCESSED, returns ptr to IFQ's procI array\n")); #endif return (void*)(pChannels[channel]->procI); break; case PAD_PROCESSED_NON_INTERLACED_Q: #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: with inputType=PROCESSED, returns ptr to IFQ's procQ array\n")); #endif return (void*)(pChannels[channel]->procQ); break; case PAD_PCAV: /* return the pointer to an array that contains: avgI for sample 0, avgQ for sample 0, midpoint for sample 0, avgI for sample1, avgQ for sample 1, midpoint of sample 1 */ switch (channel) { case 0: ch0data[0]=0; ch0data[1]=*(pChannels[0]->avgI); ch0data[2]=*(pChannels[0]->avgQ); ch0data[3]=(long)(*(pChannels[0]->ofst) + (*(pChannels[0]->sz))/2); ch0data[4]=*(pChannels[0]->avgI+1); ch0data[5]=*(pChannels[0]->avgQ+1); ch0data[6]=(long)(*(pChannels[0]->ofst+1) + (*(pChannels[0]->sz+1))/2); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: with inputType=PCAV, data array is: %ld %ld %ld %ld %ld %ld %ld\n", ch0data[0], ch0data[1], ch0data[2], ch0data[3], ch0data[4], ch0data[5], ch0data[6])); #endif return (void*)ch0data; break; case 1: ch1data[0]=1; ch1data[1]=*(pChannels[1]->avgI); ch1data[2]=*(pChannels[1]->avgQ); ch1data[3]=(long)(*(pChannels[1]->ofst) + (*(pChannels[1]->sz))/2); ch1data[4]=*(pChannels[1]->avgI+1); ch1data[5]=*(pChannels[1]->avgQ+1); ch1data[6]=(long)(*(pChannels[1]->ofst+1) + (*(pChannels[1]->sz+1))/2); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: with inputType=PCAV, data array is: %ld %ld %ld %ld %ld %ld %ld\n", ch1data[0], ch1data[1], ch1data[2], ch1data[3], ch1data[4], ch1data[5], ch1data[6])); #endif return (void*)ch1data; break; case 2: ch2data[0]=2; ch2data[1]=*(pChannels[2]->avgI); ch2data[2]=*(pChannels[2]->avgQ); ch2data[3]=(long)(*(pChannels[2]->ofst) + (*(pChannels[2]->sz))/2); ch2data[4]=*(pChannels[2]->avgI+1); ch2data[5]=*(pChannels[2]->avgQ+1); ch2data[6]=(long)(*(pChannels[2]->ofst+1) + (*(pChannels[2]->sz+1))/2); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: with inputType=PCAV, data array is: %ld %ld %ld %ld %ld %ld %ld\n", ch2data[0], ch2data[1], ch2data[2], ch2data[3], ch2data[4], ch2data[5], ch2data[6])); #endif return (void*)ch2data; break; case 3: ch3data[0]=3; ch3data[1]=*(pChannels[3]->avgI); ch3data[2]=*(pChannels[3]->avgQ); ch3data[3]=(long)(*(pChannels[3]->ofst) + (*(pChannels[3]->sz))/2); ch3data[4]=*(pChannels[3]->avgI+1); ch3data[5]=*(pChannels[3]->avgQ+1); ch3data[6]=(long)(*(pChannels[3]->ofst+1) + (*(pChannels[3]->sz+1))/2); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: with inputType=PCAV, data array is: %ld %ld %ld %ld %ld %ld %ld\n", ch3data[0], ch3data[1], ch3data[2], ch3data[3], ch3data[4], ch3data[5], ch3data[6])); #endif return (void*)ch3data; break; default: DEBUGPRINT(DP_ERROR, drvPadGetSigFlag, ("padGetSignal: unrecognized channel: %d\n", channel)); return (void*) NULL; } break; default: DEBUGPRINT(DP_ERROR, drvPadGetSigFlag, ("padGetSignal: unrecognized input type: %d\n", inputType)); return (void*) NULL; } } else { /* Send back calibration data */ return (void*)(pCalChannels[channel-4]->data0); } return (void*) NULL; } /*============================================================================= Name: padGetAverageQ(int channel, int sample) Abs: =============================================================================*/ long padGetAverageQ(int channel, int sample) { long* addr; long val; addr = pChannels[channel]->avgQ; val = *((long*)addr+sample); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageQ: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageQ: address to avgQ[0] is 0x%08x\n", (unsigned int) addr)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageQ: address to avgQ[%d] is 0x%08x\n", sample, (unsigned int) (addr+sample))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageQ: avgQ is %ld\n", val)); #endif return val; } /*============================================================================= Name: padGetAverageI(int channel, int sample) Abs: =============================================================================*/ long padGetAverageI(int channel, int sample) { long *addr; long val; addr = pChannels[channel]->avgI; val = *((long*)addr+sample); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageI: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageI: address to avgI[0] is 0x%08x\n", (unsigned int) addr)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageI: address to avgI[%d] is 0x%08x\n", sample, (unsigned int) (addr+sample))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageI: avgI is %ld\n", val)); #endif return val; } /*============================================================================= Name: padGetAverageS(int channel, int sample) Abs: =============================================================================*/ long padGetAverageS(int channel, int sample) { long val; val = pChannels[channel]->avgS; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageS: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageS: address to avgS is 0x%08x\n", (unsigned int) &(pChannels[channel]->avgS))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageS: avgS is %ld\n", val)); #endif return val; } /*============================================================================= Name: padGetSumS(int channel, int sample) Abs: =============================================================================*/ long padGetSumS(int channel, int sample) { long long val; val = pChannels[channel]->sumS; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSumS: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSumS: address to sumS is 0x%08x\n", (unsigned int) &(pChannels[channel]->avgS))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSumS: sumS is %lld\n", val)); #endif return val; } /*============================================================================= Name: padGetNumS(int channel, int sample) Abs: =============================================================================*/ long padGetNumS(int channel, int sample) { int val; val = pChannels[channel]->numS; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetNumS: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetNumS: address to numS is 0x%08x\n", (unsigned int) &(pChannels[channel]->avgS))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetNumS: numS is %d\n", val)); #endif return val; } #ifdef PAD_SIMPLE_PROCESSING /* scondam: 01.28.09: */ /*============================================================================= Name: padGetScalarS(int channel) Abs: =============================================================================*/ short padGetScalarS(int channel) { short val; val = *(pChannels[channel]->scalar); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetScalarS: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetScalarS: scalar is %d\n", val)); #endif return val; } /*============================================================================= Name: padSetScalarS Abs: Sets the scalar value =============================================================================*/ int padSetScalarS(int channel, short scalar) { rf_channel_t* pCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; *(pCh->scalar) = scalar; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetScalarS: Set scalar to = %d\n", scalar)); #endif break; default: DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetScalarS: unrecognized channel: %d\n", channel)); return ERROR; } return OK; } /*============================================================================= Name: padGetPulseAverage Abs: Returns the number of pulses to average over. This was set by user. =============================================================================*/ int padGetPulseAverage(int channel) { int val = 0; rf_channel_t* pCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; val = pCh->plsavg; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetPulseAverage: Number of pulses to average is %d\n", val)); #endif break; default: DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetSampleOffset: unrecognized channel: %d\n", channel)); break; } return val; } /*============================================================================= Name: padSetPulseAverage Abs: Sets the number of pulses to average =============================================================================*/ int padSetPulseAverage(int channel, int plsavg) { rf_channel_t* pCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; pCh->plsavg = plsavg; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetPulseAverage: Pulse Average = %d\n", plsavg)); #endif break; default: DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetPulseAverage: unrecognized channel: %d\n", channel)); return ERROR; } return OK; } /*============================================================================= Name: padGetAverageOutPedestal Abs: Returns the number of pedestal value of avgOut. This was set by user. A FAULT condition can occur only if avgOut is above this pedestal value. =============================================================================*/ int padGetAverageOutPedestal(int channel) { int val = 0; rf_channel_t* pCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; val = pCh->avgOut_ped; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageOutPedestal: Pedestal value of Average Out is %d\n", val)); #endif break; default: DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetAverageOutPedestal: unrecognized channel: %d\n", channel)); break; } return val; } /*============================================================================= Name: padSetAverageOutPedestal Abs: Sets the number of pulses to average =============================================================================*/ int padSetAverageOutPedestal(int channel, int avgOut_ped) { rf_channel_t* pCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; pCh->avgOut_ped = avgOut_ped; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetAverageOutPedestal: Pedestal value of Average Out = %d\n", plsavg)); #endif break; default: DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetAverageOutPedestal: unrecognized channel: %d\n", channel)); return ERROR; } return OK; } /*============================================================================= Name: padGetAverageOut Abs: Returns the Average raw value of 'plsavg' pulses. =============================================================================*/ int padGetAverageOut(int channel) { int val = 0; rf_channel_t* pCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; val = pCh->avgOut; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageOut: Average value of pulses is %d\n", val)); #endif break; default: DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetAverageOut: unrecognized channel: %d\n", channel)); break; } return val; } #endif /*============================================================================= Name: padGetVarianceQ(int channel, int sample) Abs: get the variance of Q for this sample =============================================================================*/ unsigned long padGetVarianceQ(int channel, int sample) { long *addr; long val; addr = &(pChannels[channel]->sigmaSqQ); val = *((long*)addr+sample); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetVarianceQ: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetVarianceQ: address to sigmaSqI[0] is 0x%08x\n", (unsigned int) addr)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetVarianceQ: address to sigmaSqI[%d] is 0x%08x\n", sample, (unsigned int) (addr+sample))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetVarianceQ: sigmaSqI is %lu\n", val)); #endif return val; } /*============================================================================= Name: padGetVarianceI(int channel, int sample) Abs: get the variance of I for this sample =============================================================================*/ unsigned long padGetVarianceI(int channel, int sample) { long *addr; unsigned long val; addr = &(pChannels[channel]->sigmaSqI); val = *((long*)addr+sample); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetVarianceI: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetVarianceI: address to sigmaSqI[0] is 0x%08x\n", (unsigned int) addr)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetVarianceI: address to sigmaSqI[%d] is 0x%08x\n", sample, (unsigned int) (addr+sample))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetVarianceI: sigmaSqI is %lu\n", val)); #endif return val; } /*============================================================================= Abs: return a value for x to plot the average I and Q =============================================================================*/ short padGetX(int channel, int sample) { int* addr1; int* addr2; int val1; int val2; int val3; addr1 = pChannels[channel]->ofst; val1 = *(addr1+sample); addr2 = pChannels[channel]->sz; val2 = *(addr2+sample); /* x is the offset + halfway point of the sample */ val3 = val1 + val2/2; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetX: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetX: address to ofst[0] is 0x%08x\n", (unsigned int) addr1)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetX: address to ofst[%d] is 0x%08x\n", sample, (unsigned int) (addr1+sample))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetX: ofst is %d\n", (int) val1)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetX: address to sz[0] is 0x%08x\n", (unsigned int) addr2)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetX: address to sz[%d] is 0x%08x\n", sample, (unsigned int) (addr2+sample))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetX: sz is %d\n", (int) val2)); DEBUGPRINT(DP_INFO, drvPadFlag, ("x=%d\n",val3)); #endif return (short) val3; } /*============================================================================= Name: padGetNumShortsToRead(int channel, int sample) Abs: Debug routine to check number of words planned to read, given channel and sample within the channel =============================================================================*/ int padGetNumShortsToRead(int channel, int sample) { int* addr; int val; addr = pChannels[channel]->nToRead; val = *((int*)addr+sample); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetNumShortsToRead: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetNumShortsToRead: address to nToRead[0] is 0x%08x\n", (unsigned int) addr)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetNumShortsToRead: address to nToRead[%d] is 0x%08x\n", sample, (unsigned int) (addr+sample))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetNumShortsToRead: numToRead is %d\n", val)); #endif return val; } /*============================================================================= Name: padGetNumShortsRead() Abs: Debug routine to check number of words actually read, given channel and sample within the channel =============================================================================*/ int padGetNumShortsRead(int channel, int sample) { int* addr; int val; addr = pChannels[channel]->nRead; val = *((int*)addr+sample); /* does this work so that sample of 1 steps 1 integer past nToRead[0] to find nToRead[1]? */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetNumShortsRead: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetNumShortsRead: address to nRead[0] is 0x%08x\n", (unsigned int) addr)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetNumShortsRead: address to nRead[%d] is 0x%08x\n", sample, (unsigned int) (addr+sample))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetNumShortsRead: numToRead is %d\n", val)); #endif return val; } /*============================================================================= Name: padGetIoScanPvt Abs: Used by records that process on I/O Intr. Device support calls this in its get_ioint_info routines at init'n. =============================================================================*/ #ifdef SCANIO IOSCANPVT padGetIoScanPvt() { return pad_ioscanpvt; } #endif /*============================================================================= Name: padCalibration Abs: Does the calibration data acquisition upon receipt of calibration request. =============================================================================*/ int padCalibration() { printf("padCalibration: disabling hardware triggers for the calibration\n"); padDisableIntr(); printf("padCalibration: calling padCalReadSignal\n"); padCalReadSignal(&padCalCh0,&padCalCh1,&padCalCh2,&padCalCh3); /* For the PAD, calibration is a one-shot. Write the calibration data to files, one per channel and set the calibration done indicator */ /* printf("padCalibration: calling padDumpCalDataToFile\n"); int rc; rc = padDumpCalDataToFile(); if (rc == ERROR) { printf ("padCalibration: error writing calibration data to server\n"); return rc; } */ #ifdef SCANIO /* Process I/O Intr scan records registered to this callback COULD DO ANOTHER PAD_IOSCANPVT VARIABLE JUST TO PROCESS CAL WAVEFORM RECORDS scanIoRequest(pad_cal_ioscanpvt); if (scanIoCalCount < PAD_MAX_INT) { scanIoCalCount++; } else { scanIoCalCount = 0; numScanIoCalRollovers++; } */ #endif /* Since it's a one-shot, we want to automatically set calibration done here. I.e. don't wait for the button press */ printf("padCalibration: setting calib done\n"); padSetCalibrationControl(PAD_CAL_DONE); /* Check: this may be also being done by the state machine upon receipt of calib done */ printf("padCalibration: enabling hardware triggers since calibration complete\n"); padEnableIntr(); return OK; } /*============================================================================= Name: padDacqTask Abs: acquire and process the data upon receipt of event from ISR =============================================================================*/ int padDacqTask() { int rc; epicsEventWaitStatus waitStatus; /* Do forever */ while (TRUE) { /* epicsEventShow(waitForData, level);*/ /* wait for the ISR to indicate that there is data waiting to be read */ waitStatus = epicsEventWait(waitForData); if (waitStatus == epicsEventWaitOK) { #ifdef USING_TIMER /* zero the timer and reset the loop var for this iteration */ benchmark_timer_initialize(); timer_ii = 0; #endif #ifdef PAD_KLY /* case of RFGun Kly, L0A Kly, L0B Kly, TCav Kly, L1S Kly, L1X Kly, L2TCav Kly and L1X */ /* Simple average S for all channels */ padSimpReadSignal(&padCh0,&padCh1,&padCh2,&padCh3); #endif #ifdef SCANIO /* Process I/O Intr scan records registered to this callback */ scanIoRequest(pad_ioscanpvt); if (scanIoCount < PAD_MAX_INT) { scanIoCount++; } else { scanIoCount = 0; numScanIoRollovers++; } #endif } else { if (waitStatus == epicsEventWaitTimeout) { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padDacqTask: timed out waiting for data to be available to read out of FIFO\n")); return ERROR; } else { if (waitStatus == epicsEventWaitError) { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padDacqTask: error waiting for data to be available to read out of FIFO\n")); return ERROR; } } } } return rc; } #ifdef USING_TIMER /*============================================================================= Name: padUpdateWorstCase Abs: copy values of current timers into worst case set =============================================================================*/ void padUpdateWorstCase() { int ii; for (ii=0; iidata0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[ii]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[ii]->plsavg = 0; pChannels[ii]->avgOut = 0; pChannels[ii]->threshold = 0; /* initially set threshold to 20% */ pChannels[ii]->state = 0; pChannels[ii]->scalar = malloc(sizeof(short)); *(pChannels[ii]->scalar) = 0; /* scalar value of 100% means no scaling factor applied */ pChannels[ii]->logindex = 0; #ifdef PAD_SIMPLE_PROCESSING fst[ii]->sz = 0; fst[ii]->ofst = 0; fst[ii]->sumS = 0; fst[ii]->nRead = 0; fst[ii]->nToRead = 0; fst[ii]->sumS = 0; fst[ii]->numS = 0; fst[ii]->avgS = 0; fst[ii]->avgOut = 0; fst[ii]->thresh = 0; fst[ii]->ulevel = 0; fst[ii]->llevel = 0; fst[ii]->state = 0; fst[ii]->faults = 0; #endif } for (ii=4; ii<7; ii++) { pCalChannels[ii-4]->data0 = malloc(sizeof(short)*PAD_FIFO_SIZE); } #endif /* Initialize our watchdog timer queue */ padWdTimerQ=epicsTimerQueueAllocate( 1, /* ok to share queue */ epicsThreadPriorityLow); if (padWdTimerQ==0) { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padInitialise: error initialising watchdog timer queue\n")); return ERROR; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padInitialise: watchdog timer queue initialised\n")); #endif waitForData = epicsEventCreate(epicsEventEmpty); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padInitialise: created event sem %d\n", (int) waitForData)); #endif /* Set this up if there are records scanned on "I/O Intr" in the db */ #ifdef SCANIO scanIoInit(&pad_ioscanpvt); #endif /* Create the task to do data acquisition */ rcv_id = epicsThreadCreate( "padDacqTask", DACQ_TASK_PRIO, DACQ_TASK_STACK, (EPICSTHREADFUNC)padDacqTask, 0); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padInitialise: data acquistion task id is %d\n", (int)rcv_id)); #endif /* Connect the interrupt vector to the interrupt service routine */ rc = devConnectInterruptVME(PAD_IRQ_VECTOR, padISR, 0); if (rc != 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_FATAL, drvPadFlag, ("padInitialise: unable to connect to intr vector to ISR. rc = %d\n", rc)); #endif return ERROR; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padInitialise: intr vector, %x, connected to ISR\n", PAD_IRQ_VECTOR)); #endif #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padInitialise: hardware initialisation complete, but interrupts not yet enabled.\n")); #endif /* Initialisation is done. Turn off DP_INFO messages */ drvPadFlag=DP_ERROR; return OK; } int padEnableIntr() { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIntrFlag, ("padEnableIntr: enabling interrupts.\n")); #endif /* Enable the interrupt. Don't do this UNTIL everything is set up */ *epier = 0x04; /* Enable IRQ2 */ return OK; } int padDisableIntr() { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIntrFlag, ("padEnableIntr: disabling interrupts.\n")); #endif /* Disable the interrupt. Called when operational mode starts CALIBRATING */ *epier = 0x00; return OK; } #ifdef USING_TIMER int padGetTime() { return benchmark_timer_read(); } #endif /*============================================================================= Name: padSoftTrigger Abs: toggles IRQ1 bit which causes an IRQ2 hardware trigger The rising edge of IRQ1 is what is tied to IRQ2 in the CPLD, so first lower it (to make sure it's low), then raise it, so the IRQ2 gets triggered. Then, leave IRQ1 low. =============================================================================*/ int padSoftTrigger(){ *epdr &= ~2; *epdr |= 2; *epdr &= ~2; return OK; } /*============================================================================= Name: padResetFifo Abs: manually resets FIFO =============================================================================*/ int padResetFifo(){ *epdr &= ~8; /* 1111 0111 */ *epdr |= 8; /* 0000 1000 */ return OK; } /* DEBUG ROUTINES. NOT PART OF OPERATIONAL CODE */ /*============================================================================= Name: padDumpEppdr Abs: Debug function =============================================================================*/ int padDumpEppdr() { printf("eppdr contents: %d\n", *eppdr); return OK; } /*============================================================================= Name: padToggleIRQ5 Abs: Debug function =============================================================================*/ int padToggleIRQ5() { volatile char b; b = *eppdr ^ 32; *epdr = b; return (int) b; } void setDiagnosticBit() { volatile char b; b = *eppdr | Bit(5); *epdr = b; } void clearDiagnosticBit() { volatile char b; b = *eppdr & ~Bit(5); *epdr = b; } /*============================================================================= Name: setPTA0ToOutput() setPTA0ToInput() Abs: =============================================================================*/ void setPTA0ToOutput() { volatile char b; b = *gptddr | Bit(0); *gptddr = b; } void setPTA0ToInput() { volatile char b; b = *gptddr & ~Bit(0); *gptddr = b; } /*============================================================================= Name: setPTA0ToHigh() setPTA0ToLow() Abs: =============================================================================*/ void setPTA0ToHigh() { volatile char b; b = *gptport | Bit(0); *gptport = b; } void setPTA0ToLow() { volatile char b; b = *gptport & ~Bit(0); *gptport = b; } /*============================================================================= Name: padCauseChipSelect Abs: =============================================================================*/ int padCauseChipSelect() { printf("csar1_actual has address 0x%08x and contents: %d\n", (unsigned int) csar1_actual, (short) *csar1_actual); return OK; } /*============================================================================= Name: padClearChannel Abs: Sets entire contents of channel struct to zero. TO DO: Maybe this is too drastic? better to just wipe out data? TO DO: CLEAR padCalCh0 =============================================================================*/ int padClearChannel(int channel) { if (channel >= PAD_NUM_CHANNELS_ON_BOARD ) { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padClearChannel: unrecognized channel %d\n", channel)); return ERROR; } memset(pChannels[channel],0,sizeof(rf_channel_t)); return OK; }