/*============================================================================= 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) 23-may-2007, bob steele dma, diagnostic bit for timing eval 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 #include /* for IOSCANPVT */ #include /* for DRVSUPFN */ #include /* for dev*routines, atVMEA16 */ #include /* for epicsEvent* */ #include /* for epicsExportAddress */ #include /* for epicsMessageQueueCreate, ... */ #include /* for epicsTimeStamp and assoc fns */ #include /* for epicsTimerQueue for watchdog */ #include /* for epicsThreadPriorityMax and to get confdefs.h indirectly inc'd */ /* #include for in_le32 */ #include /* for DEBUGPRINT */ #include "drvPad.h" #include "flash.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 */ /* scm registers */ #define MPARK (IPSBAR+0x1C) /* Bus Master Park Register */ #define Mpark_BCR24BIT Bit(24) /* Enables the use of 24 bit byte count registers in the DMA module */ /* 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 #define array_length(a) (sizeof(a)/sizeof((a)[0])) /* only works on one dimensional arrays! */ #ifdef USING_DMA #define DMA_reqc ((unsigned int *)(IPSBAR+0x340)) /* dma request control */ void dmaTest(); /*int coldfDMATimerSetup( int chan, int dtmr ); int coldfDMAStart( int chan, uint8_t *to, uint8_t *from, uint32_t size, int ext, int poll, int tomem, int cc); */ #define DSR_DONE Bit(0) /* DMA transfer done */ #define DSR_BSY Bit(1) /* DMA transfer busy */ #define DSR_REQ Bit(2) /* DMA request */ #define DSR_RSD1 Bit(3) /* Reserved. Should be cleared. */ #define DSR_BED Bit(4) /* Bus error on destination */ #define DSR_BES Bit(5) /* Bus error on source */ #define DSR_CE Bit(6) /* Configuration error */ #define DSR_RSD2 Bit(7) /* Reserved. Should be cleared. */ #define DCR_start Bit(16) #define DCR_dsize_longword 0 #define DCR_dsize_byte Bit(17) #define DCR_dsize_word Bit(18) #define DCR_dsize_line (Bit(18)|Bit(17)) #define DCR_dinc Bit(19) #define DCR_ssize_longword 0 #define DCR_ssize_byte Bit(20) #define DCR_ssize_word Bit(21) #define DCR_ssize_line (Bit(21)|Bit(20)) #define DCR_sinc Bit(22) #define DCR_bwc_dma_priority 0 #define DCR_auto_align Bit(28) #define DCR_cycle_steal Bit(29) #define DCR_eext Bit(30) #define DCR_int Bit(31) typedef struct { volatile unsigned int source_address_register; /* SAR */ volatile unsigned int destination_address_register; /* DAR */ volatile unsigned int dma_control_register; /* DCR */ /*if MPARK[BCR24BIT] == 0 */ /* volatile unsigned short byte_count_register; / * BCR */ /* unsigned short filler_0x0e; */ volatile unsigned int byte_count_register; /* BCR */ volatile unsigned char dma_status_register; /* DSR */ } dma_channel_hardware_map; dma_channel_hardware_map *dma_channels[] = { (dma_channel_hardware_map*)(IPSBAR+0x100), (dma_channel_hardware_map*)(IPSBAR+0x140), (dma_channel_hardware_map*)(IPSBAR+0x180), (dma_channel_hardware_map*)(IPSBAR+0x1C0) }; #endif #ifdef DEBUG_BIT int diagnosticBitType = 0; #endif /* DEBUG_BIT */ /* use low priority to allow large messages (larger than the interrupt cycle) * to run over the top of the creation of subsequent messages */ #define DACQ_TASK_PRIO epicsThreadPriorityLow #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]; 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; /* 11/17/2006 change from short* */ long* proc1; /* 11/17/2006 change from short* */ 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; long yintQ; long yintI; short* rawData; /* raw data from the trigger to the offset including the fifo startup data */ int zeroOffsetDataRequested; /* request for a data sample */ short* zeroOffsetSampleData; /* sampled data from the trigger to the offset without the fifo startup data */ } 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 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 = 4; int drvPadGetSigFlag = 2; int drvPadCfgFlag = 4; int drvPadScalFlag = 4; int drvPadIntrFlag = 4; int drvPadSSFlag = 4; int drvPadGenFlag = DP_FATAL; int drvPadIfqFlag = DP_FATAL; int drvPadRefFlag = 4; int drvPadBphFlag = 4; int drvPadSimpFlag = 4; int drvPadZoffFlag = 4; int drvPadDmaFlag = DP_ERROR; #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 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 int padDumpQavgToFile(); int padDumpIavgToFile(); const int fifo_startup = 3; const char *dobootp = "DO_BOOTP"; const char *dobootpClear = "DO_BOOTP="; int test; /* remove? */ /*============================================================================= 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: %d\n", (int) *(pCh->avgQ)); printf("sumI: %lld\n", *(pCh->sumI)); printf("numI: %d\n", *(pCh->numI)); printf("avgI: %d\n", (int) *(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: %d\n", (int) *(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: %d\n", (int) *(pCh->avgI+1)); printf("yintQ: %d\n", (int) pCh->yintQ); printf("yintI: %d\n", (int) pCh->yintI); 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 if ( interest > 0 ) { 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 and for PAD_L1X_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 () { DEBUG_setDiagnosticBitType(0); 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 break; } 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 channel and sample since sample = 3 means non-interlaced I processed sample and since sample = 4 means non-interlaced Q processed sample, this is getting messy, but still possible =============================================================================*/ 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; /* here is the nasty part where we use sample==3 to mean non-interlaced I processed data and sample==4 to mean non-interlaced Q processed data */ if (sample==3 || sample==4) { /* use what is stored for sample[0] */ sample = 0; } /* 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; } #ifndef USING_DMA /* * to minimize the impact of acquiring the zero offset data, * pre-offset data is stored in a known location, * normal data is later copied to fill the location, * and data is read from fifo if required to complete the zero offset data. */ void finishZeroOffsetDataRequest( rf_channel_t* chan, int srcOfst ) { int count; if ( chan->zeroOffsetDataRequested == 0 ) return; /* nothing to do */ chan->zeroOffsetDataRequested--; if ( chan->zeroOffsetDataRequested != 0 ) return; /* nothing to do */ count = *(chan->ofst); DEBUGPRINT(DP_INFO, drvPadZoffFlag, ("finishZeroOffsetDataRequest: offset=%d\n", count )); if ( count < PAD_ZERO_OFFSET_DATA_SIZE ) { int size = *(chan->sz); if ( count + size > PAD_ZERO_OFFSET_DATA_SIZE ) size = PAD_ZERO_OFFSET_DATA_SIZE - count; DEBUGPRINT(DP_INFO, drvPadZoffFlag, ("finishZeroOffsetDataRequest: size=%d\n", size )); /* copy the required data */ memcpy( chan->rawData+count, chan->data0, sizeof(short)*size ); count += size; } /* complete the data acquistion with data from the fifo */ if ( count < PAD_ZERO_OFFSET_DATA_SIZE ) { int size = PAD_ZERO_OFFSET_DATA_SIZE - count; int i; short *datap = chan->rawData + count; volatile short *pSrc = csar1_actual + srcOfst; for ( i = 0; i < size; i++ ) *datap++ = *pSrc; DEBUGPRINT(DP_INFO, drvPadZoffFlag, ("finishZeroOffsetDataRequest: fifo pSrc=0x%x\n", (unsigned int) pSrc )); DEBUGPRINT(DP_INFO, drvPadZoffFlag, ("finishZeroOffsetDataRequest: fifo size=%d\n", size )); } DEBUGPRINT(DP_INFO, drvPadZoffFlag, ("finishZeroOffsetDataRequest: count=%d\n", count )); } #endif /* USING_DMA */ /* process the raw data for one channel */ #ifndef PAD_UNSCALED_PROCESSING void processChannelData( rf_channel_t* chan, /* channel to proces */ short *zeroOffsetDatap /* pointer to zero offset data to process */ ) { #ifdef PAD_PROCESS_DURING_READ int ii; /* absolute position in the FIFO */ short data; long *proc0p; short *scalarp; long long sumQ; long long sumI; int overflowQ; int overflowI; #endif /* calculates avgI and avgQ for one channel */ #ifdef PAD_PROCESS_DURING_READ /* presumptions: * * sample sizes are multiples of 4 */ DEBUG_setDiagnosticBitType(21); sumQ = 0; sumI = 0; int limit = chan->nToRead[0]; /* count of shorts to process */ if ( (limit & 3) != 0 ) printf( "odd size! %d\n", limit ); /* fixme: better error message */ #ifdef PROC0 #define IncrementProc0() proc0p++ #define AccumulateSum(sum,v) { *proc0p = (v); (sum) += *proc0p++; } #else #define IncrementProc0() #define AccumulateSum(sum,v) (sum) += (v) #endif overflowI = 0; overflowQ = 0; proc0p = chan->proc0; scalarp = chan->scalar; for ( ii = 0; ii < limit; ii += 2 ) { /* get the I data */ data = *zeroOffsetDatap++; DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: scaling %hd by %hd to: %ld\n", data, *scalarp, *proc0p )); /* I (even) first, Q (odd) second */ /* 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*/ if ( ( data & 1 ) != 0 ) { /* overflow! */ overflowI++; scalarp++; IncrementProc0(); } else { AccumulateSum(sumI, ( *scalarp++ * data ) ); DEBUGPRINT(DP_INFO, drvPadGenFlag,("padGenReadSignal: sumI now: %lld\n", sumI )); } /* get the Q value */ data = *zeroOffsetDatap++; /* 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 */ if ( (data & 1) != 0 ) { /* overflow! */ overflowQ++; scalarp++; IncrementProc0(); } 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 */ AccumulateSum(sumQ, ( *scalarp++ * data ) ); DEBUGPRINT(DP_INFO, drvPadGenFlag,("padGenReadSignal: sumQ now: %lld\n", sumQ )); } } limit /= 2; *(chan->numI) = limit - overflowI; *(chan->sumI) = sumI; *(chan->numQ) = limit - overflowQ; *(chan->sumQ) = sumQ; overflowICount += overflowI; if ( overflowICount < 0 ) { overflowICount += PAD_MAX_INT; /* off by one? */ numOverflowIRollovers++; } overflowQCount += overflowQ; if ( overflowQCount < 0 ) { overflowQCount += PAD_MAX_INT; /* off by one? */ numOverflowQRollovers++; } DEBUG_clearDiagnosticBitType(21); #else /* not PAD_PROCESS_DURING_READ */ *(chan->numI) = 0; *(chan->sumI) = 0; *(chan->numQ) = 0; *(chan->sumQ) = 0; #endif /* 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) = chan->nToRead[0]; #ifdef PAD_PROCESS_DURING_READ /* Calculate the average Q and I */ DEBUG_setDiagnosticBitType(22); if (*(chan->numQ) == 0) { DEBUGPRINT(DP_ERROR, drvPadGenFlag, ("padGenReadSignal: numQ is zero\n")); /* return ERROR; */ } 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_ERROR, drvPadGenFlag, ("padGenReadSignal: numI is zero\n")); /*return ERROR; */ } 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 } DEBUG_clearDiagnosticBitType(22); #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 } #endif /* PAD_UNSCALED_PROCESSING */ /* process the raw data for one channel */ #if defined PAD_UNSCALED_PROCESSING && defined USING_DMA void processUnscaledChannelData( rf_channel_t* chan, /* channel to proces */ short *zeroOffsetDatap /* pointer to zero offset data to process */ ) { #ifdef PAD_PROCESS_DURING_READ int ii; /* absolute position in the FIFO */ short data; long *proc0p; long sumQ; long sumI; int overflowQ; int overflowI; #endif /* calculates avgI and avgQ for one channel */ #ifdef PAD_PROCESS_DURING_READ /* presumptions: * * sample sizes are multiples of 4 */ DEBUG_setDiagnosticBitType(21); sumQ = 0; sumI = 0; int limit = chan->sz[0]; /* count of shorts to process */ if ( (limit & 3) != 0 ) printf( "odd size! %d\n", limit ); /* fixme: better error message */ #undef IncrementProc0 #undef AccumulateSum #ifdef PROC0 #define IncrementProc0() proc0p++ #define AccumulateSum(sum,v) { *proc0p = (v); (sum) += *proc0p++; } #else #define IncrementProc0() #define AccumulateSum(sum,v) (sum) += (v) #endif overflowI = 0; overflowQ = 0; proc0p = chan->proc0; for ( ii = 0; ii < limit; ii += 4 ) { /* get the I data */ data = *zeroOffsetDatap++; /* I (even) first, Q (odd) second */ /* 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*/ if ( ( data & 1 ) != 0 ) { /* overflow! */ overflowI++; IncrementProc0(); } else { AccumulateSum(sumI, data ); } /* get the Q value */ data = *zeroOffsetDatap++; /* 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 */ if ( (data & 1) != 0 ) { /* overflow! */ overflowQ++; IncrementProc0(); } 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 */ AccumulateSum(sumQ, data ); } /* get the I data */ data = - *zeroOffsetDatap++; /* I (even) first, Q (odd) second */ /* 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*/ if ( ( data & 1 ) != 0 ) { /* overflow! */ overflowI++; IncrementProc0(); } else { AccumulateSum(sumI, data ); } /* get the Q value */ data = - *zeroOffsetDatap++; /* 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 */ if ( (data & 1) != 0 ) { /* overflow! */ overflowQ++; IncrementProc0(); } 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 */ AccumulateSum(sumQ, data ); } } limit /= 2; *(chan->numI) = limit - overflowI; *(chan->sumI) = sumI; *(chan->numQ) = limit - overflowQ; *(chan->sumQ) = sumQ; overflowICount += overflowI; if ( overflowICount < 0 ) { overflowICount += PAD_MAX_INT; /* off by one? */ numOverflowIRollovers++; } overflowQCount += overflowQ; if ( overflowQCount < 0 ) { overflowQCount += PAD_MAX_INT; /* off by one? */ numOverflowQRollovers++; } DEBUG_clearDiagnosticBitType(21); #else /* not PAD_PROCESS_DURING_READ */ *(chan->numI) = 0; *(chan->sumI) = 0; *(chan->numQ) = 0; *(chan->sumQ) = 0; #endif /* 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) = chan->nToRead[0]; #ifdef PAD_PROCESS_DURING_READ /* Calculate the average Q and I */ DEBUG_setDiagnosticBitType(22); if (*(chan->numQ) == 0) { DEBUGPRINT(DP_ERROR, drvPadGenFlag, ("padGenReadSignal: numQ is zero\n")); /* return ERROR; */ } else { *(chan->avgQ) = (short)(*(chan->sumQ)/(*(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_ERROR, drvPadGenFlag, ("padGenReadSignal: numI is zero\n")); /*return ERROR; */ } else { *(chan->avgI) = (short)(*(chan->sumI)/(*(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 } DEBUG_clearDiagnosticBitType(22); #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 } #endif /* defined PAD_UNSCALED_PROCESSING && defined USING_DMA */ /*============================================================================= 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. =============================================================================*/ #ifndef USING_DMA 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 */ short *zeroOffsetDatap; /* pointer to zero offset data */ #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, 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 /* Skip first three clock cycles to allow FIFO to fill */ *(pSrc+srcOfst); *(pSrc+srcOfst); *(pSrc+srcOfst); zeroOffsetDatap = chan->rawData; 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 if ( chan->zeroOffsetDataRequested == 1 ) *zeroOffsetDatap++ = *(pSrc+srcOfst); /* capture data in front of the offset */ else *(pSrc+srcOfst); /* dump data in front of the offset */ #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; finishZeroOffsetDataRequest( chan, srcOfst ); #ifdef PAD_PROCESS_DURING_READ /* Calculate the average Q and I */ if (*(chan->numQ) == 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadGenFlag, ("padGenReadSignal: numQ is zero\n")); #endif /* return ERROR; */ } 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_ERROR, drvPadGenFlag, ("padGenReadSignal: numI is zero\n")); /*return ERROR; */ } 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 /* dma processing test */ /* compare dma processing with non-dma data and non-data results */ /* if ( mm == 1 && ( test++ & 0xff ) == 0 ) { int avgI = (int) *(chan->avgI); int avgQ = (int) *(chan->avgQ); processUnscaledChannelData( chan, chan->data0 ); printf ( "%d %d vs %d %d\n", avgI, avgQ, (int) *(chan->avgI), (int) *(chan->avgQ) ); } */ #endif } /* mm loop */ return OK; } #else /* USING_DMA */ /* * read the fifos * * read the fifos by starting a dma channel on each fifo to be read. * then "wait" for all dma channels to finish. * since the any cpu cycles spent on the bus are a waste of time, * fifos are given full bus priority. the fifos are clocked faster than * the bus, so the dma channel should never have to yeild the bus. * thus the expectation is that there will be no waiting under normal conditions. */ void dmaReadFifos( rf_channel_t **channels ) { const unsigned int cr = 0 /*| DCR_int */ /*| DCR_eext */ /*| DCR_cycle_steal */ /*| DCR_auto_align */ | DCR_bwc_dma_priority /*| DCR_sinc read fifo at a single address */ | DCR_ssize_word /* native fifo size */ | DCR_dinc /* read fifo data to sequential addresses */ | DCR_dsize_word /* native fifo size */ | DCR_start ; int i; rf_channel_t *chan; unsigned int status; volatile short *fifo; /* fifo pointer */ dma_channel_hardware_map *dma; /* dma channel */ int byteCount; int n; /* *DMA_reqc = 0; / * from reset? */ DEBUGPRINT(DP_INFO, drvPadDmaFlag, ("dmaReadFifos() here\n" )); DEBUG_setDiagnosticBitType(11); for ( i = 0; i < PAD_NUM_CHANNELS_ON_BOARD; i++ ) { chan = channels[i]; if ( chan == NULL ) continue; fifo = csar1_actual + 2*i; /* point to the appropriate fifo */ /* launch the channel's fifo with it's own dma channel */ dma = dma_channels[i]; status = dma->dma_status_register; dma->dma_status_register = DSR_DONE; dma->source_address_register = (uint32_t) fifo; dma->destination_address_register = (uint32_t) ( chan->rawData ); chan->nToRead[0] = chan->ofst[0] + chan->sz[0]; /* set the byte count */ n = chan->nToRead[0]; if ( chan->zeroOffsetDataRequested == 1 && PAD_ZERO_OFFSET_DATA_SIZE > n ) /* read more than minimum for the zero offset sample */ n = PAD_ZERO_OFFSET_DATA_SIZE; byteCount = sizeof(short)*( n + fifo_startup ); dma->byte_count_register = byteCount; /* start the channel */ dma->dma_control_register = cr; DEBUGPRINT(DP_INFO, drvPadDmaFlag, ("dmaReadFifos: status on channel %d before request: 0x%x\n", i, status)); DEBUGPRINT(DP_INFO, drvPadDmaFlag, ("\tcr : 0x%x\n", cr )); DEBUGPRINT(DP_INFO, drvPadDmaFlag, ("\t source_address_register : 0x%x\n", dma->source_address_register )); DEBUGPRINT(DP_INFO, drvPadDmaFlag, ("\t destination_address_register : 0x%x\n", dma->destination_address_register )); DEBUGPRINT(DP_INFO, drvPadDmaFlag, ("\t nToRead[0] set to 0x%x\n", chan->nToRead[0] )); DEBUGPRINT(DP_INFO, drvPadDmaFlag, ("\t byte_count_register set to 0x%x\n", byteCount )); DEBUGPRINT(DP_INFO, drvPadDmaFlag, ("\t zeroOffsetDataRequested = 0x%x\n", chan->zeroOffsetDataRequested )); } DEBUG_clearDiagnosticBitType(11); /* "wait" for the dma channels to complete */ DEBUG_setDiagnosticBitType(12); for ( i = 0; i < PAD_NUM_CHANNELS_ON_BOARD; i++ ) { chan = channels[i]; if ( chan == NULL ) continue; /* wait for the channel */ dma = dma_channels[i]; while ( ((status = dma->dma_status_register) & DSR_BSY ) != 0 ) { if ( status & DSR_CE ) { printf( "error: dma_status_register = 0x%x\n", status ); break; } } if ( status & DSR_CE ) { /* deal with error?*/ DEBUGPRINT(DP_ERROR, drvPadDmaFlag, ("dmaReadFifos: error on channel %d: 0x%x\n",i,status)); continue; } DEBUGPRINT(DP_INFO, drvPadDmaFlag, ("dmaReadFifos: status on channel %d after done: 0x%x\n", i, status )); if ( chan->zeroOffsetDataRequested ) { chan->zeroOffsetDataRequested--; if ( chan->zeroOffsetDataRequested == 0 ) /* copy the required data to the sample storage location */ memcpy( &chan->zeroOffsetSampleData[0], &chan->rawData[fifo_startup], /* skip fifo startup */ sizeof(short)*PAD_ZERO_OFFSET_DATA_SIZE ); } } DEBUG_clearDiagnosticBitType(12); } #endif /* USING_DMA */ #ifdef USING_DMA int padGenReadSignal(rf_channel_t* chan0, rf_channel_t* chan1, rf_channel_t* chan2, rf_channel_t* chan3) { int mm; /* loop variable for all channels */ rf_channel_t* chan = NULL; /* generic channel used in loop of all channels */ rf_channel_t *channels[4]; int offset; /* padSampleOffset */ channels[0] = chan0; channels[1] = chan1; channels[2] = chan2; channels[3] = chan3; DEBUG_setDiagnosticBitType(10); dmaReadFifos( channels ); DEBUG_clearDiagnosticBitType(10); /* calculates avgI and avgQ for all four channels */ DEBUG_setDiagnosticBitType(20); for (mm=0; mmsumQ))); 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 offset = chan->ofst[0]; /* index into data after offset */ if ( (offset & 3) != 0 ) printf( "odd offset! %d\n", offset ); /* fixme: better error message */ #ifdef PAD_UNSCALED_PROCESSING processUnscaledChannelData( chan, &chan->rawData[ offset + fifo_startup] ); #else processChannelData( chan, &chan->rawData[ offset + fifo_startup] ); #endif } /* mm loop */ DEBUG_clearDiagnosticBitType(20); return OK; } #endif /* USING_DMA */ /*============================================================================= 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 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_ERROR, drvPadFlag, ("padIfqReadSignal: numQ is zero\n")); #endif return ERROR; } 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_ERROR, drvPadFlag, ("padIfqReadSignal: numI is zero\n")); return ERROR; } 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 } /* 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_ERROR, drvPadIfqFlag, ("padIfqReadSignal: numI is zero\n")); #endif return ERROR; } else { chan->sigmaSqI = (chan->sumSqI)/(*(chan->numI)/6); } if (*(chan->numQ) == 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadIfqFlag, ("padIfqReadSignal: numQ is zero\n")); #endif return ERROR; } 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 } 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 mm; /* loop variable for all channels */ #ifdef PAD_PROCESS_DURING_READ int ii; /* position in sample */ 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; /* 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; #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_ERROR, drvPadRefFlag, ("padRefReadSignal: numI is zero\n")); #endif return ERROR; } 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_ERROR, drvPadRefFlag, ("padRefReadSignal: numI is zero\n")); #endif return ERROR; } 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 } 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; #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 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 /* Start by multiplying the signal by the constant */ *(chan->proc0+jj) = (*(chan->scalar)) * (*(chan->data0+jj)); chan->sumS += *(chan->proc0+jj); chan->numS += 1; #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_ERROR, drvPadFlag, ("padKlyReadSignal: Channel %d: num pts is zero.\n", mm)); return ERROR; } 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 /* 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; if (x2 != x1) { long b; #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 0; } /* 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; } /*============================================================================= Goal: find the y-intercept of I=fn(n) and Q=fn(n) from the line given by two points, which are the averages of each of the samples =============================================================================*/ 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 = 0; /* (ii-*ofst)th position in FIFO */ int kk = 0; /* (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 */ #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) { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padBphReadSignal: first sample surpasses end of FIFO! %d + %d > %d\n", *(chan->ofst), *(chan->sz), PAD_FIFO_SIZE)); free(chan->proc0); free(chan->proc1); return ERROR; } /* don't count the 3 garbage values at start of (each) FIFO */ /* CHECK: but these 3 got added to ofst, so is this right? */ *(chan->nToRead) = *(chan->ofst) + *(chan->sz); #ifndef FAKE_DATA pSrc = csar1_actual; #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 */ *(chan->proc0+jj) = *(chan->scalar+ii) * (*(chan->data0+jj)); isQ = ii & 1; /* examine last bit to determine odd/even */ if (isQ == 1) { /* Q values in the odd positions */ *(chan->sumQ) += *(chan->proc0+jj); *(chan->numQ) += 1; /* TO DO 29sep2006: add overflow check */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("s1 ch0 added %d-th pt: %ld to sumQ %lld\n", *(chan->numQ), *(chan->proc0+jj), *(chan->sumQ))); #endif } else { /* I values in the even positions */ *(chan->sumI) += *(chan->proc0+jj); *(chan->numI) += 1; /* TO DO 29sep2006: add overflow check */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("s1 ch0 added %d-th pt: %ld to sumI %lld\n", *(chan->numQ), *(chan->proc0+jj), *(chan->sumI))); #endif } #endif jj++; } } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("\nSample 0\n")); DEBUGPRINT(DP_INFO, drvPadFlag, (" sumQ %lld\n", *(chan->sumQ))); DEBUGPRINT(DP_INFO, drvPadFlag, (" sumI %lld\n", *(chan->sumI))); DEBUGPRINT(DP_INFO, drvPadFlag, (" numQ %d\n", *(chan->numQ))); DEBUGPRINT(DP_INFO, drvPadFlag, (" numI %d\n", *(chan->numI))); #endif /* Save the number of 16-bit words read into sample 1 */ *(chan->nRead) = jj; /* Save the current position in the FIFO */ indexAfterSample0 = ii; DEBUGPRINT(DP_INFO, drvPadFlag, ("padBphReadSignal: after first sample, position in FIFO is %d\n", indexAfterSample0)); /* Calculate first sample's average I (Y1) and Q */ /* I: */ if (*(chan->numI) <= 0) { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padBphReadSignal: I num pts of first sample is zero.\n")); free(chan->proc0); free(chan->proc1); return ERROR; } *(chan->avgI) = (short) ((*(chan->sumI))/(*(chan->numI))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padBphReadSignal: I-Y1:%d\n", (int) *(chan->avgI))); /* Q: */ if (*(chan->numQ) <= 0) { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padBphReadSignal: Q num pts of first sample is zero.\n")); free(chan->proc0); free(chan->proc1); return ERROR; } *(chan->avgQ) = (short) ((*(chan->sumQ))/(*(chan->numQ))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padBphReadSignal: Q-Y1: :%d\n", (int) *(chan->avgQ))); /* 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) { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padBphReadSignal: second sample starts before end of first! %d < %d\n", *(chan->ofst+1), indexAfterSample0)); free(chan->proc0); free(chan->proc1); return ERROR; } if (*(chan->ofst+1) + *(chan->sz+1) > PAD_FIFO_SIZE) { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padBphReadSignal: second sample surpasses end of FIFO! %d + %d > %d\n", *(chan->ofst+1), *(chan->sz+1), PAD_FIFO_SIZE)); free(chan->proc0); free(chan->proc1); return ERROR; } *(chan->nRead+1) = *(chan->ofst+1) + *(chan->sz+1); DEBUGPRINT(DP_INFO, drvPadFlag, ("padBphReadSignal: starting at %d, skipping next %d shorts to read %d\n", indexAfterSample0,*(chan->ofst+1)-indexAfterSample0, *(chan->sz+1))); /* 3.2 Read second sample */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("\nSample 1\n")); #endif for (ii=indexAfterSample0; ii<*(chan->nToRead+1); ii++) { if (ii < *(chan->ofst+1)) { /*DEBUGPRINT(DP_INFO, drvPadFlag, ("skipping ii %d\n", ii));*/ #ifndef FAKE_DATA *(pSrc+srcOfst); #endif } else { /*DEBUGPRINT(DP_INFO, drvPadFlag, ("storing ii %d\n", ii));*/ #ifndef FAKE_DATA *(chan->data1+kk) = *(pSrc+srcOfst); #else /* Make fake data */ *(chan->data1+kk) = (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 */ *(chan->proc1+kk) = *(chan->scalar+ii)*(*(chan->data1+kk)); isQ = ii & 1; /* examine last bit to determine odd/even */ if (isQ == 1) { /* Q values in the odd positions */ #ifdef DEBUG_PRINT /* TO DO 29sep2006: add overflow check */ DEBUGPRINT(DP_INFO, drvPadFlag, ("s2 ch0 adding %d-th pt: %ld to sumQ %lld\n", *(chan->numQ+1), *(chan->proc1+kk), *(chan->sumQ+1))); #endif *(chan->sumQ+1) += *(chan->proc1+kk); *(chan->numQ+1) += 1; } else { /* TO DO 29sep2006: add overflow check */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("s2 ch0 adding %d-th pt: %ld to sumI %lld\n", *(chan->numI+1), *(chan->proc1+kk), *(chan->sumI+1))); #endif *(chan->sumI+1) += *(chan->proc1+kk); *(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_ERROR, drvPadFlag, ("padBphReadSignal: I num pts for sample 2 is zero.\n")); #endif free(chan->proc0); free(chan->proc1); return ERROR; } *(chan->avgI+1) = (*(chan->sumI+1))/(*(chan->numI+1)); /* Q: */ if (*(chan->numQ+1)<=0) { #ifdef DEBUGPRINT DEBUGPRINT(DP_ERROR, drvPadFlag, ("padBphReadSignal: Q num pts for sample 2 is zero.\n")); #endif free(chan->proc0); free(chan->proc1); return ERROR; } *(chan->avgQ+1) = (short)(*(chan->sumQ+1))/(*(chan->numQ+1)); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("\nSample 1\n")); DEBUGPRINT(DP_INFO, drvPadFlag, (" sumQ %lld\n", *(chan->sumQ+1))); DEBUGPRINT(DP_INFO, drvPadFlag, (" sumI %lld\n", *(chan->sumI+1))); DEBUGPRINT(DP_INFO, drvPadFlag, (" numQ %d\n", *(chan->numQ+1))); DEBUGPRINT(DP_INFO, drvPadFlag, (" numI %d\n", *(chan->numI+1))); #endif /* 4. Calculate the y-intercept */ /* Calculate x1 and x2 as the midpoints of the samples */ *(chan->x) = *(chan->ofst) + (*chan->sz)/2; *(chan->x+1) = *(chan->ofst+1) + (*(chan->sz+1))/2; chan->yintQ = padGetYIntercept(*(chan->x), *(chan->avgQ), *(chan->x+1), *(chan->avgQ+1)); chan->yintI = padGetYIntercept(*(chan->x), *(chan->avgI), *(chan->x+1), *(chan->avgI+1)); #ifdef DEBUGPRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padBphReadSignal: Qb:%d Ib:%d\n",(int)(chan->yintQ),(int) (chan->yintI))); #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" =============================================================================*/ short *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: #ifdef USING_DMA /* data has been placed in zero offset data by dma */ { rf_channel_t *chan = pChannels[channel]; return &chan->rawData[ fifo_startup + chan->ofst[0] ]; } #else return pChannels[channel]->data0; #endif break; case PAD_SCALAR: case PAD_CONSTANT: return pChannels[channel]->scalar; break; case PAD_PROCESSED: switch (sample) { case 0: return (short*)(pChannels[channel]->proc0); break; case 1: return (short*)(pChannels[channel]->proc1); break; case 2: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadGetSigFlag, ("padGetSignal: with inputType=PROCESSED, sample %d should not be possible\n", sample)); #endif return (short*) NULL; break; case 3: #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: with inputType=PROCESSED, sample %d returns ptr to IFQ's procI array\n", sample)); #endif return (short*)(pChannels[channel]->procI); break; case 4: #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: with inputType=PROCESSED, sample %d returns ptr to IFQ's procQ array\n", sample)); #endif return (short*)(pChannels[channel]->procQ); break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadGetSigFlag, ("padGetSignal: unrecognized sample: %d\n", sample)); #endif return (short*) 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_ZERO_OFFSET_DATA: /* refresh, but delay the capture to avoid a race condition */ if ( pChannels[channel]->zeroOffsetDataRequested > 0 ) /* speed things up if there is no interrupt */ pChannels[channel]->zeroOffsetDataRequested = 1; else /* assumes reference interrupt is higher than 5 hertz */ pChannels[channel]->zeroOffsetDataRequested = 5; return &pChannels[channel]->zeroOffsetSampleData[0]; break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadGetSigFlag, ("padGetSignal: unrecognized input type: %d\n", inputType)); #endif return (short*) NULL; } } else { /* Send back calibration data */ return pCalChannels[channel-4]->data0; } return (short*) 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: 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 %lu\n", *(pChannels[jj]->sumQ+1)); fprintf(fout, "sumI %lu\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 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); } #elif 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); } #elif 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); } #elif PAD_L1X_KLY 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); } #elif PAD_KLY /* Average I, Q algoritm for channels 0+1 */ /* Simple average S for channels 2+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); } #elif 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 for (ii=0; ii<4; ii++) { pChannels[ii]->zeroOffsetDataRequested = 1; pChannels[ii]->rawData = malloc(sizeof(short)*(PAD_SAMPLE_SIZE+fifo_startup)); pChannels[ii]->zeroOffsetSampleData = malloc(sizeof(short)*(PAD_ZERO_OFFSET_DATA_SIZE)); } /* 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 setDmaTo24BitByteCount( TRUE ); /* 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 /* force the turnoff of bootp! * * to over-ride the flash configuration, kly-lclsdiag uses bootp/dhcp to force * the internet parameters. see kly-lclsdiag:/etc/dhcpd.conf * unfortunately, the bootp choice is in flash and cannot be overridden. * the DO_BOOTP flag is set to get the diagnostic boot, and then automatically * cleared here to avoid leaving the board in this condition. * this costs a flash burn every boot, but there is no other choice at the moment. */ const char *cp = pad_getbenv( dobootp ); if ( cp != NULL && strlen( cp ) > 0 ) { printf( "DO_BOOTP in NVRAM is %s\n", pad_getbenv( dobootp )); /* pad_setbenv( dobootpClear ); printf( "forcing %s off\n", dobootp ); */ } 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_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 = 0x020;/* 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 = 0x8020;/* 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 = 0x20; /* 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 = 0x8020; /* 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()); } else { done = 1; } } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("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, drvPadFlag, ("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, drvPadFlag, ("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, drvPadFlag, ("after 3 bytes read, adc: 0x%08x\n", adc)); #endif #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("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; }