/****************************************************************************** * Module: Llrf fcom * Filename: fcomLlrf.c * Author: Shantha Condamoor * Created: 24-Feb-2014 * Description: * Fcom functions to send LLRF Readback PVs on FNET (PAVG,AAVG etc.) ******************************************************************************/ #include #include #include #include #include #include /* for struct subRecord */ #include /* DBADDR */ #include /* DBADDR */ #include #include #include #include #include #include #include #include #include #include #include #include "fcomLlrf.h" volatile unsigned llrfFcomTimeoutMs = 5; int llrfFcomDebug = 0; volatile unsigned llrfFcomMaxPadDelayUs = 0; int gLlrfSection = 0; /* DMP1=0, LI24=1, LI21=2, IN20=3 */ FcomID pvFcomAvg[NUM_AVG_FCOM_DATA]; int fcomSyncMode = 0; subRecord *pFbSubRecord[NUM_AVG_FCOM_DATA] = {NULL}; DBADDR dbAddrFcomAvg[NUM_AVG_FCOM_DATA]; volatile int fcomTimeouts = 0; volatile int fcomLlrfFcomErrors = 0; volatile int fcomLlrfPadTimeouts = 0; volatile int fcomLlrfProcessed = 0; /* scondam: 25-Feb-2014 Max Length of PV name string is 60+1 characters in EPICS base R3-14-12 */ /* PBR is BSA PV whose datasource PV is PAVG. ABR is BSA PV whose datasource PV is AAVG */ const char pvNameAvg[NUM_AVG_FCOM_DATA * NUM_SECTIONS][19] = { "TCAV:DMP1:360:PAVG", "TCAV:DMP1:360:AAVG" }; const char pvNameFcomAvg[NUM_AVG_FCOM_DATA * NUM_SECTIONS][17] = { "TCAV:DMP1:360:PV", "TCAV:DMP1:360:AV" }; /* Fcom */ FcomGroup fcomGroup = NULL; FcomBlob fcomTxBlob[2]; FcomBlobLlrfData_t fcomLlrfData[2]; /* data in the "blob" */ /****************************************************************************** * Function: fcomLlrfInit * Description: Initialization for LLRF readback PVs for fcom. Everything we can do ahead of the time * to prepare for fcom processing. Getting fcomId for PVS and * prepare blobs for sending data, etc * Inputs: * Returns: *****************************************************************************/ int fcomLlrfInit(void) { int i; int rc = 0; int st = 0; char *fcomMcPrefix; /* We need a system clock frequency >= ~1kHz so that we can have * sub-fiducial timeouts. */ if ( sysClkPeriodGet() > 1./900. ) { cantProceed("FATAL ERROR: Need a OS system clock frequency >= ~1kHz -- are you using a recent GeSys (rtems)"); } /* find all the fcom ids for this section. The section is set in st.cmd DMP1=0, LI24=1, LI21=2, IN20=3 */ if(gLlrfSection >= NUM_SECTIONS) { printf("ERR: gLlrfSection is set to %d, (it should be 0-3 where DMP1=0, LI24=1, LI21=2, IN20=3\n", gLlrfSection); printf("---Force the section to DMP1 --- \n"); gLlrfSection = 0; } for(i=0; imutex ); tsSum = station->tsSum; epicsMutexUnlock( station->mutex ); rc = evrTimeGetFromPipeline(&epicsTs, evrTimeActive, 0, 0, 0, 0, 0); if( llrfFcomTsDebug) { /* take one shot of the time stamps */ llrfFcomTs[0] = epicsTs.secPastEpoch; llrfFcomTs[1] = epicsTs.nsec; llrfFcomTsDebug=0; } /* calculate the fcom values */ sendGroup = 0; j=0; for(i=0; imutex ); station->fcomPavg = fcomLlrfData[0].pavg; station->fcomAavg = fcomLlrfData[0].aavg; station->tsSum = tsSum; epicsMutexUnlock( station->mutex ); /* allocate the groups for the next time */ fcomGroup = NULL; rc = fcomAllocGroup(FCOM_ID_ANY, &fcomGroup); /* event already posted in end of drvLlrfUdpComm.c llrf_cook() funciton */ /* post_event(station->epics_event_no); */ fcomLlrfProcessed++; FCOM_EXIT: return; } /****************************************************************************** * Function: llrfFcomShow * Description: Debug print function * Inputs: * Returns: *****************************************************************************/ void llrfFcomShow() { int i; for(i=0; irawDataReady ); /* Now we need to wait for PAD data */ epicsTimeGetCurrent( &now ); diff = epicsTimeDiffInSeconds( &now, &then ); diffus = (unsigned)(diff * 1000000.); switch ( st ) { case 0: break; case FCOM_ERR_TIMEDOUT: fcomTimeouts++; /* FIXME: HANDLE FCOM TIMEOUT */ continue; default: /* FIXME: Handle other FCOM errors */ fcomLlrfFcomErrors++; continue; } if ( (remaining = ((double)llrfFcomTimeoutMs/1000.0 - diff)) < 0. ) { remaining = 0.; } st = epicsEventWaitWithTimeout( station->rawDataReady, remaining ); /* Now we need to wait for PAD data */ epicsTimeGetCurrent( &now ); diff = epicsTimeDiffInSeconds( &now, &then ); diffus = (unsigned)(diff * 1000000.); if ( diffus > llrfFcomMaxPadDelayUs ) llrfFcomMaxPadDelayUs = diffus; if ( st ) { /* FIXME: Handle UDPCOMM timeout */ fcomLlrfPadTimeouts++; continue; } /* Everything OK */ /* get pavg and avg from subroutine fcomGetAVG() */ fcomLlrfProc(sta_p,pavg,aavg); } } #endif