/*-*-C-*-*/ /*============================================================================= Name: klysSub.c Abs: This file contains subroutine support for klystron records Rem: Functions Description ---------- ----------- klysInit General initialization klysStatusCheck Get accelerate/standby/error status of klys countBits Count bits set in word klysTmskCheck Compare klys TMSK to beam code TMSK Side: Auth: 09-Dec-2010, Sonya Hoobler (SONYA) Rev: dd-mmm-yyyy, Reviewer's Name (USERNAME) -------------------------------------------------------------- Mod: 24-Feb-2023, K. Luchini (LUCHINI): epics upgrade epics V7 (64-bit) chg unsigned long to epicsUInt32 ==============================================================================*/ #include /*============================================================================= Name: klysInit Abs: Klystron initialization subroutine Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read/write pointer to subroutine record Rem: Side: Ret: int 0 - Successful (always) ==============================================================================*/ static int klysInit(subRecord *psub) { return 0; } /*============================================================================= Name: klysStatusCheck Abs: On a given beamcode, check klystron trigger activate/deactivate status, HSTA, and TMSK status to create overall accelerate/standby/error status. This is a subset of the VMS function linac_klys_stat. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read/write pointer to subroutine record Input: ------- INPA = Klystron HSTA (longin record?) INPB = Klystron trigger status (bo record) INPC = Klystron beamcode TMSK status (sub record) Output: ------- VAL = Mask of status bits set (see klystron.h for details) Rem: Side: Ret: int 0 ==============================================================================*/ static int klysStatusCheck(subRecord *psub) { epicsUInt32 linklysta = 0; epicsUInt32 hsta; if( psub == NULL) return 0; hsta = psub->a; if ( hsta & HSTA_GOOD ) { if( psub->b == 0 ) { if ( psub->c == TMSK_OK ) linklysta |= LINKLYSTA_STANDBY; else { linklysta |= LINKLYSTA_BAD; recGblSetSevr(psub, STATE_ALARM, MAJOR_ALARM); } } else linklysta |= LINKLYSTA_ACCEL; } else { linklysta |= LINKLYSTA_BAD; recGblSetSevr(psub, STATE_ALARM, MAJOR_ALARM); } psub->val = linklysta; /* here linac_klys_stat checks hsta for sled-tune, sled-ed, pampl, pphas and adds that data to linklysta. not sure if needed for upgrade, leaving out for now */ return 0; } /*=================================================================== Name: countBits Abs: Count the number of bits that are set in a word: loop through the word, adding up the set bits. Args: unsigned long word: word to count bits in Rem: Side: Ret: unsigned short count of set bits =====================================================================*/ int countBits( epicsUInt32 word ) { unsigned short n = 0; while(word > 0) { if ((word & 1) == 1) { n++; } word>>=1; } return n; } /*============================================================================= Name: klysTmskCheck Rem: Check that tmsk_bc is a logical subset of tmsk. Used to verify that a beam code's TMSK does not require a klystron to trigger at a rate greater than allowed by the individual klystron's TMSK. Each TMSK is 2 32-bit words. 36 bits are used to encode the rate masks (the left word and the right 4 bits of the right word). 8 bits encode the rate/10 (the left 8 bits of the right word). This is also the number of bits set in the tmsk (each bit represents 10 Hz) Strip the encoded rate from the TMSK. AND the klystron tmsk with the NOT (negated) beam tmsk. As an additional check, verify that the encoded rate matches the number of 10 Hz rate bits set. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read/write pointer to subroutine record Input: ------- INPA = Klystron TMSK (2-element waveform) INPB = Beamcode TMSK (2-element waveform) Output: ------- VAL = TMSK_OK if Klys TMSK is a subset of beamcode TMSK and rate bits match encoded rates TMSK_BAD if not Side: Ret: TMSK comparison status, TMSK_OK or TMSK-BAD Functions: countBits ==============================================================================*/ int klysTmskCheck( epicsUInt32 *tmsk, epicsUInt32 *tmsk_bc ) { int rval; unsigned short r_tmsk; /* Encoded rates */ unsigned short r_tmsk_bc; r_tmsk = (tmsk[1] & 0xFF000000) >> 24; r_tmsk_bc = (tmsk_bc[1] & 0xFF000000) >> 24; tmsk[1] = tmsk[1] & 0xFF; tmsk_bc[1] = tmsk_bc[1] & 0xFF; if (((tmsk_bc[0] & ~tmsk[0]) == 0) && ((tmsk_bc[1] & ~tmsk[1]) == 0)) { if (r_tmsk == (countBits(tmsk[0]) + countBits(tmsk[1])) && r_tmsk_bc == (countBits(tmsk_bc[0]) + countBits(tmsk_bc[1]))) { rval = TMSK_OK; } else rval = TMSK_BAD; } else rval = TMSK_BAD; return rval; } /*============================================================================= Name: klysTmskCheckSub Rem: Call klysTmskcheck to verify that tmsk_bc is a logical subset of tmsk. Used to verify that a beam code's TMSK does not require a klystron to trigger at a rate greater than allowed by the individual klystron's TMSK. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read/write pointer to subroutine record Input: ------- INPA = Klystron TMSK (2-element waveform) INPB = Beamcode TMSK (2-element waveform) Output: ------- VAL = TMSK_OK if Klys TMSK is a subset of beamcode TMSK and rate bits match encoded rates TMSK_BAD if not Side: Ret: return value from klysTmskCheck Functions: klysTmskCheck ==============================================================================*/ static long klysTmskCheckInit(struct aSubRecord *pasub) { size_t n_tmsk_words = 2*sizeof(epicsUInt32); epicsUInt32 *tmsk = (epicsUInt32 *) pasub->a; memset(tmsk, 0x00, n_tmsk_words); epicsUInt32 *tmsk_bc = (epicsUInt32 *) pasub->b; memset(tmsk_bc, 0x00, n_tmsk_words); return 0; } static long klysTmskCheckSub(struct aSubRecord *pasub) { int rval; if( pasub == NULL) return TMSK_BAD; rval = klysTmskCheck( (epicsUInt32 *) pasub->a, (epicsUInt32 *) pasub->b ); if ( rval == TMSK_BAD ) recGblSetSevr(pasub, STATE_ALARM, MAJOR_ALARM); return rval; } epicsRegisterFunction( klysInit); epicsRegisterFunction( klysStatusCheck); epicsRegisterFunction( countBits); epicsRegisterFunction( klysTmskCheckInit); epicsRegisterFunction( klysTmskCheckSub);