def set(self, value: TFloat): """Set spline value. :param value: Spline value relative to full-scale. """ if self.width > 32: l = [int32(0)] * 2 self.pack_coeff_mu([self.to_mu64(value)], l) rtio_output_wide(now_mu(), self.channel, 0, l) else: rtio_output(now_mu(), self.channel, 0, self.to_mu(value))
def set_mu(self, ftw, pow_=0, asf=0x3fff, phase_mode=_PHASE_MODE_DEFAULT, ref_time_mu=int64(-1), profile=0): """Set profile 0 data in machine units. This uses machine units (FTW, POW, ASF). The frequency tuning word width is 32, the phase offset word width is 16, and the amplitude scale factor width is 12. After the SPI transfer, the shared IO update pin is pulsed to activate the data. .. seealso: :meth:`set_phase_mode` for a definition of the different phase modes. :param ftw: Frequency tuning word: 32 bit. :param pow_: Phase tuning word: 16 bit unsigned. :param asf: Amplitude scale factor: 14 bit unsigned. :param phase_mode: If specified, overrides the default phase mode set by :meth:`set_phase_mode` for this call. :param ref_time_mu: Fiducial time used to compute absolute or tracking phase updates. In machine units as obtained by `now_mu()`. :param profile: Profile number to set (0-7, default: 0). :return: Resulting phase offset word after application of phase tracking offset. When using :const:`PHASE_MODE_CONTINUOUS` in subsequent calls, use this value as the "current" phase. """ if phase_mode == _PHASE_MODE_DEFAULT: phase_mode = self.phase_mode # Align to coarse RTIO which aligns SYNC_CLK. I.e. clear fine TSC # This will not cause a collision or sequence error. at_mu(now_mu() & ~7) if phase_mode != PHASE_MODE_CONTINUOUS: # Auto-clear phase accumulator on IO_UPDATE. # This is active already for the next IO_UPDATE self.set_cfr1(phase_autoclear=1) if phase_mode == PHASE_MODE_TRACKING and ref_time_mu < 0: # set default fiducial time stamp ref_time_mu = 0 if ref_time_mu >= 0: # 32 LSB are sufficient. # Also no need to use IO_UPDATE time as this # is equivalent to an output pipeline latency. dt = int32(now_mu()) - int32(ref_time_mu) pow_ += dt*ftw*self.sysclk_per_mu >> 16 self.write64(_AD9910_REG_PROFILE0 + profile, (asf << 16) | (pow_ & 0xffff), ftw) delay_mu(int64(self.io_update_delay)) self.cpld.io_update.pulse_mu(8) # assumes 8 mu > t_SYN_CCLK at_mu(now_mu() & ~7) # clear fine TSC again if phase_mode != PHASE_MODE_CONTINUOUS: self.set_cfr1() # future IO_UPDATE will activate return pow_
def write(self, data): """Write SPI data to shift register register and start transfer. * The ``data`` register and the shift register are 32 bits wide. * Data writes take one ``ref_period`` cycle. * A transaction consisting of a single transfer (``SPI_END``) takes :attr:`xfer_duration_mu` ``=(n + 1)*div`` cycles RTIO time where ``n`` is the number of bits and ``div`` is the SPI clock divider. * Transfers in a multi-transfer transaction take up to one SPI clock cycle less time depending on multiple parameters. Advanced users may rewind the timeline appropriately to achieve faster multi-transfer transactions. * The SPI core will be busy for the duration of the SPI transfer. * For bit alignment and bit ordering see :meth:`set_config`. * The SPI core can only be written to when it is idle or waiting for the next transfer data. Writing (:meth:`set_config`, :meth:`set_config_mu` or :meth:`write`) when the core is busy will result in an RTIO busy error being logged. This method advances the timeline by the duration of one single-transfer SPI transaction (:attr:`xfer_duration_mu`). :param data: SPI output data to be written. """ rtio_output(now_mu(), self.channel, SPI_DATA_ADDR, data) delay_mu(self.xfer_duration_mu)
def set_config_mu(self, flags, length, div, cs): """Set the ``config`` register (in SPI bus machine units). .. seealso:: :meth:`set_config` :param flags: A bit map of `SPI_*` flags. :param length: Number of bits to write during the next transfer. (reset=1) :param div: Counter load value to divide the RTIO clock by to generate the SPI clock. (minimum=2, reset=2) ``f_rtio_clk/f_spi == div``. If ``div`` is odd, the setup phase of the SPI clock is one coarse RTIO clock cycle longer than the hold phase. :param cs: Bit pattern of chip selects to assert. Or number of the chip select to assert if ``cs`` is decoded downstream. (reset=0) """ if length > 32 or length < 1: raise ValueError("Invalid SPI transfer length") if div > 257 or div < 2: raise ValueError("Invalid SPI clock divider") self.xfer_duration_mu = (length + 1)*div*self.ref_period_mu rtio_output(now_mu(), self.channel, SPI_CONFIG_ADDR, flags | ((length - 1) << 8) | ((div - 2) << 16) | (cs << 24)) delay_mu(self.ref_period_mu)
def set_dac_mu(self, values, channels=list(range(40))): """Program multiple DAC channels and pulse LDAC to update the DAC outputs. This method does not advance the timeline; write events are scheduled in the past. The DACs will synchronously start changing their output levels `now`. If no LDAC device was defined, the LDAC pulse is skipped. See :meth load:. :param values: list of DAC values to program :param channels: list of DAC channels to program. If not specified, we program the DAC channels sequentially, starting at 0. """ t0 = now_mu() # t10: max busy period after writing to DAC registers t_10 = self.core.seconds_to_mu(1500*ns) # compensate all delays that will be applied delay_mu(-t_10-len(values)*self.bus.xfer_duration_mu) for i in range(len(values)): self.write_dac_mu(channels[i], values[i]) delay_mu(t_10) self.load() at_mu(t0)
def set_iq_en(self, i_enable: TInt32, q_enable: TInt32): """Enable I/Q data on this DAC channel. Every pair of SAWG channels forms a buddy pair. The ``iq_en`` configuration controls which DDS data is emitted to the DACs. Refer to the documentation of :class:`SAWG` for a mathematical description of ``i_enable`` and ``q_enable``. .. note:: Quadrature data from the buddy channel is currently a technological preview only. The data is ignored in the SAWG gateware and not added to the DAC output. This is equivalent to the ``q_enable`` switch always being ``0``. :param i_enable: Controls adding the in-phase DUC-DDS data of *this* SAWG channel to *this* DAC channel. Default: ``1``. :param q_enable: controls adding the quadrature DUC-DDS data of this SAWG's *buddy* channel to *this* DAC channel. Default: ``0``. """ rtio_output(now_mu(), self.channel, _SAWG_IQ_EN, i_enable | (q_enable << 1)) delay_mu(self._rtio_interval)
def write(self, data=0): """Write data to data register. * The ``data`` register and the shift register are 32 bits wide. If there are no writes to the register, ``miso`` data reappears on ``mosi`` after 32 cycles. * A wishbone data register write is acknowledged when the transfer has been written to the intermediate buffer. It will be started when there are no other transactions being executed, either beginning a new SPI transfer of chained to an in-flight transfer. * Writes take three ``ref_period`` cycles unless another chained transfer is pending and the transfer being executed is not complete. * The SPI ``data`` register is double-buffered: Once a transfer has started, new write data can be written, queuing a new transfer. Transfers submitted this way are chained and executed without deasserting ``cs`` in between. Once a transfer completes, the previous transfer's read data is available in the ``data`` register. * For bit alignment and bit ordering see :meth:`set_config`. This method advances the timeline by the duration of the SPI transfer. If a transfer is to be chained, the timeline needs to be rewound. """ rtio_output(now_mu(), self.channel, SPI_DATA_ADDR, data) delay_mu(self.xfer_period_mu + self.write_period_mu)
def read_async(self): """Trigger an asynchronous read from the ``data`` register. Reads always finish in two cycles. Every data register read triggered by a :meth:`read_async` must be matched by a :meth:`input_async` to retrieve the data. This method advances the timeline by the duration of the RTIO-to-Wishbone bus transaction (three RTIO clock cycles). """ rtio_output(now_mu(), self.channel, SPI_DATA_ADDR | SPI_RT2WB_READ, 0) delay_mu(3 * self.ref_period_mu)
def set_duc_max_mu(self, limit: TInt32): """Set the digital up-converter (DUC) I and Q data summing junctions upper limit. In machine units. The default limits are chosen to reach maximum and minimum DAC output amplitude. For a description of the limiter functions in normalized units see: .. seealso:: :meth:`set_duc_max` """ rtio_output(now_mu(), self.channel, _SAWG_DUC_MAX, limit) delay_mu(self._rtio_interval)
def set_div(self, div: TInt32, n: TInt32=0): """Set the spline evolution divider and current counter value. The divider and the spline evolution are synchronized across all spline channels within a SAWG channel. The DDS/DUC phase accumulators always evolves at full speed. .. note:: The spline evolution divider has not been tested extensively and is currently considered a technological preview only. :param div: Spline evolution divider, such that ``t_sawg_spline/t_rtio_coarse = div + 1``. Default: ``0``. :param n: Current value of the counter. Default: ``0``. """ rtio_output(now_mu(), self.channel, _SAWG_DIV, div | (n << 16)) delay_mu(self._rtio_interval)
def set_config_mu(self, flags=0, write_div=6, read_div=6): """Set the ``config`` register (in SPI bus machine units). .. seealso:: :meth:`set_config` :param write_div: Counter load value to divide the RTIO clock by to generate the SPI write clk. (minimum=2, reset=2) ``f_rtio_clk/f_spi_write == write_div``. If ``write_div`` is odd, the setup phase of the SPI clock is biased to longer lengths by one RTIO clock cycle. :param read_div: Ditto for the read clock. """ rtio_output(now_mu(), self.channel, SPI_CONFIG_ADDR, flags | ((write_div - 2) << 16) | ((read_div - 2) << 24)) self.write_period_mu = int(write_div * self.ref_period_mu) self.read_period_mu = int(read_div * self.ref_period_mu) delay_mu(3 * self.ref_period_mu)
def set_xfer(self, chip_select=0, write_length=0, read_length=0): """Set the ``xfer`` register. * Every transfer consists of a write of ``write_length`` bits immediately followed by a read of ``read_length`` bits. * ``cs_n`` is asserted at the beginning and deasserted at the end of the transfer if there is no other transfer pending. * ``cs_n`` handling is agnostic to whether it is one-hot or decoded somewhere downstream. If it is decoded, "``cs_n`` all deasserted" should be handled accordingly (no slave selected). If it is one-hot, asserting multiple slaves should only be attempted if ``miso`` is either not connected between slaves, or open collector, or correctly multiplexed externally. * For 4-wire SPI only the sum of ``read_length`` and ``write_length`` matters. The behavior is the same (except for clock speeds) no matter how the total transfer length is divided between the two. For 3-wire SPI, the direction of ``mosi`` is switched from output to input after ``write_length`` bits. * Data output on ``mosi`` in 4-wire SPI during the read cycles is what is found in the data register at the time. Data in the ``data`` register outside the least/most (depending on ``config.lsb_first``) significant ``read_length`` bits is what is seen on ``miso`` (or ``mosi`` if ``config.half_duplex``) during the write cycles. * Writes to ``xfer`` are synchronized to the start of the next (possibly chained) transfer. This method advances the timeline by the duration of the RTIO-to-Wishbone bus transaction (three RTIO clock cycles). :param chip_select: Bit mask of chip selects to assert. Or number of the chip select to assert if ``cs`` is decoded downstream. (reset=0) :param write_length: Number of bits to write during the next transfer. (reset=0) :param read_length: Number of bits to read during the next transfer. (reset=0) """ rtio_output(now_mu(), self.channel, SPI_XFER_ADDR, chip_select | (write_length << 16) | (read_length << 24)) self.xfer_period_mu = int(write_length*self.write_period_mu + read_length*self.read_period_mu) delay_mu(3*self.ref_period_mu)
def measure_io_update_alignment(self, delay_start, delay_stop): """Use the digital ramp generator to locate the alignment between IO_UPDATE and SYNC_CLK. The ramp generator is set up to a linear frequency ramp (dFTW/t_SYNC_CLK=1) and started at a coarse RTIO time stamp plus `delay_start` and stopped at a coarse RTIO time stamp plus `delay_stop`. :param delay_start: Start IO_UPDATE delay in machine units. :param delay_stop: Stop IO_UPDATE delay in machine units. :return: Odd/even SYNC_CLK cycle indicator. """ # set up DRG self.set_cfr1(drg_load_lrr=1, drg_autoclear=1) # DRG -> FTW, DRG enable self.write32(_AD9910_REG_CFR2, 0x01090000) # no limits self.write64(_AD9910_REG_RAMP_LIMIT, -1, 0) # DRCTL=0, dt=1 t_SYNC_CLK self.write32(_AD9910_REG_RAMP_RATE, 0x00010000) # dFTW = 1, (work around negative slope) self.write64(_AD9910_REG_RAMP_STEP, -1, 0) # delay io_update after RTIO edge t = now_mu() + 8 & ~7 at_mu(t + delay_start) # assumes a maximum t_SYNC_CLK period self.cpld.io_update.pulse_mu(16 - delay_start) # realign # disable DRG autoclear and LRR on io_update self.set_cfr1() # stop DRG self.write64(_AD9910_REG_RAMP_STEP, 0, 0) at_mu(t + 0x1000 + delay_stop) self.cpld.io_update.pulse_mu(16 - delay_stop) # realign ftw = self.read32(_AD9910_REG_FTW) # read out effective FTW delay(100*us) # slack # disable DRG self.write32(_AD9910_REG_CFR2, 0x01010000) self.cpld.io_update.pulse_mu(8) return ftw & 1
def set_clr(self, clr0: TInt32, clr1: TInt32, clr2: TInt32): """Set the accumulator clear mode for the three phase accumulators. When the ``clr`` bit for a given DDS/DUC phase accumulator is set, that phase accumulator will be cleared with every phase offset RTIO command and the output phase of the DDS/DUC will be exactly the phase RTIO value ("absolute phase update mode"). .. math:: q^\prime(t) = p^\prime + (t - t^\prime) f^\prime In turn, when the bit is cleared, the phase RTIO channels determine a phase offset to the current (carrier-) value of the DDS/DUC phase accumulator. This "relative phase update mode" is sometimes also called “continuous phase mode”. .. math:: q^\prime(t) = q(t^\prime) + (p^\prime - p) + (t - t^\prime) f^\prime Where: * :math:`q`, :math:`q^\prime`: old/new phase accumulator * :math:`p`, :math:`p^\prime`: old/new phase offset * :math:`f^\prime`: new frequency * :math:`t^\prime`: timestamp of setting new :math:`p`, :math:`f` * :math:`t`: running time :param clr0: Auto-clear phase accumulator of the ``phase0``/ ``frequency0`` DUC. Default: ``True`` :param clr1: Auto-clear phase accumulator of the ``phase1``/ ``frequency1`` DDS. Default: ``True`` :param clr2: Auto-clear phase accumulator of the ``phase2``/ ``frequency2`` DDS. Default: ``True`` """ rtio_output(now_mu(), self.channel, _SAWG_CLR, clr0 | (clr1 << 1) | (clr2 << 2)) delay_mu(self._rtio_interval)
def init(self, blind=False): """Initialize and detect Urukul. Resets the DDS I/O interface and verifies correct CPLD gateware version. Does not pulse the DDS MASTER_RESET as that confuses the AD9910. :param blind: Do not attempt to verify presence and compatibility. """ cfg = self.cfg_reg # Don't pulse MASTER_RESET (m-labs/artiq#940) self.cfg_reg = cfg | (0 << CFG_RST) | (1 << CFG_IO_RST) if blind: self.cfg_write(self.cfg_reg) else: proto_rev = urukul_sta_proto_rev(self.sta_read()) if proto_rev != STA_PROTO_REV_MATCH: raise ValueError("Urukul proto_rev mismatch") delay(100 * us) # reset, slack self.cfg_write(cfg) if self.sync_div: at_mu(now_mu() & ~0xf) # align to RTIO/2 self.set_sync_div(self.sync_div) # 125 MHz/2 = 1 GHz/16 delay(1 * ms) # DDS wake up
def init(self, blind=False): """Initialize and detect Urukul. Resets the DDS I/O interface and verifies correct CPLD gateware version. Does not pulse the DDS MASTER_RESET as that confuses the AD9910. :param blind: Do not attempt to verify presence and compatibility. """ cfg = self.cfg_reg # Don't pulse MASTER_RESET (m-labs/artiq#940) self.cfg_reg = cfg | (0 << CFG_RST) | (1 << CFG_IO_RST) if blind: self.cfg_write(self.cfg_reg) else: proto_rev = urukul_sta_proto_rev(self.sta_read()) if proto_rev != STA_PROTO_REV_MATCH: raise ValueError("Urukul proto_rev mismatch") delay(100*us) # reset, slack self.cfg_write(cfg) if self.sync_div: at_mu(now_mu() & ~0xf) # align to RTIO/2 self.set_sync_div(self.sync_div) # 125 MHz/2 = 1 GHz/16 delay(1*ms) # DDS wake up
def run_daq(self, duration, daqs, chunk=200): self.core.break_realtime() self.trigger_controller.reset.off() at_mu(now_mu() + self.core.seconds_to_mu(duration)) self.trigger_controller.reset.on() self.daq_transfer_loop(daqs, duration, chunk)
def set_out_min_mu(self, limit: TInt32): """.. seealso:: :meth:`set_duc_max_mu`""" rtio_output(now_mu(), self.channel, _SAWG_OUT_MIN, limit) delay_mu(self._rtio_interval)
def set_mu(self, ftw: TInt32 = 0, pow_: TInt32 = 0, asf: TInt32 = 0x3fff, phase_mode: TInt32 = _PHASE_MODE_DEFAULT, ref_time_mu: TInt64 = int64(-1), profile: TInt32 = 7, ram_destination: TInt32 = -1) -> TInt32: """Set DDS data in machine units. This uses machine units (FTW, POW, ASF). The frequency tuning word width is 32, the phase offset word width is 16, and the amplitude scale factor width is 14. After the SPI transfer, the shared IO update pin is pulsed to activate the data. .. seealso: :meth:`set_phase_mode` for a definition of the different phase modes. :param ftw: Frequency tuning word: 32 bit. :param pow_: Phase tuning word: 16 bit unsigned. :param asf: Amplitude scale factor: 14 bit unsigned. :param phase_mode: If specified, overrides the default phase mode set by :meth:`set_phase_mode` for this call. :param ref_time_mu: Fiducial time used to compute absolute or tracking phase updates. In machine units as obtained by `now_mu()`. :param profile: Single tone profile number to set (0-7, default: 7). Ineffective if `ram_destination` is specified. :param ram_destination: RAM destination (:const:`RAM_DEST_FTW`, :const:`RAM_DEST_POW`, :const:`RAM_DEST_ASF`, :const:`RAM_DEST_POWASF`). If specified, write free DDS parameters to the ASF/FTW/POW registers instead of to the single tone profile register (default behaviour, see `profile`). :return: Resulting phase offset word after application of phase tracking offset. When using :const:`PHASE_MODE_CONTINUOUS` in subsequent calls, use this value as the "current" phase. """ if phase_mode == _PHASE_MODE_DEFAULT: phase_mode = self.phase_mode # Align to coarse RTIO which aligns SYNC_CLK. I.e. clear fine TSC # This will not cause a collision or sequence error. at_mu(now_mu() & ~7) if phase_mode != PHASE_MODE_CONTINUOUS: # Auto-clear phase accumulator on IO_UPDATE. # This is active already for the next IO_UPDATE self.set_cfr1(phase_autoclear=1) if phase_mode == PHASE_MODE_TRACKING and ref_time_mu < 0: # set default fiducial time stamp ref_time_mu = 0 if ref_time_mu >= 0: # 32 LSB are sufficient. # Also no need to use IO_UPDATE time as this # is equivalent to an output pipeline latency. dt = int32(now_mu()) - int32(ref_time_mu) pow_ += dt * ftw * self.sysclk_per_mu >> 16 if ram_destination == -1: self.write64(_AD9910_REG_PROFILE0 + profile, (asf << 16) | (pow_ & 0xffff), ftw) else: if not ram_destination == RAM_DEST_FTW: self.set_ftw(ftw) if not ram_destination == RAM_DEST_POWASF: if not ram_destination == RAM_DEST_ASF: self.set_asf(asf) if not ram_destination == RAM_DEST_POW: self.set_pow(pow_) delay_mu(int64(self.sync_data.io_update_delay)) self.cpld.io_update.pulse_mu(8) # assumes 8 mu > t_SYN_CCLK at_mu(now_mu() & ~7) # clear fine TSC again if phase_mode != PHASE_MODE_CONTINUOUS: self.set_cfr1() # future IO_UPDATE will activate return pow_
def set_mu(self, value: TInt32): """Set spline value (machine units). :param value: Spline value in integer machine units. """ rtio_output(now_mu(), self.channel, 0, value)
def set_coeff_mu(self, value): # TList(TInt32) """Set spline raw values. :param value: Spline packed raw values. """ rtio_output_wide(now_mu(), self.channel, 0, value)
def _get_config_sync(self): rtio_output(now_mu(), self.channel, SPI_CONFIG_ADDR | SPI_RT2WB_READ, 0) return rtio_input_data(self.channel)
def _get_xfer_sync(self): rtio_output(now_mu(), self.channel, SPI_XFER_ADDR | SPI_RT2WB_READ, 0) return rtio_input_data(self.channel)