|
| 1 | +"""Defines the NI RRAM controller class""" |
| 2 | +import json |
| 3 | +import time |
| 4 | +import warnings |
| 5 | +import nidaqmx |
| 6 | +import nidcpower |
| 7 | +import nifgen |
| 8 | +import numpy as np |
| 9 | +from ni_hsdio import NIHSDIO |
| 10 | + |
| 11 | +warnings.filterwarnings("error") |
| 12 | + |
| 13 | +def accurate_delay(delay): |
| 14 | + """Function to provide accurate time delay""" |
| 15 | + _ = time.perf_counter() + delay |
| 16 | + while time.perf_counter() < _: |
| 17 | + pass |
| 18 | + |
| 19 | + |
| 20 | +class NIRRAMException(Exception): |
| 21 | + """Exception produced by the NIRRAM class""" |
| 22 | + def __init__(self, msg): |
| 23 | + super().__init__(f"NIRRAM: {msg}") |
| 24 | + |
| 25 | + |
| 26 | +class NIRRAM: |
| 27 | + """The NI RRAM controller class that controls the instrument drivers.""" |
| 28 | + def __init__(self, chip, settings="settings.json"): |
| 29 | + # If settings is a string, load as JSON file |
| 30 | + if isinstance(settings, str): |
| 31 | + with open(settings) as settings_file: |
| 32 | + settings = json.load(settings_file) |
| 33 | + |
| 34 | + # Ensure settings is a dict |
| 35 | + if not isinstance(settings, dict): |
| 36 | + raise NIRRAMException(f"Settings should be a dict, got {repr(settings)}.") |
| 37 | + |
| 38 | + # TODO: initialize logger |
| 39 | + |
| 40 | + # Store/initialize parameters |
| 41 | + self.chip = chip |
| 42 | + self.settings = settings |
| 43 | + self.addr = 0 |
| 44 | + self.prof = {"READs": 0, "SETs": 0, "RESETs": 0} |
| 45 | + |
| 46 | + # Initialize NI-HSDIO driver |
| 47 | + self.hsdio = NIHSDIO(**settings["HSDIO"]) |
| 48 | + |
| 49 | + # Initialize NI-DAQmx driver for READ voltage |
| 50 | + self.read_chan = nidaqmx.Task() |
| 51 | + self.read_chan.ai_channels.add_ai_voltage_chan(settings["DAQmx"]["chanMap"]["read_ai"]) |
| 52 | + self.read_chan.timing.cfg_samp_clk_timing(settings["READ"]["read_clk_rate"]) |
| 53 | + |
| 54 | + # Initialize NI-DAQmx driver for WL voltages |
| 55 | + self.wl_ext_chans = [] |
| 56 | + for chan in settings["DAQmx"]["chanMap"]["wl_ext"]: |
| 57 | + task = nidaqmx.Task() |
| 58 | + task.ao_channels.add_ao_voltage_chan(chan) |
| 59 | + task.timing.cfg_samp_clk_timing(settings["samp_clk_rate"]) |
| 60 | + task.start() |
| 61 | + self.wl_ext_chans.append(task) |
| 62 | + |
| 63 | + # Initialize NI-DCPower driver for BL voltages |
| 64 | + self.bl_ext_chans = [] |
| 65 | + for chan in settings["DCPower"]["chans"]: |
| 66 | + sess = nidcpower.Session(settings["DCPower"]["deviceID"], chan) |
| 67 | + sess.voltage_level = 0 |
| 68 | + sess.commit() |
| 69 | + sess.initiate() |
| 70 | + self.bl_ext_chans.append(sess) |
| 71 | + |
| 72 | + # Initialize NI-FGen driver for SL voltage |
| 73 | + self.sl_ext_chan = nifgen.Session(settings["FGen"]["deviceID"]) |
| 74 | + self.sl_ext_chan.output_mode = nifgen.OutputMode.FUNC |
| 75 | + self.sl_ext_chan.configure_standard_waveform(nifgen.Waveform.DC, 0.0, frequency=10000000) |
| 76 | + self.sl_ext_chan.initiate() |
| 77 | + |
| 78 | + # Set address to 0 |
| 79 | + self.set_address(0) |
| 80 | + |
| 81 | + def read(self): |
| 82 | + """Perform a READ operation. Returns tuple with (res, cond, meas_i, meas_v)""" |
| 83 | + # Increment the number of READs |
| 84 | + self.prof["READs"] += 1 |
| 85 | + |
| 86 | + # Address decoder enable |
| 87 | + self.decoder_enable() |
| 88 | + |
| 89 | + # Set voltages |
| 90 | + self.set_vsl(0) |
| 91 | + self.set_vbl(self.settings["READ"]["VBL"]) |
| 92 | + self.set_vwl(self.settings["READ"]["VWL"]) |
| 93 | + |
| 94 | + # Settling time for VBL |
| 95 | + accurate_delay(self.settings["READ"]["settling_time"]) |
| 96 | + |
| 97 | + # Measure |
| 98 | + meas_v = np.mean(self.read_chan.read(self.settings["READ"]["n_samples"])) |
| 99 | + meas_i = meas_v/self.settings["READ"]["shunt_res_value"] |
| 100 | + res = np.abs(self.settings["READ"]["VBL"]/meas_i - self.settings["READ"]["shunt_res_value"]) |
| 101 | + cond = 1/res |
| 102 | + |
| 103 | + # Turn off VBL and VWL |
| 104 | + self.set_vbl(0) |
| 105 | + self.set_vwl(0) |
| 106 | + |
| 107 | + # Address decoder disable |
| 108 | + self.decoder_disable() |
| 109 | + |
| 110 | + # TODO: log READ operation |
| 111 | + |
| 112 | + # Return measurement tuple |
| 113 | + return res, cond, meas_i, meas_v |
| 114 | + |
| 115 | + def set_pulse(self, vwl=None, vbl=None, pw=None): |
| 116 | + |
| 117 | + |
| 118 | + def set_vsl(self, voltage): |
| 119 | + """Set VSL using NI-FGen driver""" |
| 120 | + # Set DC offset to V/2 since it is doubled by FGen for some reason |
| 121 | + self.sl_ext_chan.func_dc_offset = voltage/2 |
| 122 | + |
| 123 | + def set_vbl(self, voltage): |
| 124 | + """Set (active) VBL using NI-DCPower driver (inactive disabled)""" |
| 125 | + # LSB indicates active BL channel |
| 126 | + active_bl_chan = (self.addr >> 0) & 0b1 |
| 127 | + active_bl = self.bl_ext_chans[active_bl_chan] |
| 128 | + inactive_bl = self.bl_ext_chans[1-active_bl_chan] |
| 129 | + |
| 130 | + # Set voltages and commit |
| 131 | + active_bl.voltage_level = voltage |
| 132 | + active_bl.commit() |
| 133 | + inactive_bl.voltage_level = 0 |
| 134 | + inactive_bl.commit() |
| 135 | + |
| 136 | + def set_vwl(self, voltage): |
| 137 | + """Set (active) VWL using NI-DAQmx driver (inactive disabled)""" |
| 138 | + # Select 8th and 9th bit to get the channel and the driver card, respectively |
| 139 | + active_wl_chan = (self.addr >> 8) & 0b1 |
| 140 | + active_wl_dev = (self.addr >> 9) & 0b1 |
| 141 | + active_wl = self.wl_ext_chans[active_wl_dev] |
| 142 | + inactive_wl = self.wl_ext_chans[1-active_wl_dev] |
| 143 | + |
| 144 | + # Write |
| 145 | + active_wl.write([(1-active_wl_chan)*voltage, active_wl_chan*voltage], auto_start=True) |
| 146 | + inactive_wl.write([0,0], auto_start=True) |
| 147 | + |
| 148 | + def pulse_vwl(self, voltage, pw): |
| 149 | + """Pulse (active) VWL using NI-DAQmx driver (inactive disabled)""" |
| 150 | + # Select 8th and 9th bit to get the channel and the driver card, respectively |
| 151 | + active_wl_chan = (self.addr >> 8) & 0b1 |
| 152 | + active_wl_dev = (self.addr >> 9) & 0b1 |
| 153 | + active_wl = self.wl_ext_chans[active_wl_dev] |
| 154 | + inactive_wl = self.wl_ext_chans[1-active_wl_dev] |
| 155 | + |
| 156 | + # Write |
| 157 | + active_wl.write([(1-active_wl_chan)*voltage, active_wl_chan*voltage], auto_start=True) |
| 158 | + inactive_wl.write([0,0], auto_start=True) |
| 159 | + |
| 160 | + def decoder_enable(self): |
| 161 | + """Enable decoding circuitry using digital signals""" |
| 162 | + self.hsdio.write_data_across_chans("wl_dec_en", 0b11) |
| 163 | + self.hsdio.write_data_across_chans("sl_dec_en", 0b1) |
| 164 | + self.hsdio.write_data_across_chans("wl_clk", 0b1) |
| 165 | + |
| 166 | + def decoder_disable(self): |
| 167 | + """Disable decoding circuitry using digital signals""" |
| 168 | + self.hsdio.write_data_across_chans("wl_dec_en", 0b00) |
| 169 | + self.hsdio.write_data_across_chans("sl_dec_en", 0b0) |
| 170 | + self.hsdio.write_data_across_chans("wl_clk", 0b0) |
| 171 | + |
| 172 | + def set_address(self, addr): |
| 173 | + """Set the address and hold briefly""" |
| 174 | + # Update address |
| 175 | + self.addr = addr |
| 176 | + |
| 177 | + # Extract SL and WL addresses |
| 178 | + sl_addr = (self.addr >> 1) & 0b1111111 |
| 179 | + wl_addr = (self.addr >> 10) & 0b111111 |
| 180 | + |
| 181 | + # Write addresses to corresponding HSDIO channels |
| 182 | + self.hsdio.write_data_across_chans("sl_addr", sl_addr) |
| 183 | + self.hsdio.write_data_across_chans("wl_addr", wl_addr) |
| 184 | + accurate_delay(self.settings["addr_hold_time"]) |
| 185 | + |
| 186 | + # Reset profiling counters |
| 187 | + self.prof = {"READs": 0, "SETs": 0, "RESETs": 0} |
| 188 | + |
| 189 | + def close(self): |
| 190 | + """Close all NI sessions""" |
| 191 | + # Close NI-HSDIO |
| 192 | + self.hsdio.close() |
| 193 | + |
| 194 | + # Close NI-DAQmx AI |
| 195 | + self.read_chan.close() |
| 196 | + |
| 197 | + # Close NI-DAQmx AOs |
| 198 | + for task in self.wl_ext_chans: |
| 199 | + task.stop() |
| 200 | + task.close() |
| 201 | + |
| 202 | + # Close NI-DCPower |
| 203 | + for sess in self.bl_ext_chans: |
| 204 | + sess.abort() |
| 205 | + sess.close() |
| 206 | + |
| 207 | + # Close NI-FGen |
| 208 | + self.sl_ext_chan.abort() |
| 209 | + self.sl_ext_chan.close() |
| 210 | + |
| 211 | + # Give some time to shutdown |
| 212 | + time.sleep(1) |
| 213 | + |
| 214 | +if __name__=="__main__": |
| 215 | + # Basic test |
| 216 | + nirram = NIRRAM("Chip9") |
| 217 | + for address in range(2000): |
| 218 | + nirram.set_address(address) |
| 219 | + if address % 100 == 0: |
| 220 | + print((address, nirram.read()),) |
0 commit comments