-
Notifications
You must be signed in to change notification settings - Fork 0
/
astropix.py
501 lines (424 loc) · 19.1 KB
/
astropix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
"""
Central module of astropix. This incorporates all of the various modules from the original 'module' directory backend (now 'core')
The class methods of all the other modules/cores are inherited here.
Author: Autumn Bauman
Maintained by: Amanda Steinhebel, [email protected]
02/2023 Jihee Kim added disable_pixel definition
Manoj Jadhav incresed threshold up to 800 mV
05/2023 Jihee Kim made changes to v3
chip version, class name, and initial voltage
"""
# Needed modules. They all import their own suppourt libraries,
# and eventually there will be a list of which ones are needed to run
from typing import Dict
from core.spi import Spi
from core.nexysio import Nexysio
from core.decode import Decode
from core.injectionboard import Injectionboard
from core.voltageboard import Voltageboard
from core.asic import Asic
from bitstring import BitArray
from tqdm import tqdm
import pandas as pd
import regex as re
import time
import yaml
import os
# Logging stuff
import logging
from modules.setup_logger import logger
logger = logging.getLogger(__name__)
class astropix3:
# Init just opens the chip and gets the handle. After this runs
# asic_config also needs to be called to set it up. Seperating these
# allows for simpler specifying of values.
def __init__(self, clock_period_ns = 5, inject:int = None, offline:bool=False):
"""
Initalizes astropix object.
No required arguments
Optional:
clock_period_ns:int - period of main clock in ns
inject:bool - if set to True will enable injection for the whole array.
offline:bool - if True, do not try to interface with chip
"""
# _asic_start tracks if the inital configuration has been run on the ASIC yet.
# By not handeling this in the init it simplifies the function, making it simpler
# to put in custom configurations and allows for less writing to the chip,
# only doing it once at init or when settings need to be changed as opposed to
# each time a parameter is changed.
if offline:
logger.info("Creating object for offline analysis")
else:
self._asic_start = False
self.nexys = Nexysio()
self._wait_progress(2)
self.handle = self.nexys.autoopen()
# Ensure it is working
logger.info("Opened FPGA, testing...")
self._test_io()
logger.info("FPGA test successful.")
# Start putting the variables in for use down the line
if inject is None:
inject = (None, None)
self.injection_col = inject[1]
self.injection_row = inject[0]
self.sampleclock_period_ns = clock_period_ns
# Creates objects used later on
self.decode = Decode(clock_period_ns)
##################### YAML INTERACTIONS #########################
#reading done in core/asic.py
#writing done here
def write_conf_to_yaml(self, filename:str = None):
"""
Write ASIC config to yaml
:param chipversion: chip version
:param filename: Name of yml file in config folder
"""
dicttofile ={self.asic.chip:
{
"telescope": {"nchips": self.asic.num_chips},
"geometry": {"cols": self.asic.num_cols, "rows": self.asic.num_rows}
}
}
if self.asic.num_chips > 1:
for chip in range(self.asic.num_chips):
dicttofile[self.asic.chip][f'config_{chip}'] = self.asic.asic_config[f'config_{chip}']
else:
dicttofile[self.asic.chip]['config'] = self.asic.asic_config
with open(f"{filename}", "w", encoding="utf-8") as stream:
try:
yaml.dump(dicttofile, stream, default_flow_style=False, sort_keys=False)
except yaml.YAMLError as exc:
logger.error(exc)
##################### ASIC METHODS FOR USERS #########################
# Method to initalize the asic. This is taking the place of asic.py.
# All of the interfacing is handeled through asic_update
def asic_init(self, yaml:str = None, dac_setup: dict = None, bias_setup:dict = None, blankmask:bool = False, analog_col:int = None):
"""
self.asic_init() - initalize the asic configuration. Must be called first
Positional arguments: None
Optional:
dac_setup: dict - dictionary of values passed to the configuration. Only needs values diffent from defaults
bias_setup: dict - dict of values for the bias configuration Only needs key/vals for changes from default
blankmask: bool - Create a blank mask (everything disabled). Pixels can be enabled manually
analog_col: int - Sets a column to readout analog data from.
"""
# Now that the asic has been initalized we can go and make this true
self._asic_start = True
self.asic = Asic(self.handle, self.nexys)
# Get config values from YAML
#set chip version
self.asic.chipversion=3
#Define YAML path variables
pathdelim=os.path.sep #determine if Mac or Windows separators in path name
ymlpath=yaml
try:
self.asic.load_conf_from_yaml(self.asic.chipversion, ymlpath)
except Exception:
logger.error('Must pass a configuration file in the form of *.yml')
#Config stored in dictionary self.asic_config . This is used for configuration in asic_update.
#If any changes are made, make change to self.asic_config so that it is reflected on-chip when
# asic_update is called
#Override yaml if arguments were given in run script
self.update_asic_config(bias_setup, dac_setup)
# Set analog output
if (analog_col is not None) and (analog_col <= self.asic._num_cols):
logger.info(f"enabling analog output in column {analog_col}")
self.asic.enable_ampout_col(analog_col, inplace=False)
# Turns on injection if so desired
if self.injection_col is not None:
self.asic.enable_inj_col(self.injection_col, inplace=False)
self.asic.enable_inj_row(self.injection_row, inplace=False)
# Load config it to the chip
logger.info("LOADING TO ASIC...")
self.asic_update()
logger.info("ASIC SUCCESSFULLY CONFIGURED")
#Interface with asic.py
def enable_pixel(self, col: int, row: int, inplace:bool=True):
self.asic.enable_pixel(col, row, inplace)
def disable_pixel(self, col: int, row: int, inplace:bool=True):
self.asic.disable_pixel(col, row, inplace)
#Turn on injection of different pixel than the one used in _init_
def enable_injection(self, col:int, row:int, inplace:bool=True):
self.asic.enable_inj_col(col, inplace)
self.asic.enable_inj_row(row, inplace)
# The method to write data to the asic. Called whenever somthing is changed
# or after a group of changes are done. Taken straight from asic.py.
def asic_update(self):
self.nexys.chip_reset()
self.asic.asic_update()
# Methods to update the internal variables. Please don't do it manually
# This updates the dac config
def update_asic_config(self, bias_cfg:dict = None, dac_cfg:dict = None, analog_col:int=None):
"""
Updates and writes confgbits to asic
bias_cfg:dict - Updates the bias settings. Only needs key/value pairs which need updated
dac_cfg:dict - Updates DAC settings. Only needs key/value pairs which need updated
"""
if self._asic_start:
if bias_cfg is not None:
for key in bias_cfg:
self.asic.asic_config['biasconfig'][key][1]=bias_cfg[key]
if dac_cfg is not None:
for key in dac_cfg:
self.asic.asic_config['idacs'][key][1]=dac_cfg[key]
else:
logger.info("update_asic_config() got no arguments, nothing to do.")
return None
self.asic_update()
else: raise RuntimeError("Asic has not been initalized")
def enable_spi(self):
"""
Starts spi bus.
Takes no arguments, returns nothing
"""
self.nexys.spi_enable()
self.nexys.spi_reset()
# Set SPI clockdivider
# freq = 100 MHz/spi_clkdiv
self.nexys.spi_clkdiv = 255
self.nexys.send_routing_cmd()
logger.info("SPI ENABLED")
def close_connection(self):
"""
Terminates the spi bus.
Takes no arguments. No returns.
"""
self.nexys.close()
################## Voltageboard Methods ############################
# Here we intitalize the 8 DAC voltageboard in slot 4. dacvals are carried over from past
# scripts. Default from beam_test.py:
# Use this: (8, [0, 0, 1.1, 1, 0, 0, 1, 1.035])
def init_voltages(self, slot: int = 4, vcal:float = .989, vsupply: float = 2.7, vthreshold:float = None, dacvals: tuple[int, list[float]] = None):
"""
Configures the voltage board
No required parameters. No return.
slot:int = 4 - Position of voltage board
vcal:float = 0.908 - Calibration of the voltage rails
vsupply = 2.7 - Supply Voltage
vthreshold:float = None - ToT threshold value. Takes precedence over dacvals if set. UNITS: mV
dacvals:tuple[int, list[float] - vboard dac settings. Must be fully specified if set.
"""
# The default values to pass to the voltage dac. Last value in list is threshold voltage, default 100mV or 1.1
# Not in YAML
# From nicholas's beam_test.py:
# 3 = Vcasc2, 4=BL, 7=Vminuspix, 8=Thpix
default_vdac = (8, [1.1, 0, 1.1, 1, 0, 0, 1, 1.100])
# used to ensure this has been called in the right order:
self._voltages_exist = True
# Set dacvals
if dacvals is None:
dacvals = default_vdac
# dacvals takes precidence over vthreshold
if vthreshold is not None:
# Turns from mV to V with the 1V offset normally present
vthreshold = (vthreshold/1000) + 1
if vthreshold > 1.801 or vthreshold < 0:
logger.warning("Threshold voltage out of range of sensor!")
if vthreshold <= 0:
vthreshold = 1.100
logger.error("Threshold value too low, setting to default 100mV")
dacvals[1][-1] = vthreshold
# Create object
self.vboard = Voltageboard(self.handle, slot, dacvals)
# Set calibrated values
self.vboard.vcal = vcal
self.vboard.vsupply = vsupply
# Send config to the chip
self.vboard.update_vb()
# Here we have the stuff to run injection
# defaults for the arguments:
# position: 3
# dac_settings: (2, [0.4, 0.0])
# Settings from the orininal scripts
"""
inj.period = 100
inj.clkdiv = 400
inj.initdelay = 10000
inj.cycle = 0
inj.pulsesperset = 1
"""
# Setup Injections
def init_injection(self, slot: int = 3, inj_voltage:float = None, inj_period:int = 100, clkdiv:int = 300, initdelay: int = 100, cycle: float = 0, pulseperset: int = 1, dac_config:tuple[int, list[float]] = None):
"""
Configure injections
No required arguments. No returns.
Optional Arguments:
slot: int - Location of the injection module
inj_voltage: float - Injection Voltage. Range from 0 to 1.8. If dac_config is set inj_voltage will be overwritten
inj_period: int
clkdiv: int
initdelay: int
cycle: float
pulseperset: int
dac_config:tuple[int, list[float]]: injdac settings. Must be fully specified if set.
"""
# Default configuration for the dac
# 0.3 is (default) injection voltage
# 2 is slot number for inj board
default_injdac = (2, [0.3, 0.0])
# Some fault tolerance
try:
self._voltages_exist
except Exception:
raise RuntimeError("init_voltages must be called before init_injection!")
# Sets the dac_setup if it isn't specified
if dac_config is None:
dac_settings = default_injdac
else:
dac_settings = dac_config
# The dac_config takes presedence over a specified threshold.
if (inj_voltage is not None) and (dac_config is None):
# elifs check to ensure we are not injecting a negative value because we don't have that ability
if inj_voltage < 0:
raise ValueError("Cannot inject a negative voltage!")
elif inj_voltage > 1800:
logger.warning("Cannot inject more than 1800mV, will use defaults")
inj_voltage = 300 #Sets to 300 mV
inj_voltage = inj_voltage / 1000
dac_settings[1][0] = inj_voltage
# Create the object!
self.inj_volts = Voltageboard(self.handle, slot, dac_settings)
# set the parameters
self.inj_volts.vcal = self.vboard.vcal
self.inj_volts.vsupply = self.vboard.vsupply
self.inj_volts.update_vb()
# Now to configure the actual injection thing
self.injector = Injectionboard(self.handle)
# Now to configure it. above are the values from the original scripting.
self.injector.period = inj_period
self.injector.clkdiv = clkdiv
self.injector.initdelay = initdelay
self.injector.cycle = cycle
self.injector.pulsesperset = pulseperset
# These start and stop injecting voltage. Fairly simple.
def start_injection(self):
"""
Starts Injection.
Takes no arguments and no return
"""
self.injector.start()
logger.info("Began injection")
def stop_injection(self):
"""
Stops Injection.
Takes no arguments and no return
"""
self.injector.stop()
logger.info("Stopped injection")
########################### Input and Output #############################
# This method checks the chip to see if a hit has been logged
def hits_present(self):
"""
Looks at interrupt
Returns bool, True if present
"""
if (int.from_bytes(self.nexys.read_register(70),"big") == 0):
return True
else:
return False
def get_log_header(self):
"""
Returns header for use in a log file with all settings.
"""
#Get config dictionaries from yaml
digitalconfig = {}
for key in self.asic.asic_config['digitalconfig']:
digitalconfig[key]=self.asic.asic_config['digitalconfig'][key][1]
biasconfig = {}
for key in self.asic.asic_config['biasconfig']:
biasconfig[key]=self.asic.asic_config['biasconfig'][key][1]
dacconfig = {}
for key in self.asic.asic_config['idacs']:
dacconfig[key]=self.asic.asic_config['idacs'][key][1]
arrayconfig = {}
for key in self.asic.asic_config['recconfig']:
arrayconfig[key]=self.asic.asic_config['recconfig'][key][1]
# This is not a nice line, but its the most efficent way to get all the values in the same place.
return f"Voltageboard settings: {self.vboard.dacvalues}\n" + f"Digital: {digitalconfig}\n" +f"Biasblock: {biasconfig}\n" + f"DAC: {dacconfig}\n" + f"Receiver: {arrayconfig}\n"
############################ Decoder ##############################
# This function generates a list of the hits in the stream. Retuerns a bytearray
def get_readout(self, bufferlength:int = 20):
"""
Reads hit buffer.
bufferlength:int - length of buffer to write. Multiplied by 8 to give number of bytes
Returns bytearray
"""
self.nexys.write_spi_bytes(bufferlength)
readout = self.nexys.read_spi_fifo()
return readout
def decode_readout(self, readout:bytearray, i:int, printer: bool = True):
"""
Decodes readout
Required argument:
readout: Bytearray - readout from sensor, not the printed Hex values
i: int - Readout number
Optional:
printer: bool - Print decoded output to terminal
Returns dataframe
"""
list_hits = self.decode.hits_from_readoutstream(readout)
hit_list = []
for hit in list_hits:
# Generates the values from the bitstream
id = int(hit[0]) >> 3
payload = int(hit[0]) & 0b111
location = int(hit[1]) & 0b111111
col = 1 if (int(hit[1]) >> 7 ) & 1 else 0
timestamp = int(hit[2])
tot_msb = int(hit[3]) & 0b1111
tot_lsb = int(hit[4])
tot_total = (tot_msb << 8) + tot_lsb
wrong_id = 0 if (id) == 0 else '\x1b[0;31;40m{}\x1b[0m'.format(id)
wrong_payload = 4 if (payload) == 4 else'\x1b[0;31;40m{}\x1b[0m'.format(payload)
# will give terminal output if desiered
if printer:
print(
f"{i} Header: ChipId: {wrong_id}\tPayload: {wrong_payload}\t"
f"Location: {location}\tRow/Col: {'Col' if col else 'Row'}\t"
f"Timestamp: {timestamp}\t"
f"ToT: MSB: {tot_msb}\tLSB: {tot_lsb} Total: {tot_total} ({(tot_total * self.sampleclock_period_ns)/1000.0} us)"
)
# hits are sored in dictionary form
# Look into dataframe
hits = {
'readout': i,
'Chip ID': id,
'payload': payload,
'location': location,
'isCol': (True if col else False),
'timestamp': timestamp,
'tot_msb': tot_msb,
'tot_lsb': tot_lsb,
'tot_total': tot_total,
'tot_us': ((tot_total * self.sampleclock_period_ns)/1000.0),
'hittime': time.time()
}
hit_list.append(hits)
# Much simpler to convert to df in the return statement vs df.concat
return pd.DataFrame(hit_list)
# To be called when initalizing the asic, clears the FPGAs memory
def dump_fpga(self):
"""
Reads out hit buffer and disposes of the output.
Does not return or take arguments.
"""
readout = self.get_readout()
del readout
###################### INTERNAL METHODS ###########################
# Below here are internal methods used for constructing things and testing
# _test_io(): A function to read and write a register on the chip to see if
# everythign is working.
# It takes no arguments
def _test_io(self):
try: # Attempts to write to and read from a register
self.nexys.write_register(0x09, 0x55, True)
self.nexys.read_register(0x09)
self.nexys.spi_reset()
self.nexys.sr_readback_reset()
except Exception:
raise RuntimeError("Could not read or write from astropix!")
# progress bar
def _wait_progress(self, seconds:int):
for _ in tqdm(range(seconds), desc=f'Wait {seconds} s'):
time.sleep(1)