/*============================================================================= Name: drvLlrfUdpComm.c Abs: drvPadUdpComm callbacks for llrf application. Modelled after T.Straumann's BunchLength Application Code. Uses UdpComm-R2-x-y Auth: Shantha Condamoor (scondam) Mod: 1-Nov-2012 - Initial Release 6-Sept-2013 - (1) Removed FIDUCIAL_BLOB compiler option per T.Straumann's suggestion. (2) Installed fid_process_install() per T.Straumann's suggestion. (3) Commented out unused functions per T.Straumann's suggestion 16-Dec-2013 - Removed aligned start calculations 06-Mar-2014 - Added FCOM processing at the end of llrf_cook() 07-Mar-2014 - per Kukhee's recommendation removed FCOM processing from llrf_cook() and added it inside a subroutine record processing 18-Mar-2014 - Changes from code review done on 17-Mar-2014: (1) Fixed bug in midpoint calculation (2) Added check for nch (3) Added more comments -----------------------------------------------------------------------------*/ #include #include #include #include #include #include #include #include #include #include #include #include "drvLlrfPad.h" #include "padLlrf.h" /*#ifdef FIDUCIAL_BLOB */ #include /*#endif */ #ifdef FCOM_BLOB #include "fcomLlrf.h" #endif #define PAD_MAX_INT (2147483647) /* 4 byte int - 1 bit for sign */ /* int drvLlrfPadDebug = 0; */ volatile unsigned llrfCooked = 0; /* scondam: 6-Sept-2013: Removed FIDUCUAL_BLOB per T.Straumann's recommendation. From Till's email: "I would recommend against building the same application multiple times with different macro expansions. I would use run-time code instead. E.g., in order to change the active timeslots you can simply do that from your startup script. I don't see the necessity for the conditional compilation of FIDUCIAL_BLOB either... */ /* #ifdef FIDUCIAL_BLOB */ volatile unsigned llrfFidProcessActiveTimeslots = TIMESLOT1_MASK | TIMESLOT4_MASK; /* #endif */ /* Overflow bit set - discard data point */ int overflow(LlrfChannel ch_p, LlrfOutNumber v) { int j; if ( v & 0x1 ) { /* Overflow bit set - discard data point */ if (ch_p->Overflow < PAD_MAX_INT) { ch_p->Overflow++; } else { ch_p->Overflow = 0; } return 1; } return 0; } void drvPadCalcPhasAmpl(LlrfChannel ch_p, int16_t *v_p, int N, int fragment) { register LlrfOutNumber v; LlrfOutNumber sacc[6] = {0}; /* six degrees */ LlrfOutNumber nacc[6] = {0}; LlrfOutNumber ssacc[6] = {0}; /* for sample 1 */ LlrfOutNumber snacc[6] = {0}; int aligned_sample_start = 0; int aligned_sample_end = 0; int midpoint_sample = 0; int i=0; int oflow=0; int mod =0; switch (ch_p->freq) { case 119: mod=6; break; case 2856: mod=4; break; case 68: mod=3; break; case 0: mod=1; break; /* default case of 0 is simple averaging */ } /* sample 0 */ int aligned_start = ch_p->ofst; int aligned_end = ch_p->ofst+ch_p->size; int midpoint = ch_p->ofst + (ch_p->size /2); if (aligned_end > N) { aligned_end = N; } if (aligned_start > N) { aligned_start = N; } ch_p->aligned_start = aligned_start; ch_p->aligned_end = aligned_end; ch_p->x = midpoint; if (ch_p->Samples) { /* sample 1 ? */ aligned_sample_start = ch_p->Samples->ofst; aligned_sample_end = ch_p->Samples->ofst+ch_p->Samples->size; midpoint_sample = ch_p->Samples->ofst + (ch_p->Samples->size /2); /*if (aligned_sample_start % mod) { aligned_sample_start += mod - (aligned_sample_start % mod); } if (aligned_sample_end % mod) { aligned_sample_end += mod - (aligned_sample_end % mod); } */ if (aligned_sample_end > N) { aligned_sample_end = N; } if (aligned_sample_start > N) { aligned_sample_start = N; } ch_p->Samples->aligned_start = aligned_sample_start; ch_p->Samples->aligned_end = aligned_sample_end; ch_p->Samples->x = midpoint_sample; } for ( i=0; i=aligned_start) && (i Samples) { /* sample 1 ? */ if (( i>=aligned_sample_start) && (i freq, we use portions of the array for calculating I/Q. For example, 119 MHz signals needs all 6 values. 2856 MHz signals need 4 values only. The other two are 0's in this case, and are not used in IQ calculations in padLlrf.c. Similarly, 68 MHz signals need only 3 values - rest are unused. Simple waveforms need only one value. */ if (!fragment) { switch (ch_p->freq) { case 119: ch_p->sum[5] = sacc[5]; ch_p->num[5] = nacc[5]; ch_p->sum[4] = sacc[4]; ch_p->num[4] = nacc[4]; case 2856: ch_p->sum[3] = sacc[3]; ch_p->num[3] = nacc[3]; case 68: ch_p->sum[2] = sacc[2]; ch_p->num[2] = nacc[2]; ch_p->sum[1] = sacc[1]; ch_p->num[1] = nacc[1]; case 0: ch_p->sum[0] = sacc[0]; ch_p->num[0] = nacc[0]; break; } } else { switch (ch_p->freq) { case 119: ch_p->sum_acc[5] = sacc[5]; ch_p->num_acc[5] = nacc[5]; ch_p->sum_acc[4] = sacc[4]; ch_p->num_acc[4] = nacc[4]; case 2856: ch_p->sum_acc[3] = sacc[3]; ch_p->num_acc[3] = nacc[3]; case 68: ch_p->sum_acc[2] = sacc[2]; ch_p->num_acc[2] = nacc[2]; ch_p->sum_acc[1] = sacc[1]; ch_p->num_acc[1] = nacc[1]; case 0: ch_p->sum_acc[0] = sacc[0]; ch_p->num_acc[0] = nacc[0]; break; } } if (ch_p->Samples) { /* sample 1 ? */ if (!fragment) { switch (ch_p->freq) { case 119: ch_p->Samples->sum[5] = ssacc[5]; ch_p->Samples->num[5] = snacc[5]; ch_p->Samples->sum[4] = ssacc[4]; ch_p->Samples->num[4] = snacc[4]; case 2856: ch_p->Samples->sum[3] = ssacc[3]; ch_p->Samples->num[3] = snacc[3]; case 68: ch_p->Samples->sum[2] = ssacc[2]; ch_p->Samples->num[2] = snacc[2]; ch_p->Samples->sum[1] = ssacc[1]; ch_p->Samples->num[1] = snacc[1]; case 0: ch_p->Samples->sum[0] = ssacc[0]; ch_p->Samples->num[0] = snacc[0]; break; } } else { switch (ch_p->freq) { case 119: ch_p->Samples->sum_acc[5] = ssacc[5]; ch_p->Samples->num_acc[5] = snacc[5]; ch_p->Samples->sum_acc[4] = ssacc[4]; ch_p->Samples->num_acc[4] = snacc[4]; case 2856: ch_p->Samples->sum_acc[3] = ssacc[3]; ch_p->Samples->num_acc[3] = snacc[3]; case 68: ch_p->Samples->sum_acc[2] = ssacc[2]; ch_p->Samples->num_acc[2] = snacc[2]; ch_p->Samples->sum_acc[1] = ssacc[1]; ch_p->Samples->num_acc[1] = snacc[1]; case 0: ch_p->Samples->sum_acc[0] = ssacc[0]; ch_p->Samples->num_acc[0] = snacc[0]; break; } } } } static int llrf_cook(PadReply rply, int nsamples, int layout_cm, int kind, void *closure) { int idx = PADRPLY_STRM_CMD_IDX_GET( rply->strm_cmd_idx ); int nch = PADRPLY_STRM_CHANNELS_IN_STRM( rply ); int N = PADRPLY_STRM_NSAMPLES( rply ); int chn, frg, n_frg; LlrfChannel ch_p; LlrfStation st_p; int i,j,k; int16_t *v_p = ((int16_t*)rply->data) - 1; register LlrfInNumber *vec_p; epicsTimeStamp ts; epicsTimeStamp tsSum; /* scondam: 18-Mar-2014: Add check for nch as it comes from unverified network protocol */ if ( layout_cm || nch > PAD_NUM_CHANNELS_ON_BOARD ) return -1; /* Fatal error */ if ( ! (st_p = llrfGetStation( rply->chnl )) ) { return -1; } if ( (chn = PADRPLY_STRM_CHANNELS_IN_FRAG( rply )) > 1 ) { /* all channels in a single packet (provided we're row-major) */ if ( chn != nch ) { errlogPrintf("llrf_cook: channel number mismatch: # in frag: %i, in stream: %i\n", chn, nch); return -1; } N /= chn; if ( N > sizeof( ((LlrfChannel)0)->vec )/sizeof( ((LlrfChannel)0)->vec[0] ) ) { /* vector too small */ errlogPrintf("llrf_cook: Too many samples (%i) for processing vector (dim: %i)\n", N, sizeof( ((LlrfChannel)0)->vec )/sizeof( ((LlrfChannel)0)->vec[0] )); return -1; } ts.nsec = ntohl( rply->timestampLo ); ts.secPastEpoch = ntohl( rply->timestampHi ); epicsMutexMustLock( st_p->mutex ); /* unfragmented */ for ( j=0, ch_p = st_p->chnls; jnumber = j; /* channel number */ drvPadCalcPhasAmpl(ch_p, (int16_t*) v_p, N, 0); st_p->tsSum = ts; st_p->timedout &= ~LLRF_TIMEDOUT(j); } epicsMutexUnlock( st_p->mutex ); st_p->chnlsDone = nch; } else { /* XID is opaque endian-ness; we just compare values */ if ( rply->xid != st_p->xid ) { /* fragment part of a new sequence */ for ( i = 0; i < nch; i++ ) { for ( j=0; j < 6; j++) { st_p->chnls[i].sum_acc[j] = 0; st_p->chnls[i].num_acc[j] = 0; st_p->chnls[i].number = i; /* channel number */ if (ch_p->Samples) { /* sample 1 ? */ st_p->chnls[i].Samples->sum_acc[j] = 0; st_p->chnls[i].Samples->num_acc[j] = 0; } } st_p->chnls[i].frags = 0; } st_p->xid = rply->xid; st_p->chnlsDone = 0; } /* Unfortunately, the stream data itself doesn't provide information (direct * or indirect about the row dimension -- there was just not enough space * in the protocol header to add this way after the original design). * Therefore, we must inquire the sending side who knows... * passing a negative number of samples just yields the currently * configured number of samples per channel. From this and the number of * samples in this fragment we can compute the row dimension. Assuming * that all fragments are of the same size. */ n_frg = (drvPadUdpCommStrmSetNSamples( rply->chnl, -1 ) + N - 1 ) / N; chn = idx / n_frg; frg = idx % n_frg; if ( N*(frg+1) > sizeof(ch_p->vec)/sizeof(ch_p->vec[0]) ) { /* bad N or frg (too big) */ errlogPrintf("llrf_cook: bad N: %i or frg: %i (too big) (idx %i, n_frg %i) for processing vector (dim: %i)\n", N, frg, idx, n_frg, sizeof( ((LlrfChannel)0)->vec )/sizeof( ((LlrfChannel)0)->vec[0] )); return -1; } epicsMutexMustLock( st_p->mutex ); ch_p = st_p->chnls + chn; vec_p = ch_p->vec + N*frg - 1; /* fragmented */ drvPadCalcPhasAmpl(ch_p, (int16_t*) v_p, N, 1); ch_p->frags |= (1<frags == (1<sum[0] = ch_p->sum_acc[0]; ch_p->sum[1] = ch_p->sum_acc[1]; ch_p->sum[2] = ch_p->sum_acc[2]; ch_p->sum[3] = ch_p->sum_acc[3]; ch_p->sum[4] = ch_p->sum_acc[4]; ch_p->sum[5] = ch_p->sum_acc[5]; ch_p->num[0] = ch_p->num_acc[0]; ch_p->num[1] = ch_p->num_acc[1]; ch_p->num[2] = ch_p->num_acc[2]; ch_p->num[3] = ch_p->num_acc[3]; ch_p->num[4] = ch_p->num_acc[4]; ch_p->num[5] = ch_p->num_acc[5]; if (ch_p->Samples) { /* sample 1 ? */ ch_p->Samples->sum[0] = ch_p->Samples->sum_acc[0]; ch_p->Samples->sum[1] = ch_p->Samples->sum_acc[1]; ch_p->Samples->sum[2] = ch_p->Samples->sum_acc[2]; ch_p->Samples->sum[3] = ch_p->Samples->sum_acc[3]; ch_p->Samples->sum[4] = ch_p->Samples->sum_acc[4]; ch_p->Samples->sum[5] = ch_p->Samples->sum_acc[5]; ch_p->Samples->num[0] = ch_p->Samples->num_acc[0]; ch_p->Samples->num[1] = ch_p->Samples->num_acc[1]; ch_p->Samples->num[2] = ch_p->Samples->num_acc[2]; ch_p->Samples->num[3] = ch_p->Samples->num_acc[3]; ch_p->Samples->num[4] = ch_p->Samples->num_acc[4]; ch_p->Samples->num[5] = ch_p->Samples->num_acc[5]; } ch_p->frags = 0; st_p->timedout &= ~LLRF_TIMEDOUT(chn); st_p->chnlsDone++; } st_p->tsSum.nsec = ntohl( rply->timestampLo ); st_p->tsSum.secPastEpoch = ntohl( rply->timestampHi ); epicsMutexUnlock( st_p->mutex ); } if ( st_p->chnlsDone == nch ) { epicsEventSignal( st_p->rawDataReady ); padLlrfProc(st_p); #ifdef FCOM_BLOB /* call this function only for stations that need to ship fcom data */ /****************************************************************************** * Comment: scondam: 7-Mar-2014 * Description: fcomLlrfProc() moved to subroutine fcomGetAVG() - in dbSubLlrf.c * It is left here in source code for possible future inclusion * to setup and process fcom activities at the end of cook for each station. * Or fcomLlrfProc() can also be called directly from a separate task like llrfFcomTask * * Inputs: LlrfStation * Returns: *****************************************************************************/ /* fcomLlrfProc(st_p); */ #endif post_event(st_p->epics_event_no); } llrfCooked++; return 0; } static int llrf_get_prefs(DrvPadUdpCommPrefs prefs_p) { prefs_p->nsamples = LLRF_NUM_SAMPLES; prefs_p->d32 = 0; prefs_p->nsamples_dynamic = 0; prefs_p->nchannels_dynamic = 0; prefs_p->col_major = 0; return 0; } static void llrf_watchdog(int channel, int kind, void *closure) { LlrfStation sta_p; if ( (sta_p = llrfGetStation(channel)) ) { sta_p->timedout = LLRF_TIMEDOUT_ALL; } } static int llrf_cook(PadReply, int, int, int, void *); /* scondam: 6-Sept-2013: Removed FIDUCUAL_BLOB per T.Straumann's recommendation. */ /* #ifdef FIDUCIAL_BLOB */ static void llrfFidProcess(void *unused) { if ( ( fidProcessGeneric( 0, llrfFidProcessActiveTimeslots ) & llrfFidProcessActiveTimeslots ) ) { #ifdef FCOM_BLOB /* active timeslot; resume fcom processing task(s) */ /* llrfFcomTaskResumeAll(); */ #endif /* scondam: 6-Sept-2013: fcom processing for LLRF is handled within PAU in xyzDataProc.c for a particular station. Hence no need for any special LLRF fcom processing. However, installing llrfFidProcess per T.Straumann's recommendation for the following reason: From Till's email: "you still want to install llrfFidProcess so that you can change the active timeslots when needed." "One reason is that you can set the active timeslots. This makes sure the drvPadUdpCommSendStartAll() commands are not getting in the way of any PAD triggers. StartAll' serves an additional purpose besides propagating timestamps to the PAD: as a side-effect it lets come PADs back on-line after a power-cycle/reset (of a PAD)." */ } } static void llrf_fid_process_install(void) { fidProcessHasBeam = 1; evrTimeRegister(llrfFidProcess, 0); printf("LLRF FID PROCESS INSTALL\n"); } /* #endif */ /* scondam: 6-Sept-2013: Commented out unused functions per T.Straumann's recommendation. */ /* static int llrf_sim_pos(int32_t *vals_p, float a1, float b1, float a2, float b2, float psi, void *parms) { vals_p[0] = a1; vals_p[1] = b1; vals_p[2] = a2; vals_p[3] = b2; return 0; } */ DrvPadUdpCommCallbacksRec llrf_udpCommCallbacks = { cook: llrf_cook, watchKinds: (1), watchdog: llrf_watchdog, get_prefs: llrf_get_prefs, /* scondam: 6-Sept-2013: Removed FIDUCUAL_BLOB and installed fid_process_install function per T.Straumann's recommendation.*/ /* #ifdef FIDUCIAL_BLOB */ fid_process_install: llrf_fid_process_install, /* #else */ /* fid_process_install: 0, */ /* #endif */ sim_pos: 0, }; DrvPadUdpCommCallbacks drvLlrfUdpCommCallbacks = &llrf_udpCommCallbacks; static void llrf_udpCommRegistrar(void) { registryAdd( (void*)drvPadUdpCommRegistryId, "drvLlrfUdpCommCallbacks", &llrf_udpCommCallbacks ); } epicsExportRegistrar(llrf_udpCommRegistrar);