/*============================================================================= Name: drvPll.c Abs: Driver support for LCLS Phase Locked Loop 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 - Auth: 16-nov-2005, Dayle Kotturi (dayle): original for LLRF PAD -----------------------------------------------------------------------------*/ #include "copyright_SLAC.h" /* SLAC copyright comments */ /*----------------------------------------------------------------------------- Mod: (newest to oldest) 05-jun-2007, Robert Steele: hacked for pll 23-may-2007, Robert Steele: dma, diagnostic bit for timing eval =============================================================================*/ #include /* for printf */ #include /* for malloc */ #include #include /* for errno */ #include /* for sqrt */ #include /* for strerror */ #include #include /* for IOSCANPVT */ #include /* for DRVSUPFN */ #include /* for dev*routines, atVMEA16 */ #include /* for epicsEvent* */ #include /* for epicsExportAddress */ /*#include / * for epicsMessageQueueCreate, ... */ #include /* for epicsTimeStamp and assoc fns */ #include /* for epicsTimerQueue for watchdog */ #include /* for epicsThreadPriorityMax and to get confdefs.h indirectly inc'd */ /* #include for in_le32 */ #include /* for DEBUGPRINT */ #include "drvPll.h" #include "pllChannel.hxx" #include #include /* Interrupt/General-Purpose I/O Pin Descriptions All pins default to general-purpose input pins at reset. The pin value is synchronized to the rising edge of CLKOUT when read from the EPORT pin data register (EPPDR). The values used in the edge/level detect logic are also synchronized to the rising edge of CLKOUT. These pins use Schmitt triggered input buffers which have built in hysteresiss designed to decrease the probability of generating false edge-triggered interrrupts for slow rising and falling input signals. When a pin is configured as an output, it is driven to a state whose level is determined by the corresponding bit in the EPORT data register(EPDR). All bits in the EPDR are high at reset. */ #define IPSBAR (0x40000000) /* Default base address for the Edgeport and the Chip Select Module */ /* Adding the base address here, defies the purpose of having the change base address routine */ /* scm registers */ #define MPARK (IPSBAR+0x1C) /* Bus Master Park Register */ #define Mpark_BCR24BIT Bit(24) /* Enables the use of 24 bit byte count registers in the DMA module */ /* Interrupt controller module */ #define ICR02 (IPSBAR+0xC42) /* Address of interrupt control register #2. 1 byte */ #define IMRL (IPSBAR+0xC0C) /* Address of interrupt mask register low. 4 bytes */ #define EPPAR (IPSBAR+0x00130001) /* Two bytes. Big endian. 0=bits 15-8, 1=bits 7-0 */ #define EPDDR (IPSBAR+0x00130002) /* EPORT Data Direction Register */ #define EPIER (IPSBAR+0x00130003) /* EPORT Interrupt Enable Register */ #define EPDR (IPSBAR+0x00130004) /* EPORT Data Register */ #define EPPDR (IPSBAR+0x00130005) /* EPORT Pin Data Register */ #define EPFR (IPSBAR+0x00130006) /* EPORT Flag Register */ /* Note: 0x00130007 is reserved */ /* Note about access: EPPAR, EPDDR, and EPIER are Supervisor access EPDR, EPPDR and EPRF are Supervisor/User access Writing to reserved address has no effect, reading returns zeroes */ #define CSAR1 (IPSBAR+0x00008C) /* 2 bytes */ #define CSMR1 (IPSBAR+0x000090) /* 4 bytes */ #define CSCR1 (IPSBAR+0x000096) /* 2 bytes */ #ifdef DEBUG_BIT int diagnosticBitType = 0; #endif /* DEBUG_BIT */ /* use low priority to allow large messages to run over the top * of the creation of subsequent messages */ #define DACQ_TASK_PRIO epicsThreadPriorityLow #define DACQ_TASK_STACK (20000) /* task stack size */ #define IRQ_VECTOR (66) /* DECIMAL! */ struct drvet drvPll = { 2, (DRVSUPFUN) pllReport, /* subroutine defined in this file */ (DRVSUPFUN) pllInitialise /* subroutine defined in this file */ }; epicsExportAddress(drvet, drvPll); epicsTimerQueueId pllWdTimerQ = 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 drvPllFlag = DP_ERROR; /* manipulated below, so this is just a placeholder value */ int drvPllIntrFlag = DP_DEBUG; /* DP_DEBUG DP_ERROR */ #endif /* Hardware registers for chip select and EPORT module */ LOCAL volatile unsigned char *icr02 = (unsigned char *)ICR02; LOCAL volatile unsigned long *imrl = (unsigned long *)IMRL; LOCAL volatile unsigned char *epddr = (unsigned char *)EPDDR; LOCAL volatile unsigned char *epier = (unsigned char *)EPIER; LOCAL volatile unsigned char *epdr = (unsigned char *)EPDR; LOCAL volatile unsigned char *eppar = (unsigned char *)EPPAR; LOCAL volatile unsigned char *eppdr = (unsigned char *)EPPDR; LOCAL volatile unsigned char *epfr = (unsigned char *)EPFR; LOCAL volatile short *csar1 = (short *)CSAR1; LOCAL volatile short *csar1_actual = (short *) 0x80000000; LOCAL volatile unsigned short *cscr1 = (unsigned short *)CSCR1; LOCAL volatile unsigned long *csmr1 = (unsigned long *)CSMR1; /* Driver globals */ LOCAL epicsEventId waitForData; LOCAL epicsTimeStamp timeAtBoot; LOCAL int intrCount = 0; LOCAL int numIntrRollovers = 0; LOCAL int scanIoCount = 0; LOCAL IOSCANPVT pll_ioscanpvt; /* single interrupt source for all pll recs */ LOCAL int numScanIoRollovers = 0; int pllDumpQavgToFile(); int pllDumpIavgToFile(); const int fifo_startup = 3; int lastIntrCount = -1; /*============================================================================= Name: pllDumpSignal(int channel) Abs: Print out latest raw data in channel "channel" =============================================================================*/ int pllDumpSignal(int channel) { switch (channel) { case 0: printf("Channel %d's raw data:\n", channel); printf(" sample 0: "); printf("\n"); break; default: printf("pllDumpSignal: invalid channel: %d\n", channel); return ERROR; } return OK; } /*============================================================================= Name: pllDumpProcessedSignal(int channel) Abs: Print out latest processed data for channel "channel" =============================================================================*/ int pllDumpProcessedSignal(int channel) { printf("Channel %d's processed data:\n", channel); printf(" sample 0: "); printf("\n"); return OK; } /*============================================================================= Name: pllDumpChannel Abs: Debug routine used to look at the contents of a channel's structure Ret: OK =============================================================================*/ int pllDumpChannel(int channel ) { switch (channel) { case 0: printf("Contents of RF channel %d, (range is 0-3), sample 0\n", channel); printf("\n"); pllDumpSignal(channel); pllDumpProcessedSignal(channel); return OK; break; default: printf("pllDumpChannel: unrecognized channel: %d\n", channel); return ERROR; } return OK; } /*============================================================================= Name: pllReport Ret: Driver support routine registered in DRVET structure =============================================================================*/ int pllReport( 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); if ( interest > 0 ) { rc = pllDumpChannel(0); } return interest; } /*============================================================================= Name: pllISR Abs: Executes every time the interrupt that indicates FIFO full arrives Rem: =============================================================================*/ void pllISR () { DEBUG_setDiagnosticBitType(0); intrCount++; if ( intrCount < 0 ) { /* rolled over the max of its signed size */ 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: pllGetIntrCount Abs: Return the number of interrupts that have been processed since boot =============================================================================*/ int pllGetIntrCount() { /* restart the interrupts if something has gone seriously wrong */ if ( intrCount == lastIntrCount ) { pllhw_initialize(); printf( "calling pllhw_initialize()\n" ); } lastIntrCount = intrCount; return intrCount; } /*============================================================================= Name: pllGetScanIOCount Abs: =============================================================================*/ int pllGetScanIOCount() { return scanIoCount; } /*============================================================================= Name: pllTurnOn/OffLED Abs: It should be obvious =============================================================================*/ int pllTurnOnLED() { volatile unsigned char *place; place = (unsigned char *) EPDR; *place = 0xF7; DEBUGPRINT(DP_INFO, drvPllFlag, ("pllTurnOnLED: %d\n", (int) *place)); return OK; } int pllTurnOffLED() { volatile unsigned char *place; place = (unsigned char *) EPDR; *place = 0xFF; DEBUGPRINT(DP_INFO, drvPllFlag, ("pllTurnOffLED: %d\n", (int) *place)); return OK; } /*============================================================================= Name: pllGetIoScanPvt Abs: Used by records that process on I/O Intr. Device support calls this in its get_ioint_info routines at init'n. =============================================================================*/ IOSCANPVT pllGetIoScanPvt() { return pll_ioscanpvt; } /*============================================================================= Name: pllDumpAvgToFile Abs: =============================================================================*/ int pllDumpAvgToFile() { return OK; } /*============================================================================= Name: pllDacqTask Abs: acquire and process the data upon receipt of event from ISR =============================================================================*/ int pllDacqTask() { int rc; epicsEventWaitStatus waitStatus = epicsEventWaitOK; /* Do forever */ while (TRUE) { /* epicsEventShow(waitForData, level);*/ DEBUGPRINT(DP_INFO, drvPllFlag, ("pllDacqTask: %d\n", waitStatus )); /* wait for the ISR to indicate that there is data waiting to be read */ waitStatus = epicsEventWait(waitForData); if (waitStatus == epicsEventWaitOK) { pllReadTrackingData(); /* Process I/O Intr scan records registered to this callback */ scanIoRequest(pll_ioscanpvt); if ( ++scanIoCount < 0) { scanIoCount = 0; numScanIoRollovers++; } } else { if (waitStatus == epicsEventWaitTimeout) { DEBUGPRINT(DP_ERROR, drvPllFlag, ("pllDacqTask: timed out waiting for data to be available to read out of FIFO\n")); /*return ERROR; */ } else if (waitStatus == epicsEventWaitError) { DEBUGPRINT(DP_ERROR, drvPllFlag, ("pllDacqTask: error waiting for data to be available to read out of FIFO\n")); /* return ERROR; */ } else DEBUGPRINT(DP_ERROR, drvPllFlag, ("pllDacqTask: error waiting for data: error unknown: %d\n", waitStatus )); } /* place a small mark at the end of all pre-rtems processing */ DEBUG_clearDiagnosticBitType(0); } return rc; } void setDmaTo24BitByteCount( int b ) { volatile unsigned int *mpark = (volatile unsigned int *) MPARK; /* interrupt disable? */ if ( b ) *mpark |= Mpark_BCR24BIT; else *mpark &= ~Mpark_BCR24BIT; } /*============================================================================= Name: pllInitialise 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 pllInitialise() { int rc; epicsThreadId rcv_id; #ifdef DEBUG_PRINT char sw_version[100]; drvPllFlag=DP_ERROR; /* DP_ERROR */ DEBUGPRINT(DP_INFO, drvPllFlag, ("pllInitialise: hardware initialisation in progress...\n")); /* Write out the version of the software to the console log */ strcpy(sw_version, "PLL_GEN"); DEBUGPRINT(DP_INFO, drvPllFlag, ("pllInitialise: this version of the software was compiled for %s\n", sw_version)); DEBUGPRINT(DP_INFO, drvPllFlag, ("pllInitialise: IRQ1 before: %d\n", *epddr & 2)); DEBUGPRINT(DP_INFO, drvPllFlag, ("pllInitialise: IRQ3 before: %d\n", *epddr & 8)); DEBUGPRINT(DP_INFO, drvPllFlag, ("pllInitialise: IRQ4 before: %d\n", *epddr & 16)); DEBUGPRINT(DP_INFO, drvPllFlag, ("pllInitialise: IRQ5 before: %d\n", *epddr & 32)); #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 on eppa2 (save 6 usec) */ /* 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; DEBUGPRINT(DP_INFO, drvPllFlag, ("pllInitialise: IRQ1 after: %d\n", *epddr &2 )); DEBUGPRINT(DP_INFO, drvPllFlag, ("pllInitialise: IRQ3 after: %d\n", *epddr &8 )); DEBUGPRINT(DP_INFO, drvPllFlag, ("pllInitialise: IRQ4 after: %d\n", *epddr & 16)); DEBUGPRINT(DP_INFO, drvPllFlag, ("pllInitialise: IRQ5 after: %d\n", *epddr & 32)); /* 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. */ /*original *csmr1 = 0x103;*/ /* bit 31: 0000 0000 0000 0000 0000 0001 0000 0011: bit 0 */ *csmr1 = 0x003; /* bit 31: 0000 0000 0000 0000 0000 0000 0000 0011: bit 0 */ /* 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 (PLL_MAX_SAMPLE_SIZE for operational channels) */ /* Initialize our watchdog timer queue */ pllWdTimerQ=epicsTimerQueueAllocate( 1, /* ok to share queue */ epicsThreadPriorityLow); if (pllWdTimerQ==0) { DEBUGPRINT(DP_ERROR, drvPllFlag, ("pllInitialise: error initialising watchdog timer queue\n")); return ERROR; } DEBUGPRINT(DP_INFO, drvPllFlag, ("pllInitialise: watchdog timer queue initialised\n")); waitForData = epicsEventCreate(epicsEventEmpty); DEBUGPRINT(DP_INFO, drvPllFlag, ("pllInitialise: created event sem %d\n", (int) waitForData)); scanIoInit(&pll_ioscanpvt); setDmaTo24BitByteCount( TRUE ); /* Create the task to do data acquisition */ rcv_id = epicsThreadCreate( "pllDacqTask", DACQ_TASK_PRIO, DACQ_TASK_STACK, (EPICSTHREADFUNC)pllDacqTask, 0); DEBUGPRINT(DP_INFO, drvPllFlag, ("pllInitialise: data acquistion task id is %d\n", (int)rcv_id)); /* Connect the interrupt vector to the interrupt service routine */ rc = devConnectInterruptVME(IRQ_VECTOR, pllISR, 0); if (rc != 0) { DEBUGPRINT(DP_FATAL, drvPllFlag, ("pllInitialise: unable to connect to intr vector to ISR. rc = %d\n", rc)); return ERROR; } DEBUGPRINT(DP_INFO, drvPllFlag, ("pllInitialise: intr vector, %x, connected to ISR\n", IRQ_VECTOR)); pllhw_initialize(); DEBUGPRINT(DP_INFO, drvPllFlag, ("pllInitialise: hardware initialisation complete, but interrupts not yet enabled.\n")); /* Initialisation is done. Turn off DP_INFO messages */ #ifdef DEBUG_PRINT drvPllFlag=DP_ERROR; #endif /* force the turnoff of bootp! * * to over-ride the flash configuration, kly-lclsdiag uses bootp/dhcp to force * the internet parameters. see kly-lclsdiag:/etc/dhcpd.conf * unfortunately, the bootp choice is in flash and cannot be overridden. * the DO_BOOTP flag is set to get the diagnostic boot, and then automatically * cleared here to avoid leaving the board in this condition. * this costs a flash burn every boot, but there is no other choice at the moment. */ /* const char *cp = pad_getbenv( dobootp ); if ( cp != NULL && strlen( cp ) > 0 ) { pad_setbenv( dobootpClear ); printf( "forcing %s off\n", dobootp ); } */ /* interrupts on */ pllEnableIntr(); /* kick the first interrupt on in case the adc counter has already counted past the * interrupt count */ pllResetAdc(); return OK; } int pllEnableIntr() { DEBUGPRINT(DP_INFO, drvPllIntrFlag, ("pllEnableIntr: enabling interrupts.\n")); /* Enable the interrupt. Don't do this UNTIL everything is set up */ *epier = 0x04; /* Enable IRQ2 */ return OK; } int pllDisableIntr() { DEBUGPRINT(DP_INFO, drvPllIntrFlag, ("pllEnableIntr: disabling interrupts.\n")); /* Disable the interrupt. */ *epier = 0x00; return OK; } /*============================================================================= Name: pllSoftTrigger 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 pllSoftTrigger(){ *epdr &= ~2; *epdr |= 2; *epdr &= ~2; return OK; } /*============================================================================= Name: pllResetFifo Abs: manually resets FIFO =============================================================================*/ int pllResetFifo(){ *epdr &= ~8; /* 1111 0111 */ *epdr |= 8; /* 0000 1000 */ return OK; } /* DEBUG ROUTINES. NOT PART OF OPERATIONAL CODE */ /*============================================================================= Name: pllDumpEppdr Abs: Debug function =============================================================================*/ int pllDumpEppdr() { printf("eppdr contents: 0x%04x\n", *eppdr); return OK; } /*============================================================================= Name: pllToggleIRQ5 Abs: Debug function =============================================================================*/ int pllToggleIRQ5() { volatile char b; b = *eppdr ^ 32; *epdr = b; return (int) b; } /*============================================================================= Name: pllCauseChipSelect Abs: =============================================================================*/ int pllCauseChipSelect() { printf("csar1_actual has address 0x%08x and contents: %d\n", (unsigned int) csar1_actual, (short) *csar1_actual); return OK; }