DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
debug.py
Go to the documentation of this file.
1from __future__ import print_function
2
3# Python imports
4import uhal
5import click
6import click_didyoumean
7import time
8import collections
9import timing
10
11import timing.cli.toolbox as toolbox
12import timing.common.definitions as defs
13
14from click import echo, style, secho
15from os.path import join, expandvars
16from timing.core import SI534xSlave, I2CExpanderSlave
17
18
19from timing.common.definitions import kBoardSim, kBoardFMC, kBoardPC059, kBoardMicrozed, kBoardTLU, kBoardMIB, kBoardGIB, kBoardPC069
20from timing.common.definitions import kCarrierEnclustraA35, kCarrierKC705, kCarrierMicrozed
21from timing.common.definitions import kBoardNameMap, kCarrierNameMap, kDesignNameMap
22
23
24# ------------------------------------------------------------------------------
25@click.group('debug', invoke_without_command=True)
26@click.pass_obj
27@click.argument('device', callback=toolbox.validate_device, shell_complete=toolbox.completeDevices)
28def debug(obj, device):
29 '''
30 Timing master commands.
31
32 DEVICE: uhal device identifier
33 '''
34 lDevice = obj.mConnectionManager.getDevice(str(device))
35 if obj.mTimeout:
36 lDevice.setTimeoutPeriod(obj.mTimeout)
37
38 echo('Created device ' + click.style(lDevice.id(), fg='blue'))
39
40 lBoardInfo = toolbox.readSubNodes(lDevice.getNode('io.config'), False)
41 lDevice.dispatch()
42
43 # print({ k:v.value() for k,v in lBoardInfo.items()})
44 # raise SystemExit(0)
45
46 echo("Design '{}' on board '{}' on carrier '{}'".format(
47 style(kDesignNameMap[lBoardInfo['design_type'].value()], fg='blue'),
48 style(kBoardNameMap[lBoardInfo['board_type'].value()], fg='blue'),
49 style(kCarrierNameMap[lBoardInfo['carrier_type'].value()], fg='blue')
50 ))
51
52 obj.mDevice = lDevice
53 obj.mBoardType = lBoardInfo['board_type'].value()
54 obj.mCarrierType = lBoardInfo['carrier_type'].value()
55 obj.mDesignType = lBoardInfo['design_type'].value()
56# ------------------------------------------------------------------------------
57
58
59# ------------------------------------------------------------------------------
60@debug.command('inspect')
61@click.argument('nodes')
62@click.pass_obj
63def inspect(obj, nodes):
64 lDevice = obj.mDevice
65
66 lNodeIds = lDevice.getNodes(nodes.encode('ascii','replace'))
67 lNodeVals = {n:lDevice.getNode(n).read() for n in lNodeIds}
68
69 lDevice.dispatch()
70
71 toolbox.printRegTable(lNodeVals, False)
72# ------------------------------------------------------------------------------
73
74
75# ------------------------------------------------------------------------------
76@debug.command()
77@click.pass_context
78def ipy(ctx):
79 '''
80 Start an interactive IPython session.
81
82 The board HwInterface is accessible as 'lDevice'
83 '''
84 lDevice = ctx.obj.mDevice
85
86 from IPython import embed
87 embed()
88# ------------------------------------------------------------------------------
89
90
91# ------------------------------------------------------------------------------
92@debug.command('uid', short_help="Unique ID reader.")
93@click.pass_obj
94def uuid(obj):
95
96 lDevice = obj.mDevice
97 lBoardType = obj.mBoardType
98 lIO = lDevice.getNode('io')
99
100 # Detect the on-board eprom and read the board UID
101 if lBoardType in [kBoardPC059, kBoardTLU, kBoardMIB, kBoardGIB]:
102 lUID = lDevice.getNode('io.i2c')
103 else:
104 lUID = lDevice.getNode('io.uid_i2c')
105
106 lPROMSlave = 'UID_PROM' if lBoardType in [kBoardTLU,kBoardMIB,kBoardGIB] else 'FMC_UID_PROM'
107
108 if lBoardType == kBoardGIB:
109 lDevice.getNode("io.csr.ctrl.i2c_sw_rst").write(0x0)
110 lDevice.dispatch()
111 lDevice.getNode("io.csr.ctrl.i2c_sw_rst").write(0x1)
112 lDevice.dispatch()
113 lIO.set_i2c_mux_channels(0x1)
114
115 lValues = lUID.get_slave(lPROMSlave).read_i2cArray(0xfa, 6)
116 lUniqueID = 0x0
117 for lVal in lValues:
118 lUniqueID = ( lUniqueID << 8 ) | lVal
119 echo("Timing Board PROM UID: "+style(hex(lUniqueID), fg="blue"))
120# ------------------------------------------------------------------------------
121
122
123# ------------------------------------------------------------------------------
124@debug.command('sfpexpander', short_help="Debug.")
125@click.pass_obj
126def sfpexpander(obj):
127 lDevice = obj.mDevice
128 lBoardType = obj.mBoardType
129
130 if lBoardType != kBoardPC059:
131 secho('No SFP expander on {}'.format(kBoardNameMap[lBoardInfo['board_type'].value()]))
132 return
133 lI2CBusNode = lDevice.getNode("io.i2c")
134 lSFPExp = I2CExpanderSlave(lI2CBusNode, lI2CBusNode.get_slave('SFPExpander').get_i2c_address())
135 lSFPExpStatus = lSFPExp.debug()
136
137 lLabels = [
138 'B0 values',
139 'B1 values',
140 'B0 enable',
141 'B1 enable',
142 'B0 invert',
143 'B1 invert',
144 'B0 I/O ',
145 'B1 I/O ',
146 ]
147 for a,v in enumerate(lSFPExpStatus):
148 echo("{} ({}): {}".format(lLabels[a], hex(a), hex(v)))
149# ------------------------------------------------------------------------------
150
151
152# ------------------------------------------------------------------------------
153@debug.command('scan-i2c', short_help="Debug.")
154@click.pass_obj
155def sfp_status(obj):
156 lDevice = obj.mDevice
157 lBoardType = obj.mBoardType
158
159 lNodes = []
160 lSwitches={}
161 lSwitchChannels={}
162 if lBoardType in [kBoardFMC, kBoardPC069]:
163 lNodes = ['io.sfp_i2c','io.uid_i2c','io.pll_i2c']
164 elif lBoardType == kBoardPC059:
165 lNodes = ['io.i2c', 'io.usfp_i2c']
166 switch_address=lDevice.getNode('io.i2c').get_slave_address('SFP_Switch')
167 lSwitches={"io.i2c": switch_address}
168 lSwitchChannels={"io.i2c": 8}
169 elif lBoardType in [kBoardTLU, kBoardMIB, kBoardGIB]:
170 lNodes = ['io.i2c']
171 if lBoardType == kBoardGIB:
172 lSwitches={"io.i2c": 0x70}
173 lSwitchChannels={"io.i2c": 7}
174 else:
175 secho(f"Error I don't know about board {lBoardType} : {kBoardNameMap[lBoardType]}", fg='red')
176 # if lBoardType == kBoardPC059:
177 # lSFPSwitch = lDevice.getNode('io.i2c').get_slave('SFP_Switch')
178 # print(lSFPSwitch.read_i2cPrimitive(1))
179 # print(lSFPSwitch.write_i2cPrimitive([0x1]))
180 # print(lSFPSwitch.read_i2cPrimitive(1))
181
182 for n in lNodes:
183 lI2CBusNode = lDevice.getNode(n)
184 echo('Scanning '+style(n,fg='cyan'))
185 lAddresses = lI2CBusNode.scan()
186 print(" '{}': {} devices found.\n Addresses: {}".format(n, len(lAddresses), ', '.join((hex(a) for a in lAddresses))))
187 if n in lSwitches:
188 print(f" Found {len(lSwitches)} switches.\n Addresses: {lSwitches.values()}")
189 for switch,address in lSwitches.items():
190 print (f"switch {switch} address: {address}")
191 switch_channels=lSwitchChannels[switch]
192 print(f"working with {switch}, @ adr {address}, it has {switch_channels} channels")
193 for channel in range(0,switch_channels):
194 secho(f"Scanning with channel {channel} enabled", fg='cyan')
195 lI2CBusNode.write_i2cPrimitive(address, [1<<channel])
196 lAddresses = lI2CBusNode.scan()
197 print(" '{}': {} devices found.\n Addresses: {}".format(n, len(lAddresses), ', '.join((hex(a) for a in lAddresses))))
198
199
200
201# ------------------------------------------------------------------------------
202
203@debug.command('fanout-sfp-scan', short_help="Debug.")
204@click.pass_obj
206
207 lDevice = obj.mDevice
208 lBoardType = obj.mBoardType
209
210 if lBoardType != kBoardPC059:
211 print('Wrong board', lBoardType, 'sorry mate')
212 return
213
214
215 lDevice.getNode('io.csr.ctrl.rst_i2cmux').write(0x1)
216 lDevice.dispatch()
217 lDevice.getNode('io.csr.ctrl.rst_i2cmux').write(0x0)
218 lDevice.dispatch()
219 time.sleep(1)
220
221 lSFPNodeName = 'io.i2c'
222 lI2CBusNode = lDevice.getNode(lSFPNodeName)
223 lSwitchSlave = lI2CBusNode.get_slave('SFP_Switch')
224 print(lSwitchSlave.ping())
225
226 # print(lSwitchSlave.read_i2cPrimitive(1))
227 # time.sleep(0.1)
228
229 lSwitchSlave.write_i2cPrimitive([3])
230 print(lSwitchSlave.read_i2cPrimitive(1))
231 print(lSwitchSlave.read_i2cPrimitive(1))
232 print(lSwitchSlave.read_i2cPrimitive(1))
233
234# ------------------------------------------------------------------------------
235@debug.command('sfp-status', short_help="Debug.")
236@click.pass_obj
237def sfp_status(obj):
238
239 lDevice = obj.mDevice
240 lBoardType = obj.mBoardType
241
242 if lBoardType == kBoardFMC:
243 lSFPNodeName = 'io.sfp_i2c'
244 lSFPLabel = 'SFP'
245 elif lBoardType == kBoardPC059:
246 lSFPNodeName = 'io.usfp_i2c'
247 lSFPLabel = 'USFP'
248 else:
249 secho('No SFP on {}'.format(kBoardNameMap[lBoardType]))
250 return
251
252 lI2CBusNode = lDevice.getNode(lSFPNodeName)
253 lEEPromSlave = lI2CBusNode.get_slave('SFP_EEProm')
254 lDiagSlave = lI2CBusNode.get_slave('SFP_Diag')
255
256 readSFPStatus(lEEPromSlave, lDiagSlave, lSFPLabel)
257
258 if lBoardType == kBoardPC059:
259 lDevice.getNode('io.csr.ctrl.rst_i2cmux').write(0x1)
260 lDevice.dispatch()
261 lDevice.getNode('io.csr.ctrl.rst_i2cmux').write(0x0)
262 lDevice.dispatch()
263 time.sleep(0.1)
264
265 lI2CBusNode = lDevice.getNode('io.i2c')
266 lSwitchSlave = lI2CBusNode.get_slave('SFP_Switch')
267 lEEPromSlave = lI2CBusNode.get_slave('SFP_EEProm')
268 lDiagSlave = lI2CBusNode.get_slave('SFP_Diag')
269
270 # lOld = lSwitchSlave.read_i2cPrimitive(1)
271 # lSwitchSlave.write_i2cPrimitive([0x0])
272 time.sleep(0.1)
273 for iSFP in range(8):
274 try:
275 lSwitchSlave.write_i2cPrimitive([1 << iSFP])
276 except RuntimeError as lExc:
277 pass
278 if not lEEPromSlave.ping():
279 secho('SFP {} not available'.format(iSFP),fg='yellow')
280 continue
281 else:
282 secho('SFP {} found'.format(iSFP),fg='green')
283 # readSFPStatus(lEEPromSlave, lDiagSlave)
284
285# ------------------------------------------------------------------------------
286
287
288
289# ------------------------------------------------------------------------------
290def readSFPStatus(aEEProm, aDiag, aLabel):
291
292 def asciidecode( v ):
293 return (''.join([chr(c) for c in v])).rstrip()
294
295 def tempdecode( v ):
296 lSign = -1 if ((v[0]>>7) & 0x1) else 1
297 x = lSign*((v[0] & 0x7f) + v[1]/float(0xff))
298 return '{:.3f} C'.format(x)
299
300 def vccdecode( v ):
301 return '{:.4f} V'.format(((v[0] << 8) + v[1])/float(10000))
302
303 def biascurdecode( v ):
304 return '{:.3f} mA'.format(((v[0] << 8) + v[1])*2e-3)
305
306 def powerdecode( v ):
307 return '{:.3f} mW'.format(((v[0] << 8) + v[1])*1e-3)
308
309
310 echo()
311 lVenInfoEnc = collections.OrderedDict()
312 lVenInfoEnc['Name'] = asciidecode(aEEProm.read_i2cArray(20,16))
313 lVenInfoEnc['OUI'] = '{}.{}.{}'.format(*(aEEProm.read_i2cArray(37,3)))
314 lVenInfoEnc['Part Number'] = asciidecode(aEEProm.read_i2cArray(40,16))
315 lVenInfoEnc['Revision'] = asciidecode(aEEProm.read_i2cArray(56,4))
316 lVenInfoEnc['Serial Number'] = asciidecode(aEEProm.read_i2cArray(68,16))
317 lVenInfoEnc['Day'] = asciidecode(aEEProm.read_i2cArray(88,2))
318 lVenInfoEnc['Month'] = asciidecode(aEEProm.read_i2cArray(86,2))
319 lVenInfoEnc['Year'] = asciidecode(aEEProm.read_i2cArray(84,2))
320
321 secho("{} Vendor info".format(aLabel), fg='cyan')
322 # for k,v in lVenInfoEnc.items():
323 # v = ''.join([chr(c) for c in v])
324 # echo(' - '+k+': '+style(v, fg='cyan'))
325 echo(toolbox.formatDictTable(lVenInfoEnc, aHdr=False, aSort=False))
326 echo()
327 lLaserWl = aEEProm.read_i2cArray(60,2)
328 lLaserWl = (lLaserWl[0] << 8) + lLaserWl[1]
329 echo('Laser Wavelength: '+style(str(lLaserWl)+'nm', fg='cyan'))
330
331 lRegs = collections.OrderedDict()
332
333 lRegs['Identifier'] = aEEProm.read_i2c(0)
334 lRegs['Ext Identifier'] = aEEProm.read_i2c(1)
335 lRegs['Connector'] = aEEProm.read_i2c(2)
336
337 # Transciever Compatinility
338 lTransComp = aEEProm.read_i2cArray(3, 8)
339
340 lRegs['Encoding'] = aEEProm.read_i2c(11)
341 lRegs['BR, Nominal'] = aEEProm.read_i2c(12)
342 lRegs['Rate ID'] = aEEProm.read_i2c(13)
343
344 toolbox.printRegTable(lRegs, aHeader=False, sort=False)
345
346 echo()
347 secho("{} Diagnostic info".format(aLabel), fg='cyan')
348
349 lReadings = collections.OrderedDict()
350
351 lReadings['Temp'] = tempdecode(aDiag.read_i2cArray(96, 2))
352 lReadings['Vcc'] = vccdecode(aDiag.read_i2cArray(98, 2))
353 lReadings['TX bias'] = biascurdecode(aDiag.read_i2cArray(100, 2))
354 lReadings['TX power'] = powerdecode(aDiag.read_i2cArray(102, 2))
355 lReadings['RX power'] = powerdecode(aDiag.read_i2cArray(104, 2))
356 lMiscStatus = aDiag.read_i2c(110)
357 lReadings['TX disable'] = (lMiscStatus >> 7) & 0x1
358
359 # for k,v in lReadings.items():
360 # print (k, v)
361
362 echo(toolbox.formatDictTable(lReadings, aHdr=False, aSort=False))
363# ------------------------------------------------------------------------------
readSFPStatus(aEEProm, aDiag, aLabel)
Definition debug.py:290
sfp_status(obj)
Definition debug.py:155
inspect(obj, nodes)
Definition debug.py:63
sfpexpander(obj)
Definition debug.py:126
fanout_sfpscan(obj)
Definition debug.py:205