⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aamonitor.py

📁 Aardvark Example Source Code Version: 4.00 Date: 2007-04-20 Source code which shows how to use the
💻 PY
字号:
#!/bin/env python
#==========================================================================
# (c) 2004-2005  Total Phase, Inc.
#--------------------------------------------------------------------------
# Project : Aardvark Sample Code
# File    : aamonitor.py
#--------------------------------------------------------------------------
# Perform I2C monitoring functions with the Aardvark I2C/SPI adapter.
#--------------------------------------------------------------------------
# Redistribution and use of this file in source and binary forms, with
# or without modification, are permitted.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#==========================================================================

#==========================================================================
# IMPORTS
#==========================================================================
import sys

from aardvark_py import *


#==========================================================================
# CONSTANTS
#==========================================================================
BUFFER_SIZE      = 32767

INITIAL_TIMEOUT  =  5000
INTERVAL_TIMEOUT =   500


#==========================================================================
# FUNCTIONS
#==========================================================================
def dump (handle):
    # Wait for data on the bus
    print "Waiting %d ms for first transaction..." % INITIAL_TIMEOUT
    result = aa_async_poll(handle, INITIAL_TIMEOUT)
    if (result == AA_ASYNC_NO_DATA):
        print "  no data pending."
        return

    print "  data received"
    
    last_data0 = 0
    last_data1 = 0

    # Loop until aa_async_poll times out
    while 1:
        # Read the next monitor transaction.
        # This function has an internal timeout (see datasheet), though
        # since we have already checked for data using aa_async_poll,
        # the timeout should never be exercised.
        (status, data) = aa_i2c_monitor_read(handle, BUFFER_SIZE)
        
        if (status < 0):
            print "error: %s" % aa_status_string(status)
            return
        
        for i in range(len(data)):
            # Check for start condition
            if (data[i] == AA_I2C_MONITOR_CMD_START):
                sys.stdout.write("\n[S] ")

            # Check for stop condition
            elif (data[i] == AA_I2C_MONITOR_CMD_STOP):
                sys.stdout.write("[P]\n")

            else:
                nack = (data[i] & AA_I2C_MONITOR_NACK)
                if nack:  nack_str = "*"
                else:     nack_str = ""
                
                # 7-bit addresses
                if (last_data0 == AA_I2C_MONITOR_CMD_START and
                    ((data[i] & 0xf8) != 0xf0 or nack)):
                    if (data[i] & 0x01): dir_str = "r"
                    else:                dir_str = "w"
                    sys.stdout.write("<%02x:%s>%s " %
                        ((data[i] & 0xff) >> 1,
                         dir_str,
                         nack_str
                        ))

                # 10-bit addresses
                # See Philips specification for more details.
                elif (last_data1 == AA_I2C_MONITOR_CMD_START and
                     (last_data0 & 0xf8) == 0xf0):
                    if (last_data0 & 0x01): dir_str = "r"
                    else:                   dir_str = "w"
                    sys.stdout.write("<%03x:%s>%s " %
                        (((last_data0 << 7) & 0x300) | (data[i] & 0xff),
                         dir_str,
                         nack_str
                        ))

                # Normal data
                elif (last_data0 != AA_I2C_MONITOR_CMD_START):
                    sys.stdout.write("%02x%s " % (data[i] & 0xff, nack_str))

            last_data1 = last_data0
            last_data0 = data[i]
            sys.stdout.flush()


        print "\nWaiting %d ms for subsequent transaction..." % INTERVAL_TIMEOUT

        # Use aa_async_poll to wait for the next transaction
        result = aa_async_poll(handle, INTERVAL_TIMEOUT)
        if (result == AA_ASYNC_NO_DATA):
            print "  no more data pending."
            break

        print "  data received\n"
            

#==========================================================================
# MAIN PROGRAM
#==========================================================================
if (len(sys.argv) < 2):
    print "usage: aamonitor PORT"
    sys.exit()

port       = int(sys.argv[1])

# 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()
    
# Ensure that the I2C subsystem is enabled
aa_configure(handle,  AA_CONFIG_SPI_I2C)
    
# Disable 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_i2c_pullup(handle, AA_I2C_PULLUP_NONE)

# Disable the Aardvark adapter's power pins.
# 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_target_power(handle, AA_TARGET_POWER_NONE)

# Enable the monitor
result = aa_i2c_monitor_enable(handle)
if (result < 0):
    print "error: %s\n" % aa_status_string(result)
    sys.exit()
    
print "Enabled I2C monitor."

# Watch the I2C port
dump(handle)

# Disable the slave and close the device
aa_i2c_monitor_disable(handle)
aa_close(handle)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -