示例#1
0
 def i2c_bitrate(self, value):
     if not isinstance(value, int):
         raise TypeError("invalid I2C bitrate value: %s" % value)
     if value > 800:
         raise AardvarkError("unsupported I2C bitrate: %d" % value)
     i2c_bitrate = aardvark_py.aa_i2c_bitrate(self._aardvark_handle, value)
     _validate_status(i2c_bitrate)
示例#2
0
 def i2c_bitrate(self, value):
     if not isinstance(value, int):
         raise TypeError("invalid I2C bitrate value: %s" % value)
     if value > 800:
         raise AardvarkError("unsupported I2C bitrate: %d" % value)
     i2c_bitrate = aardvark_py.aa_i2c_bitrate(self._aardvark_handle, value)
     _validate_status(i2c_bitrate)
示例#3
0
    def i2c_bitrate(self):
        """Set the I2C bit rate in kHz.

        Maximum bit rate is 800kHz.  Minimum bit rate for I2C master is 1kHz.
        """
        i2c_bitrate = aardvark_py.aa_i2c_bitrate(self._aardvark_handle, 0)
        _validate_status(i2c_bitrate)
        return i2c_bitrate
示例#4
0
    def i2c_bitrate(self):
        """Set the I2C bit rate in kHz.

        Maximum bit rate is 800kHz.  Minimum bit rate for I2C master is 1kHz.
        """
        i2c_bitrate = aardvark_py.aa_i2c_bitrate(self._aardvark_handle, 0)
        _validate_status(i2c_bitrate)
        return i2c_bitrate
    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))
示例#6
0
文件: smbus.py 项目: taiwenko/python
 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
示例#7
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
示例#8
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
示例#9
0
def update_aardvark(command, address, Aardvark_in_use):
    """
    Perform the configureation requested by a config command
    
    @param[in]  command:         The configuration command requested 
                                 (string).
    @param[in]  address:         The current I2C slave address in use 
                                 (int).
    @param[in]  AArdvark_in_use: The aardvark port in use 
                                 (aardvark_py.Aardvark).
    @return     (int)            The new I2C address to use 
                                 (potentially unchanged).
    """    
    new_address = address
    # determine the appropriate action to take
    if 'DELAY ' in command:
        delay_time = query_delay_command(command)
        aardvark_py.aa_sleep_ms(delay_time) 
    elif 'ADDRESS ' in command:
        # strip out the number
        address_list = command.split(' ')
        address_hex = address_list[1][0:-1]
        # verify that it is a number and that the beginning of the 
        # command was correct
        if address_hex.startswith('0x') and (len(address_hex) == 4) and \
           pySCPI_config.is_hex(address_hex[2:]) and \
           (address_list[0] == '<ADDRESS'):
            # is a good address
            new_address = int(address_hex,16)
            print 'Changed slave I2C address to ' + address_hex + '.'
        else:
            # the adderss is invlaid
            print '*** The requested ADDRESS command is not valid. '\
                  'Use <ADDRESS 0xYY>***'
        # end if        
    
    elif 'BITRATE ' in command:
        # strip out the number
        speed_list = command.split(' ')
        speed_num = speed_list[1][0:-1] 
        if speed_num.isdigit() and (speed_list[0] == '<BITRATE'):
            # is a good bitrate
            bitrate = aardvark_py.aa_i2c_bitrate(Aardvark_in_use, int(speed_num))
            aardvark_py.aa_sleep_ms(200)             
            print 'Changed I2C bitrate to ' + str(bitrate) + 'kHz.'
        else:
            # the bitrate is invlaid
            print '*** The requested BITRATE command is not valid. '\
                  'Use <BITRATE x>***'
        # end if         
        
    elif 'PULLUPS ' in command:
        # check command
        if command == '<PULLUPS ON>':
            # turn pullups on
            aardvark_py.aa_i2c_pullup(Aardvark_in_use, 
                                      aardvark_py.AA_I2C_PULLUP_BOTH)
            aardvark_py.aa_sleep_ms(200)   
            print 'Turned I2C pullups on.'
        
        elif command == '<PULLUPS OFF>':
            # turn pullups off
            aardvark_py.aa_i2c_pullup(Aardvark_in_use, 
                                      aardvark_py.AA_I2C_PULLUP_NONE)
            aardvark_py.aa_sleep_ms(200)  
            print 'Turned I2C pullups off.'
        
        else:
            print '*** Invalid Pullup Command, use either '\
                  '<PULLUPS ON> or <PULLUPS OFF>***'
        #end if  
        
    else:
        print '*** The configuration command requested in not valid, '\
              'refer to Read Me***'
    # end if  
    
    return new_address
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()

if dev_rev(handle) <= 0:
    exit()

if write_config(handle) <= 0:
    exit()

if read_config(handle) <= 0:
    exit()