#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define PI 3.14159265359 #define MAX(A,B) ((A)>(B) ? (A) : (B)) /* Detune frequency calculator from chirp data */ static long calc_df_chirp(aSubRecord* prec) { short debug = (prec->tpro > 1) ? 1 : 0; s_state state; double complex fit[2]; double rms[2]; double sampt = 1.0; char chirpmsg[255]; epicsUInt32 len = prec->nea; /* actual output length */ short CHIRP = *(short*)prec->e; short PULSE = *(short*)prec->g; double *CAVI = (double*)prec->a, *CAVQ = (double*)prec->b, *FWDI = (double*)prec->c, *FWDQ = (double*)prec->d, ATHRES = *(double*)prec->h, FTHRES = *(double*)prec->i, AMAX = *(double*)prec->j, FMAX = *(double*)prec->k, *DF = (double*)prec->vala, *BW = (double*)prec->valb, *DVDTI = (double*)prec->valc, *DVDTQ = (double*)prec->vald, *FITRMS = (double*)prec->vale, *DFRMS = (double*)prec->valf; if ((CHIRP == 0) && (PULSE != 2)) { if (debug) { printf("%s: not in chirp %i or pulsed %i modes, exiting\n", prec->name, CHIRP, PULSE); } return -1; } if(prec->fta!=menuFtypeDOUBLE || prec->ftb!=menuFtypeDOUBLE || prec->ftc!=menuFtypeDOUBLE || prec->ftd!=menuFtypeDOUBLE || prec->ftva!=menuFtypeDOUBLE || prec->ftvb!=menuFtypeDOUBLE || prec->ftvc!=menuFtypeDOUBLE || prec->ftvd!=menuFtypeDOUBLE || prec->ftve!=menuFtypeDOUBLE) { (void)recGblSetSevr(prec, COMM_ALARM, INVALID_ALARM); return 1; } if(prec->ftf==menuFtypeDOUBLE) { sampt = *(double*)prec->f; } epicsTimeStamp cavi, cavq, fwdi, fwdq, cavamax, fwdpwrmax; double t1, t2, t3, t4, t5; dbGetTimeStamp(&prec->inpa, &cavi); dbGetTimeStamp(&prec->inpb, &cavq); dbGetTimeStamp(&prec->inpc, &fwdi); dbGetTimeStamp(&prec->inpd, &fwdq); dbGetTimeStamp(&prec->inpj, &cavamax); dbGetTimeStamp(&prec->inpk, &fwdpwrmax); t1 =epicsTimeDiffInSeconds(&cavi,&cavq); t2 =epicsTimeDiffInSeconds(&fwdi,&cavq); t3 =epicsTimeDiffInSeconds(&fwdq,&cavq); t4 =epicsTimeDiffInSeconds(&fwdq,&cavamax); t5 =epicsTimeDiffInSeconds(&fwdq,&fwdpwrmax); if ( (0.0 != t1) || (0.0 != t2) || (0.0 != t3) ) { if ( debug ) { printf("calc_df_chirp %s: Cav, Fwd, I, Q signal timestamps do not all match: deltas %.2f s %.2f s %.2f s %.2f s %2f s\n", prec->name, t1, t2, t3, t4, t5); } /* Do not process outputs */ return -1; } if(len > prec->neb) len = prec->neb; if(len > prec->nec) len = prec->nec; if(len > prec->ned) len = prec->ned; if(len > prec->novc) len = prec->novc; if(len > prec->novd) len = prec->novd; state.npt = len; state.dt = sampt; state.cav_iwf = CAVI; state.cav_qwf = CAVQ; state.fwd_iwf = FWDI; state.fwd_qwf = FWDQ; state.dvdt_iwf = DVDTI; state.dvdt_qwf = DVDTQ; analyze_chirp(&state, fit, FITRMS, rms, chirpmsg); *DF = cimag(fit[0]) / (2 * PI); *BW = -creal(fit[0]) / (2 * PI); *DFRMS = rms[0] / (2 * PI); prec->nevc = prec->nevd = len; if (debug) { printf("%s: len %i sampt %f detune %.1f bandwidth %.1f fit rms %.1f detune/bandwidth rms %.1f\n", prec->name, state.npt, state.dt, *DF, *BW, *FITRMS, *DFRMS); printf("analyze_chirp msg: %s\n", chirpmsg); } /* For now at least, continue to calculate/post values * when signal is low. Later when thresholds are better * trusted, may choose to perform this check before analyze_chirp */ if ( (AMAX < ATHRES) || (FMAX < FTHRES) ) { if ( debug ) { printf("calc_df_chirp %s: Cav ampl %.1f MV or fwd power %.1f W below threshold " " %.1f MV or %.1f W\n", prec->name, AMAX, FMAX, ATHRES, FTHRES); } return -1; } return 0; } /* Recommended decimation based on chirp frequency range */ static void chirp_recommend_decim(epicsInt32 *DECIM_REC, epicsInt32 FREQ_START, epicsInt32 FREQ_STOP, epicsInt32 *STAT, int debug) { epicsInt32 decim_tmp; double decim_log; epicsInt32 max; /* Calculate decimation that can analyze the maximum frequency offset of the requested range */ max = MAX(abs(FREQ_STOP),abs(FREQ_START)); decim_tmp = (epicsInt32)((0.3 * 1320.0e6)/(max * 14.0 * 33.0 * 2)); if ((decim_tmp < 1) || (max == 0)) { *STAT = 1; if (debug) { printf("chirp_recommend_decim: calculated decim %i or max frequency range %i out of range\n", decim_tmp, max); } return; } /* To (often) improve resolution of waveform data, choose nearest compatible power of 2 */ decim_log = log2(decim_tmp); if (decim_log >= 8 ) { *DECIM_REC = 128; } else if ((floor(decim_log) != decim_log) ) { *DECIM_REC = pow(2,floor(decim_log)); } else { *DECIM_REC = (epicsInt32)(decim_tmp); } if (debug) { printf("chirp_recommend_decim: tmp %i log base 2 %f final %i\n", decim_tmp, decim_log, *DECIM_REC); } } /* Setup chirp controller * * NOTE: Output links are manually processed in the * subroutine in order to control which are processed * based on mode. If more outputs are added, you will * need to update NUM_OUTPUTS below. */ static long setup_chirp(aSubRecord* prec) { short debug = (prec->tpro > 1) ? 1 : 0; /* Inputs */ short MODE = *(short*)prec->a; double ASET = *(double*)prec->b; epicsInt32 NORD = *(epicsInt32*)prec->c, FREQ_START = *(epicsInt32*)prec->d, FREQ_STOP = *(epicsInt32*)prec->e; /* Outputs */ epicsInt32 *ACQ_PER_WF = (epicsInt32*)prec->vala, *AMP_MAX = (epicsInt32*)prec->valb, *AMP_SLOPE = (epicsInt32*)prec->valc, *FREQ_SLOPE = (epicsInt32*)prec->vald, *FREQ_INIT = (epicsInt32*)prec->vale, *TIME = (epicsInt32*)prec->valf, *DECIM_CHIRP = (epicsInt32*)prec->valg, *STAT = (epicsInt32*)prec->valh, *DECIM_NEW = (epicsInt32*)prec->vali; if (debug) { printf("%s: mode %i aset %f nord %i\n", prec->name, MODE, ASET, NORD); } double cordicgain = 1.64676025812107; double fracofscreen = 0.78; double rampratio = 0.125; double ampmax = ASET / 100.0; double fsamp = 1320.0e6 / 14.0; double chirp_dt = 8.0 / fsamp; double waveformlength = (double)NORD; epicsInt32 PH_WI = 32, DD_SHIFT = 8, AMP_WI = 20; int NUM_OUTPUTS, i; if ( FREQ_START > FREQ_STOP ) { if (debug) { printf("%s: Frequency start %i must be <= stop %i\n", prec->name, FREQ_START, FREQ_STOP); } *STAT = 2; dbPutLink(&prec->outh, prec->ftvh, prec->valh, prec->nevh); return -1; } *STAT = 0; /* Initialize status to okay */ chirp_recommend_decim(DECIM_CHIRP, FREQ_START, FREQ_STOP, STAT, debug); double waveform_dt = 33 * 2 * *DECIM_CHIRP * 14 / 1320e6; double chirptime = fracofscreen * waveformlength * waveform_dt; /* in seconds */ double ramptime = chirptime * rampratio; /* in seconds */ *ACQ_PER_WF = 1; *AMP_MAX = (epicsInt32)(ampmax * pow(2,AMP_WI) / cordicgain); *AMP_SLOPE = (epicsInt32)(*AMP_MAX/(ramptime / chirp_dt) + 0.5); if (*AMP_SLOPE < 1) *AMP_SLOPE = 1; *FREQ_SLOPE = (epicsInt32)(pow(chirp_dt,2) * pow(2,(PH_WI + DD_SHIFT - 1)) * (FREQ_STOP - FREQ_START) / chirptime); *FREQ_INIT = (epicsInt32)(FREQ_START * chirp_dt * pow(2,(PH_WI-1))); *TIME = (epicsInt32)(chirptime / chirp_dt); if (*STAT != 0) { if (debug) { printf("%s: Set status to %i and exit\n", prec->name, *STAT); } dbPutLink(&prec->outh, prec->ftvh, prec->valh, prec->nevh); return -1; } if (debug) { printf("%s: ampmax %f, fstart %i fstop %i waveform_dt %f chirptime %f ramptime %f decim %i\n", prec->name, ampmax, FREQ_START, FREQ_STOP, waveform_dt, chirptime, ramptime, *DECIM_CHIRP); } /* If not in chirp mode, update the chirp configuration settings but not the live decimation * If in chirp mode, process all outputs */ NUM_OUTPUTS = 8; if (MODE == 5) { NUM_OUTPUTS++; *DECIM_NEW = *DECIM_CHIRP; } for (i = 0; i < NUM_OUTPUTS; i++) { dbPutLink(&(&prec->outa)[i], (&prec->ftva)[i], (&prec->vala)[i], (&prec->neva)[i]); } return -1; } /* * Shift frequency of entire rack, 4 cavities * */ static long set_freq_offset_rack(aSubRecord* prec) { short debug = (prec->tpro > 1) ? 1 : 0; /* Inputs */ epicsInt32 OFFSET = *(epicsInt32*)prec->a; /* Outputs */ epicsInt32 *PRC_PH_STEP_H = (epicsInt32*)prec->vala, *RFS_PH_STEP = (epicsInt32*)prec->valb; double *OFFSET_EXACT = (double*)prec->valc; epicsUInt16 *MASK = (epicsUInt16*)prec->vald; short reset = (OFFSET==0.0) ? 1 : 0; /* If returning to zero offset, * reset PRC and RFS phase accumulators */ *MASK = 0x1F; if ( reset ) { *MASK |= 0x20; } double tstep = 14 / 1320.0e6; int ph_step_l_wi = 12; int ph_step_l = 868; double dds_step_h, dds_step_combined; dds_step_h = 222425 - round( OFFSET * pow(2,20) * tstep ); dds_step_combined = (dds_step_h * pow(2,ph_step_l_wi)) + ph_step_l; *OFFSET_EXACT = 20.0e6 - ((dds_step_h + (ph_step_l)/4092.0) * pow(0.5,20)/tstep); if (debug) { printf("%s: requested offset %i kHz reset %i exact offset %.3f kHz " " PRC step %i RFS step %i link mask 0x%x\n", prec->name, OFFSET/1000, reset, *OFFSET_EXACT/1000, *PRC_PH_STEP_H, *RFS_PH_STEP, *MASK); } *PRC_PH_STEP_H = dds_step_h; *RFS_PH_STEP = dds_step_combined; return 0; } static double calc_rfs_offset(epicsInt32 rfs_ph_step) { double tstep = 14 / 1320.0e6; int ph_step_l_wi = 12; int ph_step_l = 868; double rfs_dds_step_h; rfs_dds_step_h = (rfs_ph_step - ph_step_l) / pow(2,ph_step_l_wi); return ( (222425 - rfs_dds_step_h) / (pow(2,20) * tstep) ); } static double calc_prc_offset(epicsInt32 prc_ph_step_h) { double tstep = 14 / 1320.0e6; int ph_step_l = 868; return ( 20.0e6 - ((prc_ph_step_h + (ph_step_l)/4092.0) * pow(0.5,20)/tstep) ); } static int compare_offset(double offset_prc, double offset_rfs) { if ( abs( offset_prc - offset_rfs) < 1.0 ) { return 0; } else { return -1; } } struct subPriv { epicsUInt32 usechan; }; typedef struct subPriv subPriv; #define MAX_NCHAN 8 static long init_calc_freq_offset(aSubRecord* prec) { size_t i; subPriv* priv; priv = calloc(1, sizeof(subPriv)); if(!priv) { (void)recGblSetSevr(prec, COMM_ALARM, INVALID_ALARM); return -1; } for(i=0; iftb)[i]==menuFtypeLONG) { priv->usechan |= (1<usechan==0) { errlogPrintf("%s: No input arguments???", prec->name); } prec->dpvt = priv; return 0; } /* * Calculate frequency offset from ideal for cavity * based on current RFS/PRC DDS settings * */ static long calc_freq_offset(aSubRecord* prec) { short debug = (prec->tpro > 1) ? 1 : 0; int i; subPriv* priv = prec->dpvt; /* Inputs */ epicsInt32 PRC_PH_STEP_H = *(epicsInt32*)prec->a, *RFS_PH_STEP_P = (epicsInt32*)prec->b; /* Outputs */ double *OFFSET = (double*)prec->vala; double offset_prc, offset_rfs; epicsInt32 RFS_PH_STEP; offset_prc = calc_prc_offset( PRC_PH_STEP_H ); for ( i=0; i < MAX_NCHAN; i++ ) { if ( priv->usechan & (1<name, i, PRC_PH_STEP_H, RFS_PH_STEP); printf("%s: PRC offset %f RFS offset %f\n", prec->name, offset_prc, offset_rfs); } if ( compare_offset( offset_prc, offset_rfs ) ) { *OFFSET = *(double*)prec->ovla; return -1; } } } if ( i == 0 ) { if (debug) { printf("%s: no RFS channels selected for comparison\n", prec->name); } *OFFSET = *(double*)prec->ovla; return -1; } *OFFSET = offset_prc; return 0; } /* Build bit mask from individual bits. */ long buildMask(struct aSubRecord *prec) { unsigned nbits = 21; int i; /* inputs */ unsigned short **bits = (unsigned short **)&prec->a; /* output */ long *mask = (long *)prec->vala; short debug = (prec->tpro > 1) ? 1 : 0; *mask = 0; for ( i = 0; i < nbits; i++ ) *mask = *mask | (*bits[i] << i ); if ( debug ) { printf("%s: mask output %i 0x%x\n", prec->name, (int)*mask, (unsigned int)*mask); } return 0; } static void build_pulse(double *twf, double *awf, double aset, double on, double off, short debug) { double period = on + off; double delta = period/100; /* arbitrarily chosen delta to show rising/falling edges */ twf[0] = 0; twf[1] = delta; twf[2] = on; twf[3] = on + delta; twf[4] = period; awf[0] = 0; awf[1] = aset; awf[2] = aset; awf[3] = awf[4] = 0; if (debug) { printf(" time array %f %f %f %f %f\n", twf[0], twf[1], twf[2], twf[3], twf[4]); printf(" ampl array %f %f %f %f %f\n", awf[0], awf[1], awf[2], awf[3], awf[4]); } } static long asub_pulse_diag(aSubRecord *prec) { /* Inputs */ /* short pulse_stat = *(short *)prec->a; */ double period = *(double *)prec->b, aset = *(double *)prec->c, aset_inuse = *(double *)prec->d, on = *(double *)prec->e, on_inuse = *(double *)prec->f, off = *(double *)prec->g, off_inuse = *(double *)prec->h; /* Outputs */ double *awf = (double *)prec->vala, *awf_inuse = (double *)prec->valb, *twf = (double *)prec->valc, *twf_inuse = (double *)prec->vald; short debug = (prec->tpro > 1) ? 1 : 0; if ( (on + off) != period ) { if (debug) { printf("%s: new calculated period %f does not match ACQ_PERIOD.OVAL %f\n", prec->name, (on + off), period); } } build_pulse(twf, awf, aset, on, off, debug); build_pulse(twf_inuse, awf_inuse, aset_inuse, on_inuse, off_inuse, debug); return 0; } static long asub_wfstats_diag(aSubRecord* prec) { int i, up, down; /* Waveform settings */ double start = *(double*)prec->a, width = *(double*)prec->b, acq_period = *(double*)prec->c; /* Number of samples for active waveforms */ epicsUInt32 len = *(epicsUInt32*)prec->d; /* Window arrays for waveform graphic */ double *x = (double*)prec->vala, *y = (double*)prec->valb; double sample_period = acq_period/len; short debug = (prec->tpro > 1) ? 1 : 0; if(prec->ftva!=menuFtypeDOUBLE || prec->ftvb!=menuFtypeDOUBLE) { (void)recGblSetSevr(prec, COMM_ALARM, INVALID_ALARM); return 1; } if ( debug ) { printf("%s: start %.2e width %.2e sample_period %.2e acq_period %.2e len %i\n", prec->name, start, width, sample_period, acq_period, len); } if(len > prec->nova) len = prec->nova; if(len > prec->novb) len = prec->novb; if ( acq_period > (start + width) ) { up = 1 + (int)ceil(start/sample_period); down = (int)floor((start+width)/sample_period); } else { up = -1; down = len; } for ( i = 0; i < len; i++ ) { x[i] = sample_period * i; if ( i < up || i > down) y[i] = -100000.0; else y[i] = 100000.00; } prec->neva = prec->nevb = len; return 0; } static registryFunctionRef rf_asub_seq[] = { {"Calculate Detune Chirp", (REGISTRYFUNCTION) &calc_df_chirp}, {"Setup Chirp", (REGISTRYFUNCTION) &setup_chirp}, {"Init Freq Offset", (REGISTRYFUNCTION) &init_calc_freq_offset}, {"Calculate Freq Offset", (REGISTRYFUNCTION) &calc_freq_offset}, {"Set Rack Freq Offset", (REGISTRYFUNCTION) &set_freq_offset_rack}, {"Build Mask", (REGISTRYFUNCTION) &buildMask}, {"RF Pulse Diag", (REGISTRYFUNCTION) &asub_pulse_diag}, {"WF Stats Graphic", (REGISTRYFUNCTION) &asub_wfstats_diag}, }; static void rfasubRegister(void) { registryFunctionRefAdd(rf_asub_seq, NELEMENTS(rf_asub_seq)); } #include epicsExportRegistrar(rfasubRegister);