def close(self):
        "Disconnect from bus adapter"
        _spi_bus.close(self)

        assert self._handle > 0, 'Connection is already close.'
        aa.aa_close(self._handle)
        self._handle = None
示例#2
0
 def __exit__(self, type, value, traceback):
     """
     Ensures that the aardvark port is closed correctly
     
     For use with the 'with' operator
     """      
     if self.port != None:
         aardvark_py.aa_close(self.port)
    def hw_close(self):
        status = 0
        message = "No Error"

        if (self.handle == None) :
            print "Hardware channel is already closed. Ignoring redundant close request\n"
            return

        if (self.openCloseActive == 1) :
            print "Hardware channel open or close in process by another thread. Ignoring redundant close request\n"
 
        # lock the thread incase multiple threads attempt to open, close read or write simultaneously
        with self.activityLock :
            # recheck status within protected space before acting
            # exterior check is to keep multiple redundant requests from queueing
            if (self.handle == None) :
                print "Hardware channel is already closed. Ignoring redundant close request\n"
                return

            if (self.openCloseActive == 1) :
                print "Hardware channel open or close in process by another thread. Ignoring redundant close request\n"

            self.openCloseActive = 1
            
            # Close the I2C hardware interface
            if self.HW_INTERFACE == config.AARDVARK:
                aardvark_py.aa_close(self.handle)
                status = 0
                message = ''
            elif self.HW_INTERFACE == config.FTDI:
                ftdi.ftdi_close(self.handle, self.spihandle)
                status = 0
                message = ''
            elif self.HW_INTERFACE == config.USB_TO_ANY:
                status = -1
                message = 'USB_TO_ANY hardware interface hw_interface_init not implemented\n'
                self.openCloseActive = 0
                return (status, message)
            elif self.HW_INTERFACE == config.USB_EP:
                usbep.usbep_close()
                status = 0
                message = ''
            else:
                status = -1
                message = 'Invalid hardware interface selected in config.py\n'
                self.openCloseActive = 0
                return (status, message)

            self.handle = None
            self.spihandle = None

            self.openCloseActive = 0

        return (status, message)
示例#4
0
 def close(self):
     """Close the Aardvark adapter."""
     if self._aardvark_handle:
         num_closed = aardvark_py.aa_close(self._aardvark_handle)
         if not (num_closed == 1):
             raise AardvarkError("closed %d Aardvark adapters, expected to close only %s" % (num_closed, self._aardvark_handle))
         self._aardvark_handle = None
示例#5
0
 def close(self):
     """Close the Aardvark adapter."""
     if self._aardvark_handle:
         num_closed = aardvark_py.aa_close(self._aardvark_handle)
         if not (num_closed == 1):
             raise AardvarkError(
                 "closed %d Aardvark adapters, expected to close only %s" %
                 (num_closed, self._aardvark_handle))
         self._aardvark_handle = None
    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)
示例#7
0
 def _close(self):
     """(Protected) Closes the device handle opened by __init__(), returning
 the result code.  Called by __del__(), __exit__(), and close().
 """
     if self._open:
         result = api.aa_close(self._handle)
         self._open = False
         self._handle = None
         return result
     else:
         return 0
示例#8
0
文件: smbus.py 项目: taiwenko/python
 def _close(self):
   """(Protected) Closes the device handle opened by __init__(), returning
   the result code.  Called by __del__(), __exit__(), and close().
   """
   if self._open:
     result = api.aa_close(self._handle)
     self._open = False
     self._handle = None
     return result
   else:
     return 0
def Close_Aardvark():
    aardvark_py.aa_close(Aardvark_Handle)
