|
| 1 | +import os |
| 2 | +import xrfclk |
| 3 | + |
| 4 | +def _get_lmclk_devices(): |
| 5 | + """Populate LMK and LMX devices. |
| 6 | + """ |
| 7 | + |
| 8 | + # Search for devices if none exist |
| 9 | + if xrfclk.lmk_devices == [] and xrfclk.lmx_devices == []: |
| 10 | + xrfclk.xrfclk._find_devices() |
| 11 | + |
| 12 | +def _get_custom_lmclks(loc): |
| 13 | + """Search for LMK and LMX clock files with a given address. |
| 14 | + """ |
| 15 | + |
| 16 | + # Check type and value |
| 17 | + if not isinstance(loc, str): |
| 18 | + raise TypeError('Address location must be a string.') |
| 19 | + if not os.path.isdir(loc): |
| 20 | + raise ValueError('Address location does not exist.') |
| 21 | + |
| 22 | + # Variables |
| 23 | + lmk_loc = '' |
| 24 | + lmx_loc = '' |
| 25 | + lmclk_loc = '' |
| 26 | + |
| 27 | + # Walk through directory and find .txt files |
| 28 | + for root, dirs, files in os.walk(loc): |
| 29 | + for d in dirs: |
| 30 | + for lmk in xrfclk.lmk_devices: |
| 31 | + if d == lmk['compatible']: |
| 32 | + lmclk_loc = os.path.join(root, d) |
| 33 | + break |
| 34 | + |
| 35 | + # Check variable is empty |
| 36 | + if lmclk_loc == '': |
| 37 | + raise RuntimeError('Could not find lmclk files.') |
| 38 | + |
| 39 | + # Use root directory to extract LMK and LMX locs |
| 40 | + for file in os.listdir(lmclk_loc): |
| 41 | + if file.endswith('.txt'): |
| 42 | + if 'LMK' in file: |
| 43 | + lmk_loc = os.path.join(lmclk_loc, file) |
| 44 | + elif 'LMX' in file: |
| 45 | + lmx_loc = os.path.join(lmclk_loc, file) |
| 46 | + |
| 47 | + # Check variables are empty |
| 48 | + if lmk_loc == '' or lmx_loc == '': |
| 49 | + raise RuntimeError('Could not find lmclk files.') |
| 50 | + |
| 51 | + return lmk_loc, lmx_loc |
| 52 | + |
| 53 | +def _get_custom_lmclk_props(lmk_loc, lmx_loc): |
| 54 | + """Obtain the properties for LMK and LMX clocks using |
| 55 | + a set of address locations for clock files. |
| 56 | + """ |
| 57 | + |
| 58 | + # Check type, value, and file format |
| 59 | + if not isinstance(lmk_loc, str) or not isinstance(lmx_loc, str): |
| 60 | + raise TypeError('TICS files must be a string.') |
| 61 | + if not os.path.isfile(lmk_loc) or not os.path.isfile(lmx_loc): |
| 62 | + raise ValueError('TICS file paths do not exist.') |
| 63 | + if not lmk_loc[-4:] == '.txt' or not lmx_loc[-4:] == '.txt': |
| 64 | + raise ValueError('TICS files must be .txt files.') |
| 65 | + |
| 66 | + # Strip file name from arguments |
| 67 | + lmk_name = lmk_loc.split('/')[-1] |
| 68 | + lmx_name = lmx_loc.split('/')[-1] |
| 69 | + |
| 70 | + # Split file name into LMK and LMX chip and freq (strip .txt) |
| 71 | + lmk_split = lmk_name.strip('.txt').split('_') |
| 72 | + lmx_split = lmx_name.strip('.txt').split('_') |
| 73 | + |
| 74 | + # Obtain LMK and LMX chip and freq components and |
| 75 | + # check for errors in format |
| 76 | + if len(lmk_split) == 2 and len(lmx_split) == 2: |
| 77 | + lmk_chip, lmk_freq = lmk_split |
| 78 | + lmx_chip, lmx_freq = lmx_split |
| 79 | + else: |
| 80 | + raise ValueError('TICS file names have incorrect format.') |
| 81 | + |
| 82 | + # Open files and parse registers |
| 83 | + with open(lmk_loc, 'r') as file: |
| 84 | + reg = [line.rstrip("\n") for line in file] |
| 85 | + lmk_reg = [int(r.split('\t')[-1], 16) for r in reg] |
| 86 | + with open(lmx_loc, 'r') as file: |
| 87 | + reg = [line.rstrip("\n") for line in file] |
| 88 | + lmx_reg = [int(r.split('\t')[-1], 16) for r in reg] |
| 89 | + |
| 90 | + # Populate TICS file dictionary |
| 91 | + clk_props = { |
| 92 | + 'lmk' : { |
| 93 | + 'file' : lmk_name, |
| 94 | + 'loc' : lmk_loc, |
| 95 | + 'chip' : lmk_chip, |
| 96 | + 'freq' : lmk_freq, |
| 97 | + 'reg' : lmk_reg |
| 98 | + }, |
| 99 | + 'lmx' : { |
| 100 | + 'file' : lmx_name, |
| 101 | + 'loc' : lmx_loc, |
| 102 | + 'chip' : lmx_chip, |
| 103 | + 'freq' : lmx_freq, |
| 104 | + 'reg' : lmx_reg |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return clk_props |
| 109 | + |
| 110 | +def _program_custom_lmclks(clk_props): |
| 111 | + """Program the LMK and LMX clocks using clock properties. |
| 112 | + """ |
| 113 | + |
| 114 | + # Program each device |
| 115 | + for lmk in xrfclk.lmk_devices: |
| 116 | + xrfclk.xrfclk._write_LMK_regs(clk_props['lmk']['reg'], lmk) |
| 117 | + for lmx in xrfclk.lmx_devices: |
| 118 | + xrfclk.xrfclk._write_LMX_regs(clk_props['lmx']['reg'], lmx) |
| 119 | + |
| 120 | +def set_custom_lmclks(): |
| 121 | + """Populate LMK and LMX clocks. Search for clock files. |
| 122 | + Obtain the properties of the clock files. Program the |
| 123 | + LMK and LMX clocks with the properties of the files. |
| 124 | + """ |
| 125 | + |
| 126 | + # Ensure LMK and LMX devices are known |
| 127 | + _get_lmclk_devices() |
| 128 | + |
| 129 | + # Get custom ref clock locs |
| 130 | + cwd = os.path.dirname(os.path.realpath(__file__)) |
| 131 | + lmk_loc, lmx_loc = _get_custom_lmclks(cwd) |
| 132 | + |
| 133 | + # Get custom ref clock props |
| 134 | + clk_props = _get_custom_lmclk_props(lmk_loc, lmx_loc) |
| 135 | + |
| 136 | + # Program custom ref clocks |
| 137 | + _program_custom_lmclks(clk_props) |
| 138 | + |
0 commit comments