/*============================================================================= Name: mpgPatternMod.c mpgPatternMod - 360Hz Pattern Modifier Processing mpgPatternModInit - 360Hz Pattern Modifier Processing Init mpgPatternModUser - User Input to Fixed Modifier Bits mpgPatternModCnt - 360Hz Pattern Countdown Modifier Bits mpgPatternModCntInp - User Input to Countdown Modifier Bit Processing mpgPatternModCntState - State of Countdown Modifier Bit Processing mpgPatternModEdef - 360Hz EDEF Processing mpgPatternModEdefInp - User Input to EDEF Processing mpgPatternModEdefState - EDEF Processing State Abs: This file contains all subroutine support for the MPG IOC processing of pattern modifier bits. Rem: All functions called by the subroutine record get passed one argument: psub Pointer to the subroutine record data. Use: pointer Type: struct longSubRecord * 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) Rev: -----------------------------------------------------------------------------*/ #include "copyright_SLAC.h" /* SLAC copyright comments */ /*----------------------------------------------------------------------------- Mod: (newest to oldest) =============================================================================*/ #include /* for memset */ #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 "dbScan.h" /* for post_event */ #include "alarm.h" /* for *_ALARM */ #include "evrMessage.h" /* for evrMessagePattern_ts */ #include "evrTime.h" /* for EDEF_MAX */ #include "evrPattern.h" /* for MOD5_USER_MASK */ /* Pattern RWMutex - to protect data below */ static epicsMutexId mpgPatternModRWMutex_ps = 0; /* EDEF Pattern table and bit masks */ typedef struct { /* Inputs */ unsigned long mask; evrModifier_ta inclusion_a; evrModifier_ta exclusion_a; unsigned long beamCode; unsigned long avgCount; unsigned long measCount;/* 0xFFFFFFFF = forever */ unsigned long maxCount; /* avgCount * measCount */ /* Input/Outputs */ unsigned long control; /* 0 = off, 1 = on */ unsigned long controlOffCount; /* Counter before CTRL is turned off */ unsigned long avgCountSoFar; /* Number in average so far */ unsigned long countSoFar; /* Number of matches so far */ unsigned long countZero; /* countSoFar set to zero */ } mpgPatternModEdef_ts; static mpgPatternModEdef_ts edef_as[EDEF_MAX]; static unsigned long edefInitMask = 0; static unsigned long edefMinorMask = 0; static unsigned long edefMajorMask = 0; /* Modifier Bits using countdown */ typedef struct { /* Inputs */ unsigned long mask; /* Modifier Bit Mask */ unsigned long index; /* Modifier Array Index */ unsigned long rateMask; /* Rate mask at which bit is removed */ unsigned long maxCount; /* 0xFFFFFFFF = forever */ /* Input/Outputs */ unsigned long control; /* 0 = on, 1 = leave alone */ unsigned long countSoFar; /* Countdown */ unsigned long countZero; /* countSoFar set to zero */ unsigned long inuse; /* 0 = available, 1 = in use */ } mpgPatternModCnt_ts; #define MOD_MAX 5 static mpgPatternModCnt_ts mod_as[MOD_MAX]; static unsigned long modifier5UserMask = 0; /* Fixed bits in modifier 5 */ /*============================================================================= Name: mpgPatternModInit Abs: Initialization for 360Hz Processing of Pattern Modifier Bits Args: None. Rem: Call once by mpgPatternInit. Ret: -1 if mutex cannot be allocated or is already allocated. 0 if OK. ==============================================================================*/ int mpgPatternModInit(void) { /* If the mutex is already created, then this routine is being called more than once which is an error. */ if (mpgPatternModRWMutex_ps) return -1; /* Initialize Edef and modifier bit arrays */ memset(edef_as, 0, sizeof(mpgPatternModEdef_ts) * EDEF_MAX); memset(mod_as , 0, sizeof(mpgPatternModCnt_ts ) * MOD_MAX ); /* Create the mutex used by all other routines. */ mpgPatternModRWMutex_ps = epicsMutexCreate(); if (!mpgPatternModRWMutex_ps) return -1; return (0); } /*============================================================================= Name: mpgPatternModUser Abs: Fixed User Modifier Bit Mask Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- longSubRecord * psub read point to subroutine record Rem: Subroutine for IOC:LOCA:UNIT:MODIFIER5INP Inputs: A - Modifier Bit Mask Outputs: VAL - modifier5UserMask Side: Write to memory used by 360hz processing (mutex protected) Ret: 0 = OK, -1 = ERROR ==============================================================================*/ static long mpgPatternModUser(longSubRecord *psub) { psub->val = psub->a & MOD5_USER_MASK; /* Fixed bits in modifier 5 */ if ((!mpgPatternModRWMutex_ps) || epicsMutexLock(mpgPatternModRWMutex_ps)) return -1; modifier5UserMask = psub->val; epicsMutexUnlock(mpgPatternModRWMutex_ps); return 0; } /*============================================================================= Name: mpgPatternModCntInp Abs: User Input to Countdown Modifier Bit Processing Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- longSubRecord * psub read point to subroutine record Rem: Subroutine for PATT:LOCA:UNIT:NAME Inputs: A - Unique index into mod_as array (0 to MOD_MAX-1). B - Modifier Bit Mask C - Modifier Array Index (0 to EVR_MODIFIER_MAX-1) D - Modifier5 rate mask at which bit is removed E - Max Pulses that bit can be removed (0xFFFFFFFF = forever) F - Modifier bit Control (0 = remove bit, 1 = allow bit) L - Previous value of A Outputs: L - A Side: Write to memory used by 360hz processing (mutex protected) Ret: 0 = OK, -1 = ERROR ==============================================================================*/ static long mpgPatternModCntInp(longSubRecord *psub) { if ((!mpgPatternModRWMutex_ps) || epicsMutexLock(mpgPatternModRWMutex_ps)) return -1; /* Clear out previous pattern if the index has changed or there is an input error. */ if ((psub->l < MOD_MAX) && ((psub->a != psub->l) || (psub->c >= EVR_MODIFIER_MAX))) { mod_as[psub->l].mask = 0; mod_as[psub->l].index = 0; mod_as[psub->l].rateMask = 0; mod_as[psub->l].maxCount = 0; mod_as[psub->l].control = 0; mod_as[psub->l].inuse = 0; } /* Don't fill in the new pattern if there is an error or the pattern is being used by another record. */ if ((psub->a >= MOD_MAX) || (psub->c >= EVR_MODIFIER_MAX) || ((psub->a != psub->l) && mod_as[psub->a].inuse)) { psub->l = MOD_MAX; epicsMutexUnlock(mpgPatternModRWMutex_ps); return -1; } psub->l = psub->a; mod_as[psub->a].mask = psub->b; mod_as[psub->a].index = psub->c; mod_as[psub->a].rateMask = psub->d; mod_as[psub->a].maxCount = psub->e; mod_as[psub->a].control = psub->f; mod_as[psub->a].inuse = 1; if (mod_as[psub->a].control && mod_as[psub->a].countSoFar) { mod_as[psub->a].countSoFar = 0; mod_as[psub->a].countZero = 1; } epicsMutexUnlock(mpgPatternModRWMutex_ps); return 0; } /*============================================================================= Name: mpgPatternModCntState Abs: State of Countdown Modifier Bit Processing Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- longSubRecord * psub read/write point to subroutine record Rem: Subroutine for PATT:LOCA:UNIT:NAMESTATE Processed on EVENT_MPG event. Inputs: A - Unique index into mod_as array (0 to MOD_MAX-1). B - Control value (0=on, 1=leave alone) Outputs: L - Control on (0 = don't turn control on, 1 = turn control on) VAL - Countdown so far Side: Read from memory used by 360hz processing (mutex protected) Ret: 0 = OK, -1 = ERROR ==============================================================================*/ static long mpgPatternModCntState(longSubRecord *psub) { psub->l = 0; psub->val = 0; if (psub->a >= MOD_MAX) return -1; if ((!mpgPatternModRWMutex_ps) || epicsMutexLock(mpgPatternModRWMutex_ps)) return -1; if ((!psub->b) && mod_as[psub->a].control) psub->l = 1; psub->val = mod_as[psub->a].countSoFar; epicsMutexUnlock(mpgPatternModRWMutex_ps); return 0; } /*============================================================================= Name: mpgPatternModCnt Abs: 360Hz Processing of Countdown Modifier Bits Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- unsigned long * modifier_a read/write Modifier Bit Mask Array Rem: Assume mpgPatternModRWMutex is already taken. Side: None. Ret: 0 = control not changed, 1 = control changed ==============================================================================*/ static int mpgPatternModCnt(unsigned long *modifier_a) { int idx; int changeFlag = 0; for (idx = 0; idx < MOD_MAX; idx++) { if (mod_as[idx].countZero) { mod_as[idx].countZero = 0; changeFlag = 1; } /* Remove bit only if it's not being externally controlled. */ if (!mod_as[idx].control) { /* Check if this bit is done - set changeFlag so that state record will be processed soon. */ if ((mod_as[idx].maxCount != 0xFFFFFFFF) && (mod_as[idx].maxCount <= mod_as[idx].countSoFar)) { mod_as[idx].control = 1; changeFlag = 1; /* If this pulse applies (depends on the desired rate), remove the bit. */ } else if ((modifier_a[mod_as[idx].index] & mod_as[idx].mask) && ((mod_as[idx].rateMask == 0) || (modifier_a[4] & mod_as[idx].rateMask))) { mod_as[idx].countSoFar++; modifier_a[mod_as[idx].index] &= ~(mod_as[idx].mask); } } } return(changeFlag); } /*============================================================================= Name: mpgPatternModEdefInp Abs: User Input to EDEF Checking Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- longSubRecord * psub read point to subroutine record Rem: Subroutine for EDEF:LOCA:UNIT:PATTERNCHECK Inputs: A - EDEF Number (starting from 1) B - EDEF Initialize Flag C - EDEF AVGCNT D - EDEF Severity I - EDEF Beam Code (0 = use any beam code) J - EDEF MEASCNT * AVGCNT = CNTMAX M - EDEF INCLUSION1 (for Modifier2) N - EDEF INCLUSION2 (for Modifier3) O - EDEF INCLUSION3 (for Modifier4) P - EDEF INCLUSION4 (for Modifier5) Q - EDEF INCLUSION5 (not used) R - EDEF EXCLUSION1 (for Modifier2) S - EDEF EXCLUSION2 (for Modifier3) T - EDEF EXCLUSION3 (for Modifier4) U - EDEF EXCLUSION4 (for Modifier5) V - EDEF EXCLUSION5 (not used) X - EDEF MEASCNT (= 0xFFFFFFFF means forever) Z - EDEF control (0=off, 1=on) Outputs: B - EDEF Init flag reset to 0. E - EDEF Index Side: Write to memory used by 360hz processing (mutex protected) Ret: 0 = OK, -1 = ERROR ==============================================================================*/ static long mpgPatternModEdefInp(longSubRecord *psub) { int midx; unsigned long *outp; psub->e = psub->a - 1; if (psub->e >= EDEF_MAX) return -1; if ((!mpgPatternModRWMutex_ps) || epicsMutexLock(mpgPatternModRWMutex_ps)) return -1; for (midx = 1, outp = &psub->m; midx < EVR_MODIFIER_MAX; midx++, outp++) edef_as[psub->e].inclusion_a[midx] = *outp; for (midx = 1, outp = &psub->r; midx < EVR_MODIFIER_MAX; midx++, outp++) edef_as[psub->e].exclusion_a[midx] = *outp; edef_as[psub->e].mask = 1 << psub->e; edef_as[psub->e].beamCode = psub->i; edef_as[psub->e].avgCount = psub->c; edef_as[psub->e].measCount = psub->x; edef_as[psub->e].maxCount = psub->j; /* If control has changed and it's now on, reset counters. */ if (edef_as[psub->e].control != psub->z) { edef_as[psub->e].control = psub->z; edef_as[psub->e].countZero = 1; if (edef_as[psub->e].control) { edefInitMask |= edef_as[psub->e].mask; edef_as[psub->e].controlOffCount = 0; edef_as[psub->e].avgCountSoFar = 0; edef_as[psub->e].countSoFar = 0; } } if (psub->b) { psub->b = 0; edefInitMask |= edef_as[psub->e].mask; edef_as[psub->e].controlOffCount = 0; edef_as[psub->e].avgCountSoFar = 0; if (edef_as[psub->e].countSoFar) { edef_as[psub->e].countSoFar = 0; edef_as[psub->e].countZero = 1; } } if (psub->d == MINOR_ALARM) { edefMinorMask |= edef_as[psub->e].mask; edefMajorMask |= edef_as[psub->e].mask; } else if (psub->d == MAJOR_ALARM) { edefMinorMask &= ~(edef_as[psub->e].mask); edefMajorMask |= edef_as[psub->e].mask; } else { edefMinorMask &= ~(edef_as[psub->e].mask); edefMajorMask &= ~(edef_as[psub->e].mask); } epicsMutexUnlock(mpgPatternModRWMutex_ps); return 0; } /*============================================================================= Name: mpgPatternModEdefState Abs: EDEF State Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- longSubRecord * psub read/write point to subroutine record Rem: Subroutine for EDEF:LOCA:UNIT:PATTERNSTATE Inputs: A - EDEF Number (starting from 1) B - EDEF control (0=off, 1=on) Outputs: E - EDEF Index K - # of counts since control off L - Control off (0 = don't turn control off, 1 = turn control off) VAL - Number of Matches so far Side: Read from memory used by 360hz processing (mutex protected) Ret: 0 = OK, -1 = ERROR ==============================================================================*/ static long mpgPatternModEdefState(longSubRecord *psub) { psub->e = psub->a - 1; psub->k = 0; psub->l = 0; psub->val = 0; if (psub->e >= EDEF_MAX) return -1; if ((!mpgPatternModRWMutex_ps) || epicsMutexLock(mpgPatternModRWMutex_ps)) return -1; psub->k = edef_as[psub->e].controlOffCount; if (psub->b && (!edef_as[psub->e].control)) psub->l = 1; psub->val = edef_as[psub->e].countSoFar; epicsMutexUnlock(mpgPatternModRWMutex_ps); return 0; } /*============================================================================= Name: mpgPatternModEdef Abs: 360Hz EDEF Processing Check to see if current beam pulse is to be used in any current event measurement definition. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- unsigned long * modifier_a read/write Modifier Bit Mask Array Rem: Assume mpgPatternModRWMutex is already taken. Side: None. Ret: 0 = control not changed, 1 = control changed ==============================================================================*/ static int mpgPatternModEdef(unsigned long *modifier_a, unsigned long *edefAvgDoneMask_p) { int idx; int changeFlag = 0; *edefAvgDoneMask_p = 0; for (idx = 0; idx < EDEF_MAX; idx++) { if (edef_as[idx].countZero) { edef_as[idx].countZero = 0; changeFlag = 1; } if (edef_as[idx].control) { /* if pattern beam code == EDEF beamcode. if zero, use any beam code */ if (evrPatternCheck(edef_as[idx].beamCode, 0, edef_as[idx].inclusion_a, edef_as[idx].exclusion_a, modifier_a)) { /* if the last cycle is done, turn CTRL off now. EVR IOCs should be finished now. */ if ((edef_as[idx].measCount != 0xFFFFFFFF) && (edef_as[idx].countSoFar >= edef_as[idx].maxCount)) { edef_as[idx].control = 0; changeFlag = 1; /* Set bit in modifer5 so that EVR IOCs will use the data on this pulse. */ } else { modifier_a[4] |= edef_as[idx].mask; edef_as[idx].countSoFar++; edef_as[idx].avgCountSoFar++; /* If the average is done, set a bit in the avgdone mask so that EVR IOCs finish their averages. */ if (edef_as[idx].avgCountSoFar >= edef_as[idx].avgCount) { *edefAvgDoneMask_p |= edef_as[idx].mask; edef_as[idx].avgCountSoFar = 0; } } /* Don't let CTRL stay on too long after measurements are done. */ } else if ((edef_as[idx].measCount != 0xFFFFFFFF) && (edef_as[idx].countSoFar >= edef_as[idx].maxCount)) { edef_as[idx].controlOffCount++; if (edef_as[idx].controlOffCount > MODULO720_COUNT) { edef_as[idx].control = 0; changeFlag = 1; } } } } return(changeFlag); } /*============================================================================= Name: mpgPatternMod Abs: 360Hz Processing of the Pattern Modifier Bits Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- evrMessagePattern_ts * pattern_ps read/write Pattern Rem: None. Side: None Ret: -1 if mutex error, 0 if OK. ==============================================================================*/ int mpgPatternMod(evrMessagePattern_ts *pattern_ps) { int changeFlag = 0; if ((!mpgPatternModRWMutex_ps) || epicsMutexLock(mpgPatternModRWMutex_ps)) { pattern_ps->modifier_a[0] |= MPG_IPLING; return -1; } pattern_ps->modifier_a[4] |= modifier5UserMask; pattern_ps->edefInitMask = edefInitMask; pattern_ps->edefMinorMask = edefMinorMask; pattern_ps->edefMajorMask = edefMajorMask; edefInitMask = 0; /* Don't change modifier bits that depend on a good pattern. */ if (!(pattern_ps->modifier_a[0] & MPG_IPLING)) { if (mpgPatternModEdef(pattern_ps->modifier_a, &pattern_ps->edefAvgDoneMask)) changeFlag = 1; if (mpgPatternModCnt(pattern_ps->modifier_a)) changeFlag = 1; } epicsMutexUnlock(mpgPatternModRWMutex_ps); /* Post an event if anything has changed or if we're at the 2 second mark. */ if (changeFlag || (pattern_ps->modifier_a[0] & MODULO720_MASK)) post_event(EVENT_MPG); return 0; } epicsRegisterFunction(mpgPatternModUser); epicsRegisterFunction(mpgPatternModCntInp); epicsRegisterFunction(mpgPatternModCntState); epicsRegisterFunction(mpgPatternModEdefInp); epicsRegisterFunction(mpgPatternModEdefState);