#include #include #include #include #include #include #include #include #include #include #include #include #undef DEBUG /* Optional default AFE version. * If AFE unresponsive during installation, software * will treat it like this version. * Define in st.cmd before AFE initialization. * ** NOTE: Still needs to be tested with PAD and MicroTCA AFEs!! */ int defaultAfeFwVers = 0; int afeGetFeatures(unsigned fw_vers, AfeFeatures *f_p) { AfeFeatures features = 0; if ( 0xff == fw_vers || 0x00 == fw_vers ) { if ( defaultAfeFwVers == 0 ) { errlogPrintf("afeGetFeatures: ERROR -- Firmware version 0x%02x -- AFE not connected?\n", fw_vers); return -1; } else { fw_vers = defaultAfeFwVers; errlogPrintf("afeGetFeatures: Warning -- No response from AFE. Assuming FW version 0x%02x\n", fw_vers); } } if ( fw_vers > 64 ) { if ( fw_vers < 67 ) { errlogPrintf("afeGetFeatures: ERROR -- firmware (0x%02x) too old\n", fw_vers); return -1; } /* XTA AFE Chassis */ features |= AFE_FEAT_ATT1_0_15 | AFE_FEAT_ATT2_0_31 | AFE_FEAT_CALM_QSPI | AFE_FEAT_PWR_INDEP; } else if ( fw_vers >= 32 ) { /* PAD AFE - later versions */ features |= AFE_FEAT_ATT1_0_15 | AFE_FEAT_ATT2_0_31 | AFE_FEAT_CALM_QSPI ; } else if ( fw_vers >= 8 ) { /* SLAC uTCA RTM */ features |= AFE_FEAT_ATT1_0_31 | AFE_FEAT_ATT2_0_31 | AFE_FEAT_PWR_INDEP; features |= AFE_FEAT_CALTIM_INT; features |= AFE_FEAT_CSR_CLKS; } else if ( 4 == fw_vers ) { /* PAD AFE - initial version */ features |= AFE_FEAT_ATT1_0_15 | AFE_FEAT_ATT2_0_15 | AFE_FEAT_CALM_QSPI ; } else { errlogPrintf("afeGetFeatures: ERROR -- Unsupported firmware version 0x%02x\n", fw_vers); return -1; } *f_p = features; return 0; } AfeData afeDataCreate(AfeWriteReadFunc rwf, void *io_device) { AfeData rval; uint16_t data; AfeFeatures features = 0; AfeCacheMsk regSave = 0; if ( sizeof(rval->regCache)/sizeof(rval->regCache[0]) > 8*sizeof(rval->regValid) || sizeof(rval->regCache)/sizeof(rval->regCache[0]) > 8*sizeof(rval->regSave) ) { errlogPrintf("Programming error! AFE regCache must not have more elements than bit # of regValid/regSave.\n"); return 0; } if ( ! (rval = calloc(1, sizeof(*rval))) ) { errlogPrintf("afeDataCreate: ERROR -- no memory.\n"); return 0; } /* Check firmware version */ data = AFE_MSG_HEADER(AFE_MSG_READ + AFE_REG_VER); #ifdef DEBUG printf("afeDataCreate: read FW version, write 0x%04x to AFE\n", data); #endif if ( rwf( io_device, data, &data ) ) { errlogPrintf("afeDataCreate: ERROR -- AFE Communication failed.\n"); goto bail; } #ifdef DEBUG printf("afeDataCreate: read FW version, read 0x%04x from AFE\n", data); #endif if ( afeGetFeatures( AFE_MSG_DATA(data), &features ) ) goto bail; regSave = (1<cb.afe_write_read = rwf; rval->io_dev = io_device; rval->features = features; rval->regSave = regSave; return rval; } int afeAttachRegister(const char *name, AfeAttachFunc f) { return registryAdd(afeAttachRegister, name, f); } AfeAttachFunc afeAttachFind(const char *name) { return registryFind(afeAttachRegister, name); } /* Generic device support method for AFE input record. * Send 'read' message to AFE register specified in .INP field and read response. * If there is a data transfer timeout, return error. * Else, cache register contents and set .VAL. * * RETURNS: 0 on success * -1 on failure */ static int qspiReadAfeReg(DevBusMappedPvt pvt, unsigned *pvalue, int idx, dbCommon *prec) { uint16_t data,data_out; /* AFE message */ uint8_t a = ((uintptr_t)pvt->addr & 0x3f) - AFE_BASE; /* Get AFE register */ int rval; DevBusMappedDev afe = pvt->dev; AfeData afeData = afe->udata; /* Read AFE register a */ data_out = AFE_MSG_HEADER(AFE_MSG_READ + a); afeLock( afeData ); #ifdef DEBUG printf("qspiReadAfeReg; writing to %s, AFE register 0x%02x 0x%04x\n", afe->name, a, data_out); #endif if ( (rval = afeDevSupWriteRead( afeData, data_out, &data )) ) { /* strobe online flag */ afeData->onln = 0; goto bail; } if ( (data_out & 0xFF00) != (data & 0xFF00 ) ) { /* strobe online flag */ afeData->onln = 0; /* maybe not online ? */ goto bail; } #ifdef DEBUG printf("qspiReadAfeReg; %s, got 0x%04x\n", afe->name, data); #endif if ( a < sizeof(afeData->regCache)/sizeof(afeData->regCache[0]) ) { afeData->regCache[a] = AFE_MSG_DATA(data); afeData->regValid |= 1<addr & 0x3f) - AFE_BASE; /* Get AFE register to write to */ int rval; DevBusMappedDev afe = pvt->dev; AfeData afeData = afe->udata; /* Write value to AFE register a */ data = AFE_MSG_HEADER(AFE_MSG_WRITE + a) + value; #ifdef DEBUG printf("qspiWriteAfeReg; writing to %s, AFE register 0x%02x 0x%04x\n", afe->name, a, data); #endif afeLock( afeData ); if ( (rval = afeDevSupWrite( afeData, data )) ) goto bail; if ( a < sizeof(afeData->regCache)/sizeof(afeData->regCache[0]) ) { afeData->regCache[a] = AFE_MSG_DATA(data); afeData->regValid |= 1<name, data); #endif bail: afeUnlock( afeData ); return rval; } /* Prototypes for our read/write routines */ static DevBusMappedAccessRec qspiAfeAccess = { qspiReadAfeReg, qspiWriteAfeReg }; /* For PAD and VME digi AFE, used to set calibration mode * If val is 4, the software toggles the cal mode on * each data acquisition cycle. Otherwise, it sets the * value in the CSR * For SLAC RTM AFE, use this routine to set int/ext clock * * NOTE: this will write zeros to all other CSR bits. If we were * using a cal oscillator mode != AUTO, that would overwrite that mode * and this routine should be changed. At this time, we only use AUTO. * Also, cal mode is typically only set on boot and int/ext clock * is typically not changed.. */ static int qspiWriteAfeCalm(DevBusMappedPvt pvt, unsigned value, int idx, dbCommon *prec) { DevBusMappedDev afe = pvt->dev; AfeData afeData = afe->udata; int rval = 0; #ifdef DEBUG printf("qspiWriteAfeCalm; %s val is %i; value is %i\n", afe->name, ((mbboRecord *)prec)->val, value); #endif /* extract 'soft toggle' information via an ugly hack * (we shouldn't have to know what record type 'prec' is * at the driver level!) */ afeData->calMode = (((mbboRecord *)prec)->val); if ( (afeData->features & AFE_FEAT_CALM_QSPI) && afeData->calMode < 4 ) rval = qspiWriteAfeReg(pvt, value, idx, prec); return rval; } /* Prototypes for our read/write routines */ static DevBusMappedAccessRec qspiCalmAccess = { qspiReadAfeReg, qspiWriteAfeCalm }; /* AFE device support init */ static long afeDevSupInit(void) { long rval; if ( (rval = devBusMappedRegisterIO("qspiAfe", &qspiAfeAccess)) ) return rval; if ( (rval = devBusMappedRegisterIO("softTogAfe",&qspiCalmAccess)) ) return rval; return 0; } static long afeDevSupReport(int level) { errlogPrintf("QSPI Support for Communication with AFE\n"); return 0; } static struct { long number; DEVSUPFUN report; DEVSUPFUN init; } devAfeDevSup = { 2, afeDevSupReport, afeDevSupInit }; epicsExportAddress(drvet,devAfeDevSup);