#include #include #include #include #include #include #include #include #include #include #include #include #undef DEBUG /* Diagnotic counters for troubleshooting */ volatile epicsUInt32 afeBeam=0; volatile epicsUInt32 afeCalR=0; volatile epicsUInt32 afeCalG=0; static void afe_lock(AfeData); static void afe_unlock(AfeData); static void afe_cal_flip(AfeData); static AfeGotCalt afe_got_calt(AfeData); static int afe_recover(AfeData); /* Callbacks */ static AfeOpsRec afeHaveFlipOps = { lock: afe_lock, unlock: afe_unlock, cal_flip: afe_cal_flip, got_calt: afe_got_calt, recover: afe_recover, }; static AfeOpsRec afeNoFlipOps = { lock: afe_lock, unlock: afe_unlock, cal_flip: 0, got_calt: 0, recover: afe_recover, }; /* * Internal routine to re-setup AFE registers after an AFE outage. * * Also check that fw version is correct and print message * if it is not. * * IMPORTANT: assumes that AFE is already locked and caller will * unlock after return * * RETURN: 0 on success, -1 on failure (serial data transfer timeout) */ static int afe_recover(AfeData afeData) { /* Set these registers to cached values */ uint16_t data; int i; AfeCacheMsk r; #ifdef DEBUG DevBusMappedDev afe = afeData->ops_priv; #endif /* Set up CSR, clear 'cal trigger seen' bit, set AFE to 'configured' */ data = AFE_MSG_HEADER(AFE_MSG_WRITE + AFE_REG_CSR) + (afeData->regCache[AFE_REG_CSR] | AFE_REG_CSR_CNFG); if ( (afeData->features & AFE_FEAT_CALM_QSPI) ) { data |= AFE_REG_CSR_CALT; } #ifdef DEBUG printf("drvGenericAfeRecover; recovering from outage\n"); printf("drvGenericAfeRecover; writing 0x%04x to %s\n", data, afe->name); #endif if ( afeDevSupWrite( afeData, data ) ) return -1; /* Set up remainting registers */ for ( i = 0, r = 1; i < sizeof(afeData->regCache)/sizeof(afeData->regCache[0]) ; i++, r<<=1 ) { if ( (afeData->regSave & afeData->regValid & r) ) { data = AFE_MSG_HEADER(AFE_MSG_WRITE + i) + afeData->regCache[i]; #ifdef DEBUG printf("drvGenericAfeRecover; writing 0x%04x to %s\n", data, afe->name); #endif if ( afeDevSupWrite( afeData, data ) ) return -1; #ifdef DEBUG data = AFE_MSG_HEADER(AFE_MSG_READ + i); afeDevSupWriteRead( afeData, data, &data ); printf("drvGenericAfeRecover: Register %02x contents now %04x\n", i, data); #endif } } /* Check firmware version */ data = AFE_MSG_HEADER(AFE_MSG_READ + AFE_REG_VER); if ( afeDevSupWriteRead( afeData, data, &data ) ) return -1; data = AFE_MSG_DATA(data); if ( data != afeData->regCache[AFE_REG_VER] ) { errlogPrintf("ERROR: Firmware version changed (or no AFE present) -- was 0x%02x but now is 0x%02x\n", afeData->regCache[AFE_REG_VER], data); return -1; } return 0; } /* Periodically check AFE online and configured. * If AFE not configured, call drvGenericAfeRecover to recover settings * * Reset online flag to default status of online at the end. Other tasks * set that flag if they detect the AFE is NOT online. In that case, we * don't bother to read the CSR. */ void drvGenericAfeCheck(void *usr_arg) { uint16_t data; DevBusMappedDev afe = usr_arg; AfeData afeData = afe->udata; int s; while (1) { data = AFE_MSG_HEADER(AFE_MSG_READ + AFE_REG_CSR); afeLock( afeData ); if ( afeData->onln ) { #ifdef DEBUG printf("drvGenericAfeCheck: %s online, write 0x%04x\n", afe->name, data); #endif s = afeDevSupWriteRead( afeData, data, &data ); #ifdef DEBUG printf("drvGenericAfeCheck: %s read CSR 0x%04x\n", afe->name, data); #endif if ( !s && (AFE_IS_ONLN(data)) ) { /* If AFE just came online, configure it * (Check that message header is what we expect: 0xC000) */ if ( AFE_NOT_CNFG(data) && ((data & AFE_MSG_HEADER(0xff)) == AFE_MSG_HEADER(AFE_MSG_READ+AFE_REG_CSR)) ) { #ifdef DEBUG printf("%s just came back online, CSR %02x\n", afe->name, data); #endif if ( afeRecover( afeData ) ) { errlogPrintf("WARNING: Failed to set up %s after AFE outage\n", afe->name); } } } } /* Reset online flag for next time */ afeData->onln = 1; afeUnlock( afeData ); epicsThreadSleep(15); } } static void afe_lock(AfeData afeData) { DevBusMappedDev afe = afeData->ops_priv; epicsMutexLock( afe->mutex ); } static void afe_unlock(AfeData afeData) { DevBusMappedDev afe = afeData->ops_priv; epicsMutexUnlock( afe->mutex ); } /* Note: afe_cal_flip and afe_got_calt share work of checking if last data * acquisition was a cal cycle and if so, setting data kind. * * Callback to toggle cal color and clear 'trigger seen' bit. To be called after * digitizer acquisition complete. Digitizer acquires data for cal and beam events, * so for equal rates of beam, green cal, red cal, pattern will be: * * beam - grn cal - red cal - beam - red cal - grn cal - beam - grn cal ... * * Send message to AFE, setting new cal color and clearing cal 'trigger seen' bit. * Store AFE CSR register in cache array. If DMA was not started, read response and unlock mutex. */ static void afe_cal_flip(AfeData afeData) { uint16_t data; unsigned calm = 0; /* AFE message; clear 'cal trigger seen' bit */ data = AFE_MSG_HEADER(AFE_MSG_WRITE + AFE_REG_CSR) + (afeData->regCache[AFE_REG_CSR] | AFE_REG_CSR_CALT); /* If in toggle mode, toggle cal color */ if ( afeData->calMode == AFE_CALM_TOGGLE ) { switch( calm = AFE_REG_CSR_CALM_GET(afeData->regCache[AFE_REG_CSR]) ) { default: /* If cal color neither red nor green, (which should not happen), set to green */ calm = AFE_REG_CSR_CALM_GRN; break; case AFE_REG_CSR_CALM_GRN: calm = AFE_REG_CSR_CALM_RED; break; case AFE_REG_CSR_CALM_RED: calm = AFE_REG_CSR_CALM_GRN; break; } /* Modify message to set new cal color */ data &= ~AFE_REG_CSR_CALM_GET(-1); data |= calm; } afeDevSupWrite( afeData, data ); /* Store new register contents in cache */ afeData->regCache[AFE_REG_CSR] = AFE_MSG_DATA(data); } /* * Callback to test for cal cycle and set data kind. To be called after digitizer data * copied to memory. Wait for AFE response. If AFE offline, return. If AFE online * but not 'configured' (must have been off), call drvGenericAfeRecover to re-setup * AFE registers and then return. Otherwise, If cal trigger seen, set data kind. */ AfeGotCalt afe_got_calt(AfeData afeData) { uint16_t data; AfeGotCalt rval = AfeCaltErr; /* returning this causes data to be discarded */ /* If data transfer timeout, return */ if ( afeDevSupRead( afeData, &data ) ) { /* Strobe online flag */ afeData->onln = 0; goto bail; } /* If AFE not present or off, return */ if ( ! (AFE_IS_ONLN(data)) ) { /* Strobe online flag */ afeData->onln = 0; goto bail; } /* If AFE not configured, return */ if ( AFE_NOT_CNFG(data) && !((data)&(0x7f00)) && ((data)&(0x8000)) ) { goto bail; } /* If this was a calibration cycle then compute return value */ if ( (data & AFE_REG_CSR_CALT) ) { if ( afeData->calMode == AFE_CALM_TOGGLE ) { /* If we are in toggle mode: * We already have switched the color (in afe_cal_flip); therefore, if the * current color is 'green' then the data we have corresponds to a 'red' cycle * and vice-versa. */ rval = AFE_REG_CSR_CALM_GRN == AFE_REG_CSR_CALM_GET( afeData->regCache[AFE_REG_CSR] ) ? AfeCaltRed : AfeCaltGrn; } else if ( afeData->calMode == AFE_CALM_GRN ) rval = AfeCaltGrn; else if ( afeData->calMode == AFE_CALM_RED ) rval = AfeCaltRed; else /* Cal mode = NONE */ goto bail; if ( AfeCaltRed == rval ) { afeCalR++; } else { afeCalG++; } } else { afeBeam++; rval = AfeCaltNone; } bail: return rval; } /* Initialize AFE. To be called at init. Register AFE with devBusMapped. * Allocate memory for structure containing afe-specific data; attach this * structure to DevBusMappedDev->udata and attach DevBusMappedDev struct to usr_arg_p. * This provides access to this data from other callbacks and devBusMapped device support. * Initialize AFE: set cal color, clear cal 'trigger seen' bit, set AFE to 'configured'. * Store AFE CSR register settings in cache array. Check that AFE 'online' bit is set. * If it is not, exit. Check that FW version is acceptable. */ AfeData afe_attach(unsigned instance, AfeWriteReadFunc io_f, const void *io_device) { char name[20], taskName[50]; DevBusMappedDev afe; uint16_t data; AfeData afeData = 0, rval = 0; sprintf(name,"AFE%i",instance); if ( ! (afe = devBusMappedRegister(name, (volatile void *)(AFE_BASE))) ) { cantProceed("FATAL ERROR: Unable to register AFE %s with bus mapped devsup\n", name); } epicsMutexLock( afe->mutex ); if ( ! (afeData = afeDataCreate( io_f, io_device )) ) { epicsMutexUnlock( afe->mutex ); errlogPrintf("FATAL ERROR: afeDataCreate failed\n"); goto bail; } /* Initialize AFE: set cal mode to green, cal oscillator mode to auto, clear 'cal trigger seen' bit, and set AFE to 'configured' */ data = AFE_MSG_HEADER(AFE_MSG_WRITE + AFE_REG_CSR) + AFE_REG_CSR_CNFG; if ( (afeData->features & AFE_FEAT_CALM_QSPI) ) { data |= (AFE_REG_CSR_CALM_GRN | AFE_REG_CSR_CALT); } if ( (afeData->features & AFE_FEAT_CSR_CLKS) ) { data |= AFE_REG_CSR_CLKS; } #ifdef DEBUG printf("drvGenericAfeAttach; writing %s CSR 0x%04x\n", name, data); #endif /* Cache new register contents */ afeData->regCache[AFE_REG_CSR] = AFE_MSG_DATA(data) & ~AFE_REG_CSR_CNFG; if ( (afeData->features & AFE_FEAT_CALM_QSPI) ) { afeData->regCache[AFE_REG_CSR] &= ~AFE_REG_CSR_CALT; } if ( (afeData->features & AFE_FEAT_CALM_QSPI) ) { afeData->ops = &afeHaveFlipOps; } else { afeData->ops = &afeNoFlipOps; } afe->udata = afeData; afeData->ops_priv = afe; if ( afeDevSupWriteRead( afeData, data, &data ) ) { errlogPrintf("WARNING: %s digitizer serial interface data transfer TIMEOUT\n", name); rval = afeData; goto bail; } rval = afeData; if ( ! (AFE_IS_ONLN(data)) ) { errlogPrintf("WARNING: %s AFE off or not present\n", name); rval = afeData; goto bail; } afeData->regValid |= (1<regCache[AFE_REG_VER] = AFE_MSG_DATA( data ); afeData->regValid |= (1<regCache[AFE_REG_CSR]); #endif rval = afeData; bail: epicsMutexUnlock( afe->mutex ); if ( ! rval ) { cantProceed("FATAL ERROR: Failed to initialize AFE %s\n", name); } else if ( (afeData->features & AFE_FEAT_PWR_INDEP) ) { /* Start task to periodically check if AFE online and configured */ snprintf( taskName, sizeof(taskName), "%s_ConfigChk", afe->name ); epicsThreadMustCreate( taskName, epicsThreadPriorityLow, epicsThreadGetStackSize(epicsThreadStackMedium), drvGenericAfeCheck, afe ); } return rval; } AfeAttachFunc drvGenericAfeAttach = afe_attach; static void drvGenericAfeRegister() { if ( !AFE_ATTACH_REGISTER(drvGenericAfeAttach) ) cantProceed("Unable to register drvGenericAfeAttach\n"); } epicsExportRegistrar(drvGenericAfeRegister);