/*============================================================================= Name: drvBeamChargeCF.c Abs: Driver support for LCLS Beam Charge 4 channel ADC module (Arcturus Coldfire 5282 processor with LTC2208 ADC). Refs: See list of docs under the heading "" on page: http://www.slac.stanford.edu/grp/lcls/controls/global/subsystems/llrf Compiler Flags: DEBUGPRINT - undefine if you don't have debugPrint.h in $EPICS/site/include SCANIO - should only be undefined if you use ASYN to scan I/O Intr via callbacks RUNNING_SLOWLY - define this only if triggers are either manual or < 1Hz; it writes a lot of output to the console Auth: 16-nov-2005, Dayle Kotturi (dayle): Initial version for LLRF PAD -----------------------------------------------------------------------------*/ #include "copyright_SLAC.h" /* SLAC copyright comments */ /*----------------------------------------------------------------------------- Mod: (newest to oldest) 18-jan-2007, Dayle Kotturi: Adapted for BeamChargeCFApp =============================================================================*/ #include /* for printf */ #include /* for malloc */ #include /* for memcpy */ #include /* for errno */ #include /* for sqrt */ #include /* for strerror */ #include #include /* for IOSCANPVT */ #include /* for DRVSUPFN */ #include /* for dev*routines, atVMEA16 */ #include /* for epicsEvent* */ #include /* for epicsExportAddress */ #include /* for epicsMessageQueueCreate, ... */ #include /* for epicsTimeStamp and assoc fns */ #include /* for epicsTimerQueue for watchdog */ #include /* for epicsThreadPriorityMax and to get confdefs.h indirectly inc'd */ #ifdef DEBUG_PRINT #include /* for DEBUGPRINT */ #endif #include "drvBeamChargeCF.h" /* Interrupt/General-Purpose I/O Pin Descriptions All pins default to general-purpose input pins at reset. The pin value is synchronized to the rising edge of CLKOUT when read from the EPORT pin data register (EPPDR). The values used in the edge/level detect logic are also synchronized to the rising edge of CLKOUT. These pins use Schmitt triggered input buffers which have built in hysteresiss designed to decrease the probability of generating false edge-triggered interrrupts for slow rising and falling input signals. When a pin is configured as an output, it is driven to a state whose level is determined by the corresponding bit in the EPORT data register(EPDR). All bits in the EPDR are high at reset. */ #define IPSBAR (0x40000000) /* Default base address for the Edgeport and the Chip Select Module */ /* Adding the base address here, defies the purpose of having the change base address routine */ /* Interrupt controller module */ #define ICR02 (IPSBAR+0xC42) /* Address of interrupt control register #2. 1 byte */ #define IMRL (IPSBAR+0xC0C) /* Address of interrupt mask register low. 4 bytes */ #define EPPAR (IPSBAR+0x00130001) /* Two bytes. Big endian. 0=bits 15-8, 1=bits 7-0 */ #define EPDDR (IPSBAR+0x00130002) /* EPORT Data Direction Register */ #define EPIER (IPSBAR+0x00130003) /* EPORT Interrupt Enable Register */ #define EPDR (IPSBAR+0x00130004) /* EPORT Data Register */ #define EPPDR (IPSBAR+0x00130005) /* EPORT Pin Data Register */ #define EPFR (IPSBAR+0x00130006) /* EPORT Flag Register */ /* Note: 0x00130007 is reserved */ /* Note about access: EPPAR, EPDDR, and EPIER are Supervisor access EPDR, EPPDR and EPRF are Supervisor/User access Writing to reserved address has no effect, reading returns zeroes */ #define CSAR1 (IPSBAR+0x00008C) /* 2 bytes */ #define CSMR1 (IPSBAR+0x000090) /* 4 bytes */ #define CSCR1 (IPSBAR+0x000096) /* 2 bytes */ #ifdef USING_QSPI /* Queued serial port interface (QSPI) */ #define PQSPAR (IPSBAR+0x100059)/* Port QS Pin Assignment Register */ #define QMR (IPSBAR+0x340) /* QSPI Mode Register. 2 bytes */ #define QDLYR (IPSBAR+0x344) /* QSPI Delay Register. 2 bytes */ #define QWR (IPSBAR+0x348) /* QSPI Wrap Register. 2 bytes */ #define QIR (IPSBAR+0x34C) /* QSPI Interrupt Register. 2 bytes */ #define QAR (IPSBAR+0x350) /* QSPI Address Register. 2 bytes */ #define QDR (IPSBAR+0x354) /* QSPI Data Register. 2 bytes */ #endif #define DACQ_TASK_PRIO epicsThreadPriorityHigh #define DACQ_TASK_STACK (20000) /* task stack size */ #define BCC_MAX_INT (2147483647) /* 4 byte int - 1 bit for sign */ #define BCC_IRQ_VECTOR (66) /* DECIMAL! */ #define BCC_MAX_FILENAME (100) /* 2nd dimension, data1 and yint* are only used by BCC_BPH app *S vars are only used by BCC_WF app sumSq* and sigmaSq* are only used by BCC_REF app */ typedef struct rf_channel_s { int ofst; int sz; int x; int nToRead; int nRead; int num; short* data; /* the sample of interest in the FIFO */ } rf_channel_t; typedef struct iqcal_channel_s { int ofst; int sz; int nToRead; int nRead; short* data; /* the sample of interest in the FIFO */ } iqcal_channel_t; rf_channel_t bccCh0; rf_channel_t bccCh1; rf_channel_t bccCh2; rf_channel_t bccCh3; iqcal_channel_t bccCalCh0; iqcal_channel_t bccCalCh1; iqcal_channel_t bccCalCh2; iqcal_channel_t bccCalCh3; /* Use this array of pointers to group the 4 like channel structures together */ rf_channel_t* pChannels[4]={&bccCh0, &bccCh1, &bccCh2, &bccCh3}; iqcal_channel_t* pCalChannels[4]={&bccCalCh0, &bccCalCh1, &bccCalCh2, &bccCalCh3}; struct drvet drvBeamChargeCF = { 2, (DRVSUPFUN) bccReport, /* subroutine defined in this file */ (DRVSUPFUN) bccInitialise /* subroutine defined in this file */ }; epicsExportAddress(drvet, drvBeamChargeCF); epicsTimerQueueId bccWdTimerQ = 0; #ifdef DEBUG_PRINT /* ENUM with values: DP_FATAL, DP_ERROR, DP_WARN, DP_INFO, DP_DEBUG Anything less than or equal to drvPnetFlag gets printed out. Can be set to 0 here and altered via prompt or in st.cmd file */ int drvBccFlag = 2; int drvBccCfgFlag = 2; int drvBccScalFlag = 2; int drvBccIntrFlag = 2; #endif /* Hardware registers for chip select and EPORT module */ LOCAL volatile unsigned char *icr02 = (unsigned char *)ICR02; LOCAL volatile unsigned long *imrl = (unsigned long *)IMRL; LOCAL volatile unsigned char *epddr = (unsigned char *)EPDDR; LOCAL volatile unsigned char *epier = (unsigned char *)EPIER; LOCAL volatile unsigned char *epdr = (unsigned char *)EPDR; LOCAL volatile unsigned char *eppar = (unsigned char *)EPPAR; LOCAL volatile unsigned char *eppdr = (unsigned char *)EPPDR; LOCAL volatile unsigned char *epfr = (unsigned char *)EPFR; LOCAL volatile short *csar1 = (short *)CSAR1; LOCAL volatile short *csar1_actual = (short *) 0x80000000; LOCAL volatile unsigned short *cscr1 = (unsigned short *)CSCR1; LOCAL volatile unsigned long *csmr1 = (unsigned long *)CSMR1; /* Driver globals */ LOCAL epicsEventId waitForData; LOCAL epicsTimeStamp timeAtBoot; LOCAL int intrCount = 0; LOCAL int numIntrRollovers = 0; LOCAL int overflowCount = 0; LOCAL int numOverflowRollovers = 0; LOCAL char filename[BCC_MAX_FILENAME]; LOCAL char filenameStem[BCC_MAX_FILENAME]; #ifdef SCANIO LOCAL int scanIoCount = 0; LOCAL IOSCANPVT bcc_ioscanpvt; /* single interrupt source for all bcc 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 int bccDumpQavgToFile(); int bccDumpIavgToFile(); /*============================================================================= Name: bccDumpSignal(int channel) Abs: Print out latest raw data in channel "channel" =============================================================================*/ int bccDumpSignal(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; iisz; ii++) { printf ("%8d ", *(pCh->data+ii)); } printf("\n"); for (ii=0; iisz; ii++) { printf ("0x%08x: ", (unsigned int)(pCh->data+ii)); } printf("\n"); 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->data+ii), *(pCCh->data+ii)); } printf("\n"); break; default: printf("bccDumpSignal: invalid channel: %d\n", channel); return ERROR; } return OK; } /*============================================================================= Name: bccDumpChannel Abs: Debug routine used to look at the contents of a channel's structure Ret: OK =============================================================================*/ int bccDumpChannel(int channel ) { 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("num: %d\n", pCh->num); printf("\n"); bccDumpSignal(channel); 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); bccDumpSignal(channel); break; default: printf("bccDumpChannel: unrecognized channel: %d\n", channel); return ERROR; } return OK; } /*============================================================================= Name: bccReport Ret: Driver support routine registered in DRVET structure =============================================================================*/ int bccReport( int interest ) { int rc; printf("IOC booted at %d seconds from EPOCH\n", timeAtBoot.secPastEpoch); printf("Current epics time: %d seconds from EPOCH\n", 0); printf("Num interrupts since boot: %d\n", intrCount); printf("Num I overflow errors since boot: %d\n", overflowCount); #ifdef SCAN_IO printf("Num scan I/O requests since boot: %d\n", scanIoCount); #endif rc = bccDumpChannel(0); rc = bccDumpChannel(1); rc = bccDumpChannel(2); rc = bccDumpChannel(3); #ifdef BCC_PROCESS_DURING_READ /* Since processed data is not currently saved, skip this */ #endif return interest; } /*============================================================================= Name: bccSetDataFileStem Abs: =============================================================================*/ /* Set the stem of the filename for the output data files */ int bccSetDataFileStem(char * stem){ strcpy(filenameStem, stem); printf("bccSetDataFileStem: setting filename to stem '%s'\n", filenameStem); return OK; } /*============================================================================= Name: bccGetDataFileStem Abs: =============================================================================*/ char * bccGetDataFileStem() { return filenameStem; } /*============================================================================= Name: bccISR Abs: Executes every time the interrupt that indicates FIFO full arrives Rem: =============================================================================*/ void bccISR () { if (intrCount < BCC_MAX_INT) { intrCount++; } else { intrCount = 0; numIntrRollovers++; } /* Announce that data is available for read */ epicsEventSignal(waitForData); /* Clear the interrupt by writing a "1" to "EPF2" */ /* Ref: p. 11-7 of EPORT manual. */ *epfr = 4; return; } /*============================================================================= Name: bccGetIntrCount Abs: Return the number of interrupts that have been processed since boot =============================================================================*/ int bccGetIntrCount() { return intrCount; } /*============================================================================= Name: bccGetOverflowCount Abs: Return the number of data overflows that have been processed since boot =============================================================================*/ int bccGetOverflowCount() { return overflowCount; } #ifdef SCANIO /*============================================================================= Name: bccGetScanIOCount Abs: =============================================================================*/ int bccGetScanIOCount() { return scanIoCount; } #endif /*============================================================================= Name: bccTurnOn/OffLED Abs: It should be obvious =============================================================================*/ int bccTurnOnLED() { volatile unsigned char *place; place = (unsigned char *) EPDR; *place = 0xF7; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccTurnOnLED: %d\n", (int) *place)); #endif return OK; } int bccTurnOffLED() { volatile unsigned char *place; place = (unsigned char *) EPDR; *place = 0xFF; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccTurnOffLED: %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: bccGetSampleOffset Abs: =============================================================================*/ int bccGetSampleOffset(int channel) { int val = 0; iqcal_channel_t* pCCh; rf_channel_t* pCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; val = pCh->ofst; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccGetSampleOffset: 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, drvBccFlag, ("bccGetSampleOffset: address to channel structure is 0x%08x\n", (unsigned int) &pCCh)); DEBUGPRINT(DP_INFO, drvBccFlag, ("bccGetSampleOffset: address to ofst is 0x%08x\n", (unsigned int) &(pCCh->ofst))); DEBUGPRINT(DP_INFO, drvBccFlag, ("bccGetSampleOffset: ofst is %d\n", val)); #endif break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvBccFlag, ("bccGetSampleOffset: unrecognized channel: %d\n", channel)); #endif } return val; } /*============================================================================= Name: bccSetSampleOffset Abs: Set offset to reflect desired value + 3 clock cycles that we must wait for =============================================================================*/ int bccSetSampleOffset(int channel, int ofst) { 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]; pCCh->ofst = ofst + 3; /* do 3 garbage reads while FIFO fills */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccCfgFlag, ("bccSetSampleOffset: ofst = %d\n", pCCh->ofst)); #endif break; case 4: case 5: case 6: case 7: pCCh = pCalChannels[channel-4]; pCCh->ofst = ofst + 3; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccCfgFlag, ("bccSetSampleOffset: ofst = %d\n", pCCh->ofst)); #endif break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvBccCfgFlag, ("bccSetSampleOffset: unrecognized channel: %d\n", channel)); #endif return ERROR; } return OK; } /*============================================================================= Name: bccGetSampleSize Abs: return sample size for any channel =============================================================================*/ int bccGetSampleSize(int channel) { int val; rf_channel_t* pCh; iqcal_channel_t* pCCh; switch (channel) { case 0: case 1: case 2: case 3: pCh = pChannels[channel]; val = pCh->sz; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccGetSampleSize: sz is 0x%08x\n", val)); #endif break; case 4: case 5: case 6: case 7: pCCh = pCalChannels[channel-4]; val = pCCh->sz; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccGetSampleSize: sz is 0x%08x\n", val)); #endif break; default: return ERROR; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccGetSampleSize: sz is %d\n", val)); #endif return val; } /*============================================================================= Name: bccSetSampleSize Abs: set sample size, free memory for the previous size and malloc the new =============================================================================*/ int bccSetSampleSize(int channel, int size) { 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 size is the BCC_FIFO_SIZE. When RUNNING, channels 0-3 are used. The sample size is BCC_SAMPLE_SIZE */ switch (channel) { case 0: case 1: case 2: case 3: if (size > BCC_SAMPLE_SIZE || size < 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvBccCfgFlag, ("bccSetSampleSize: requested size, %d, out of range [0,%d]\n", size, BCC_SAMPLE_SIZE)); #endif return ERROR; } pCh = pChannels[channel]; /* Free old and allocate new size for this channel's sample 0 */ free(pCh->data); pCh->data = malloc(sizeof(short)*size); if (pCh->data == NULL) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_FATAL, drvBccCfgFlag, ("bccSetSampleSize: unable to malloc space for ch %d sample data\n", channel)); #endif return ERROR; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccCfgFlag, ("bccSetSampleSize: malloced space for ch %d data: %d shorts at addr 0x%08x\n", channel, size, (unsigned int) (pCh->data))); #endif pCh->sz = size; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccCfgFlag, ("bccSetSampleSize: sz = %d\n", pCh->sz)); #endif break; case 4: case 5: case 6: case 7: if (size > BCC_FIFO_SIZE || size < 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvBccCfgFlag, ("bccSetSampleSize: requested size, %d, out of range [0,%d]\n", size, BCC_FIFO_SIZE)); #endif return ERROR; } pCCh = pCalChannels[channel-4]; free(pCCh->data); pCCh->data = malloc(sizeof(short)*size); if (pCCh->data == NULL) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_FATAL, drvBccCfgFlag, ("bccSetSampleSize: unable to malloc space for ch %d calibration data 0\n", channel)); #endif return ERROR; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccCfgFlag, ("bccSetSampleSize: malloced space for ch %d data: %d shorts at addr 0x%08x\n", channel, size, (unsigned int) (pCCh->data))); #endif pCCh->sz = size; break; default: #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvBccCfgFlag, ("bccSetSampleSize: unrecognized channel: %d\n", channel)); #endif return ERROR; } return OK; } /*============================================================================= Name: bccGetOperationMode Abs: return mode of operation =============================================================================*/ int bccGetOperationMode() { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccGetOperationMode: %d\n", bccOperationMode)); #endif return bccOperationMode; } /*============================================================================= Name: bccSetOperationMode Abs: set mode of operation =============================================================================*/ int bccSetOperationMode(int mode) { /* range check on mode */ if ((mode < 0) || (mode> BCC_NUM_OP_MODES)) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvBccFlag, ("bccSetOperationMode: mode %d is out of range [0,%d]\n", mode, BCC_NUM_OP_MODES)); #endif return ERROR; } bccOperationMode = mode; return OK; } /*============================================================================= Name: bccGetCalibrationControl Abs: return mode of operation =============================================================================*/ int bccGetCalibrationControl() { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccGetCalibrationControl: %d\n", bccCalibrationControl)); #endif return bccCalibrationControl; } /*============================================================================= Name: bccSetCalibrationControl Abs: set calibration control =============================================================================*/ int bccSetCalibrationControl(int calibrationControl) { /* range check on calibrationControl */ if ((calibrationControl < 0) || (calibrationControl > BCC_NUM_CAL_MODES)) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvBccFlag, ("bccSetCalibrationControl: calibrationControl %d is out of range [0,%d]\n", calibrationControl, BCC_NUM_CAL_MODES)); #endif return ERROR; } bccCalibrationControl = calibrationControl; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccSetCalibrationControl: calibrationControl set to %d\n", calibrationControl)); #endif return OK; } /*============================================================================= Name: bccReadSignal 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: bccReadSignal(NULL, NULL, pChannels[2], NULL); What else must be set up first: Abs: What is set up after: =============================================================================*/ int bccReadSignal(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 bccChanneli[jj] = *pSrc from being optimized to be assigned */ #endif int srcOfst = 0; rf_channel_t* chan = NULL; #ifndef FAKE_DATA pSrc = csar1_actual; #endif for (mm=0; mmnToRead); #else printf("bccReadSignal: faking channel %d data for %d words\n", mm, chan->sz); #endif #endif chan->nToRead = chan->ofst + chan->sz; #ifdef BCC_PROCESS_DURING_READ chan->num = 0; #endif #ifdef RUNNING_SLOWLY printf("bccReadSignal: guts of read %d\n",mm); #endif 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 clearing chip select */ #ifndef FAKE_DATA *(pSrc+srcOfst); #endif } else { #ifndef FAKE_DATA *(chan->data+jj) = *(pSrc+srcOfst); #else /* Make fake data */ printf("writing %d into addr 0x%08x (ch[%d]->data[%d])\n",ii,(unsigned int)(chan->data+jj),mm,jj); *(chan->data+jj) = ii; #endif chan->num += 1; jj++; } } /* We've finished reading out the FIFO now. Do the operations on the full dataset, bccChanneli[]... */ /* Save the number of 16-bit words read */ chan->nRead = jj; /* Calculate the average of the sample S */ if (chan->num == 0) { DEBUGPRINT(DP_ERROR, drvBccFlag, ("bccKlyReadSignal: Channel %d: num pts is zero.\n", mm)); return ERROR; } } /* mm loop */ return OK; } /*============================================================================= Name: bccCalReadSignal 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 BCC_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 BCC 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 bccCalChannels where chan gets set to bccCalChannels[mm]; for (mm=0; mmnToRead = chan->ofst + chan->sz; #ifndef FAKE_DATA printf("bccCalReadSignal: initiating chip select read at 0x%08x for %d words\n", (unsigned int) csar1_actual, chan->nToRead); #else printf("bccCalReadSignal: faking channel %d data for %d words\n", mm, chan->sz); #endif /* Here are the guts of the read. This code was timed and found to take 250 ns to read one short */ printf("bccCalReadSignal: 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->data+jj) = *(pSrc+srcOfst); #else /* Make fake data */ *(chan->data+jj) = ii; printf("bccCalReadSignal: writing %d into addr 0x%08x (ch[%d]->data[%d])\n",ii,(unsigned int)chan->data+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("bccCalReadSignal: number of words read is: %d\n", chan->nRead); } /* mm loop */ return OK; } /*============================================================================= Name: bccGetSignal(int channel, int sample) Abs: Ret: The data read from the FIFO for a specified channel =============================================================================*/ short *bccGetSignal(int channel) { #ifdef DEBUGPRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccGetSignal: returning signal for ch:%d\n", channel)); #endif /* Determine if operational data or calibration data based on channel */ if (channel >= 0 && channel < BCC_NUM_CHANNELS_ON_BOARD) { return pChannels[channel]->data; } else { /* Send back calibration data */ return pCalChannels[channel-4]->data; } return (short*) NULL; } /*============================================================================= Name: bccGetNumShortsToRead(int channel) Abs: Debug routine to check number of words planned to read, given channel =============================================================================*/ int bccGetNumShortsToRead(int channel) { int val; val = pChannels[channel]->nToRead; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccGetNumShortsToRead: numToRead is %d\n", val)); #endif return val; } /*============================================================================= Name: bccGetNumShortsRead() Abs: Debug routine to check number of words actually read, given channel and sample within the channel =============================================================================*/ int bccGetNumShortsRead(int channel) { int val; val = pChannels[channel]->nRead; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccGetNumShortsRead: numToRead is %d\n", val)); #endif return val; } /*============================================================================= Name: bccSetDataFileName Abs: =============================================================================*/ /* Create unique filename by appending first the "seconds past Epoch" and .dat" */ int bccSetDataFileName() { char timestampForFile[20]; epicsTimeStamp timeAtWrite; strcpy(filename, bccGetDataFileStem()); 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("bccSetDataFileName: complete filename is now '%s'\n",filename); return OK; } /*============================================================================= Name: bccGetDataFileName Abs: =============================================================================*/ char * bccGetDataFileName() { return filename; } /*============================================================================= Name: bccDumpDataToFile Abs: data for all channel structures get written The second sample, sample '1', is only dumped for the BCC_BPH case =============================================================================*/ int bccDumpDataToFile() { int jj; int kk; int rc; FILE *fout; /* Create a new file for this acquisition */ rc = bccSetDataFileName(); fout = fopen(bccGetDataFileName(), "w"); if (fout == NULL) { fprintf(stderr, "bccDumpDataToFile: error opening file '%s': %s\n", filename, strerror(errno)); return ERROR; } else { printf("bccDumpDataToFile: writing to file '%s'\n", filename); } for (jj=0; jjsz, filename); fprintf(fout,"Channel %d Sample: ",jj); for (kk=0; kksz; kk++) { fprintf (fout,"%d ", (int) *(pChannels[jj]->data+kk)); } fprintf(fout,"\n"); #ifdef BCC_PROCESS_DURING_READ fprintf(fout, "num %d\n", pChannels[jj]->num); #endif } fclose(fout); return OK; } /*============================================================================= Name: bccDumpCalDataToFile Abs: data for all calibration channel structures get written =============================================================================*/ int bccDumpCalDataToFile() { 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]->data+kk)); } fclose(fout); } return OK; } /*============================================================================= Name: bccDumpNonLinDataToFile 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 bccHist executable. =============================================================================*/ int bccDumpNonLinDataToFile(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, "bccDumpNonLinDataToFile: error opening file '%s': %s\n", filename0, strerror(errno)); return ERROR; } else { printf("bccDumpNonLinDataToFile: writing to file '%s'\n", filename0); } fout1 = fopen(filename1, "w"); if (fout1 == NULL) { fprintf(stderr, "bccDumpNonLinDataToFile: error opening file '%s': %s\n", filename1, strerror(errno)); return ERROR; } else { printf("bccDumpNonLinDataToFile: writing to file '%s'\n", filename1); } fout2 = fopen(filename2, "w"); if (fout2 == NULL) { fprintf(stderr, "bccDumpNonLinDataToFile: error opening file '%s': %s\n", filename2, strerror(errno)); return ERROR; } else { printf("bccDumpNonLinDataToFile: writing to file '%s'\n", filename2); } fout3 = fopen(filename3, "w"); if (fout3 == NULL) { fprintf(stderr, "bccDumpNonLinDataToFile: error opening file '%s': %s\n", filename3, strerror(errno)); return ERROR; } else { printf("bccDumpNonLinDataToFile: writing to file '%s'\n", filename3); } /* Write the data to the files */ printf("bccDumpNonLinDataToFile: 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]->data+kk)); } printf("bccDumpNonLinDataToFile: 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]->data+kk)); } printf("bccDumpNonLinDataToFile: 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]->data+kk)); } printf("bccDumpNonLinDataToFile: 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]->data+kk)); } /* Close the files */ fclose(fout0); fclose(fout1); fclose(fout2); fclose(fout3); return OK; } /*============================================================================= Name: bccGetIoScanPvt Abs: Used by records that process on I/O Intr. Device support calls this in its get_ioint_info routines at init'n. =============================================================================*/ #ifdef SCANIO IOSCANPVT bccGetIoScanPvt() { return bcc_ioscanpvt; } #endif /*============================================================================= Name: bccCalibration Abs: Does the calibration data acquisition upon receipt of calibration request. =============================================================================*/ int bccCalibration() { int rc; printf("bccCalibration: disabling hardware triggers for the calibration\n"); bccDisableIntr(); printf("bccCalibration: calling bccCalReadSignal\n"); bccCalReadSignal(&bccCalCh0,&bccCalCh1,&bccCalCh2,&bccCalCh3); /* For the BCC, calibration is a one-shot. Write the calibration data to files, one per channel and set the calibration done indicator */ printf("bccCalibration: calling bccDumpCalDataToFile\n"); rc = bccDumpCalDataToFile(); if (rc == ERROR) { printf ("bccCalibration: error writing calibration data to server\n"); return rc; } #ifdef SCANIO /* Process I/O Intr scan records registered to this callback COULD DO ANOTHER BCC_IOSCANPVT VARIABLE JUST TO PROCESS CAL WAVEFORM RECORDS scanIoRequest(bcc_cal_ioscanpvt); if (scanIoCalCount < BCC_MAX_INT) { scanIoCalCount++; } else { scanIoCalCount = 0; numScanIoCalRollovers++; } */ #endif /* Since it's a one-shot, we want to automatically set calibration done here. I.e. don't wait for the button press */ printf("bccCalibration: setting calib done\n"); bccSetCalibrationControl(BCC_CAL_DONE); /* Check: this may be also being done by the state machine upon receipt of calib done */ printf("bccCalibration: enabling hardware triggers since calibration complete\n"); bccEnableIntr(); return OK; } /*============================================================================= Name: bccDacqTask Abs: acquire and process the data upon receipt of event from ISR =============================================================================*/ int bccDacqTask() { int rc; epicsEventWaitStatus waitStatus; /* Do forever */ while (TRUE) { /* epicsEventShow(waitForData, level);*/ /* wait for the ISR to indicate that there is data waiting to be read */ waitStatus = epicsEventWait(waitForData); if (waitStatus == epicsEventWaitOK) { bccReadSignal(&bccCh0,&bccCh1,&bccCh2,&bccCh3); #ifdef SCANIO /* Process I/O Intr scan records registered to this callback */ scanIoRequest(bcc_ioscanpvt); if (scanIoCount < BCC_MAX_INT) { scanIoCount++; } else { scanIoCount = 0; numScanIoRollovers++; } #endif } else { if (waitStatus == epicsEventWaitTimeout) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvBccFlag, ("bccDacqTask: 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, drvBccFlag, ("bccDacqTask: error waiting for data to be available to read out of FIFO\n")); #endif return ERROR; } } } } return rc; } /*============================================================================= Name: bccInitialise 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 bccInitialise() { int rc; epicsThreadId rcv_id; #ifdef DEBUG_PRINT drvBccFlag=4; DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: hardware initialisation in progress...\n")); DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: IRQ1 before: %d\n", *epddr & 2)); DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: IRQ3 before: %d\n", *epddr & 8)); DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: IRQ4 before: %d\n", *epddr & 16)); DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: IRQ5 before: %d\n", *epddr & 32)); #endif #ifdef NOTYET /* Save the time of start up for reporting purposes */ epicsTimeGetCurrent(&timeAtBoot); #endif *icr02 = 0x30; /* use interrupt level 6 */ *imrl &= 0xFFFB; /* 1111 1111 1111 1011 clears the bit which enables the interrupt */ *eppar = 0x10; /* set to use rising edge trigger */ /* *eppar = 0x20; set to use falling edge trigger (save 6 usec) */ /**eppar = 0x00; set to use level edge trigger */ /* Clear epdr to start. This disables all interrupts. */ *epdr = 0; /* Clear epddr to start. This means that all interrupts are inputs. */ *epddr = 0; /* USED FOR S/W triggering data acquistion. Make IRQ1 an output by turning on 1st bit */ *epddr |= 2; /* NOTE: leave IRQ2 at its default setting: input; this is the interrupt we are using for the hardware interrupts to come */ /* NOT USED: Make IRQ3 an output by turning on 3rd bit */ *epddr |= 8; /* NOT USED: Make IRQ4 an output by turning on the 4th bit */ *epddr |= 16; /* USED TO MEASURE XF TIME TO VME AND BACK: Make IRQ5 an output by turning on the 5th bit */ *epddr |= 32; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: IRQ1 after: %d\n", *epddr &2 )); DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: IRQ3 after: %d\n", *epddr &8 )); DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: IRQ4 after: %d\n", *epddr & 16)); DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: IRQ5 after: %d\n", *epddr & 32)); #endif /* 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, drvBccFlag, ("bccInitialise: 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, drvBccFlag, ("bccInitialise: csar1_actual has addr 0x%08x and contents 0x%08x\n", (int) csar1_actual, *csar1_actual)); DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: cscr1 has addr 0x%08x and contents 0x%08x\n", (int) cscr1, (short) *cscr1)); DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: 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 /* Initialize our watchdog timer queue */ bccWdTimerQ=epicsTimerQueueAllocate( 1, /* ok to share queue */ epicsThreadPriorityLow); if (bccWdTimerQ==0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvBccFlag, ("bccInitialise: error initialising watchdog timer queue\n")); #endif return ERROR; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: watchdog timer queue initialised\n")); #endif waitForData = epicsEventCreate(epicsEventEmpty); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: created event sem %d\n", (int) waitForData)); #endif /* Set this up if there are records scanned on "I/O Intr" in the db */ #ifdef SCANIO scanIoInit(&bcc_ioscanpvt); #endif /* Create the task to do data acquisition */ rcv_id = epicsThreadCreate( "bccDacqTask", DACQ_TASK_PRIO, DACQ_TASK_STACK, (EPICSTHREADFUNC)bccDacqTask, 0); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: data acquistion task id is %d\n", (int)rcv_id)); #endif /* Connect the interrupt vector to the interrupt service routine */ rc = devConnectInterruptVME(BCC_IRQ_VECTOR, bccISR, 0); if (rc != 0) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_FATAL, drvBccFlag, ("bccInitialise: unable to connect to intr vector to ISR. rc = %d\n", rc)); #endif return ERROR; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: intr vector, %x, connected to ISR\n", BCC_IRQ_VECTOR)); #endif #ifdef DEBUG_PRINT drvBccFlag=0; #endif #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccFlag, ("bccInitialise: hardware initialisation complete, but interrupts not yet enabled.\n")); #endif return OK; } int bccEnableIntr() { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccIntrFlag, ("bccEnableIntr: enabling interrupts.\n")); #endif /* Enable the interrupt. Don't do this UNTIL everything is set up */ *epier = 0x04; /* Enable IRQ2 */ return OK; } int bccDisableIntr() { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, drvBccIntrFlag, ("bccEnableIntr: disabling interrupts.\n")); #endif /* Disable the interrupt. Called when operational mode starts CALIBRATING */ *epier = 0x00; return OK; } /*============================================================================= Name: bccSoftTrigger 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 bccSoftTrigger(){ *epdr &= ~2; *epdr |= 2; *epdr &= ~2; return OK; } /*============================================================================= Name: bccNonLin 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 bccDumpNonLinDataToFile() for exact dir names. =============================================================================*/ int bccNonlin(int testSize) { int kk; printf("bccNonLin: Setting up for non-linearity measurement...\n"); printf("bccNonLin: Writing %d files for each channels to nonlin dir\n", testSize); printf("bccNonLin: Disabling hardware interrupts\n"); bccDisableIntr(); for (kk=0; kk= BCC_NUM_CHANNELS_ON_BOARD ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, drvBccFlag, ("bccClearChannel: unrecognized channel %d\n", channel)); #endif return ERROR; } memset(pChannels[channel],0,sizeof(rf_channel_t)); return OK; }