diff --git a/econt_sw/testing/PLL_frequency.py b/econt_sw/testing/PLL_frequency.py index 2ac8d63..af8fc93 100644 --- a/econt_sw/testing/PLL_frequency.py +++ b/econt_sw/testing/PLL_frequency.py @@ -1,11 +1,27 @@ import mmap -import numpy +import numpy as np import os import logging import bitstruct +from i2c import I2C_Client +from time import sleep +import uhal +from PLL_class import pll_freq_setup +import argparse +import json +import sys +import time +start = time.time() +fname = "none" +if("--fname" in sys.argv): + fname= sys.argv[sys.argv.index("--fname") + 1] + +i2cClient = I2C_Client() +pllObj = pll_freq_setup() label_to_uio = {} label_to_size = {} + for uio in os.listdir('/sys/class/uio'): try: with open(f'/sys/class/uio/{uio}/device/of_node/instance_id') as label_file: @@ -21,40 +37,65 @@ def uio_open(label): with open(f'/dev/{label_to_uio[label]}', 'r+b') as uio_dev_file: - return numpy.frombuffer(mmap.mmap(uio_dev_file.fileno(), label_to_size[label], access=mmap.ACCESS_WRITE, offset=0), numpy.uint32) + return np.frombuffer(mmap.mmap(uio_dev_file.fileno(), label_to_size[label], access=mmap.ACCESS_WRITE, offset=0), np.uint32) clk = uio_open('FC-FC-clk-generator') -FPFDmin = 10 -FPFDmax = 450 -FVCOmin = 800 -FVCOmax = 1600 - -Ms = numpy.arange(2, 128.1, 0.125) -Os = numpy.concatenate([[1], numpy.arange(2, 128.1, 0.125)]) -Ds = numpy.arange(1, 106.0001) - -fIN = 100 - -fs = {} -for D in Ds: - fFB = fIN / D - if fFB >= FPFDmin and fFB <= FPFDmax: - for M in Ms: - fVCO = fFB * M - if fVCO >= FVCOmin and fVCO <= FVCOmax: - for O in Os: - fOUT = fVCO / O - if fOUT not in fs: - fs[fOUT] = (D, M, O) - -allfs = numpy.sort(numpy.array(list(fs.keys()))) - -f = 320 -k = numpy.searchsorted(allfs, f) -D, M, O = fs[allfs[k]] -print(allfs[k], k, D, M, O, fIN/D, fIN*M/D, fIN*M/(D*O)) - -clk[0x80] = int.from_bytes(bitstruct.pack('u6u10u8u8', 1, int((M%1)*1000), int(M), int(D)), 'big') -clk[0x82] = int.from_bytes(bitstruct.pack('u14u10u8', 1, int((O%1)*1000), int(O)), 'big') -clk[0x97] = 3 \ No newline at end of file +## +xml_path = "/opt/cms-hgcal-firmware/hgc-test-systems/active/uHAL_xml" +connections_file = f"file://{xml_path}/connections.xml" +man = uhal.ConnectionManager(connections_file) +dev = man.getDevice("TOP") +## +allowedCapSelectVals=np.array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, + 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31, 56, 57, + 58, 59, 60, 61, 62, 63, 120, 121, 122, 123, 124, 125, 126, + 127, 248, 249, 250, 251, 252, 253, 254, 255, 504, 505, 506, 507, + 508, 509, 510, 511]) +## +frequencies = np.arange(35, 44, (1/8)) +frequencies = frequencies*8 +b = [] +frequency_locked = [] +i2cClient.call(args_yaml="../configs/startup.yaml",args_i2c='ASIC',args_write=True) +for i in range(len(frequencies)): + f_actual, D, M, O, fVCO = pllObj.freq_info(frequencies[i]) + print(f_actual, D, M, O, fVCO) + ## + dev.getNode("clk_wiz.divclk_divide").write(int(D)) + dev.getNode("clk_wiz.clkFBout_mult").write(int(M)) + dev.getNode("clk_wiz.clkFBout_frac").write(int((M%1)*1000)) + dev.getNode("clk_wiz.clkout0_divide").write(int(O)) + dev.getNode("clk_wiz.clkout0_frac").write(int((O%1)*1000)) + dev.dispatch() + dev.getNode("clk_wiz.default").write(1) + dev.getNode("clk_wiz.load").write(1) + dev.dispatch() + ### end of added code ### + ## hard reset from i2c fudge + dev.getNode("I2C-I2C-fudge-0.resets.ECONT_ASIC_SOFT_RESETB").write(0) + dev.dispatch() + dev.getNode("I2C-I2C-fudge-0.resets.ECONT_ASIC_SOFT_RESETB").write(1) + dev.dispatch() + ## + sleep(0.01) + ##future code to change the cap select setting + a = [] + for j in allowedCapSelectVals: + i2cClient.call('PLL_*CapSelect',args_value=str(j)) + sleep(0.05) + status = i2cClient.call(args_name='PLL_lfLocked') + pll_locked = status['ASIC']['RO']['PLL_ALL']['pll_read_bytes_2to0_lfLocked'] + a.append(pll_locked) + #if pll_locked == 1: + # frequency_locked.append(frequencies[i]) + ## + print(a) + b.append(a) +#min_freq = frequency_locked[0] +#max_freq = frequency_locked[-1] +#freq_dict = {"min_freq": min_freq, "max_freq": max_freq} +with open(f'/home/HGCAL_dev/acampbell/econt_sw/econt_sw/testing/plldata/{fname}.csv','w') as filehandle: + json.dump(b, filehandle) +elapsed = time.time() - start +print(elapsed) diff --git a/econt_sw/testing/i2c.py b/econt_sw/testing/i2c.py index ccbfbe5..dbb95db 100644 --- a/econt_sw/testing/i2c.py +++ b/econt_sw/testing/i2c.py @@ -74,7 +74,7 @@ def call(self, if args_name: import json - with open("zmq_i2c/reg_maps/ECON_I2C_dict.json") as f: + with open("../zmq_i2c/reg_maps/ECON_I2C_dict.json") as f: names_to_register = json.load(f) names = args_name.split(',') p = re.compile('^(\w*)\[(\d*)-(\d*)\](\w*)$') #match to find a range of channels @@ -187,7 +187,7 @@ def call(self, ### if '--name' argument is also supplied, it pattern matches to the name, printing only applicable registers if args.listRegisters: import json - with open("zmq_i2c/reg_maps/ECON_I2C_dict.json") as f: + with open("../zmq_i2c/reg_maps/ECON_I2C_dict.json") as f: names_to_register = json.load(f) if args.name: p2 = re.compile('^(\w*)\*(\w*)$')