#include #include #include #include #include #include #include #include #include #include #include #include #include static int pvt2fd(void *fePvt) { return (int)(intptr_t)fePvt; } static Sis8300ChannelSel chSel2ChSel(GenDigiChannelSel sel) { return sel; } static int sis8300FEClose(void *fePvt) { int fd = pvt2fd( fePvt ); if ( fd >= 0 ) return close( fd ); return 0; } static int sis8300FEArm(void *fePvt, PadDataKind kind) { static const int kindRevMap[] = { [PadDataBpm] SIS8300_KIND_BEAM, [PadDataCalRed] SIS8300_KIND_CRED, [PadDataCalGrn] SIS8300_KIND_CGRN }; int fd = pvt2fd( fePvt ); return sis8300DigiArm( fd, kindRevMap[kind] ); } static int sis8300FEDisarm(void *fePvt) { int fd = pvt2fd( fePvt ); return sis8300DigiArm( fd, SIS8300_KIND_OFF ); } static int sis8300FEWriteReg(void *frontEndPvt, unsigned reg, uint32_t val) { int fd = pvt2fd( frontEndPvt ); return sis8300DigiWriteReg(fd, reg, val); } static int sis8300FEReadReg(void *frontEndPvt, unsigned reg, uint32_t *val_p) { int fd = pvt2fd( frontEndPvt ); return sis8300DigiReadReg(fd, reg, val_p); } static int sis8300FESetCount(void *frontEndPvt, GenDigiChannelSel sel, unsigned nSamples) { int fd = pvt2fd( frontEndPvt ); return sis8300DigiSetCount(fd, chSel2ChSel( sel ), nSamples); } static void sis8300FESetSim(void *frontEndPvt, int32_t a, int32_t b, int32_t c, int32_t d, int quiet) { int fd = pvt2fd( frontEndPvt ); sis8300DigiSetSim( fd, a, b, c, d, quiet ); } static int sis8300FEValidateSel(void *frontEndPvt, GenDigiChannelSel sel) { int fd = pvt2fd( frontEndPvt ); return sis8300DigiValidateSel( fd, chSel2ChSel( sel ) ); } static int sis8300FERead(void *frontEndPvt, uint8_t **buf_p, size_t bufsz, size_t nBytes, FrontEndTimestamp *ts) { int fd = pvt2fd( frontEndPvt ); return pread(fd, *buf_p, nBytes, 0); } static int sis8300FEOpen(const char *devnam, unsigned long fclk, void **frontEndPvt_p) { Si5326ParmsRec si5326; Si5326Parms si5326_p = &si5326; unsigned clkhl = SIS8300_BYPASS_9510_DIVIDER; unsigned long fmax; int fd; int rval = -1; if ( (fd = open(devnam, O_RDWR)) < 0 ) { fprintf(stderr,"Unable to open %s: %s\n", devnam, strerror(errno)); goto bail; } if ( 0 == (fmax = sis8300DigiGetFclkMax( fd )) ) { fprintf(stderr,"Unable to determine max. digitizer clock frequency!\n"); goto bail; } if ( fclk > fmax ) { fprintf(stderr,"Requested clock frequency too big - digitizer supports only %lu Hz\n", fmax); goto bail; } si5326.fin = 250000000UL; si5326.bw = 4000; if ( si5326.fin == fclk ) { si5326_p = 0; } else if ( si5326.fin/2 == fclk ) { si5326_p = 0; clkhl = 0; /* divide by two */ } else { switch ( sis8300ClkDetect( fd ) ) { default: fprintf(stderr,"Unable to detect Si5326 clock synthesizer\n"); goto bail; case Si5326_WidebandMode: si5326.wb = 1; break; case Si5326_NarrowbandMode: si5326.wb = 0; break; } switch ( fclk ) { case 109000000UL: if ( si5326.wb ) { /* Use 109MHz clock */ si5326.n3 = 10; /* f3 = 25Mhz */ si5326.n2h = 1; si5326.n2l = 109*2; /* fo = 50*109 MHz */ si5326.n1h = 5; si5326.nc = 10; si5326.bwsel = 1; /* dspllsim gave us this; no other info available :-( */ clkhl = SIS8300_BYPASS_9510_DIVIDER; } else { /* Use 218MHz clock */ si5326.n3 = 125; /* f3 = 2Mhz */ si5326.n2h = 4; si5326.n2l = 654; /* fo = 2*109 MHz */ si5326.n1h = 6; si5326.nc = 4; si5326.bwsel = 5; /* dspllsim gave us this; no other info available :-( */ clkhl = 0; } break; default: if ( si53xx_calcParms( fclk, &si5326, 0 ) ) { fprintf(stderr,"Unable to find a PLL divider configuration for Fclk == %luHz\n", fclk); goto bail; } break; } } sis8300DigiSetup( fd, si5326_p , clkhl, 1 ); *frontEndPvt_p = (void*)(uintptr_t)fd; return 0; bail: if ( fd >= 0 ) close( fd ); return rval; } static GenDigiFrontEndRec sis8300FrontEnd = { arm : sis8300FEArm, disarm : sis8300FEDisarm, writeReg : sis8300FEWriteReg, readReg : sis8300FEReadReg, setCount : sis8300FESetCount, afeWriteRead : sis8300DigiQspiWriteRead, setSim : sis8300FESetSim, open : sis8300FEOpen, close : sis8300FEClose, validSel : sis8300FEValidateSel, read : sis8300FERead, }; int sis8300DigiCommConfig(unsigned udpCommChnl, const char *devnam, unsigned sel_in, unsigned fclk, int nPkts, AfeAttachFunc afe_attach) { return genDigiCommConfig(udpCommChnl, devnam, sel_in, fclk, nPkts, afe_attach, &sis8300FrontEnd); } static const struct iocshArg args[] = { { "udpComm channel", iocshArgInt }, { "sis8300 device file", iocshArgString }, { "digitizer channel selector", iocshArgInt }, { "digitizer clock frequency (in Hz) - currently only 125MHz or 109MHz supported", iocshArgInt }, { "number of packet buffers (per channel; optional)", iocshArgInt }, { "name of AFE attach function", iocshArgString } }; static const struct iocshArg *bloatp[sizeof(args)/sizeof(args[0])]; static const struct iocshFuncDef sis8300DigiCommConfigDesc = { "sis8300DigiCommConfig", sizeof(args)/sizeof(args[0]), bloatp }; static void sis8300DigiCommConfigFunc(const iocshArgBuf *args) { AfeAttachFunc f = 0; int i; if ( !args[1].sval ) { epicsPrintf("sis8300DigiCommConfig -- need non-NULL device file name arg\n"); return; } if ( args[5].sval && args[5].sval[0] ) { if ( strcmp(args[5].sval,"NULL") && ( 1 != sscanf(args[5].sval,"%i",&i) || 0 != i) ) { if ( ! (f = afeAttachFind( args[5].sval )) ) { errlogPrintf("sis8300DigiCommConfig: AFE attach function '%s' not found!\n", args[5].sval); return; } } } if ( genDigiCommConfig(args[0].ival, args[1].sval, (unsigned)args[2].ival, (unsigned)args[3].ival, args[4].ival, f, &sis8300FrontEnd) ) { epicsPrintf("sis8300DigiCommConfig() FAILED for channel %i\n", args[0].ival); } } static void sis8300FERegistrar(void) { int i; for ( i=0; i