Beispiel #1
0
	def __init__(self, uid = None):
		(num, ports, unique_ids) = aa_py.aa_find_devices_ext(16, 16)
		if num > 0:
			ix = 0
			if uid:
				if uid in unique_ids:
					ix = unique_ids.index(uid)
					port = ports[ix]
					if port & aa_py.AA_PORT_NOT_FREE:
						raise ValueError
				else:
					raise ValueError
			else:
				while ix < num:
					port = ports[ix]
					if port & aa_py.AA_PORT_NOT_FREE:
						ix += 1
						continue
					break
				if ix == num:
					print "No Free Device"
					raise IndexError
		else:
			print "No devices connected"
			raise IndexError

		self.aa = aa_py.aa_open(port)
		self.get_serial_number()
		aa_py.aa_configure(self.aa,  aa_py.AA_CONFIG_GPIO_I2C)
		aa_py.aa_configure(self.aa,  aa_py.AA_TARGET_POWER_NONE)
		aa_py.aa_configure(self.aa,  aa_py.AA_CONFIG_GPIO_I2C)
    def open(self, port_index=0):
        "Connect to bus adapter"
        _i2c_bus.open(self)

        # TODO, need to support multiple instances of bus_aadvark_i2c with single adapter.
        # This way it is possible to connect multiple i2c slaves to same bus.

        self._handle = aa.aa_open(port_index)
        if self._handle < 0:
            if (self._handle == -7):
                raise BusException(
                    'bus_aadvark_i2c.open failed with error code %d (Aardvark disconnected)'
                    % self._handle)
            else:
                raise BusException(
                    'bus_aadvark_i2c.open failed with error code %d' %
                    self._handle)

        # IO direction now all input, do this before AA_CONFIG_GPIO_I2C
        aa.aa_gpio_direction(
            self._handle,
            int(evkit_config.get('aardvark_i2c', 'aa_gpio_direction'), 16))

        ## select pullup or floating
        #aa.aa_gpio_pullup(self._handle, aa.AA_GPIO_SCK | aa.AA_GPIO_MOSI) # pullup for gpio lines
        aa.aa_gpio_pullup(
            self._handle,
            int(evkit_config.get('aardvark_i2c', 'aa_gpio_pullup'), 16))

        #todo slave address selection GPIO to output

        # Ensure that the I2C subsystem is enabled
        aa.aa_configure(self._handle, aa.AA_CONFIG_GPIO_I2C)

        # Enable the I2C bus pullup resistors (2.2k resistors).
        # This command is only effective on v2.0 hardware or greater.
        # The pullup resistors on the v1.02 hardware are enabled by default.
        aa.aa_i2c_pullup(
            self._handle,
            self.aa_i2c_pullup[evkit_config.get('aardvark_i2c',
                                                'aa_i2c_pullup')])

        # Power the board using the Aardvark adapter's power supply.
        # This command is only effective on v2.0 hardware or greater.
        # The power pins on the v1.02 hardware are not enabled by default.
        aa.aa_target_power(
            self._handle,
            self.aa_target_power[evkit_config.get('aardvark_i2c',
                                                  'aa_target_power')])

        # Set the bitrate
        requested = self._bitrate
        self._bitrate = aa.aa_i2c_bitrate(self._handle, self._bitrate)
        if requested != self._bitrate:
            logger.warning('Bitrate set to %d kHz. Wanted to set %d kHz' %
                           (self._bitrate, requested))
    def test_02_open_close(self):
        """Tests that the port can be successfully opened and closed"""
        handle = aa_open(self.port_number)
        self.assertGreater(handle, 0)  # check that the port is open

        self.configure()
        _, status = AardvarkConnection.get_status(self.port_number)
        self.assertEqual(status, False)
        num_closed = aa_close(handle)
        self.assertEqual(num_closed, 1)
    def open(self, port_index=0):
        assert evkit_config.get(
            'spi', 'ss_polarity') in ['ACTIVE_LOW', 'ACTIVE_HIGH']
        assert evkit_config.get('spi', 'bitorder') in ['MSB', 'LSB']
        _spi_bus.open(self)

        self._handle = aa.aa_open(port_index)
        if self._handle < 0:
            if (self._handle == -7):
                raise BusException(
                    'bus_aadvark_spi.open failed with error code %d (Aardvark disconnected)'
                    % self._handle)
            else:
                raise BusException(
                    'bus_aadvark_spi.open failed with error code %d' %
                    self._handle)

        aa.aa_gpio_direction(self._handle,
                             int(
                                 evkit_config.get('aardvark_spi',
                                                  'aa_gpio_direction'),
                                 16))  # IO direction

        ## select pullup or floating
        #aa.aa_gpio_pullup(self._handle, aa.AA_GPIO_SCL | aa.AA_GPIO_SDA)             # pullup
        aa.aa_gpio_pullup(
            self._handle,
            int(evkit_config.get('aardvark_spi', 'aa_gpio_pullup'), 16))

        aa.aa_configure(self._handle,
                        aa.AA_CONFIG_SPI_GPIO)  # SPI subsystem is enabled

        aa.aa_target_power(
            self._handle,
            self._aa_target_power[evkit_config.get('aardvark_spi',
                                                   'aa_target_power')])

        aa.aa_spi_configure(
            self._handle, self._cpol, self._cpha,
            self._bitorder[evkit_config.get('spi', 'bitorder')])

        aa.aa_spi_master_ss_polarity(
            self._handle,
            self._ss_polarity[evkit_config.get('spi', 'ss_polarity')])

        requested = self._bitrate
        self._bitrate = aa.aa_spi_bitrate(self._handle,
                                          self._bitrate)  # Set the bitrate

        if requested != self._bitrate:
            logger.warning(
                "Warning Bitrate set to %d kHz. Wanted to set %d kHz" %
                (self._bitrate, requested))
