/* $Id$ */ #define _ISOC99_SOURCE #undef __STRICT_ANSI__ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __PPC__ uint32_t totTimeMax = 0; uint32_t totTimeAvg = 0; #endif #define ZERO_PT_5 0.5 int mpsDiagMsg = 0; static int setStatFromErr(dbCommon *prec, int err) { int sevr = NO_ALARM; if ( err > 0 ) { sevr = err & 0xff; recGblSetSevr(prec, ((err>>8) & 0xff), sevr); } else if ( err < 0 ) { recGblSetSevr(prec, READ_ALARM, INVALID_ALARM); sevr = INVALID_ALARM; } return sevr; } /* u,v,correction,x,y,tmit */ #define RES_U 0 #define RES_V 1 #define RES_C 2 #define RES_X 3 #define RES_Y 4 #define RES_T 5 /* * The bpmResultsxx[] and bpmStatus[] etc. variables * are written/computed by drvBpmCalcPos * to be read by record device support. */ #define NUM_RES 6 BpmNumber bpmResults [MAX_BPM][NUM_RES]; float bpmResultsScl[MAX_BPM][NUM_RES]; float bpmResultsOff[MAX_BPM][NUM_RES] = { {0.} }; BsaAlarmLimitsStruct bpmAlarmLimits[MAX_BPM][NUM_RES] = { { {0.} } }; /* Don't bother locking the alarm limits. They are supposed * to change extremely rarely and a false alarm just during * a change seems acceptable. * If there are problems simply define this... */ #define DO_LOCK_ALARM_LIMITS DrvBpmMpsThresVal drvBpmMpsLolo[MAX_BPM][NUM_MPSTHRES] = { {0.} }; DrvBpmMpsThresVal drvBpmMpsHihi[MAX_BPM][NUM_MPSTHRES] = { {0.} }; int bpmStatus[MAX_BPM] = {0}; TimeStamp bpmTimeStamp[MAX_BPM] = { {0} }; FcomBlob bpmBlobs[MAX_BPM] = { {{0}} }; BsaChannel bpmBsaChannels[MAX_BPM][NUM_RES] = { {0} }; FcomGroup bpmGroup = 0; FcomGroup bpmGroupAlt = 0; volatile uint32_t drvBpmFcomAddGroupFailed = 0; volatile int drvBpmFcomAddGroupError = 0; volatile uint32_t drvBpmFcomPutGroupFailed = 0; volatile int drvBpmFcomPutGroupError = 0; volatile uint32_t drvBpmFcomPurgedGroups = 0; volatile uint32_t drvBpmFcomPostedGroups = 0; /* Status bitmasks for MPS -- the individual words * MPS_X and MPS_Y must be interrupt-locked to keep * them thread-safe. * MPS_BP is the 'bypass mask'. */ /* NOTE: EPICS records rely on MPS_BP .. MPS_FLT * being 0..5, respectively. */ #define MPS_BP 0 /* bypass mask */ #define MPS_LBP 1 /* low-beam current bypasses */ #define MPS_X_F 2 /* latched failed-X */ #define MPS_Y_F 3 /* latched failed-Y */ #define MPS_T_F 4 /* latched failed-TMIT */ #define MPS_FLT 5 /* actual state */ #define MPS_X 6 #define MPS_Y 7 /* code relies on MPS_Y = MPS_X + 1 */ #define MPS_NOB 8 /* got TMIT pulse but below thresh */ #define MPS_B 9 /* got TMIT pulse above thresh */ #define MPS_NUM 10 volatile uint32_t drvBpmMps[MPS_NUM] = {0}; IOSCANPVT drvBpmMpsScan; /* Cached rotation matrix coefficients */ #define ROT_UU 0 #define ROT_UV 1 #define ROT_VU 2 #define ROT_VV 3 BpmNumber bpmRot[MAX_BPM][PADRPLY_STRM_NCHANNELS]; /* Cached inter-axes angle parameter */ BpmNumber bpmCosPsi[MAX_BPM]; BpmNumber bpmSinPsi[MAX_BPM]; epicsSpinId bpmChannelLocks[MAX_BPM]; epicsSpinId mpsSpinLockId; epicsSpinId bpmFcomSpinLockId; #ifndef DO_LOCK_ALARM_LIMITS #define LOCK_ALARM_LIMITS(c) do {} while(0) #define UNLOCK_ALARM_LIMITS(c) do {} while(0) #else #define LOCK_ALARM_LIMITS(c) do { bpmChannelSpinLock( c ); } while(0) #define UNLOCK_ALARM_LIMITS(c) do { bpmChannelSpinUnlock( c );} while(0) #endif #if 0 /* Various cached parameters */ float bpmParms[MAX_BPM][NUM_BPM_PARMS] = { {0.} }; #endif #if 0 static BpmRawAmplitudes ampFree = 0; static __inline__ volatile BpmRawAmplitudes ampAlloc(unsigned chnl) { int key; BpmRawAmplitudes rval; key = epicsInterruptLock(); if ( (rval = ampFree) ) ampFree = *(BpmRawAmplitudes*)ampFree; epicsInterruptUnlock(key); return (volatile BpmRawAmplitudes)rval; } static __inline__ void ampFreeNonl(BpmRawAmplitudes a) { *(BpmRawAmplitudes*)a = ampFree; ampFree = a; } /* The cooked (and calibrated) strip signals */ BpmRawAmplitudes bpmRawAmplitudes[MAX_BPM] = { 0 }; static BpmRawAmplitudesRec ampBuffers[MAX_BPM*PADRPLY_STRM_NCHANNELS]; static __inline__ void ampInit() { int i; /* Don't lock - we assume nobody is using this yet */ for ( i=0; i 32 #error "MAX_BPM too big for current bitmask implementation" #endif volatile uint32_t bpmRawAmplitudesPending = 0; /* Operate on the bitmask */ /* Check if new data for 'channel' is pending */ static __inline__ int bpmRawAmplitudePending(unsigned channel) { int rval, key; key = epicsInterruptLock(); rval = (1<status |= BPM_AMPL_SKIPPED; } else { /* mark as old */ oa->status |= BPM_AMPL_OLD; oa = a; } ampFreeNonl(oa); } bpmRawAmplitudesPending |= msk; epicsInterruptUnlock(key); return rval ? 1 : 0; } #else BpmRawAmplitudesRec ampBuffers[MAX_BPM]; static __inline__ void ampInit() { int i; memset(ampBuffers, 0, sizeof(ampBuffers)); for ( i=0; isecPastEpoch; bpmBlobs[channel].fc_tsLo = p_ts->nsec; addStatus = fcomAddGroup(grp, &bpmBlobs[channel]); if ( (FCOS_POST & add_post) ) { postStatus = fcomPutGroup(grp); newGrp = bpmFcomGroupMustAlloc("bpmFcomPost"); grp = 0; } bpmFcomSpinLock(); if ( addStatus ) { drvBpmFcomAddGroupFailed++; drvBpmFcomAddGroupError = addStatus; } if ( !newGrp ) { /* added -- try to restore the bpmGroup */ if ( ! bpmGroup ) { bpmGroup = grp; grp = 0; } /* else it has been purged in the meanwhile */ } else { if ( postStatus ) { drvBpmFcomAddGroupFailed++; drvBpmFcomAddGroupError = postStatus; } if ( ! bpmGroup ) { bpmGroup = newGrp; newGrp = 0; } else { /* has been purged already; try to keep around... */ if ( ! bpmGroupAlt ) { bpmGroupAlt = newGrp; newGrp = 0; } } } bpmFcomSpinUnlock(); if ( newGrp ) { fcomFreeGroup(newGrp); } if ( grp ) { fcomFreeGroup(grp); } } } void bpmWatchdog_unlocked(int channel, int kind, void *closure) { if ( channel < 0 || channel >= MAX_BPM ) return; if ( kind < 0 || kind >= NUM_DATA_KINDS ) return; if ( PadDataBpm == kind ) ampBuffers[channel].status = BPM_AMPL_TIMEDOUT; } volatile DrvPadUdpCommHWTime drvBpmMaxProcessingTime = 0; /* from 'start' command (FIDP) to end of position calc */ volatile DrvPadUdpCommHWTime drvBpmMaxTotalTime = 0; long drvBpmCalcPos( unsigned c, BpmNumber u, BpmNumber v, BpmNumber t, uint32_t raw_status, BpmNumber pinc, TimeStamp *p_ts ) { int ovrErr = 0; int rval = 0; DrvPadUdpCommHWTime totTime, now; BpmNumber uscl,vscl,uoff,voff,xraw,yraw,x,y; BpmNumber xscl, yscl, tscl, xoff, yoff, toff; BpmNumber *prot; int newStatus; BsaStat bsaStat; BsaSevr bsaSevr; if ( c >= MAX_BPM ) { errlogPrintf("drvBpmCalcPos - invalid channel # %i (INPL)\n", c); return -1; } if ( raw_status & BPM_AMPL_RNG_MSK(-1) ) { newStatus = ENC_ST_SVR(READ_ALARM, INVALID_ALARM); bpmBlobs[c].fc_stat = FC_STAT_BPM_INVAL; } else if ( raw_status & (BPM_AMPL_OVR_MSK) ) { newStatus = ENC_ST_SVR(HIHI_ALARM, MAJOR_ALARM ); bpmBlobs[c].fc_stat = FC_STAT_BPM_OVRFL; ovrErr = 1; } else if ( raw_status & (BPM_AMPL_UDR_MSK) ) { newStatus = ENC_ST_SVR(LOLO_ALARM, INVALID_ALARM); bpmBlobs[c].fc_stat = FC_STAT_BPM_REFLO; /* set TMIT for underrange channels */ bpmChannelSpinLock( c ); bpmResults[c][RES_T] = t*bpmResultsScl[c][RES_T] + bpmResultsOff[c][RES_T]; /* Sonya prefers the old behavior where X/Y just freeze the last * valid beam position... bpmResults[c][RES_X] = bpmResults[c][RES_Y] = NAN; */ bpmStatus[c] = newStatus; bpmChannelSpinUnlock( c ); epicsSpinLock( mpsSpinLockId ); drvBpmMps[MPS_NOB] &= ~ (1< drvBpmMpsLolo[c][MPSTHRES_T] ) drvBpmMps[MPS_B] &= ~ (1< drvBpmMpsLolo[c][MPSTHRES_X] ) drvBpmMps[MPS_X] &= ~ (1< drvBpmMpsLolo[c][MPSTHRES_Y] ) drvBpmMps[MPS_Y] &= ~ (1< drvBpmMaxProcessingTime ) { drvBpmMaxProcessingTime = totTime; } totTime = now - fidTimeBaseline; if ( totTime > drvBpmMaxTotalTime ) { drvBpmMaxTotalTime = totTime; } return rval; } /* Check for MPS violations during last cycle and * re-arm this check. This routine is intended to * be executed from the fiducial callback. */ uint32_t drvBpmMpsPoll(void) { uint32_t rval, res_x, res_y, res_nob, res_b, beam_and_not_bypassed; /* Note that we are not protected against modifications of * the bypass-mask by devBusMapped device support (because * that devsup doesn't interrupt-lock) but we assume * that such modifications (changing a 32-bit bitmask) * can never happen in an inconsistent way (flipping a * particular bit does not affect other bits) */ epicsSpinLock( mpsSpinLockId ); res_x = drvBpmMps[MPS_X]; res_y = drvBpmMps[MPS_Y]; res_nob = drvBpmMps[MPS_NOB]; res_b = drvBpmMps[MPS_B]; /* Reset */ drvBpmMps[MPS_X] = drvBpmMps[MPS_Y] = drvBpmMps[MPS_NOB] = drvBpmMps[MPS_B] = (uint32_t)-1; epicsSpinUnlock( mpsSpinLockId ); /* bits in res_b, res_nob are set for BPMs where TMIT was not updated */ /* *negative* logic: * bypassed OR ( received TMIT AND NOT beam AND low-TMIT bypasses ) */ beam_and_not_bypassed = drvBpmMps[MPS_BP] & ( res_nob | drvBpmMps[MPS_LBP] ); /* 'negative' logic: 1 == 'FAILED' */ rval = (res_x | res_y | res_b) & beam_and_not_bypassed; if ( rval ) { /* only latch first fault */ if ( ! (drvBpmMps[MPS_X_F] | drvBpmMps[MPS_Y_F] | drvBpmMps[MPS_T_F]) ) { if ( mpsDiagMsg ) { printf("Setting:\n"); printf("res_x: 0x%08"PRIx32"\n",res_x); printf("res_y: 0x%08"PRIx32"\n",res_y); printf("res_nob: 0x%08"PRIx32"\n",res_nob); printf("res_b: 0x%08"PRIx32"\n",res_b); } drvBpmMps[MPS_X_F] = res_x & beam_and_not_bypassed; drvBpmMps[MPS_Y_F] = res_y & beam_and_not_bypassed; drvBpmMps[MPS_T_F] = res_b & beam_and_not_bypassed; scanIoRequest(drvBpmMpsScan); } } else { if ( (drvBpmMps[MPS_X_F] | drvBpmMps[MPS_Y_F] | drvBpmMps[MPS_T_F]) ) { /* If there is beam in at least one BPM and all * non-bypassed BPMs with beam are good then * we can reset the latched state. */ if ( 0 != ~res_b ) { if ( mpsDiagMsg ) { printf("Clearing:\n"); printf("res_x: 0x%08"PRIx32"\n",res_x); printf("res_y: 0x%08"PRIx32"\n",res_y); printf("res_nob: 0x%08"PRIx32"\n",res_nob); printf("res_b: 0x%08"PRIx32"\n",res_b); } drvBpmMps[MPS_X_F] = drvBpmMps[MPS_Y_F] = drvBpmMps[MPS_T_F] = 0; scanIoRequest(drvBpmMpsScan); } } } drvBpmMps[MPS_FLT] = rval; return rval; } static long generic_cb(aiRecord *pai, TimeStamp *pts, unsigned c, unsigned s) { uint32_t st = 0; uint32_t umsk = 0, omsk = 0; long err = 0; if ( s < NumberOf(ampBuffers[0].vals) ) { /* They want to read stripline signals */ bpmChannelSpinLock( c ); /* get cached copies */ st = ampBuffers[c].status; pai->val = ampBuffers[c].vals[s]; *pts = ampBuffers[c].timeStamp; bpmChannelSpinUnlock( c ); omsk = BPM_AMPL_OVR(0) << (s % BPM_NUM_VALS); umsk = BPM_AMPL_UDR(0) << (s % BPM_NUM_VALS); err = drvBpm_map_status(st, omsk, umsk); /* NO aslo/aoff scaling on the raw signals */ err |= STAT_NO_CVT; } else if ( (s-=NumberOf(ampBuffers[0].vals)) < NumberOf(bpmResults[0]) ) { BpmNumber newScl = (0. == pai->aslo ? 1.0 : pai->aslo); BpmNumber newOff = pai->aoff; switch ( pai->linr ) { case menuConvertNO_CONVERSION: break; case menuConvertLINEAR: case menuConvertSLOPE: if ( 0. != pai->eslo ) { newScl *= pai->eslo; newOff *= pai->eslo; } newOff += pai->eoff; break; default: assert( 0 == "breakpoint table conversion not implemented"); break; } /* cache possibly new values for ASLO/AOFF */ bpmChannelSpinLock( c ); bpmResultsScl[c][s] = newScl; bpmResultsOff[c][s] = newOff; pai->val = bpmResults[c][s]; err = bpmStatus[c]; *pts = bpmTimeStamp[c]; bpmChannelSpinUnlock( c ); /* A hack; if we are reading TMIT flagged as underflown * then de-escalate from INVALID_ALARM to MAJOR_ALARM */ if ( RES_T == s && ENC_ST_SVR(LOLO_ALARM, INVALID_ALARM) == err ) { /* If llsv is set then de-escalate to OK so that the database * limits are used. */ if ( pai->llsv ) err = 0; else err = ENC_ST_SVR(LOLO_ALARM, MAJOR_ALARM); } /* PINC was not scaled by low-level code - all others were */ if ( RES_C != s ) err |= STAT_NO_CVT; #ifdef __PPC__ if ( 5 == s ) { uint32_t tbl; __asm__ __volatile__ ("mftb %0":"=r"(tbl)); tbl -= ampBuffers[c].timeStamp.nsec; if ( tbl > totTimeMax ) totTimeMax = tbl; /* to read AVG, divide by 16 */ totTimeAvg += (tbl - (totTimeAvg >> 4)); } #endif } else { return -1; } return err; } long drvBpm_read_raw_ai(aiRecord *pai) { return drvBpm_read_raw_ai_helper(pai, generic_cb); } long drvBpm_read_raw_stat(longinRecord *pli) { unsigned c = pli->inp.value.vmeio.card; /* unsigned s = pli->inp.value.vmeio.signal; */ bpmChannelSpinLock( c ); pli->val = ampBuffers[c].status; /* get cached copy */ if ( epicsTimeEventDeviceTime == pli->tse ) pli->time = ampBuffers[c].timeStamp; bpmChannelSpinUnlock( c ); if ( pli->time.secPastEpoch == 0 ) { epicsTimeGetCurrent(&pli->time); } return 0; } long drvBpm_init_raw_stat(void *record_p) { longinRecord *li_ps = record_p; if ( VME_IO != li_ps->inp.type ) { errlogPrintf("drvBpm_init_raw_stat(%s): INP must be VMEIO\n", li_ps->name); goto egress; } return 0; egress: li_ps->pact = TRUE; return -1; } long drvBpm_read_raw_ai_helper(aiRecord *pai, DrvBpm_read_raw_ai_cb cb) { unsigned c = pai->inp.value.vmeio.card; unsigned s = pai->inp.value.vmeio.signal; TimeStamp ts; int err = 0; if ( c >= MAX_BPM ) { pai->pact = TRUE; errlogPrintf("drvBpm_read_raw_ai_helper (rec: %s) -- invalid channel # %i\n", pai->name, c); return -1; } ts.secPastEpoch = 0; err = cb(pai, &ts, c, s); /* Should we set the timestamp ? */ if ( epicsTimeEventDeviceTime == pai->tse ) { if ( ts.secPastEpoch ) pai->time = ts; else epicsTimeGetCurrent(&pai->time); } if ( -1 == err ) { pai->pact = TRUE; errlogPrintf("drvBpm_read_raw_ai_helper (rec: %s) -- invalid signal # %i\n", pai->name, s); return -1; } if ( ! (STAT_NO_CVT & err) ) { /* Linear transformation just with ASLO/AOFF */ if ( pai->aslo ) pai->val *= pai->aslo; pai->val += pai->aoff; } pai->udf = isnan(pai->val); if ( INVALID_ALARM == setStatFromErr((dbCommon*)pai, err) ) pai->udf = TRUE; return 2; } /* For now there is no need to make this public */ typedef struct DevAiRawBpmDpvt_ts_ { struct DrvPadUdpCommDpvtRec_ drvPadUdpCommPart; } DevAiRawBpmDpvt_ts, *DevAiRawBpmDpvt_tps; static long init_bpm_results_cb(aiRecord *ai_ps, unsigned c, unsigned s) { int sres = s - NumberOf(ampBuffers[0].vals); if ( c >= MAX_BPM ) { errlogPrintf("drvBpm_init_raw_ai(%s): INP #C must be < %u\n", ai_ps->name, MAX_BPM); return -1; } if ( sres < 0 ) return 0; if ( sres >= NUM_RES ) { errlogPrintf("drvBpm_init_raw_ai(%s): VMEIO INP 'Signal' must be < %u\n", ai_ps->name, NUM_RES + (s-sres) ); return -1; } /* initializei FCOM blob headers */ if ( RES_X == sres ) { /* First make sure we have matching layouts: */ if ( sizeof(bpmResults[c][sres]) != sizeof( ((FcomBlobRef)0)->fcbl_bpm_X ) ) { errlogPrintf("drvBpm_init_raw_ai(%s): FATAL PROGRAMMING ERROR -- internal data size doesn't match chosen FCOM type\n", ai_ps->name); errlogFlush(); abort(); } if ( &((FcomBlobRef)0)->fcbl_bpm_X - &((FcomBlobRef)0)->fc_flt[0] != RES_X - RES_X ) { errlogPrintf("drvBpm_init_raw_ai(%s): FATAL PROGRAMMING ERROR -- internal layout doesn't match chosen FCOM layout (X)\n", ai_ps->name); errlogFlush(); abort(); } if ( &((FcomBlobRef)0)->fcbl_bpm_Y - &((FcomBlobRef)0)->fc_flt[0] != RES_Y - RES_X ) { errlogPrintf("drvBpm_init_raw_ai(%s): FATAL PROGRAMMING ERROR -- internal layout doesn't match chosen FCOM layout (Y)\n", ai_ps->name); errlogFlush(); abort(); } if ( &((FcomBlobRef)0)->fcbl_bpm_T - &((FcomBlobRef)0)->fc_flt[0] != RES_T - RES_X ) { errlogPrintf("drvBpm_init_raw_ai(%s): FATAL PROGRAMMING ERROR -- internal layout doesn't match chosen FCOM layout (TMIT)\n", ai_ps->name); errlogFlush(); abort(); } bpmBlobs[c].fc_vers = FCOM_PROTO_VERSION; bpmBlobs[c].fc_type = FCOM_EL_FLOAT; bpmBlobs[c].fc_nelm = 3; bpmBlobs[c].fc_flt = &bpmResults[c][RES_X]; if ( FCOM_ID_NONE == (bpmBlobs[c].fc_idnt = fcomLCLSPV2FcomID(ai_ps->name)) ) { errlogPrintf("drvBpm_init_raw_ai(%s): CONFIGURATION ERROR -- unable to determine FCOM ID; NOT using FCOM for this channel\n", ai_ps->name); } else { fcomOutStateMask |= (1<name ); break; default: break; } bpmResultsScl[c][sres] = (0. == ai_ps->aslo ? 1.0 : ai_ps->aslo); bpmResultsOff[c][sres] = ai_ps->aoff; return 0; } long drvBpm_init_raw_ai_helper(aiRecord *ai_ps, DrvBpm_init_raw_ai_cb cb) { unsigned c = ai_ps->inp.value.vmeio.card; unsigned s = ai_ps->inp.value.vmeio.signal; DevAiRawBpmDpvt_tps dpvt_ps; if ( VME_IO != ai_ps->inp.type ) { errlogPrintf("drvBpm_init_raw_ai(%s): INP must be VMEIO\n", ai_ps->name); goto egress; } if ( !(dpvt_ps = calloc(1, sizeof(*dpvt_ps))) ) { epicsPrintf("drvBpm_init_raw_ai(%s): Error -- no memory\n", ai_ps->name); goto egress; } ai_ps->dpvt = dpvt_ps; if ( drvPadUdpCommDpvtInit(&dpvt_ps->drvPadUdpCommPart, ai_ps->inp.value.vmeio.card) ) goto egress; if ( cb && cb(ai_ps, c, s) ) goto egress; return 0; egress: ai_ps->pact = TRUE; return -1; } long drvBpm_init_raw_ai(aiRecord *pai) { return drvBpm_init_raw_ai_helper(pai, init_bpm_results_cb); } long drvBpm_init_raw_ai_generic(void *precord) { return drvBpm_init_raw_ai_helper((aiRecord*)precord, 0); } static long bpmReport(int level) { int N = sizeof(digiHasOvfl)/sizeof(digiHasOvfl[0]); char ovflSup[ N + 1]; int i,j; char ch; epicsPrintf("Generic BPM Support\n"); if ( level > 0 ) { epicsPrintf("Max processing time (ticks) %d\n", (unsigned)drvBpmMaxProcessingTime); epicsPrintf("Max total time (ticks) %d\n", (unsigned)drvBpmMaxTotalTime); i = N; ovflSup[--i] = 0; /* terminating 0 */ for ( j=0; i>=0; j++, i-- ) { if ( DIGI_HAS_OVFL_SUP(j) ) ch = 'Y'; else if ( DIGI_OVFL_NO_SUP == digiHasOvfl[j] ) ch = 'N'; else ch = 'X'; ovflSup[i] = ch; } epicsPrintf("Digitizer hardware overflow detection: %s\n", ovflSup); } return 0; } static long bpmInit(void) { int i,j; static int init_flag = 0; if ( init_flag ) return 0; init_flag = 1; for ( i=0; ie) #define PSI (psub->f) long drvBpmBvectSetupSub(subRecord *psub) { unsigned c = psub->l; BpmNumber *prot; BpmNumber cpsi, spsi, ruu, ruv, rvu, rvv; if ( c >= MAX_BPM ) { psub->pact=TRUE; errlogPrintf("drvBpmBvectSetupSub - invalid channel # %i (INPL)\n", c); return -1; } /* Did one of the angles change */ if ( PHI != psub->le || PSI != psub->lf ) { /* compute new values */ cpsi = cos(PSI*RAD); spsi = sin(PSI*RAD); if ( spsi < ZERO_PT_5 ) { /* Invalid */ recGblSetSevr(psub, CALC_ALARM, INVALID_ALARM); psub->udf = TRUE; return 0; } /* uu */ ruu = sin((PSI-PHI)*RAD)/spsi; ruv = sin(PHI*RAD) /spsi; rvu = -cos((PSI-PHI)*RAD)/spsi; rvv = cos(PHI*RAD) /spsi; prot = bpmRot[c]; /* store a consistent set */ bpmChannelSpinLock( c ); prot[ROT_UU] = ruu; prot[ROT_UV] = ruv; prot[ROT_VU] = rvu; prot[ROT_VV] = rvv; bpmCosPsi[c] = cpsi; bpmSinPsi[c] = spsi; bpmChannelSpinUnlock( c ); psub->le = PHI; psub->lf = PSI; if ( spsi < 0.5 ) { /* Invalid; PSI too small */ recGblSetSevr(psub, CALC_ALARM, INVALID_ALARM); psub->udf = TRUE; } } return 0; } DrvPadUdpCommHWTime drvBpmMaxProcessingTimeReset(int doit) { DrvPadUdpCommHWTime rval = drvBpmMaxProcessingTime; if ( doit ) drvBpmMaxProcessingTime = 0; return rval; } DrvPadUdpCommHWTime drvBpmMaxTotalTimeReset(int doit) { DrvPadUdpCommHWTime rval = drvBpmMaxTotalTime; if ( doit ) drvBpmMaxTotalTime = 0; return rval; } #define LOLO(psub) (psub)->a #define LOW( psub) (psub)->b #define HIGH(psub) (psub)->c #define HIHI(psub) (psub)->d #define HYST(psub) (psub)->e #define LLSV(psub) (psub)->f #define LSV( psub) (psub)->g #define HSV( psub) (psub)->h #define HHSV(psub) (psub)->i #define CHNL(psub) (psub)->j #define RES( psub) (psub)->k static long drvBpmCopyAlarmLimitsIni(subRecord *psub) { unsigned c = (unsigned) CHNL(psub); unsigned res = (unsigned) RES (psub); uintptr_t enc; if ( c >= MAX_BPM ) { errlogPrintf("drvBpmCopyAlarmLimitsIni -- bpm index # out of range"); recGblSetSevr((dbCommon*)psub, HW_LIMIT_ALARM, INVALID_ALARM); psub->pact = 1; return -1; } if ( res >= NUM_RES ) { errlogPrintf("drvBpmCopyAlarmLimitsIni -- result index # out of range"); recGblSetSevr((dbCommon*)psub, HW_LIMIT_ALARM, INVALID_ALARM); psub->pact = 1; return -1; } enc = (c << 8) | res; psub->dpvt = (void*)enc; return 0; } static long drvBpmCopyAlarmLimits(subRecord *psub) { uintptr_t dec = (uintptr_t)psub->dpvt; unsigned c = (unsigned) (dec >> 8); unsigned res = (unsigned) (dec & 0xff); epicsAlarmSeverity llsv = (epicsAlarmSeverity)LLSV(psub); epicsAlarmSeverity lsv = (epicsAlarmSeverity)LSV (psub); epicsAlarmSeverity hsv = (epicsAlarmSeverity)HSV (psub); epicsAlarmSeverity hhsv = (epicsAlarmSeverity)HHSV(psub); if ( llsv < firstEpicsAlarmSev || llsv > lastEpicsAlarmSev ) { errlogPrintf("drvBpmCopyAlarmLimits -- llsv out of range"); recGblSetSevr((dbCommon*)psub, LOLO_ALARM, INVALID_ALARM); return -1; } if ( lsv < firstEpicsAlarmSev || lsv > lastEpicsAlarmSev ) { errlogPrintf("drvBpmCopyAlarmLimits -- lsv out of range"); recGblSetSevr((dbCommon*)psub, LOW_ALARM, INVALID_ALARM); return -1; } if ( hsv < firstEpicsAlarmSev || hsv > lastEpicsAlarmSev ) { errlogPrintf("drvBpmCopyAlarmLimits -- hsv out of range"); recGblSetSevr((dbCommon*)psub, HIGH_ALARM, INVALID_ALARM); return -1; } if ( hhsv < firstEpicsAlarmSev || hhsv > lastEpicsAlarmSev ) { errlogPrintf("drvBpmCopyAlarmLimits -- hhsv out of range"); recGblSetSevr((dbCommon*)psub, HIHI_ALARM, INVALID_ALARM); return -1; } LOCK_ALARM_LIMITS( c ); bpmAlarmLimits[c][res].lolo = LOLO(psub); bpmAlarmLimits[c][res].low = LOW (psub); bpmAlarmLimits[c][res].high = HIGH(psub); bpmAlarmLimits[c][res].hihi = HIHI(psub); bpmAlarmLimits[c][res].hyst = HYST(psub); bpmAlarmLimits[c][res].llsv = llsv; bpmAlarmLimits[c][res].lsv = lsv; bpmAlarmLimits[c][res].hsv = hsv; bpmAlarmLimits[c][res].hhsv = hhsv; UNLOCK_ALARM_LIMITS( c ); return 0; } static const struct iocshArg do_rst_args[] = { { "do_reset_if_nonzero", iocshArgInt, }, }; static const struct iocshArg *do_rst_bloat[] = { &do_rst_args[0], }; static void mptFunc(const iocshArgBuf *args) { epicsPrintf("%d\n", (unsigned)drvBpmMaxProcessingTimeReset(args[0].ival)); } static void mttFunc(const iocshArgBuf *args) { epicsPrintf("%d\n", (unsigned)drvBpmMaxTotalTimeReset(args[0].ival)); } static const iocshFuncDef mptDesc = { "drvBpmMaxProcessingTimeReset", sizeof(do_rst_bloat)/sizeof(do_rst_bloat[0]), do_rst_bloat }; static const iocshFuncDef mttDesc = { "drvBpmMaxTotalTimeReset", sizeof(do_rst_bloat)/sizeof(do_rst_bloat[0]), do_rst_bloat }; static void ih(initHookState s) { switch ( s ) { case initHookAtBeginning: /* Initialize stuff that must work before drivers are initialized. * In particular: the udpComm driver already starts fiducial processing, * so all the spin-locks must be initialized etc. */ bpmInit(); break; default: break; } } static void drvBpmRegistrar(void) { initHookRegister(ih); iocshRegister( &mptDesc, mptFunc ); iocshRegister( &mttDesc, mttFunc ); } epicsRegisterFunction(drvBpmBvectSetupSub); epicsRegisterFunction(drvBpmCopyAlarmLimitsIni); epicsRegisterFunction(drvBpmCopyAlarmLimits); epicsExportRegistrar(drvBpmRegistrar);