/* $Id: drvPadLan9118.c,v 1.2 2014/02/04 20:13:54 scondam Exp $ */ /*============================================================================= Name: drvPadLan9118.c Abs: Driver support for PAD's 9118 chip, based on Till's device support code "devWfPadBpm.c" Auth: 14-apr-2008, Dayle Kotturi (dayle) Mod: 18-May-2018, Shantha Condamoor (scondam): 1. Add check for divide by zero 23-may-2012, Shantha Condamoor (scondam): 1. Removed the comparison against PEDAVG PV in the Fault logic. 11-Aug-11, Shantha Condamoor (scondam): 1. Added padResetFaultingPulses() to reset the total number of faults since reboot and the number of faulting pulses for each channel per NLCTA's request. A new RST button next to 'Total Sys Faults' will now reset all of the above. 2. padSimpReadSignal() changed: Per NLCTA's request modified the logic that compares the running average of pulses against the pedestal value instead of a single pulse against the pedestal value. 15-Jul-11, Shantha Condamoor (scondam): 1. Change 'chan->avgS not within accepted range' to 'chan->avgOut not within accepted range' in the function padSimpReadSignal() 2. The KTL do not need the pedestal as they have their hardware device that is used during RF power ramping. 03-may-2011, Shantha Condamoor (scondam): 1. Added functions to support PEDAVG pv. This is the pedestal or threshold of avgOut for each of the 4 channels set by the user. When avgOut is below this threshold, fault signal is not generated. Useful during RF power ramping. 29-mar-2011, Shantha Condamoor (scondam): 1. Added prev_intrCount to aid in calculating the Trigger rate. 2. Add the following reset functions: padResetIntrCount() padResetScanIOCount() padResetOverflowICount() padResetOverflowQCount() padResetState() 02-mar-2010, Shantha Condamoor (scondam): Initial version for Klystron Test Station 01 Interlocking with PAD. 1. Completely scratched out the existing code as it was causing troubles in sampling more than 220 points at higher trigger rates. IOC was hanging up. New code base is derived from the pad4App package and has been modified extensively for the TS01 test setup. This code has been tested and found to be capable of handling upto 512 sampling points at 60 Hz. 2. Added the scalar variables for scaling down raw averages. 28-jan-2010, Shantha Condamoor Modified to setup special test for TS01 in Klystron Lab. This test is described in the KLY_TS01_PAD_User_Manual.doc in TS01 windows machine in T:/Pad Information directory: 1. GPT PA0 (Pin 24) configured as an output pin to indicate the status. status = NORMAL (1) when ADC pulse value is within an expected range. = FAULT (0) when it exceed the range. 2. Added more members to rf_channel_s structure. 3. Added the compiler option 'PAD_SIMPLE_PROCESSING. It is used for compiling in the code specific to the Klystron Test Station TS01 test. 4. Added dump functions for Average Out, Faulting channel statistics etc. 5. Added Get and Set functions for the new PVs. 6. Added code in the ioc init function to initialize all the newly added data variables. -----------------------------------------------------------------------------*/ #if 0 #include "copyright_SLAC.h" /* SLAC copyright comments */ #endif /*----------------------------------------------------------------------------- Mod: (see CVS log) =============================================================================*/ #include #include #include #include #include #ifdef BASE_HAS_NO_NTP_PATCH #include #include #endif #include #include #include /* for DRVSUPFUN */ #include /* for devConnectInterruptVME */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* for DEBUGPRINT */ #include #include #include #include "padLan9118Macros.h" #include "drvPadLan9118.h" #include #include /* Which Arcturus 'IRQ' pins is used for triggering the FIFOs. This one is configured as an OUTPUT so that it can function as a s/w trigger */ #define TRIG_PIN 1 /* Which Arcturus 'IRQ' pins is used for interrupting the CPU. This one is configured as an INPUT (i.e. unchanged from default) so that it can function as the h/w trigger */ #define IRQ_PIN 2 /* Shall we interrupt on the falling (trigger detected) or * raising ('fifo full' [rather: N clocks expired]) edge? * The former is OK if the clock rate is faster than * we can read the fifo (~8MB/s -> clock > 1MHz). */ #define IRQ_EDGE EPORT_FALLING /* Which chip select we are using - this is setup by the BSP * but not published :-( */ #define CS1_BASE_ADDR 0x30000000 /* Just a name for sake of readability */ #define CS1 (1) #define DEBUG_TIMING (1<<1) typedef int16_t __attribute__ ((may_alias)) int16_t_a; typedef uint32_t __attribute__ ((may_alias)) uint32_t_a; typedef struct padLan9118_s { epicsEventId data_ready; int irq_pin; int trig_pin; volatile int irq; CALLBACK cb_s; /* int orarm; not sure if needed */ unsigned long nusr; /* support user resizing of array */ void (*readFifos)(volatile void*, int, int); } padLan9118_t; /* from drvPad.c 4/23/8 */ /* Interrupt/General-Purpose I/O Pin Descriptions All pins default to general-purpose input pins at reset. The pin value is synchronized to the rising edge of CLKOUT when read from the EPORT pin data register (EPPDR). The values used in the edge/level detect logic are also synchronized to the rising edge of CLKOUT. These pins use Schmitt triggered input buffers which have built in hysteresiss designed to decrease the probability of generating false edge-triggered interrrupts for slow rising and falling input signals. When a pin is configured as an output, it is driven to a state whose level is determined by the corresponding bit in the EPORT data register(EPDR). All bits in the EPDR are high at reset. */ #define IPSBAR (0x40000000) /* Default base address for the Edgeport and the Chip Select Module */ /* Adding the base address here, defies the purpose of having the change base address routine */ /* Interrupt controller module */ #define ICR02 (IPSBAR+0xC42) /* Address of interrupt control register #2. 1 byte */ #define IMRL (IPSBAR+0xC0C) /* Address of interrupt mask register low. 4 bytes */ #define EPPAR (IPSBAR+0x00130001) /* Two bytes. Big endian. 0=bits 15-8, 1=bits 7-0 */ #define EPDDR (IPSBAR+0x00130002) /* EPORT Data Direction Register */ #define EPIER (IPSBAR+0x00130003) /* EPORT Interrupt Enable Register */ #define EPDR (IPSBAR+0x00130004) /* EPORT Data Register */ #define EPPDR (IPSBAR+0x00130005) /* EPORT Pin Data Register */ #define EPFR (IPSBAR+0x00130006) /* EPORT Flag Register */ /* Note: 0x00130007 is reserved */ /* Note about access: EPPAR, EPDDR, and EPIER are Supervisor access EPDR, EPPDR and EPRF are Supervisor/User access Writing to reserved address has no effect, reading returns zeroes */ #define CSAR1 (IPSBAR+0x00008C) /* 2 bytes */ #define CSMR1 (IPSBAR+0x000090) /* 4 bytes */ #define CSCR1 (IPSBAR+0x000096) /* 2 bytes */ /* General Purpose Timer Modules (GPTA and GPTB) setup */ /* The port should initialize as general purpose inputs with the timer functions disabled. */ /* The general purpose timer module can be configured as IO and appears in Connector J21 of the PAD board. */ /* Connector Port -Pin Name J21-11 PTA0 J21-12 PTA1 J21-9 PTA2 J21-10 PTA3 */ /* GPT Port Data Direction Register (GPTDDR) */ #define GPTDDR (IPSBAR+0x1A001E) /* GPT Port Data Direction Register. */ /* Setting Bits 0 to 3 at Byte address IPSBAR + 0x1A001E will cause the corresponding PTA0 to PTA3 to become outputs. */ /* Write 0x01 to IPSBAR + 0x1A001E to set PTA0 to an output pin. */ /* GPT Port Data Register (GPTPORT) */ /* Writing to bits 0 to 3 of byte address IPSBAR+0x1A001D will set corresponding output pins PTA0 to PTA3 to the value written. */ #define GPTPORT (IPSBAR+0x1A001D) /* GPT Port Data Register */ /* Set bit low write 0x00 to IPSBAR+0x1A001D */ /* Set bit low write 0x00 to IPSBAR+0x1A001D */ /* scondam: 01.28.09: The compiler option 'PAD_SIMPLE_PROCESSING is used for compiling in the code specific to the Klystron Test Station TS01 test */ #ifdef PAD_SIMPLE_PROCESSING #define PAD_NUM_AVGS_TO_SAVE (256) #else #define PAD_NUM_AVGS_TO_SAVE (32768) #endif /* #define DACQ_TASK_PRIO epicsThreadPriorityHigh */ /* #define DACQ_TASK_STACK (20000) */ /* task stack size */ /* #define PAD_IRQ_VECTOR (66) */ /* DECIMAL! */ /*#define PAD_IRQ_LEVEL (?) done differently */ #ifdef USING_QSPI /* Queued serial port interface (QSPI) */ #define PQSPAR (IPSBAR+0x100059)/* Port QS Pin Assignment Register */ #define QMR (IPSBAR+0x340) /* QSPI Mode Register. 2 bytes */ #define QDLYR (IPSBAR+0x344) /* QSPI Delay Register. 2 bytes */ #define QWR (IPSBAR+0x348) /* QSPI Wrap Register. 2 bytes */ #define QIR (IPSBAR+0x34C) /* QSPI Interrupt Register. 2 bytes */ #define QAR (IPSBAR+0x350) /* QSPI Address Register. 2 bytes */ #define QDR (IPSBAR+0x354) /* QSPI Data Register. 2 bytes */ #endif #ifdef USING_DMA #define DONE (1) /* DMA transfer done */ #define BSY (2) /* DMA transfer busy */ #define REQ (4) /* DMA request */ #define RSD1 (8) /* Reserved. Should be cleared. */ #define BED (16) /* Bus error on destination */ #define BES (32) /* Bus error on source */ #define CE (64) /* Configuration error */ #define RSD2 (128) /* Reserved. Should be cleared. */ #define SAR2 (IPSBAR+0x180) /* Source address register 0 */ #define DAR2 (IPSBAR+0x184) /* Destination address register 0 */ #define DCR2 (IPSBAR+0x188) /* DMA control register 0 */ #define BCR2 (IPSBAR+0x18C) /* Byte count register 0 if data is 16 bit*/ #define DSR2 (IPSBAR+0x190) /* DMA status register 0 */ #endif #define PAD_NUM_WAVEFORMS_KEPT (10) #define PAD_MAX_INT (2147483647) /* 4 byte int - 1 bit for sign */ #define PAD_MAX_FILENAME (100) #define PAD_SCALED_DATA_NORMALIZATION (32767) /* 2nd dimension, data1 and yint* are only used by PADLAN9118_BPH app *S vars are only used by PAD_WF app sumSq* and sigmaSq* are only used by PAD_REF app */ typedef struct rf_channel_s { long long sumQ[2]; long long sumI[2]; long long sumS; long long sumSqQ; /* used by pad{Ref,Ifq}ReadSignal */ long long sumSqI; /* used by pad{Ref,Ifq}ReadSignal */ long sigmaSqQ; /* used by pad{Ref,Ifq}ReadSignal */ long sigmaSqI; /* used by pad{Ref,Ifq}ReadSignal */ short* scalarI; /* array of scalar corrections used by padIfqReadSignal */ short* scalarQ; /* array of scalar corrections used by padIfqReadSignal */ long* procI; /* array of processed data used by padIfqReadSignal */ long* procQ; /* array of processed data used by padIfqReadSignal */ int ofst[2]; int sz[5]; /* 0=sample 0, 1=sample 1, 3=IFQ sample I, 4=IFQ sample Q */ int x[2]; /* <--- 8/14/7: may be able to do without this now */ int nToRead[2]; int nRead[2]; int numQ[2]; int numI[2]; int numS; short* data0; /* the first or only sample of interest in the FIFO */ short* data1; /* the second sample of interest in the FIFO (only padBph) */ short* scalar; /* array of scalar corrections used by padGenReadSignal */ long* proc0; long* proc1; long avgQ[2]; /* 1/22/2007 with scalar array ranging from -32767 to 32767, avg doesn't fit into 2 bytes */ long avgI[2]; /* 1/22/2007 with scalar array ranging from -32767 to 32767, avg doesn't fit into 2 bytes */ long avgS; #ifdef PAD_SIMPLE_PROCESSING short plsavg; /* number of pulses to average. Overwrites PAD_NUM_AVGS_TO_SAVE for a channel */ long avgOut; /* Average value of 'avgS' for 'plsavg' number of pulses */ long avgOut_ped; /* 5/02/2011 - add a user-settable pedestal value below which avgS will not be set to FAULT state */ short threshold; /* The upper fault threshold of a CH is set to this % above the avgOut and lower threshold to this% below avgOut */ short state; /* indicates the state of a channel, = NORMAL (1) when RAW value within range. = FAULT (0) otherwise. */ long padLogAvg[PAD_NUM_AVGS_TO_SAVE]; /* array of sets of avgS for 'plsavg' pulses */ short logindex; /* sliding window index. When logindex == plsavg, it rolls over to 0 */ #endif } 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; int updatingWfs = 0; epicsTimerQueueId padWdTimerQ = 0; /* scondam: 01.28.09: The compiler option 'PAD_SIMPLE_PROCESSING is used for compiling in the code specific to the Klystron Test Station TS01 test */ #ifdef PAD_SIMPLE_PROCESSING #define FAULT (0) #define NORMAL (1) int State = FAULT; int fault_chan = -1; typedef struct { short* data; int ofst; int sz; int nToRead; int nRead; long long sumS; int numS; long avgS; long avgOut; long thresh; long ulevel; long llevel; short state; long faults; } fail_state; fail_state fst0; fail_state fst1; fail_state fst2; fail_state fst3; fail_state* fst[4]={&fst0, &fst1, &fst2, &fst3}; #endif #ifdef USING_TIMER int timers[20]; int worst_case_timers[21]; /* has one more to hold the number of valid timers */ int timer_ii; int longest_time_at_done = 0; #endif #ifdef DEBUG_PRINT /* ENUM with values: DP_FATAL, DP_ERROR, DP_WARN, DP_INFO, DP_DEBUG Anything less than or equal to drvPnetFlag gets printed out. Can be set to 0 here and altered via prompt or in st.cmd file */ int drvPadFlag = 2; int drvPadLan9118Flag = 2; /*4/14/8*/ int drvPadLan9118DacqFlag = 2; /*6/26/8*/ int drvPadLan9118ArmFlag = 2; /*6/26/8*/ int drvPadLan9118ISRFlag = 0; /*6/12/8*/ int drvPadLan9118GetdataFlag = 2; /*5/13/8*/ int drvPadGetSigFlag = 2; int drvPadCfgFlag = 2; int drvPadScalFlag = 2; int drvPadIntrFlag = 2; int drvPadSSFlag = 2; int drvPadGenFlag = 2; int drvPadIfqFlag = 2; int drvPadRefFlag = 2; int drvPadBphFlag = 2; int drvPadSimpFlag = 2; int drvPadCbFlag = 2; int drvPadLan9118ReadFifosFlag=2; #endif /* Hardware registers for chip select and EPORT module */ LOCAL volatile unsigned char *epddr = (unsigned char *)EPDDR; LOCAL volatile unsigned char *icr02 = (unsigned char *)ICR02; LOCAL volatile unsigned long *imrl = (unsigned long *)IMRL; 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 unsigned char *epier = (unsigned char *)EPIER; LOCAL volatile unsigned char *epdr = (unsigned char *)EPDR; LOCAL volatile short *csar1 = (short *)CSAR1; LOCAL volatile short *csar1_actual = (short *) 0x80000000; LOCAL volatile unsigned short *cscr1 = (unsigned short *)CSCR1; LOCAL volatile unsigned long *csmr1 = (unsigned long *)CSMR1; /* scondam: 01.28.09: General Purpose Timer and IO Port configuration */ LOCAL volatile unsigned char *gptddr = (unsigned char *)GPTDDR; LOCAL volatile unsigned char *gptport = (unsigned char *)GPTPORT; /* Driver globals */ LOCAL int prev_intrCount = 0; /* scondam: 03.29.11: added to calculate trigger rate */ LOCAL epicsTimeStamp timeAtBoot; LOCAL int numIntrRollovers = 0; LOCAL int overflowICount = 0; LOCAL int numOverflowIRollovers = 0; LOCAL int overflowQCount = 0; LOCAL int numOverflowQRollovers = 0; LOCAL int overflowSCount = 0; LOCAL int numOverflowSRollovers = 0; LOCAL char filename[PAD_MAX_FILENAME]; LOCAL char filenameStem[PAD_MAX_FILENAME]; /* For interlocking PAD */ LOCAL int faultCount = 0; /* Number of times FAULT state was entered since boot*/ #ifdef SCAN_IO LOCAL int scanIoCount = 0; LOCAL IOSCANPVT pad_ioscanpvt; /* single interrupt source for all pad recs */ LOCAL int numScanIoRollovers = 0; #endif #ifdef USING_QSPI LOCAL volatile unsigned char *pqspar = (unsigned char *) PQSPAR; LOCAL volatile unsigned short *qmr = (unsigned short *)QMR; LOCAL volatile unsigned short *qdlyr = (unsigned short *)QDLYR; LOCAL volatile unsigned short *qwr = (unsigned short *)QWR; LOCAL volatile unsigned short *qir = (unsigned short *)QIR; LOCAL volatile unsigned short *qar = (unsigned short *)QAR; LOCAL volatile unsigned short *qdr = (unsigned short *)QDR; #endif #ifdef USING_DMA LOCAL volatile unsigned long *sar2 = (unsigned long *)SAR2; LOCAL volatile unsigned long *dar2 = (unsigned long *)DAR2; LOCAL volatile unsigned long *dcr2 = (unsigned long *)DCR2; LOCAL volatile unsigned long *bcr2 = (unsigned long *)BCR2; LOCAL volatile unsigned long *dsr2 = (unsigned long *)DSR2; #endif long ch0data[7]; /* used by padGetSignal */ long ch1data[7]; /* used by padGetSignal */ long ch2data[7]; /* used by padGetSignal */ long ch3data[7]; /* used by padGetSignal */ int padDumpQavgToFile(); int padDumpIavgToFile(); /* end from drvPad.c */ /* Globals */ extern IpBscIf lanIpIf; epicsThreadId drvPadStreamInit(); epicsThreadId drvPadLan9118DacqThread = 0; padLan9118_t padLan9118; padLan9118_t *ppadLan9118 = &padLan9118; int drvPadLan9118Irqs = 0; int drvPadLan9118Acqs = 0; int drvPadLan9118Debug = DEBUG_TIMING; int pad_card; /* called PADSLOT in startup, must match "card" on VME */ typedef struct { int channel; uint32_t mcaddr; } *PadUdpThreadArg; /* Forward Declarations */ static void padISR(void *arg_p); /* 4/25/8 not needed. 5/12/8 put back */ static void padUdpThread(void *arg); /* 5/12/8 make static */ int drvPadLan9118Initialise(); int drvPadLan9118Report(int level); struct drvet drvPadLan9118 = { 2, (DRVSUPFUN) drvPadLan9118Report, /* subroutine defined in this file */ (DRVSUPFUN) drvPadLan9118Initialise /* subroutine defined in this file */ }; epicsExportAddress(drvet, drvPadLan9118); /* Little Helpers */ static int trigRaise(int trig_pin) { coldfEportEpdrSet(trig_pin, 1); return OK; } static int trigLower(int trig_pin) { coldfEportEpdrSet(trig_pin, 0); return OK; } static void disarm(padLan9118_t* dpvt_ps) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("disarm\n")); #endif /* if the device is currently armed we can't * really disarm it, i.e., we can't prevent * the next trigger from happening. */ coldfEportIntDisable(dpvt_ps->irq_pin); /* If a hw trigger happens after the board * is disarmed the following can happen * on the next 'arm' operation: * a) FIFO full -> everything OK * b) FIFO still filling -> OK, next trigger will be caught * c) FIFO still filling, SW trigger -> BAD will effectively * disarm board. * ==> SW trigger should verify that fifo is not * currently filling. If it is then * just run the ISR so the current data * is sucked. */ } /* 6/26/8: this seems unnecessary. This is the callback run at 1 Hz by the VME as part of the StartStrmRequest. What do I really need to do here? I already have the disable, flag clear, release trigger happening every time the DacqTask gets an event from ISR... Tried making this routine a no-op and no interrupts got through with either OFFLINE or Running ACCESS */ int myArm() { /* to keep things simple, we disable interrupts here * so the ISR doesn't change EPORT bits under our feet. */ coldfEportIntDisable(ppadLan9118->irq_pin); /* clear old flag set by any pre-arm triggers */ coldfEportFlagClr(ppadLan9118->irq_pin); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118ArmFlag, ("myArm: toggling interrupt\n")); #endif coldfEportIntEnable(ppadLan9118->irq_pin); return OK; } /* 7/27/8: since there is only one padStreamInit on vme side, make all cases send the same number of shorts, which is 24 (the size of the PCAV1&2 dataset */ void myDrvPadReadFifosRowMajor(volatile void *dst_pv, int n, int do_bswap) { volatile uint32_t_a *dst_p = dst_pv; switch (pad_card) { case (1): /* LSR */ *dst_p = (padCh0.avgQ[0]<<16) | (padCh0.avgI[0] & 0xffff); *dst_p = (padCh1.avgQ[0]<<16) | (padCh1.avgI[0] & 0xffff); *dst_p = (padCh2.avgQ[0]<<16) | (padCh2.avgI[0] & 0xffff); *dst_p = (padCh3.avgQ[0]<<16) | (padCh3.avgI[0] & 0xffff); *dst_p = (padCh0.sigmaSqQ<<16) | (padCh0.sigmaSqI & 0xffff); /* *dst_p = (padCh1.sigmaSqQ<<16) | (padCh1.sigmaSqI & 0xffff); */ *dst_p = (padCh2.sigmaSqQ<<16) | (padCh2.sigmaSqI & 0xffff); *dst_p = (padCh3.sigmaSqQ<<16) | (padCh3.sigmaSqI & 0xffff); /* padding to make LSR packet equal in size to PH1 and 2 packet */ /* IMPORTANT: if you uncomment the Ch 1 sigmaSqs above, you must reduce padding by 2 uint32_t_a */ *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118ReadFifosFlag, ("myDrvPadReadFifosRowMajor: %ld %ld %ld %ld %ld %ld %ld %ld\n", padCh0.avgQ[0],padCh0.avgI[0], padCh1.avgQ[0],padCh1.avgI[0], padCh2.avgQ[0],padCh2.avgI[0],padCh3.avgQ[0],padCh3.avgI[0])); DEBUGPRINT(DP_INFO, drvPadLan9118ReadFifosFlag, ("myDrvPadReadFifosRowMajor: %ld %ld %ld %ld %ld %ld\n", padCh0.sigmaSqQ, padCh0.sigmaSqI, /* padCh1.sigmaSqQ, padCh1.sigmaSqI, */ padCh2.sigmaSqQ, padCh2.sigmaSqI, padCh3.sigmaSqQ, padCh3.sigmaSqI)); #endif break; case (2): /* GN1 */ case (3): /* L0. Both A and B */ case (5): /* TC0 */ case (7): /* L1S */ case (8): /* TC3 */ *dst_p = (padCh0.avgQ[0]<<16) | (padCh0.avgI[0] & 0xffff); *dst_p = (padCh1.avgQ[0]<<16) | (padCh1.avgI[0] & 0xffff); *dst_p = (padCh2.avgQ[0]<<16) | (padCh2.avgI[0] & 0xffff); *dst_p = (padCh3.avgQ[0]<<16) | (padCh3.avgI[0] & 0xffff); /* padding to make this packet equal in size to PH1 and 2 packet */ *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118ReadFifosFlag, ("myDrvPadReadFifosRowMajor: %ld %ld %ld %ld %ld %ld %ld %ld\n", padCh0.avgQ[0],padCh0.avgI[0], padCh1.avgQ[0],padCh1.avgI[0], padCh2.avgQ[0],padCh2.avgI[0],padCh3.avgQ[0],padCh3.avgI[0])); #endif break; case (6): /* L1X */ *dst_p = (padCh0.avgQ[0]<<16) | (padCh0.avgI[0] & 0xffff); *dst_p = (padCh1.avgQ[0]<<16) | (padCh1.avgI[0] & 0xffff); *dst_p = (padCh3.avgQ[0]<<16) | (padCh3.avgI[0] & 0xffff); *dst_p = (padCh2.avgS<<16) | 0xffff; /* padding to make L1X packet equal in size to PH1 and 2 packet */ *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118ReadFifosFlag, ("myDrvPadReadFifosRowMajor: %ld %ld %ld %ld %ld %ld %ld\n", padCh0.avgQ[0],padCh0.avgI[0], padCh1.avgQ[0],padCh1.avgI[0], padCh3.avgQ[0],padCh3.avgI[0], padCh2.avgS)); #endif break; case (9): /* PH1 and 2 */ *dst_p = (padCh0.avgQ[0]<<16) | (padCh0.avgI[0] & 0xffff); *dst_p = (padCh0.avgQ[1]<<16) | (padCh0.avgI[1] & 0xffff); *dst_p = (padCh1.avgQ[0]<<16) | (padCh1.avgI[0] & 0xffff); *dst_p = (padCh1.avgQ[1]<<16) | (padCh1.avgI[1] & 0xffff); *dst_p = (padCh2.avgQ[0]<<16) | (padCh2.avgI[0] & 0xffff); *dst_p = (padCh2.avgQ[1]<<16) | (padCh2.avgI[1] & 0xffff); *dst_p = (padCh3.avgQ[0]<<16) | (padCh3.avgI[0] & 0xffff); *dst_p = (padCh3.avgQ[1]<<16) | (padCh3.avgI[1] & 0xffff); *dst_p = ((*(padCh0.ofst)+*(padCh0.sz)/2)<<16) | (((*(padCh0.ofst+1)+*(padCh0.sz+1)/2) & 0xffff)); *dst_p = ((*(padCh1.ofst)+*(padCh1.sz)/2)<<16) | (((*(padCh1.ofst+1)+*(padCh1.sz+1)/2) & 0xffff)); *dst_p = ((*(padCh2.ofst)+*(padCh2.sz)/2)<<16) | (((*(padCh2.ofst+1)+*(padCh2.sz+1)/2) & 0xffff)); *dst_p = ((*(padCh3.ofst)+*(padCh3.sz)/2)<<16) | (((*(padCh3.ofst+1)+*(padCh3.sz+1)/2) & 0xffff)); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118ReadFifosFlag, ("myDrvPadReadFifosRowMajor: %ld %ld %ld %ld %ld %ld %ld %ld\n", padCh0.avgQ[0],padCh0.avgI[0],padCh0.avgQ[1],padCh0.avgQ[1], padCh1.avgQ[0],padCh1.avgI[0],padCh1.avgQ[1],padCh1.avgQ[1])); DEBUGPRINT(DP_INFO, drvPadLan9118ReadFifosFlag, ("myDrvPadReadFifosRowMajor: %d %d %d %d\n", padCh0.ofst[0]+padCh0.sz[0]/2,padCh0.ofst[1]+padCh0.sz[1]/2, padCh1.ofst[0]+padCh1.sz[0]/2,padCh1.ofst[1]+padCh1.sz[1]/2)); DEBUGPRINT(DP_INFO, drvPadLan9118ReadFifosFlag, ("myDrvPadReadFifosRowMajor: %ld %ld %ld %ld %ld %ld %ld %ld\n", padCh2.avgQ[0],padCh2.avgI[0],padCh2.avgQ[1],padCh2.avgQ[1], padCh3.avgQ[0],padCh3.avgI[0],padCh3.avgQ[1],padCh3.avgQ[1])); DEBUGPRINT(DP_INFO, drvPadLan9118ReadFifosFlag, ("myDrvPadReadFifosRowMajor: %d %d %d %d\n", padCh2.ofst[0]+padCh2.sz[0]/2,padCh2.ofst[1]+padCh2.sz[1]/2, padCh3.ofst[0]+padCh3.sz[0]/2,padCh3.ofst[1]+padCh3.sz[1]/2)); #endif break; case (11): /* PH3 */ *dst_p = (padCh3.avgQ[0]<<16) | (padCh3.avgI[0] & 0xffff); *dst_p = (padCh3.avgQ[1]<<16) | (padCh3.avgI[1] & 0xffff); *dst_p = ((*(padCh3.ofst)+*(padCh3.sz)/2)<<16) | (((*(padCh3.ofst+1)+*(padCh3.sz+1)/2) & 0xffff)); /* padding to make PH3 packet equal in size to PH1 and 2 packet */ *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118ReadFifosFlag, ("myDrvPadReadFifosRowMajor: %ld %ld %ld %ld %d %d\n", padCh3.avgQ[0],padCh3.avgI[0],padCh3.avgQ[1],padCh3.avgQ[1], padCh3.ofst[0]+padCh3.sz[0]/2,padCh3.ofst[1]+padCh3.sz[1]/2)); #endif break; case (12): /* PH4 Assume it's in channel 3 so we can use BPH3 define */ *dst_p = (padCh3.avgQ[0]<<16) | (padCh3.avgI[0] & 0xffff); *dst_p = (padCh3.avgQ[1]<<16) | (padCh3.avgI[1] & 0xffff); *dst_p = ((*(padCh3.ofst)+*(padCh3.sz)/2)<<16) | (((*(padCh3.ofst+1)+*(padCh3.sz+1)/2) & 0xffff)); /* padding to make PH4 packet equal in size to PH1 and 2 packet */ *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; *dst_p = 0x0; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118ReadFifosFlag, ("myDrvPadReadFifosRowMajor: %ld %ld %ld %ld %d %d\n", padCh3.avgQ[0],padCh3.avgI[0],padCh3.avgQ[1],padCh3.avgQ[1], padCh3.ofst[0]+padCh3.sz[0]/2,padCh3.ofst[1]+padCh3.sz[1]/2)); #endif break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadLan9118ReadFifosFlag, ("myDrvPadReadFifosRowMajor: unrecognized pad card: %d\n", pad_card)); #endif } } /* could be an array for all IRQs in reg. for now, assume just 1 is used */ int padIntrStatus = 0; /*0=disabled, 1=enabled */ int padGetIntrStatus() { return padIntrStatus; } int padSetIntrStatus(int status) { padIntrStatus = status; return OK; } /* 4/28/8: who/where sets nsamples? packBuffer? etc */ /* 5/16/8: I still don't know, but here is how to calculate it: 1 sample = 4 16-bit values, i.e. 1 value for each of the 4 channels */ /* 6/26/8: nsamples is the number sent by the VME requesting streaming */ void * getdata(void *packBuffer, int idx, int nsamples, int endianLittle, int colMajor, void *uarg) { assert ( 0 == ( nsamples & 1 ) ); if ( colMajor ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118GetdataFlag, ("getdata: reading %d*4 samples in column major order into 0x%0x\n", nsamples, (volatile unsigned int) LAN9118_FIFO_HACK)); #endif drvPadReadFifosColMajor( LAN9118_FIFO_HACK, nsamples * 4, endianLittle ); } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118GetdataFlag, ("getdata: reading %d*4 samples in row major order into 0x%0x\n", nsamples, (volatile unsigned int) LAN9118_FIFO_HACK)); #endif myDrvPadReadFifosRowMajor( LAN9118_FIFO_HACK, nsamples * 4, endianLittle ); } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118GetdataFlag, ("getdata: LAN gets %d samples of four 16b vals\n", nsamples)); #endif return 0; } void * getProcessedData(void *packBuffer, int idx, int nsamples, int d32, int endianLittle, int colMajor, void *uarg) { assert ( 0 == ( nsamples & 1 ) ); if ( colMajor ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118GetdataFlag, ("getProcessedData: reading %d*4 samples in column major order into 0x%0x\n", nsamples, (volatile unsigned int) LAN9118_FIFO_HACK)); #endif drvPadReadFifosColMajor( LAN9118_FIFO_HACK, nsamples * 4, endianLittle ); } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118GetdataFlag, ("getProcessedData: reading %d*4 samples in row major order into 0x%0x\n", nsamples, (volatile unsigned int) LAN9118_FIFO_HACK)); #endif myDrvPadReadFifosRowMajor( LAN9118_FIFO_HACK, nsamples * 4, endianLittle ); } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118GetdataFlag, ("getProcessedData: LAN gets %d samples of four 16b vals\n", nsamples)); #endif return 0; } void * getRawData(void *packBuffer, int idx, int nsamples, int d32, int endianLittle, int colMajor, void *uarg) { assert ( 0 == ( nsamples & 1 ) ); if ( colMajor ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118GetdataFlag, ("getRawData: reading %d*4 samples in column major order into 0x%0x\n", nsamples, (volatile unsigned int) LAN9118_FIFO_HACK)); #endif drvPadReadFifosColMajor( LAN9118_FIFO_HACK, nsamples * 4, endianLittle ); } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118GetdataFlag, ("getRawData: reading %d*4 samples in row major order into 0x%0x\n", nsamples, (volatile unsigned int) LAN9118_FIFO_HACK)); #endif drvPadReadFifosRowMajor( LAN9118_FIFO_HACK, nsamples * 4, endianLittle ); } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118GetdataFlag, ("getRawData: LAN gets %d samples of four 16b vals\n", nsamples)); #endif return 0; } static int checkOverrun(int irq_pin) { /* If we need a soft trigger then things are more complicated: * We need to probe the status of the IRQ line because if we * raise the trigger while a acquisition is in progress * then we could lock out a trigger instead of generating one... */ if ( coldfEportEppdrGet(irq_pin) ) { /* no acquisition in progress, either a HW trigger * happening right now or this SW trigger will be * caught. */ } else { /* Ops. IRQ line was already low. In this case, * we have to check if this is a new interrupt * that just happened or if it was lost. */ if ( coldfEportFlagGet(irq_pin) ) { /* still pending just let them detect this interrupt */ } else { return -1; } } return 0; } /* Issue a software ADC trigger * RETURNS: 0 on success, nonzero if no trigger raised * (but a ongoing acq. is used with no IRQ pending). * */ int drvPadAdcTrigger(int trig_pin, int irq_pin) { int old_mask; int rval; old_mask = coldfEportIntDisable_inl(irq_pin); #ifdef OUT coldfEportEpdrSet(trig_pin, 1); #endif rval = checkOverrun(irq_pin); if ( old_mask ) coldfEportIntEnable(irq_pin); return rval; } /*============================================================================= Name: padISR Abs: Executes every time the interrupt that indicates FIFO full arrives Rem: No function prototype. Passes typedef struct in. =============================================================================*/ static void padISR (void *arg_p) { padLan9118_t* info_ps = (padLan9118_t*)arg_p; coldfEportIntDisable(info_ps->irq_pin); /* Clear interrupt here so we can detect a trigger overrun */ coldfEportFlagClr(info_ps->irq_pin); /* disarm by raising SW trigger * * NOTE: THIS REQUIRES THE TRIGGER INPUT BEING 'active high' * w/o this arrangement there is no way to disarm * the PAD and preserve its FIFO. * We use this triggering scheme as a workaround: * * Trigger: ---------------- ------------------ * | 10ns | | overrun * |______| |___ * * IRQ: ------------- * fifo | * full | * ______________________________| */ #ifdef OUT trigRaise(info_ps->trig_pin); #endif if (drvPadLan9118ISRFlag) { epicsInterruptContextMessage("drvPadLan9118: Interrupt\n"); } /* Increment counter to show life */ if (drvPadLan9118Irqs< PAD_MAX_INT) { drvPadLan9118Irqs++; } else { drvPadLan9118Irqs= 0; numIntrRollovers++; } /* Wake up task which does the data acquisition */ epicsEventSignal(info_ps->data_ready); #ifdef PAD_SIMPLE_PROCESSING /* Clear the interrupt by writing a "1" to "EPF2" */ /* Ref: p. 11-7 of EPORT manual. */ *epfr = 4; #endif return; } /* scondam: 01.28.09: */ /*============================================================================= Name: padGetPA0 Abs: show the output port status (Pin 24 which is GPT PA0) =============================================================================*/ int padGetPA0() { return *gptport; } #ifdef PAD_SIMPLE_PROCESSING /* Beginning of functions specific to interlocking PADs */ /*============================================================================= Name: padGetFaultCount Abs: show the number of times the Fault states were entered since boot =============================================================================*/ int padGetFaultCount() { return faultCount; } /* scondam: 01.28.09: */ /*============================================================================= Name: padGetState Abs: Return the combined state of all channels. Even if one channel is in FAULT, State is FAULT. All channels must be NORMAL, for State to be NORMAL. =============================================================================*/ int padGetState() { return State; } /*============================================================================= Name: padResetState Abs: Reset State to NORMAL and set gptport to HIGH. =============================================================================*/ int padResetState() { *gptport = 0x1; State = NORMAL; return OK; } /*============================================================================= Name: padGetThreshold Abs: Return the threshold % set by user for a channel 'x' in the PV TST_x_LEVEL =============================================================================*/ int padGetThreshold(int channel) { if (channel <= 3) return pChannels[channel]->threshold; DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetThreshold: unrecognized channel: %d\n", channel)); return 0; } /*============================================================================= Name: padSetThreshold Abs: Set the threshold in % for a channel 'x' in the PV TST_x_LEVEL =============================================================================*/ int padSetThreshold(int channel, int val) { if (channel <= 3) { if ((val >= 0) && (val <= 100)) { pChannels[channel]->threshold = val; } else { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padSetThreshold: unrecognized threshold: %d. Accepted values are 0-100\n", channel)); } } else { DEBUGPRINT(DP_ERROR, drvPadFlag, ("padSetThreshold: unrecognized channel: %d\n", channel)); } return 0; } /*============================================================================= Name: padGetUpperThreshold Abs: Return the threshold % set by user for a channel 'x' in the PV TST_x_LEVEL =============================================================================*/ int padGetUpperThreshold(int channel) { if (channel <= 3) { long thresh = labs(pChannels[channel]->avgOut) * (pChannels[channel]->threshold / 100.0); return (pChannels[channel]->avgOut + thresh); } DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetUpperThreshold: unrecognized channel: %d\n", channel)); return 0; } /*============================================================================= Name: padGetLowerThreshold Abs: Return the threshold % set by user for a channel 'x' in the PV TST_x_LEVEL =============================================================================*/ int padGetLowerThreshold(int channel) { if (channel <= 3) { long thresh = labs(pChannels[channel]->avgOut) * (pChannels[channel]->threshold / 100.0); return (pChannels[channel]->avgOut - thresh); } DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetLowerThreshold: unrecognized channel: %d\n", channel)); return 0; } /*============================================================================= Name: padGetFaultingPulses Abs: Return the number of pulses in fault since boot =============================================================================*/ int padGetFaultingPulses(int channel) { if (channel <= 3) { fail_state* fSt; fSt = fst[channel]; return fSt->faults; } DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetFaultingPulses: unrecognized channel: %d\n", channel)); return 0; } /*============================================================================= Name: padResetFaultingPulses Abs: Reset the number of faulting pulses for all 4 channels and the total number of system faults since boot. =============================================================================*/ int padResetFaultingPulses(void) { int channel; for (channel=0; channel<4; channel++) { fail_state* fSt; fSt = fst[channel]; fSt->faults = 0; } faultCount = 0; DEBUGPRINT(DP_ERROR, drvPadFlag, (" padResetFaultingPulses: Resetting number of fault pulses for all channels and the total number of system faults\n")); return 0; } #endif /* end of functions specific to Interlocking PADs */ /*============================================================================= Name: padGetIntrCount Abs: Return the number of interrupts that have been processed since boot =============================================================================*/ int padGetIntrCount() { return drvPadLan9118Irqs; } /*============================================================================= Name: padResetIntrCount Abs: Reset the intrCount to 0 =============================================================================*/ int padResetIntrCount() { drvPadLan9118Irqs = 0; return OK; } /*============================================================================= Name: padGetPreviousIntrCount Abs: Return the number of interrupts processed in the previous second =============================================================================*/ int padGetPreviousIntrCount() { int prev = prev_intrCount; prev_intrCount = drvPadLan9118Irqs; return prev; } int drvPadLan9118Initialise_Timer() { #ifdef Never extern void Timer_initialize(); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118: init\n")); #endif if ( !after ) Timer_initialize(); #endif return 0; } int drvPadLan9118Report(int level) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("PAD Stream Support for Communication with BPM Processor\n")); DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("over 2nd Ethernet Interface\n")); #endif if ( level > 1 ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("Dumping Interface Statistics\n"); #endif drvLan9118DumpStats(lanIpBscIfGetDrv(lanIpIf), 0)); } return 0; } static int start_stop_cb(int start, void *uarg) { /* 6/12/8 new guts */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadCbFlag, ("start_stop_cb:\n")); #endif myArm(); #ifdef OLD_WAY /* return devPadWfSetRarm(start ? 2 : 0); out 4/23/8 */ #endif return OK; } epicsThreadId drvPadStreamInit() { char *padslot; char *mcgrp; PadUdpThreadArg arg =0; struct in_addr ina; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadStreamInit: calling drvLan9118/padStreamInitialize...\n")); #endif /* ( padStreamInitialize(lanIpIf, start_stop_cb, 0) ) */ if ( padStreamInitialize(lanIpIf, start_stop_cb, (void*)&(ppadLan9118->irq_pin)) ) { /* 5/12/8 */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadLan9118Flag, ("drvPadStreamInit: error calling padStreamInitialize\n")); #endif return 0; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadStreamInit: padStreamInitialize successful\n")); #endif if ( ! (arg = malloc(sizeof(*arg))) ) { errlogPrintf("drvPadStreamInit(): no memory\n"); return 0; } if ( !(padslot = getenv("PADSLOT")) ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadLan9118Flag, ("drvPadStreamInit: enviroment variable PADSLOT not set\n")); #endif errlogPrintf("Unable to start UDP listener; enviroment variable PADSLOT not set\n"); return 0; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadStreamInit: padslot set to enviroment variable PADSLOT: %s\n",padslot)); DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadStreamInit: padslot must match VME's $(pad) macro in _WF_CP INP field: %s\n",padslot)); #endif pad_card = atoi(padslot); arg->channel = atoi(padslot); arg->mcaddr = 0; if ( !(mcgrp = getenv("PADMCGRP")) ) { errlogPrintf("Warning: 'PADMCGRP' envvar unset; expecting to use BROADCAST for IOC communication\n"); } else { if ( hostToIPAddr(mcgrp, &ina) ) { errlogPrintf("Error: Lookup for '%s' failed -- unable to start UDP listener\n", mcgrp); return 0; } arg->mcaddr = ina.s_addr; errlogPrintf("UDP Listener uses IP Multicast (%s) -- reception of broadcast traffic DISABLED\n", mcgrp); drvLan9118BcFilterSet(lanIpBscIfGetDrv(lanIpIf), 1); } errlogPrintf("Starting UDP listener for PAD channel %i\n", arg->channel); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadStreamInit: Starting padUdpListener thread to run padUdpThread() for PAD channel %i\n", pad_card)); #endif return epicsThreadCreate( "padUdpListener", epicsThreadPriorityScanHigh, epicsThreadGetStackSize(epicsThreadStackMedium), padUdpThread, /* (void*)pad_card */ (void*) arg); } char ourBuffer[2000]; int setOurBuffer(char* stringToWrite) { strcpy(ourBuffer, stringToWrite); return OK; } void * getMyData(void *buf, int idx, int nsamples, int endianLittle, int colMajor, void *uarg) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("getMyData:\n")); #endif memcpy( buf, ourBuffer, nsamples * 2 ); return buf; } static void padUdpThread(void *arg_p) { int err; PadUdpThreadArg arg = arg_p; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("padUdpThread: starting padUdpHandler()\n")); #endif /* use default port and channel number passed as arg */ if ( /* (err = padUdpHandler(0, (int)arg))*/ (err = padUdpHandler(arg->mcaddr /*mac*/, 0 /* default port*/, arg->channel /* port */, 0 /* default timeout */, 0 /*poll callback */ , 0 /*arg for the callback */)) ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadLan9118Flag, ("padUdpThread: padUdpHandler() aborted: %s\n", strerror(-err))); #endif return; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("padUdpThread: padUdpHandler started successfully for channel %d\n", (int)arg)); #endif } /* * /CS (chip-select) of the 4 fifos are driven by A2 / A3. * * This leads to the following memory layout starting at CS1_BASE_ADDR: * * 0..3 channel 0 * 4..7 channel 1 * 8..11 channel 2 * 12..15 channel 3 * 16..19 channel 0 * 20..23 channel 1 * 24..27 channel 2 * 28..31 channel 3 * ... and so on ... * * Seen as a struct of 16-bit values this looks like this * * struct { * signed short ch0; * signed short ch0; * signed short ch1; * signed short ch1; * signed short ch2; * signed short ch2; * signed short ch3; * signed short ch3; * } */ static inline uint32_t bswap(uint32_t x) { asm volatile("byterev %0; swap %0":"+d"(x)); return x; } #define CPYW(i) *dst_p++ = src_p[i]; #define CPY4W do { CPYW(0); CPYW(2); CPYW(4); CPYW(6); } while (0) #define CPYWS(i) *dst_p++ = bswap(src_p[i]); #define CPY4WS do { CPYWS(0); CPYWS(2); CPYWS(4); CPYWS(6); } while (0) void drvPadReadFifosColMajor(volatile void *dst_p_v, int n, int do_bswap) { /* not implemented */ } #define CPYL *dst2_p++ = *(volatile uint32_t_a*)src_p; #define CPY4L do { CPYL; CPYL; CPYL; CPYL; } while (0) #define CPYLS *dst2_p++ = bswap(*(volatile uint32_t_a*)src_p); #define CPY4LS do { CPYLS; CPYLS; CPYLS; CPYLS; } while (0) static void readChannel(volatile int16_t_a *dst_p, volatile int16_t_a *src_p, int leading, int n, int do_bswap) { /* word align destination */ register volatile uint32_t_a *dst2_p; /* 'leading' indicates that the destination is unaligned */ if ( leading ) *dst_p++ = do_bswap ? bswap(*src_p) : *src_p; /* Now we are dword aligned and can transfer 32-bit words at a time; * this should work endian-independently :-): the first word read * out of the fifo gets to whatever word-address that is written first... */ dst2_p = (uint32_t_a*)dst_p; if ( !do_bswap ) { while ( n>=16 ) { CPY4L; CPY4L; n-=16; } while ( n>1 ) { CPYL; n-=2; } } else { while ( n>=16 ) { CPY4LS; CPY4LS; n-=16; } while ( n>1 ) { CPYLS; n-=2; } } /* cleanup unaligned end */ if ( n ) { dst_p = (int16_t_a*)dst2_p; *dst_p++ = do_bswap ? bswap(*src_p) : *src_p; } } void drvPadReadFifosRowMajor(volatile void *dst_pv, int n, int do_bswap) { volatile int16_t_a *dst_p = dst_pv; volatile int16_t_a *src_p = (volatile int16_t_a *)CS1_BASE_ADDR; int odd; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadReadFifosRowMajor: n=%d, do_bswap=%d\n", n, do_bswap)); #endif assert ( !(n & 3) ); /* Every channel needs 3 initial clock cycles (Ron uses /CS for the clock also) */ *(volatile uint32_t_a*)(src_p + 0); src_p[0]; *(volatile uint32_t_a*)(src_p + 2); src_p[2]; *(volatile uint32_t_a*)(src_p + 4); src_p[4]; *(volatile uint32_t_a*)(src_p + 6); src_p[6]; n>>=2; /* samples per channel */ /* If n is odd then the destination for the first sample of odd-numbered * channels is not aligned. Get a single sample on the odd channels * and a pair on the even ones. */ odd = n & 1; readChannel(dst_p, src_p + 0, 0, n, do_bswap); dst_p += n; readChannel(dst_p, src_p + 2, odd, n - odd, do_bswap); dst_p += n; readChannel(dst_p, src_p + 4, 0, n, do_bswap); dst_p += n; readChannel(dst_p, src_p + 6, odd, n - odd, do_bswap); } /*============================================================================= Name: padDumpSignal(int channel) Abs: Print out latest raw data in channel "channel" Special handling for PADLAN9118_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 PADLAN9118_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 #ifdef PADLAN9118_BPH3 if (channel == 3) { 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 PADLAN9118_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 PADLAN9118_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 #ifdef PADLAN9118_BPH3 if (channel == 3) { printf(" sample 1: "); for (ii=0; ii<*(pCh->sz); ii++) { printf ("addr:0x%08x val:%ld ", (unsigned int)(pCh->proc1+ii),*(pCh->proc1+ii)); } printf("\n"); } #endif return OK; } /*============================================================================= Name: padDumpChannel Abs: Debug routine used to look at the contents of a channel's structure Ret: OK =============================================================================*/ int padDumpChannel(int channel ) { int ii; rf_channel_t* pCh; iqcal_channel_t* pCCh; switch (channel) { case 0: case 1: case 2: case 3: printf("Contents of RF channel %d, (range is 0-3), sample 0\n", channel); pCh = pChannels[channel]; printf("sz: %d\n", *(pCh->sz)); printf("ofst: %d\n", *(pCh->ofst)); printf("\n"); printf("nToRead: %d\n", *(pCh->nToRead)); printf("nRead: %d\n", *(pCh->nRead)); printf("sumQ: %lld\n", *(pCh->sumQ)); printf("numQ: %d\n", *(pCh->numQ)); printf("avgQ: %ld\n", *(pCh->avgQ)); printf("sumI: %lld\n", *(pCh->sumI)); printf("numI: %d\n", *(pCh->numI)); printf("avgI: %ld\n", *(pCh->avgI)); printf("x: %d\n", *(pCh->x)); printf("sumS: %lld\n", pCh->sumS); printf("numS: %d\n", pCh->numS); printf("avgS: %d\n", (int) pCh->avgS); printf("\nsample 1. valid for phase cavity channels\n"); printf("sz: %d\n", *(pCh->sz+1)); printf("ofst: %d\n", *(pCh->ofst+1)); printf("sumQ: %lld\n", *(pCh->sumQ+1)); printf("numQ: %d\n", *(pCh->numQ+1)); printf("avgQ: %ld\n", *(pCh->avgQ+1)); printf("sumI: %lld\n", *(pCh->sumI+1)); printf("x: %d\n", *(pCh->x+1)); printf("nToRead: %d\n", *(pCh->nToRead+1)); printf("nRead: %d\n", *(pCh->nRead+1)); printf("numI: %d\n", *(pCh->numI+1)); printf("avgI: %ld\n", *(pCh->avgI+1)); printf("\nVariance-related terms. valid for some reference dist'n and laser channels\n"); printf("sumSqQ: %lld\n", pCh->sumSqQ); printf("sumSqI: %lld\n", pCh->sumSqI); printf("sigmaSqQ: %ld\n", pCh->sigmaSqQ); printf("sigmaSqI: %ld\n", pCh->sigmaSqI); printf("First 10 values of scalarI...valid only where channel is not sampled mult of 8.5MHz\n"); for (ii=0; ii<10; ii++) { printf("addr:0x%08x val:%8hd\n", (unsigned int)(pCh->scalarI+ii), *(pCh->scalarI+ii)); } printf("First 10 values of scalarQ...valid same as scalarI\n"); for (ii=0; ii<10; ii++) { printf("addr:0x%08x val:%8hd\n", (unsigned int)(pCh->scalarQ+ii), *(pCh->scalarQ+ii)); } printf("First 10 values of scalar...valid where channel is sampled at mult of 8.5 MHz\n"); for (ii=0; ii<10; ii++) { printf("addr:0x%08x val:%8hd\n", (unsigned int)(pCh->scalar+ii), *(pCh->scalar+ii)); } printf("\n"); padDumpSignal(channel); /* does both samples if PADLAN9118_BPH or PADLAN9118_BPH3 */ padDumpProcessedSignal(channel); /* does both samples if PADLAN9118_BPH or PADLAN9118_BPH3*/ 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 drvPadLan9118Initialise?\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", drvPadLan9118Irqs); printf("Num I overflow errors since boot: %d\n", overflowICount); printf("Num Q overflow errors since boot: %d\n", overflowQCount); #ifdef SCAN_IO printf("Num scan I/O requests since boot: %d\n", scanIoCount); #endif #ifdef USING_QSPI /* print out QSPI register contents */ rc = padQSPIReport(); #endif rc = padDumpChannel(0); rc = padDumpChannel(1); rc = padDumpChannel(2); rc = padDumpChannel(3); #ifdef PAD_PROCESS_DURING_READ /* Since processed data is not currently saved, skip this */ #endif return interest; } /*============================================================================= Name: padSetDataFileStem Abs: =============================================================================*/ /* Set the stem of the filename for the output data files */ int padSetDataFileStem(char * stem){ strcpy(filenameStem, stem); printf("padSetDataFileStem: setting filename to stem '%s'\n", filenameStem); return OK; } /*============================================================================= Name: padGetDataFileStem Abs: =============================================================================*/ char * padGetDataFileStem() { return filenameStem; } /*============================================================================= Name: padInitialiseScalarFromFile Abs: RF is at 2856 MHz. There is no digitizer that runs this fast. So work at IF (intermediate frequency) of 25.5 MHz and (smartly) choose to "digitally downmix" (a fancy name for taking the sine and cosine of something) to the baseband frequency of DC (0 MHz). By choosing and ADC clock of 102 MHz, there are 4 points to each IF cycle. They are pi/2 apart, so taking the sine and cosine has the result that: 0 sin(0)=0; Q=0 cos(0)=1; I=I pi/2 sin(pi/2)=1; Q=Q cos(pi/2)=0; I=0 pi sin(pi)=0, Q=0 cos(pi)=-1; I=-I 3pi/2 sin(3pi/2)=-1; Q=-Q cos(3pi/2)=0; I=0 Since Q is non-zero only when I is zero, and vice versa, the two variables can be stored in a single array, in alternating positions What is stored in "scalar" looks like: [ 1, 1, -1, -1, ... ] and maps to the Qs and Is as: [ Q1, I2, Q3, I4, ... ] where I1, Q2, I3 and Q4 are all zero. But this is not all. There is also a weighting factor multiplied to each value in scalar to correct the I and Qs to be "what the beam sees". If the application is PADLAN9118_LSR ch0 or PAD_REF ch0-3, then scalar needs to be 64 Kwords long (PAD_FIFO_SIZE). Otherwise, it needs to be only PAD_SAMPLE_SIZE long. Note that for PAD_KLY, no scalar correction is needed. Go get values for the scalar corrections from a value. One of three pointers in channel structure is filled in, depending on the value of sample. Ret: OK/ERROR =============================================================================*/ int padInitialiseScalarFromFile(int channel, int inputType, char* filename){ int ii; rf_channel_t* pCh; FILE *fin; /* Ensure nelm <= PAD_SAMPLE_SIZE? */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadScalFlag, ("padInitialiseScalarFromFile: inputs: channel: %d, inputType: %d, filename = %s\n", channel, inputType, filename)); #endif /* Attempt to find/open the file */ fin = fopen(filename, "r"); if (fin==NULL) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadScalFlag, ("padInitialiseScalarFromFile: unable to open '%s' for read. Exiting.\n", filename)); #endif return ERROR; } /* Read the values into scalar */ pCh = pChannels[channel]; switch (inputType) { case PAD_SCALAR: /* malloc space for and put file contents into channel struct's "scalar" */ printf("Filling scalar for ch%d\n", channel); pCh->scalar = (short*)malloc(sizeof(short)*PAD_SAMPLE_SIZE); for (ii=0; iiscalar+ii)); } break; case PAD_SCALAR_NON_INTERLACED_I: /* file contents into channel struct's "scalarI" */ printf("Filling scalarI for ch%d\n", channel); pCh->scalarI = (short*)malloc(sizeof(short)*PAD_SAMPLE_SIZE); for (ii=0; iiscalarI+ii)); } break; case PAD_SCALAR_NON_INTERLACED_Q: /* file contents into channel struct's "scalarQ" */ printf("Filling scalarQ for ch%d\n", channel); pCh->scalarQ = (short*)malloc(sizeof(short)*PAD_SAMPLE_SIZE); for (ii=0; iiscalarQ+ii)); } break; case PAD_CONSTANT: pCh->scalar = (short*)malloc(sizeof(short)); /* only 1 constant; not an array */ fscanf(fin, "%hd", pCh->scalar); printf("Filling scalar constant with %hd for ch%d from '%s'\n", *(pCh->scalar), channel, filename); break; default: DEBUGPRINT(DP_ERROR, drvPadScalFlag, ("padInitialiseScalarFromFile: unrecognized inputType: %d\n", inputType)); fclose(fin); return ERROR; } /* Close the file */ fclose(fin); return OK; } /*============================================================================= Name: padGetOverflowQCount Abs: Return the number of Q data overflows that have been processed since boot =============================================================================*/ int padGetOverflowQCount() { return overflowQCount; } /*============================================================================= Name: padGetOverflowICount Abs: Return the number of I data overflows that have been processed since boot =============================================================================*/ int padGetOverflowICount() { return overflowICount; } /*============================================================================= Name: padResetOverflowICount Abs: Reset the number of I data overflows =============================================================================*/ int padResetOverflowICount() { overflowICount = 0; return OK; } /*============================================================================= Name: padResetOverflowQCount Abs: Reset the number of Q data overflows =============================================================================*/ int padResetOverflowQCount() { overflowQCount = 0; return OK; } #ifdef SCAN_IO /*============================================================================= Name: padGetScanIOCount Abs: =============================================================================*/ int padGetScanIOCount() { return scanIoCount; } /*============================================================================= Name: padResetScanIOCount Abs: Reset the number of Scan I/O counts =============================================================================*/ int padResetScanIOCount() { scanIoCount = 0; return OK; } #endif /*============================================================================= Name: padTurnOn/OffLED Abs: It should be obvious =============================================================================*/ int padTurnOnLED() { volatile unsigned char *place; place = (unsigned char *) EPDR; *place = 0xF7; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padTurnOnLED: %d\n", (int) *place)); #endif return OK; } int padTurnOffLED() { volatile unsigned char *place; place = (unsigned char *) EPDR; *place = 0xFF; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padTurnOffLED: %d\n", (int) *place)); #endif return OK; } /* It should take 2.5 millisec to write to the 256 Kword FIFO It should take 12.5 millisec to read from the 256 Kword FIFO NB: without the "while" stmt, you have to call this once to enable FIFO (which isn't yet full) and then call it again to see the full FIFO Args: Type Name Access Description --------------- ------------- ---------- ---------------------------- int startingPoint read-only first 40 nsec increment to read int num40SecIncs read-only number of (16 bit) shorts to read */ /*============================================================================= Name: padGetSampleOffset Abs: =============================================================================*/ int padGetSampleOffset(int channel, int sample) { int* addr = NULL; int val = 0; iqcal_channel_t* pCCh; rf_channel_t* pCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; addr = pCh->ofst; val = *(addr+sample); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleOffset: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleOffset: address to ofst[0] is 0x%08x\n", (unsigned int) addr)); if (sample > 0) { DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleOffset: address to ofst + sample %d is 0x%08x\n", sample, (unsigned int) (addr+sample))); } DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleOffset: ofst is %d\n", val)); #endif break; case 4: case 5: case 6: case 7: pCCh = pCalChannels[channel-4]; val = pCCh->ofst; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleOffset: address to channel structure is 0x%08x\n", (unsigned int) &pCCh)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleOffset: address to ofst is 0x%08x\n", (unsigned int) &(pCCh->ofst))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleOffset: ofst is %d\n", val)); #endif break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetSampleOffset: unrecognized channel: %d\n", channel)); #endif } return val; } /*============================================================================= Name: padSetSampleOffset Abs: Set offset to reflect desired value + 3 clock cycles that we must wait for =============================================================================*/ int padSetSampleOffset(int channel, int sample, int ofst) { int* addr; rf_channel_t* pCh; iqcal_channel_t* pCCh; /* Range checking limit depends on the mode of operation. When CALIBRATING, channels 4-7 are used. The sample offset is __________. When RUNNING, channels 0-3 are used. The sample offset is __________. */ switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; addr = pCh->ofst; addr = addr + sample; /*if (sample == 0) { ofst += 3; do 3 garbage reads while FIFO fills }*/ *addr = ofst; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetSampleOffset: address to channel structure is 0x%08x\n", (unsigned int) pCh)); DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetSampleOffset: address to ofst[%d] is 0x%08x\n", sample, (unsigned int) (addr+sample))); DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetSampleOffset: ofst[%d] = %d\n", sample, *addr)); #endif break; case 4: case 5: case 6: case 7: pCCh = pCalChannels[channel-4]; pCCh->ofst = ofst /*+ 3*/; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetSampleOffset: address to channel structure is 0x%08x\n", (unsigned int) pCCh)); DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetSampleOffset: address to ofst is 0x%08x\n", (unsigned int) &(pCCh->ofst))); DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetSampleOffset: ofst = %d\n", pCCh->ofst)); #endif break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetSampleOffset: unrecognized channel: %d\n", channel)); #endif return ERROR; } return OK; } /*============================================================================= Name: padGetSampleSize Abs: return sample size for any inputType, channel and sample =============================================================================*/ int padGetSampleSize(int channel, int sample) { int* addr; int val; rf_channel_t* pCh; iqcal_channel_t* pCCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; addr = pCh->sz; /* now that that has been dealt with, from this point on in this routine, sample indicates the sample number... */ val = *(addr+sample); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleSize: address to channel structure is 0x%08x\n", (unsigned int) &pCh)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleSize: address to sz[0] is 0x%08x\n", (unsigned int) addr)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleSize: address to sz[%d] is 0x%08x\n", sample, (unsigned int) (addr+sample))); #endif break; case 4: case 5: case 6: case 7: pCCh = pCalChannels[channel-4]; val = pCCh->sz; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleSize: address to channel structure is 0x%08x\n", (unsigned int) &pCCh)); #endif break; default: return ERROR; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSampleSize: sz is %d\n", val)); #endif return val; } /*============================================================================= Name: padSetSampleSize Abs: set sample size Inputs: type indicates interlaced vs non-interlaced processed data array channel is ADC channel sample is the window of interest in the FIFO, normally there is only one, which is numbered '0'. Phasing cavity PAD uses a second sample, which is numbered '1'. size is the size of the window of interest which must be no bigger than the allowed size. The allowed size depends on whether it is the calibration data or the operational data. =============================================================================*/ int padSetSampleSize(int type, int channel, int sample, int size) { int* addr=NULL; rf_channel_t* pCh; iqcal_channel_t* pCCh; int tmp; /* Range checking limit depends on the mode of operation. When CALIBRATING, channels 4-7 are used. The sample size is the PAD_FIFO_SIZE. When RUNNING, channels 0-3 are used. The sample size is PAD_SAMPLE_SIZE */ switch (channel) { case 0: case 1: case 2: case 3: if (size > PAD_SAMPLE_SIZE || size < 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetSampleSize: channel %d: requested size, %d, out of range [0,%d]. Using 24\n", channel, size, PAD_SAMPLE_SIZE)); #endif /*return ERROR;*/ /* 2/26/2007. This is not enough of an error handler. If you enter a huge number by accident. I.e. you append 300 to initial 80 and end up with 80300, you see this msg go by, but system hangs waiting for mbuf!!! */ size = 24; } /* 4/24/7: TO DO: If raw data is interlaced, ensure that size is a mult of 4; if raw data is non-interlaced, ensure that size is a mult of 6 */ if ((type == PAD_NON_INTERLACED_RAW)) { /* size must be a multiple of 6 */ tmp=size; while (tmp >= 6) { tmp -= 6; } if (tmp !=0 ) { /* Reject choice */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetSampleSize: channel %d: requested size, %d, is not a multiple of 6. using 24\n", channel, size)); #endif size = 24; } } else { if (type == PAD_RAW) { /* size must be a multiple of 4 */ tmp=size; while (tmp >= 4) { tmp -= 4; } if (tmp !=0 ) { /* Reject choice */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetSampleSize: channel %d: requested size, %d, is not a multiple of 4. using 24\n", channel, size)); #endif size = 24; } } } pCh = pChannels[channel]; if (sample==1) { addr = pCh->sz; addr = addr + sample; *addr = size; } else { /* case for first sample, just one sample and for Ifq */ /* Pb here is that "sample" is being used for two diff purposes: to hold the sample size of the window of interest AND to indicate that it's a non-interlaced sample (3 indicates it's non-inter I, 4 indicates non- inter Q. */ *(pCh->sz) = size; break; } case 4: case 5: case 6: case 7: pCCh = pCalChannels[channel-4]; pCCh->sz = size; break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetSampleSize: unrecognized channel: %d\n", channel)); #endif return ERROR; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetSampleSize: changing sample size for ch%d type %d sample %d to %d\n", channel, type, sample, size)); #endif return OK; } /*============================================================================= Name: padGetOperationMode Abs: return mode of operation =============================================================================*/ int padGetOperationMode() { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetOperationMode: %d\n", padOperationMode)); #endif return padOperationMode; } /*============================================================================= Name: padSetOperationMode Abs: set mode of operation =============================================================================*/ int padSetOperationMode(int mode) { /* range check on mode */ if ((mode < 0) || (mode> PAD_NUM_OP_MODES)) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadFlag, ("padSetOperationMode: mode %d is out of range [0,%d]\n", mode, PAD_NUM_OP_MODES)); #endif return ERROR; } padOperationMode = mode; return OK; } /*============================================================================= Name: padGetCalibrationControl Abs: return mode of operation =============================================================================*/ int padGetCalibrationControl() { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetCalibrationControl: %d\n", padCalibrationControl)); #endif return padCalibrationControl; } /*============================================================================= Name: padSetCalibrationControl Abs: set calibration control =============================================================================*/ int padSetCalibrationControl(int calibrationControl) { /* range check on calibrationControl */ if ((calibrationControl < 0) || (calibrationControl > PAD_NUM_CAL_MODES)) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadFlag, ("padSetCalibrationControl: calibrationControl %d is out of range [0,%d]\n", calibrationControl, PAD_NUM_CAL_MODES)); #endif return ERROR; } padCalibrationControl = calibrationControl; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padSetCalibrationControl: calibrationControl set to %d\n", calibrationControl)); #endif return OK; } int padReadSignal_interlaced(rf_channel_t *chan, int chan_num) { union { int32_t vi; struct { int16_t i; int16_t q; } vs; } tmp1, tmp2; int i; int j = 0; long isum = 0; long qsum = 0; volatile uint16_t *pSrc16 = csar1_actual; volatile uint32_t *pSrc32 = csar1_actual; pSrc16 += (chan_num *2); pSrc32 += chan_num; *(chan->nToRead) = *(chan->ofst) + *(chan->sz); for(i=0; i<3;i++) tmp1.vs.i = *pSrc16; for(i=0; i< *(chan->ofst); i++) tmp1.vs.i = *pSrc16; for(i=0; i < *(chan->sz)/4; i++) { tmp1.vi = *pSrc32; tmp2.vi = *pSrc32; if((tmp1.vs.i & 0x0001) || (tmp2.vs.i & 0x0001)) { overflowICount++; return 0; } if((tmp1.vs.q & 0x0001) || (tmp2.vs.q &0x0001)) { overflowQCount++; return 0; } isum += tmp1.vs.i - tmp2.vs.i; qsum += tmp1.vs.q - tmp2.vs.q; *(chan->data0 + (j++)) = tmp1.vs.i; *(chan->data0 + (j++)) = tmp1.vs.q; *(chan->data0 + (j++)) = tmp2.vs.i; *(chan->data0 + (j++)) = tmp2.vs.q; } if(!j) return 0; *(chan->nRead) = j; *(chan->sumI) = isum; *(chan->sumQ) = qsum; *(chan->avgI) = isum / (j/2); *(chan->avgQ) = qsum / (j/2); return 0; } /*============================================================================= Name: padGenReadSignal Abs: Reads PAD_NUM_CHANNELS_ON_BOARD FIFOs first by reading, but not storing, up to the offset, Past the offset, the 16bit integers are stored, corrected (scaled by and array of corrections) and then averaged. There are two variables, I and Q interleaved in a single channel, so the averages are obtained from summing the Is and Qs separately. Values past the ROI are never read. FIFO is reset by CPLD, not by any pad*.c code here. To just read the raw data (and skip correction and averaging), compile without PAD_PROCESS_DURING_READ set. =============================================================================*/ int padGenReadSignal(rf_channel_t* chan0, rf_channel_t* chan1, rf_channel_t* chan2, rf_channel_t* chan3) { int ii; /* absolute position in the FIFO */ int jj; /* the (ii - padSampleOffset)-th position in FIFO */ int mm; /* loop variable for all channels */ #ifndef FAKE_DATA volatile short *pSrc; /* MUST be volatile to avoid read-only *pSrc line below from being optimized away by the compiler AND for the padChanneli[jj] = *pSrc from being optimized to be assigned */ #endif int srcOfst = 0; #ifdef PAD_PROCESS_DURING_READ int isQ; int isOverflowI; int isOverflowQ; #endif rf_channel_t* chan = NULL; /* generic channel used in loop of all channels */ #ifndef FAKE_DATA pSrc = csar1_actual; #endif /* Fills data0, calculates avgI and avgQ for all four channels */ for (mm=0; mmnToRead) = *(chan->ofst) + *(chan->sz); #ifndef FAKE_DATA #ifdef DEBUG_PRINT /* This next line causes a read - it would change result if DEBUG_PRINT not defined! 6/12/7 */ DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: initiating chip select read at 0x%08x for %d words\n", (unsigned int) csar1_actual, *(chan->nToRead))); #endif #else #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: faking channel %d data for %d words\n", mm, *(chan->sz))); #endif #endif /* Here are the guts of the read. This code was timed and found to take 250 ns to read one short */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: guts of read for channel %d\n",mm)); #endif jj = 0; *(chan->sumQ) = 0; *(chan->sumI) = 0; *(chan->numQ) = 0; *(chan->numI) = 0; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: initially sumQ is %lld\n", *(chan->sumQ))); DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: initially numQ is %d\n", *(chan->numQ))); DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: initially sumI is %lld\n", *(chan->sumI))); DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: initially numI is %d\n", *(chan->numI))); #endif #ifndef FAKE_DATA /* Skip first three clock cycles to allow FIFO to fill */ *(pSrc+srcOfst); *(pSrc+srcOfst); *(pSrc+srcOfst); #endif for (ii=0; ii<*(chan->nToRead); ii++) { /* Have we reached the starting point yet? Only copy data after that point*/ /* *pSrc will be refilled by hardware after read */ if (ii < *(chan->ofst)) { /* We've not yet reached the starting point. Read pSrc, but only for purpose of getting to the next value */ #ifndef FAKE_DATA *(pSrc+srcOfst); #endif } else { #ifndef FAKE_DATA *(chan->data0+jj) = *(pSrc+srcOfst); #else /* Make fake data */ *(chan->data0+jj) = ii; #endif #ifdef PAD_PROCESS_DURING_READ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: scaling %hd by %hd to: %ld\n", (*(chan->data0+jj)), *(chan->scalar+ii), *(chan->proc0+jj))); #endif /* Examine last bit of counter to determine whether this is a Q (odd) or an I (even) */ isQ = ii & 1; if (isQ == 1) { /* Q values in the odd positions */ /* We are only using the upper 15 bits of the ADC. A '1' in the least significant bit indicates an overflow in Q */ isOverflowQ = *(chan->data0+jj) & 1; if (isOverflowQ) { if (overflowQCount < PAD_MAX_INT) { overflowQCount++; } else { overflowQCount = 0; numOverflowQRollovers++; } } else { /* Good data. Multiplying the signal by the scalar (combined sin/cos and normalization) vector. NB: the index of scalar must be offset by the points skipped over during read. Add to sum, inc num pts */ *(chan->proc0+jj) = *(chan->scalar+ii) * (*(chan->data0+jj)); *(chan->sumQ) += *(chan->proc0+jj); *(chan->numQ) += 1; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag,("padGenReadSignal: sumQ now: %lld\n", *(chan->sumQ))); #endif } } else { /* I values in the even positions */ /* We are only using the upper 15 bits of the ADC. A '1' in the least significant bit indicates an overflow in I*/ isOverflowI = *(chan->data0+jj) & 1; if (isOverflowI) { if (overflowICount < PAD_MAX_INT) { overflowICount++; } else { overflowICount = 0; numOverflowIRollovers++; } } else { *(chan->proc0+jj) = *(chan->scalar+ii) * (*(chan->data0+jj)); *(chan->sumI) += *(chan->proc0+jj); *(chan->numI) +=1; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag,("padGenReadSignal: sumI now: %lld\n", *(chan->sumI))); #endif } } #endif jj++; } } /* We've finished reading out the FIFO now. Do the operations on the full dataset, data0[]... */ /* Save the number of 16-bit words read */ *(chan->nRead) = jj; #ifdef PAD_PROCESS_DURING_READ /* Calculate the average Q and I */ if (*(chan->numQ) == 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: ch%d numQ is zero\n", mm)); #endif return OK; } else { *(chan->avgQ) = (short)(*(chan->sumQ)/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numQ))); #ifdef DATA_LOGGING if ((chan == chan3) && (!Qdone)) { if (logQindex == PAD_NUM_AVGS_TO_SAVE) { printf("padGenReadSignal: Q done\n"); Qdone = 1; } else { /* keep filling array */ padLogQavg[logQindex] = *(chan->avgQ); logQindex++; } } #endif } if (*(chan->numI) == 0) { DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: ch%d numI is zero\n", mm)); return OK; } else { *(chan->avgI) = (short)(*(chan->sumI)/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numI))); #ifdef DATA_LOGGING if ((chan == chan3) && (!Idone)) { if (logIindex == PAD_NUM_AVGS_TO_SAVE) { printf("padGenReadSignal: done\n"); Idone = 1; } else { /* keep filling array */ padLogIavg[logIindex] = *(chan->avgI); logIindex++; } } #endif } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: average Q:%ld\n", *(chan->avgQ))); DEBUGPRINT(DP_INFO, drvPadGenFlag, ("padGenReadSignal: average I:%ld\n", *(chan->avgI))); #endif #endif #ifdef USING_TIMER timers[timer_ii]=Read_timer(); timer_ii++; #endif } /* mm loop */ return OK; } /*============================================================================= Name: padIfqReadSignal Abs: Use this subroutine for channels where the signal is not coming in at 4 times the 25.5 MHz clock. Eg. for 119 MHz ref. Here, there is an I and a Q for EACH point of the waveform. Interlacing not possible. Working with padChannel[0..3] and average{I,Q}[0..3], calculate the standard deviation, padSigma{I,Q}[0..3] as: 1/n*sum((padChannel[0..3]-average{I,Q}[0..3])^2) Inp: What else must be set up first: padSampleOffset, padSampleSize What is set up after: padSigmaI[0..3], padSigmaQ[0..3] =============================================================================*/ int padIfqReadSignal(rf_channel_t* chan0, rf_channel_t* chan1, rf_channel_t* chan2, rf_channel_t* chan3) { int ii; /* absolute position in the FIFO */ int jj; /* the (ii - padSampleOffset)-th position in FIFO */ int mm; /* loop variable for all channels */ #ifndef FAKE_DATA volatile short *pSrc; /* MUST be volatile to avoid read-only *pSrc line below from being optimized away by the compiler AND for the padChanneli[jj] = *pSrc from being optimized to be assigned */ #endif int srcOfst = 0; #ifdef PAD_PROCESS_DURING_READ int isOverflow; long long diff; long long runningAverageI; long long runningAverageQ; #endif rf_channel_t* chan = NULL; /* generic channel used in loop of all channels */ #ifndef FAKE_DATA pSrc = csar1_actual; #endif /* Fills data0, calculates avgI and avgQ for all four channels */ for (mm=0; mmnToRead) = *(chan->ofst) + *(chan->sz); #ifndef FAKE_DATA #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: initiating chip select read at 0x%08x for %d words\n", (unsigned int) csar1_actual, *(chan->nToRead))); #endif #else printf("padIfqReadSignal: faking channel %d data for %d words\n", mm, *(chan->sz)); #endif /* Here are the guts of the read. This code was timed and found to take 250 ns to read one short */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: guts of read for channel %d\n",mm)); #endif jj = 0; *(chan->sumQ) = 0; *(chan->sumI) = 0; *(chan->numQ) = 0; *(chan->numI) = 0; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: initially sumQ is %lld\n", *(chan->sumQ))); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: initially numQ is %d\n", *(chan->numQ))); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: initially sumI is %lld\n", *(chan->sumI))); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: initially numI is %d\n", *(chan->numI))); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: srcOfst is %d\n", srcOfst)); #endif #ifndef FAKE_DATA /* Skip first three clock cycles to allow FIFO to fill */ *(pSrc+srcOfst); *(pSrc+srcOfst); *(pSrc+srcOfst); #endif for (ii=0; ii<*(chan->nToRead); ii++) { /* Only copy data after the offset to region of interest */ /* *pSrc will be refilled by hardware after read */ if (ii < *(chan->ofst)) { /* We've not yet reached the starting point. Read pSrc, but only for purpose of getting to the next value */ #ifndef FAKE_DATA *(pSrc+srcOfst); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: read one short\n")); #endif #endif } else { #ifndef FAKE_DATA *(chan->data0+jj) = *(pSrc+srcOfst); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: stored one short, %hd, in data0\n", *(chan->data0+jj))); #endif #else /* Make fake data */ *(chan->data0+jj) = ii; #endif #ifdef PAD_PROCESS_DURING_READ /* Check for data overflow in this channel's raw data. Since there is a value of I and Q for each jj, increment counters for both types of overflows. We are only using the upper 15 bits of the ADC. A '1' in the least significant bit indicates an overflow */ isOverflow = *(chan->data0+jj) & 1; if (isOverflow) { if (overflowQCount < PAD_MAX_INT) { overflowQCount++; overflowICount++; } else { overflowQCount = 0; numOverflowQRollovers++; overflowICount = 0; numOverflowIRollovers++; } } else { *(chan->procQ+jj) = (*(chan->scalarQ+ii) * (*(chan->data0+jj))); *(chan->sumQ) += *(chan->procQ+jj); *(chan->numQ) += 1; *(chan->procI+jj) = (*(chan->scalarI+ii) * (*(chan->data0+jj))); *(chan->sumI) += *(chan->procI+jj); *(chan->numI) +=1; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal %d: scaling %hd by %hd to processed Q: %ld\n", ii, *(chan->data0+jj), *(chan->scalarQ+ii), *(chan->procQ+jj))); DEBUGPRINT(DP_INFO, drvPadIfqFlag,("padIfqReadSignal: sumQ now: %lld\n", *(chan->sumQ))); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal %d: scaling %hd by %hd to processed I: %ld\n", ii, *(chan->data0+jj), *(chan->scalarI+ii), *(chan->procI+jj))); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: sumI now: %lld\n", *(chan->sumI))); #endif } #endif jj++; } } /* We've finished reading out the FIFO now. Do the operations on the full dataset, data0[]... */ /* Save the number of 16-bit words read */ *(chan->nRead) = jj; #ifdef PAD_PROCESS_DURING_READ /* Calculate the average Q and I */ if (*(chan->numQ) == 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: numQ is zero\n")); #endif return OK; } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: avgQ is %lld/%hd/%d\n",*(chan->sumQ),PAD_SCALED_DATA_NORMALIZATION,*(chan->numQ))); #endif *(chan->avgQ) = (short) (*(chan->sumQ)/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numQ))); #ifdef DATA_LOGGING if ((chan == chan3) && (!Qdone)) { if (logQindex == PAD_NUM_AVGS_TO_SAVE) { printf("padIfqReadSignal: Q done\n"); Qdone = 1; } else { /* keep filling array */ padLogQavg[logQindex] = *(chan->avgQ); logQindex++; } } #endif } if (*(chan->numI) == 0) { DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: numI is zero\n")); return OK; } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: avgI is %lld/%hd/%d\n",*(chan->sumI),PAD_SCALED_DATA_NORMALIZATION,*(chan->numI))); #endif *(chan->avgI) = (short)(*(chan->sumI)/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numI))); #ifdef DATA_LOGGING if ((chan == chan3) && (!Idone)) { if (logIindex == PAD_NUM_AVGS_TO_SAVE) { printf("padIfqReadSignal: done\n"); Idone = 1; } else { /* keep filling array */ padLogIavg[logIindex] = *(chan->avgI); logIindex++; } } #endif } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: average Q:%ld\n", *(chan->avgQ))); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: average I:%ld\n", *(chan->avgI))); #endif #endif #ifdef USING_TIMER timers[timer_ii]=Read_timer(); timer_ii++; #endif } /* first mm loop */ /* Loop through the data again, summing the squares of the difference between each data point and its average */ for (mm=0; mmsumSqQ = 0; chan->sumSqI = 0; #ifdef PAD_PROCESS_DURING_READ for (ii=0; ii<*(chan->nRead); ii=ii+6) { runningAverageQ = *(chan->procQ+ii); runningAverageQ += *(chan->procQ+ii+1); runningAverageQ += *(chan->procQ+ii+2); runningAverageQ += *(chan->procQ+ii+3); runningAverageQ += *(chan->procQ+ii+4); runningAverageQ += *(chan->procQ+ii+5); runningAverageQ /= 6; runningAverageQ /= PAD_SCALED_DATA_NORMALIZATION; diff = runningAverageQ-(*(chan->avgQ)); chan->sumSqQ += (diff * diff); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadRefFlag, ("ch%d: Q%d: %ld Q%d: %ld Q%d: %ld Q%d: %ld Q%d: %ld Q%d: %ld avgQ: %ld\n", mm, ii, *(chan->procQ+ii), ii+1, *(chan->procQ+ii+1), ii+2, *(chan->procQ+ii+2), ii+3, *(chan->procQ+ii+3), ii+4, *(chan->procQ+ii+4), ii+5, *(chan->procQ+ii+5), (*(chan->procQ+ii) + *(chan->procQ+ii+1) + *(chan->procQ+ii+2) + *(chan->procQ+ii+3 + *(chan->procQ+ii+4 + *(chan->procQ+ii+5)/6))))); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("%d: avg of two Qs/normalzn: %lld - Qavg: %ld = %lld. diff squared is %lld ", ii, runningAverageQ,*(chan->avgQ), diff, (diff * diff))); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("and sumSqQ is: %lld\n",chan->sumSqQ)); #endif runningAverageI = *(chan->procI+ii); runningAverageI += *(chan->procI+ii+1); runningAverageI += *(chan->procI+ii+2); runningAverageI += *(chan->procI+ii+3); runningAverageI += *(chan->procI+ii+4); runningAverageI += *(chan->procI+ii+5); runningAverageI /= 6; runningAverageI /= PAD_SCALED_DATA_NORMALIZATION; diff = runningAverageI-(*(chan->avgI)); chan->sumSqI += (diff * diff); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadRefFlag, ("ch%d: I%d: %ld I%d: %ld I%d: %ld I%d: %ld I%d: %ld I%d: %ld avgI: %ld\n", mm, ii, *(chan->procI+ii), ii+1, *(chan->procI+ii+1), ii+2, *(chan->procI+ii+2), ii+3, *(chan->procI+ii+3), ii+4, *(chan->procI+ii+4), ii+5, *(chan->procI+ii+5), (*(chan->procI+ii) + *(chan->procI+ii+1) + *(chan->procI+ii+2) + *(chan->procI+ii+3 + *(chan->procI+ii+4 + *(chan->procI+ii+5)/6))))); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("%d: avg of two Is/normalzn: %lld - Iavg: %ld = %lld. diff squared is %lld ", ii, runningAverageI,*(chan->avgI), diff, (diff * diff))); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("and sumSqI is: %lld\n",chan->sumSqI)); #endif } /* CHECK: N (padSigma) vs N-1 (s) */ if (*(chan->numI) == 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: numI is zero\n")); #endif return OK; } else { chan->sigmaSqI = (chan->sumSqI)/(*(chan->numI)/6); } if (*(chan->numQ) == 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: numQ is zero\n")); #endif return OK; } else { chan->sigmaSqQ = (chan->sumSqQ)/(*(chan->numQ)/6); } /* NO SQRT IS AVAIL ON CF, SO SEND SQUARED TERM TO VME FOR THIS */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: sum of squares for Q:%lld\n", chan->sumSqI)); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: sum of squares for I:%lld\n", chan->sumSqQ)); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: sigma squared for Q:%ld\n", chan->sigmaSqI)); DEBUGPRINT(DP_INFO, drvPadIfqFlag, ("padIfqReadSignal: sigma squared for I:%ld\n", chan->sigmaSqQ)); #endif #endif #ifdef USING_TIMER timers[timer_ii]=Read_timer(); timer_ii++; #endif } return OK; } /*============================================================================= Name: padRefReadSignal Abs: Call padGenReadSignal from within here to fill padChannel[0..3] arrays with data AND calculate the average I and Q for each channel. Working with padChannel[0..3] and average{I,Q}[0..3], calculate the standard deviation, padSigma{I,Q}[0..3] as: 1/n*sum((padChannel[0..3]-average{i,Q}[0..3])^2) Inp: What else must be set up first: padSampleOffset, padSampleSize What is set up after: padSigmaI[0..3], padSigmaQ[0..3] =============================================================================*/ int padRefReadSignal(rf_channel_t* chan0, rf_channel_t* chan1, rf_channel_t* chan2, rf_channel_t* chan3) { int rc; int ii; /* position in sample */ int mm; /* loop variable for all channels */ #ifndef FAKE_DATA volatile short *pSrc; /* MUST be volatile to avoid read-only *pSrc line below from being optimized away by the compiler AND for the padChanneli[jj] = *pSrc from being optimized to be assigned */ #endif #ifdef PAD_PROCESS_DURING_READ long long diff; long long runningAverageI; long long runningAverageQ; #endif rf_channel_t* chan = NULL; /* generic channel used in loop of all channels */ int srcOfst = 0; #ifndef FAKE_DATA pSrc = csar1_actual; #endif /* 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; /* 11/17/2008 NO NO NO Skip first three clock cycles to allow FIFO to fill THIS IS EXTRA 3 READS. Calling GenRead above already does this #ifndef FAKE_DATA *(pSrc+srcOfst); *(pSrc+srcOfst); *(pSrc+srcOfst); #endif */ #ifdef PAD_PROCESS_DURING_READ for (ii=0; ii<(*(chan->nRead)); ii=ii+4) { runningAverageI = (*(chan->proc0+ii) + *(chan->proc0+ii+2))/2/PAD_SCALED_DATA_NORMALIZATION; diff = runningAverageI-(*(chan->avgI)); chan->sumSqI += (diff * diff); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadRefFlag, ("ch%d: I%d: %ld I%d: %ld avgI: %ld\n", mm, ii, *(chan->proc0+ii), ii+2, *(chan->proc0+ii+2), (*(chan->proc0+ii) + *(chan->proc0+ii+2))/2)); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("%d: avg of two Is/normalzn: %lld - Iavg: %ld = %lld. diff squared is %lld ", ii, runningAverageI,*(chan->avgI), diff, (diff * diff))); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("and sumSqI is: %lld\n",chan->sumSqI)); #endif runningAverageQ = (*(chan->proc0+ii+1) + *(chan->proc0+ii+3))/2/PAD_SCALED_DATA_NORMALIZATION; diff = runningAverageQ-(*(chan->avgQ)); chan->sumSqQ += (diff * diff); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadRefFlag, ("ch%d: Q%d: %ld Q%d: %ld avgQ: %ld\n", mm, ii+1, *(chan->proc0+ii+1), ii+3, *(chan->proc0+ii+3), (*(chan->proc0+ii+1) + *(chan->proc0+ii+3))/2)); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("%d: avg of two Qs/normalzn: %lld - Qavg: %ld = %lld. diff squared is %lld ", ii, runningAverageQ,*(chan->avgQ), diff, (diff * diff))); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("and sumSqQ is: %lld\n",chan->sumSqQ)); #endif } /* CHECK: N (padSigma) vs N-1 (s) */ if (*(chan->numI) == 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadRefFlag, ("padRefReadSignal: ch%d numI is zero\n", mm)); #endif return OK; } else { chan->sigmaSqI = (long)((chan->sumSqI)/(*(chan->numI))/4); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("padRefReadSignal: sigma squared for I:%lu\n", chan->sigmaSqQ)); } if (*(chan->numQ) == 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadRefFlag, ("padRefReadSignal: ch%d numQ is zero\n", mm)); #endif return OK; } else { chan->sigmaSqQ = (long)((chan->sumSqQ)/(*(chan->numQ))/4); DEBUGPRINT(DP_INFO, drvPadRefFlag, ("padRefReadSignal: sigma squared for Q:%lu\n", chan->sigmaSqI)); } /* NO SQRT IS AVAIL ON CF, SO SEND SQUARED TERM TO VME FOR THIS */ #endif #ifdef USING_TIMER timers[timer_ii]=Read_timer(); timer_ii++; #endif } return OK; } /*============================================================================= Name: padSimpReadSignal Inp: Takes up to four rf_channel structures as input. Pass NULL ptrs as placeholders where you do NOT pass a structure. For eg., if you want to use this algorithm on channel '2' only, then call this routine with the signature: padSimpReadSignal(NULL, NULL, pChannels[2], NULL); What else must be set up first: Abs: What is set up after: =============================================================================*/ int padSimpReadSignal(rf_channel_t* chan0, rf_channel_t* chan1, rf_channel_t* chan2, rf_channel_t* chan3) { int ii; /* abs pos in the FIFO */ int jj; /* (ii-*ofst)th position in FIFO */ int mm = 0; /* channel */ #ifndef FAKE_DATA volatile short *pSrc; /* MUST be volatile to avoid read-only *pSrc line below from being optimized away by the compiler AND for the padChanneli[jj] = *pSrc from being optimized to be assigned */ #endif #ifdef PAD_SIMPLE_PROCESSING int all_state=NORMAL; /* check State for all channels */ #endif int srcOfst = 0; rf_channel_t* chan = NULL; int isOverflow; #ifndef FAKE_DATA pSrc = csar1_actual; #endif for (mm=0; mmnToRead))); #else DEBUGPRINT(DP_INFO, drvPadSimpFlag, ("padSimpReadSignal: faking channel %d data for %d words\n", mm, *(chan->sz))); #endif #endif *(chan->nToRead) = *(chan->ofst) + *(chan->sz); #ifdef PAD_PROCESS_DURING_READ chan->sumS = 0; chan->numS = 0; /* CHECK: set other, non-used vars in struct to zero? */ #endif #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadSimpFlag, ("padSimpReadSignal: guts of read for ch%d\n",mm)); #endif #ifndef FAKE_DATA /* Skip first three clock cycles to allow FIFO to fill */ *(pSrc+srcOfst); *(pSrc+srcOfst); *(pSrc+srcOfst); #endif jj=0; for (ii=0; ii<*(chan->nToRead); ii++) { /* Have we reached the starting point yet? Only copy data after that point*/ /* *pSrc will be refilled by hardware after read */ if (ii < *(chan->ofst)) { /* We've not yet reached the starting point. Read pSrc, but only for purpose of clearing chip select */ #ifndef FAKE_DATA *(pSrc+srcOfst); #endif } else { #ifndef FAKE_DATA *(chan->data0+jj) = *(pSrc+srcOfst); #else /* Make fake data */ printf("writing %d into addr 0x%08x (ch[%d]->data0[%d])\n",ii,(unsigned int)(chan->data0+jj),mm,jj); *(chan->data0+jj) = ii; #endif #ifdef PAD_PROCESS_DURING_READ /* New 6/7/7... */ isOverflow = *(chan->data0+jj) & 1; if (isOverflow) { if (overflowSCount < PAD_MAX_INT) { overflowSCount++; } else { overflowQCount = 0; numOverflowSRollovers++; } } else { /*... end New 6/7/7 */ /* Good data. Start by multiplying the signal by the constant */ *(chan->proc0+jj) = (*(chan->scalar)) * (*(chan->data0+jj)); chan->sumS += *(chan->proc0+jj); chan->numS += 1; } /* New 6/7/7 */ #endif jj++; } } /* We've finished reading out the FIFO now. Do the operations on the full dataset, padChanneli[]... */ /* Save the number of 16-bit words read */ *(chan->nRead) = jj; /* Calculate the average of the sample S */ if (chan->numS == 0) { DEBUGPRINT(DP_INFO, drvPadFlag, ("padKlyReadSignal: Channel %d: num pts is zero.\n", mm)); return OK; } else { chan->avgS = chan->sumS/chan->numS; } #if defined PAD_SIMPLE_PROCESSING /* scondam: 01.28.09: This functions processes the raw data. Processing here means multiplying by a scalar value. Default value of this scalar is 1.*/ long sum = 0; int chan_num = mm; /* keep filling array until logXindex rollsovers */ chan->padLogAvg[chan->logindex++] = chan->avgS; if (chan->logindex >= chan->plsavg) { /* index rollover */ chan->logindex = 0; } for (ii=0; iiplsavg; ii++) {sum += chan->padLogAvg[ii];} if (chan->plsavg > 0) { chan->avgOut = sum / chan->plsavg; } /* calculate higher and lower thresholds based on the last updated average value */ /* chan->avgOut will be updated once every chan->plsavg pulses */ /* take the absolute value - ignore the sign - and calculate the threshold in % */ long thresh = labs(chan->avgOut) * (chan->threshold / 100.0); /* now compare the current avg to seen if it is within the thresholds */ if ((chan->avgS <= (chan->avgOut + thresh)) && (chan->avgS >= (chan->avgOut - thresh))) { /* chan->avgS within accepted range */ chan->state = NORMAL; /* channel state is set to TRUE or NORMAL */ } else { /* chan->avgS not within accepted range */ /* while rf power is ramping up, do not trip until avg reaches a stable user-settable pedestal value */ #if defined NLCTA /* scondam: 15-Jul-11 Change 'chan->avgS not within accepted range' to chan->avgOut not within accepted range */ /* while rf power is ramping up, do not trip until avg reaches a stable user-settable pedestal value */ if (chan->avgOut > chan->avgOut_ped) { /* fault state only if avg is above a user-settable pedestal value */ #endif chan->state = FAULT; /* channel state is set to FALSE or FAULT */ #if defined NLCTA } else { chan->state = NORMAL; /* channel state is set to TRUE or NORMAL */ } #endif } if (chan->state == FAULT) { fault_chan = chan_num; fst[chan_num]->ofst = *(chan->ofst); fst[chan_num]->sumS = *(chan->sz); fst[chan_num]->nToRead = *(chan->nToRead); fst[chan_num]->nRead = *(chan->nRead); fst[chan_num]->sumS = chan->sumS; fst[chan_num]->numS = chan->numS; fst[chan_num]->avgS = chan->avgS; fst[chan_num]->avgOut = chan->avgOut; fst[chan_num]->thresh = thresh; fst[chan_num]->ulevel = (chan->avgOut + thresh); fst[chan_num]->llevel = (chan->avgOut - thresh); fst[chan_num]->state = chan->state; fst[chan_num]->faults++; /* for ( ii = 0; ii < padGetSampleSize(chan_num,0); ii++ ) { *(fst[chan_num]->data+ii) = *(chan->data0+ii);} */ /* padDumpFaultCountToFile(); */ } if (chan->state == FAULT) { /* even if one channel is faulty, set OUT line to FAULT */ all_state = FAULT; /* setPTA0ToLow(); */ /* OUT line is set to LOW */ *gptport = 0x0; State = FAULT; /* even if one channel is in FALSE, then State will become FAULT */ } #endif } /* mm loop */ #ifdef PAD_SIMPLE_PROCESSING if (all_state == NORMAL) { /* if all channels are NORMAL, then OUT line stays high */ /* setPTA0ToHigh(); */ /* OUT line is set to HIGH */ *gptport = 0x1; State = NORMAL; } /* scondam: 11-Aug-2011: Keep track of total number of system faults since boot per NLCTA's request */ else { ++faultCount; } #endif return OK; } /*============================================================================= Name: padCalReadSignal Abs: What makes this read unique is that no processing is done; i.e., there is no correction factor multiplied in to the each data point What else must be set up first:... What is set up after: if in PAD_CALIBRATING operational mode:... NB: Since this calibration channel is ALWAYS in channel 0, there is no need for a srcOfst term to be added to pSrc here 9/29/2006 TO DO: Since every PAD now can do calibration in all of its channels, this routine no longer needs to pass any args in. Just make a loop through all 4 padCalChannels where chan gets set to padCalChannels[mm]; for (mm=0; mmnToRead = chan->ofst + chan->sz; #ifndef FAKE_DATA printf("padCalReadSignal: initiating chip select read at 0x%08x for %d words\n", (unsigned int) csar1_actual, chan->nToRead); #else printf("padCalReadSignal: faking channel %d data for %d words\n", mm, chan->sz); #endif #ifndef FAKE_DATA /* Skip first three clock cycles to allow FIFO to fill */ *(pSrc+srcOfst); *(pSrc+srcOfst); *(pSrc+srcOfst); #endif /* Here are the guts of the read. This code was timed and found to take 250 ns to read one short */ printf("padCalReadSignal: guts of read for channel %d\n",mm); jj = 0; for (ii=0; iinToRead; ii++) { /* Have we reached the starting point yet? Only copy data after that point*/ /* *pSrc will be refilled by hardware after read */ if (ii < chan->ofst) { /* We've not yet reached the starting point. Read pSrc, but only for purpose of getting to the next value */ #ifndef FAKE_DATA *(pSrc+srcOfst); #endif } else { #ifndef FAKE_DATA *(chan->data0+jj) = *(pSrc+srcOfst); #else /* Make fake data */ *(chan->data0+jj) = ii; printf("padCalReadSignal: writing %d into addr 0x%08x (ch[%d]->data0[%d])\n",ii,(unsigned int)chan->data0+jj,mm,jj); #endif jj++; } } /* We've finished reading out the FIFO now */ /* Save the number of 16-bit words read */ chan->nRead = jj; printf("padCalReadSignal: number of words read is: %d\n", chan->nRead); } /* mm loop */ return OK; } #if defined (PADLAN9118_BPH) || defined (PADLAN9118_BPH3) /*============================================================================= Goal: find the avg I and avg Q for two samples in the FIFO =============================================================================*/ int padBphReadSignal(rf_channel_t* chan0, rf_channel_t* chan1, rf_channel_t* chan2, rf_channel_t* chan3) { int ii; /* abs pos in the FIFO */ int jj; /* (ii-*ofst)th position in FIFO */ int kk; /* (ii-*(ofst+1))th position in FIFO */ int mm; #ifndef FAKE_DATA volatile short *pSrc; /* MUST be volatile to avoid read-only *pSrc line below from being optimized away by the compiler AND for the padChanneli[jj] = *pSrc from being optimized to be assigned */ #endif int srcOfst = 0; #ifdef PAD_PROCESS_DURING_READ int isQ; /* knowing order of data in FIF0, [I,Q,I,Q], can find Q where counter is an odd number */ int isOverflowI; int isOverflowQ; #endif int indexAfterSample0; /* use to determine relative distance to sample 1 */ rf_channel_t* chan = NULL; /* Loop through the data again, summing the squares of the difference between each data point and its average */ for (mm=0; mmsumI) = 0; *(chan->sumQ) = 0; *((chan->sumI)+1) = 0; *((chan->sumQ)+1) = 0; *(chan->numI) = 0; *(chan->numQ) = 0; *((chan->numI)+1) = 0; *((chan->numQ)+1) = 0; *(chan->nToRead) = 0; *((chan->nToRead)+1) = 0; *(chan->nRead) = 0; *((chan->nRead)+1) = 0; /* 1. Find the average I and average Q of the first sample. */ /* NB: the number read is the SUM Of the Is and the Qs for one channel */ /* 1.1 Check range of first sample */ if (*(chan->ofst) + *(chan->sz) > PAD_FIFO_SIZE) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadBphFlag, ("padBphReadSignal: s0 surpasses end of FIFO! %d + %d > %d\n", *(chan->ofst), *(chan->sz), PAD_FIFO_SIZE)); #endif /* return is the wrong action here. It means that the remaining channels are skipped over. return ERROR; Instead, use "continue", so that executions goes to the next channel */ continue; } *(chan->nToRead) = *(chan->ofst) + *(chan->sz); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("\nSample 0\n")); #endif #ifndef FAKE_DATA pSrc = csar1_actual; /* Skip first three clock cycles to allow FIFO to fill */ *(pSrc+srcOfst); *(pSrc+srcOfst); *(pSrc+srcOfst); #endif /* 1.2 Read first sample */ for (ii=0; ii<*(chan->nToRead); ii++) { if (ii < *(chan->ofst)) { /* 1.2.1 Read up to start first sample */ #ifndef FAKE_DATA *(pSrc+srcOfst); #endif } else { /* 1.2.2 Read and store first sample */ #ifndef FAKE_DATA *(chan->data0+jj) = *(pSrc+srcOfst); #else /* FAKE THE DATA */ *(chan->data0+jj) = (short) ii; #endif #ifdef PAD_PROCESS_DURING_READ /* Start by multiplying the signal by the scalar (combined sin/cos and normalization) vector. NB: the index of scalar must be offset by the points skipped over during read */ isQ = ii & 1; /* examine last bit to determine odd/even */ if (isQ == 1) { /* Q values in the odd positions */ isOverflowQ = *(chan->data0+jj) & 1; if (isOverflowQ) { if (overflowQCount < PAD_MAX_INT) { overflowQCount++; } else { overflowQCount = 0; numOverflowQRollovers++; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s0 overflow in Q \n", mm)); #endif } else { /* Good data. */ *(chan->proc0+jj) = *(chan->scalar+ii) * (*(chan->data0+jj)); *(chan->sumQ) += *(chan->proc0+jj); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s0 ch%d: scale %hd by %hd to Q[%d]: %ld\n", mm, (*(chan->data0+jj)), *(chan->scalar+ii), *(chan->numQ), *(chan->proc0+jj))); DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s0 ch%d: sumQ now: %lld\n", mm, *(chan->sumQ))); #endif *(chan->numQ) += 1; } } else { /* I values in the even positions */ isOverflowI = *(chan->data0+jj) & 1; if (isOverflowI) { if (overflowICount < PAD_MAX_INT) { overflowICount++; } else { overflowICount = 0; numOverflowIRollovers++; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s0 overflow in I \n", mm)); #endif } else { /* Good data. */ *(chan->proc0+jj) = *(chan->scalar+ii) * (*(chan->data0+jj)); *(chan->sumI) += *(chan->proc0+jj); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s0 ch%d: scale %hd by %hd to I[%d]: %ld\n", mm, (*(chan->data0+jj)), *(chan->scalar+ii), *(chan->numI), *(chan->proc0+jj))); DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s0 ch%d: sumI now: %lld\n", mm, *(chan->sumI))); #endif *(chan->numI) += 1; } } #endif jj++; } } /* Save the number of 16-bit words read into sample 1 */ *(chan->nRead) = jj; /* Save the current position in the FIFO */ indexAfterSample0 = ii; /* Calculate first sample's average I and Q */ /* I: */ if (*(chan->numI) <= 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s0 numI is zero. Can't calc avgI\n", mm)); #endif /* return is the wrong action here. It means that the remaining channels are skipped over. return OK; Instead, use "continue", so that execution goes to the next channel */ continue; } *(chan->avgI) = ((*(chan->sumI))/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numI))); /* Q: */ if (*(chan->numQ) <= 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s0 numQ is zero. Can't calc avgQ\n", mm)); #endif /* return is the wrong action here. It means that the remaining channels are skipped over. return OK; Instead, use "continue", so that execution goes to the next channel */ continue; } *(chan->avgQ) = ((*(chan->sumQ))/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numQ))); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("\npadBphReadSignal: ch%d sample 0\n", mm)); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" sumQ %lld\n", *(chan->sumQ))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" sumI %lld\n", *(chan->sumI))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" numQ %d\n", *(chan->numQ))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" numI %d\n", *(chan->numI))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" avgI %d\n", (int) *(chan->avgI))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" avgQ %d\n", (int) *(chan->avgQ))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" after s0, position in FIFO is %d\n", indexAfterSample0)); #endif /* 3. Find the average I and average Q of the second sample */ /* NB: the number read is the SUM Of the Is and the Qs */ /* 3.1 Check range of second sample */ if (*(chan->ofst+1) < indexAfterSample0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadBphFlag, ("padBphReadSignal: s1 starts before end of s0! %d < %d\n", *(chan->ofst+1), indexAfterSample0)); #endif /* return is the wrong action here. It means that the remaining channels are skipped over. return ERROR; Instead, use "continue", so that execution goes to the next channel */ continue; } if (*(chan->ofst+1) + *(chan->sz+1) > PAD_FIFO_SIZE) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadBphFlag, ("padBphReadSignal: s1 surpasses end of FIFO! %d + %d > %d\n", *(chan->ofst+1), *(chan->sz+1), PAD_FIFO_SIZE)); #endif /* return is the wrong action here. It means that the remaining channels are skipped over. return ERROR; Instead, use "continue", so that execution goes to the next channel */ continue; } *(chan->nRead+1) = *(chan->ofst+1) + *(chan->sz+1); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: already at %d, skipping next %d shorts to read %d\n", indexAfterSample0,*(chan->ofst+1)-indexAfterSample0, *(chan->sz+1))); #endif /* 3.2 Read sample 1 */ *(chan->nToRead+1) = *(chan->ofst+1) + *(chan->sz+1); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("\nSample 1\n")); #endif for (ii=indexAfterSample0; ii<*(chan->nToRead+1); ii++) { if (ii < *(chan->ofst+1)) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_DEBUG, drvPadBphFlag, ("skipping ii %d\n", ii)); #endif #ifndef FAKE_DATA *(pSrc+srcOfst); #endif } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_DEBUG, drvPadBphFlag, ("storing ii %d\n", ii)); #endif #ifndef FAKE_DATA *(chan->data1+kk) = *(pSrc+srcOfst); #else /* Make fake data */ *(chan->data1+kk) = (short) ii; #endif #ifdef PAD_PROCESS_DURING_READ isQ = ii & 1; /* examine last bit to determine odd/even */ if (isQ == 1) { /* Q values in the odd positions */ isOverflowQ = *(chan->data1+kk) & 1; if (isOverflowQ) { if (overflowQCount < PAD_MAX_INT) { overflowQCount++; } else { overflowQCount = 0; numOverflowQRollovers++; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s1 overflow in Q \n", mm)); #endif } else { /* Good data. */ *(chan->proc1+kk) = *(chan->scalar+ii)*(*(chan->data1+kk)); *(chan->sumQ+1) += *(chan->proc1+kk); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s1 ch%d: scale %hd by %hd to Q[%d]: %ld\n", mm, (*(chan->data1+kk)), *(chan->scalar+ii), *(chan->numQ+1), *(chan->proc1+kk))); DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s1 ch%d: sumQ now: %lld\n", mm, *(chan->sumQ+1))); #endif *(chan->numQ+1) += 1; } } else { /* We are only using the upper 15 bits of the ADC. A '1' in the least significant bit indicates an overflow in I*/ isOverflowI = *(chan->data1+kk) & 1; if (isOverflowI) { if (overflowICount < PAD_MAX_INT) { overflowICount++; } else { overflowICount = 0; numOverflowIRollovers++; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s1 overflow in I \n", mm)); #endif } else { /* Good data. */ *(chan->proc1+kk) = *(chan->scalar+ii)*(*(chan->data1+kk)); *(chan->sumI+1) += *(chan->proc1+kk); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s1 ch%d: scale %hd by %hd to Q[%d]: %ld\n", mm, (*(chan->data1+kk)), *(chan->scalar+ii), *(chan->numI+1), *(chan->proc1+kk))); DEBUGPRINT(DP_INFO, drvPadBphFlag, ("s1 ch%d: sumI now: %lld\n", mm, *(chan->sumI+1))); #endif *(chan->numI+1) += 1; } } #endif kk++; } } /* Save the number of 16-bit words read into sample 2 */ *(chan->nRead+1) = kk; #ifdef PAD_PROCESS_DURING_READ /* 4. Calculate second sample's average I and Q */ /* I: */ if (*(chan->numI+1)<=0) { #ifdef DEBUGPRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s1 numI is zero. Can't calc avgI\n", mm)); #endif /* return is the wrong action here. It means that the remaining channels are skipped over. return OK; Instead, use "continue", so that executions goes to the next channel */ continue; } *(chan->avgI+1) = (*(chan->sumI+1))/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numI+1)); /* Q: */ if (*(chan->numQ+1)<=0) { #ifdef DEBUGPRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("padBphReadSignal: ch%d s1 numQ is zero. Can't calc avgQ\n", mm)); #endif /* return is the wrong action here. It means that the remaining channels are skipped over. return OK; Instead, use "continue", so that executions goes to the next channel */ continue; } *(chan->avgQ+1) = (*(chan->sumQ+1))/PAD_SCALED_DATA_NORMALIZATION/(*(chan->numQ+1)); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadBphFlag, ("\npadBphReadSignal: ch%d sample 1\n", mm)); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" sumQ %lld\n", *(chan->sumQ+1))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" sumI %lld\n", *(chan->sumI+1))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" numQ %d\n", *(chan->numQ+1))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" numI %d\n", *(chan->numI+1))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" avgI %ld\n", *(chan->avgI+1))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" avgQ %ld\n", *(chan->avgQ+1))); DEBUGPRINT(DP_INFO, drvPadBphFlag, (" after sample 0, position in FIFO is %d\n", indexAfterSample0)); #endif #endif } /* mm loop */ return OK; } #endif /*============================================================================= Name: padDacqTask Abs: acquire and process the data upon receipt of event from ISR =============================================================================*/ int padDacqTask(void* arg) { struct timeval start, stop; int rc; int status=0; epicsEventWaitStatus waitStatus; padLan9118_t *ppadLan9118 = arg; PadDataKind kind = PadDataBpm; /* PadDataKind is an enum defined in drvLan9118/ padStream.h distinguishing beam data from cal data */ /* Do forever */ while (1) { /*epicsEventShow(ppadLan9118->data_ready, 1);*/ /* wait for the ISR to indicate that there is data waiting to be read */ waitStatus = epicsEventWait(ppadLan9118->data_ready); if (waitStatus == epicsEventWaitOK) { padRaiseIRQ5(); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("padDacqTask: wake-up via data_ready event\n")); #endif drvPadLan9118Acqs++; #ifdef USING_TIMER /* zero the timer and reset the loop var for this iteration */ Timer_initialize(); timer_ii = 0; #endif #ifdef PADLAN9118_GEN /* case of RFGun1,RFGun2,L0A,L0B,TCav,L1S,L2Tcav */ /* Average I, Q algoritm for channels 0-3 */ padGenReadSignal(&padCh0,&padCh1,&padCh2,&padCh3); /* Daly's original code */ /* new code to improve the real time performance gettimeofday(&start,NULL); padReadSignal_interlaced(&padCh0, 0); padReadSignal_interlaced(&padCh1, 1); padReadSignal_interlaced(&padCh2, 2); padReadSignal_interlaced(&padCh3, 3); gettimeofday(&stop,NULL); */ /* overflowQCount = (stop.tv_sec-start.tv_sec)*1000000+(stop.tv_usec-start.tv_usec); */ #endif #ifdef PADLAN9118_REF /* case of all RF Distribution where dig at 4x clock */ /* Average I, Q and standard deviation algoritm for channels 0,1,3 */ padRefReadSignal(&padCh0,&padCh1,NULL,&padCh3); /* Average I, Q and standard deviation algoritm for channel 2 since 119 and not 102 MHz with I and Q in their own array */ padIfqReadSignal(NULL,NULL,&padCh2,NULL); #endif #ifdef PADLAN9118_LSR /* case of laser */ /* Average I, Q and standard deviation algorithm for channel 0 */ /* 12/19/2006 now reading ch2+3 as IFQ since they are 119 and not 102 MHz */ #ifdef USING_TIMER timers[timer_ii]=Read_timer(); timer_ii++; #endif /* NEW 6/13/7 Do this for ch0 and 1 padGenReadSignal(&padCh0,&padCh1,NULL,NULL); */ /* 10/10/8 try this now that second eth set up */ /* Dayl's original code */ padRefReadSignal(&padCh0,NULL,NULL,NULL); padGenReadSignal(NULL,&padCh1,NULL,NULL); /* new code to improve the real time performance padReadSignal_interlaced(&padCh0, 0); padReadSignal_interlaced(&padCh1, 1); */ #ifdef USING_TIMER timers[timer_ii]=Read_timer(); timer_ii++; #endif padIfqReadSignal(NULL,NULL,&padCh2,&padCh3); #ifdef USING_TIMER timers[timer_ii]=Read_timer(); timer_ii++; /* This is how many valid timers there are for this alg, in positions starting at 0. Eg. if timer_ii++ was just incremented to 3, valid timers in 0,1,2. */ if (timers[timer_ii-1] > longest_time_at_done) { printf("new worst case: %d\n", timers[timer_ii-1]); padUpdateWorstCase(); } #endif #endif #ifdef PADLAN9118_BPH /* case of Beam Phase Cav1 and Beam Phase Cav2 */ /* Beam phasing cavity algorithm for channels 0 + 1 */ /* 12/19/2006 now Beam Phase Cav2 is in ch2+3 */ padBphReadSignal(&padCh0,&padCh1,&padCh2,&padCh3); #endif #ifdef PADLAN9118_BPH3 /* case of hybrid S24-8 Kly and Beam Phase Cav3 */ /* Beam phasing cavity algorithm for channel 3 */ padBphReadSignal(NULL, NULL, NULL, &padCh3); #endif #ifdef PADLAN9118_KLY /* case of RFGun Kly, L0A Kly, L0B Kly, TCav Kly, L1S Kly, L1X Kly, L2TCav Kly and L1X */ /* Average I, Q algoritm for channels 0,1 and 3 */ /* Simple average S for channel 2 */ padGenReadSignal(&padCh0,&padCh1,NULL,&padCh3); padSimpReadSignal(NULL,NULL,&padCh2,NULL); #endif /* Start of import from padThread */ padLowerIRQ5(); padRaiseIRQ5(); #ifndef NOSTREAM /* 6/26/8: Replace status = padStreamSend(getdata, kind, 0, 0); with this: TO DO NEXT: be able to toggle back and forth 8/5/8: there is a gotcha here: the way the listener (vme) is set up, all the streams it is listening to must be the same length, so this raw/processed idea is less attractive without changing that */ int processed = 1; if (processed) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118DacqFlag, ("padDacqTask: padStreamSend kind=%d\n", kind)); #endif /* status = padStreamSend(getProcessedData, kind, 0, 0); */ status = padStreamSend(getProcessedData, kind, 0); } else { /*status = padStreamSend(getRawData, kind, 0, 0); */ status = padStreamSend(getRawData, kind, 0); } #endif padLowerIRQ5(); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118DacqFlag, ("padDacqTask: trigger pin toggled\n")); #endif #ifdef PADLAN9118_BPH3 /* case of hybrid S24-8 Kly and Beam Phase Cav3 */ /* Now that streamed data has been sent out, do the slower, potentially larger channels now. This is the only way to accomodate BSA channel on klystron diagnostic PAD, for now */ /* Average I, Q algoritm for channels 0 and 1 */ /* Simple average S for channel 2 */ padGenReadSignal(&padCh0,&padCh1,NULL,NULL); padSimpReadSignal(NULL,NULL,&padCh2,NULL); #endif /* If we can't send streaming data then * 'ordinary' record support is engaged. */ if ( status ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118DacqFlag, ("padDacqTask: stream is not active\n")); #endif ppadLan9118->irq = 1; /* signal IRQ */ /* 6/26/8: why necess? */ updatingWfs++; #ifdef NOT_YET if ( epicsTimeEventDeviceTime == waveform_ps->tse ) { #ifdef BASE_HAS_NO_NTP_PATCH struct ntptimeval ntv; if ( 0 == ntp_gettime(&ntv) ) { #ifdef NTP_NANO epicsTimeFromTimespec(&waveform_ps->time, &ntv.time); #else epicsTimeFromTimeval(&waveform_ps->time, &ntv.time); #endif } else { #endif epicsTimeGetEvent(&waveform_ps->time, epicsTimeEventCurrentTime); } } #endif /* NOT_YET */ /* stream not active; process record TSS way: callbackRequestProcessCallback(&dpvt_ps->cb_s, priorityHigh, ppadLan9118->rec_p); KDK way: */ #ifdef SCAN_IO /* Process I/O Intr scan records registered to this callback */ scanIoRequest(pad_ioscanpvt); if (scanIoCount < PAD_MAX_INT) { scanIoCount++; } else { scanIoCount = 0; numScanIoRollovers++; } #endif } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118DacqFlag, ("padDacqTask: stream is active\n")); #endif /* 6/26/8: scan anyway? take this out of just this clause if too much */ #ifdef SCAN_IO /* Process I/O Intr scan records registered to this callback */ scanIoRequest(pad_ioscanpvt); if (scanIoCount < PAD_MAX_INT) { scanIoCount++; } else { scanIoCount = 0; numScanIoRollovers++; } #endif } /* 6/26/8 moved up from just before "End of import: Making this change makes it keep processing interrupts when streaming turned off by VME, which is what we want */ /* Clear interrupt here so we can detect a trigger overrun */ coldfEportFlagClr(ppadLan9118->irq_pin); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118DacqFlag, ("padDacqTask: interrupt cleared so we can detect a trigger overrun\n")); #endif coldfEportIntEnable(ppadLan9118->irq_pin); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118DacqFlag, ("padDacqTask: interrupts enabled\n")); #endif /* End of import from padThread */ } else { if (waitStatus == epicsEventWaitTimeout) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadLan9118DacqFlag, ("padDacqTask: timed out waiting for data to be available to read out of FIFO\n")); #endif return ERROR; } else { if (waitStatus == epicsEventWaitError) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadLan9118DacqFlag, ("padDacqTask: error waiting for data to be available to read out of FIFO\n")); #endif return ERROR; } } } } return rc; } /*============================================================================= Name: padLan9118Initialise Abs: Sets up interrupts and the chip select register: CSCR CSAR1 CSMR1. Sets up the event semaphore that ISR will post "data available" with. =============================================================================*/ int drvPadLan9118Initialise() { int rc; char sw_version[100]; #ifndef PADLAN9118_BPH int ii; #endif #ifdef DEBUG_PRINT drvPadLan9118Flag=DP_INFO; DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: hardware initialisation in progress...\n")); /* Write out the version of the software to the console log */ #ifdef PADLAN9118_GEN strcpy(sw_version, "PADLAN9118_GEN"); #endif #ifdef PADLAN9118_REF strcpy(sw_version, "PADLAN9118_REF"); #endif #ifdef PADLAN9118_LSR strcpy(sw_version, "PADLAN9118_LSR"); #endif #ifdef PADLAN9118_KLY strcpy(sw_version, "PADLAN9118_KLY"); #endif #ifdef PADLAN9118_BPH strcpy(sw_version, "PADLAN9118_BPH"); #endif #ifdef PADLAN9118_BPH3 strcpy(sw_version, "PADLAN9118_BPH3"); #endif DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: this version of the software was compiled for %s\n", sw_version)); #endif /* Fill a structure pointed to by ppadLan9118 with useful info */ ppadLan9118->irq_pin = IRQ_PIN; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: Set irq_pin to %d\n", IRQ_PIN)); #endif #ifdef OUT ppadLan9118->trig_pin = TRIG_PIN; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: Set trig_pin to %d\n", TRIG_PIN)); #endif #endif /* USED TO MEASURE XF TIME TO VME AND BACK: Make IRQ5 an output by turning on the 5th bit */ *epddr |= 32; /* Make an event that ISR will use to signal data acquisition task */ ppadLan9118->data_ready = epicsEventCreate(0); if ( ! ppadLan9118->data_ready ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadLan9118Flag, ("drvPadLan9118Initialise: Unable to create EPICS event\n")); return ERROR; #endif } /* Initialize streaming mode */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadLan9118Flag, ("drvPadLan9118Initialise: calling drvPadStreamInit()\n")); #endif if ( ! drvPadStreamInit(padLan9118)) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadLan9118Flag, ("drvPadLan9118Initialise: Warning - no streaming mode available\n")); #endif } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: streaming mode initialized\n")); #endif } #ifdef LATER /* Get this working so that can just use coldfUtils and rm a lot of lines Setup Chip Select #1 with flags set for : internal cycle termination and one wait state */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: set up chip select\n")); #endif coldfCsSetup(CS1, CSELECT_FLAG_AA | CSELECT_FLAG_WS(1)); #endif /* LATER */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: ensure interrupts disabled\n")); #endif coldfEportIntDisable_inl(ppadLan9118->irq_pin); /* Setup trigger: configure eport as output, pull it low. Forever. This is not a BPM app. We don't want to block triggers by a s/w trigger */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: set up trigger\n")); #endif /* 7/2/8: coldfEport routines not working. See high impedance value (2V) for this pin on scope. Not sure why. #define EPORT_OUTPUT 1 coldfEportSetup(ppadLan9118->trig_pin, EPORT_OUTPUT); coldfEportEpdrSet(ppadLan9118->trig_pin, 0); */ *epddr |= 2; *epdr &= ~2; /* Setup Interrupt: configure EPORT for rising edge interrupt; clear any pending IRQs */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: set up interrupt\n")); #endif coldfEportFlagClr(ppadLan9118->irq_pin); coldfEportSetup(ppadLan9118->irq_pin, IRQ_EDGE); /* Connect the interrupt vector to the interrupt service routine */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: set up interrupt vector and ISR\n")); #endif rc = devConnectInterruptVME(64 + ppadLan9118->irq_pin, padISR, ppadLan9118); if (rc != 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_FATAL, drvPadLan9118Flag, ("drvPadLan9118Initialise: unable to connect to intr vector to padISR. rc = %d\n", rc)); #endif return ERROR; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: intr vector, %x, connected to ISR\n", 64 + ppadLan9118->irq_pin)); #endif /* NOT SURE ABOUT THIS if ( dpvt_ps->orarm ) { arm(dpvt_ps, -1 == dpvt_ps->orarm); } */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: IRQ1 after: %d (isn't this 2? TODO)\n", *epddr & 2 )); #endif /* TAKE THIS OUT ONCE COLDFUTILS IS DOING THIS */ /* Set up the chip select register 1 */ *csar1 = 0x8000;/* bit 15: 1000 0000 0000 0000: bit 0 */ *cscr1 = 0x0580;/* bit 15: 0000 0101 1000 0000: bit 0 */ /* x one wait state */ /* x auto-acknowledge enable. Detemines the assertion of the internal transfer ack for accesses specified by the chip select address. */ /* x 16-bit port size. Valid data sampled and driven on D[31:16] */ /* NB: set csmr1 up LAST. It sets the chip select to valid. */ *csmr1 = 0x103; /* bit 31: 0000 0000 0000 0000 0000 0001 0000 0011: bit 0 */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: csar1 has addr 0x%08x and contents 0x%08x\n", (int) csar1, *csar1)); /* NB: Doing this next line actually triggers a READ of the chip select reg */ DEBUGPRINT(DP_INFO, drvPadFlag, ("drvPadLan9118Initialise: csar1_actual has addr 0x%08x and contents 0x%08x\n", (int) csar1_actual, *csar1_actual)); DEBUGPRINT(DP_INFO, drvPadFlag, ("drvPadLan9118Initialise: cscr1 has addr 0x%08x and contents 0x%08x\n", (int) cscr1, (short) *cscr1)); DEBUGPRINT(DP_INFO, drvPadFlag, ("drvPadLan9118Initialise: csmr1 has addr 0x%08x and contents 0x%08x\n", (int) csmr1, (unsigned int) *csmr1)); #endif #ifdef USING_QSPI /* Set up the queued serial port interface */ *pqspar = 0x7F; /* see ch. 26 of MCF5282UM.pdf for QSPI configuration desc. */ *qmr = 0xE140; /* setup for clock phase, ..., byte transfer */ /* last two digits are half the factor to divide system clk by so... 64MHz system clk/0x40*2 => 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 PADLAN9118_GEN /* Average I, Q algoritm for channels 0-3 */ for (ii=0; ii<4; ii++) { pChannels[ii]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[ii]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); } for (ii=4; ii<7; ii++) { pCalChannels[ii-4]->data0 = malloc(sizeof(short)*PAD_FIFO_SIZE); } #endif #ifdef PADLAN9118_REF /* Average I, Q and standard deviation algoritm for channels 0,1,3 */ /* Average I, Q and standard deviation algoritm for channel 2 since 119 and not 102 MHz with I and Q in their own array */ pChannels[0]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[0]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[1]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[1]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[2]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[2]->procI = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[2]->procQ = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[3]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[3]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); for (ii=4; ii<7; ii++) { pCalChannels[ii-4]->data0 = malloc(sizeof(short)*PAD_FIFO_SIZE); } #endif #ifdef PADLAN9118_LSR /* Average I, Q and standard deviation algoritm for channel 0 */ /* 12/19/2006 now reading ch2+3 as IFQ since they are 119 and not 102 MHz */ pChannels[0]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[0]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[1]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[1]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[2]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[2]->procI = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[2]->procQ = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[3]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[3]->procI = malloc(sizeof(long)*PAD_SAMPLE_SIZE); pChannels[3]->procQ = malloc(sizeof(long)*PAD_SAMPLE_SIZE); for (ii=4; ii<7; ii++) { pCalChannels[ii-4]->data0 = malloc(sizeof(short)*PAD_FIFO_SIZE); } #endif #ifdef PAD_SIMPLE_PROCESSING /* scondam: 01.28.09: */ /* Set up the General Purpose Timer Modules (GPTA) */ /* GPT Port Data Direction Register (GPTDDR) */ /* Setting Bits 0 to 3 at Byte address IPSBAR + 0x1A001E will cause the corresponding PTA0 to PTA3 to become outputs. */ /* Write 0x01 to IPSBAR + 0x1A001E to set PTA0 to an output pin. */ setPTA0ToOutput(); /* *gptddr = 0x01; Chapter 20, Section 20.5.19 */ /* GPT Port Data Register (GPTPORT) */ /* Writing to bits 0 to 3 of byte address IPSBAR+0x1A001D will set corresponding output pins PTA0 to PTA3 to the value written. */ /* Set bit high write 0x01 to IPSBAR+0x1A001D */ /* Set bit low write 0x00 to IPSBAR+0x1A001D */ setPTA0ToLow(); /* *gptport = 0x01; Chapter 20, Section 20.5.18 */ /* setPTA0ToHigh(); */ #endif #ifdef PADLAN9118_KLY /* Average I, Q algoritm for channels 0,1 and 3 */ /* Simple average S for channel 2 */ for (ii=0; ii<4; ii++) { pChannels[ii]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[ii]->proc0 = malloc(sizeof(long)*PAD_SAMPLE_SIZE); #ifdef PAD_SIMPLE_PROCESSING pChannels[ii]->plsavg = 0; pChannels[ii]->avgOut = 0; pChannels[ii]->threshold = 0; /* initially set threshold to 20% */ pChannels[ii]->state = 0; pChannels[ii]->scalar = malloc(sizeof(short)); *(pChannels[ii]->scalar) = 0; /* scalar value of 100% means no scaling factor applied */ pChannels[ii]->logindex = 0; fst[ii]->sz = 0; fst[ii]->ofst = 0; fst[ii]->sumS = 0; fst[ii]->nRead = 0; fst[ii]->nToRead = 0; fst[ii]->sumS = 0; fst[ii]->numS = 0; fst[ii]->avgS = 0; fst[ii]->avgOut = 0; fst[ii]->thresh = 0; fst[ii]->ulevel = 0; fst[ii]->llevel = 0; fst[ii]->state = 0; fst[ii]->faults = 0; #endif } for (ii=4; ii<7; ii++) { pCalChannels[ii-4]->data0 = malloc(sizeof(short)*PAD_FIFO_SIZE); } #endif #ifdef PADLAN9118_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); /* 2/28/8: is there a reason that ch4-7 aren't malloced for BPH or is this just an oversight? */ #endif #ifdef PADLAN9118_BPH3 /* Average I, Q algoritm for channels 0,1 and 3 */ /* Simple average S for channel 2 */ /* Beam Phase Cav3 for channel 3 */ for (ii=0; ii<3; ii++) { pChannels[ii]->data0 = malloc(sizeof(short)*PAD_SAMPLE_SIZE); pChannels[ii]->proc0 = 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); for (ii=4; ii<7; ii++) { pCalChannels[ii-4]->data0 = malloc(sizeof(short)*PAD_FIFO_SIZE); } #endif /* Initialize our watchdog timer queue */ padWdTimerQ=epicsTimerQueueAllocate( 1, /* ok to share queue */ epicsThreadPriorityLow); if (padWdTimerQ==0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadFlag, ("drvPadLan9118Initialise: error initialising watchdog timer queue\n")); #endif return ERROR; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("drvPadLan9118Initialise: watchdog timer queue initialised\n")); #endif /* Set this up if there are records scanned on "I/O Intr" in the db */ #ifdef SCAN_IO scanIoInit(&pad_ioscanpvt); #endif /* Ensure only one Dacq thread */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: start Dacq thread to run padDacqTask\n")); #endif if ( drvPadLan9118DacqThread ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: Dacq thread present, so skipping creation of another thread and using this one\n")); #endif } else { /* Create a thread to run at highest priority to do the data acquistion */ drvPadLan9118DacqThread = epicsThreadCreate( "Dacq", epicsThreadPriorityMax, epicsThreadGetStackSize(epicsThreadStackMedium), (EPICSTHREADFUNC)padDacqTask, ppadLan9118); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: Dacq thread started. Id is %d \n", (int)drvPadLan9118DacqThread)); #endif } /* To do: check val of drvPadLan9118Thread and complain if error */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadLan9118Flag, ("drvPadLan9118Initialise: hardware initialisation complete, but interrupts not yet enabled.\n")); #endif /* Initialisation is done. Turn off DP_INFO messages */ #ifdef DEBUG_PRINT drvPadLan9118Flag=DP_ERROR; #endif return OK; } /*============================================================================= Name: padGetYIntercept(y1, y2) Abs: from a pair of y-values (and globally-available, corresponding x-values) calculate first the slope, m=(y2-y1)/(x2-x1), and then the y-intercept, b=y-mx. Ret: y-intercept =============================================================================*/ long padGetYIntercept(int x1, long y1, int x2, long y2) { long m; long b; if (x2 != x1) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetYIntercept: points are: (%d,%ld) (%d,%ld)\n",x1,y1,x2,y2)); #endif m = (y2 - y1)/(long)(x2 - x1); b = y2 - m*x2; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetYIntercept: slope:%ld int:%ld\n",m,b)); #endif } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetYIntercept: vertical lines. slopes are inf\n")); return ERROR; #endif } return b; } #ifdef NEVER /* Accessor functions for dev support */ long padGetInterceptI(int channel) { long val; rf_channel_t* pCh; pCh = pChannels[channel]; val = pCh->yintI; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetInterceptI: address to channel structure is 0x%08x\n", (unsigned int) &pCh)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetInterceptI: yintI is %ld\n", val)); #endif return val; } long padGetInterceptQ(int channel) { long val; rf_channel_t* pCh; pCh = pChannels[channel]; val = pCh->yintQ; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetInterceptQ: address to channel structure is 0x%08x\n", (unsigned int) &pCh)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetInterceptQ: yintQ is %ld\n", val)); #endif return val; } #endif /*============================================================================= Name: padGetSignal(int channel, int sample, int inputType) Abs: Ret: The data read from the FIFO for a specified channel and sample for any type: raw (0), scalar (1), processed (2), ... Here and other places: TO DO: range check on "channel" =============================================================================*/ void *padGetSignal(int channel, int sample, int inputType) { #ifdef DEBUGPRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: returning signal for ch:%d, samp:%d, inputType:%d\n", channel, sample, inputType)); #endif /* Determine if operational data or calibration data based on channel */ if (channel >= 0 && channel < PAD_NUM_CHANNELS_ON_BOARD) { /* Figure out what to do based on inputType */ switch (inputType) { case PAD_RAW: switch (sample) { case 0: return (void*)(pChannels[channel]->data0); break; case 1: return (void*)(pChannels[channel]->data1); break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadGetSigFlag, ("padGetSignal: unrecognized sample for ch %d type %d: %d\n", channel, inputType, sample)); #endif return (void*) NULL; } break; case PAD_SCALAR: case PAD_CONSTANT: return pChannels[channel]->scalar; break; case PAD_PROCESSED: switch (sample) { case 0: return (void*)(pChannels[channel]->proc0); break; case 1: return (void*)(pChannels[channel]->proc1); break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadGetSigFlag, ("padGetSignal: unrecognized sample for ch %d type %d: %d\n", channel, inputType, sample)); #endif return (void*) NULL; } break; case PAD_NON_INTERLACED_RAW: return pChannels[channel]->data0; break; case PAD_SCALAR_NON_INTERLACED_I: return pChannels[channel]->scalarI; break; case PAD_SCALAR_NON_INTERLACED_Q: return pChannels[channel]->scalarQ; break; case PAD_PROCESSED_NON_INTERLACED_I: #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: with inputType=PROCESSED, returns ptr to IFQ's procI array\n")); #endif return (void*)(pChannels[channel]->procI); break; case PAD_PROCESSED_NON_INTERLACED_Q: #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: with inputType=PROCESSED, returns ptr to IFQ's procQ array\n")); #endif return (void*)(pChannels[channel]->procQ); break; case PAD_PCAV: /* return the pointer to an array that contains: avgI for sample 0, avgQ for sample 0, midpoint for sample 0, avgI for sample1, avgQ for sample 1, midpoint of sample 1 */ switch (channel) { case 0: ch0data[0]=0; ch0data[1]=*(pChannels[0]->avgI); ch0data[2]=*(pChannels[0]->avgQ); ch0data[3]=(long)(*(pChannels[0]->ofst) + (*(pChannels[0]->sz))/2); ch0data[4]=*(pChannels[0]->avgI+1); ch0data[5]=*(pChannels[0]->avgQ+1); ch0data[6]=(long)(*(pChannels[0]->ofst+1) + (*(pChannels[0]->sz+1))/2); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: with inputType=PCAV, data array is: %ld %ld %ld %ld %ld %ld %ld\n", ch0data[0], ch0data[1], ch0data[2], ch0data[3], ch0data[4], ch0data[5], ch0data[6])); #endif return (void*)ch0data; break; case 1: ch1data[0]=1; ch1data[1]=*(pChannels[1]->avgI); ch1data[2]=*(pChannels[1]->avgQ); ch1data[3]=(long)(*(pChannels[1]->ofst) + (*(pChannels[1]->sz))/2); ch1data[4]=*(pChannels[1]->avgI+1); ch1data[5]=*(pChannels[1]->avgQ+1); ch1data[6]=(long)(*(pChannels[1]->ofst+1) + (*(pChannels[1]->sz+1))/2); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: with inputType=PCAV, data array is: %ld %ld %ld %ld %ld %ld %ld\n", ch1data[0], ch1data[1], ch1data[2], ch1data[3], ch1data[4], ch1data[5], ch1data[6])); #endif return (void*)ch1data; break; case 2: ch2data[0]=2; ch2data[1]=*(pChannels[2]->avgI); ch2data[2]=*(pChannels[2]->avgQ); ch2data[3]=(long)(*(pChannels[2]->ofst) + (*(pChannels[2]->sz))/2); ch2data[4]=*(pChannels[2]->avgI+1); ch2data[5]=*(pChannels[2]->avgQ+1); ch2data[6]=(long)(*(pChannels[2]->ofst+1) + (*(pChannels[2]->sz+1))/2); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: with inputType=PCAV, data array is: %ld %ld %ld %ld %ld %ld %ld\n", ch2data[0], ch2data[1], ch2data[2], ch2data[3], ch2data[4], ch2data[5], ch2data[6])); #endif return (void*)ch2data; break; case 3: ch3data[0]=3; ch3data[1]=*(pChannels[3]->avgI); ch3data[2]=*(pChannels[3]->avgQ); ch3data[3]=(long)(*(pChannels[3]->ofst) + (*(pChannels[3]->sz))/2); ch3data[4]=*(pChannels[3]->avgI+1); ch3data[5]=*(pChannels[3]->avgQ+1); ch3data[6]=(long)(*(pChannels[3]->ofst+1) + (*(pChannels[3]->sz+1))/2); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadGetSigFlag, ("padGetSignal: with inputType=PCAV, data array is: %ld %ld %ld %ld %ld %ld %ld\n", ch3data[0], ch3data[1], ch3data[2], ch3data[3], ch3data[4], ch3data[5], ch3data[6])); #endif return (void*)ch3data; break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadGetSigFlag, ("padGetSignal: unrecognized channel: %d\n", channel)); #endif return (void*) NULL; } break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvPadGetSigFlag, ("padGetSignal: unrecognized input type: %d\n", inputType)); #endif return (void*) NULL; } } else { /* Send back calibration data */ return (void*)(pCalChannels[channel-4]->data0); } return (void*) NULL; } /*============================================================================= Name: padGetAverageQ(int channel, int sample) Abs: =============================================================================*/ long padGetAverageQ(int channel, int sample) { long* addr; long val; addr = pChannels[channel]->avgQ; val = *((long*)addr+sample); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageQ: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageQ: address to avgQ[0] is 0x%08x\n", (unsigned int) addr)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageQ: address to avgQ[%d] is 0x%08x\n", sample, (unsigned int) (addr+sample))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageQ: avgQ is %ld\n", val)); #endif return val; } /*============================================================================= Name: padGetAverageI(int channel, int sample) Abs: =============================================================================*/ long padGetAverageI(int channel, int sample) { long *addr; long val; addr = pChannels[channel]->avgI; val = *((long*)addr+sample); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageI: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageI: address to avgI[0] is 0x%08x\n", (unsigned int) addr)); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageI: address to avgI[%d] is 0x%08x\n", sample, (unsigned int) (addr+sample))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageI: avgI is %ld\n", val)); #endif return val; } /*============================================================================= Name: padGetAverageS(int channel, int sample) Abs: =============================================================================*/ long padGetAverageS(int channel, int sample) { long val; val = pChannels[channel]->avgS; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageS: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageS: address to avgS is 0x%08x\n", (unsigned int) &(pChannels[channel]->avgS))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageS: avgS is %ld\n", val)); #endif return val; } /*============================================================================= Name: padGetSumS(int channel, int sample) Abs: =============================================================================*/ long padGetSumS(int channel, int sample) { long long val; val = pChannels[channel]->sumS; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSumS: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSumS: address to sumS is 0x%08x\n", (unsigned int) &(pChannels[channel]->avgS))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetSumS: sumS is %lld\n", val)); #endif return val; } /*============================================================================= Name: padGetNumS(int channel, int sample) Abs: =============================================================================*/ long padGetNumS(int channel, int sample) { int val; val = pChannels[channel]->numS; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetNumS: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetNumS: address to numS is 0x%08x\n", (unsigned int) &(pChannels[channel]->avgS))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetNumS: numS is %d\n", val)); #endif return val; } /*============================================================================= 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[%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[%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; } /* scondam: 01.28.09: */ /*============================================================================= Name: padGetScalarS(int channel) Abs: =============================================================================*/ short padGetScalarS(int channel) { short val; val = *(pChannels[channel]->scalar); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetScalarS: address to channel structure is 0x%08x\n", (unsigned int) &(pChannels[channel]))); DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetScalarS: scalar is %d\n", val)); #endif return val; } /*============================================================================= Name: padSetScalarS Abs: Sets the scalar value =============================================================================*/ int padSetScalarS(int channel, short scalar) { rf_channel_t* pCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; *(pCh->scalar) = scalar; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetScalarS: Set scalar to = %d\n", scalar)); #endif break; default: DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetScalarS: unrecognized channel: %d\n", channel)); return ERROR; } return OK; } #if defined PAD_SIMPLE_PROCESSING /* Begin functions specific to Interlocking PADs */ /*============================================================================= Name: padGetPulseAverage Abs: Returns the number of pulses to average over. This was set by user. =============================================================================*/ int padGetPulseAverage(int channel) { int val = 0; rf_channel_t* pCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; val = pCh->plsavg; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetPulseAverage: Number of pulses to average is %d\n", val)); #endif break; default: DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetSampleOffset: unrecognized channel: %d\n", channel)); break; } return val; } /*============================================================================= Name: padSetPulseAverage Abs: Sets the number of pulses to average =============================================================================*/ int padSetPulseAverage(int channel, int plsavg) { rf_channel_t* pCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; pCh->plsavg = plsavg; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetPulseAverage: Pulse Average = %d\n", plsavg)); #endif break; default: DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetPulseAverage: unrecognized channel: %d\n", channel)); return ERROR; } return OK; } /*============================================================================= Name: padGetAverageOutPedestal Abs: Returns the number of pedestal value of avgOut. This was set by user. A FAULT condition can occur only if avgOut is above this pedestal value. =============================================================================*/ int padGetAverageOutPedestal(int channel) { int val = 0; rf_channel_t* pCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; val = pCh->avgOut_ped; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageOutPedestal: Pedestal value of Average Out is %d\n", val)); #endif break; default: DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetAverageOutPedestal: unrecognized channel: %d\n", channel)); break; } return val; } /*============================================================================= Name: padSetAverageOutPedestal Abs: Sets the number of pulses to average =============================================================================*/ int padSetAverageOutPedestal(int channel, int avgOut_ped) { rf_channel_t* pCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; pCh->avgOut_ped = avgOut_ped; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadCfgFlag, ("padSetAverageOutPedestal: Pedestal value of Average Out = %d\n", pCh->plsavg)); #endif break; default: DEBUGPRINT(DP_ERROR, drvPadCfgFlag, ("padSetAverageOutPedestal: unrecognized channel: %d\n", channel)); return ERROR; } return OK; } /*============================================================================= Name: padGetAverageOut Abs: Returns the Average raw value of 'plsavg' pulses. =============================================================================*/ int padGetAverageOut(int channel) { int val = 0; rf_channel_t* pCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; val = pCh->avgOut; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvPadFlag, ("padGetAverageOut: Average value of pulses is %d\n", val)); #endif break; default: DEBUGPRINT(DP_ERROR, drvPadFlag, ("padGetAverageOut: unrecognized channel: %d\n", channel)); break; } return val; } #endif /* End of functions specific to Interlocking PADs */ /*============================================================================= 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 PADLAN9118_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 PADLAN9118_BPH fprintf(fout,"Channel %d Sample 1: ", jj); printf("padDumpDataToFile: channel %d: writing %d shorts to '%s'\n", jj, *(pChannels[jj]->sz+1), filename); for (kk=0; kk<*(pChannels[jj]->sz+1); kk++) { fprintf (fout,"%d ", (int) *(pChannels[jj]->data1+kk)); } fprintf(fout,"\n"); #ifdef PAD_PROCESS_DURING_READ fprintf(fout, "sumQ %llu\n", *(pChannels[jj]->sumQ+1)); fprintf(fout, "sumI %llu\n", *(pChannels[jj]->sumI+1)); fprintf(fout, "numQ %d\n", *(pChannels[jj]->numQ+1)); fprintf(fout, "numI %d\n", *(pChannels[jj]->numI+1)); #endif #endif #ifdef PADLAN9118_BPH3 if (jj == 3) { fprintf(fout,"Channel %d Sample 1: ", jj); printf("padDumpDataToFile: channel %d: writing %d shorts to '%s'\n", jj, *(pChannels[jj]->sz+1), filename); for (kk=0; kk<*(pChannels[jj]->sz+1); kk++) { fprintf (fout,"%d ", (int) *(pChannels[jj]->data1+kk)); } fprintf(fout,"\n"); #ifdef PAD_PROCESS_DURING_READ fprintf(fout, "sumQ %llu\n", *(pChannels[jj]->sumQ+1)); fprintf(fout, "sumI %llu\n", *(pChannels[jj]->sumI+1)); fprintf(fout, "numQ %d\n", *(pChannels[jj]->numQ+1)); fprintf(fout, "numI %d\n", *(pChannels[jj]->numI+1)); #endif } #endif } fclose(fout); return OK; } /*============================================================================= Name: padDumpCalDataToFile Abs: data for all calibration channel structures get written =============================================================================*/ int padDumpCalDataToFile() { int jj; int kk; FILE *fout; char filename[40]; for (jj=0; jjsz, filename); } for (kk=0; kksz; kk++) { fprintf (fout,"%d\n", (int) *(pCalChannels[jj]->data0+kk)); } fclose(fout); } return OK; } /*============================================================================= Name: padDumpNonLinDataToFile Abs: Pass in num of channel 0 raw datasets you want written to server (one per file). No timestamping into filename is done because files are named so that they match names expected by padHist executable. =============================================================================*/ int padDumpNonLinDataToFile(int fileprefix) { int kk; FILE *fout0; FILE *fout1; FILE *fout2; FILE *fout3; char filename0[40]; char filename1[40]; char filename2[40]; char filename3[40]; /* Create 4 new files for this acquisition */ sprintf(filename0,"/data/zero/%d.dat",fileprefix); sprintf(filename1,"/data/one/%d.dat",fileprefix); sprintf(filename2,"/data/two/%d.dat",fileprefix); sprintf(filename3,"/data/three/%d.dat",fileprefix); /* Open the 4 new files */ fout0 = fopen(filename0, "w"); if (fout0 == NULL) { fprintf(stderr, "padDumpNonLinDataToFile: error opening file '%s': %s\n", filename0, strerror(errno)); return ERROR; } else { printf("padDumpNonLinDataToFile: writing to file '%s'\n", filename0); } fout1 = fopen(filename1, "w"); if (fout1 == NULL) { fprintf(stderr, "padDumpNonLinDataToFile: error opening file '%s': %s\n", filename1, strerror(errno)); return ERROR; } else { printf("padDumpNonLinDataToFile: writing to file '%s'\n", filename1); } fout2 = fopen(filename2, "w"); if (fout2 == NULL) { fprintf(stderr, "padDumpNonLinDataToFile: error opening file '%s': %s\n", filename2, strerror(errno)); return ERROR; } else { printf("padDumpNonLinDataToFile: writing to file '%s'\n", filename2); } fout3 = fopen(filename3, "w"); if (fout3 == NULL) { fprintf(stderr, "padDumpNonLinDataToFile: error opening file '%s': %s\n", filename3, strerror(errno)); return ERROR; } else { printf("padDumpNonLinDataToFile: writing to file '%s'\n", filename3); } /* Write the data to the files */ printf("padDumpNonLinDataToFile: calibration channel 0: writing %d shorts to '%s'\n", pCalChannels[0]->sz, filename0); for (kk=0; kksz; kk++) { fprintf (fout0,"%d\n", (int) *(pCalChannels[0]->data0+kk)); } printf("padDumpNonLinDataToFile: calibration channel 1: writing %d shorts to '%s'\n", pCalChannels[1]->sz, filename1); for (kk=0; kksz; kk++) { fprintf (fout1,"%d\n", (int) *(pCalChannels[1]->data0+kk)); } printf("padDumpNonLinDataToFile: calibration channel 2: writing %d shorts to '%s'\n", pCalChannels[2]->sz, filename2); for (kk=0; kksz; kk++) { fprintf (fout2,"%d\n", (int) *(pCalChannels[2]->data0+kk)); } printf("padDumpNonLinDataToFile: calibration channel 3: writing %d shorts to '%s'\n", pCalChannels[3]->sz, filename3); for (kk=0; kksz; kk++) { fprintf (fout3,"%d\n", (int) *(pCalChannels[3]->data0+kk)); } /* Close the files */ fclose(fout0); fclose(fout1); fclose(fout2); fclose(fout3); return OK; } /*============================================================================= Name: padDumpAvgToFile Abs: =============================================================================*/ int padDumpAvgToFile() { int nn; FILE *fout; fout = fopen("junk.dat", "w"); if (fout == NULL) { fprintf(stderr, "padDumpQavgToFile: error opening file 'junk.dat': %s\n", strerror(errno)); return ERROR; } else { printf("padDumpQavgToFile: writing q averages to file 'junk.dat'\n"); } for (nn=0; nn 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; } #ifdef USING_DMA /*============================================================================= Test DMA transfer in channel 0 from EIM to memory Ref: Chapter 16 of MCF5282 User's Manual Step 1. Channel 2 initialization: load control info, addr ptrs and byte-xf count Step 2. Data transfer Step 3. Channel termination =============================================================================*/ int dmaTest() { unsigned long dcr2_mask; int ii = 0; /* Set the data source of the DMA transfer to be channel 2s FIFO */ sar2= csar1_actual; dar2= padChannel2; bcr2 = 4096; /* 2^16 words * 2 bytes/word */ *dsr2 = DONE; /* Clear the status register to start */ dcr2_mask = 0x086d0000; /* Steal cycles, source size, dest size, no auto-align, and ... START!!! this is: 1000 0110 1101 0000 0000 0000 0000 in binary */ *dcr2 = dcr2_mask; while (1) { /* Check for channel termination */ switch (*dsr2) { case DONE: printf ("dmaTest: DMA transfer done after %d loops.\n", ii); return OK; break; case BSY: printf ("dmaTest: DMA transfer busy after %d loops.\n", ii); break; case REQ: printf ("dmaTest: DMA transfer requested. %d-th loop.\n", ii); break; case BED: printf ("dmaTest: DMA bus error on destination. %d-th loop.\n", ii); return ERROR; break; case BES: printf ("dmaTest: DMA bus error on source. %d-th loop.\n", ii); return ERROR; break; case CE: printf ("dmaTest: DMA configuration error. %d-th loop.\n", ii); return ERROR; break; case RSD1: case RSD2: default: /* seeing this in dsr2: 111 0010 1001 0110 0010 0101 0101 */ printf ("dmaTest: invalid setting: %lu. %d-th loop.\n", (unsigned int) *dsr2, ii); } ii++; sleep(1); } return OK; } #endif