/*============================================================================= Name: mpgPattern.c mpgPatternPnetInit - Pnet Pattern Processing Initialization mpgPatternPnet - 360Hz Pnet Pattern Processing mpgPatternProcInit - MPG Pattern Setup Initialization mpgPatternProc - 360Hz MPG Pattern Setup mpgPatternState - Pattern Processing State and Diagnostics mpgPatternCheck - Check Pattern against Event Definitions Abs: This file contains all subroutine support for the MPG IOC processing of the incoming Pnet pattern and outgoing MPG pattern. Rem: All functions called by the subroutine record get passed one argument: psub Pointer to the subroutine record data. Use: pointer Type: struct subRecord * Acc: read/write Mech: reference All functions return a long integer. 0 = OK, -1 = ERROR. The subroutine record ignores the status returned by the Init routines. For the calculation routines, the record status (STAT) is set to SOFT_ALARM (unless it is already set to LINK_ALARM due to severity maximization) and the severity (SEVR) is set to psub->brsv (BRSV - set by the user in the database though it is expected to be invalid). Auth: 04-dec-2006, S. Allison (saa), some logic taken from D. Kotturi's drvPnet.c Rev: -----------------------------------------------------------------------------*/ #include "copyright_SLAC.h" /* SLAC copyright comments */ /*----------------------------------------------------------------------------- Mod: (newest to oldest) =============================================================================*/ #include /* for memset */ #include "debugPrint.h" #include "longSubRecord.h" /* for struct longSubRecord */ #include "registryFunction.h" /* for epicsExport */ #include "epicsExport.h" /* for epicsRegisterFunction */ #include "epicsMutex.h" /* EPICS Mutex support */ #include "epicsTime.h" /* for epicsTimeGetCurrent */ #include "errlog.h" /* errlogPrintf */ #include "evrMessage.h" /* for EVR_MESSAGE_PATTERN* */ #include "evrTime.h" /* for evrTime* prototypes */ #include "evrPattern.h" /* for PATTERN* defines */ #include "pnetSeqCheck.h" /* for pnetSeqCheck* protos */ #include "mpgEvent.h" /* for mpgEvent* protos */ #include "drvMrfEg.h" /* for EgDataBufferLoad proto */ #define EVG_DELTA_TIME 0.008333333333 /* 1/120sec for setting time 3 pulses ahead */ #ifdef DEBUG_PRINT /* ENUM with values: DP_FATAL, DP_ERROR, DP_WARN, DP_INFO, DP_DEBUG Anything less than or equal to mpgPatternFlag gets printed out. Can be set to 0 here and altered via prompt or in st.cmd file */ int mpgPatternFlag = DP_INFO; #endif /* Time Slot Counters - one counter per supported rate */ static int tsCounter_a[TIMESLOT_RATE_MAX]; /* 30hz, 10hz, 5hz, 1hz, 0.5hz */ static int tsCounterMax_a[TIMESLOT_RATE_MAX] = { 1, 5, 11, 59, 119 }; static unsigned long msgCount = 0; /* # waveforms processed since boot/reset */ static unsigned long msgRolloverCount = 0; /* # time msgCount reached EVR_MAX_INT */ static unsigned long resyncCount = 0; /* # of times 6min 2s rollover happened since boot/reset */ static unsigned long patternErrCount = 0; /* # bad PNET waveforms */ static unsigned long timestampErrCount = 0; /* # bad timestamps */ static unsigned long pulseIDSyncErrCount = 0; /* # of pulse ID sync errors */ static unsigned long modulo720SyncErrCount = 0; /* # of modulo720 sync errors */ static unsigned long seqCheck1ErrCount = 0; static unsigned long seqCheck2ErrCount = 0; static unsigned long seqCheck3ErrCount = 0; /*============================================================================= Name: mpgPatternPnetInit Abs: Initialization for 360Hz Processing to Process the Pnet Pattern Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- longSubRecord * psub read point to subroutine record Rem: Initialization subroutine for IOC:LOCA:UNIT:PNET Inputs: None Outputs: None ==============================================================================*/ static int mpgPatternPnetInit(longSubRecord *psub) { /* Register this record for pattern processing */ if (evrMessageRegister(EVR_MESSAGE_PNET_NAME, sizeof(evrMessagePnet_ts), (dbCommon *)psub) < 0) return -1; /* Initialize rate counters for every timeslot and every rate */ memset(tsCounter_a, 0, sizeof(int) * TIMESLOT_RATE_MAX); return 0; } /*============================================================================= Name: mpgPatternPnet Abs: 360Hz Processing of the Pnet pattern Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- longSubRecord * psub read point to subroutine record Rem: Subroutine for IOC:LOCA:UNIT:PNET Inputs: A - Spare B - Spare H - Previous Modifier5 without EDEF bits I - Previous Modulo 720 Count K - Flag to stop sequence checking L - Previous Pulse ID M - Mask of user-settable bits of Modifier5 N - Allow Shutter Permit (0 = remove shutter_perm, 1 = allow shutter_perm) O - Allow Pockels Cell (0 = remove pockcel_perm, 1 = allow pockcel_perm) P - Max Pulses that shutter permit can be removed (0xFFFFFFFF = forever) Q - Max Pulses that pockels cell permit can be removed (0xFFFFFFFF = forever) R - Modifier5 rate mask at which shutter permit is removed (not used) S - Modifier5 rate mask at which pockels cell permit is removed T - Number of Pulses that shutter permit has been removed U - Number of Pulses that pockels cell permit has been removed Outputs: C - Time Slot D - Pnet Modifier 1 E - Pnet Modifier 2 F - Pnet Modifier 3 G - Pnet Modifier 4 H - Modifier5 without EDEF bits I - Modulo 720 Count J - Beam Code L - Pulse ID (set invalid if error) T - Number of Pulses that shutter permit has been removed U - Number of Pulses that pockels cell permit has been removed V - Restore Shutter Permit W - Restore Pockels Cell Permit X - Spare Y - Spare Z - Spare VAL = Error flag: 0 = OK Seq check 1 error Seq check 2 error Seq check 3 error Invalid Waveform MPG IPLing Pulse ID not synchronized with the SLC MPG Modulo 720 not synchronized with the SLC MPG Side: None Ret: 0 = OK, -1 = ERROR ==============================================================================*/ static long mpgPatternPnet(longSubRecord *psub) { evrMessagePattern_ts *pattern_ps; unsigned long *timeslot_p; unsigned long *patternStatus_p; evrMessageReadStatus_te evrMessageStatus; epicsTimeStamp *mod720time_ps; epicsTimeStamp currentTime; unsigned long modifier5mask; int pulsid = psub->l + 1; int modulo720Count = psub->i + 1; int pulsid_resync; unsigned int idx; unsigned char traveling_one; static unsigned int pulseIDSync = 0; /* EVG and SLC MPG pulse ID synced */ static unsigned int modulo720Sync = 0; /* EVG and SLC MPG modulo720 synced */ static unsigned int seqCheckSync = 0; /* sequence checking synced */ /* Keep a count of messages and reset before overflow */ if (msgCount < EVR_MAX_INT) { msgCount++; } else { msgRolloverCount++; msgCount = 0; } /* Lock the pattern table and get pointer to newest pattern data */ if (evrTimePatternPutStart(&pattern_ps, ×lot_p, &patternStatus_p, &mod720time_ps)) { psub->val = PATTERN_INVALID_TIMESTAMP; return -1; } /* if we cannot read the message - set record invalid and force a resync */ evrMessageStatus = evrMessageRead(EVR_MESSAGE_PNET, (evrMessage_tu *)pattern_ps->modifier_a); if (evrMessageStatus) { for (idx = 0; idx < EVR_MODIFIER_MAX; idx++) pattern_ps->modifier_a[idx] = 0; *timeslot_p = 0; if (evrMessageStatus == evrMessageDataNotAvail) { *patternStatus_p = PATTERN_TIMEOUT; } else { *patternStatus_p = PATTERN_INVALID_WF; patternErrCount++; } pulseIDSync = 0; modulo720Sync = 0; seqCheckSync = 0; epicsTimeGetCurrent(¤tTime); if (epicsTimeDiffInSeconds(¤tTime, mod720time_ps) > MODULO720_SECS) modulo720Count = MODULO720_COUNT; } else { *patternStatus_p = PATTERN_OK; /* Remove unused high PSK bit of modifier1 until EDM is fixed. */ pattern_ps->modifier_a[0] &= (~0x80000000); *timeslot_p = TIMESLOT(pattern_ps->modifier_a); /* Check if SLC MPG is rebooting */ if (pattern_ps->modifier_a[0] & MPG_IPLING) { *patternStatus_p = PATTERN_MPG_IPLING; pulseIDSync = 0; modulo720Sync = 0; seqCheckSync = 0; /* Do sync checking if requested */ } else if (psub->k > 0) { traveling_one = (unsigned char)(pattern_ps->modifier_a[1] & TIMESLOT_MASK); if (pnetSeqCheckData1(traveling_one, seqCheckSync)) { /* "traveling 1" check */ *patternStatus_p = PATTERN_SEQ_CHECK1_ERR; seqCheck1ErrCount++; } else if (pnetSeqCheckData2(*timeslot_p, seqCheckSync)) { /* 0-5 looping counter check */ *patternStatus_p = PATTERN_SEQ_CHECK2_ERR; seqCheck2ErrCount++; } else if (pnetSeqCheckData3(traveling_one, *timeslot_p)) {/* check that two checksum values stay in sync */ *patternStatus_p = PATTERN_SEQ_CHECK3_ERR; seqCheck3ErrCount++; } /* Both pulse ID and modulo 720 will need to be resync'ed too */ if (*patternStatus_p) { pulseIDSync = 0; modulo720Sync = 0; } seqCheckSync = 1; /* Check if modulo-720 can be resynchronized on this pulse. */ if (pattern_ps->modifier_a[0] & MODULO720_MASK) { /* Synch all time slot counters on MOD720 */ for (idx = 0; idx < TIMESLOT_RATE_MAX; idx++) { tsCounter_a[idx] = tsCounterMax_a[idx]; } /* If we should be sync'ed but are not, update a counter */ if (modulo720Sync && (modulo720Count != MODULO720_COUNT)) { modulo720SyncErrCount++; errlogPrintf("mpgPatternPnet: Modulo 720 not synchronized, modulo720Count = %d\n", modulo720Count); /* Assume pulse ID is also out-of-sync */ pulseIDSync = 0; } modulo720Count = 0; modulo720Sync = 1; } else if (!modulo720Sync) { *patternStatus_p = PATTERN_MODULO720_NO_SYNC; } else if (modulo720Count >= MODULO720_COUNT) { /* If we should be sync'ed but are not, update a counter */ modulo720SyncErrCount++; errlogPrintf("mpgPatternPnet: Modulo 720 not synchronized, modulo720Count = %d\n", modulo720Count); modulo720Count = 0; modulo720Sync = 0; *patternStatus_p = PATTERN_MODULO720_NO_SYNC; /* Assume pulse ID is also out-of-sync */ pulseIDSync = 0; } /* Check for a non-zero value in the 4 bit pulsid_resync field of Pnet data. If found, it resets upper 4 bits of the pulse ID (lower bit will be zero). Otherwise, the pulse ID is set to 1 if the last value is the rollover value. Otherwise, the pulse ID is simply incremented.*/ pulsid_resync = (pattern_ps->modifier_a[0] >> 3) & PULSEID_RESYNC; if (pulsid_resync) { /* If we should be sync'ed but are not, update a counter */ if ((pulseIDSync) && (pulsid != pulsid_resync)) { pulseIDSyncErrCount++; errlogPrintf("mpgPatternPnet: Pulse IDs not synchronized, MPG = %d, EVG = %d\n", pulsid_resync, pulsid); /* Assume modulo 720 out-of-sync too unless the RESYNC bit is set */ if (!(pattern_ps->modifier_a[0] & MODULO720_MASK)) { modulo720Sync = 0; *patternStatus_p = PATTERN_MODULO720_NO_SYNC; } } pulseIDSync = 1; pulsid = pulsid_resync; } else if (!pulseIDSync) { *patternStatus_p = PATTERN_PULSEID_NO_SYNC; } else if (pulsid > PULSEID_MAX) { /* Aha. Time to rollover, so THIS pulse will be set back to zero */ DEBUGPRINT(DP_DEBUG, mpgPatternFlag, ("mpgPatternPnet: Rollover, pulsid+1 = %d\n", pulsid)); pulsid = 0; resyncCount++; } } else { pulseIDSync = 0; modulo720Sync = 0; seqCheckSync = 0; } /* For timeslot 1, update counters and rate bits in modifier5 */ if (*timeslot_p == 1) { pattern_ps->modifier_a[4] = 0; modifier5mask = 1<= tsCounterMax_a[idx]) { tsCounter_a[idx] = 0; if (!*patternStatus_p) pattern_ps->modifier_a[4] |= modifier5mask; } else { tsCounter_a[idx]++; } modifier5mask = modifier5mask<<1; } /* For other timeslots, use previously calculated rate bits */ /* Zero out previous user-settable bits. EDEF bits will be */ /* set/cleared later. */ } else { pattern_ps->modifier_a[4] = psub->h & (~MOD5_USER_MASK); } pattern_ps->modifier_a[4] |= psub->m; /* Check if shutter permit is allowed */ psub->v = 0; if (psub->n) { psub->t = 0; /* Check if shutter permit can be allowed again */ } else if ((psub->p != 0xFFFFFFFF) && (psub->t >= psub->p)) { psub->t = 0; psub->v = 1; /* Check if shutter permit must be removed. */ } else if (pattern_ps->modifier_a[1] & SHUTTER_PERM) { psub->t++; pattern_ps->modifier_a[1] &= ~SHUTTER_PERM; } /* Check if pockels cell permit is allowed */ psub->w = 0; if (psub->o) { psub->u = 0; /* Check if pockels cell permit can be allowed again */ } else if ((psub->q != 0xFFFFFFFF) && (psub->u >= psub->q)) { psub->u = 0; psub->w = 1; /* Check if pockels cell permit must be removed */ } else if (pattern_ps->modifier_a[2] & POCKCEL_PERM) { modifier5mask = psub->s; if ((modifier5mask == 0) || (pattern_ps->modifier_a[4] & modifier5mask)) { psub->u++; pattern_ps->modifier_a[2] &= ~POCKCEL_PERM; } } } /* Tell EVRs if data is out-of-sync */ if (*patternStatus_p) pattern_ps->modifier_a[0] |= MPG_IPLING; if (pulsid > PULSEID_MAX) pulsid = 0; if (modulo720Count >= MODULO720_COUNT) { pattern_ps->modifier_a[0] |= MODULO720_MASK; modulo720Count = 0; } psub->c = *timeslot_p; psub->d = pattern_ps->modifier_a[0]; psub->e = pattern_ps->modifier_a[1]; psub->f = pattern_ps->modifier_a[2]; psub->g = pattern_ps->modifier_a[3]; psub->h = pattern_ps->modifier_a[4]; psub->i = modulo720Count; /* beamcode decoded from modifier 1 */ psub->j = BEAMCODE(pattern_ps->modifier_a); psub->l = pulsid; psub->val = *patternStatus_p; /* Unlock pattern data */ evrTimePatternPutEnd(0); if (psub->val) return -1; return 0; } /*============================================================================= Name: mpgPatternProcInit Abs: Initialization for 360Hz Processing to Set Up the MPG Pattern Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- longSubRecord * psub read point to subroutine record Rem: Initialization subroutine for IOC:LOCA:UNIT:PATTERNN-3 Inputs: None. Outputs: DPVT - Pointer to EVG Card Structure ==============================================================================*/ static int mpgPatternProcInit(longSubRecord *psub) { /* * For RTEMS, initialize the EVG data buffer size * and get the pointer to the card structure. */ #ifdef __rtems__ EgDataBufferInit(0, sizeof(evrMessagePattern_ts)); psub->dpvt = (EgCardStruct *)EgGetCardStruct(0); #endif return 0; } /*============================================================================= Name: mpgPatternProc Abs: 360Hz Processing to Set Up the MPG Pattern Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- longSubRecord * psub read point to subroutine record Rem: Subroutine for IOC:LOCA:UNIT:PATTERNN-3 Inputs: DPVT - Pointer to EVG Card Structure A - Spare B - Pnet processing error flag C - Time Slot D - Pnet Modifier 1 E - Pnet Modifier 2 F - Pnet Modifier 3 G - Pnet Modifier 4 H - EVG Modifier 5 - Beam Synchronous Acquisition (BSA) Bit Mask One bit per BSA event definition (EDEF). 20 EDEF total. 0 = don't process this pulse, 1 = process this pulse. Also, 5 time slot rate bits. I - EVG Modifier 6 J - Beam Code K - EDAVGDONEN-3 L - Pulse ID U - edef Init Mask V - edef MeasSevr Minor Mask W - edef MeasSevr Major Mask Outputs: VAL = Error flag: 0 = OK Seq check 1 error Seq check 2 error Seq check 3 error Invalid Waveform MPG IPLing Pulse ID not synchronized with the SLC MPG Modulo 720 not synchronized with the SLC MPG Invalid Timestamp Side: Pattern is sent out via the EVG. Ret: 0 = OK, -1 = ERROR ==============================================================================*/ static long mpgPatternProc(longSubRecord *psub) { evrMessagePattern_ts *pattern_ps; unsigned long *timeslot_p; unsigned long *patternStatus_p; epicsTimeStamp *mod720time_ps; epicsTimeStamp prev_time; double delta_time; int modulo720Flag; int idx; psub->val = psub->b; /* Lock the pattern table and get pointer to newest pattern data */ if (evrTimePatternPutStart(&pattern_ps, ×lot_p, &patternStatus_p, &mod720time_ps)) { psub->val = PATTERN_INVALID_TIMESTAMP; return -1; } /* Fill in the the pattern except for time */ pattern_ps->header_s.type = EVR_MESSAGE_PATTERN; pattern_ps->header_s.version = EVR_MESSAGE_PATTERN_VERSION; for (idx = 0; idx < EVR_MODIFIER_MAX; idx++) pattern_ps->modifier_a[idx] = (&psub->d)[idx]; pattern_ps->edefInitMask = psub->u; pattern_ps->edefAvgDoneMask = psub->k; pattern_ps->edefMinorMask = psub->v; pattern_ps->edefMajorMask = psub->w; *timeslot_p = psub->c; /* Initialize EVR timestamp to system time */ if (epicsTimeGetCurrent(&pattern_ps->time)) { evrTimePutPulseID(&pattern_ps->time, PULSEID_INVALID); psub->val = PATTERN_INVALID_TIMESTAMP; } else { /* The time is for 3 pulses in the future - add 1/120sec */ epicsTimeAddSeconds(&pattern_ps->time, EVG_DELTA_TIME); /* Overlay the pulse ID into the lower 17 bits of the nsec field */ evrTimePutPulseID(&pattern_ps->time, (unsigned int)psub->l); /* The timestamp must ALWAYS be increasing. Check if this time is less than the previous time (due to rollover) and adjust up slightly using bit 17 (the lower-most bit of the top 15 bits). */ if (!evrTimeGetFromPipeline(&prev_time, evrTimeNext2, 0,0,0,0,0)) { delta_time = epicsTimeDiffInSeconds(&pattern_ps->time, &prev_time); if (delta_time < 0.0) { epicsTimeAddSeconds(&pattern_ps->time, PULSEID_BIT17/NSEC_PER_SEC); evrTimePutPulseID(&pattern_ps->time, (unsigned int)psub->l); /* If the timestamp STILL isn't greater than the past, perhaps system time is not increasing as it should. For now, keep a counter - later we may want to add code to handle this. */ delta_time = epicsTimeDiffInSeconds(&pattern_ps->time, &prev_time); if (delta_time < 0.0) timestampErrCount++; } } /* send pattern on to all EVRs */ #ifdef __rtems__ if (psub->dpvt) { epicsMutexLock(((EgCardStruct *)psub->dpvt)->EgLock); EgDataBufferLoad((EgCardStruct *)psub->dpvt, (epicsUInt32 *)pattern_ps, sizeof(evrMessagePattern_ts)/sizeof(epicsUInt32)); epicsMutexUnlock(((EgCardStruct *)psub->dpvt)->EgLock); } #endif } *patternStatus_p = psub->val; /* modulo720 decoded from modifier 1*/ if (pattern_ps->modifier_a[0] & MODULO720_MASK) modulo720Flag = 1; else modulo720Flag = 0; /* Unlock pattern data and post MOD720 events if needed */ evrTimePatternPutEnd(modulo720Flag); if (psub->val) return -1; return 0; } /*============================================================================= Name: mpgPatternState Abs: Access to Last MPG Pattern State and Pattern Diagnostics. This subroutine is for status only and must update at 0.5Hz. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- longSubRecord * psub read point to subroutine record Rem: Subroutine for IOC:LOCA:UNIT:PATTERNSTATE Inputs: A - Error Flag from mpgPatternProc B - Counter Reset Flag Outputs: C - Number of timeout errors D - Number of waveforms processed by mpgPatternPnet E - Number of times D has rolled over F - Number of bad waveforms The following G-J outputs are pop'ed by a call to evrMessageCounts() G - Number of times ISR wrote a message H - Number of times G has rolled over I - Number of times ISR overwrote a message J - Number of backwards time errors K - Number of unexpected unsynchronized pulse IDs L - Number of unexpected unsynchronized modulo720s M - Number of seq check 1 errors N - Number of seq check 2 errors O - Number of seq check 3 errors P - Number of pulse Id 6min 2sec cycles that have transpired since reset Q - Number of lock errors in event processing R - Number of times a seq ram was still busy S - Number of times a seq ram was not in single mode T - Number of times a seq ram was still active U - Number of bad patterns in event processing V - Minimum PNET Delta Start Time (us) W - Maximum PNET Delta Start Time (us) X - Average Data Processing Time (us) Y - Number of message write errors Z - Maximum Data Processing Time (us) VAL = Last Error flag (see mpgPatternProc for values) Side: File-scope counters may be reset. Ret: 0 = OK ==============================================================================*/ static long mpgPatternState(longSubRecord *psub) { psub->val = psub->a; psub->d = msgCount; /* # waveforms processed since boot/reset */ psub->e = msgRolloverCount; /* # time msgCount reached EVR_MAX_INT */ psub->f = patternErrCount; evrMessageCounts(EVR_MESSAGE_PNET, &psub->g,&psub->h,&psub->i,&psub->c,&psub->y, &psub->j,&psub->v,&psub->w,&psub->x,&psub->z); psub->j = timestampErrCount; psub->k = pulseIDSyncErrCount; psub->l = modulo720SyncErrCount; psub->m = seqCheck1ErrCount; psub->n = seqCheck2ErrCount; psub->o = seqCheck3ErrCount; psub->p = resyncCount; /* # of times 6min 2s rollover happened since boot/reset */ mpgEventCounts(&psub->q,&psub->r,&psub->s,&psub->t,&psub->u); if (psub->b > 0) { psub->b = 0; msgCount = 0; msgRolloverCount = 0; patternErrCount = 0; resyncCount = 0; seqCheck1ErrCount = 0; seqCheck2ErrCount = 0; seqCheck3ErrCount = 0; pulseIDSyncErrCount = 0; modulo720SyncErrCount = 0; evrMessageCountReset(EVR_MESSAGE_PNET); mpgEventCountReset(); } return 0; } /*============================================================================= Name: mpgPatternCheck Abs: 360Hz Processing Check to see if current beam pulse is to be used in any current event measurement definition. Keep running measurement and average count. Keep done flag (z) Keep pattern match flag (val). Keep average done flag (k) Record is disabled when CTRL = OFF. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- longSubRecord * psub read point to subroutine record Rem: Subroutine for PATTERNCHECK Inputs INPB - Beam Code INPC - EDEF AVGCNT INPD - Pnet Modifier 1 INPE - Pnet Modifier 2 INPF - Pnet Modifier 3 INPG - Pnet Modifier 4 INPH - Modifier 5 without EDEF bits INPI - EDEF Beam Code INPJ - EDEF MEASCNT * AVGCNT = CNTMAX INPM - EDEF INCLUSION1 (for Modifier2) INPN - EDEF INCLUSION2 (for Modifier3) INPO - EDEF INCLUSION3 (for Modifier4) INPP - EDEF INCLUSION4 (for Modifier5) INPQ - EDEF INCLUSION5 (not used) INPR - EDEF EXCLUSION1 (for Modifier2) INPS - EDEF EXCLUSION2 (for Modifier3) INPT - EDEF EXCLUSION3 (for Modifier4) INPU - EDEF EXCLUSION4 (for Modifier5) INPV - EDEF EXCLUSION5 (not used) INPW - Beam Code SEVR INPX - EDEF MEASCNT (= 0xFFFFFFFF means forever) Outputs VAL = Pattern match = 1, no match = 0; K = Done averaging = 1; not done averaging = 0 The following are reset when CTRL is turned ON: A = Counter before CTRL is turned off L = Running Average Count Y = Running Measurement Count Z = Done =1; not done measuring = 0 Ret: 0 - always good ==============================================================================*/ static long mpgPatternCheck(longSubRecord *psub) { psub->val = 0; psub->k = 0; /* Exit now if we're done */ if (psub->z) return 0; /* if good pattern and pattern beam code == EDEF beamcode. if zero, use any beam code */ if ((psub->w == 0) && ((psub->i == 0) || (psub->b==psub->i))) { /* check inclusion mask */ DEBUGPRINT(DP_DEBUG, mpgPatternFlag, ("\t mpgPatternCheck: Beam Codes match \n")); if ( ((psub->e & psub->m) == psub->m) && ((psub->f & psub->n) == psub->n) && ((psub->g & psub->o) == psub->o) && ((psub->h & psub->p) == psub->p)) { DEBUGPRINT(DP_DEBUG, mpgPatternFlag, ("mpgPatternCheck: inclusion match\n")); if ( ((psub->r & psub->e) == 0) && ((psub->s & psub->f) == 0) && ((psub->t & psub->g) == 0) && ((psub->u & psub->h) == 0)) { DEBUGPRINT(DP_DEBUG, mpgPatternFlag, ("mpgPatternCheck: exclusion match\n")); /* if the last cycle is done, turn CTRL off now - EVR IOCs should be finished now. */ if ((psub->y >= psub->j) && (psub->x != 0xFFFFFFFF)) { psub->z = 1; return 0; } psub->val = 1; /* pattern match*/ psub->y++; /* inc. meas. count */ psub->l++; /* inc. avg count */ if (psub->l >=psub->c) { /* avg cnt >= edef avg cnt?*/ psub->k = 1; /* averaging done */ psub->l = 0; /* reset avg count */ } } } } /* Don't let CTRL stay on too long after last match. */ if ((psub->y >= psub->j) && (psub->x != 0xFFFFFFFF)) { /* Don't wait for more than 2 seconds before turning CTRL off */ psub->a++; if (psub->a > MODULO720_COUNT) psub->z = 1; } return 0; } /*============================================================================= Name: mpgPatternSim Abs: Simulate PNET data stream. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- longSubRecord * psub read point to subroutine record Rem: Subroutine for IOC:LOCA:UNIT:PATTERNSIM Side: Sends a message to the PNET pattern queue. Sub Inputs/ Outputs: Simulated inputs: D - MODIFIER1 E - MODIFIER2 F - MODIFIER3 G - MODIFIER4 J - BEAMCODE K - YY L - PULSE ID (not used) Ret: Return from evrMessageWrite. ==============================================================================*/ static long mpgPatternSim(longSubRecord *psub) { evrMessage_tu evrMessage_u; int idx; void pnetISR (epicsUInt32 *buffer_a); /*------------- parse input into sub outputs ----------------------------*/ for (idx = 0; idx < EVR_PNET_MODIFIER_MAX; idx++) evrMessage_u.pnet_s.modifier_a[idx] = (&psub->d)[idx]; evrMessage_u.pnet_s.modifier_a[0] = psub->d & 0xFFFF8000; evrMessage_u.pnet_s.modifier_a[0] |= ((psub->j << 8) & 0x1F00); evrMessage_u.pnet_s.modifier_a[0] |= (psub->k & YY_BIT_MASK); /* Send the pattern to the PNET pattern queue and wake up PNET task*/ evrMessageWrite(EVR_MESSAGE_PNET, &evrMessage_u); pnetISR(0); return 0; } epicsRegisterFunction(mpgPatternPnetInit); epicsRegisterFunction(mpgPatternPnet); epicsRegisterFunction(mpgPatternProcInit); epicsRegisterFunction(mpgPatternProc); epicsRegisterFunction(mpgPatternState); epicsRegisterFunction(mpgPatternCheck); epicsRegisterFunction(mpgPatternSim);