/* $Id: devWfPadBpm.c,v 1.1.1.1 2010/05/03 22:17:34 khkim 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 /* for DEBUGPRINT */ #include "drvPadBpm.h" #include "devWfPadBpm.h" #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) #define STATIC #define DEBUG_PROC (1<<0) int readMethod = 0; int devWfPadBpmDebug = 0; static DBADDR dbAddrRarm = {0}; static int gotCalt; #ifdef DEBUG_PRINT int devWfPadBpmFlag = 4; #endif typedef struct DevWfPadDpvt2_ts_ { waveformRecord *rec_p; CALLBACK cb_s; int orarm; unsigned long nusr; /* support user resizing of array */ void (*readFifos)(volatile void*, int, int); } DevWfPadDpvt2_ts, *DevWfPadDpvt2_tps; typedef struct { long number; DEVSUPFUN report; DEVSUPFUN init; DEVSUPFUN init_record; DEVSUPFUN get_ioint_info; DEVSUPFUN read; } WFDSET; LOCAL long init_record(); LOCAL long read_waveform(); WFDSET devWfPadBpm = { 5, NULL, NULL, init_record, NULL, read_waveform }; epicsExportAddress(dset, devWfPadBpm); /* Write RARM field through database */ int devPadWfSetRarm(int val) { long x = val; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devWfPadBpmFlag, ("devPadWfSetRarm: set to %d\n", val)); #endif if ( ! dbAddrRarm.precord ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, devWfPadBpmFlag, ("devPadWfSetRarm: dbAddr not initialized\n")); #endif return -1; } return dbPutField(&dbAddrRarm, DBR_LONG, &x, 1); } static long init_record(waveformRecord *waveform_ps) { DevWfPadDpvt2_tps dpvt_ps; char rarm_name[100]; /* Check DB configuration */ if ( waveform_ps->nelm & 3 ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devWfPadBpmFlag, (DEVSUPNAM": Warning -- NELM should be a multiple of four, truncating\n")); #endif waveform_ps->nelm &= ~3; if ( ! waveform_ps->nelm ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devWfPadBpmFlag, (DEVSUPNAM": Error -- NELM zero\n")); #endif goto egress; } } if ( waveform_ps->ftvl != menuFtypeSHORT ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devWfPadBpmFlag, (DEVSUPNAM": Error -- FTVL should be SHORT (is %i)\n", waveform_ps->ftvl)); #endif goto egress; } if ( VME_IO != waveform_ps->inp.type ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devWfPadBpmFlag, (DEVSUPNAM": Error -- INP is not VME_IO (but type %i)\n", waveform_ps->inp.type)); #endif goto egress; } if ( !(dpvt_ps = waveform_ps->dpvt = malloc(sizeof(*dpvt_ps))) ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devWfPadBpmFlag, (DEVSUPNAM": No memory to allocate DPVT\n")); #endif waveform_ps->pact = 1; return -1; } dpvt_ps->rec_p = waveform_ps; dpvt_ps->orarm = waveform_ps->rarm; dpvt_ps->nusr = waveform_ps->nelm; strcpy(rarm_name, waveform_ps->name); strcat(rarm_name, ".RARM"); if ( dbNameToAddr(rarm_name, &dbAddrRarm) ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devWfPadBpmFlag, (DEVSUPNAM": Unable to resolve dbAddr of RARM field\n")); #endif waveform_ps->pact = 1; return -1; } dpvt_ps->readFifos = drvPadReadFifosColMajor; return 0; egress: waveform_ps->pact = 1; return -1; } static long read_waveform(waveformRecord *waveform_ps) { DevWfPadDpvt2_tps dpvt_ps = waveform_ps->dpvt; long rval = 0; int post = 0; #ifdef NEVER uint32_t then,now; int needsArm = 0; #endif if ( devWfPadBpmDebug & DEBUG_PROC ) #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devWfPadBpmFlag, ("Entering READ\n")); #endif /* 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; } #ifdef NEVER 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 ) #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devWfPadBpmFlag, ("Reading FIFOs took %luus\n",now-then)); #endif if ( coldfEportFlagClr(dpvt_ps->irq_pin) ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devWfPadBpmFlag, (DEVSUPNAM": triggering too fast\n")); #endif 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 ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devWfPadBpmFlag, ("RARM changed (from %i to %i)\n",dpvt_ps->orarm, waveform_ps->rarm)); #endif 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)); #endif if ( devWfPadBpmDebug & DEBUG_PROC ) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, devWfPadBpmFlag, ("Leaving READ\n")); #endif } return rval; }