/*============================================================================= Name: mpsEvrProc.c mpsEvrProcInit - MPS EVR Pattern Processing Initialization mpsEvrProc - MPS EVR Pattern Processing mpsTask - MPS EVR Task for testing Abs: This file contains all subroutine support for mps evr pattern processing records. 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: Rev: -----------------------------------------------------------------------------*/ #include "copyright_SLAC.h" /* SLAC copyright comments */ /*----------------------------------------------------------------------------- Mod: (newest to oldest) =============================================================================*/ #include /* for struct subRecord */ #include /* for struct subRecord */ #include /* for epicsExport */ #include /* for epicsRegisterFunction */ #include /* epicsMutexId and protos */ #include /* for epicsThreadCreate */ #include /* for epicsThreadCreate */ #include /* MAX_EVR_TIME,evrTimeNext3 */ #include /* evrPatternModifier5_ts */ #include "Timing.h" static timingData mpsPulse_as[MAX_EVR_TIME]; static epicsMutexId mpsPulseRWMutex_ps = 0; static epicsEventId mpsTaskEventSem = 0; int mpsTaskCounter = 0; /* Testing */ void mpsTaskPrint() { int i; timingData mpsPrint_as[MAX_EVR_TIME]; if (mpsPulseRWMutex_ps && (!epicsMutexLock(mpsPulseRWMutex_ps))) { for (i=0; i < MAX_EVR_TIME; i++) { mpsPrint_as[i] = mpsPulse_as[i]; } epicsMutexUnlock(mpsPulseRWMutex_ps); } printf("Num Seconds Nanoseconds Timeslot Pulse ID Beam Requested\n"); printf("---------------------------------------------------------------\n"); for (i = 0; i < MAX_EVR_TIME; i++) { printf(" %d %9d %9d %d 0x%05X %c\n", i, mpsPrint_as[i].timestamp.secPastEpoch, TIMESTAMP_NSEC_WITHOUT_PULSE_ID(mpsPrint_as[i].timestamp), mpsPrint_as[i].timeslot, mpsPrint_as[i].pulseid, (mpsPrint_as[i].beamRequested ? 'Y':'N')); } return; } /* Testing */ static int mpsTask() { epicsEventWaitStatus status; for (;;) { status = epicsEventWaitWithTimeout(mpsTaskEventSem, 0.04); if (status == epicsEventWaitOK) { mpsTaskCounter++; } else if (status == epicsEventWaitTimeout) { printf("MPS Task timeout\n"); } else return -1; } return 0; } /*============================================================================= Name: mpsEvrProcInit Abs: Initialization for 360Hz MPS EVR processing. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read point to subroutine record Rem: Subroutine for Side: ? Sub Inputs/ Outputs: Inputs: None Outputs: None Ret: 0 = OK, -1 = Error. ==============================================================================*/ static int mpsEvrProcInit(subRecord *psub) { int err; /* Create the semaphore used to wake up the mps task */ mpsTaskEventSem = epicsEventMustCreate(epicsEventEmpty); if (!mpsTaskEventSem) { errlogPrintf("mpsEvrProcInit: unable to create the MPS task semaphore\n"); return -1; } /* Create the mutex used to protect mps pulse data */ mpsPulseRWMutex_ps = epicsMutexCreate(); if (!mpsPulseRWMutex_ps) { errlogPrintf("mpsEvrProcInit: unable to create the MPS pulse data mutex\n"); return -1; } /* Create the MPS task */ err = epicsThreadCreate("mpsTask", epicsThreadPriorityHigh, epicsThreadGetStackSize(epicsThreadStackMedium), (EPICSTHREADFUNC) mpsTask, 0); if (err == 0) { errlogPrintf("mpsEvrProcInit: unable to create the EVR task\n"); return -1; } /* do whatever initialization is needed here */ return 0; } /*============================================================================= Name: mpsEvrProc Abs: 360Hz MPS EVR Processing. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read point to subroutine record Rem: Subroutine for Side: Mutex lock/unlock, event signal. Sub Inputs/ Outputs: Inputs: A - Time Slot N B - PULSEID N C - Modifier5 N (has beam request bit) D - Spare N E - Time Slot N-1 F - PULSEID N-1 G - Modifier5 N-1 H - Spare N-1 I - Time Slot N-2 J - PULSEID N-2 K - Modifier5 N-2 L - Spare N-2 Outputs: VAL - Status Ret: 0 = OK, -1 = Error. =============================================================================*/ static long mpsEvrProc(subRecord *psub) { double *inputField; epicsUInt32 modifier5; long status; int i; /* Set pointer to first input field. Note that all input field types are doubles. */ inputField = &psub->a; if (mpsPulseRWMutex_ps && (!epicsMutexLock(mpsPulseRWMutex_ps))) { for (i = 0; i < evrTimeNext3; i++) { /* For N'th pulse in the future (where N=i)... */ /* Get timestamp */ evrTimeGetFromPipeline(&mpsPulse_as[i].timestamp, i); /* Get timeslot, (A, E, I) */ mpsPulse_as[i].timeslot = (epicsUInt32)(*inputField); /* Get pulse ID, (B, F, J) */ inputField++; mpsPulse_as[i].pulseid = (epicsUInt32)(*inputField); /* Get beam requested, ((C, G, K) & MOD_5_BEAMFULL_MASK) */ inputField++; modifier5 = (epicsUInt32)(*inputField); mpsPulse_as[i].beamRequested = ((modifier5 & MOD5_BEAMFULL_MASK) ? 1 : 0); /* Get spare data (D, H, L)*/ inputField++; mpsPulse_as[i].reserved = (epicsUInt32)(*inputField); /* Move pointer to start of N+1'th pulse's data */ inputField++; } epicsMutexUnlock(mpsPulseRWMutex_ps); if (mpsTaskEventSem) { epicsEventSignal(mpsTaskEventSem); } /* Successful */ status = 0; } else { /* Unsuccessful */ status = -1; } psub->val = status; return status; } epicsRegisterFunction(mpsEvrProcInit); epicsRegisterFunction(mpsEvrProc);