Example #1
0
def _usgn(i, width):
    """ Return i as an unsigned of given width, raising exceptions for out of bounds """
    if 0 <= i <= 2**width:
        return int(i)

    raise ValueOutOfRangeException("%d doesn't fit in %d unsigned bits" %
                                   (i, width))
Example #2
0
def _upsgn(i, width):
    """ Return the signed integer that comes about by interpretting *i* as a signed
	field of *width* bytes"""

    if i < 0 or i > 2**width:
        raise ValueOutOfRangeException()

    if i < 2**(width - 1):
        return i

    return i - 2**width
Example #3
0
def _sgn(i, width):
    """ Return the unsigned that, when interpretted with given width, represents
	    the signed value i """
    if i < -2**(width - 1) or 2**(width - 1) - 1 < i:
        raise ValueOutOfRangeException("%d doesn't fit in %d signed bits" %
                                       (i, width))

    if i >= 0:
        return int(i)

    return int(2**width + i)
Example #4
0
    def _set_source(self, ch, source):
        """ Sets the source of the channel data to either the analog input or
            internally looped-back digital output.
        """
        # TODO: Add loopback mode parameter
        _utils.check_parameter_valid('set', ch, [1, 2], 'channel')

        if ch == 1:
            self.source_ch1 = source
        elif ch == 2:
            self.source_ch2 = source
        else:
            raise ValueOutOfRangeException("Incorrect channel number %d", ch)
Example #5
0
def _set_meth(reg, set_xform, self, data):
    # Support a single register or a tuple of registers
    try:
        old = [
            self._localregs[r]
            if self._localregs[r] is not None else self._remoteregs[r] or 0
            for r in reg
        ]
    except TypeError:
        old = self._localregs[reg] if self._localregs[
            reg] is not None else self._remoteregs[reg] or 0

    new = set_xform(data, old)
    if new is None:
        raise ValueOutOfRangeException("Reg %d Data %d" % (reg, data))

    try:
        for r, n in zip(reg, new):
            self._localregs[r] = n
    except TypeError:
        self._localregs[reg] = new
Example #6
0
    def synth_sinewave(self, ch, amplitude, frequency, offset=0, clip=None):
        """ Generate a Sine Wave with the given parameters on the given channel.

		:type ch: int
		:param ch: Channel on which to generate the wave

		:type amplitude: float, volts
		:param amplitude: Waveform peak-to-peak amplitude

		:type frequency: float
		:param frequency: Freqency of the wave

		:type offset: float, volts
		:param offset: DC offset applied to the waveform

		:type clip: float, 0-1
		:param clip: Fraction of the waveform to scale to the amplitude. Sine waves with high clipping ratios can be used to generate
			high-quality, high-speed square waves where a normal square would suffer from edge jitter.  If the clipping ratio is
			greater than 1,	the amplitude field refers to the size of the clipped output waveform.

			Only integer ratios are allowed, others will be rounded (i.e. *1 / n* for integer *n*).

			*clip = None* behaves the same as *clip = 1* and preserves the entire waveform."""
        if ch == 1:
            self.out1_waveform = SG_WAVE_SINE if clip is None else SG_WAVE_CLIPSINE
            self.out1_enable = True
            self.out1_amplitude = amplitude
            self.out1_frequency = frequency
            self.out1_offset = offset
            self.out1_amp_pc = clip or 1
        elif ch == 2:
            self.out2_waveform = SG_WAVE_SINE if clip is None else SG_WAVE_CLIPSINE
            self.out2_enable = True
            self.out2_amplitude = amplitude
            self.out2_frequency = frequency
            self.out2_offset = offset
            self.out2_amp_pc = clip or 1
        else:
            raise ValueOutOfRangeException("Invalid Channel")
