/*============================================================================= 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 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 */ #ifdef DEBUG_PRINT #include /* for DEBUGPRINT */ #endif #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 USING_QSPI /* Queued serial port interface (QSPI) */ #define PQSPAR (IPSBAR+0x100059)/* Port QS Pin Assignment Register */ #define QMR (IPSBAR+0x340) /* QSPI Mode Register. 2 bytes */ #define QDLYR (IPSBAR+0x344) /* QSPI Delay Register. 2 bytes */ #define QWR (IPSBAR+0x348) /* QSPI Wrap Register. 2 bytes */ #define QIR (IPSBAR+0x34C) /* QSPI Interrupt Register. 2 bytes */ #define QAR (IPSBAR+0x350) /* QSPI Address Register. 2 bytes */ #define QDR (IPSBAR+0x354) /* QSPI Data Register. 2 bytes */ #endif #ifdef USING_DMA #define DONE (1) /* DMA transfer done */ #define BSY (2) /* DMA transfer busy */ #define REQ (4) /* DMA request */ #define RSD1 (8) /* Reserved. Should be cleared. */ #define BED (16) /* Bus error on destination */ #define BES (32) /* Bus error on source */ #define CE (64) /* Configuration error */ #define RSD2 (128) /* Reserved. Should be cleared. */ #define SAR2 (IPSBAR+0x180) /* Source address register 0 */ #define DAR2 (IPSBAR+0x184) /* Destination address register 0 */ #define DCR2 (IPSBAR+0x188) /* DMA control register 0 */ #define BCR2 (IPSBAR+0x18C) /* Byte count register 0 if data is 16 bit*/ #define DSR2 (IPSBAR+0x190) /* DMA status register 0 */ #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 */ 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; } 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}; #define PAD_NUM_AVGS_TO_SAVE (32768) 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; #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 #ifdef DEBUG_PRINT /* 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 = 2; int drvPadGetSigFlag = 2; int drvPadCfgFlag = 2; int drvPadScalFlag = 2; int drvPadIntrFlag = 2; int drvPadSSFlag = 2; int drvPadGenFlag = 2; int drvPadIfqFlag = 2; int drvPadRefFlag = 2; int drvPadBphFlag = 2; int drvPadSimpFlag = 2; int drvPadQspiFlag = 2; #endif /* 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; /* Driver globals */ LOCAL epicsEventId waitForData; LOCAL epicsTimeStamp timeAtBoot; LOCAL int intrCount = 0; 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]; #ifdef SCANIO LOCAL int scanIoCount = 0; LOCAL IOSCANPVT pad_ioscanpvt; /* single interrupt source for all pad recs */ LOCAL int numScanIoRollovers = 0; #endif #ifdef USING_QSPI LOCAL volatile unsigned char *pqspar = (unsigned char *) PQSPAR; LOCAL volatile unsigned short *qmr = (unsigned short *)QMR; LOCAL volatile unsigned short *qdlyr = (unsigned short *)QDLYR; LOCAL volatile unsigned short *qwr = (unsigned short *)QWR; LOCAL volatile unsigned short *qir = (unsigned short *)QIR; LOCAL volatile unsigned short *qar = (unsigned short *)QAR; LOCAL volatile unsigned short *qdr = (unsigned short *)QDR; #endif #ifdef USING_DMA LOCAL volatile unsigned long *sar2 = (unsigned long *)SAR2; LOCAL volatile unsigned long *dar2 = (unsigned long *)DAR2; LOCAL volatile unsigned long *dcr2 = (unsigned long *)DCR2; LOCAL volatile unsigned long *bcr2 = (unsigned long *)BCR2; LOCAL volatile unsigned long *dsr2 = (unsigned long *)DSR2; #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 */ int padDumpQavgToFile(); int padDumpIavgToFile(); /*============================================================================= 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; } #ifdef USING_QSPI /*============================================================================= Name: padQSPIReport Ret: =============================================================================*/ int padQSPIReport() { printf ("PQSPAR: 0x%08x should be 0x7F. If not, did you run padInitialise?\n", (unsigned int) *pqspar); printf("QMR: 0x%08x\n", (unsigned int) *qmr); printf("QDLYR: 0x%08x\n", (unsigned int) *qdlyr); printf("QIR: 0x%08x\n", (unsigned int) *qir); printf("QAR: 0x%08x\n", (unsigned int) *qar); printf("QDR: 0x%08x\n", (unsigned int) *qdr); printf("QWR: 0x%08x\n", (unsigned int) *qwr); return OK; } #endif /*============================================================================= Name: padReport Ret: Driver support routine registered in DRVET structure =============================================================================*/ int padReport( int interest ) { int rc; 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 USING_QSPI /* print out QSPI register contents */ rc = padQSPIReport(); #endif rc = padDumpChannel(0); rc = padDumpChannel(1); rc = padDumpChannel(2); rc = padDumpChannel(3); #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: padInitialiseScalarFromFile Abs: RF is at 2856 MHz. There is no digitizer that runs this fast. So work at IF (intermediate frequency) of 25.5 MHz and (smartly) choose to "digitally downmix" (a fancy name for taking the sine and cosine of something) to the baseband frequency of DC (0 MHz). By choosing and ADC clock of 102 MHz, there are 4 points to each IF cycle. They are pi/2 apart, so taking the sine and cosine has the result that: 0 sin(0)=0; Q=0 cos(0)=1; I=I pi/2 sin(pi/2)=1; Q=Q cos(pi/2)=0; I=0 pi sin(pi)=0, Q=0 cos(pi)=-1; I=-I 3pi/2 sin(3pi/2)=-1; Q=-Q cos(3pi/2)=0; I=0 Since Q is non-zero only when I is zero, and vice versa, the two variables can be stored in a single array, in alternating positions What is stored in "scalar" looks like: [ 1, 1, -1, -1, ... ] and maps to the Qs and Is as: [ Q1, I2, Q3, I4, ... ] where I1, Q2, I3 and Q4 are all zero. But this is not all. There is also a weighting factor multiplied to each value in scalar to correct the I and Qs to be "what the beam sees". If the application is PAD_LSR ch0 or PAD_REF ch0-3, then scalar needs to be 64 Kwords long (PAD_FIFO_SIZE). Otherwise, it needs to be only PAD_SAMPLE_SIZE long. Note that for PAD_KLY, no scalar correction is needed. Go get values for the scalar corrections from a value. One of three pointers in channel structure is filled in, depending on the value of sample. Ret: OK/ERROR =============================================================================*/ int padInitialiseScalarFromFile(int channel, int inputType, char* filename){ int ii; rf_channel_t* pCh; FILE *fin; /* Ensure nelm <= PAD_SAMPLE_SIZE? */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadScalFlag, ("padInitialiseScalarFromFile: inputs: channel: %d, inputType: %d, filename = %s\n", channel, inputType, filename)); #endif /* Attempt to find/open the file */ fin = fopen(filename, "r"); if (fin==NULL) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadScalFlag, ("padInitialiseScalarFromFile: unable to open '%s' for read. Exiting.\n", filename)); #endif return ERROR; } /* Read the values into scalar */ pCh = pChannels[channel]; switch (inputType) { case PAD_SCALAR: /* malloc space for and put file contents into channel struct's "scalar" */ printf("Filling scalar for ch%d\n", channel); pCh->scalar = (short*)malloc(sizeof(short)*PAD_SAMPLE_SIZE); for (ii=0; iiscalar+ii)); } break; case PAD_SCALAR_NON_INTERLACED_I: /* file contents into channel struct's "scalarI" */ printf("Filling scalarI for ch%d\n", channel); pCh->scalarI = (short*)malloc(sizeof(short)*PAD_SAMPLE_SIZE); for (ii=0; iiscalarI+ii)); } break; case PAD_SCALAR_NON_INTERLACED_Q: /* file contents into channel struct's "scalarQ" */ printf("Filling scalarQ for ch%d\n", channel); pCh->scalarQ = (short*)malloc(sizeof(short)*PAD_SAMPLE_SIZE); for (ii=0; iiscalarQ+ii)); } break; case PAD_CONSTANT: pCh->scalar = (short*)malloc(sizeof(short)); /* only 1 constant; not an array */ fscanf(fin, "%hd", pCh->scalar); printf("Filling scalar constant with %hd for ch%d from '%s'\n", *(pCh->scalar), channel, filename); break; default: DEBUGPRINT(DP_ERROR, drvPadScalFlag, ("padInitialiseScalarFromFile: unrecognized inputType: %d\n", inputType)); fclose(fin); return ERROR; } /* Close the file */ fclose(fin); return OK; } /*============================================================================= 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; } /*============================================================================= Name: padGetIntrCount Abs: Return the number of interrupts that have been processed since boot =============================================================================*/ int padGetIntrCount() { return intrCount; } /*============================================================================= 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; } #ifdef SCANIO /*============================================================================= Name: padGetScanIOCount Abs: =============================================================================*/ int padGetScanIOCount() { return scanIoCount; } #endif /*============================================================================= 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: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetSampleOffset: unrecognized channel: %d\n", channel)); #endif } 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: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetSampleOffset: unrecognized channel: %d\n", channel)); #endif 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) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetSampleSize: channel %d: requested size, %d, out of range [0,%d]. Using 24\n", channel, size, PAD_SAMPLE_SIZE)); #endif /*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 */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetSampleSize: channel %d: requested size, %d, is not a multiple of 6. using 24\n", channel, size)); #endif 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 */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetSampleSize: channel %d: requested size, %d, is not a multiple of 4. using 24\n", channel, size)); #endif 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: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetSampleSize: unrecognized channel: %d\n", channel)); #endif 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)) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadFlag, ("padSetOperationMode: mode %d is out of range [0,%d]\n", mode, PAD_NUM_OP_MODES)); #endif 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)) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadFlag, ("padSetCalibrationControl: calibrationControl %d is out of range [0,%d]\n", calibrationControl, PAD_NUM_CAL_MODES)); #endif return ERROR; } padCalibrationControl = calibrationControl; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padSetCalibrationControl: calibrationControl set to %d\n", calibrationControl)); #endif return OK; } /*============================================================================= Name: padGenReadSignal Abs: Reads PAD_NUM_CHANNELS_ON_BOARD FIFOs first by reading, but not storing, up to the offset, Past the offset, the 16bit integers are stored, corrected (scaled by and array of corrections) and then averaged. There are two variables, I and Q interleaved in a single channel, so the averages are obtained from summing the Is and Qs separately. Values past the ROI are never read. FIFO is reset by CPLD, not by any pad*.c code here. To just read the raw data (and skip correction and averaging), compile without PAD_PROCESS_DURING_READ set. =============================================================================*/ int padGenReadSignal(rf_channel_t* chan0, rf_channel_t* chan1, rf_channel_t* chan2, rf_channel_t* chan3) { int ii; /* absolute position in the FIFO */ int jj; /* the (ii - padSampleOffset)-th position in FIFO */ int mm; /* loop variable for all channels */ #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; #ifdef PAD_PROCESS_DURING_READ int isQ; int isOverflowI; int isOverflowQ; #endif rf_channel_t* chan = NULL; /* generic channel used in loop of all channels */ #ifndef FAKE_DATA pSrc = csar1_actual; #endif /* Fills data0, calculates avgI and avgQ for all four channels */ for (mm=0; mmnToRead) = *(chan->ofst) + *(chan->sz); #ifndef FAKE_DATA #ifdef DEBUG_PRINT /* This next line causes a read - it would change result if DEBUG_PRINT not defined! 6/12/7 */ DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: initiating chip select read at 0x%08x for %d words\n", (unsigned int) csar1_actual, *(chan->nToRead))); #endif #else #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: faking channel %d data for %d words\n", mm, *(chan->sz))); #endif #endif /* Here are the guts of the read. This code was timed and found to take 250 ns to read one short */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: guts of read for channel %d\n",mm)); #endif jj = 0; *(chan->sumQ) = 0; *(chan->sumI) = 0; *(chan->numQ) = 0; *(chan->numI) = 0; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: initially sumQ is %lld\n", *(chan->sumQ))); DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: initially numQ is %d\n", *(chan->numQ))); DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: initially sumI is %lld\n", *(chan->sumI))); DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: initially numI is %d\n", *(chan->numI))); #endif #ifndef FAKE_DATA /* Skip first three clock cycles to allow FIFO to fill */ *(pSrc+srcOfst); *(pSrc+srcOfst); *(pSrc+srcOfst); #endif 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 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; #endif #ifdef PAD_PROCESS_DURING_READ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: scaling %hd by %hd to: %ld\n", (*(chan->data0+jj)), *(chan->scalar+ii), *(chan->proc0+jj))); #endif /* Examine last bit of counter to determine whether this is a Q (odd) or an I (even) */ isQ = ii & 1; if (isQ == 1) { /* Q values in the odd positions */ /* We are only using the upper 15 bits of the ADC. A '1' in the least significant bit indicates an overflow in Q */ isOverflowQ = *(chan->data0+jj) & 1; if (isOverflowQ) { if (overflowQCount < PAD_MAX_INT) { overflowQCount++; } else { overflowQCount = 0; numOverflowQRollovers++; } } else { /* Good data. Multiplying the signal by the scalar (combined sin/cos and normalization) vector. NB: the index of scalar must be offset by the points skipped over during read. Add to sum, inc num pts */ *(chan->proc0+jj) = *(chan->scalar+ii) * (*(chan->data0+jj)); *(chan->sumQ) += *(chan->proc0+jj); *(chan->numQ) += 1; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag,("padGenReadSignal: sumQ now: %lld\n", *(chan->sumQ))); #endif } } else { /* I values in the even positions */ /* We are only using the upper 15 bits of the ADC. A '1' in the least significant bit indicates an overflow in I*/ isOverflowI = *(chan->data0+jj) & 1; if (isOverflowI) { if (overflowICount < PAD_MAX_INT) { overflowICount++; } else { overflowICount = 0; numOverflowIRollovers++; } } else { *(chan->proc0+jj) = *(chan->scalar+ii) * (*(chan->data0+jj)); *(chan->sumI) += *(chan->proc0+jj); *(chan->numI) +=1; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag,("padGenReadSignal: sumI now: %lld\n", *(chan->sumI))); #endif } } #endif jj++; } } /* We've finished reading out the FIFO now. Do the operations on the full dataset, data0[]... */ /* Save the number of 16-bit words read */ *(chan->nRead) = jj; #ifdef PAD_PROCESS_DURING_READ /* Calculate the average Q and I */ if (*(chan->numQ) == 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: ch%d numQ is zero\n", mm)); #endif return OK; } else { *(chan->avgQ) = (short)(*(chan->sumQ)/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numQ))); #ifdef DATA_LOGGING if ((chan == chan3) && (!Qdone)) { if (logQindex == PAD_NUM_AVGS_TO_SAVE) { printf("padGenReadSignal: Q done\n"); Qdone = 1; } else { /* keep filling array */ padLogQavg[logQindex] = *(chan->avgQ); logQindex++; } } #endif } if (*(chan->numI) == 0) { DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: ch%d numI is zero\n", mm)); return OK; } else { *(chan->avgI) = (short)(*(chan->sumI)/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numI))); #ifdef DATA_LOGGING if ((chan == chan3) && (!Idone)) { if (logIindex == PAD_NUM_AVGS_TO_SAVE) { printf("padGenReadSignal: done\n"); Idone = 1; } else { /* keep filling array */ padLogIavg[logIindex] = *(chan->avgI); logIindex++; } } #endif } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: average Q:%ld\n", *(chan->avgQ))); DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: average I:%ld\n", *(chan->avgI))); #endif #endif #ifdef USING_TIMER timers[timer_ii]=benchmark_timer_read(); timer_ii++; #endif } /* mm loop */ return OK; } /*============================================================================= Name: padIfqReadSignal Abs: Use this subroutine for channels where the signal is not coming in at 4 times the 25.5 MHz clock. Eg. for 119 MHz ref. Here, there is an I and a Q for EACH point of the waveform. Interlacing not possible. Working with padChannel[0..3] and average{I,Q}[0..3], calculate the standard deviation, padSigma{I,Q}[0..3] as: 1/n*sum((padChannel[0..3]-average{I,Q}[0..3])^2) Inp: What else must be set up first: padSampleOffset, padSampleSize What is set up after: padSigmaI[0..3], padSigmaQ[0..3] =============================================================================*/ int padIfqReadSignal(rf_channel_t* chan0, rf_channel_t* chan1, rf_channel_t* chan2, rf_channel_t* chan3) { int ii; /* absolute position in the FIFO */ int jj; /* the (ii - padSampleOffset)-th position in FIFO */ int mm; /* loop variable for all channels */ #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; #ifdef PAD_PROCESS_DURING_READ int isOverflow; long long diff; long long runningAverageI; long long runningAverageQ; #endif rf_channel_t* chan = NULL; /* generic channel used in loop of all channels */ #ifndef FAKE_DATA pSrc = csar1_actual; #endif /* Fills data0, calculates avgI and avgQ for all four channels */ for (mm=0; mmnToRead) = *(chan->ofst) + *(chan->sz); #ifndef FAKE_DATA #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: initiating chip select read at 0x%08x for %d words\n", (unsigned int) csar1_actual, *(chan->nToRead))); #endif #else printf("padIfqReadSignal: faking channel %d data for %d words\n", mm, *(chan->sz)); #endif /* Here are the guts of the read. This code was timed and found to take 250 ns to read one short */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: guts of read for channel %d\n",mm)); #endif jj = 0; *(chan->sumQ) = 0; *(chan->sumI) = 0; *(chan->numQ) = 0; *(chan->numI) = 0; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: initially sumQ is %lld\n", *(chan->sumQ))); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: initially numQ is %d\n", *(chan->numQ))); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: initially sumI is %lld\n", *(chan->sumI))); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: initially numI is %d\n", *(chan->numI))); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: srcOfst is %d\n", srcOfst)); #endif /* THIS IS WRONG. DOING THIS PUTS REF CH2 at -180 deg off of where it was for s/w loaded under Pad-R1-3-7 #ifndef FAKE_DATA Skip first three clock cycles to allow FIFO to fill *(pSrc+srcOfst); *(pSrc+srcOfst); *(pSrc+srcOfst); #endif */ for (ii=0; ii<*(chan->nToRead); ii++) { /* Only copy data after the offset to region of interest */ /* *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); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: read one short\n")); #endif #endif } else { #ifndef FAKE_DATA *(chan->data0+jj) = *(pSrc+srcOfst); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: stored one short, %hd, in data0\n", *(chan->data0+jj))); #endif #else /* Make fake data */ *(chan->data0+jj) = ii; #endif #ifdef PAD_PROCESS_DURING_READ /* Check for data overflow in this channel's raw data. Since there is a value of I and Q for each jj, increment counters for both types of overflows. We are only using the upper 15 bits of the ADC. A '1' in the least significant bit indicates an overflow */ isOverflow = *(chan->data0+jj) & 1; if (isOverflow) { if (overflowQCount < PAD_MAX_INT) { overflowQCount++; overflowICount++; } else { overflowQCount = 0; numOverflowQRollovers++; overflowICount = 0; numOverflowIRollovers++; } } else { *(chan->procQ+jj) = (*(chan->scalarQ+ii) * (*(chan->data0+jj))); *(chan->sumQ) += *(chan->procQ+jj); *(chan->numQ) += 1; *(chan->procI+jj) = (*(chan->scalarI+ii) * (*(chan->data0+jj))); *(chan->sumI) += *(chan->procI+jj); *(chan->numI) +=1; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal %d: scaling %hd by %hd to processed Q: %ld\n", ii, *(chan->data0+jj), *(chan->scalarQ+ii), *(chan->procQ+jj))); DEBUGPRINT(DP_INFO, drvPadIfqFlag,("padIfqReadSignal: sumQ now: %lld\n", *(chan->sumQ))); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal %d: scaling %hd by %hd to processed I: %ld\n", ii, *(chan->data0+jj), *(chan->scalarI+ii), *(chan->procI+jj))); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: sumI now: %lld\n", *(chan->sumI))); #endif } #endif jj++; } } /* We've finished reading out the FIFO now. Do the operations on the full dataset, data0[]... */ /* Save the number of 16-bit words read */ *(chan->nRead) = jj; #ifdef PAD_PROCESS_DURING_READ /* Calculate the average Q and I */ if (*(chan->numQ) == 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: numQ is zero\n")); #endif return OK; } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: avgQ is %lld/%hd/%d\n",*(chan->sumQ),PAD_SCALED_DATA_NORMALIZATION,*(chan->numQ))); #endif *(chan->avgQ) = (short) (*(chan->sumQ)/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numQ))); #ifdef DATA_LOGGING if ((chan == chan3) && (!Qdone)) { if (logQindex == PAD_NUM_AVGS_TO_SAVE) { printf("padIfqReadSignal: Q done\n"); Qdone = 1; } else { /* keep filling array */ padLogQavg[logQindex] = *(chan->avgQ); logQindex++; } } #endif } if (*(chan->numI) == 0) { DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: numI is zero\n")); return OK; } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: avgI is %lld/%hd/%d\n",*(chan->sumI),PAD_SCALED_DATA_NORMALIZATION,*(chan->numI))); #endif *(chan->avgI) = (short)(*(chan->sumI)/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numI))); #ifdef DATA_LOGGING if ((chan == chan3) && (!Idone)) { if (logIindex == PAD_NUM_AVGS_TO_SAVE) { printf("padIfqReadSignal: done\n"); Idone = 1; } else { /* keep filling array */ padLogIavg[logIindex] = *(chan->avgI); logIindex++; } } #endif } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: average Q:%ld\n", *(chan->avgQ))); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: average I:%ld\n", *(chan->avgI))); #endif #endif #ifdef USING_TIMER timers[timer_ii]=benchmark_timer_read(); timer_ii++; #endif } /* first mm loop */ /* Loop through the data again, summing the squares of the difference between each data point and its average */ for (mm=0; mmsumSqQ = 0; chan->sumSqI = 0; #ifdef PAD_PROCESS_DURING_READ for (ii=0; ii<*(chan->nRead); ii=ii+6) { runningAverageQ = *(chan->procQ+ii); runningAverageQ += *(chan->procQ+ii+1); runningAverageQ += *(chan->procQ+ii+2); runningAverageQ += *(chan->procQ+ii+3); runningAverageQ += *(chan->procQ+ii+4); runningAverageQ += *(chan->procQ+ii+5); runningAverageQ /= 6; runningAverageQ /= PAD_SCALED_DATA_NORMALIZATION; diff = runningAverageQ-(*(chan->avgQ)); chan->sumSqQ += (diff * diff); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadRefFlag, ("ch%d: Q%d: %ld Q%d: %ld Q%d: %ld Q%d: %ld Q%d: %ld Q%d: %ld avgQ: %ld\n", mm, ii, *(chan->procQ+ii), ii+1, *(chan->procQ+ii+1), ii+2, *(chan->procQ+ii+2), ii+3, *(chan->procQ+ii+3), ii+4, *(chan->procQ+ii+4), ii+5, *(chan->procQ+ii+5), (*(chan->procQ+ii) + *(chan->procQ+ii+1) + *(chan->procQ+ii+2) + *(chan->procQ+ii+3 + *(chan->procQ+ii+4 + *(chan->procQ+ii+5)/6))))); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("%d: avg of two Qs/normalzn: %lld - Qavg: %ld = %lld. diff squared is %lld ", ii, runningAverageQ,*(chan->avgQ), diff, (diff * diff))); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("and sumSqQ is: %lld\n",chan->sumSqQ)); #endif runningAverageI = *(chan->procI+ii); runningAverageI += *(chan->procI+ii+1); runningAverageI += *(chan->procI+ii+2); runningAverageI += *(chan->procI+ii+3); runningAverageI += *(chan->procI+ii+4); runningAverageI += *(chan->procI+ii+5); runningAverageI /= 6; runningAverageI /= PAD_SCALED_DATA_NORMALIZATION; diff = runningAverageI-(*(chan->avgI)); chan->sumSqI += (diff * diff); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadRefFlag, ("ch%d: I%d: %ld I%d: %ld I%d: %ld I%d: %ld I%d: %ld I%d: %ld avgI: %ld\n", mm, ii, *(chan->procI+ii), ii+1, *(chan->procI+ii+1), ii+2, *(chan->procI+ii+2), ii+3, *(chan->procI+ii+3), ii+4, *(chan->procI+ii+4), ii+5, *(chan->procI+ii+5), (*(chan->procI+ii) + *(chan->procI+ii+1) + *(chan->procI+ii+2) + *(chan->procI+ii+3 + *(chan->procI+ii+4 + *(chan->procI+ii+5)/6))))); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("%d: avg of two Is/normalzn: %lld - Iavg: %ld = %lld. diff squared is %lld ", ii, runningAverageI,*(chan->avgI), diff, (diff * diff))); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("and sumSqI is: %lld\n",chan->sumSqI)); #endif } /* CHECK: N (padSigma) vs N-1 (s) */ if (*(chan->numI) == 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: numI is zero\n")); #endif return OK; } else { chan->sigmaSqI = (chan->sumSqI)/(*(chan->numI)/6); } if (*(chan->numQ) == 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: numQ is zero\n")); #endif return OK; } else { chan->sigmaSqQ = (chan->sumSqQ)/(*(chan->numQ)/6); } /* NO SQRT IS AVAIL ON CF, SO SEND SQUARED TERM TO VME FOR THIS */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: sum of squares for Q:%lld\n", chan->sumSqI)); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: sum of squares for I:%lld\n", chan->sumSqQ)); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: sigma squared for Q:%ld\n", chan->sigmaSqI)); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: sigma squared for I:%ld\n", chan->sigmaSqQ)); #endif #endif #ifdef USING_TIMER timers[timer_ii]=benchmark_timer_read(); timer_ii++; #endif } return OK; } /*============================================================================= Name: padRefReadSignal Abs: Call padGenReadSignal from within here to fill padChannel[0..3] arrays with data AND calculate the average I and Q for each channel. Working with padChannel[0..3] and average{I,Q}[0..3], calculate the standard deviation, padSigma{I,Q}[0..3] as: 1/n*sum((padChannel[0..3]-average{i,Q}[0..3])^2) Inp: What else must be set up first: padSampleOffset, padSampleSize What is set up after: padSigmaI[0..3], padSigmaQ[0..3] =============================================================================*/ int padRefReadSignal(rf_channel_t* chan0, rf_channel_t* chan1, rf_channel_t* chan2, rf_channel_t* chan3) { int rc; int ii; /* position in sample */ int mm; /* loop variable for all channels */ #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. pSrc NOT NEEDED, BUT SEE 5/12/2009 NOTE BELOW */ #endif #ifdef PAD_PROCESS_DURING_READ long long diff; long long runningAverageI; long long runningAverageQ; #endif rf_channel_t* chan = NULL; /* generic channel used in loop of all channels */ int srcOfst = 0; /* NOT NEEDED, BUT SEE 5/12/2009 NOTE BELOW */ /* Fills data0, calculates avgI and avgQ for all four channels */ rc = padGenReadSignal(chan0, chan1, chan2, chan3); /* Loop through the data again, summing the squares of the difference between each data point and its average */ for (mm=0; mmsumSqQ = 0; chan->sumSqI = 0; #ifndef FAKE_DATA /* 5/12/2009: THIS SHOULD NOT BE PART OF REF BECAUSE REF DOES THIS VIA ITS CALL TO GEN, BUT!!! NOT DOING THIS CAUSES A JUMP IN PHASE OF -90 deg RELATIVE TO PREV VERSION */ /* Skip first three clock cycles to allow FIFO to fill */ *(pSrc+srcOfst); *(pSrc+srcOfst); *(pSrc+srcOfst); #endif #ifdef PAD_PROCESS_DURING_READ for (ii=0; ii<(*(chan->nRead)); ii=ii+4) { runningAverageI = (*(chan->proc0+ii) + *(chan->proc0+ii+2))/2/PAD_SCALED_DATA_NORMALIZATION; diff = runningAverageI-(*(chan->avgI)); chan->sumSqI += (diff * diff); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadRefFlag, ("ch%d: I%d: %ld I%d: %ld avgI: %ld\n", mm, ii, *(chan->proc0+ii), ii+2, *(chan->proc0+ii+2), (*(chan->proc0+ii) + *(chan->proc0+ii+2))/2)); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("%d: avg of two Is/normalzn: %lld - Iavg: %ld = %lld. diff squared is %lld ", ii, runningAverageI,*(chan->avgI), diff, (diff * diff))); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("and sumSqI is: %lld\n",chan->sumSqI)); #endif runningAverageQ = (*(chan->proc0+ii+1) + *(chan->proc0+ii+3))/2/PAD_SCALED_DATA_NORMALIZATION; diff = runningAverageQ-(*(chan->avgQ)); chan->sumSqQ += (diff * diff); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadRefFlag, ("ch%d: Q%d: %ld Q%d: %ld avgQ: %ld\n", mm, ii+1, *(chan->proc0+ii+1), ii+3, *(chan->proc0+ii+3), (*(chan->proc0+ii+1) + *(chan->proc0+ii+3))/2)); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("%d: avg of two Qs/normalzn: %lld - Qavg: %ld = %lld. diff squared is %lld ", ii, runningAverageQ,*(chan->avgQ), diff, (diff * diff))); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("and sumSqQ is: %lld\n",chan->sumSqQ)); #endif } /* CHECK: N (padSigma) vs N-1 (s) */ if (*(chan->numI) == 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadRefFlag, ("padRefReadSignal: ch%d numI is zero\n", mm)); #endif return OK; } else { chan->sigmaSqI = (long)((chan->sumSqI)/(*(chan->numI))/4); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("padRefReadSignal: sigma squared for I:%lu\n", chan->sigmaSqQ)); } if (*(chan->numQ) == 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadRefFlag, ("padRefReadSignal: ch%d numQ is zero\n", mm)); #endif return OK; } else { chan->sigmaSqQ = (long)((chan->sumSqQ)/(*(chan->numQ))/4); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("padRefReadSignal: sigma squared for Q:%lu\n", chan->sigmaSqI)); } /* NO SQRT IS AVAIL ON CF, SO SEND SQUARED TERM TO VME FOR THIS */ #endif #ifdef USING_TIMER timers[timer_ii]=benchmark_timer_read(); timer_ii++; #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 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)); 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) { DEBUGPRINT(DP_INFO, drvPadFlag, ("padKlyReadSignal: Channel %d: num pts is zero.\n", mm)); return OK; } else { chan->avgS = chan->sumS/chan->numS; } } /* mm loop */ 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 { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetYIntercept: vertical lines. slopes are inf\n")); return ERROR; #endif } 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 /*============================================================================= Goal: find the avg I and avg Q for two samples in the FIFO =============================================================================*/ int padBphReadSignal(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 kk; /* (ii-*(ofst+1))th position in FIFO */ int mm; #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; #ifdef PAD_PROCESS_DURING_READ int isQ; /* knowing order of data in FIF0, [I,Q,I,Q], can find Q where counter is an odd number */ int isOverflowI; int isOverflowQ; #endif int indexAfterSample0; /* use to determine relative distance to sample 1 */ rf_channel_t* chan = NULL; /* Loop through the data again, summing the squares of the difference between each data point and its average */ for (mm=0; mmsumI) = 0; *(chan->sumQ) = 0; *((chan->sumI)+1) = 0; *((chan->sumQ)+1) = 0; *(chan->numI) = 0; *(chan->numQ) = 0; *((chan->numI)+1) = 0; *((chan->numQ)+1) = 0; *(chan->nToRead) = 0; *((chan->nToRead)+1) = 0; *(chan->nRead) = 0; *((chan->nRead)+1) = 0; /* 1. Find the average I and average Q of the first sample. */ /* NB: the number read is the SUM Of the Is and the Qs for one channel */ /* 1.1 Check range of first sample */ if (*(chan->ofst) + *(chan->sz) > PAD_FIFO_SIZE) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadBphFlag, ("padBphReadSignal: s0 surpasses end of FIFO! %d + %d > %d\n", *(chan->ofst), *(chan->sz), PAD_FIFO_SIZE)); #endif /* return is the wrong action here. It means that the remaining channels are skipped over. return ERROR; Instead, use "continue", so that executions goes to the next channel */ continue; } *(chan->nToRead) = *(chan->ofst) + *(chan->sz); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("\nSample 0\n")); #endif #ifndef FAKE_DATA pSrc = csar1_actual; /* Skip first three clock cycles to allow FIFO to fill */ *(pSrc+srcOfst); *(pSrc+srcOfst); *(pSrc+srcOfst); #endif /* 1.2 Read first sample */ for (ii=0; ii<*(chan->nToRead); ii++) { if (ii < *(chan->ofst)) { /* 1.2.1 Read up to start first sample */ #ifndef FAKE_DATA *(pSrc+srcOfst); #endif } else { /* 1.2.2 Read and store first sample */ #ifndef FAKE_DATA *(chan->data0+jj) = *(pSrc+srcOfst); #else /* FAKE THE DATA */ *(chan->data0+jj) = (short) ii; #endif #ifdef PAD_PROCESS_DURING_READ /* Start by multiplying the signal by the scalar (combined sin/cos and normalization) vector. NB: the index of scalar must be offset by the points skipped over during read */ isQ = ii & 1; /* examine last bit to determine odd/even */ if (isQ == 1) { /* Q values in the odd positions */ isOverflowQ = *(chan->data0+jj) & 1; if (isOverflowQ) { if (overflowQCount < PAD_MAX_INT) { overflowQCount++; } else { overflowQCount = 0; numOverflowQRollovers++; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s0 overflow in Q \n", mm)); #endif } else { /* Good data. */ *(chan->proc0+jj) = *(chan->scalar+ii) * (*(chan->data0+jj)); *(chan->sumQ) += *(chan->proc0+jj); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s0 ch%d: scale %hd by %hd to Q[%d]: %ld\n", mm, (*(chan->data0+jj)), *(chan->scalar+ii), *(chan->numQ), *(chan->proc0+jj))); DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s0 ch%d: sumQ now: %lld\n", mm, *(chan->sumQ))); #endif *(chan->numQ) += 1; } } else { /* I values in the even positions */ isOverflowI = *(chan->data0+jj) & 1; if (isOverflowI) { if (overflowICount < PAD_MAX_INT) { overflowICount++; } else { overflowICount = 0; numOverflowIRollovers++; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s0 overflow in I \n", mm)); #endif } else { /* Good data. */ *(chan->proc0+jj) = *(chan->scalar+ii) * (*(chan->data0+jj)); *(chan->sumI) += *(chan->proc0+jj); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s0 ch%d: scale %hd by %hd to I[%d]: %ld\n", mm, (*(chan->data0+jj)), *(chan->scalar+ii), *(chan->numI), *(chan->proc0+jj))); DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s0 ch%d: sumI now: %lld\n", mm, *(chan->sumI))); #endif *(chan->numI) += 1; } } #endif jj++; } } /* Save the number of 16-bit words read into sample 1 */ *(chan->nRead) = jj; /* Save the current position in the FIFO */ indexAfterSample0 = ii; /* Calculate first sample's average I and Q */ /* I: */ if (*(chan->numI) <= 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s0 numI is zero. Can't calc avgI\n", mm)); #endif /* return is the wrong action here. It means that the remaining channels are skipped over. return OK; Instead, use "continue", so that executions goes to the next channel */ continue; } *(chan->avgI) = ((*(chan->sumI))/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numI))); /* Q: */ if (*(chan->numQ) <= 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s0 numQ is zero. Can't calc avgQ\n", mm)); #endif /* return is the wrong action here. It means that the remaining channels are skipped over. return OK; Instead, use "continue", so that executions goes to the next channel */ continue; } *(chan->avgQ) = ((*(chan->sumQ))/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numQ))); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("\npadBphReadSignal: ch%d sample 0\n", mm)); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" sumQ %lld\n", *(chan->sumQ))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" sumI %lld\n", *(chan->sumI))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" numQ %d\n", *(chan->numQ))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" numI %d\n", *(chan->numI))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" avgI %d\n", (int) *(chan->avgI))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" avgQ %d\n", (int) *(chan->avgQ))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" after s0, position in FIFO is %d\n", indexAfterSample0)); #endif /* 3. Find the average I and average Q of the second sample */ /* NB: the number read is the SUM Of the Is and the Qs */ /* 3.1 Check range of second sample */ if (*(chan->ofst+1) < indexAfterSample0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadBphFlag, ("padBphReadSignal: s1 starts before end of s0! %d < %d\n", *(chan->ofst+1), indexAfterSample0)); #endif /* return is the wrong action here. It means that the remaining channels are skipped over. return ERROR; Instead, use "continue", so that executions goes to the next channel */ continue; } if (*(chan->ofst+1) + *(chan->sz+1) > PAD_FIFO_SIZE) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadBphFlag, ("padBphReadSignal: s1 surpasses end of FIFO! %d + %d > %d\n", *(chan->ofst+1), *(chan->sz+1), PAD_FIFO_SIZE)); #endif /* return is the wrong action here. It means that the remaining channels are skipped over. return ERROR; Instead, use "continue", so that executions goes to the next channel */ continue; } *(chan->nRead+1) = *(chan->ofst+1) + *(chan->sz+1); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: already at %d, skipping next %d shorts to read %d\n", indexAfterSample0,*(chan->ofst+1)-indexAfterSample0, *(chan->sz+1))); #endif /* 3.2 Read sample 1 */ *(chan->nToRead+1) = *(chan->ofst+1) + *(chan->sz+1); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("\nSample 1\n")); #endif for (ii=indexAfterSample0; ii<*(chan->nToRead+1); ii++) { if (ii < *(chan->ofst+1)) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_DEBUG, drvPadBphFlag, ("skipping ii %d\n", ii)); #endif #ifndef FAKE_DATA *(pSrc+srcOfst); #endif } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_DEBUG, drvPadBphFlag, ("storing ii %d\n", ii)); #endif #ifndef FAKE_DATA *(chan->data1+kk) = *(pSrc+srcOfst); #else /* Make fake data */ *(chan->data1+kk) = (short) ii; #endif #ifdef PAD_PROCESS_DURING_READ isQ = ii & 1; /* examine last bit to determine odd/even */ if (isQ == 1) { /* Q values in the odd positions */ isOverflowQ = *(chan->data1+kk) & 1; if (isOverflowQ) { if (overflowQCount < PAD_MAX_INT) { overflowQCount++; } else { overflowQCount = 0; numOverflowQRollovers++; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s1 overflow in Q \n", mm)); #endif } else { /* Good data. */ *(chan->proc1+kk) = *(chan->scalar+ii)*(*(chan->data1+kk)); *(chan->sumQ+1) += *(chan->proc1+kk); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s1 ch%d: scale %hd by %hd to Q[%d]: %ld\n", mm, (*(chan->data1+kk)), *(chan->scalar+ii), *(chan->numQ+1), *(chan->proc1+kk))); DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s1 ch%d: sumQ now: %lld\n", mm, *(chan->sumQ+1))); #endif *(chan->numQ+1) += 1; } } else { /* We are only using the upper 15 bits of the ADC. A '1' in the least significant bit indicates an overflow in I*/ isOverflowI = *(chan->data1+kk) & 1; if (isOverflowI) { if (overflowICount < PAD_MAX_INT) { overflowICount++; } else { overflowICount = 0; numOverflowIRollovers++; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s1 overflow in I \n", mm)); #endif } else { /* Good data. */ *(chan->proc1+kk) = *(chan->scalar+ii)*(*(chan->data1+kk)); *(chan->sumI+1) += *(chan->proc1+kk); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s1 ch%d: scale %hd by %hd to Q[%d]: %ld\n", mm, (*(chan->data1+kk)), *(chan->scalar+ii), *(chan->numI+1), *(chan->proc1+kk))); DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s1 ch%d: sumI now: %lld\n", mm, *(chan->sumI+1))); #endif *(chan->numI+1) += 1; } } #endif kk++; } } /* Save the number of 16-bit words read into sample 2 */ *(chan->nRead+1) = kk; #ifdef PAD_PROCESS_DURING_READ /* 4. Calculate second sample's average I and Q */ /* I: */ if (*(chan->numI+1)<=0) { #ifdef DEBUGPRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s1 numI is zero. Can't calc avgI\n", mm)); #endif /* return is the wrong action here. It means that the remaining channels are skipped over. return OK; Instead, use "continue", so that executions goes to the next channel */ continue; } *(chan->avgI+1) = (*(chan->sumI+1))/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numI+1)); /* Q: */ if (*(chan->numQ+1)<=0) { #ifdef DEBUGPRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s1 numQ is zero. Can't calc avgQ\n", mm)); #endif /* return is the wrong action here. It means that the remaining channels are skipped over. return OK; Instead, use "continue", so that executions goes to the next channel */ continue; } *(chan->avgQ+1) = (*(chan->sumQ+1))/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numQ+1)); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("\npadBphReadSignal: ch%d sample 1\n", mm)); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" sumQ %lld\n", *(chan->sumQ+1))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" sumI %lld\n", *(chan->sumI+1))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" numQ %d\n", *(chan->numQ+1))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" numI %d\n", *(chan->numI+1))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" avgI %ld\n", *(chan->avgI+1))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" avgQ %ld\n", *(chan->avgQ+1))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" after sample 0, position in FIFO is %d\n", indexAfterSample0)); #endif #endif } /* mm loop */ return OK; } /*============================================================================= 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: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadGetSigFlag, ("padGetSignal: unrecognized sample: %d\n", sample)); #endif 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: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadGetSigFlag, ("padGetSignal: unrecognized channel: %d\n", channel)); #endif return (void*) NULL; } break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadGetSigFlag, ("padGetSignal: unrecognized input type: %d\n", inputType)); #endif 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: 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: padSetDataFileName Abs: =============================================================================*/ /* Create unique filename by appending first the "seconds past Epoch" and .dat" */ int padSetDataFileName() { char timestampForFile[20]; epicsTimeStamp timeAtWrite; strcpy(filename, padGetDataFileStem()); epicsTimeGetCurrent(&timeAtWrite); printf("sec=%d\n",timeAtWrite.secPastEpoch); /* seconds since 0000 Jan 1, 1990 */ printf("nsec=%d\n",timeAtWrite.nsec); /* nanoseconds */ sprintf(timestampForFile,"%lu", (unsigned long) timeAtWrite.secPastEpoch); printf("%s\n",timestampForFile); strcat(filename, timestampForFile); strcat(filename, ".dat"); printf("padSetDataFileName: complete filename is now '%s'\n",filename); return OK; } /*============================================================================= Name: padGetDataFileName Abs: =============================================================================*/ char * padGetDataFileName() { return filename; } /*============================================================================= Name: padDumpDataToFile Abs: data for all channel structures get written The second sample, sample '1', is only dumped for the PAD_BPH case =============================================================================*/ int padDumpDataToFile() { int jj; int kk; int rc; FILE *fout; /* Create a new file for this acquisition */ rc = padSetDataFileName(); fout = fopen(padGetDataFileName(), "w"); if (fout == NULL) { fprintf(stderr, "padDumpDataToFile: error opening file '%s': %s\n", filename, strerror(errno)); return ERROR; } else { printf("padDumpDataToFile: writing to file '%s'\n", filename); } for (jj=0; jjsz, filename); fprintf(fout,"Channel %d Sample 0: ",jj); for (kk=0; kk<*pChannels[jj]->sz; kk++) { fprintf (fout,"%d ", (int) *(pChannels[jj]->data0+kk)); } fprintf(fout,"\n"); #ifdef PAD_PROCESS_DURING_READ fprintf(fout, "sumQ %lld\n", *(pChannels[jj]->sumQ)); fprintf(fout, "sumI %lld\n", *(pChannels[jj]->sumI)); fprintf(fout, "numQ %d\n", *(pChannels[jj]->numQ)); fprintf(fout, "numI %d\n", *(pChannels[jj]->numI)); #endif #ifdef PAD_BPH fprintf(fout,"Channel %d Sample 1: ", jj); printf("padDumpDataToFile: channel %d: writing %d shorts to '%s'\n", jj, *(pChannels[jj]->sz+1), filename); for (kk=0; kk<*(pChannels[jj]->sz+1); kk++) { fprintf (fout,"%d ", (int) *(pChannels[jj]->data1+kk)); } fprintf(fout,"\n"); #ifdef PAD_PROCESS_DURING_READ fprintf(fout, "sumQ %llu\n", *(pChannels[jj]->sumQ+1)); fprintf(fout, "sumI %llu\n", *(pChannels[jj]->sumI+1)); fprintf(fout, "numQ %d\n", *(pChannels[jj]->numQ+1)); fprintf(fout, "numI %d\n", *(pChannels[jj]->numI+1)); #endif #endif } fclose(fout); return OK; } /*============================================================================= Name: padDumpCalDataToFile Abs: data for all calibration channel structures get written =============================================================================*/ int padDumpCalDataToFile() { int jj; int kk; FILE *fout; char filename[40]; for (jj=0; jjsz, filename); } for (kk=0; kksz; kk++) { fprintf (fout,"%d\n", (int) *(pCalChannels[jj]->data0+kk)); } fclose(fout); } return OK; } /*============================================================================= Name: padDumpNonLinDataToFile Abs: Pass in num of channel 0 raw datasets you want written to server (one per file). No timestamping into filename is done because files are named so that they match names expected by padHist executable. =============================================================================*/ int padDumpNonLinDataToFile(int fileprefix) { int kk; FILE *fout0; FILE *fout1; FILE *fout2; FILE *fout3; char filename0[40]; char filename1[40]; char filename2[40]; char filename3[40]; /* Create 4 new files for this acquisition */ sprintf(filename0,"/data/zero/%d.dat",fileprefix); sprintf(filename1,"/data/one/%d.dat",fileprefix); sprintf(filename2,"/data/two/%d.dat",fileprefix); sprintf(filename3,"/data/three/%d.dat",fileprefix); /* Open the 4 new files */ fout0 = fopen(filename0, "w"); if (fout0 == NULL) { fprintf(stderr, "padDumpNonLinDataToFile: error opening file '%s': %s\n", filename0, strerror(errno)); return ERROR; } else { printf("padDumpNonLinDataToFile: writing to file '%s'\n", filename0); } fout1 = fopen(filename1, "w"); if (fout1 == NULL) { fprintf(stderr, "padDumpNonLinDataToFile: error opening file '%s': %s\n", filename1, strerror(errno)); return ERROR; } else { printf("padDumpNonLinDataToFile: writing to file '%s'\n", filename1); } fout2 = fopen(filename2, "w"); if (fout2 == NULL) { fprintf(stderr, "padDumpNonLinDataToFile: error opening file '%s': %s\n", filename2, strerror(errno)); return ERROR; } else { printf("padDumpNonLinDataToFile: writing to file '%s'\n", filename2); } fout3 = fopen(filename3, "w"); if (fout3 == NULL) { fprintf(stderr, "padDumpNonLinDataToFile: error opening file '%s': %s\n", filename3, strerror(errno)); return ERROR; } else { printf("padDumpNonLinDataToFile: writing to file '%s'\n", filename3); } /* Write the data to the files */ printf("padDumpNonLinDataToFile: calibration channel 0: writing %d shorts to '%s'\n", pCalChannels[0]->sz, filename0); for (kk=0; kksz; kk++) { fprintf (fout0,"%d\n", (int) *(pCalChannels[0]->data0+kk)); } printf("padDumpNonLinDataToFile: calibration channel 1: writing %d shorts to '%s'\n", pCalChannels[1]->sz, filename1); for (kk=0; kksz; kk++) { fprintf (fout1,"%d\n", (int) *(pCalChannels[1]->data0+kk)); } printf("padDumpNonLinDataToFile: calibration channel 2: writing %d shorts to '%s'\n", pCalChannels[2]->sz, filename2); for (kk=0; kksz; kk++) { fprintf (fout2,"%d\n", (int) *(pCalChannels[2]->data0+kk)); } printf("padDumpNonLinDataToFile: calibration channel 3: writing %d shorts to '%s'\n", pCalChannels[3]->sz, filename3); for (kk=0; kksz; kk++) { fprintf (fout3,"%d\n", (int) *(pCalChannels[3]->data0+kk)); } /* Close the files */ fclose(fout0); fclose(fout1); fclose(fout2); fclose(fout3); return OK; } /*============================================================================= Name: padDumpAvgToFile Abs: =============================================================================*/ int padDumpAvgToFile() { int nn; FILE *fout; fout = fopen("junk.dat", "w"); if (fout == NULL) { fprintf(stderr, "padDumpQavgToFile: error opening file 'junk.dat': %s\n", strerror(errno)); return ERROR; } else { printf("padDumpQavgToFile: writing q averages to file 'junk.dat'\n"); } for (nn=0; nn longest_time_at_done) { printf("new worst case: %d\n", timers[timer_ii-1]); padUpdateWorstCase(); } #endif #endif #ifdef PAD_BPH /* case of Beam Phase Cav1 and Beam Phase Cav2 */ /* Beam phasing cavity algorithm for channels 0 + 1 */ /* 12/19/2006 now Beam Phase Cav2 is in ch2+3 */ padBphReadSignal(&padCh0,&padCh1,&padCh2,&padCh3); #endif #ifdef PAD_KLY /* case of RFGun Kly, L0A Kly, L0B Kly, TCav Kly, L1S Kly, L1X Kly, L2TCav Kly and L1X */ /* Average I, Q algoritm for channels 0,1 and 3 */ /* Simple average S for channel 2 */ padGenReadSignal(&padCh0,&padCh1,NULL,&padCh3); padSimpReadSignal(NULL,NULL,&padCh2,NULL); #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) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadFlag, ("padDacqTask: timed out waiting for data to be available to read out of FIFO\n")); #endif return ERROR; } else { if (waitStatus == epicsEventWaitError) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadFlag, ("padDacqTask: error waiting for data to be available to read out of FIFO\n")); #endif 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; ii 500 KHz. KEEP THIS NUMBER */ #endif /* Allocate memory for the data once only. This used to be done every time sample size changed, but now we do it once for the max size (PAD_SAMPLE_SIZE for operational channels and PAD_FIFO_SIZE for calibration channels */ #ifdef PAD_GEN /* Average I, Q algoritm for channels 0-3 */ for (ii=0; ii<4; ii++) { pChannels[ii]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[ii]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); } for (ii=4; ii<7; ii++) { pCalChannels[ii-4]->data0 = malloc(sizeof(short)*PAD_FIFO_SIZE); } #endif #ifdef PAD_REF /* Average I, Q and standard deviation algoritm for channels 0,1,3 */ /* Average I, Q and standard deviation algoritm for channel 2 since 119 and not 102 MHz with I and Q in their own array */ pChannels[0]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[0]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[1]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[1]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[2]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[2]->procI = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[2]->procQ = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[3]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[3]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); for (ii=4; ii<7; ii++) { pCalChannels[ii-4]->data0 = malloc(sizeof(short)*PAD_FIFO_SIZE); } #endif #ifdef PAD_LSR /* Average I, Q and standard deviation algoritm for channel 0 */ /* 12/19/2006 now reading ch2+3 as IFQ since they are 119 and not 102 MHz */ pChannels[0]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[0]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[1]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[1]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[2]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[2]->procI = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[2]->procQ = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[3]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[3]->procI = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[3]->procQ = malloc(sizeof(long)*PAD_SAMPLE_SIZE); for (ii=4; ii<7; ii++) { pCalChannels[ii-4]->data0 = malloc(sizeof(short)*PAD_FIFO_SIZE); } #endif #ifdef PAD_KLY /* Average I, Q algoritm for channels 0,1 and 3 */ /* Simple average S for channel 2 */ for (ii=0; ii<4; ii++) { pChannels[ii]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[ii]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); } for (ii=4; ii<7; ii++) { pCalChannels[ii-4]->data0 = malloc(sizeof(short)*PAD_FIFO_SIZE); } #endif #ifdef PAD_BPH /* Beam Phase Cav1 for channels 0+1 */ /* Beam Phase Cav2 for channels 2+3 */ pChannels[0]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[0]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[0]->data1 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[0]->proc1 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[1]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[1]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[1]->data1 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[1]->proc1 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[2]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[2]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[2]->data1 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[2]->proc1 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[3]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[3]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[3]->data1 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[3]->proc1 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); #endif /* Initialize our watchdog timer queue */ padWdTimerQ=epicsTimerQueueAllocate( 1, /* ok to share queue */ epicsThreadPriorityLow); if (padWdTimerQ==0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadFlag, ("padInitialise: error initialising watchdog timer queue\n")); #endif 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 */ #ifdef DEBUG_PRINT drvPadFlag=DP_ERROR; #endif 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 #ifdef USING_QSPI /* Here's how the queued serial port interface is used: First, set up the MUX for what you want to do. Call padQSPISetMUX and pass it the byte corresponding to what you want to do. The choices are: 0x08: Ain+ Ch0 Ain- 0x18: Ain+ Ch1 Ain- 0x28: Ain+ Ch2 Ain- 0x38: Ain+ Ch3 Ain- 0x48: Ain+ Ch4 Ain- 0x58: Ain+ Ch5 Ain- 0x68: Ain+ Ch6 Ain- 0x78: Ain+ Ch7 Ain- 0xFF: Temperature Sense Diode This byte gets appended to the first two: 0x51 0x00 and sent. Now you are ready to call padQSPIReadData()... */ int padQSPISetMUX(volatile short val) { *qdlyr = 0x040;/* something reasonable: x20 = 16usec (32/64MHz*32) */ *qir = 0xD00D; /* Turn off interrupts and clear fault flags */ *qar = 0x0; /* transmit RAM0 address0x0 */ *qdr = 0x51; /* WREG ADC command. ADS1218 manual p. 30 */ *qar = 0x20; /* command RAM0 address0x20 */ *qdr = 0x8000; /* set CS0 low (active) */ *qar = 0x1; /* transmit RAM1 address1 */ *qdr = 0x0; /* write only 1 reg */ *qar = 0x21; /* command RAM1 address0x21 */ *qdr = 0x8000; /* set CS0 low (active) */ *qar = 0x2; /* transmit RAM2 address2 */ *qdr = val; /* data to reg = 08, selectchannel0 */ *qar = 0x22; /* command RAM2 address 0x22 */ *qdr = 0x000; /* set CS0 low (active) */ *qwr = 0x1200; /* run 3 commands: write 3 bytes */ *qdlyr = 0x8040;/* start QSPI */ return OK; } long padQSPIReadData() { int done; /* keeps track of when QSPI transfer is complete */ int jj; /* keeps track of how many loops to complete QSPI transfer (info) */ unsigned int adc; /* holds 24 bit value read from ADC */ short val; *qdlyr = 0x40; /* something reasonable (was 202) */ *qir = 0xD00D; /* IMPORTANT */ /* Write 1 command byte */ *qar = 0x0; /* set address of transmit RAM0 address0 */ *qdr = 0x1; /* RDATA ADC command. right-justified. ADS1218 manual p.28 */ *qar = 0x20; /* set address of command RAM0 address0x20. */ *qdr = 0xAE00; /* Add delay (was E, now A) 8 bits to CS0 enable delay after transmit. left-justified. drive CS0 low (only) */ /* Read 3 data bytes */ *qar = 0x1; /* set address of transmit RAM1 address1 */ *qdr = 0x0; /* zero RAM for read */ *qar = 0x21; /* command RAM1 address0x21 */ *qdr = 0x8E00; /* 8 bits to CS0 disable delay after transmit */ *qar = 0x2; /* set address of transmit RAM2 address2 */ *qdr = 0x0; /* zero RAM for read */ *qar = 0x22; /* command RAM2 address0x22 */ *qdr = 0x8E00; /* 8 bits to CS0 disable delay after transmit */ *qar = 0x3; /* set address of transmit RAM3 address3 */ *qdr = 0x0; /* zero RAM for read */ *qar = 0x23; /* command RAM3 address0x23 */ *qdr = 0xE00; /* 8 bits to CS0 disable delay after transmit */ *qwr = 0x1300; /* run 4 commands: write 1 cmd byte, read 3 data bytes */ *qdlyr = 0x8040; /* start QSPI transfer (by setting qdlyr[15]) */ done = 0; jj = 0; while (!done && jj<100) { jj++; if (*qdlyr >> 15) { /* transfer still in progress */ /* epicsThreadSleep(5*epicsThreadSleepQuantum()); */ epicsThreadSleep(0.2); } else { done = 1; } } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadQspiFlag, ("number of times through loop: %d\n", jj)); #endif /* Read ADC data now that transfer is complete */ *qar = 0x11; /* set byte address of data1 */ val = *qdr; adc = val * 65536; /* contents written to most significant byte of ADC */ #ifdef DEBUG_PRINT /*printf("MSB qdr contains: %d\n", val);*/ DEBUGPRINT(DP_INFO, drvPadQspiFlag, ("after 1 byte read, qar: 0x%08x\n", *qar)); DEBUGPRINT(DP_INFO, drvPadQspiFlag, ("after 1 byte read, qdr: 0x%08x\n", *qdr)); DEBUGPRINT(DP_INFO, drvPadQspiFlag, ("after 1 byte read, adc: 0x%08x\n", adc)); #endif *qar = 0x12; /* set byte address of data2 */ /*printf("--- qdr contains: %d\n", *qdr);*/ adc += *qdr * 256; /* contents written to mid byte of ADC */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadQspiFlag, ("after 2 byte read, qar: 0x%08x\n", *qar)); DEBUGPRINT(DP_INFO, drvPadQspiFlag, ("after 2 byte read, qdr: 0x%08x\n", *qdr)); DEBUGPRINT(DP_INFO, drvPadQspiFlag, ("after 2 bytes read, adc: 0x%08x\n", adc)); #endif *qar = 0x13; /* set byte address of data3 */ /*printf("LSB qdr contains: %d\n", *qdr);*/ adc += *qdr; /* contents written to least significant byte of ADC */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadQspiFlag, ("after 3 byte read, qar: 0x%08x\n", *qar)); DEBUGPRINT(DP_INFO, drvPadQspiFlag, ("after 3 byte read, qdr: 0x%08x\n", *qdr)); DEBUGPRINT(DP_INFO, drvPadQspiFlag, ("after 3 bytes read, adc: 0x%08x\n", adc)); #endif #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadQspiFlag, ("store in EPICS record: 0x%08x\n", adc)); #endif return adc; } #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: padNonLin Abs: write a number, testSize, of raw 64K data files to server for input to a histogram program. Files are named: 0.dat, ... testSize.dat. Since there are four channels, four directories are used. See padDumpNonLinDataToFile() for exact dir names. =============================================================================*/ int padNonlin(int testSize) { int kk; printf("padNonLin: Setting up for non-linearity measurement...\n"); printf("padNonLin: Writing %d files for each channels to nonlin dir\n", testSize); printf("padNonLin: Disabling hardware interrupts\n"); padDisableIntr(); /* SINCE I'M DISABLING INTRS HERE AND CALLING padCalReadSignal BELOW, I DON'T THINK THERE IS ANY REASON TO ISSUE CAL RQST HERE. THAT WOULD JUST TRIGGER STUFF IN THE SNL. */ for (kk=0; kk= PAD_NUM_CHANNELS_ON_BOARD ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadFlag, ("padClearChannel: unrecognized channel %d\n", channel)); #endif return ERROR; } memset(pChannels[channel],0,sizeof(rf_channel_t)); return OK; }