/****************************************************************************** * Module: Bunch length fcom * Filename: fcomBlm.c * Author: Qing Yang * Created: 1/5/2010 * Description: * Fcom related bunch length processing ****************************************************************************** * Modification history * * $Log: fcomBlm.c,v $ * Revision 1.6 2010/04/14 22:51:44 qing * Change to use evrTimeActive for TS. An old hack to forgive the slowness of BLM was causing the TS problem. That is fixed in the evr db definition. * * Revision 1.5 2010/03/30 19:13:48 qing * Add error codes for the fcom blob data. * * Revision 1.4 2010/03/24 16:59:30 qing * Change tmit to BPMS:LI24:901 because the one in li25 is not on the fnet. * * Revision 1.3 2010/03/23 00:51:37 qing * Change to use the actual tmit in production. * * Revision 1.2 2010/03/08 18:36:09 qing * Change time stamp to use evrTimeGetFromPipeline, to get the time stamp of the current slot. * * Revision 1.1 2010/03/04 23:09:48 qing * Initial check in, after testing in building 34 test network. * *****************************************************************************/ #include #include #include #include /* for struct subRecord */ #include /* DBADDR */ #include #include #include #include #include #include #include #include #include "fcomBlm.h" extern uint32_t drvPadUdpCommDebug; int blmFcomDebug = 0; /* fcom id for (station, channel) */ #define NUM_SECTIONS 2 int gBlenSection = 0; /* 0 = LI21, 1 = LI24 */ FcomID pvFcomRaw[NUM_RAW_FCOM_DATA]; FcomID pvFcomImax[NUM_RAW_FCOM_DATA]; FcomID pvFcomTmit; int fcomSyncMode; subRecord *pFbSubRecord[NUM_RAW_FCOM_DATA] = {NULL}; const char pvNameRaw[NUM_RAW_FCOM_DATA * NUM_SECTIONS][20] = { "BLEN:LI21:265:ARAW", "BLEN:LI21:265:BRAW", "BLEN:LI21:280:ARAW", "BLEN:LI21:280:BRAW", "BLEN:LI24:886:ARAW", "BLEN:LI24:886:BRAW", "BLEN:LI24:883:ARAW", "BLEN:LI24:883:BRAW" }; const char pvNameImax[NUM_RAW_FCOM_DATA * NUM_SECTIONS][20] = { "BLEN:LI21:265:AIMAX", "BLEN:LI21:265:BIMAX", "BLEN:LI21:280:AIMAX", "BLEN:LI21:280:BIMAX", "BLEN:LI24:886:AIMAX", "BLEN:LI24:886:BIMAX", "BLEN:LI24:883:AIMAX", "BLEN:LI24:883:BIMAX" }; const char pvNameTmit[NUM_SECTIONS][20] = { "BPMS:LI21:278:TMIT", /*"BPMS:LI22:301:TMIT", */ "BPMS:LI24:901:TMIT", }; /* Fcom */ FcomGroup fcomGroup = NULL; FcomBlob fcomTxBlob[2]; FcomBlobBlenData_t fcomBlenData[2]; /* data in the "blob" */ /****************************************************************************** * Function: tmitTimerIsr * Description: Timer ISR for tmit fcom receive retries * Inputs: * Returns: *****************************************************************************/ epicsEventId tmitTimerEvent = NULL; int gFcomTmitRetryNum = 3; /* we will retry three times to get the timit number */ unsigned int tmitDelayInClockCycles; int tmitTimerCount=0; /* debug count */ void tmitTimerIsr() { if(tmitTimerEvent) epicsEventSignal(tmitTimerEvent); tmitTimerCount++; return; } /* hack to fix a linker problem */ unsigned short int ntohs(unsigned short int x) { return x; } /****************************************************************************** * Function: fcomBlmInit * Description: Initialization for fcom. Everything we can do ahead of the time * to prepare for fcom processing. Getting fcomId for PVS, subscribe to * tmit, prepare blobs for sending data, etc * Inputs: * Returns: *****************************************************************************/ int fcomBlmInit(void) { int i; int rc = 0; int st = 0; DBADDR dbAddrImax; unsigned short int tmp=0; char *fcomMcPrefix; tmp=ntohs(tmp); /* hack */ /* find all the fcom ids for this section. The section is set in st.cmd 0=LI21, 1=LI24 */ if(gBlenSection != 0) { if(gBlenSection != 1) { printf("ERR: gBlenSection is set to %d, (it should be 0 for LI21 and 1 for LI24)\n", gBlenSection); printf("---Force the section to LI24 --- \n"); gBlenSection = 1; } } for(i=0; i:$(BLEN)IMAX Side: INPA to INPH = Coefficients for the given bunch length monitor INPI = divide by number to be used when filter is not in INPJ = filter in state, 1=filter in Ret: calculted Ipk Future: -1 when bunch readout or TMIT value is bad ==============================================================================*/ float fbckFcomIpkCalc(subRecord *psub, float bl, float tmit) { double *coeff_p; double Ipk = 0; double filter = 0; /* filter state, 0 = not in */ double divider = 0; /* divider to be used whenthe filter is not in */ coeff_p = &psub->a; /* Note A,B,C,... contiguous in subRecord structure */ filter = psub->j; divider = psub->i; if(blmFcomDebug) { printf("tmit=%e, bl=%e\n", tmit, bl); printf("c1=%e, c2=%e, c3=%e, c4=%e\n", coeff_p[0],coeff_p[1], coeff_p[2],coeff_p[3]); } /* now calculate. Leave of value too small or negative */ if( tmit < 0.000001 || bl < 0.000001 ) { psub->val = 0; return 0; } if (coeff_p[0]!=0) { Ipk = coeff_p[0]; if(blmFcomDebug) printf("c1 Ipk=%e\n", Ipk); } if (coeff_p[1]!=0) { Ipk = Ipk + coeff_p[1]*pow(tmit,coeff_p[2])*pow(bl,coeff_p[3]); if(blmFcomDebug) printf("c2, c3, c4, Ipk=%e\n", Ipk); } if (coeff_p[4]!=0) { Ipk = Ipk + coeff_p[4]*pow(tmit,coeff_p[5])*pow(bl,coeff_p[6]); if(blmFcomDebug) printf("c5, c6, c7 Ipk=%e\n", Ipk); } if (coeff_p[7]!=0) { Ipk = Ipk + coeff_p[7] /* take out coeff8 and coeff9 *pow(tmit,coeff_p[8])*pow(bl,coeff_p[9]) */; } /* adjust if filter is not in for filter status */ if (divider != 0) if (filter == 0) Ipk /= divider; return(Ipk); } /****************************************************************************** * Function: blenCalImax * Description: imax calc function * Inputs: * Returns: *****************************************************************************/ int blenCalImax(int recordIndex, float raw, float tmit, float *pImax) { subRecord *pSub; pSub = pFbSubRecord[recordIndex]; if(pSub == NULL) { *pImax = 0; return -1; } *pImax = fbckFcomIpkCalc(pSub, raw, tmit ); if ( blmFcomDebug ) printf("pSub=%x, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f\n", (int)pSub, pSub->a, pSub->b, pSub->c, pSub->d,pSub->e, pSub->f, pSub->g, pSub->h, pSub->i, pSub->j, pSub->k, pSub->l); return(0); } /****************************************************************************** * Function: fcomBlmProc * Description: fcom processing, called every beam cycle * Inputs: * Returns: *****************************************************************************/ int blmFcomPutGroupErr = 0; int blmFcomTsDebug=0; /* turn this on to take a snap shot of time stamp */ int blmFcomTs[4]; int tmitMissCount=0; int tmitTsMisCount=0; void fcomBlmProc( PadReply rply) /* the pad stream packet received from our lone pad */ { FcomBlob *pfcomRxBlob; int rc; float tmit = 0; int sendGroup; int i, j; unsigned short *pShort; int padData[4]; epicsTimeStamp epicsTs; epicsTimeStamp epicsTsTmit; /* do some work first before trying to get tmit to save overall time */ if(fcomSyncMode < 0) goto FCOM_EXIT; /* FCOM not available */ if(fcomGroup == NULL) { rc = fcomAllocGroup(FCOM_ID_ANY, &fcomGroup); if(rc != 0) goto FCOM_EXIT; /* give up. Try again next time */ } /* data is not guaranteed to be long word aligned, so copy in shorts */ pShort=(unsigned short *)rply->data; *(unsigned short *)(&(padData[0])) = *pShort++; *( (unsigned short *)(&(padData[0])) + 1) = *pShort++; *(unsigned short *)(&(padData[1])) = *pShort++; *( (unsigned short *)(&(padData[1])) + 1) = *pShort++; *(unsigned short *)(&(padData[2])) = *pShort++; *( (unsigned short *)(&(padData[2])) + 1) = *pShort++; *(unsigned short *)(&(padData[3])) = *pShort++; *( (unsigned short *)(&(padData[3])) + 1) = *pShort++; fcomBlenData[0].araw = (float)padData[0]; fcomBlenData[0].braw = (float)padData[1]; fcomBlenData[1].araw = (float)padData[2]; fcomBlenData[1].braw = (float)padData[3]; if ( blmFcomDebug ) { printf("STRM data: %f, %f, %f, %f, \n", fcomBlenData[0].araw, fcomBlenData[0].braw, fcomBlenData[1].araw, fcomBlenData[1].braw); } /* now try to get the TMIT data */ pfcomRxBlob = NULL; tmit = 0; /* clear the number. if timeout happens, we will know and not to use this */ for (i=0; ifcbl_bpm_T; epicsTsTmit.secPastEpoch = pfcomRxBlob->hdr.tsHi; epicsTsTmit.nsec = pfcomRxBlob->hdr.tsLo; /* set tmit value to zero, if the tmit status is invalid */ if(pfcomRxBlob->hdr.stat == FC_STAT_BPM_INVAL) tmit = 0; fcomReleaseBlob(&pfcomRxBlob); /* got the data, get out of the loop */ break; } /* start the timer and wait for a while: 300 us*/ BSP_timer_start(0, tmitDelayInClockCycles); rc = epicsEventWaitWithTimeout(tmitTimerEvent, 0.02); /* the time out comes to one tick, 20 msec */ } rc = evrTimeGetFromPipeline(&epicsTs, evrTimeActive, 0, 0, 0, 0, 0); if ( blmFcomDebug ) printf("rc = %d, epics time stamp %d, %d\n",rc , epicsTs.secPastEpoch, epicsTs.nsec ); if( blmFcomTsDebug) { /* take one shot of the time stamps */ blmFcomTs[0] = epicsTs.secPastEpoch; blmFcomTs[1] = epicsTs.nsec; blmFcomTs[2] = epicsTsTmit.secPastEpoch; blmFcomTs[3] = epicsTsTmit.nsec; blmFcomTsDebug=0; } if(epicsTs.nsec != epicsTsTmit.nsec) { tmit = 0; /* wrong time stamp, invalidate the number */ tmitTsMisCount++; } /* calculate the imax values, use whatever tmit we have */ sendGroup = 0; j=0; for(i=0; i<2; i++) { blenCalImax(j++, fcomBlenData[i].araw, tmit, &(fcomBlenData[i].aimax) ); blenCalImax(j++, fcomBlenData[i].braw, tmit, &(fcomBlenData[i].bimax) ); /* fill in the time stamp */ fcomTxBlob[i].hdr.tsHi = epicsTs.secPastEpoch; fcomTxBlob[i].hdr.tsLo = epicsTs.nsec; /* set the status */ fcomTxBlob[i].hdr.stat = 0; if(fcomBlenData[i].aimax == 0) fcomTxBlob[i].hdr.stat |= FC_STAT_BLEN_INVAL_AIMAX_MASK; if(fcomBlenData[i].bimax == 0) fcomTxBlob[i].hdr.stat |= FC_STAT_BLEN_INVAL_BIMAX_MASK; /* send the two blobs out */ rc = fcomAddGroup(fcomGroup, &(fcomTxBlob[i]) ); if ( blmFcomDebug ) printf("fcomBlob number=%d, araw=%f, aimax=%f, braw=%f, bimax=%f\n", i, fcomBlenData[i].araw, fcomBlenData[i].aimax, fcomBlenData[i].braw,fcomBlenData[i].bimax); if(rc!=0) { if ( blmFcomDebug ) errlogPrintf("fcomBlmProc: fcomAddGroup Failed %d\n", i); } else sendGroup++; } if(sendGroup) { rc = fcomPutGroup(fcomGroup); if ( rc != 0) { blmFcomPutGroupErr++; if(blmFcomDebug ) errlogPrintf("fcomBlmProc: fcomPutGroup Failed %d\n", rc); } } /* allocate the groups for the next time */ fcomGroup = NULL; rc = fcomAllocGroup(FCOM_ID_ANY, &fcomGroup); FCOM_EXIT: return; } /****************************************************************************** * Function: blmFcomShow * Description: Debug print function * Inputs: * Returns: *****************************************************************************/ void blmFcomShow() { int i; for(i=0; i<2; i++) { printf("fcomBlob number=%d, araw=%f, aimax=%f, braw=%f, bimax=%f\n", i, fcomBlenData[i].araw, fcomBlenData[i].aimax, fcomBlenData[i].braw,fcomBlenData[i].bimax); printf("fcomBlob status = %d\n", fcomTxBlob[i].hdr.stat); } printf("tmit miss count = %d\n", tmitTimerCount); printf("tmit tim stamp mismatch count = %d\n", tmitTsMisCount); printf("tmit timestamp: %d, %d\n", blmFcomTs[2], blmFcomTs[3]); printf("blen timestamp: %d, %d\n", blmFcomTs[0], blmFcomTs[1]); }