Example #7
0
def check_parameter_valid(check_type,
                          v,
                          allowed=None,
                          desc="",
                          units="",
                          allow_none=False):
    if allow_none and v is None:
        return

    if check_type == 'bool':
        if not isinstance(v, bool):
            raise InvalidParameterException(
                "Invalid parameter \'%s\': %s. "
                "Expected boolean value [True, False]." % (desc, v))
    elif check_type == 'int':
        try:
            int(v)
        except (ValueError, TypeError):
            raise InvalidParameterException(
                "Invalid parameter \'%s\': %s. Expected integer." % (desc, v))
    elif check_type == 'string':
        try:
            # Correct string check for Python 2.x
            if not isinstance(v, basestring):
                raise InvalidParameterException(
                    "Invalid parameter \'%s\': %s. Expected string." %
                    (desc, v))
        except NameError:
            # Correct string check for Python 3.x
            if not isinstance(v, str):
                raise InvalidParameterException(
                    "Invalid parameter \'%s\': %s. Expected string." %
                    (desc, v))
    elif check_type == 'float':
        try:
            float(v)
        except (ValueError, TypeError):
            raise InvalidParameterException(
                "Invalid parameter \'%s\': %s %s. Expected "
                "floating-point number." % (desc, v, units))
    elif not isinstance(allowed, (list, tuple)) and \
            (sys.version_info[0] == 3 and not isinstance(allowed, range)):
        # This case enables the "allow" parameter to be specified using
        # Python 3's built-in range function which returns a 'range' type
        # object
        raise InvalidParameterException(
            "Invalid parameter 'allowed': %s %s. Expected array, tuple or "
            "range type object." % (allowed, units))
    elif check_type == 'set':
        if not (v in allowed):
            raise InvalidParameterException(
                "Invalid parameter \'%s\': %s. Valid set %s %s." %
                (desc, v, allowed, units))
    elif check_type == 'range':
        if not (len(allowed) == 2):
            raise InvalidParameterException(
                "Invalid allowed range %s. Expected [MIN,MAX]." % allowed)
        elif isinstance(allowed,
                        tuple) and not (v > allowed[0] and v < allowed[1]):
            raise ValueOutOfRangeException(
                "Invalid parameter \'%s\': %s. Valid range (%s, %s) %s." %
                (desc, v, allowed[0], allowed[1], units))
        elif not ((v >= allowed[0]) and (v <= allowed[1])):
            raise ValueOutOfRangeException(
                "Invalid parameter \'%s\': %s. Valid range [%s, %s] %s." %
                (desc, v, allowed[0], allowed[1], units))
    else:
        raise InvalidParameterException("Invalid parameter 'check_type': %s. "
                                        "Expected ['bool','set','range']." %
                                        check_type)
Example #8
0
    def synth_squarewave(self,
                         ch,
                         amplitude,
                         frequency,
                         offset=0,
                         duty=0.5,
                         risetime=0,
                         falltime=0):
        """ Generate a Square Wave with given parameters on the given channel.

		:type ch: int
		:param ch: Channel on which to generate the wave

		:type amplitude: float, volts
		:param amplitude: Waveform peak-to-peak amplitude

		:type frequency: float
		:param frequency: Freqency of the wave

		:type offset: float, volts
		:param offset: DC offset applied to the waveform

		:type duty: float, 0-1
		:param duty: Fractional duty cycle

		:type risetime: float, 0-1
		:param risetime: Fraction of a cycle taken for the waveform to rise

		:type falltime: float 0-1
		:param falltime: Fraction of a cycle taken for the waveform to fall"""

        if duty < risetime:
            raise ValueOutOfRangeException(
                "Duty too small for given rise rate")
        elif duty + falltime > 1:
            raise ValueOutOfRangeException("Duty and fall time too big")

        if ch == 1:
            self.out1_waveform = SG_WAVE_SQUARE
            self.out1_enable = True
            self.out1_amplitude = amplitude
            self.out1_frequency = frequency
            self.out1_offset = offset

            # This is overdefined, but saves the FPGA doing a tricky division
            self.out1_t0 = risetime
            self.out1_t1 = duty
            self.out1_t2 = duty + falltime
            self.out1_riserate = frequency / risetime
            self.out1_fallrate = frequency / falltime
        elif ch == 2:
            self.out2_waveform = SG_WAVE_SQUARE
            self.out2_enable = True
            self.out2_amplitude = amplitude
            self.out2_frequency = frequency
            self.out2_offset = offset
            self.out2_t0 = risetime
            self.out2_t1 = duty
            self.out2_t2 = duty + falltime
            self.out2_riserate = frequency / risetime
            self.out2_fallrate = frequency / falltime
        else:
            raise ValueOutOfRangeException("Invalid Channel")