/* $Id: devWfPadBpm.c,v 1.13 2008/01/30 05:44:12 till Exp $ */ /*============================================================================= Name: devWfPadBpm.c Abs: Device support for pumping data of a 4-channel PAD into a single waveform. Auth: 10-oct-2006, Till Straumann (TSS) Rev: -----------------------------------------------------------------------------*/ #if 0 #include "copyright_SLAC.h" /* SLAC copyright comments */ #endif /*----------------------------------------------------------------------------- Mod: (see CVS log) =============================================================================*/ #include #include #include #ifdef BASE_HAS_NO_NTP_PATCH #include #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* #include */ #include #include #include #include #include #define DEVSUPNAM "devWfPadBpm" /* Align up to next multiple of 8. * We strictly only need multiples of 4 (4 channels) * but making sure we always have an even number of * samples makes storing the row-major format easier */ #define CHANNEL_NO_ALIGN(x) (((x)+7) & ~7) /* Which chip select we are using - this is setup by the BSP * but not published :-( */ #define CS1_BASE_ADDR 0x30000000 /* Direct access to the LAN9118 FIFO -- an ugly hack * but it's much faster... */ #define LAN9118_FIFO_HACK ((volatile void*)0x31000800) /* Just a name for sake of readability */ #define CS1 1 /* 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 #define STATIC #define DEBUG_PROC (1<<0) #define DEBUG_TIMING (1<<1) #define DEBUG_IRQ (1<<3) int devWfPadBpmDebug = DEBUG_IRQ | DEBUG_TIMING; int devWfPadBpmIrqs = 0; int readMethod = 0; typedef int16_t __attribute__ ((may_alias)) int16_t_a; typedef uint32_t __attribute__ ((may_alias)) uint32_t_a; epicsThreadId devWfPadThread = 0; static DBADDR dbAddrRarm = {0}; static epicsThreadId drvPadStreamInit(); static int gotCalt; typedef struct DevWfPadDpvt_ts_ { waveformRecord *rec_p; CALLBACK cb_s; int irq_pin; int trig_pin; volatile int irq; int orarm; epicsEventId sync; unsigned long nusr; /* support user resizing of array */ void (*readFifos)(volatile void*, int, int); } DevWfPadDpvt_ts, *DevWfPadDpvt_tps; /* Forward Declarations */ static void isr(void *arg_p); static void padThread(void *arg); /* Little Helpers */ static void trigRaise(int trig_pin) { coldfEportEpdrSet(trig_pin, 1); } static void disarm(DevWfPadDpvt_tps dpvt_ps) { /* 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. */ } /* Write RARM field through database */ int devPadWfSetRarm(int val) { long x = val; if ( ! dbAddrRarm.precord ) { epicsPrintf("devPadWfSetRarm: dbAddr not initialized\n"); return -1; } return dbPutField(&dbAddrRarm, DBR_LONG, &x, 1); } static void arm(DevWfPadDpvt_tps dpvt_ps, int doSwTrig) { int intEnable = 1; /* to keep things simple, we disable interrupts here * so the ISR doesn't change EPORT bits under our feet. */ coldfEportIntDisable(dpvt_ps->irq_pin); /* clear old flag set by any pre-arm triggers */ coldfEportFlagClr(dpvt_ps->irq_pin); /* clear old CAL flag */ drvPadAfeGotCalt(1); /* release trigger pin */ coldfEportEpdrSet(dpvt_ps->trig_pin, 0); if ( doSwTrig ) { intEnable = ( 0 == drvPadAdcTrigger(dpvt_ps->trig_pin, dpvt_ps->irq_pin) ); if ( !intEnable ) { /* no; it appears above we cleared the interrupt * that caused this acquisition. * just fake an IRQ here. */ dpvt_ps->irq = 1; /* signal IRQ */ callbackRequestProcessCallback(&dpvt_ps->cb_s, priorityHigh, dpvt_ps->rec_p); /* If we just signalled an IRQ manually then we must not * enable interrupts (arm()) */ } } if ( intEnable ) coldfEportIntEnable(dpvt_ps->irq_pin); } static void * getdata(void *packBuffer, int idx, int nsamples, int endianLittle, int colMajor, void *uarg) { assert ( 0 == ( nsamples & 1 ) ); if ( colMajor ) drvPadReadFifosColMajor( LAN9118_FIFO_HACK, nsamples * 4, endianLittle ); else drvPadReadFifosRowMajor( LAN9118_FIFO_HACK, nsamples * 4, endianLittle ); return 0; } static long report(int interest) { epicsPrintf(DEVSUPNAM": PAD raw data into a single waveform\n"); return 0; } static long init(int after) { extern void Timer_initialize(); if ( !after ) Timer_initialize(); return 0; } static void isr(void *arg_p) { DevWfPadDpvt_tps info_ps = arg_p; coldfEportIntDisable(info_ps->irq_pin); /* Clear interrupt here so we can detect a trigger overrun */ coldfEportFlagClr(info_ps->irq_pin); /* Check and clear CAL trigger flag */ gotCalt = drvPadAfeGotCalt(1); /* 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 | * ______________________________| * * SW-TRIG: -------- * | * 'armed' ISR |disarmed * _____________________________________| */ trigRaise(info_ps->trig_pin); if ( (devWfPadBpmDebug & DEBUG_IRQ) > 0 ) { epicsInterruptContextMessage(DEVSUPNAM": Interrupt\n"); } devWfPadBpmIrqs++; epicsEventSignal(info_ps->sync); } static long init_record(void *record_p) { waveformRecord *waveform_ps = record_p; DevWfPadDpvt_tps dpvt_ps; char rarm_name[100]; /* Check DB configuration */ if ( waveform_ps->nelm & 3 ) { epicsPrintf(DEVSUPNAM": Warning -- NELM should be a multiple of four, truncating\n"); waveform_ps->nelm &= ~3; if ( ! waveform_ps->nelm ) { epicsPrintf(DEVSUPNAM": Error -- NELM zero\n"); goto egress; } } if ( waveform_ps->ftvl != menuFtypeSHORT ) { epicsPrintf(DEVSUPNAM": Error -- FTVL should be SHORT (is %i)\n", waveform_ps->ftvl); goto egress; } if ( VME_IO != waveform_ps->inp.type ) { epicsPrintf(DEVSUPNAM": Error -- INP is not VME_IO (but type %i)\n", waveform_ps->inp.type); goto egress; } if ( !(dpvt_ps = waveform_ps->dpvt = malloc(sizeof(*dpvt_ps))) ) { epicsPrintf(DEVSUPNAM": No memory to allocate DPVT\n"); waveform_ps->pact = 1; return -1; } dpvt_ps->rec_p = waveform_ps; dpvt_ps->irq_pin = IRQ_PIN; dpvt_ps->trig_pin = TRIG_PIN; dpvt_ps->irq = 0; dpvt_ps->orarm = waveform_ps->rarm; dpvt_ps->nusr = waveform_ps->nelm; if ( ! (dpvt_ps->sync = epicsEventCreate(0)) ) { epicsPrintf(DEVSUPNAM": Unable to create EPICS event\n"); waveform_ps->pact = 1; return -1; } if ( devWfPadThread ) { epicsPrintf(DEVSUPNAM": Only ONE raw waveform record can be present\n"); waveform_ps->pact = 1; return -1; } /* Start task that does the interrupt work */ devWfPadThread = epicsThreadCreate( "padT", epicsThreadPriorityMax, epicsThreadGetStackSize(epicsThreadStackMedium), padThread, waveform_ps); if ( 0 == drvPadStreamInit(waveform_ps) ) { epicsPrintf(DEVSUPNAM": Warning - no streaming mode available\n"); } strcpy(rarm_name, waveform_ps->name); strcat(rarm_name, ".RARM"); if ( dbNameToAddr(rarm_name, &dbAddrRarm) ) { epicsPrintf(DEVSUPNAM": Unable to resolve dbAddr of RARM field\n"); waveform_ps->pact = 1; return -1; } { const char *x = waveform_ps->inp.value.vmeio.parm; if ( x && strstr(x, "RowMajor") ) dpvt_ps->readFifos = drvPadReadFifosRowMajor; else dpvt_ps->readFifos = drvPadReadFifosColMajor; } /* Setup Chip Select */ coldfCsSetup(CS1, CSELECT_FLAG_AA | CSELECT_FLAG_WS(1)); coldfEportIntDisable_inl(dpvt_ps->irq_pin); /* Setup trigger */ /* Configure eport as output, hold trigger high */ trigRaise(dpvt_ps->trig_pin); coldfEportSetup(dpvt_ps->trig_pin, EPORT_OUTPUT); /* Setup Interrupt */ /* Configure EPORT for raising edge interrupt; clear any pending IRQS */ coldfEportFlagClr(dpvt_ps->irq_pin); coldfEportSetup(dpvt_ps->irq_pin, IRQ_EDGE); devConnectInterruptVME(64 + dpvt_ps->irq_pin, isr, dpvt_ps); if ( dpvt_ps->orarm ) { arm(dpvt_ps, -1 == dpvt_ps->orarm); } return 0; egress: waveform_ps->pact = 1; return -1; } #if 0 static long get_ioint_info(int cmd, struct dbCommon *record_ps, IOSCANPVT *pvt_ps) { DevWfPadDpvt_tps dpvt_ps = record_ps->dpvt; *pvt_ps = *dpvt_ps->scan_p; return 0; } #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) { volatile int16_t_a *dst_p = dst_p_v; volatile int16_t_a *src_p = (volatile int16_t_a *)CS1_BASE_ADDR; assert ( (n & 3) == 0 ); /* 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]; if ( ! do_bswap ) { while ( n >= 8 ) { CPY4W; CPY4W; n-=8; } /* n must be multiple of 4 */ while ( n > 0 ) { CPY4W; n-=4; } } else { while ( n >= 8 ) { CPY4WS; CPY4WS; n-=8; } /* n must be multiple of 4 */ while ( n > 0 ) { CPY4WS; n-=4; } } } #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; 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); } static long read_waveform(void *record_p) { waveformRecord *waveform_ps = record_p; DevWfPadDpvt_tps dpvt_ps = waveform_ps->dpvt; long rval = 0; int post = 0; uint32_t then,now; int needsArm = 0; if ( devWfPadBpmDebug & DEBUG_PROC ) epicsPrintf("Entering READ\n"); /* Record can be processed due to a PUTF or an interrupt (or both) */ if ( waveform_ps->nord && waveform_ps->nord != dpvt_ps->nusr ) { unsigned long a; /* user may have resized the array (by writing an array of the * desired size to 'VAL'. * Omit setting 'nusr' if 'nord' is zero - we could have written * zero as a result of a data acq. failure. */ dpvt_ps->nusr = waveform_ps->nord; /* must always be a multiple of eight; bump up */ a = CHANNEL_NO_ALIGN(dpvt_ps->nusr); if ( a > waveform_ps->nelm ) { a = waveform_ps->nelm; } if ( a != dpvt_ps->nusr ) { waveform_ps->nord = a; /* clear excess elements */ while ( --a >= dpvt_ps->nusr ) { ((int16_t*)waveform_ps->bptr)[a] = 0; } dpvt_ps->nusr = waveform_ps->nord; } /* It seems the waveform record doesn't post monitors if users change the size * of the array. Therefore we post here anyways (whether we aligned or not). */ post |= 1; } if ( dpvt_ps->irq ) { extern uint32_t Read_timer(); dpvt_ps->irq = 0; then = Read_timer(); dpvt_ps->readFifos( waveform_ps->bptr, dpvt_ps->nusr, 0 ); now = Read_timer(); if ( devWfPadBpmDebug & DEBUG_TIMING ) epicsPrintf("Reading FIFOs took %luus\n",now-then); if ( coldfEportFlagClr(dpvt_ps->irq_pin) ) { epicsPrintf(DEVSUPNAM": triggering too fast\n"); recGblSetSevr(waveform_ps, SCAN_ALARM, MAJOR_ALARM); waveform_ps->nord = 0; post |= 1; rval = -1; } else { /* SUCCESS */ if ( waveform_ps->nord != dpvt_ps->nusr ) post |= 1; waveform_ps->nord = dpvt_ps->nusr; } if ( waveform_ps->rarm > 1 ) { /* auto-rearm */ needsArm = 1; } else { /* disarm (leave IRQ disabled) */ waveform_ps->rarm = dpvt_ps->orarm = 0; post |= 2; } } /* handle rearm and soft trigger */ if ( dpvt_ps->orarm != waveform_ps->rarm ) { if ( devWfPadBpmDebug & DEBUG_PROC ) epicsPrintf("RARM changed (from %i to %i)\n",dpvt_ps->orarm, waveform_ps->rarm); if ( -1 == waveform_ps->rarm ) { /* Soft Trigger */ needsArm = 2; } else { if ( waveform_ps->rarm ) { needsArm = 1; } else { disarm(dpvt_ps); } } dpvt_ps->orarm = waveform_ps->rarm; } if ( needsArm ) { arm(dpvt_ps, needsArm > 1); } if ( post & 1 ) db_post_events(waveform_ps, &waveform_ps->nord, (DBE_VALUE|DBE_LOG)); if ( post & 2 ) db_post_events(waveform_ps, &waveform_ps->rarm, (DBE_VALUE|DBE_LOG)); if ( devWfPadBpmDebug & DEBUG_PROC ) epicsPrintf("Leaving READ\n"); return rval; } struct { long number; DEVSUPFUN report; DEVSUPFUN init; DEVSUPFUN init_record; DEVSUPFUN get_ioint_info; DEVSUPFUN read; } devWfPadBpm = { 5, report, init, init_record, 0 /* get_ioint_info */, read_waveform }; epicsExportAddress(dset, devWfPadBpm); /* Stuff below should eventually move to a separate module */ #include #include #undef DEBUG void padUdpThread(void *arg); extern IpBscIf lanIpIf; static long drvPadStreamReport(int level) { epicsPrintf("PAD Stream Support for Communication with BPM Processor\n"); epicsPrintf("over 2nd Ethernet Interface\n"); if ( level > 1 ) { epicsPrintf("Dumping Interface Statistics\n"); drvLan9118DumpStats(lanIpBscIfGetDrv(lanIpIf), 0); } return 0; } static int start_stop_cb(int start, void *uarg) { return devPadWfSetRarm(start ? 2 : 0); } static epicsThreadId drvPadStreamInit() { char *padslot; int channel; if ( padStreamInitialize(lanIpIf, start_stop_cb, 0) ) { return 0; } if ( !(padslot = getenv("PADSLOT")) ) { epicsPrintf("Unable to start UDP listener; enviroment variable PADSLOT not set\n"); return 0; } channel = atoi(padslot); epicsPrintf("Starting UDP listener for PAD channel %i\n", channel); return epicsThreadCreate( "padUdpListener", epicsThreadPriorityScanHigh, epicsThreadGetStackSize(epicsThreadStackMedium), padUdpThread, (void*)channel); } static void padThread(void *arg) { waveformRecord *waveform_ps = arg; DevWfPadDpvt_tps dpvt_ps = waveform_ps->dpvt; PadDataKind kind; int isCal, status; while (1) { epicsEventWait( dpvt_ps->sync ); kind = PadDataBpm; /* A CAL event ? */ if ( (isCal = gotCalt) ) { switch ( AFE_REG_CSR_CALM_GET(drvPadAfeRegCache[AFE_REG_CSR]) ) { case AFE_REG_CSR_CALM_RED: kind = PadDataCalRed; break; case AFE_REG_CSR_CALM_GRN: kind = PadDataCalGrn; break; case AFE_REG_CSR_CALM_BOTH: /* TODO -- handle this case */ break; case AFE_REG_CSR_CALM_NONE: default: break; } } if ( waveform_ps->rarm == 10 ) { if ( PadDataBpm == kind ) status = padStreamSim(0); else status = 0; /* ignore cal data -- otherwise we * might be unable to keep up if they * are triggering fast */ } else { status = padStreamSend(getdata, kind, 0, 0); } /* Toggle CAL mode; if we're not in 'soft toggle' mode * then nothing happens. */ drvPadCalToggle(AFE_CS); /* If we can't send streaming data then * 'ordinary' record support is engaged. */ if ( status ) { dpvt_ps->irq = 1; /* signal IRQ */ 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); } /* stream not active; process record */ callbackRequestProcessCallback(&dpvt_ps->cb_s, priorityHigh, dpvt_ps->rec_p); } else { /* Clear interrupt here so we can detect a trigger overrun */ coldfEportFlagClr(dpvt_ps->irq_pin); /* release trigger pin */ coldfEportEpdrSet(dpvt_ps->trig_pin, 0); coldfEportIntEnable(dpvt_ps->irq_pin); } } } void padUdpThread(void *arg) { int err; /* use default port and channel number passed as arg */ if ( (err = padUdpHandler(0, (int)arg)) ) { epicsPrintf("ERROR: padUdpHandler() aborted: %s\n", strerror(-err)); } }