00001 #!/usr/local/bin/python 00002 # 00003 # Copyright 2004 00004 # by 00005 # The Board of Trustees of the 00006 # Leland Stanford Junior University. 00007 # All rights reserved. 00008 # 00009 00010 00011 __facility__ = "Online" 00012 __abstract__ = "GLAST GLT functional block" 00013 __author__ = "Selim Tuvi <stuvi@slac.stanford.edu> SLAC - GLAST LAT I&T/Online" 00014 __date__ = ("$Date: 2005/02/02 00:42:04 $").split(' ')[1] 00015 __version__ = "$Revision: 2.7 $" 00016 __release__ = "$Name: R04-12-00 $" 00017 __credits__ = "SLAC" 00018 00019 import LATTE.copyright_SLAC 00020 00021 from threading import Event 00022 00023 from gDb import Gdb 00024 import gAttr 00025 import LATTE.client.gBits as gBits 00026 00027 class GGLT(Gdb): 00028 """\brief Global Trigger 00029 00030 Has GGLT specific registers and dataless commands. 00031 00032 Valid Registers: 00033 - \c mask 00034 - \c busy_disable 00035 - \c ext_cal_strobe 00036 - \c ext_tack 00037 - \c counter_mask 00038 - \c counter 00039 - \c ext_clk_out_enable 00040 00041 Dataless Commands: 00042 - \c cmd_reset 00043 - \c cmd_self_trigger 00044 00045 <a name="option_fields"></a> 00046 Options Register Bit Fields: 00047 - \c parity 00048 - \c tag 00049 - \c event_number 00050 00051 <a name="conf_fields"></a> 00052 Configuration Register Bit Fields: 00053 - \c destination 00054 - \c marker 00055 - \c tack 00056 - \c zero_suppress 00057 - \c four_range_readout 00058 - \c cal_strobe 00059 00060 Mask Register Bit Fields: 00061 - \c internal_trg 00062 - \c external_trg 00063 - \c cal_low 00064 - \c cal_high 00065 - \c three_in_a_row 00066 - \c acd_veto_00 00067 - \c acd_veto_01 00068 - \c acd_veto_02 00069 - \c acd_veto_03 00070 - \c acd_veto_04 00071 - \c acd_veto_05 00072 - \c acd_veto_06 00073 - \c acd_veto_07 00074 - \c acd_veto_08 00075 - \c acd_veto_09 00076 - \c acd_veto_10 00077 - \c acd_veto_11 00078 - \c acd_veto_12 00079 - \c acd_veto_13 00080 - \c acd_veto_14 00081 - \c acd_veto_15 00082 - \c acd_veto_16 00083 - \c acd_veto_17 00084 - \c acd_cno 00085 00086 """ 00087 __attrs = [ 00088 # Registers 00089 gAttr.GattrRaw('mask', 1, 4), 00090 gAttr.GattrRaw('busy_disable', 3, 2), 00091 gAttr.GattrRaw('ext_cal_strobe', 4, 2), 00092 gAttr.GattrRaw('ext_tack', 5, 2), 00093 gAttr.GattrRaw('counter_mask', 6, 4), 00094 gAttr.GattrRaw('counter', 7, 4), 00095 gAttr.GattrRaw('ext_clk_out_enable', 8, 4), 00096 # Dataless Commands 00097 gAttr.GattrDlc('cmd_reset', 9, 0), 00098 gAttr.GattrDlc('cmd_self_trigger', 10, 0), 00099 # Bit fields 00100 gAttr.GattrRaw('parity', 11, 2), 00101 gAttr.GattrRaw('tag', 12, 2), 00102 gAttr.GattrRaw('event_number', 13, 2), 00103 gAttr.GattrRaw('destination', 14, 2), 00104 gAttr.GattrRaw('marker', 15, 2), 00105 gAttr.GattrRaw('tack', 16, 2), 00106 gAttr.GattrRaw('zero_suppress', 17, 2), 00107 gAttr.GattrRaw('four_range_readout', 18, 2), 00108 gAttr.GattrRaw('cal_strobe', 19, 2) 00109 ] 00110 00111 __mask_BF = [ 00112 ('internal_trg', 0, 1), 00113 ('external_trg', 1, 2), 00114 ('cal_low', 2, 3), 00115 ('cal_high', 3, 4), 00116 ('three_in_a_row', 4, 5), 00117 ('acd_veto_00', 5, 6), 00118 ('acd_veto_01', 6, 7), 00119 ('acd_veto_02', 7, 8), 00120 ('acd_veto_03', 8, 9), 00121 ('acd_veto_04', 9, 10), 00122 ('acd_veto_05', 10, 11), 00123 ('acd_veto_06', 11, 12), 00124 ('acd_veto_07', 12, 13), 00125 ('acd_veto_08', 13, 14), 00126 ('acd_veto_09', 14, 15), 00127 ('acd_veto_10', 15, 16), 00128 ('acd_veto_11', 16, 17), 00129 ('acd_veto_12', 17, 18), 00130 ('acd_veto_13', 18, 19), 00131 ('acd_veto_14', 19, 20), 00132 ('acd_veto_15', 20, 21), 00133 ('acd_veto_16', 21, 22), 00134 ('acd_veto_17', 22, 23), 00135 ('acd_cno', 23, 24) 00136 ] 00137 00138 __TRG_DISABLE_MASK = 0xFFFFFFFFL 00139 00140 __EPU_ADDR = 0x20 00141 00142 def __init__(self, client, parent=None): 00143 """\brief Initialize valid registers as Gattrs. 00144 00145 Assign default values to members 00146 """ 00147 self.__dirtyMask = 0 00148 self.__saveMask = None 00149 self.__bitsMask = 0xFFFFFFFFL 00150 self.__triggersEnabled = Event() 00151 self.__enableTime = None 00152 self.__disableTime = None 00153 self.__gltMode = 1 00154 self.__destinationOverride = 0 00155 self.__lcbAddr = None 00156 Gdb.__init__(self, client, parent, 0, self.__attrs) 00157 00158 def getEPUaddr(self): 00159 # Hard code EPU_ADDR to 0x20 to work with the Caligula FSW release 00160 return GGLT.__EPU_ADDR 00161 00162 def read(self, reg): 00163 return self.cmdrsp('ggGLTread', reg, '!I') 00164 00165 def load(self, reg, value): 00166 return self.cmdrsp('ggGLTload', reg, value) 00167 00168 def send(self, cmd): 00169 return self.cmdrsp('ggGLTcmd', cmd) 00170 00171 def initialize(self): 00172 if self.__gltMode == 1: 00173 # We need to find which bits are enabled 00174 self.refreshMask() 00175 #self.regs['mask'].set(self, GGLT.__TRG_DISABLE_MASK) # Revisit: Why next? 00176 Gdb.__setattr__(self, 'mask', GGLT.__TRG_DISABLE_MASK) 00177 self.__bitsMask = Gdb.__getattr__(self, 'mask') 00178 Gdb.__setattr__(self, 'mask', int(self.__saveMask)) 00179 if self.__lcbAddr is None: 00180 self.__lcbAddr = self.getEPUaddr() 00181 if self.__destinationOverride == 0: 00182 Gdb.__setattr__(self, 'destination', self.__lcbAddr) 00183 00184 def waitForMaskEnable(self, tmo = None): 00185 """\brief Waits until the enableMask() method is called. 00186 00187 This is usually needed by non-self-triggering scripts where 00188 the _commandSynch thread needs to wait until RunControl 00189 actually enables the mask before continuing. 00190 00191 \param tmo Optional parameter giving the wait timeout in seconds 00192 """ 00193 self.__triggersEnabled.wait(tmo) 00194 00195 def enableMask(self): 00196 """\brief Commits the mask value to hardware. 00197 00198 \return The value of the dirty flag before the commit is made. 00199 """ 00200 if self.__gltMode == 1: 00201 if self.__dirtyMask: 00202 if int(self.__saveMask) == GGLT.__TRG_DISABLE_MASK: 00203 self.__triggersEnabled.clear() 00204 Gdb.__setattr__(self, 'mask', int(self.__saveMask)) 00205 self.__disableTime = self.regs['mask'].getTimeStamp() 00206 else: 00207 Gdb.__setattr__(self, 'mask', int(self.__saveMask)) 00208 self.__enableTime = self.regs['mask'].getTimeStamp() 00209 self.__triggersEnabled.set() 00210 self.__dirtyMask = 0 00211 return 1 00212 else: 00213 return 0 00214 else: 00215 return 0 00216 00217 def disableMask(self): 00218 """\brief Disables all trigger masks but doesn't change the cached mask value. 00219 00220 This way when the triggers are enabled again, the old setting can be applied. 00221 """ 00222 #Note: This method should not modify the cached value. 00223 if self.__gltMode == 1: 00224 self.__dirtyMask = 1 00225 self.__triggersEnabled.clear() 00226 Gdb.__setattr__(self, 'mask', GGLT.__TRG_DISABLE_MASK) 00227 self.__disableTime = self.regs['mask'].getTimeStamp() 00228 00229 def triggersDisabled(self): 00230 """\brief Check if the triggers are disabled. 00231 00232 \return True if triggers are disabled, 00233 False if triggers are disabled. 00234 """ 00235 return not self.__triggersEnabled.isSet() 00236 00237 def isMaskDirty(self): 00238 """\brief Return the mask dirty flag. 00239 00240 \return Mask dirty flag. 00241 """ 00242 return self.__dirtyMask 00243 00244 def refreshMask(self): 00245 """\brief Refreshes the cached value of the mask from the hardware. 00246 """ 00247 self.__saveMask = gBits.bf(Gdb.__getattr__(self, 'mask')) 00248 00249 def getEnableTime(self): 00250 """\brief Returns the VxWorks timestamp at the time the triggers are enabled. 00251 00252 \return Trigger enable time 00253 """ 00254 return self.__enableTime 00255 00256 def getDisableTime(self): 00257 """\brief Returns the VxWorks timestamp at the time the triggers are disabled. 00258 00259 \return Trigger disable time 00260 """ 00261 return self.__disableTime 00262 00263 def setDestinationOverride(self, override): 00264 """\brief Sets the flag to determine whether the user should be able to 00265 override the destination value. 00266 00267 \param override 0=Destination is supplied as the LCB address as returned by CmdCli.getLcbAddr() 00268 1=The user supplied destination value takes effect 00269 """ 00270 self.__destinationOverride = override 00271 if override == 0: 00272 Gdb.__setattr__(self, 'destination', self.__lcbAddr) 00273 00274 00275 ## private: 00276 00277 def __setGLTmode(self, mode): 00278 """Controls the GLT behaviour 00279 00280 \param mode If set to 0 the GLT behaves in immediate mode 00281 meaning the trigger mask settings take effect 00282 immediately. 00283 If set to 1 then the settings are stored in a 00284 cached variable and the script needs to call 00285 enableMask() and/or disableMask() to commit 00286 the changes from the cache to the hardware. 00287 """ 00288 self.__gltMode = mode 00289 00290 def __getGLTmode(self): 00291 """Retrieves the current GLT mode 00292 00293 \return Current GLT mode 00294 """ 00295 return self.__gltMode 00296 00297 def __getattr__(self, attribute): 00298 attr = attribute.lower() 00299 if attr == 'mask' and self.__gltMode == 1: 00300 return int(self.__saveMask) 00301 else: 00302 for (bitField, fromBit, toBit) in self.__mask_BF: 00303 if attr == bitField: 00304 if self.__gltMode == 1: 00305 return self.__saveMask[fromBit:toBit] 00306 else: 00307 mask = gBits.bf(Gdb.__getattr__(self, 'mask')) 00308 return mask[fromBit:toBit] 00309 return Gdb.__getattr__(self, attribute) 00310 00311 def __setattr__(self, attribute, value): 00312 attr = attribute.lower() 00313 if attr == 'mask' and self.__gltMode == 1: 00314 self.__dirtyMask = 1 00315 self.__saveMask = gBits.bf(value & self.__bitsMask) 00316 return 00317 elif attr == 'cmd_self_trigger': 00318 pass 00319 elif attr == 'destination': 00320 if not self.__destinationOverride: 00321 return 00322 else: 00323 for (bitField, fromBit, toBit) in self.__mask_BF: 00324 if attr == bitField: 00325 if self.__gltMode == 1: 00326 newVal = gBits.bf(0) 00327 newVal[fromBit:toBit] = value 00328 if (int(newVal) | self.__bitsMask) == self.__bitsMask: 00329 self.__dirtyMask = 1 00330 self.__saveMask[fromBit:toBit] = value 00331 else: 00332 mask = gBits.bf(Gdb.__getattr__(self, 'mask')) 00333 mask[fromBit:toBit] = value 00334 Gdb.__setattr__(self, 'mask', int(mask)) 00335 return 00336 Gdb.__setattr__(self, attribute, value) 00337