示例#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
示例#11
0
def log_aardvark(directives, filename, gui):
    """
    Write to the slave device using the Aardvark and print to 
    the gui and save the data to a csv log file.
    
    @param[in]  directives:  Instructions to direct the sending of 
                             data (pySCPI_config.write_directives)
    @param[in]  filename:    The absolute directory of the file to write 
                             log to (string).
    @param[in]  gui:         Instance of the gui that this function is 
                             called by (pySCPI_gui.main_gui).
    @return     int(0):      Failed to use Aardvark.
    """     
    # unpack the write directives
    addr = directives.addr
    commands = directives.command_list
    Delay = directives.delay_time
    Ascii_delay = directives.ascii_time
    logging_p = directives.logging_p
    
    # configure the progress bar to be the correct length
    gui.progress.config(maximum = logging_p*10)
    # increment the progress bar every 100 ms
    gui.progress.start(100)
    
    # set up the csv writing output
    csv_output = open(filename, 'wb')
    output_writer = csv.writer(csv_output, delimiter = '\t')
    
    # create the header for the csv file
    csv_line = create_csv_header(commands, gui)
    
    # write the header
    output_writer.writerow(csv_line)     
    
    # configure Aardvark if available
    Aardvark_in_use = configure_aardvark()
    
    # Check to see if an Aardvark was actually found
    if Aardvark_in_use != None:    
        
        # start the loop timer
        start_time = time.time()
        
        # loop until the thread is asked to exit
        while not gui.terminator.kill_event.isSet():
            
            # define variables for the row
            csv_row = []
            dec_addr = addr
            first_timestamp = ''
            
            # iterate through commands and add them to the aardvark file
            command_count = 0
            # commands that start with # are deemed comments
            for command in commands:
                
                # skip comments that start with #
                if command.startswith('#'):
                    command_count += 1
                    continue
                # end if
                
                gui.highlight_line(command_count)
                
                # determine if the command is a configuration command
                if pySCPI_config.is_config(command):
                    # configure the system based on the config command
                    dec_addr = update_aardvark(command, dec_addr, 
                                               Aardvark_in_use)
                    
                else:
                    # Prepare the data for transmission
                    if pySCPI_config.is_raw_write(command):
                        # it is a raw write command to send that
                        send_raw_command(command, Aardvark_in_use)
                        
                    elif pySCPI_config.is_raw_read(command):
                        # it is a rew read command so read the data and 
                        # add it to the csv row
                        csv_row.append(read_raw_command(command, 
                                                        Aardvark_in_use,
                                                        logging = True))
                        
                    else:
                        # it is a normal command
                        send_scpi_command(command, Aardvark_in_use, 
                                          dec_addr)
                    # end if
                # end if
                
                if 'TEL?' in command:
                    # the command is telemetry so log that
                    
                    # is it an ascii command
                    if command.endswith('ascii'):
                        # perform as ascii dealy
                        aardvark_py.aa_sleep_ms(Ascii_delay)
                        
                    else:
                        # perform a regular intermessage delay
                        aardvark_py.aa_sleep_ms(Delay)
                    # end if
                    
                    # define array to read data into
                    data = array('B', [1]*pySCPI_formatting.read_length(command, gui)) 
                    
                    # read from the slave device
                    read_data = aardvark_py.aa_i2c_read(Aardvark_in_use, 
                                                        dec_addr, 
                                                        aardvark_py.AA_I2C_NO_FLAGS, 
                                                        data)
                    
                    # print data
                    pySCPI_formatting.print_read(command, 
                                                 list(read_data[1]), 
                                                 gui)
                    
                    # log data
                    pySCPI_formatting.log_read(command, list(read_data[1]),
                                               csv_row, gui)
                # end if
                
                # print a blank line
                print ''
                
                # intermessage delay
                aardvark_py.aa_sleep_ms(Delay)
                
                # check to see if logging needs to stop
                if gui.terminator.kill_event.isSet():
                    # End if a stop has been issued
                    break            
                # end if
                
                command_count += 1
            # end while
            
            # get the earliest timestamp from the row
            first_timestamp = csv_row[0]
            
            # check to see if it is actually a timestamp
            if type(first_timestamp) == float:
                # it is a timestamp so convert it to a byte array of 
                # the  elapsed time in hundredths of a second
                timestamp_list = [ord(x) for x in '[1:' + 
                                  str(int(first_timestamp*100)) + ']']
                
                # convert the byte list to an ascii time
                timestamp_string = pySCPI_formatting.get_ascii_time(timestamp_list)
                
                # insert this timestamp at the beginning of the row
                csv_row.insert(0,timestamp_string)
                
            else:
                # the first entry is not a timestamp so leave it blank
                csv_row.insert(0,'-')
            # end if
            
            # write the row to the log file
            output_writer.writerow(csv_row)      
            
            # unhighlight the last row
            gui.highlight_line()                 
            
            # pace the loop to the correct logging period
            while (time.time() - start_time) < logging_p:
                # sleep to prevent resource hogging
                time.sleep(0.1)
                
                # check to see if logging should end
                if gui.terminator.kill_event.isSet():
                    # it should so exit
                    break
                # end if
            # end while
            
            start_time = time.time()        
            
            # check to see if we can clear the gui for the next period
            if not gui.terminator.root_destroyed:        
                # clear the output display on the GUI
                gui.output_clear()
            # end if
            
            gui.progress.config(value = 0)
        # end while
        gui.progress.stop()
    
        # close the csv file
        csv_output.close()   
        
        # unhighlight the last row
        gui.highlight_line()        
        
        # close the aardvark
        aardvark_py.aa_close(Aardvark_in_use)
        print 'Aardvark logging finished'
    
    else: 
        # no aardvark connection was established
        return 0
