def setChannelRegister(device, gtx, chip, chan, chanreg, debug=False): if (chan not in range(0, 128)): msg = "%s: Invalid VFAT channel specified %d" % (device, chan) vfatlogger.warning(colormsg(msg, logging.WARNING)) raise VFATException(colormsg(msg, logging.FATAL)) writeVFAT(device, gtx, chip, "VFATChannels.ChanReg%d" % (chan), chanreg) return
def sendBC0(device, gtx, interval=25, number=1, debug=False): """ Send a BC0 signal """ msg = "%s: resetting the T1 controller" % (device) ohlogger.debug(colormsg(msg, logging.DEBUG)) writeRegister(device, "GEM_AMC.OH.OH%d.T1Controller.RESET" % (gtx), 0x1) msg = "%s: configuring the T1 controller for mode 0x0, interval %d, nsignals %d" % ( device, interval, number) ohlogger.debug(colormsg(msg, logging.DEBUG)) configureLocalT1(device, gtx, 0x0, 0x3, 0x0, interval, number) msg = "%s: current T1 status 0x%x" % ( device, readRegister(device, "GEM_AMC.OH.OH%d.T1Controller.MONITOR" % (gtx))) ohlogger.debug(colormsg(msg, logging.DEBUG)) startLocalT1(device, gtx, debug) if not readRegister(device, "GEM_AMC.OH.OH%d.T1Controller.MONITOR" % (gtx)): msg = "%s: T1Controller failed to start: status: 0x%x" % ( device, readRegister(device, "GEM_AMC.OH.OH%d.T1Controller.MONITOR" % (gtx))) ohlogger.warning(colormsg(msg, logging.WARNING)) raise OptoHybridException(colormsg(msg, logging.FATAL)) return
def getAllChannelRegisters(device, gtx, chan, mask=0x0, debug=False): if (chan not in range(0, 128)): msg = "%s: Invalid VFAT channel specified %d" % (device, chan) vfatlogger.warning(colormsg(msg, logging.WARNING)) raise VFATException(colormsg(msg, logging.FATAL)) return readAllVFATs(device, gtx, "VFATChannels.ChanReg%d" % (chan), mask, debug)
def scanVCalByVFAT(device,gtx,vfat,channel,trim, scanmin=0x0,scanmax=0xff, stepsize=0x1,numtrigs=1000, debug=False): """ Run a firmware VCal scan on the selected VFAT/channel combination requires a trigger source, cal pulse source, channel enabled @returns an array containing the scan results """ try: checkScanParams(vfat,scanmin,scanmax,stepsize) except ValueError: msg = "Invalid scan configuration specified" scanlogger.error(colormsg(msg,logging.ERROR)) return None originalChannel = vfatfuncs.getChannelRegister(device,gtx,vfat,channel) vfatfuncs.setChannelRegister(device,gtx,vfat,channel, mask=0x0,pulse=0x1,trim=trim) msg = "Channel %d register 0x%08x"%(channel,getChannelRegister(device,gtx,vfat,channel)) scanlogger.debug(colormsg(msg,logging.DEBUG)) optofuncs.configureScanModule(device,gtx, optofuncs.scanmode.SCURVE, vfat, channel=channel, scanmin=scanmin, scanmax=scanmax, stepsize=stepsize, numtrigs=numtrigs, debug=debug) optofuncs.startScanModule(device,gtx) if (debug): optofuncs.printScanConfiguration(device,gtx) pass msg = "LocalT1Controller status %d"%(optofuncs.getLocalT1Status(device,gtx)) scanlogger.debug(colormsg(msg,logging.DEBUG)) data_scurve = optofuncs.getScanResults(device,gtx, scanmax-scanmin, debug=debug) # return settings to start mask = (originalChannel>>5)&0x1 pulse = (originalChannel>>6)&0x1 trim = originalChannel&0x1f vfatfuncs.setChannelRegister(device,gtx,vfat,channel, mask=mask,pulse=pulse,trim=trim, debug=debug) msg = "Channel %d register 0x%08x"%(channel,vfatfuncs.getChannelRegister(device,gtx,vfat,channel)) scanlogger.debug(colormsg(msg,logging.DEBUG)) return data_scurve
def startScanModule(device, gtx, useUltra=False, debug=False): """ """ scanBase = "GEM_AMC.OH.OH%d.ScanController.THLAT" % (gtx) if useUltra: scanBase = "GEM_AMC.OH.OH%d.ScanController.ULTRA" % (gtx) pass if (readRegister(device, "%s.MONITOR.STATUS" % (scanBase)) > 0): msg = "%s: Scan is already running (0x%x), not starting a new scan" % ( device, readRegister(device, "%s.MONITOR.STATUS" % (scanBase))) ohlogger.warning(colormsg(msg, logging.WARNING)) return if (readRegister(device, "%s.MONITOR.ERROR" % (scanBase)) > 0): msg = "%s: There was an error in the scan configuration, not starting a new scan" % ( device) ohlogger.warning(colormsg(msg, logging.WARNING)) raise OptoHybridException(colormsg(msg, logging.FATAL)) writeRegister(device, "%s.START" % (scanBase), 0x1) if readRegister( device, "%s.MONITOR.ERROR" % (scanBase)) or not readRegister(device, "%s.MONITOR.STATUS" % (scanBase)): msg = "%s: Scan failed to start" % (device) msg += "ERROR %d" % (readRegister(device, "%s.MONITOR.ERROR" % (scanBase))) msg += "STATUS %d" % (not readRegister( device, "%s.MONITOR.STATUS" % (scanBase))) raise OptoHybridException(colormsg(msg, logging.FATAL)) msg = "%s: After start, scan status is: 0x%08x" % ( device, readRegister(device, "%s.MONITOR" % (scanBase))) ohlogger.debug(colormsg(msg, logging.DEBUG)) return
def scanThresholdByChannel(device,gtx,vfat,channel, scanmin=0x0,scanmax=0xff, stepsize=0x1,numtrigs=1000, debug=False): """ Run a firmware threshold scan on the selected VFAT/channel combination @returns an array containing the scan results """ try: checkScanParams(vfat,scanmin,scanmax,stepsize) except ValueError: msg = "Invalid scan configuration specified" scanlogger.error(colormsg(msg,logging.ERROR)) return None optofuncs.configureScanModule(device,gtx, optofuncs.scanmode.THRESHCH, vfat,channel, scanmin,scanmax,stepsize,numtrigs, debug=debug) optofuncs.startScanModule(device,gtx) if (debug): optofuncs.printScanConfiguration(device,gtx) pass msg = "LocalT1Controller status %d"%(optofuncs.getLocalT1Status(device,gtx)) scanlogger.debug(colormsg(msg,logging.DEBUG)) data_threshold = optofuncs.getScanResults(device,gtx,scanmax-scanmin,debug=debug) return data_threshold
def sendL1ACalPulse(device, gtx, delay, interval=25, number=0, debug=False): """ Configure the T1 controller Mode: 0 (Single T1 signal), 1 (CalPulse followed by L1A), 2 (pattern) t1type only for mode 0, type of T1 signal to send, L1A, CalPulse, Resync, BC0 delay only for mode 1, delay between CalPulse and L1A interval only for mode 0,1, how often to repeat signals number how many signals to send (0 is continuous """ msg = "%s: resetting the T1 controller" % (device) ohlogger.debug(colormsg(msg, logging.DEBUG)) writeRegister(device, "GEM_AMC.OH.OH%d.T1Controller.RESET" % (gtx), 0x1) msg = "%s: configuring the T1 controller for mode 0x1, delay %d, interval %d, nsignals %d" % ( device, delay, interval, number) ohlogger.debug(colormsg(msg, logging.DEBUG)) configureLocalT1(device, gtx, 0x1, 0x0, delay, interval, number) msg = "%s: current T1 status 0x%x" % ( device, readRegister(device, "GEM_AMC.OH.OH%d.T1Controller.MONITOR" % (gtx))) ohlogger.debug(colormsg(msg, logging.DEBUG)) startLocalT1(device, gtx, debug) if not readRegister(device, "GEM_AMC.OH.OH%d.T1Controller.MONITOR" % (gtx)): msg = "%s: T1Controller failed to start: status: 0x%x" % ( device, readRegister(device, "GEM_AMC.OH.OH%d.T1Controller.MONITOR" % (gtx))) ohlogger.warning(colormsg(msg, logging.WARNING)) raise OptoHybridException(colormsg(msg, logging.FATAL)) return
def runCommand(cmd,log=None): """ Provides a wrapper around the subprocess.call command cmd should be an array of the form: ["command","arg1",...] log should specify a logfile to write to """ import datetime,os,sys import subprocess from subprocess import CalledProcessError from gempython.utils.gemlogger import colors,colormsg import logging logger = logging.getLogger(__name__) try: msg = "Executing command:" for c in cmd: msg+=" %s"%(c) logger.info(colormsg(msg,logging.INFO)) sys.stdout.flush() returncode = subprocess.call(cmd,stdout=log,stderr=log) except CalledProcessError as e: msg = "Caught exception" msg+=str(e) msg+=" running:" for c in cmd: msg+=" %s"%(c) logger.error(colormsg(msg,logging.ERROR)) sys.stdout.flush() pass return returncode
def setAllChannelRegisters(device, gtx, chan, chanreg, chipmask=0x0, debug=False): if (chan not in range(0,128)): msg = "%s: Invalid VFAT channel specified %d"%(device,chan) vfatlogger.warning(colormsg(msg,logging.WARNING)) raise VFATException(colormsg(msg,logging.FATAL)) writeAllVFATs(device, gtx, "VFATChannels.ChanReg%d"%(chan), chanreg, chipmask, debug) return
def setChannelRegister(device, gtx, chip, chan, mask=0x0, pulse=0x0, trim=0x0, debug=False): if (chan not in range(0,128)): msg = "%s: Invalid VFAT channel specified %d"%(device,chan) vfatlogger.warning(colormsg(msg,logging.WARNING)) raise VFATException(colormsg(msg,logging.FATAL)) chanReg = ((pulse&0x1) << 6)|((mask&0x1) << 5)|(trim&0x1f) writeVFAT(device, gtx, chip, "VFATChannels.ChanReg%d"%(chan),chanReg) return
def getOHObject(slot, link, shelf=1, debug=False): connection_file = "file://${GEM_ADDRESS_TABLE_PATH}/connections.xml" manager = uhal.ConnectionManager(connection_file) ohboard = manager.getDevice("gem.shelf%02d.amc%02d.optohybrid%02d" % (shelf, slot, link)) if checkOHBoard(ohboard): msg = "%s: Success!" % (ohboard) ohlogger.info(colormsg(msg, logging.INFO)) return ohboard else: msg = "%s: Failed to create OptoHybrid object" % (ohboard) raise OptoHybridException(colormsg(msg, logging.FATAL))
def scanLatencyByVFAT(device,gtx,vfat, scanmin=0x0,scanmax=0xff, stepsize=0x1,numtrigs=1000, debug=False): """ Run a firmware latency scan on the selected VFAT requires a trigger source @returns an array containing the scan results """ try: checkScanParams(vfat,scanmin,scanmax,stepsize) except ValueError: msg = "Invalid scan configuration specified" scanlogger.error(colormsg(msg,logging.ERROR)) return None channel = 10 originalChannel = vfatfuncs.getChannelRegister(device,gtx,vfat,channel) originalVCal = vfatfuncs.readVFAT(device,gtx,vfat,"VCal") vfatfuncs.setChannelRegister(device,gtx,vfat,channel, mask=0x0,pulse=0x1,trim=trim) vfatfuncs.writeVFAT(device,gtx,vfat,"VCal",0xff) optofuncs.configureScanModule(device,gtx, optofuncs.scanmode.LATENCY, vfat, scanmin,scanmax, stepsize=1,numtrigs=numtrigs, debug=debug) optofuncs.startScanModule(device,gtx) if (debug): optofuncs.printScanConfiguration(device,gtx) pass msg = "LocalT1Controller status %d"%(optofuncs.getLocalT1Status(device,gtx)) scanlogger.debug(colormsg(msg,logging.DEBUG)) data_latency = optofuncs.getScanResults(device,gtx,scanmax-scanmin,debug=debug) # return settings to start mask = (originalChannel>>5)&0x1 pulse = (originalChannel>>6)&0x1 trim = originalChannel&0x1f vfatfuncs.setChannelRegister(device,gtx,vfat,channel, mask=mask,pulse=pulse,trim=trim, debug=debug) vfatfuncs.writeVFAT(device,gtx,vfat,"VCal",originalVcal) return data_latency
def setChannelRegister(device, gtx, chip, chan, mask=0x0, pulse=0x0, trim=0x0, debug=False): if (chan not in range(0, 128)): msg = "%s: Invalid VFAT channel specified %d" % (device, chan) vfatlogger.warning(colormsg(msg, logging.WARNING)) raise VFATException(colormsg(msg, logging.FATAL)) chanReg = ((pulse & 0x1) << 6) | ((mask & 0x1) << 5) | (trim & 0x1f) writeVFAT(device, gtx, chip, "VFATChannels.ChanReg%d" % (chan), chanReg) return
def setRunMode(device, gtx, chip, enable, debug=False): regVal = readVFAT(device, gtx, chip, "ContReg0",debug) if (enable): msg = "%s: Enabling VFAT%02d - Current CR0 value 0x%02x setting to 0x%02x"%(device,chip,regVal,((0xff®Val)|0x1)) vfatlogger.debug(colormsg(msg,logging.DEBUG)) writeVFAT(device, gtx, chip, "ContReg0", ((0xff®Val)|0x01),debug) else: msg = "%s: Disabling VFAT%02d - Current CR0 value 0x%02x setting to 0x%02x"%(device,chip,regVal,((0xff®Val)&0xfe)) vfatlogger.debug(colormsg(msg,logging.DEBUG)) writeVFAT(device, gtx, chip, "ContReg0", ((0xff®Val)&0xfe),debug) pass msg = "%s: VFAT%02d CR0 is now 0x%02x"%(device,chip,readVFAT(device, gtx, chip, "ContReg0",debug)) vfatlogger.debug(colormsg(msg,logging.DEBUG)) return
def scanThresholdByVFAT(device,gtx,vfat, scanmin=0x0,scanmax=0xff, stepsize=0x1,numtrigs=1000, trkmode=False,debug=False): """ Run a firmware threshold scan on the selected VFAT trkmode=True requires a trigger source @returns an array containing the scan results """ optofuncs.configureScanModule(device,gtx, optofuncs.scanmode.THRESHTRG if trkmode else optofuncs.scanmode.THRESHTRK, vfat, scanmin,scanmax,stepsize,numtrigs, debug=debug) optofuncs.startScanModule(device,gtx) if (debug): optofuncs.printScanConfiguration(device,gtx) pass msg = "LocalT1Controller status %d"%(optofuncs.getLocalT1Status(device,gtx)) scanlogger.debug(colormsg(msg,logging.DEBUG)) data_threshold = optofuncs.getScanResults(device,gtx,scanmax-scanmin,debug=debug) return data_threshold
def readAllVFATs(device, gtx, reg, mask=0x0, debug=False): vfatVals = broadcastRead(device,gtx,reg,mask,debug) if vfatVals: msg = "%s: readAllVFATs"%(device) for i,val in enumerate(vfatVals): msg+= "%d: value = 0x%08x\n"%(i,val) pass vfatlogger.debug(colormsg(msg,logging.DEBUG)) pass ## do check on status, maybe only do the check in the calling code #if ((vfatVals >> 26) & 0x1) : # msg = "error on block VFAT transaction (%s)"%(reg) # vfatlogger.debug(colormsg(msg,logging.DEBUG)) # return -1 #elif ((vfatVals >> 25) & 0x0): # msg = "invalid block VFAT transaction (%s)"%(reg) # vfatlogger.debug(colormsg(msg,logging.DEBUG)) # return -1 #elif ((vfatVals >> 24) & 0x0): # msg = "wrong type of block VFAT transaction (%s)"%(reg) # vfatlogger.debug(colormsg(msg,logging.DEBUG)) # return -1 #else : # return vfatVals return vfatVals
def readAllVFATs(device, gtx, reg, mask=0x0, debug=False): vfatVals = broadcastRead(device, gtx, reg, mask, debug) if vfatVals: msg = "%s: readAllVFATs" % (device) for i, val in enumerate(vfatVals): msg += "%d: value = 0x%08x\n" % (i, val) pass vfatlogger.debug(colormsg(msg, logging.DEBUG)) pass ## do check on status, maybe only do the check in the calling code #if ((vfatVals >> 26) & 0x1) : # msg = "error on block VFAT transaction (%s)"%(reg) # vfatlogger.debug(colormsg(msg,logging.DEBUG)) # return -1 #elif ((vfatVals >> 25) & 0x0): # msg = "invalid block VFAT transaction (%s)"%(reg) # vfatlogger.debug(colormsg(msg,logging.DEBUG)) # return -1 #elif ((vfatVals >> 24) & 0x0): # msg = "wrong type of block VFAT transaction (%s)"%(reg) # vfatlogger.debug(colormsg(msg,logging.DEBUG)) # return -1 #else : # return vfatVals return vfatVals
def readAllVFATs(self, reg, mask=0x0): vfatVals = self.parentOH.broadcastRead(reg,mask) if vfatVals: msg = "Link %i: readAllVFATs"%(self.parentOH.link) for i,val in enumerate(vfatVals): msg+= "%d: value = 0x%08x\n"%(i,val) self.vfatlogger.debug(colormsg(msg,logging.DEBUG)) return vfatVals
def scanThresholdByChannel(device, gtx, vfat, channel, scanmin=0x0, scanmax=0xff, stepsize=0x1, numtrigs=1000, debug=False): """ Run a firmware threshold scan on the selected VFAT/channel combination @returns an array containing the scan results """ try: checkScanParams(vfat, scanmin, scanmax, stepsize) except ValueError: msg = "Invalid scan configuration specified" scanlogger.error(colormsg(msg, logging.ERROR)) return None optofuncs.configureScanModule(device, gtx, optofuncs.scanmode.THRESHCH, vfat, channel, scanmin, scanmax, stepsize, numtrigs, debug=debug) optofuncs.startScanModule(device, gtx) if (debug): optofuncs.printScanConfiguration(device, gtx) pass msg = "LocalT1Controller status %d" % (optofuncs.getLocalT1Status( device, gtx)) scanlogger.debug(colormsg(msg, logging.DEBUG)) data_threshold = optofuncs.getScanResults(device, gtx, scanmax - scanmin, debug=debug) return data_threshold
def getUltraScanResults(device, gtx, numpoints, debug=False): scanBase = "GEM_AMC.OH.OH%d.ScanController.ULTRA" % (gtx) ohnL1A_0 = getL1ACount(device, gtx) ohnL1A = getL1ACount(device, gtx) numtrigs = readRegister(device, "%s.CONF.NTRIGS" % (scanBase)) if (readRegister(device, "%s.CONF.MODE" % (scanBase)) == 2): isLatency = True print "At link %s: %d/%d L1As processed, %d%% done" % ( gtx, getL1ACount(device, gtx) - ohnL1A_0, numpoints * numtrigs, (getL1ACount(device, gtx) - ohnL1A_0) * 100. / (numpoints * numtrigs)) else: isLatency = False while (readRegister(device, "%s.MONITOR.STATUS" % (scanBase)) > 0): msg = "%s: Ultra scan still running (0x%x), not returning results" % ( device, readRegister(device, "%s.MONITOR.STATUS" % (scanBase))) ohlogger.debug(colormsg(msg, logging.DEBUG)) if (isLatency): if ((getL1ACount(device, gtx) - ohnL1A) > numtrigs): print "At link %s: %d/%d L1As processed, %d%% done" % ( gtx, getL1ACount(device, gtx) - ohnL1A_0, numpoints * numtrigs, (getL1ACount(device, gtx) - ohnL1A_0) * 100. / (numpoints * numtrigs)) ohnL1A = getL1ACount(device, gtx) else: pass time.sleep(0.1) pass msg = "%s::getScanResults\n" % (device) msg = "Ultra scan status (0x%08x)\n" % (readRegister( device, "%s.MONITOR" % (scanBase))) msg += "Ultra scan results available (0x%06x)" % (readRegister( device, "%s.MONITOR.READY" % (scanBase))) ohlogger.debug(colormsg(msg, logging.DEBUG)) results = [] for chip in range(24): results.append( readBlock(device, "%s.RESULTS.VFAT%d" % (scanBase, chip), numpoints)) return results
def broadcastWrite(device, gtx, register, value, mask=0xff000000, debug=False): """ Perform a broadcast I2C write on the VFATs specified by mask Will return when operation has completed """ baseNode = "GEM_AMC.OH.OH%d.GEB.Broadcast" % (gtx) writeRegister(device, "%s.Reset" % (baseNode), 0x1, debug) writeRegister(device, "%s.Mask" % (baseNode), mask, debug) writeRegister(device, "%s.Request.%s" % (baseNode, register), value, debug) msg = "%s: broadcast read request status 0x%x" % ( device, readRegister(device, "%s.Running" % (baseNode), debug)) ohlogger.debug(colormsg(msg, logging.DEBUG)) while (readRegister(device, "%s.Running" % (baseNode))): msg = "%s: broadcast read request still running..." % (device) ohlogger.debug(colormsg(msg, logging.DEBUG)) time.sleep(0.1) pass
def getScanResults(device, gtx, numpoints, debug=False): scanBase = "GEM_AMC.OH.OH%d.ScanController.THLAT" % (gtx) while (readRegister(device, "%s.MONITOR.STATUS" % (scanBase)) > 0): msg = "%s: Scan still running (0x%x), not returning results" % ( device, readRegister(device, "%s.MONITOR.STATUS" % (scanBase))) ohlogger.debug(colormsg(msg, logging.DEBUG)) time.sleep(0.1) pass msg = "%s::getScanResults\n" % (device) msg += "Scan status (0x%08x)\n" % (readRegister(device, "%s.MONITOR" % (scanBase))) msg += "Scan results available (0x%06x)" % (readRegister( device, "%s.MONITOR.READY" % (scanBase))) ohlogger.debug(colormsg(msg, logging.DEBUG)) results = readBlock(device, "%s.RESULTS" % (scanBase), numpoints) return results
def setRunMode(device, gtx, chip, enable, debug=False): regVal = readVFAT(device, gtx, chip, "ContReg0", debug) if (enable): msg = "%s: Enabling VFAT%02d - Current CR0 value 0x%02x setting to 0x%02x" % ( device, chip, regVal, ((0xff & regVal) | 0x1)) vfatlogger.debug(colormsg(msg, logging.DEBUG)) writeVFAT(device, gtx, chip, "ContReg0", ((0xff & regVal) | 0x01), debug) else: msg = "%s: Disabling VFAT%02d - Current CR0 value 0x%02x setting to 0x%02x" % ( device, chip, regVal, ((0xff & regVal) & 0xfe)) vfatlogger.debug(colormsg(msg, logging.DEBUG)) writeVFAT(device, gtx, chip, "ContReg0", ((0xff & regVal) & 0xfe), debug) pass msg = "%s: VFAT%02d CR0 is now 0x%02x" % ( device, chip, readVFAT(device, gtx, chip, "ContReg0", debug)) vfatlogger.debug(colormsg(msg, logging.DEBUG)) return
def zeroAllVFATChannels(device, gtx, mask=0x0, debug=False): msg = "%s: Zeroing channel registers on all VFATs" % (device) vfatlogger.debug(colormsg(msg, logging.DEBUG)) for chan in range(128): setAllChannelRegisters(device, gtx, chan, chanreg=0x0, chipmask=mask, debug=debug) return
def readVFAT(self, chip, reg, debug=False): baseNode = "" if self.parentOH.parentAMC.fwVersion > 2: baseNode = "GEM_AMC.OH.OH%d.GEB.VFAT%d"%(self.parentOH.link,chip) else: baseNode = "GEM_AMC.OH.OH%d.GEB.VFATS.VFAT%d"%(self.parentOH.link,chip) vfatVal = self.parentOH.parentAMC.readRegister("%s.%s"%(baseNode,reg)) # do check on status if ((vfatVal >> 26) & 0x1) : msg = "error on VFAT transaction (chip %d, %s)"%(chip, reg) self.vfatlogger.debug(colormsg(msg,logging.DEBUG)) return -1 elif ((vfatVal >> 25) & 0x0): msg = "invalid VFAT transaction (chip %d, %s)"%(chip, reg) self.vfatlogger.debug(colormsg(msg,logging.DEBUG)) return -1 elif ((vfatVal >> 24) & 0x0): msg = "wrong type of VFAT transaction (chip %d, %s)"%(chip, reg) self.vfatlogger.debug(colormsg(msg,logging.DEBUG)) return -1 else: return vfatVal
def getConnectedVFATsMask(device, gtx=0, debug=False): """ Returns the broadcast I2C mask corresponding to the connected VFATs """ vfatVals = broadcastRead(device, gtx, "ChipID0") bmask = 0x0 if (vfatVals): for i, val in enumerate(vfatVals): msg = "%d: value = 0x%08x" % (i, val) ohlogger.debug(colormsg(msg, logging.DEBUG)) pass pass return bmask
def enableDAQLink(device, linkEnableMask=0x1, doReset=False): msg = "%s: Reset daq_enable: %i"%(device,1) amclogger.info(colormsg(msg,logging.INFO)) if (doReset): resetDAQLink(device) writeRegister(device, "GEM_AMC.DAQ.CONTROL.DAQ_ENABLE", 0x1) writeRegister(device, "GEM_AMC.DAQ.CONTROL.TTS_OVERRIDE", 0x8) writeRegister(device, "GEM_AMC.DAQ.CONTROL.INPUT_ENABLE_MASK", linkEnableMask) writeRegister(device, "GEM_AMC.DAQ.CONTROL.DAV_TIMEOUT", 0x30D40) for olink in range(NGTX): # in 160MHz clock cycles, so multiply by 4 to get in terms of BX # 0xc35 -> 781 BX writeRegister(device,"GEM_AMC.DAQ.OH%d.CONTROL.EOE_TIMEOUT"%(olink),0x30D4)
def broadcastRead(device, gtx, register, mask=0xff000000, debug=False): """ Perform a broadcast I2C read on the VFATs specified by mask Will return data when operation has completed """ baseNode = "GEM_AMC.OH.OH%d.GEB.Broadcast" % (gtx) writeRegister(device, "%s.Reset" % (baseNode), 0x1, debug) writeRegister(device, "%s.Mask" % (baseNode), mask, debug) readRegister(device, "%s.Request.%s" % (baseNode, register), debug) msg = "%s: broadcast write request status 0x%x" % ( device, readRegister(device, "%s.Running" % (baseNode), debug)) ohlogger.debug(colormsg(msg, logging.DEBUG)) while (readRegister(device, "%s.Running" % (baseNode))): msg = "%s: broadcast write request still running..." % (device) ohlogger.debug(colormsg(msg, logging.DEBUG)) time.sleep(0.1) pass # bitcount = bits not set in mask bitcount = 24 return readBlock(device, "%s.Results" % (baseNode), bitcount)
def enableDAQLink(device, linkEnableMask=0x1, doReset=False): msg = "%s: Reset daq_enable: %i" % (device, 1) amclogger.info(colormsg(msg, logging.INFO)) if (doReset): resetDAQLink(device) writeRegister(device, "GEM_AMC.DAQ.CONTROL.DAQ_ENABLE", 0x1) writeRegister(device, "GEM_AMC.DAQ.CONTROL.TTS_OVERRIDE", 0x8) writeRegister(device, "GEM_AMC.DAQ.CONTROL.INPUT_ENABLE_MASK", linkEnableMask) writeRegister(device, "GEM_AMC.DAQ.CONTROL.DAV_TIMEOUT", 0x30D40) for olink in range(NGTX): # in 160MHz clock cycles, so multiply by 4 to get in terms of BX # 0xc35 -> 781 BX writeRegister(device, "GEM_AMC.DAQ.OH%d.CONTROL.EOE_TIMEOUT" % (olink), 0x30D4)
def scanThresholdByVFAT(device, gtx, vfat, scanmin=0x0, scanmax=0xff, stepsize=0x1, numtrigs=1000, trkmode=False, debug=False): """ Run a firmware threshold scan on the selected VFAT trkmode=True requires a trigger source @returns an array containing the scan results """ optofuncs.configureScanModule(device, gtx, optofuncs.scanmode.THRESHTRG if trkmode else optofuncs.scanmode.THRESHTRK, vfat, scanmin, scanmax, stepsize, numtrigs, debug=debug) optofuncs.startScanModule(device, gtx) if (debug): optofuncs.printScanConfiguration(device, gtx) pass msg = "LocalT1Controller status %d" % (optofuncs.getLocalT1Status( device, gtx)) scanlogger.debug(colormsg(msg, logging.DEBUG)) data_threshold = optofuncs.getScanResults(device, gtx, scanmax - scanmin, debug=debug) return data_threshold
def readVFAT(device, gtx, chip, reg, debug=False): baseNode = "GEM_AMC.OH.OH%d.GEB.VFATS.VFAT%d"%(gtx,chip) vfatVal = readRegister(device,"%s.%s"%(baseNode,reg),debug) # do check on status if ((vfatVal >> 26) & 0x1) : msg = "%s: error on VFAT transaction (chip %d, %s)"%(device,chip, reg) vfatlogger.warning(colormsg(msg,logging.WARNING)) raise VFATException(colormsg(msg,logging.FATAL)) elif ((vfatVal >> 25) & 0x0): msg = "%s: invalid VFAT transaction (chip %d, %s)"%(device,chip, reg) vfatlogger.warning(colormsg(msg,logging.WARNING)) raise VFATException(colormsg(msg,logging.FATAL)) elif ((vfatVal >> 24) & 0x0): msg = "%s: wrong type of VFAT transaction (chip %d, %s)"%(device,chip, reg) vfatlogger.warning(colormsg(msg,logging.WARNING)) raise VFATException(colormsg(msg,logging.FATAL)) else : return vfatVal
def readVFAT(device, gtx, chip, reg, debug=False): baseNode = "GEM_AMC.OH.OH%d.GEB.VFATS.VFAT%d" % (gtx, chip) vfatVal = readRegister(device, "%s.%s" % (baseNode, reg), debug) # do check on status if ((vfatVal >> 26) & 0x1): msg = "%s: error on VFAT transaction (chip %d, %s)" % (device, chip, reg) vfatlogger.warning(colormsg(msg, logging.WARNING)) raise VFATException(colormsg(msg, logging.FATAL)) elif ((vfatVal >> 25) & 0x0): msg = "%s: invalid VFAT transaction (chip %d, %s)" % (device, chip, reg) vfatlogger.warning(colormsg(msg, logging.WARNING)) raise VFATException(colormsg(msg, logging.FATAL)) elif ((vfatVal >> 24) & 0x0): msg = "%s: wrong type of VFAT transaction (chip %d, %s)" % (device, chip, reg) vfatlogger.warning(colormsg(msg, logging.WARNING)) raise VFATException(colormsg(msg, logging.FATAL)) else: return vfatVal
def zeroAllChannels(device,gtx,chip,debug=False): msg = "%s: Zeroing channel registers on VFAT%02d"%(device,chip) vfatlogger.debug(colormsg(msg,logging.DEBUG)) for chan in range(128): setChannelRegister(device,gtx,chip,chan,chanreg=0x0,debug=debug) return
def scanVCalByVFAT(device, gtx, vfat, channel, trim, scanmin=0x0, scanmax=0xff, stepsize=0x1, numtrigs=1000, debug=False): """ Run a firmware VCal scan on the selected VFAT/channel combination requires a trigger source, cal pulse source, channel enabled @returns an array containing the scan results """ try: checkScanParams(vfat, scanmin, scanmax, stepsize) except ValueError: msg = "Invalid scan configuration specified" scanlogger.error(colormsg(msg, logging.ERROR)) return None originalChannel = vfatfuncs.getChannelRegister(device, gtx, vfat, channel) vfatfuncs.setChannelRegister(device, gtx, vfat, channel, mask=0x0, pulse=0x1, trim=trim) msg = "Channel %d register 0x%08x" % ( channel, getChannelRegister(device, gtx, vfat, channel)) scanlogger.debug(colormsg(msg, logging.DEBUG)) optofuncs.configureScanModule(device, gtx, optofuncs.scanmode.SCURVE, vfat, channel=channel, scanmin=scanmin, scanmax=scanmax, stepsize=stepsize, numtrigs=numtrigs, debug=debug) optofuncs.startScanModule(device, gtx) if (debug): optofuncs.printScanConfiguration(device, gtx) pass msg = "LocalT1Controller status %d" % (optofuncs.getLocalT1Status( device, gtx)) scanlogger.debug(colormsg(msg, logging.DEBUG)) data_scurve = optofuncs.getScanResults(device, gtx, scanmax - scanmin, debug=debug) # return settings to start mask = (originalChannel >> 5) & 0x1 pulse = (originalChannel >> 6) & 0x1 trim = originalChannel & 0x1f vfatfuncs.setChannelRegister(device, gtx, vfat, channel, mask=mask, pulse=pulse, trim=trim, debug=debug) msg = "Channel %d register 0x%08x" % ( channel, vfatfuncs.getChannelRegister(device, gtx, vfat, channel)) scanlogger.debug(colormsg(msg, logging.DEBUG)) return data_scurve
while (nRetries < gMAX_RETRIES): try: controlChar = device.getNode(register).read() device.dispatch() return controlChar except uhal.exception, e: nRetries += 1 gRetries += 1 if ((nRetries % 10) == 1): msg = "%s: read (%s) error encountered (%s), retrying operation (%d,%d)" % ( device, register, str(e), nRetries, gRetries) reglogger.warning(msg) continue pass msg = colormsg("%s: Failed to readRegister %s" % (device, register), logging.ERROR) reglogger.error(msg) return 0x0 def readRegisterList(device, registers, debug=False): """ read registers 'registers' from uhal device 'device' returns values of the registers in a dict """ global gRetries nRetries = 0 msg = "%s\n" % (device) if debug: for reg in registers:
def scanLatencyByVFAT(device, gtx, vfat, scanmin=0x0, scanmax=0xff, stepsize=0x1, numtrigs=1000, debug=False): """ Run a firmware latency scan on the selected VFAT requires a trigger source @returns an array containing the scan results """ try: checkScanParams(vfat, scanmin, scanmax, stepsize) except ValueError: msg = "Invalid scan configuration specified" scanlogger.error(colormsg(msg, logging.ERROR)) return None channel = 10 originalChannel = vfatfuncs.getChannelRegister(device, gtx, vfat, channel) originalVCal = vfatfuncs.readVFAT(device, gtx, vfat, "VCal") vfatfuncs.setChannelRegister(device, gtx, vfat, channel, mask=0x0, pulse=0x1, trim=trim) vfatfuncs.writeVFAT(device, gtx, vfat, "VCal", 0xff) optofuncs.configureScanModule(device, gtx, optofuncs.scanmode.LATENCY, vfat, scanmin, scanmax, stepsize=1, numtrigs=numtrigs, debug=debug) optofuncs.startScanModule(device, gtx) if (debug): optofuncs.printScanConfiguration(device, gtx) pass msg = "LocalT1Controller status %d" % (optofuncs.getLocalT1Status( device, gtx)) scanlogger.debug(colormsg(msg, logging.DEBUG)) data_latency = optofuncs.getScanResults(device, gtx, scanmax - scanmin, debug=debug) # return settings to start mask = (originalChannel >> 5) & 0x1 pulse = (originalChannel >> 6) & 0x1 trim = originalChannel & 0x1f vfatfuncs.setChannelRegister(device, gtx, vfat, channel, mask=mask, pulse=pulse, trim=trim, debug=debug) vfatfuncs.writeVFAT(device, gtx, vfat, "VCal", originalVcal) return data_latency
def configureScanModule(device, gtx, mode, vfat, channel=0, scanmin=0x0, scanmax=0xff, stepsize=0x1, numtrigs=1000, useUltra=False, debug=False): """ Configure the firmware scan controller mode: 0 Threshold scan 1 Threshold scan per channel 2 Latency scan 3 s-curve scan 4 Threshold scan with tracking data vfat: for single VFAT scan, specify the VFAT number for ULTRA scan, specify the VFAT mask """ scanBase = "GEM_AMC.OH.OH%d.ScanController.THLAT" % (gtx) if useUltra: scanBase = "GEM_AMC.OH.OH%d.ScanController.ULTRA" % (gtx) pass if (readRegister(device, "%s.MONITOR.STATUS" % (scanBase)) > 0): msg = "%s: Scan is already running (0x%x), not starting a new scan" % ( device, readRegister(device, "%s.MONITOR.STATUS" % (scanBase))) ohlogger.warning(colormsg(msg, logging.WARNING)) return msg = "%s: %s\n" % (device, scanBase) msg += "FW scan mode : %d\n" % (mode) msg += "FW scan min : %d\n" % (scanmin) msg += "FW scan max : %d\n" % (scanmax) if useUltra: msg += "Ultra FW scan mask : 0x%08x" % (vfat) else: msg += "FW scan VFAT : %d\n" % (vfat) pass msg += "FW scan channel : %d\n" % (channel) msg += "FW scan step size : %d\n" % (stepsize) msg += "FW scan n_triggers : %d\n" % (numtrigs) ohlogger.info(colormsg(msg, logging.DEBUG)) writeRegister(device, "%s.RESET" % (scanBase), 0x1) regList = { "%s.CONF.MODE" % (scanBase): mode, "%s.CONF.MIN" % (scanBase): scanmin, "%s.CONF.MAX" % (scanBase): scanmax, "%s.CONF.CHAN" % (scanBase): channel, "%s.CONF.STEP" % (scanBase): stepsize, "%s.CONF.NTRIGS" % (scanBase): numtrigs } if useUltra: regList["%s.CONF.MASK" % (scanBase)] = vfat else: regList["%s.CONF.CHIP" % (scanBase)] = vfat pass writeRegisterList(device, regList) #regList = dict.fromkeys(["GEM_AMC.OH.OH%d.COUNTERS.CRC.INCORRECT.VFAT%d.Reset"%(gtx,i) for i in range(24)],1) #writeRegisterList(device,regList) # for some reason the code above doesn't work and triggers ipbus transaction errors... The code below works! for i in range(24): writeRegister( device, "GEM_AMC.OH.OH%d.COUNTERS.CRC.INCORRECT.VFAT%d.Reset" % (gtx, i), 1) writeRegister( device, "GEM_AMC.OH.OH%d.COUNTERS.CRC.VALID.VFAT%d.Reset" % (gtx, i), 1) return
def zeroAllVFATChannels(device,gtx,mask=0x0,debug=False): msg = "%s: Zeroing channel registers on all VFATs"%(device) vfatlogger.debug(colormsg(msg,logging.DEBUG)) for chan in range(128): setAllChannelRegisters(device,gtx,chan,chanreg=0x0,chipmask=mask,debug=debug) return
def configureLocalT1(device, gtx, mode, t1type, delay, interval, number, debug=False): """ Configure the T1 controller mode: 0 (Single T1 signal), 1 (CalPulse followed by L1A), 2 (pattern) t1type (only for mode 0, type of T1 signal to send): 0 L1A 1 CalPulse 2 Resync 3 BC0 delay (only for mode 1), delay between CalPulse and L1A interval (only for mode 0,1), how often to repeat signals number how many signals to send (0 is continuous) """ ## may be necessary, seems to help resetLocalT1(device, gtx, debug) writeRegister(device, "GEM_AMC.OH.OH%d.T1Controller.MODE" % (gtx), mode, debug) msg = "configuring the T1 controller for mode 0x%x (0x%x)" % ( mode, readRegister(device, "GEM_AMC.OH.OH%d.T1Controller.MODE" % (gtx), debug)) ohlogger.debug(colormsg(msg, logging.DEBUG)) if (mode == 0): writeRegister(device, "GEM_AMC.OH.OH%d.T1Controller.TYPE" % (gtx), t1type, debug) msg = "configuring the T1 controller for type 0x%x (0x%x)" % ( t1type, readRegister(device, "GEM_AMC.OH.OH%d.T1Controller.TYPE" % (gtx), debug)) ohlogger.debug(colormsg(msg, logging.DEBUG)) if (mode == 1): writeRegister(device, "GEM_AMC.OH.OH%d.T1Controller.DELAY" % (gtx), delay, debug) msg = "configuring the T1 controller for delay %d (%d)" % ( delay, readRegister(device, "GEM_AMC.OH.OH%d.T1Controller.DELAY" % (gtx), debug)) ohlogger.debug(colormsg(msg, logging.DEBUG)) if (mode != 2): writeRegister(device, "GEM_AMC.OH.OH%d.T1Controller.INTERVAL" % (gtx), interval, debug) msg = "configuring the T1 controller for interval %d (%d)" % ( interval, readRegister(device, "GEM_AMC.OH.OH%d.T1Controller.INTERVAL" % (gtx), debug)) ohlogger.debug(colormsg(msg, logging.DEBUG)) writeRegister(device, "GEM_AMC.OH.OH%d.T1Controller.NUMBER" % (gtx), number, debug) msg = "configuring the T1 controller for nsignals %d (%d)" % ( number, readRegister(device, "GEM_AMC.OH.OH%d.T1Controller.NUMBER" % (gtx), debug)) ohlogger.debug(colormsg(msg, logging.DEBUG)) return
def getChannelRegister(device, gtx, chip, chan, debug=False): if (chan not in range(0,128)): msg = "%s: Invalid VFAT channel specified %d"%(device,chan) vfatlogger.warning(colormsg(msg,logging.WARNING)) raise VFATException(colormsg(msg,logging.FATAL)) return readVFAT(device, gtx, chip, "VFATChannels.ChanReg%d"%(chan))
def zeroAllChannels(device, gtx, chip, debug=False): msg = "%s: Zeroing channel registers on VFAT%02d" % (device, chip) vfatlogger.debug(colormsg(msg, logging.DEBUG)) for chan in range(128): setChannelRegister(device, gtx, chip, chan, chanreg=0x0, debug=debug) return
while (nRetries < gMAX_RETRIES): try: controlChar = device.getNode(register).read() device.dispatch() return controlChar except uhal.exception, e: nRetries += 1 gRetries += 1 if ((nRetries % 10) == 1): msg = "%s: read (%s) error encountered (%s), retrying operation (%d,%d)"%(device,register,str(e),nRetries,gRetries) reglogger.warning(msg) continue pass msg = colormsg("%s: Failed to readRegister %s"%(device,register),logging.ERROR) reglogger.error(msg) return 0x0 def readRegisterList(device, registers, debug=False): """ read registers 'registers' from uhal device 'device' returns values of the registers in a dict """ global gRetries nRetries = 0 msg = "%s\n"%(device) if debug: for reg in registers: msg+= "%s\n"%(reg)
def linkParser(linkstring, linkmax=None): """ Provides functionality to parse a string as a list of integers 1,2-4,6,9-12 will return [1,2,3,4,6,9,10,11,12] Allowed delimeters are ',', ' ', and ';' Will not fail on invalid options: - 3-3 will be ignored - 6-2 will be ignored - 2- will be ignored - -2 will be ignored If 'linkmax' is set, then values parsed as integers greater than 'linkmax' will be ignored """ import sys,re from gempython.utils.gemlogger import colors,colormsg import logging logger = logging.getLogger(__name__) delimiters = "|".join(map(re.escape,(","," ",";"))) first = re.split(delimiters,linkstring) links = [] for link in first: msg = "parsing link '%s'"%(link) logger.info(colormsg(msg,logging.INFO)) sys.stdout.flush() if link.find("-") > 0: sublinks = link.split("-") if len(sublinks) != 2: msg = "error parsing '%s' as list of links"%(sublinks) logger.warning(colormsg(msg,logging.WARN)) sys.stdout.flush() continue try: low = int(sublinks[0]) hi = int(sublinks[1]) if not (int(low) < int(hi)): msg = "error parsing '%s' as list of links"%(sublinks) logger.warning(colormsg(msg,logging.WARN)) sys.stdout.flush() continue else: if linkmax and hi > linkmax: hi = linkmax msg = "Ignoring values larger than '%d'"%(linkmax) logger.warning(colormsg(msg,logging.WARN)) sys.stdout.flush() pass for li in range(low,hi+1): links.append(li) msg = "adding link '%d' to list of links"%(li) logger.info(colormsg(msg,logging.INFO)) sys.stdout.flush() pass pass except Exception as e: msg = "Caught exception parsing '%s' as list of links"%(sublinks) logger.warning(colormsg(msg,logging.WARN)) sys.stdout.flush() pass pass else: try: li = int(link) if not (linkmax and li > linkmax): links.append(li) msg = "adding link '%d' to list of links"%(li) logger.info(colormsg(msg,logging.INFO)) sys.stdout.flush() pass else: msg = "Ignoring values larger than '%d'"%(linkmax) logger.warning(colormsg(msg,logging.WARN)) sys.stdout.flush() pass except Exception as e: msg = "Caught exception parsing '%s' as a link"%(link) logger.warning(colormsg(msg,logging.WARN)) sys.stdout.flush() pass pass pass return [li for li in set(links)]