Beispiel #5
0
 def __init__(self):
   """Initialize a new instance of the Bus class.  This will access the
   Aardvark API and open the first available Aardvark USB device.  If no
   device is available, an exception will be raised.
   """
   self._port = _find_aardvark()
   self._open = False
   self._handle = _check_result(api.aa_open(self._port))
   self._open = True
   _check_result(api.aa_configure(self._handle, api.AA_CONFIG_GPIO_I2C)) # AA_CONFIG_SPI_I2C
   _check_result(api.aa_i2c_pullup(self._handle, api.AA_I2C_PULLUP_BOTH))
   _check_result(api.aa_i2c_bitrate(self._handle, 100))
   _check_result(api.aa_gpio_set(self._handle, 0x00)) # Initialize to zeros
   _check_result(api.aa_gpio_direction(self._handle, 0xFF)) # All outputs
Beispiel #6
0
 def __init__(self):
     """Initialize a new instance of the Bus class.  This will access the
 Aardvark API and open the first available Aardvark USB device.  If no
 device is available, an exception will be raised.
 """
     self._port = _find_aardvark()
     self._open = False
     self._handle = _check_result(api.aa_open(self._port))
     self._open = True
     _check_result(api.aa_configure(
         self._handle, api.AA_CONFIG_GPIO_I2C))  # AA_CONFIG_SPI_I2C
     _check_result(api.aa_i2c_pullup(self._handle, api.AA_I2C_PULLUP_BOTH))
     _check_result(api.aa_i2c_bitrate(self._handle, 100))
     _check_result(api.aa_gpio_set(self._handle,
                                   0x00))  # Initialize to zeros
     _check_result(api.aa_gpio_direction(self._handle, 0xFF))  # All outputs
Beispiel #7
0
    def _open(self, identifier=0):
        """Open Aardvark adapter given the port, unique ID, or serial number.  If no identifier is provided, the default behavior is to open port 0 (if it exists).

        Intended to be called only as part of the constructor initialization, not for existing objects.

        Raise AardvarkError if unable to open an Aardvark adapter.
        """
        devices = find_devices()
        unique_ids = devices.keys()
        ports = devices.values()
        # check if identifier is a serial number string of the form "xxxx-xxxxxx"
        if isinstance(identifier, str) and re.match("(\d{4,4}-\d{6,6})",
                                                    identifier):
            # convert the serial number to a unique ID
            int_identifier = unique_id(identifier)
        # otherwise, assume it's either a port or a unique ID
        else:
            int_identifier = int(identifier)
        # if the identifier is a serial number
        if int_identifier in unique_ids:
            # get the port that matches the serial number
            port = devices[int_identifier]
        # otherwise check if the identifier is a port
        elif int_identifier in ports:
            # it's a valid port
            port = int_identifier
        # it's not a valid port or unique ID
        else:
            raise AardvarkError("Aardvark adapter %s is not attached" %
                                identifier)
        # check if the port is in use
        if port & aardvark_py.AA_PORT_NOT_FREE:
            port ^= aardvark_py.AA_PORT_NOT_FREE
            raise AardvarkError("port %s in use" % port)
        else:
            self._aardvark_handle = aardvark_py.aa_open(port)
            _validate_status(self._aardvark_handle)
            if self._aardvark_handle <= 0:
                raise AardvarkError("invalid Aardvark handle %s" %
                                    self._aardvark_handle)
def Open_Aardvark(number=0):
    global Aardvark_Handle
    (num, ports, unique_ids) = aardvark_py.aa_find_devices_ext(16, 16)
    port = None
    dongle_id = number
    if number == 0 and len(unique_ids) > 0:
        port = ports[0]
        dongle_id = unique_ids[0]
    else:
        for i in range(0, len(unique_ids)):
            if unique_ids[i] == number:
                port = ports[i]
                dongle_id = unique_ids[i]
                break

    if port is None:
        raise Exception('Failed to find dongle: ' + str(dongle_id))
    Aardvark_Handle = aardvark_py.aa_open(port)
    if (Aardvark_Handle <= 0):
        raise Exception('Failed to open dongle: ' + str(dongle_id))
    else:
        print('Using dongle with ID: ' + str(dongle_id) + "\n")
