1from __future__
import print_function
16from click
import echo, style, secho
17from os.path
import join, expandvars, basename
18from timing.core import SI534xSlave, I2CExpanderSlave, DACSlave
20from timing.common.definitions import kBoardSim, kBoardFMC, kBoardPC059, kBoardMicrozed, kBoardTLU, kBoardFIB, kBoardMIB, kBoardPC069, kBoardGIB, kBoardGIBV3, kFIBRev2
21from timing.common.definitions import kFMCRev1, kFMCRev2, kFMCRev3, kFMCRev4, kPC059Rev1, kTLURev1, kSIMRev1, kFIBRev1, kMIBRev1, kGIBRev1
22from timing.common.definitions import kCarrierEnclustraA35, kCarrierKC705, kCarrierMicrozed, kCarrierNexusVideo, kCarrierTrenzTE0712
23from timing.common.definitions import kDesignMaster, kDesignOuroboros, kDesignOuroborosSim, kDesignEndpoint, kDesignFanout, kDesignChronos, kDesignBoreas, kDesignTest, kDesignKerberos, kDesignGaia, kDesignCharon, kDesignHades
35@click.group('io', invoke_without_command=True)
37@click.argument('device', callback=toolbox.validate_device, shell_complete=toolbox.completeDevices)
40 Timing master commands.
42 DEVICE: uhal device identifier
44 lDevice = obj.mConnectionManager.getDevice(str(device))
46 lDevice.setTimeoutPeriod(obj.mTimeout)
48 echo(
'Created device ' + click.style(lDevice.id(), fg=
'blue'))
49 lTopDesign = lDevice.getNode(
'')
50 lBoardInfo = toolbox.readSubNodes(lDevice.getNode(
'io.config'),
False)
53 if lBoardInfo[
'board_type'].value()
in kLibrarySupportedBoards
and lBoardInfo[
'design_type'].value()
in kLibrarySupportedDesigns:
54 lTopDesign.validate_firmware_version()
56 echo(
"Design '{}' on board '{}' on carrier '{}' with frequency {} MHz".format(
57 style(kDesignNameMap[lBoardInfo[
'design_type'].value()], fg=
'blue'),
58 style(kBoardNameMap[lBoardInfo[
'board_type'].value()], fg=
'blue'),
59 style(kCarrierNameMap[lBoardInfo[
'carrier_type'].value()], fg=
'blue'),
60 style(str(lBoardInfo[
'clock_frequency'].value()/1e6), fg=
'blue')
65 obj.mBoardType = lBoardInfo[
'board_type'].value()
66 obj.mCarrierType = lBoardInfo[
'carrier_type'].value()
67 obj.mDesignType = lBoardInfo[
'design_type'].value()
72@io.command('reset', short_help="Perform a hard reset on the timing master.")
73@click.option('--soft', '-s', is_flag=True, default=False, help='Soft reset i.e. skip the clock chip configuration.')
74@click.option('--clock-source', 'clocksource', type=click.Choice(ClockSource.__members__.keys()), help=
'Manually specify clock source, free-running, upstream, etc..')
75@click.option('--force-pll-cfg', 'forcepllcfg', type=click.Path(exists=True), help=
'Manually specify clock config file' )
78def reset(ctx, obj, soft, clocksource, forcepllcfg):
80 Perform a hard reset on a timing board, including
85 - pll and pll configuration
88 echo(
'Resetting ' + click.style(obj.mDevice.id(), fg=
'blue'))
91 lBoardType = obj.mBoardType
92 lDesignType = obj.mDesignType
94 lIO = lDevice.getNode(
'io')
96 if lBoardType
in kLibrarySupportedBoards:
102 if forcepllcfg
is not None:
103 if clocksource
is not None:
104 secho(
"You specified both a clock source for automatic clock config file look-up, and an explicit clock config file. Explicit clock config file will take precedence.", fg=
'yellow')
106 lIO.reset(forcepllcfg)
108 if clocksource
is None:
109 lClockSource = toolbox.get_default_clock_source(
110 lDesignType, lBoardType)
111 if lClockSource
is None:
112 secho(f
"Unable to match a default clock source for {kDesignNameMap[lDesignType]} on {kBoardNameMap[lBoardType]}\nReset failed!".format(), fg=
'red')
115 secho(f
"Default clock config selected for {kDesignNameMap[lDesignType]} on {kBoardNameMap[lBoardType]} is: {lClockSource}", fg=
'yellow')
117 lClockSource=ClockSource.__members__[clocksource]
127 lIO.reset(lClockSource)
129 ctx.invoke(clkstatus)
131 secho(
"Board identifier {} not supported by timing library".format(lBoardType), fg=
'yellow')
136@io.command('freq', short_help="Measure some frequencies.")
139 lDevice = obj.mDevice
140 lBoardType = obj.mBoardType
141 lIO = lDevice.getNode(
'io')
143 secho(
"PLL Clock frequency measurement:", fg=
'cyan')
145 if lBoardType
in kLibrarySupportedBoards:
146 echo(lIO.get_clock_frequencies_table())
148 secho(
"Board {} not supported by timing library".format(lBoardType), fg=
'yellow')
150 if lBoardType == kBoardMIB:
154 secho(
'MIB clock freqs', fg=
'green')
161@click.option('-v', 'verbose', is_flag=True)
164 lDevice = obj.mDevice
165 lBoardType = obj.mBoardType
166 lIO = lDevice.getNode(
'io')
168 if lBoardType
in kLibrarySupportedBoards:
169 ctx.invoke(print_hardware_info)
170 echo(lIO.get_status())
172 secho(
"Board {} not supported by timing library".format(lBoardType), fg=
'yellow')
177@io.command('configure-pll', short_help="Configure board PLL.")
178@click.option('--clock-source', 'clocksource', type=click.Choice(ClockSource.__members__.keys()), help=
'Manually specify clock source, free-running, upstream, etc..')
179@click.option('--force-pll-cfg', 'forcepllcfg', type=click.Path(exists=True), help=
'Manually specify clock config file' )
184 Perform a pll configuration
187 - pll and pll configuration
190 echo(
'PLL configuration ' + click.style(obj.mDevice.id(), fg=
'blue'))
192 lDevice = obj.mDevice
193 lBoardType = obj.mBoardType
194 lDesignType = obj.mDesignType
196 lIO = lDevice.getNode(
'io')
198 if lBoardType
in kLibrarySupportedBoards:
199 if forcepllcfg
is not None:
200 if clocksource
is not None:
201 secho(
"You specified both a clock source for automatic clock config file look-up, and an explicit clock config file. Explicit clock config file will take precedence.", fg=
'yellow')
203 lIO.configure_pll(forcepllcfg)
205 if clocksource
is None:
206 lClockSource = toolbox.get_default_clock_source(
207 lDesignType, lBoardType)
208 if lClockSource
is None:
209 secho(f
"Unable to match a default clock source for {kDesignNameMap[lDesignType]} on {kBoardNameMap[lBoardType]}\nReset failed!".format(), fg=
'red')
212 secho(f
"Default clock config selected for {kDesignNameMap[lDesignType]} on {kBoardNameMap[lBoardType]} is: {lClockSource}", fg=
'yellow')
214 lClockSource=ClockSource.__members__[clocksource]
215 lIO.configure_pll(lClockSource)
216 ctx.invoke(clkstatus)
218 secho(
"Board identifier {} not supported by timing library".format(lBoardType), fg=
'yellow')
223@io.command('clk-status')
226@click.option('-v', 'verbose', is_flag=True)
229 lDevice = obj.mDevice
230 lDesignType = obj.mDesignType
231 lIO = lDevice.getNode(
'io')
232 lBoardType = obj.mBoardType
238 if lBoardType
in kLibrarySupportedBoards:
239 echo(lIO.get_pll_status())
241 secho(
"Board {} not supported by timing library".format(lBoardType), fg=
'yellow')
249@io.command('dac-setup')
250@click.argument('value', type=click.IntRange(0,0xffff))
255 lDevice = obj.mDevice
256 lBoardType = obj.mBoardType
257 lIO = lDevice.getNode(
'io')
259 if lBoardType == kBoardTLU:
260 lIO.configure_dac(0,value)
261 lIO.configure_dac(1,value)
262 secho(
"DAC1 and DAC2 set to " + hex(value), fg=
'cyan')
264 secho(
"DAC setup only supported for TLU")
268@io.command('sfp-status', short_help="Read SFP parameters.")
271@click.option('--sfp-id', 'sfp_id', required=False, type=click.IntRange(0, 8), help=
'SFP id to query.')
277 lDevice = obj.mDevice
278 lBoardType = obj.mBoardType
279 lCarrierType = obj.mCarrierType
280 lDesignType = obj.mDesignType
282 lIO = lDevice.getNode(
'io')
285 if lBoardType
in kLibrarySupportedBoards:
286 ctx.invoke(print_hardware_info)
287 if sfp_id
is not None:
288 echo(lIO.get_sfp_status(sfp_id))
290 if lBoardType
in [kBoardFMC, kBoardTLU, kBoardPC069]:
291 echo(lIO.get_sfp_status(0))
292 elif lBoardType
in [ kBoardPC059, kBoardFIB, kBoardMIB,
293 kBoardGIB, kBoardGIBV3 ]:
295 if lBoardType == kBoardPC059:
297 elif lBoardType == kBoardFIB:
299 elif lBoardType == kBoardMIB:
301 elif lBoardType == kBoardGIB:
303 elif lBoardType == kBoardGIBV3:
305 for i
in range(lSFPIDRange):
307 echo(lIO.get_sfp_status(i))
309 except Exception
as e:
310 if isinstance(e, RuntimeError)
and str(e) ==
" I2C bus: i2c error. Transfer finished but bus still busy I2CException on bus: i2c":
311 secho(f
"Bad SFP {i} found with exception:")
313 if lBoardType
in [kBoardGIB, kBoardGIBV3]:
314 secho(
"resetting i2c after failure\n", fg=
'yellow')
315 lDevice.getNode(
"io.csr.ctrl.i2c_sw_rst").write(0x0)
317 lDevice.getNode(
"io.csr.ctrl.i2c_sw_rst").write(0x1)
320 secho(f
"SFP {i} status gather failed with exception:", fg=
'red')
324 secho(f
"I don't know how many SFPs there are for board: {timing.common.definitions.BoardType(lBoardType)}\n", fg=
'red')
327 secho(
"Board {} not supported by timing library".format(lBoardType), fg=
'yellow')
332@io.command('switch-sfp-tx', short_help="Control sfp tx")
335@click.option('--sfp-id', 'sfp_id', required=False, type=click.IntRange(0, 8), help=
'SFP id to query.')
336@click.option('--on/--off', default=False, help='enable/disable tx; default: FALSE')
339 Toggle SFP tx disable reg (if supported)
342 lDevice = obj.mDevice
343 lBoardType = obj.mBoardType
344 lCarrierType = obj.mCarrierType
345 lDesignType = obj.mDesignType
347 lIO = lDevice.getNode(
'io')
349 if lBoardType
in kLibrarySupportedBoards:
350 ctx.invoke(print_hardware_info)
354 lIO.switch_sfp_tx(lSFP, on)
356 secho(
"Board {} not supported by timing library".format(lBoardType), fg=
'yellow')
361@io.command('switch-sfp-mux', short_help="Switch sfp mux")
362@click.argument('mux', type=int)
366 lDevice = obj.mDevice
367 lBoardType = obj.mBoardType
368 lIO = lDevice.getNode(
'io')
370 if lBoardType
in [kBoardPC059]:
372 echo(
"Setting sfp mux channel: {}".format(mux))
373 lIO.switch_sfp_mux_channel(mux)
376 raise RuntimeError(
'Board {} does not have a sfp mux!'.format(kBoardNameMap[lBoardType]))
380@io.command('print-hardware-info')
385 lDevice = obj.mDevice
386 lBoardType = obj.mBoardType
387 lIO = lDevice.getNode(
'io')
389 if lBoardType
in kLibrarySupportedBoards:
391 echo(lIO.get_hardware_info())
393 secho(
"Failed to retrieve hardware information! I2C issue? Initial board reset needed?", fg=
'yellow')
394 e = sys.exc_info()[0]
395 secho(
"Error: {}".format(e), fg=
'red')
397 secho(
"Board {} not supported by timing library".format(lBoardType), fg=
'yellow')
402@io.command('temp', short_help="Read board temperature")
406 lDevice = obj.mDevice
407 lBoardType = obj.mBoardType
408 lIO = lDevice.getNode(
'io')
410 if lBoardType
in [kBoardFIB]:
411 temp = lIO.read_board_temperature()
412 echo(f
"Board temp: {temp} [C]")
414 raise RuntimeError(
'Board {} does not have a temp reading!'.format(kBoardNameMap[lBoardType]))
status(ctx, obj, verbose)
sfpstatus(ctx, obj, sfp_id)
print_hardware_info(ctx, obj)
dacsetup(ctx, obj, value)
reset(ctx, obj, soft, clocksource, forcepllcfg)
switchsfptx(ctx, obj, sfp_id, on)
clkstatus(ctx, obj, verbose)
configure_pll(ctx, obj, clocksource, forcepllcfg)