def main(config_name=None, controller_config=None, chip_key=None, pulse_dac=10, n_pulses=10, channels=range(0, 64, 1), runtime=0.01): print('configure thresholds') pulse_dac = int(pulse_dac) n_pulses = int(n_pulses) # create controller c = None if config_name is None: c = base_warm.main(controller_config, logger=True) else: if controller_config is None: c = load_config.main(config_name, logger=True) else: c = load_config.main(config_name, controller_config, logger=True) # set initial configuration print('channels', channels) print('pulse_dac', pulse_dac) print('n_pulses', n_pulses) chips_to_test = c.chips.keys() if not chip_key is None: chips_to_test = [chip_key] for chip_key in chips_to_test: c[chip_key].config.adc_hold_delay = 15 registers = [129] c.write_configuration(chip_key, registers) c.enable_testpulse(chip_key, channels) # verify for chip_key in chips_to_test: ok, diff = c.verify_configuration(chip_key) if not ok: print('config error', diff) else: print('config ok') # issue pulses base.flush_data(c, rate_limit=len(c.chips) * 64) for i in range(n_pulses): for chip_key in chips_to_test: c.logger.enable() c.issue_testpulse(chip_key, pulse_dac, read_time=runtime) c.logger.disable() print('pulse', i, chip_key, 'triggers', len(c.reads[-1])) c.enable_testpulse(chip_key, channels) base.flush_data(c, rate_limit=len(c.chips) * 64) return c
def main(controller_config=None, chip_key=None, threshold=128, runtime=60, channels=range(0, 64, 1)): print('rough leakage config') # create controller c = base_warm.main(controller_config, logger=True) chips_to_test = c.chips.keys() if not chip_key is None: chips_to_test = [chip_key] # set configuration print('threshold', threshold) print('channels', channels) for chip_key in chips_to_test: for channel in channels: c[chip_key].config.channel_mask[channel] = 0 c[chip_key].config.threshold_global = threshold # write configuration for chip_key in chips_to_test: registers = list(range(131, 139)) # channel mask c.write_configuration(chip_key, registers) c.write_configuration(chip_key, registers) registers = [64] # threshold c.write_configuration(chip_key, registers) c.write_configuration(chip_key, registers) ok, diff = c.verify_configuration(chip_key) if not ok: print('config error', diff) base.flush_data(c) print('run for', runtime, 'sec') c.logger.enable() c.run(runtime, 'collect data') c.logger.flush() print('packets read', len(c.reads[-1])) c.logger.disable() packet_channels, packet_keys = zip( *c.reads[-1].extract('channel_id', 'chip_key')) for chip_key in chips_to_test: for channel in channels: print( 'chip', chip_key, 'channel', channel, 'rate', len([ ch for ch, key in zip(packet_channels, packet_keys) if ch == channel and key == chip_key ]) / runtime, 'Hz') return c
def main(controller_config=None, periodic_trigger_cycles=100000, runtime=10, *args, **kwargs): # create controller c = base_warm.main(controller_config_file=controller_config, logger=True) # set configuration for chip_key, chip in c.chips.items(): chip.config.periodic_trigger_mask = [0] * 64 chip.config.channel_mask = [0] * 64 chip.config.periodic_trigger_cycles = periodic_trigger_cycles chip.config.enable_periodic_trigger = 1 chip.config.enable_rolling_periodic_trigger = 1 chip.config.enable_periodic_reset = 1 chip.config.enable_hit_veto = 0 # write configuration registers = list(range(155, 163)) # periodic trigger mask c.write_configuration(chip_key, registers) c.write_configuration(chip_key, registers) registers = list(range(131, 139)) # channel mask c.write_configuration(chip_key, registers) c.write_configuration(chip_key, registers) registers = list(range(166, 170)) # periodic trigger cycles c.write_configuration(chip_key, registers) c.write_configuration(chip_key, registers) registers = [128] # periodic trigger, reset, rolling trigger, hit veto c.write_configuration(chip_key, registers) c.write_configuration(chip_key, registers) for chip_key in c.chips: ok, diff = c.verify_configuration(chip_key) if not ok: print('config error', diff) base.flush_data( c, rate_limit=(1 + 1 / (periodic_trigger_cycles * 1e-7) * len(c.chips))) c.logger.enable() c.run(runtime, 'collect data') c.logger.flush() print('packets read', len(c.reads[-1])) c.logger.disable() return c
def main(controller_config=None, chip_key=None, channels=_default_channels, disabled_channels={None:_default_disabled_channels}.copy(), runtime=_default_runtime, target_rate=_default_target_rate, base_config=_default_config, disable_threshold=_default_disable_threshold, reset_threshold=_default_reset_threshold): print('START AUTOCONFIG') # create controller c = base.main(controller_config=controller_config) print() print('base config',base_config) print('enabled channels',channels) print('disabled channels',disabled_channels) print('target rate',target_rate) print('disable threshold',disable_threshold) print('runtime',runtime) test_chip_keys = [] for io_group in c.network: for io_channel in c.network[io_group]: test_chip_ids = [chip_id for chip_id,deg in c.network[io_group][io_channel]['miso_us'].out_degree() if deg == 0] # get network leaves test_chip_keys += [larpix.Key(io_group,io_channel,chip_id) for chip_id in test_chip_ids] print('test packets will be sent to',test_chip_keys) read_config_spec = [(key,0) for key in test_chip_keys] chips_to_configure = c.chips if not chip_key is None: chips_to_configure = [chip_key] _default_ignore = defaultdict(list) for chip_key in chips_to_configure: if None in disabled_channels: _default_ignore[chip_key] += disabled_channels[None] if "All" in disabled_channels: _default_ignore[chip_key] += disabled_channels["All"] if chip_key in disabled_channels: _default_ignore[chip_key] += disabled_channels[chip_key] channels_to_configure = defaultdict(list, [(chip_key,channels.copy()) for chip_key in chips_to_configure]) print() for chip_key in chips_to_configure: c.io.double_send_packets = True print('set config',chip_key) c[chip_key].config.load(_default_config) for channel in channels: if channel not in _default_ignore[chip_key]: c[chip_key].config.channel_mask[channel] = 0 for channel in _default_ignore[chip_key]: c[chip_key].config.csa_enable[channel] = 0 # write configuration print('verify',chip_key) c.write_configuration(chip_key) base.flush_data(c) #ok, diff = c.verify_configuration(chip_key, timeout=0.1) ok,diff = c.enforce_configuration(chip_key,timeout=0.01,n=10,n_verify=10) if not ok: print('config error',diff[chip_key]) base.flush_data(c) c.io.double_send_packets = True # verify no high rate channels repeat = True while repeat: print('check rate',chip_key,end=' ') repeat = False base.flush_data(c) c.multi_read_configuration(read_config_spec,timeout=runtime/10,message='rate check') triggered_channels = c.reads[-1].extract('chip_key','channel_id',chip_key=chip_key,packet_type=0) print('(total rate={}Hz)'.format(len(triggered_channels)/(runtime/10))) rates = dict([(channel, triggered_channels.count(list(channel))/(runtime/10)) for channel in set(map(tuple,triggered_channels))]) if rates: max_rate = max(rates.values()) for channel,rate in rates.items(): chip_key,channel = channel if rate > target_rate and channel in channels_to_configure[chip_key] \ and chip_key in c.chips and rate == max_rate: print('disable',chip_key,channel,'rate was',rate,'Hz') c.disable(chip_key,[channel]) c[chip_key].config.csa_enable[channel] = 0 c.write_configuration(chip_key,'csa_enable') channels_to_configure[chip_key].remove(channel) repeat = True if rate > reset_threshold: c = _reset_and_reload(c,controller_config) if repeat: c.write_configuration(chip_key) c.reads = [] # walk down global threshold print() print('reducing global threshold') repeat = defaultdict(lambda : True, [(key, True) for key in chips_to_configure]) target_reached = False while any(repeat.values()) or not len(repeat.values()): # check rate print('check rate',end=' ') base.flush_data(c) c.multi_read_configuration(read_config_spec,timeout=runtime,message='rate check') triggered_channels = c.reads[-1].extract('chip_key','channel_id',packet_type=0) print('(total rate={}Hz)'.format(len(triggered_channels)/runtime)) for chip_key, channel in set(map(tuple,triggered_channels)): rate = triggered_channels.count([chip_key,channel])/runtime if rate > target_rate and channel in channels_to_configure[chip_key] \ and repeat[chip_key] and chip_key in c.chips: print('reached target',chip_key,channel,'rate was',rate,'Hz') target_reached = True repeat[chip_key] = False c[chip_key].config.threshold_global = min(c[chip_key].config.threshold_global+1,255) print('\tthreshold',c[chip_key].config.threshold_global) c.write_configuration(chip_key,'threshold_global') c.write_configuration(chip_key,'threshold_global') if rate > reset_threshold: c = _reset_and_reload(c,controller_config) # walk down global threshold if not target_reached: print('reducing thresholds') for chip_key in chips_to_configure: if chip_key in c.chips: if repeat[chip_key] and c[chip_key].config.threshold_global > 0: c[chip_key].config.threshold_global -= 1 repeat[chip_key] = True elif c[chip_key].config.threshold_global == 0: repeat[chip_key] = False c.write_configuration(chip_key,'threshold_global') c.write_configuration(chip_key,'threshold_global') target_reached = False c.reads = [] print('initial global thresholds:',dict([(chip_key,c[chip_key].config.threshold_global) for chip_key in chips_to_configure if chip_key in c.chips])) print() print('increasing global threshold') above_target = defaultdict(lambda : False) for _ in range(10): # check rate print('check rate',end=' ') base.flush_data(c) c.multi_read_configuration(read_config_spec,timeout=runtime,message='rate check') triggered_channels = c.reads[-1].extract('chip_key','channel_id',packet_type=0) print('(total rate={}Hz)'.format(len(triggered_channels)/runtime)) for chip_key, channel in set(map(tuple,triggered_channels)): rate = triggered_channels.count([chip_key,channel])/runtime if rate > target_rate and channel in channels_to_configure[chip_key] \ and not above_target[chip_key] and chip_key in c.chips: print('increasing threshold',chip_key,channel,'rate was',rate,'Hz') above_target[chip_key] = True c[chip_key].config.threshold_global = min(c[chip_key].config.threshold_global+1,255) print('\tthreshold',c[chip_key].config.threshold_global) c.write_configuration(chip_key,'threshold_global') c.write_configuration(chip_key,'threshold_global') if rate > reset_threshold: c = _reset_and_reload(c,controller_config) # continue once rate is below target if not above_target or not any(above_target.values()): break else: above_target = defaultdict(lambda : False) c.reads = [] print('final global thresholds:',dict([(chip_key,c[chip_key].config.threshold_global) for chip_key in chips_to_configure if chip_key in c.chips])) print() print('decreasing pixel trim') repeat = defaultdict(lambda : True, [((key, channel),True) for key,channels in channels_to_configure.items() for channel in channels]) target_reached = False while any(repeat.values()) or not len(repeat.values()): # check rate print('check rate',end=' ') base.flush_data(c) c.multi_read_configuration(read_config_spec,timeout=runtime,message='rate check') triggered_channels = c.reads[-1].extract('chip_key','channel_id',packet_type=0) print('(total rate={}Hz)'.format(len(triggered_channels)/runtime)) for chip_key, channel in set(map(tuple,triggered_channels)): rate = triggered_channels.count([chip_key,channel])/runtime if rate > target_rate and channel in channels_to_configure[chip_key] \ and chip_key in c.chips: print('reached target',chip_key,channel,'rate was',rate,'Hz') if repeat[(chip_key, channel)]: target_reached = True if c[chip_key].config.pixel_trim_dac[channel] == 31 and rate > disable_threshold: c.disable(chip_key,[channel]) c[chip_key].config.csa_enable[channel] = 0 c.write_configuration(chip_key,'csa_enable') print('disable threshold reached') repeat[(chip_key,channel)] = False c[chip_key].config.pixel_trim_dac[channel] = min(c[chip_key].config.pixel_trim_dac[channel]+1,31) print('\ttrim',c[chip_key].config.pixel_trim_dac[channel]) c.write_configuration(chip_key,'pixel_trim_dac') c.write_configuration(chip_key,'pixel_trim_dac') if rate > reset_threshold: c = _reset_and_reload(c,controller_config) # walk down trims if not target_reached: print('reducing trims') for chip_key, channels in channels_to_configure.items(): if chip_key in c.chips: for channel in channels: if repeat[(chip_key,channel)] and c[chip_key].config.pixel_trim_dac[channel] > 0: c[chip_key].config.pixel_trim_dac[channel] -= 1 elif c[chip_key].config.pixel_trim_dac[channel] == 0: repeat[(chip_key,channel)] = False c.write_configuration(chip_key,'pixel_trim_dac') c.write_configuration(chip_key,'pixel_trim_dac') target_reached = False c.reads = [] print('initial pixel trims:') for chip_key in chips_to_configure: if chip_key in c.chips: print('\t',chip_key,c[chip_key].config.pixel_trim_dac) print() print('increasing pixel trim') above_target = defaultdict(lambda : False) for _ in range(10): # check rate print('check rate',end=' ') base.flush_data(c) c.multi_read_configuration(read_config_spec,timeout=runtime,message='rate check') triggered_channels = c.reads[-1].extract('chip_key','channel_id',packet_type=0) print('(total rate={}Hz)'.format(len(triggered_channels)/runtime)) for chip_key, channel in set(map(tuple,triggered_channels)): rate = triggered_channels.count([chip_key,channel])/runtime if rate > target_rate and channel in channels_to_configure[chip_key] \ and not above_target[(chip_key,channel)] and chip_key in c.chips: print('increasing pixel trim',chip_key,channel,'rate was',rate,'Hz') above_target[(chip_key,channel)] = True if c[chip_key].config.pixel_trim_dac[channel] == 31 and rate > disable_threshold: c.disable(chip_key,[channel]) c[chip_key].config.csa_enable[channel] = 0 c.write_configuration(chip_key,'csa_enable') print('disable threshold reached') c[chip_key].config.pixel_trim_dac[channel] = min(c[chip_key].config.pixel_trim_dac[channel]+1,31) print('\ttrim',c[chip_key].config.pixel_trim_dac[channel]) c.write_configuration(chip_key,'pixel_trim_dac') c.write_configuration(chip_key,'pixel_trim_dac') if rate > reset_threshold: c = _reset_and_reload(c,controller_config) # continue once rate is below target if not above_target or not any(above_target.values()): break else: above_target = defaultdict(lambda : False) c.reads = [] print('final pixel trims:') for chip_key in chips_to_configure: if chip_key in c.chips: print('\t',chip_key,c[chip_key].config.pixel_trim_dac) print() print('saving configs...') for chip_key in chips_to_configure: if chip_key in c.chips: # save config time_format = time.strftime('%Y_%m_%d_%H_%M_%S_%Z') config_filename = 'config-'+str(chip_key)+'-'+time_format+'.json' c[chip_key].config.write(config_filename, force=True) print('\t',chip_key,'saved to',config_filename) print('final configured rate: ',end='') base.flush_data(c) c.run(runtime,'final rate') n_packets = len(c.reads[-1].extract('io_group',packet_type=0)) print('{:0.2f}Hz ({:0.2f}Hz/channel)'.format(n_packets/runtime,n_packets/runtime/sum([len(ch) for ch in channels_to_configure.values()]))) print('END AUTOCONFIG') return c
def main(controller_config=None, chip_key=None, threshold_global_start=30, channels=range(0, 64, 1), pixel_trim_dac_start=31, runtime=1, target_rate=2): print('configure thresholds') # create controller c = base_warm.main(controller_config_file=controller_config) # set initial configuration print('channels', channels) print('threshold_global_start', threshold_global_start) print('pixel_trim_dac_start', pixel_trim_dac_start) chips_to_configure = c.chips if not chip_key is None: chips_to_configure = [chip_key] for chip_key in chips_to_configure: print('chip', chip_key) for channel in channels: c[chip_key].config.channel_mask[channel] = 0 c[chip_key].config.pixel_trim_dac[channel] = pixel_trim_dac_start c[chip_key].config.threshold_global = threshold_global_start c[chip_key].config.enable_periodic_reset = 1 # write configuration registers = list(range(131, 139)) # channel mask c.write_configuration(chip_key, registers) c.write_configuration(chip_key, registers) registers = [128] # periodic reset c.write_configuration(chip_key, registers) c.write_configuration(chip_key, registers) registers = list(range(0, 65)) # trim and threshold c.write_configuration(chip_key, registers) c.write_configuration(chip_key, registers) # verify ok, diff = c.verify_configuration(chip_key) if not ok: print('config error', diff) # walk down global threshold for threshold in range(threshold_global_start, 0, -1): # update threshold c[chip_key].config.threshold_global = threshold registers = [64] c.write_configuration(chip_key, registers) c.write_configuration(chip_key, registers) print('threshold_global', threshold) # check rate base.flush_data(c) c.run(runtime, 'rate check') rate = len(c.reads[-1].extract('channel_id', chip_key=chip_key)) / runtime print('rate', rate) # stop if rate is above target if rate > target_rate: # back off by 1 c[chip_key].config.threshold_global = min(threshold + 1, 255) registers = [64] c.write_configuration(chip_key, registers) c.write_configuration(chip_key, registers) break c.run(1, 'flush stale data') # walk down pixel trims channels_to_set = set(channels) trim = pixel_trim_dac_start while channels_to_set != set(): # update channel trims channels_to_set_copy = copy(channels_to_set) for channel in channels_to_set_copy: if not c[chip_key].config.pixel_trim_dac[channel] == 0: c[chip_key].config.pixel_trim_dac[channel] = trim else: # remove channels that have bottomed out channels_to_set.remove(channel) registers = list(range(0, 64)) c.write_configuration(chip_key, registers) c.write_configuration(chip_key, registers) print('pixel_trim_dac', trim) print('channels_to_set', channels_to_set) # check rate base.flush_data(c) c.run(runtime, 'rate check') rate = len(c.reads[-1]) / runtime print('rate', rate) # back off channels that have rates above target channel_triggers = c.reads[-1].extract('channel_id', chip_key=chip_key) rates = dict([ (channel, len([ch for ch in channel_triggers if ch == channel]) / runtime) for channel in channels_to_set ]) print(rates, 'rates') channels_to_set_copy = copy(channels_to_set) for channel in channels_to_set_copy: if rates[channel] > target_rate: # back off by 1 c[chip_key].config.pixel_trim_dac[channel] = min( trim + 1, 31) if channel in channels_to_set: channels_to_set.remove(channel) registers = list(range(0, 64)) c.write_configuration(chip_key, registers) c.write_configuration(chip_key, registers) # walk down trim if no channels above rate if not any([rate > target_rate for rate in rates.values()]): trim -= 1 base.flush_data(c) # save config time_format = '%Y_%m_%d_%H_%M_%S_%Z' config_filename = 'config-' + str(chip_key) + '-' + time.strftime( time_format) + '.json' c[chip_key].config.write(config_filename, force=True) print('saved to', config_filename) c.reads = [] # reset reads array return c