示例#12
0
def write_aardvark(directives, gui):
    """
    Write to the slave device using the Aardvark and print its results 
    to the GUI.
    
    @param[in]  directives:  Instructions to direct the sending of 
                             data (pySCPI_config.write_directives)
    @param[in]  gui:         Instance of the gui that this function is 
                             called by (pySCPI_gui.main_gui).
    @return     int(0):      Failed to use Aardvark.
                None         Otherwise.
    """    
    # local copy of the write directives
    dec_addr = directives.addr
    commands = directives.command_list
    Delay = directives.delay_time
    Ascii_delay = directives.ascii_time
    
    # configure the progress bar
    gui.progress.config(maximum = len([c for c in commands if not c.startswith('#')]))
    
    # configure Aardvark if available
    Aardvark_in_use = configure_aardvark()
    
    # Check to see if an Aardvark was actually found
    if Aardvark_in_use != None:
        # iterate through commands and add them to the aardvark file
        command_count = 0
        for command in commands:
            
            # ignore comment lines that start with a #
            if command.startswith('#'):
                command_count +=1
                continue
            # end if
            
            # find the line of execution and highlight it
            gui.highlight_line(command_count)
            
            # determine if the command is a configuration command
            if pySCPI_config.is_config(command):
                # configure the system based on the config command
                dec_addr = update_aardvark(command, dec_addr, Aardvark_in_use)
                
            else:
                # Prepare the data for transmission
                if pySCPI_config.is_raw_write(command):
                    # it is a raw write command to send that
                    send_raw_command(command, Aardvark_in_use)
                    
                elif pySCPI_config.is_raw_read(command):
                    # it is a rew read command so read the data
                    read_raw_command(command, Aardvark_in_use)
                    
                else:
                    # it is a normal command
                    send_scpi_command(command, Aardvark_in_use, dec_addr)
                # end if
            # end if
            
            if 'TEL?' in command:
                # an I2C read has been requested
                
                # delay before reading the data
                if command.endswith('ascii'):
                    # sleep a different amount if ascii was requested
                    aardvark_py.aa_sleep_ms(Ascii_delay)
                    
                else:
                    aardvark_py.aa_sleep_ms(Delay)
                # end if
                
                # define array to read data into
                data = array('B', [1]*pySCPI_formatting.read_length(command, gui)) 
                
                # read from the slave device
                read_data = aardvark_py.aa_i2c_read(Aardvark_in_use, 
                                                    dec_addr, 
                                                    aardvark_py.AA_I2C_NO_FLAGS, 
                                                    data)
                
                # print the recieved data
                pySCPI_formatting.print_read(command, list(read_data[1]), 
                                             gui)
                # end if
            # end if
            
            # print an empty line
            print ''
            
            # intermessage delay
            aardvark_py.aa_sleep_ms(Delay)
            
            if gui.terminator.kill_event.isSet():
                # this thread has been asked to terminate
                break
            # end
            
            # increment the progress bar
            gui.progress.step()
            
            # increment the command counter
            command_count += 1
        # end for
        
        # unhighlight the last row
        gui.highlight_line()
        
        # close the AArdvark device
        aardvark_py.aa_close(Aardvark_in_use)
        print 'Aardvark communications finished'
    
    else:
        return 0
示例#13
0
	def die(self):
		aa_py.aa_close(self.aa)
# Test this with an oscilloscope or Beagle protocol analyzer.
if set_wakeup_delay(handle, 20000) <= 0:
    exit()

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

if set_wakeup_delay(handle, 1000) <= 0:
    exit()

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

#negative tests
# Decrease SWI timeout so that Nonce command fails.
set_swi_timeout(handle, 10)
nonce(handle)

# We forced a premature timeout. Therefore the device did not
# execute the Idle command the firmware has sent.
# Wait for the device to go into Sleep mode.
aa_sleep_ms(1500)

# Reverse SWI timeout.
set_swi_timeout(handle, 500)
nonce(handle)
mac(handle)

# Close the device and exit
aa_close(handle)