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 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))
Esempio n. 3
0
 def spi_mode(self, value):
     if not isinstance(value, bool):
         raise TypeError("invalid SPI mode state: %s (must be boolean)" % value)
     config = aardvark_py.aa_configure(self._aardvark_handle, aardvark_py.AA_CONFIG_QUERY)
     _validate_status(config)
     if value:
         config |= aardvark_py.AA_CONFIG_SPI_MASK
     else:
         config &= ~aardvark_py.AA_CONFIG_SPI_MASK
     current_config = aardvark_py.aa_configure(self._aardvark_handle, config)
     _validate_status(current_config)
     if not (config == current_config):
         raise AardvarkError("unable to configure Aardvark for SPI mode")
Esempio n. 4
0
 def spi_mode(self, value):
     if not isinstance(value, bool):
         raise TypeError("invalid SPI mode state: %s (must be boolean)" %
                         value)
     config = aardvark_py.aa_configure(self._aardvark_handle,
                                       aardvark_py.AA_CONFIG_QUERY)
     _validate_status(config)
     if value:
         config |= aardvark_py.AA_CONFIG_SPI_MASK
     else:
         config &= ~aardvark_py.AA_CONFIG_SPI_MASK
     current_config = aardvark_py.aa_configure(self._aardvark_handle,
                                               config)
     _validate_status(current_config)
     if not (config == current_config):
         raise AardvarkError("unable to configure Aardvark for SPI mode")
Esempio n. 5
0
    def spi_mode(self):
        """Enable or disable SPI mode on the Aardvark adapter.

        True - Enable SPI mode
        False - Disable SPI mode

        If SPI is disabled, the pins can be used as GPIO instead.
        """
        config = aardvark_py.aa_configure(self._aardvark_handle, aardvark_py.AA_CONFIG_QUERY)
        _validate_status(config)
        return bool(config & aardvark_py.AA_CONFIG_SPI_MASK)
Esempio n. 6
0
    def spi_mode(self):
        """Enable or disable SPI mode on the Aardvark adapter.

        True - Enable SPI mode
        False - Disable SPI mode

        If SPI is disabled, the pins can be used as GPIO instead.
        """
        config = aardvark_py.aa_configure(self._aardvark_handle,
                                          aardvark_py.AA_CONFIG_QUERY)
        _validate_status(config)
        return bool(config & aardvark_py.AA_CONFIG_SPI_MASK)
Esempio n. 7
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)
Esempio n. 8
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
Esempio n. 9
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
Esempio n. 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

#==========================================================================
# 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.
aa_i2c_bitrate(handle, I2C_BITRATE)

# positive tests
if get_version(handle) <= 0:
    exit()

exit()