Beispiel #9
0
    def _open(self, identifier=0):
        """Open Aardvark adapter given the port, unique ID, or serial number.  If no identifier is provided, the default behavior is to open port 0 (if it exists).

        Intended to be called only as part of the constructor initialization, not for existing objects.

        Raise AardvarkError if unable to open an Aardvark adapter.
        """
        devices = find_devices()
        unique_ids = devices.keys()
        ports = devices.values()
        # check if identifier is a serial number string of the form "xxxx-xxxxxx"
        if isinstance(identifier, str) and re.match("(\d{4,4}-\d{6,6})", identifier):
            # convert the serial number to a unique ID
            int_identifier = unique_id(identifier)
        # otherwise, assume it's either a port or a unique ID
        else:
            int_identifier = int(identifier)
        # if the identifier is a serial number
        if int_identifier in unique_ids:
            # get the port that matches the serial number
            port = devices[int_identifier]
        # otherwise check if the identifier is a port
        elif int_identifier in ports:
            # it's a valid port
            port = int_identifier
        # it's not a valid port or unique ID
        else:
            raise AardvarkError("Aardvark adapter %s is not attached" % identifier)
        # check if the port is in use
        if port & aardvark_py.AA_PORT_NOT_FREE:
            port ^= aardvark_py.AA_PORT_NOT_FREE
            raise AardvarkError("port %s in use" % port)
        else:
            self._aardvark_handle = aardvark_py.aa_open(port)
            _validate_status(self._aardvark_handle)
            if self._aardvark_handle <= 0:
                raise AardvarkError("invalid Aardvark handle %s" % self._aardvark_handle)
Beispiel #10
0
def configure_aardvark():
    """ 
    Function to configure the aardvark for pySCPI operation if there is one
    available.
    
    @return  (aardvark_py.aardvark)   The handle of the aardvark to be used
                                      'None' if there is not one available
    """
    # define the handle to return
    Aardvark_in_use = None
    
    # find all connected aardvarks
    AA_Devices = aardvark_py.aa_find_devices(1)
    
    # define a port mask
    Aardvark_port = 8<<7
    
    # assume that an aardvark can be found until proved otherwise
    Aardvark_free = True
    
    # Check if there is an Aardvark present
    if (AA_Devices[0] < 1):
        # there is no aardvark to be found
        print '*** No Aardvark is present ***'
        Aardvark_free = False
        
    else:
        # there is an aardvark connected to select the first one if there
        # are many
        Aardvark_port = AA_Devices[1][0]
    # end if
    
    
    # If there is an Aardvark there is it free?
    if Aardvark_port >= 8<<7 and Aardvark_free:
        # the aardvark is not free
        print '*** Aardvark is being used, '\
              'disconnect other application or Aardvark device ***'
        # close the aardvark
        aardvark_py.aa_close(Aardvark_port)
        Aardvark_free = False
        
    elif Aardvark_free:
        # Aardvark is available so configure it
        
        # open the connection with the aardvark
        Aardvark_in_use = aardvark_py.aa_open(Aardvark_port)
        
        # set it up in teh mode we need for pumpkin modules
        aardvark_py.aa_configure(Aardvark_in_use, 
                                 aardvark_py.AA_CONFIG_SPI_I2C)
        
        # default to both pullups on
        aardvark_py.aa_i2c_pullup(Aardvark_in_use, 
                                  aardvark_py.AA_I2C_PULLUP_BOTH)
        
        # set the bit rate to be the default
        aardvark_py.aa_i2c_bitrate(Aardvark_in_use, Bitrate)
        
        # free the bus
        aardvark_py.aa_i2c_free_bus(Aardvark_in_use)
        
        # delay to allow the config to be registered
        aardvark_py.aa_sleep_ms(200)    
        
        print "Starting Aardvark communications\n"
    # end if    
    
    return Aardvark_in_use
    """This function sets the SWI Wakeup delay (us)."""
    delay %= 25500
    print "Set the SWI Wakeup delay to %d us" % (delay)
    res = write(handle, array('B', [0x01, 0x01, 0x00, delay / 100]))
    if res <= 0:
        return res
    return read(handle, 2)


#==========================================================================
# MAIN PROGRAM
#==========================================================================
port = 0

# Open the device.
handle = aa_open(port)
if (handle <= 0):
    print "Unable to open Aardvark device on port %d" % port
    print "Error code = %d" % handle
    sys.exit()

# Enable  I2C subsystem.
aa_configure(handle, AA_CONFIG_SPI_I2C)

# Enable the I2C bus pullup resistors (2.2k resistors).
#aa_i2c_pullup(handle, AA_I2C_PULLUP_BOTH)

# Power the board using the Aardvark adapter's power supply.
aa_target_power(handle, AA_TARGET_POWER_BOTH)

# Set the bitrate.