示例#1
0
    /*=====================================================================
     | MAIN PROGRAM
     | ====================================================================*/
    public static void Main(string[] args)
    {
        ushort[] ports     = new ushort[16];
        uint[]   uniqueIds = new uint[16];
        int      numElem   = 16;
        int      i;

        // Find all the attached devices
        int count = AardvarkApi.aa_find_devices_ext(numElem, ports,
                                                    numElem, uniqueIds);

        Console.WriteLine("{0} device(s) found:", count);
        if (count > numElem)
        {
            count = numElem;
        }

        // Print the information on each device
        for (i = 0; i < count; ++i)
        {
            // Determine if the device is in-use
            string status = "(avail) ";
            if ((ports[i] & AardvarkApi.AA_PORT_NOT_FREE) != 0)
            {
                ports[i] &= unchecked ((ushort)~AardvarkApi.AA_PORT_NOT_FREE);
                status    = "(in-use)";
            }

            // Display device port number, in-use status, and serial number
            uint id = unchecked ((uint)uniqueIds[i]);
            Console.WriteLine("    port={0,-3} {1} ({2:d4}-{3:d6})",
                              ports[i], status, id / 1000000,
                              id % 1000000);
        }
    }
示例#2
0
 public override bool Open()
 {
     base.ConnectErrorString = string.Empty;
     try
     {
         this._I2CHandle = AardvarkApi.aa_open(this._I2CDevicePortNum);
     }
     catch
     {
         base.ConnectErrorString = string.Format("Unable to open Aardvark device on port {0}, error: {1}", this._I2CDevicePortNum, AardvarkApi.aa_status_string(this._I2CHandle));
         return(false);
     }
     if (this._I2CHandle <= 0)
     {
         base.ConnectErrorString = string.Format("Unable to open Aardvark device on port {0}, Error : {1}", this._I2CDevicePortNum, AardvarkApi.aa_status_string(this._I2CHandle));
         return(false);
     }
     try
     {
         this._I2CHandleMaster = AardvarkApi.aa_open(this._I2CDevicePortNumMaster);
     }
     catch
     {
         base.ConnectErrorString = string.Format("Unable to open Aardvark device on port {0}, error: {1}", this._I2CDevicePortNumMaster, AardvarkApi.aa_status_string(this._I2CHandleMaster));
         return(false);
     }
     if (this._I2CHandleMaster <= 0)
     {
         base.ConnectErrorString = string.Format("Unable to open Aardvark device on port {0}, Error : {1}", this._I2CDevicePortNumMaster, AardvarkApi.aa_status_string(this._I2CHandleMaster));
         return(false);
     }
     return(true);
 }
示例#3
0
        private int _Mcp2221Read16(byte devAddr, byte[] regAddr, byte length, byte[] data)
        {
            byte[] reg = { regAddr[0], regAddr[1] };

            AardvarkApi.aa_i2c_write(iHandler, devAddr, AardvarkI2cFlags.AA_I2C_NO_STOP, 2, reg);

            return(AardvarkApi.aa_i2c_read(iHandler, devAddr, AardvarkI2cFlags.AA_I2C_NO_FLAGS, length, data));
        }
示例#4
0
        private int _AardvarkRead(byte devAddr, byte regAddr, byte length, byte[] data)
        {
            byte[] reg = { regAddr };

            AardvarkApi.aa_i2c_write(iHandler, devAddr, AardvarkI2cFlags.AA_I2C_NO_STOP, 1, reg);

            return(AardvarkApi.aa_i2c_read(iHandler, devAddr, AardvarkI2cFlags.AA_I2C_NO_FLAGS, length, data));
        }
示例#5
0
        private int _AardvarkWrite(byte devAddr, byte regAddr, byte length, byte[] data)
        {
            byte[] buf = new byte[length + 1];

            buf[0] = regAddr;
            Array.Copy(data, 0, buf, 1, length);

            AardvarkApi.aa_i2c_write(iHandler, devAddr, AardvarkI2cFlags.AA_I2C_NO_FLAGS, Convert.ToByte(length + 1), buf);

            AardvarkApi.aa_sleep_ms(100);

            return(0);
        }
示例#6
0
    /*=====================================================================
     | FUNCTIONS
     | ====================================================================*/
    static void _writeMemory(int handle, short addr, short length, bool zero)
    {
        short i, n;

        byte[] dataOut        = null;
        byte[] dataIn         = null;
        byte[] writeEnableBuf = new byte[1];

        // Write to the SPI EEPROM
        //
        // The AT25080A EEPROM has 32 byte pages.  Data can written
        // in pages, to reduce the number of overall SPI transactions
        // executed through the Aardvark adapter.
        n = 0;
        while (n < length)
        {
            // Calculate the amount of data to be written this iteration
            // and make sure dataOut is just large enough for it.
            int size = Math.Min(((addr & (PAGE_SIZE - 1)) ^ (PAGE_SIZE - 1)) + 1,
                                length - n);
            size += 3;  // Add 3 for the address and command fields.
            if (dataOut == null || dataOut.Length != size)
            {
                dataOut = new byte[size];
                dataIn  = new byte[size];
            }

            // Send write enable command
            writeEnableBuf[0] = 0x06;
            AardvarkApi.aa_spi_write(handle, (ushort)size, writeEnableBuf,
                                     (ushort)size, dataIn);

            // Assemble write command and address
            dataOut[0] = 0x02;
            dataOut[1] = (byte)((addr >> 8) & 0xff);
            dataOut[2] = (byte)((addr >> 0) & 0xff);

            // Assemble a page of data
            i = 3;
            do
            {
                dataOut[i++] = zero ? (byte)0 : (byte)n;
                ++addr; ++n;
            } while (n < length && (addr & (PAGE_SIZE - 1)) != 0);

            // Write the transaction
            AardvarkApi.aa_spi_write(handle, (ushort)size, dataOut,
                                     (ushort)size, dataIn);
            AardvarkApi.aa_sleep_ms(10);
        }
    }
示例#7
0
    static void _readMemory(int handle, byte device, byte addr,
                            short length)
    {
        int count, i;

        byte[] dataOut = { addr };
        byte[] dataIn  = new byte[length];

        // Write the address
        AardvarkApi.aa_i2c_write(handle, device,
                                 AardvarkI2cFlags.AA_I2C_NO_STOP,
                                 (ushort)length, dataOut);

        count = AardvarkApi.aa_i2c_read(handle, device,
                                        AardvarkI2cFlags.AA_I2C_NO_FLAGS,
                                        (ushort)length, dataIn);
        if (count < 0)
        {
            Console.WriteLine("error: {0}\n",
                              AardvarkApi.aa_status_string(count));
            return;
        }
        if (count == 0)
        {
            Console.WriteLine("error: no bytes read");
            Console.WriteLine("  are you sure you have the right slave address?");
            return;
        }
        else if (count != length)
        {
            Console.WriteLine("error: read {0} bytes (expected {1})",
                              count, length);
        }

        // Dump the data to the screen
        Console.Write("\nData read from device:");
        for (i = 0; i < count; ++i)
        {
            if ((i & 0x0f) == 0)
            {
                Console.Write("\n{0:x4}:  ", addr + i);
            }
            Console.Write("{0:x2} ", dataIn[i] & 0xff);
            if (((i + 1) & 0x07) == 0)
            {
                Console.Write(" ");
            }
        }
        Console.WriteLine();
    }
示例#8
0
    static void _readMemory(int handle, short addr, short length)
    {
        int count;
        int i;

        byte[] dataOut = new byte[length + 3];
        byte[] dataIn  = new byte[length + 3];

        // Assemble read command and address
        dataOut[0] = 0x03;
        dataOut[1] = (byte)((addr >> 8) & 0xff);
        dataOut[2] = (byte)((addr >> 0) & 0xff);

        // Write length+3 bytes for data plus command and 2 address bytes
        count = AardvarkApi.aa_spi_write(handle, (ushort)(length + 3), dataOut,
                                         (ushort)(length + 3), dataIn);

        if (count < 0)
        {
            Console.WriteLine("error: {0}",
                              AardvarkApi.aa_status_string(count));
        }
        else if (count != length + 3)
        {
            Console.WriteLine("error: read {0} bytes (expected {1})",
                              count - 3, length);
        }


        // Dump the data to the screen
        Console.Write("\nData read from device:");
        for (i = 0; i < length; ++i)
        {
            if ((i & 0x0f) == 0)
            {
                Console.Write("\n{0:x4}:  ", addr + i);
            }
            Console.Write("{0:x2} ", dataIn[i + 3] & 0xff);
            if (((i + 1) & 0x07) == 0)
            {
                Console.Write(" ");
            }
        }
        Console.WriteLine();
    }
示例#9
0
        private int _AardvarkDisconnect()
        {
            if (iHandler <= 0)
            {
                return(0);
            }

            if (as_atAdapterType != AdapterSelector.AdapterType.AS_AT_AARDVARK)
            {
                return(-1);
            }

            AardvarkApi.aa_close(iHandler);

            iHandler         = -1;
            as_atAdapterType = AdapterSelector.AdapterType.AS_AT_DUMMY;

            return(0);
        }
示例#10
0
        public int ReadRawApi(byte devAddr, byte length, byte[] data)
        {
            int result;

            switch (as_atAdapterType)
            {
            case AdapterSelector.AdapterType.AS_AT_DUMMY:
                break;

            case AdapterSelector.AdapterType.AS_AT_AARDVARK:
                if (iHandler <= 0)
                {
                    if (ConnectApi() < 0)
                    {
                        MessageBox.Show("I2cMasterConnectApi() fail!!");
                    }
                    return(-1);
                }
                return(AardvarkApi.aa_i2c_read(iHandler, devAddr, AardvarkI2cFlags.AA_I2C_NO_FLAGS, length, data));

            case AdapterSelector.AdapterType.AS_AT_MCP2221:
                if (ipHandler == IntPtr.Zero)
                {
                    if (ConnectApi() < 0)
                    {
                        MessageBox.Show("I2cMasterConnectApi() fail!!");
                    }
                    return(-1);
                }
                result = Mcp2221Api.Mcp2221I2cReadApi(ipHandler, length, devAddr, 1, data);
                if (result < 0)
                {
                    return(-3);
                }

                return(length);

            default:
                break;
            }

            return(0);
        }
示例#11
0
    public const int BUS_TIMEOUT = 150;  // ms


    /*=====================================================================
     | FUNCTION
     | ====================================================================*/
    static void _writeMemory(int handle, byte device, byte addr,
                             short length, bool zero)
    {
        short i, n;

        byte[] dataOut = null;

        // Write to the I2C EEPROM
        //
        // The AT24C02 EEPROM has 8 byte pages.  Data can written
        // in pages, to reduce the number of overall I2C transactions
        // executed through the Aardvark adapter.
        n = 0;
        while (n < length)
        {
            // Calculate the amount of data to be written this iteration
            // and make sure dataOut is just large enough for it.
            int size = Math.Min(((addr & (PAGE_SIZE - 1)) ^ (PAGE_SIZE - 1)) + 1,
                                length - n);
            size++;  // Add 1 for the address field
            if (dataOut == null || dataOut.Length != size)
            {
                dataOut = new byte[size];
            }

            // Fill the packet with data
            dataOut[0] = addr;

            // Assemble a page of data
            i = 1;
            do
            {
                dataOut[i++] = zero ? (byte)0 : (byte)n;
                ++addr; ++n;
            } while (n < length && (addr & (PAGE_SIZE - 1)) != 0);

            // Write the address and data
            AardvarkApi.aa_i2c_write(handle, device,
                                     AardvarkI2cFlags.AA_I2C_NO_FLAGS,
                                     (ushort)size, dataOut);
            AardvarkApi.aa_sleep_ms(10);
        }
    }
示例#12
0
            public override bool WriteData(byte[] msg)
            {
                bool flag = false;

                if (!this.IsOpen())
                {
                    return(flag);
                }
                try
                {
                    AardvarkApi.aa_i2c_write(this._I2CHandleMaster, this._I2CMasterAddress, AardvarkI2cFlags.AA_I2C_NO_FLAGS, (short)msg.Length, msg);
                    return(true);
                }
                catch (Exception exception)
                {
                    MessageBox.Show("Exception in I2C Write: " + exception.Message);
                    return(false);
                }
            }
示例#13
0
        public int SetTimeoutApi(UInt16 timeout)
        {
            switch (as_atAdapterType)
            {
            case AdapterSelector.AdapterType.AS_AT_DUMMY:
                break;

            case AdapterSelector.AdapterType.AS_AT_AARDVARK:
                if (iHandler <= 0)
                {
                    if (ConnectApi() < 0)
                    {
                        MessageBox.Show("I2cMasterConnectApi() fail!!");
                    }
                    return(-1);
                }
                return(AardvarkApi.aa_i2c_bus_timeout(iHandler, timeout));

            case AdapterSelector.AdapterType.AS_AT_MCP2221:
                if (timeout > 0xFF)
                {
                    return(-1);
                }

                if (ipHandler == IntPtr.Zero)
                {
                    if (ConnectApi() < 0)
                    {
                        MessageBox.Show("I2cMasterConnectApi() fail!!");
                    }
                    return(-1);
                }

                return(Mcp2221Api.Mcp2221SetAdvancedCommParamsApi(ipHandler, Convert.ToByte(timeout), 1));

            default:
                break;
            }

            return(-1);
        }
示例#14
0
 public void init()
 {
     byte[] buffer = new byte[0x1a];
     try
     {
         AardvarkApi.aa_configure(this._I2CHandle, AardvarkConfig.AA_CONFIG_SPI_I2C);
         AardvarkApi.aa_target_power(this._I2CHandle, 3);
         AardvarkApi.aa_i2c_pullup(this._I2CHandle, 3);
         AardvarkApi.aa_i2c_bitrate(this._I2CHandle, 800);
         for (int i = 0; i < 0x1a; i++)
         {
             buffer[i] = (byte)(0x41 + i);
         }
         AardvarkApi.aa_i2c_slave_set_response(this._I2CHandle, 0x1a, buffer);
         AardvarkApi.aa_i2c_slave_enable(this._I2CHandle, this._I2CSlaveAddress, 0, 0);
     }
     catch (Exception exception)
     {
         MessageBox.Show("Exception in I2C Read Init: " + exception.Message);
     }
 }
示例#15
0
        public int UpdateAardvarkAdapterApi()
        {
            string tmp;
            int    numElem = 16;
            int    i, count;

            lAardvarkAdapter.Clear();

            // Find all the attached devices
            count = AardvarkApi.aa_find_devices_ext(numElem, aardvarkPorts, numElem, aardvarkUniqueIds);

            if (count > numElem)
            {
                MessageBox.Show("Find " + count + " device!!");
                count = numElem;
            }

            if (count == 0)
            {
                lAardvarkAdapter.Add("Cannot find any adapter!!");
                return(0);
            }

            for (i = 0; i < count; i++)
            {
                if ((aardvarkPorts[i] & AardvarkApi.AA_PORT_NOT_FREE) != 0)
                {
                    aardvarkPorts[i] &= unchecked ((ushort)~AardvarkApi.AA_PORT_NOT_FREE);
                    tmp = string.Format("{0,-3}: {1:d4}-{2:d6} {3}", aardvarkPorts[i], aardvarkUniqueIds[i] / 1000000, aardvarkUniqueIds[i] % 1000000, "(Busy)");
                }
                else
                {
                    tmp = string.Format("{0,-3}: {1:d4}-{2:d6}", aardvarkPorts[i], aardvarkUniqueIds[i] / 1000000, aardvarkUniqueIds[i] % 1000000);
                }

                lAardvarkAdapter.Add(tmp);
            }

            return(0);
        }
示例#16
0
 public override void Close()
 {
     try
     {
         AardvarkApi.aa_i2c_slave_disable(this._I2CHandle);
         AardvarkApi.aa_close(this._I2CHandle);
     }
     catch (Exception exception)
     {
         MessageBox.Show("Exception in I2C Read Close: " + exception.Message);
     }
     this._I2CHandle = -1;
     try
     {
         AardvarkApi.aa_close(this._I2CHandleMaster);
     }
     catch (Exception exception2)
     {
         MessageBox.Show("Exception in I2C Write Close: " + exception2.Message);
     }
     this._I2CHandleMaster = -1;
 }
示例#17
0
        public int WriteRawApi(byte devAddr, byte length, byte[] data)
        {
            switch (as_atAdapterType)
            {
            case AdapterSelector.AdapterType.AS_AT_DUMMY:
                break;

            case AdapterSelector.AdapterType.AS_AT_AARDVARK:
                if (iHandler <= 0)
                {
                    if (ConnectApi() < 0)
                    {
                        MessageBox.Show("I2cMasterConnectApi() fail!!");
                    }
                    return(-1);
                }
                AardvarkApi.aa_i2c_write(iHandler, devAddr, AardvarkI2cFlags.AA_I2C_NO_FLAGS, length, data);
                AardvarkApi.aa_sleep_ms(100);
                return(0);

            case AdapterSelector.AdapterType.AS_AT_MCP2221:
                if (ipHandler == IntPtr.Zero)
                {
                    if (ConnectApi() < 0)
                    {
                        MessageBox.Show("I2cMasterConnectApi() fail!!");
                    }
                    return(-1);
                }
                Mcp2221Api.Mcp2221I2cWriteApi(ipHandler, Convert.ToUInt32(length), devAddr, 1, data);
                return(0);

            default:
                break;
            }

            return(0);
        }
示例#18
0
        private int _AardvarkConnect(int port)
        {
            int bitrate;

            if ((port < 0) || (port > 16))
            {
                return(-1);
            }

            if (iHandler > 0)
            {
                goto Exit;
            }

            iHandler = AardvarkApi.aa_open(port);
            if (iHandler <= 0)
            {
                MessageBox.Show("Please check I2C adapter connect!!");
                goto Error;
            }

            // Ensure that the I2C subsystem is enabled
            AardvarkApi.aa_configure(iHandler, AardvarkConfig.AA_CONFIG_SPI_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.
            AardvarkApi.aa_i2c_pullup(iHandler, AardvarkApi.AA_I2C_PULLUP_BOTH);

            // Set the bitrate
            bitrate = AardvarkApi.aa_i2c_bitrate(iHandler, iBitrate);

Exit:
            return(iHandler);

Error:
            return(-1);
        }
示例#19
0
    private const int I2C_BITRATE = 100; // kHz


    /*=====================================================================
     | STATIC FUNCTIONS
     | ====================================================================*/
    public static int FlashLights(int handle)
    {
        int res, i;

        byte[] data_out = new byte[2];

        // Configure I/O expander lines as outputs
        data_out[0] = 0x03;
        data_out[1] = 0x00;
        res         = AardvarkApi.aa_i2c_write(handle, 0x38,
                                               AardvarkI2cFlags.AA_I2C_NO_FLAGS,
                                               2, data_out);
        if (res < 0)
        {
            return(res);
        }

        if (res == 0)
        {
            Console.WriteLine("error: slave device 0x38 not found");
            return(0);
        }

        // Turn lights on in sequence
        i = 0xff;
        do
        {
            i           = ((i << 1) & 0xff);
            data_out[0] = 0x01;
            data_out[1] = (byte)i;
            res         = AardvarkApi.aa_i2c_write(handle, 0x38,
                                                   AardvarkI2cFlags.AA_I2C_NO_FLAGS,
                                                   2, data_out);
            if (res < 0)
            {
                return(res);
            }
            AardvarkApi.aa_sleep_ms(70);
        } while (i != 0);

        // Leave lights on for 100 ms
        AardvarkApi.aa_sleep_ms(100);

        // Turn lights off in sequence
        i = 0x00;
        do
        {
            i           = ((i << 1) | 0x01);
            data_out[0] = 0x01;
            data_out[1] = (byte)i;
            res         = AardvarkApi.aa_i2c_write(handle, 0x38,
                                                   AardvarkI2cFlags.AA_I2C_NO_FLAGS,
                                                   2, data_out);
            if (res < 0)
            {
                return(res);
            }
            AardvarkApi.aa_sleep_ms(70);
        } while (i != 0xff);

        AardvarkApi.aa_sleep_ms(100);

        // Configure I/O expander lines as inputs
        data_out[0] = 0x03;
        data_out[1] = 0xff;
        res         = AardvarkApi.aa_i2c_write(handle, 0x38,
                                               AardvarkI2cFlags.AA_I2C_NO_FLAGS,
                                               2, data_out);
        if (res < 0)
        {
            return(res);
        }

        return(0);
    }
示例#20
0
    /*=====================================================================
     | MAIN PROGRAM
     | ====================================================================*/
    public static void Main(string[] args)
    {
        int handle;
        int port    = 0;
        int bitrate = 100;
        int res     = 0;

        if (args.Length != 1)
        {
            Console.WriteLine("usage: aalights PORT");
            return;
        }

        try {
            port = Convert.ToInt32(args[0]);
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid port number");
            return;
        }

        // Open the device
        handle = AardvarkApi.aa_open(port);
        if (handle <= 0)
        {
            Console.WriteLine("Unable to open Aardvark device on port {0}",
                              port);
            Console.WriteLine("error: {0}",
                              AardvarkApi.aa_status_string(handle));
            return;
        }

        // Enable logging
        FileStream logfile   = null;
        bool       isLogging = true;

        try {
            logfile = new FileStream("log.txt", FileMode.Append,
                                     FileAccess.Write);
            AardvarkApi.aa_log(handle, 3, (int)logfile.Handle);
        }
        catch (Exception) {
            isLogging = false;
        }

        // Ensure that the I2C subsystem is enabled
        AardvarkApi.aa_configure(handle, AardvarkConfig.AA_CONFIG_SPI_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.
        AardvarkApi.aa_i2c_pullup(handle, AardvarkApi.AA_I2C_PULLUP_BOTH);

        // 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.
        AardvarkApi.aa_target_power(handle,
                                    AardvarkApi.AA_TARGET_POWER_BOTH);

        // Set the bitrate
        bitrate = AardvarkApi.aa_i2c_bitrate(handle, I2C_BITRATE);
        Console.WriteLine("Bitrate set to {0} kHz", bitrate);

        res = AALights.FlashLights(handle);
        if (res < 0)
        {
            Console.WriteLine("error: {0}", AardvarkApi.aa_status_string(res));
        }

        // Close the device and exit
        AardvarkApi.aa_close(handle);

        // Close the logging file
        if (isLogging)
        {
            logfile.Close();
        }
    }
示例#21
0
    /*=====================================================================
     | MAIN PROGRAM
     | ====================================================================*/
    public static void Main(string[] args)
    {
        int  handle;
        int  port = 0;
        byte addr = 0;

        string filename;

        int bitrate;

        if (args.Length != 3)
        {
            Console.WriteLine("usage: aai2c_file PORT SLAVE_ADDR filename");
            Console.WriteLine("  SLAVE_ADDR is the target slave address");
            Console.WriteLine();
            Console.WriteLine("  'filename' should contain data to be sent");
            Console.WriteLine("  to the downstream i2c device");
            return;
        }

        // Parse the port argument
        try {
            port = Convert.ToInt32(args[0]);
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid port");
            return;
        }

        // Parse the device address argument
        try {
            if (args[1].StartsWith("0x"))
            {
                addr = Convert.ToByte(args[1].Substring(2), 16);
            }
            else
            {
                addr = Convert.ToByte(args[1]);
            }
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid device addr");
            return;
        }

        filename = args[2];

        // Open the device
        handle = AardvarkApi.aa_open(port);
        if (handle <= 0)
        {
            Console.WriteLine("Unable to open Aardvark device on port {0}",
                              port);
            Console.WriteLine("error: {0}",
                              AardvarkApi.aa_status_string(handle));
            return;
        }

        // Ensure that the I2C subsystem is enabled
        AardvarkApi.aa_configure(handle, AardvarkConfig.AA_CONFIG_SPI_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.
        AardvarkApi.aa_i2c_pullup(handle, AardvarkApi.AA_I2C_PULLUP_BOTH);

        // Enable 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.
        AardvarkApi.aa_target_power(handle, AardvarkApi.AA_TARGET_POWER_BOTH);

        // Setup the bitrate
        bitrate = AardvarkApi.aa_i2c_bitrate(handle, I2C_BITRATE);
        Console.WriteLine("Bitrate set to {0} kHz", bitrate);

        blastBytes(handle, addr, filename);

        // Close the device
        AardvarkApi.aa_close(handle);
    }
示例#22
0
    /*=====================================================================
     | FUNCTIONS
     | ====================================================================*/
    static void dump(int handle, int timeout_ms)
    {
        int transNum = 0;
        int result;

        byte[] dataIn = new byte[BUFFER_SIZE];

        Console.WriteLine("Watching slave SPI data...");

        // Wait for data on bus
        result = AardvarkApi.aa_async_poll(handle, timeout_ms);
        if (result != AardvarkApi.AA_ASYNC_SPI)
        {
            Console.WriteLine("No data available.");
            return;
        }
        Console.WriteLine();

        // Loop until aa_spi_slave_read times out
        for (;;)
        {
            int numRead;

            // Read the SPI message.
            // This function has an internal timeout (see datasheet).
            // To use a variable timeout the function aa_async_poll could
            // be used for subsequent messages.
            numRead = AardvarkApi.aa_spi_slave_read(
                handle, unchecked ((ushort)BUFFER_SIZE), dataIn);

            if (numRead < 0 &&
                numRead != (int)AardvarkStatus.AA_SPI_SLAVE_TIMEOUT)
            {
                Console.WriteLine("error: {0}",
                                  AardvarkApi.aa_status_string(numRead));
                return;
            }
            else if (numRead == 0 ||
                     numRead == (int)AardvarkStatus.AA_SPI_SLAVE_TIMEOUT)
            {
                Console.WriteLine("No more data available from SPI master.");
                return;
            }
            else
            {
                int i;
                // Dump the data to the screen
                Console.WriteLine("*** Transaction #{0:d2}", transNum);
                Console.Write("Data read from device:");
                for (i = 0; i < numRead; ++i)
                {
                    if ((i & 0x0f) == 0)
                    {
                        Console.Write("\n{0:x4}:  ", i);
                    }
                    Console.Write("{0:x2} ", dataIn[i] & 0xff);
                    if (((i + 1) & 0x07) == 0)
                    {
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
                Console.WriteLine();

                ++transNum;
            }
        }
    }
示例#23
0
    /*=====================================================================
     | MAIN PROGRAM
     | ====================================================================*/
    static public void Main(string[] args)
    {
        int handle;

        int port      = 0;
        int mode      = 0;
        int timeoutMs = 0;

        byte[] slaveResp = new byte[SLAVE_RESP_SIZE];
        int    i;

        if (args.Length != 3)
        {
            Console.WriteLine("usage: aaspi_slave PORT MODE TIMEOUT_MS");
            Console.WriteLine("  mode 0 : pol = 0, phase = 0");
            Console.WriteLine("  mode 1 : pol = 0, phase = 1");
            Console.WriteLine("  mode 2 : pol = 1, phase = 0");
            Console.WriteLine("  mode 3 : pol = 1, phase = 1");
            Console.WriteLine();
            Console.WriteLine("  The timeout value specifies the time to");
            Console.WriteLine("  block until the first packet is received.");
            Console.WriteLine("  If the timeout is -1, the program will");
            Console.WriteLine("  block indefinitely.");
            return;
        }

        // Parse the port argument
        try {
            port = Convert.ToInt32(args[0]);
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid port");
            return;
        }

        // Parse the mode argument
        try {
            mode = Convert.ToInt32(args[1]) & 0x3;
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid mode");
            return;
        }

        // Parse the timeout argument
        try {
            timeoutMs = Convert.ToInt32(args[2]);
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid timeout value");
            return;
        }

        // Open the device
        handle = AardvarkApi.aa_open(port);
        if (handle <= 0)
        {
            Console.WriteLine("Unable to open Aardvark device on port {0}",
                              port);
            Console.WriteLine("error: {0}",
                              AardvarkApi.aa_status_string(handle));
            return;
        }

        // Ensure that the SPI subsystem is enabled
        AardvarkApi.aa_configure(handle, AardvarkConfig.AA_CONFIG_SPI_I2C);

        // 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.
        AardvarkApi.aa_target_power(handle, AardvarkApi.AA_TARGET_POWER_NONE);

        // Setup the clock phase
        AardvarkApi.aa_spi_configure(handle, (AardvarkSpiPolarity)(mode >> 1),
                                     (AardvarkSpiPhase)(mode & 1),
                                     AardvarkSpiBitorder.AA_SPI_BITORDER_MSB);

        // Set the slave response
        for (i = 0; i < SLAVE_RESP_SIZE; ++i)
        {
            slaveResp[i] = (byte)('A' + i);
        }

        AardvarkApi.aa_spi_slave_set_response(handle, SLAVE_RESP_SIZE,
                                              slaveResp);

        // Enable the slave
        AardvarkApi.aa_spi_slave_enable(handle);

        // Watch the SPI port
        dump(handle, timeoutMs);

        // Disable the slave and close the device
        AardvarkApi.aa_spi_slave_disable(handle);
        AardvarkApi.aa_close(handle);
    }
示例#24
0
    /*=====================================================================
     | MAIN PROGRAM
     | ====================================================================*/
    public static void Main(string[] args)
    {
        int   handle;
        int   port    = 0;
        int   bitrate = 100;
        int   mode    = 0;
        short addr;
        short length;

        if (args.Length != 6)
        {
            Console.WriteLine(
                "usage: aaspi_eeprom PORT BITRATE read  MODE ADDR LENGTH");
            Console.WriteLine(
                "usage: aaspi_eeprom PORT BITRATE write MODE ADDR LENGTH");
            Console.WriteLine(
                "usage: aaspi_eeprom PORT BITRATE zero  MODE ADDR LENGTH");
            Console.WriteLine("  mode 0 : pol = 0, phase = 0");
            Console.WriteLine("  mode 3 : pol = 1, phase = 1");
            Console.WriteLine("  modes 1 and 2 are not supported");
            return;
        }

        string command = args[2];

        // Parse the port argument
        try {
            port = Convert.ToInt32(args[0]);
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid port number");
            return;
        }

        // Parse the bitrate argument
        try {
            bitrate = Convert.ToInt32(args[1]);
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid bitrate");
            return;
        }

        // Parse the mode argument
        try {
            mode = Convert.ToInt32(args[3]) & 0x3;
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid mode");
            return;
        }

        // Parse the memory offset argument
        try {
            if (args[4].StartsWith("0x"))
            {
                addr = Convert.ToByte(args[4].Substring(2), 16);
            }
            else
            {
                addr = Convert.ToByte(args[4]);
            }
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid memory addr");
            return;
        }

        // Parse the length
        try {
            length = Convert.ToInt16(args[5]);
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid length");
            return;
        }

        if (mode == 1 || mode == 2)
        {
            Console.WriteLine(
                "error: spi modes 1 and 2 are not supported by the AT25080A");
            return;
        }

        // Open the device
        handle = AardvarkApi.aa_open(port);
        if (handle <= 0)
        {
            Console.WriteLine("Unable to open Aardvark device on port {0}",
                              port);
            Console.WriteLine("error: {0}",
                              AardvarkApi.aa_status_string(handle));
            return;
        }

        // Ensure that the SPI subsystem is enabled.
        AardvarkApi.aa_configure(handle, AardvarkConfig.AA_CONFIG_SPI_I2C);

        // Power the EEPROM 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_target_power(handle, AA_TARGET_POWER_BOTH);
        AardvarkApi.aa_target_power(handle, AardvarkApi.AA_TARGET_POWER_BOTH);

        // Setup the clock phase
        AardvarkApi.aa_spi_configure(handle, (AardvarkSpiPolarity)(mode >> 1),
                                     (AardvarkSpiPhase)(mode & 1),
                                     AardvarkSpiBitorder.AA_SPI_BITORDER_MSB);

        // Set the bitrate
        bitrate = AardvarkApi.aa_spi_bitrate(handle, bitrate);
        Console.WriteLine("Bitrate set to {0} kHz", bitrate);

        // Perform the operation
        if (command == "write")
        {
            _writeMemory(handle, addr, length, false);
            Console.WriteLine("Wrote to EEPROM");
        }
        else if (command == "read")
        {
            _readMemory(handle, addr, length);
        }
        else if (command == "zero")
        {
            _writeMemory(handle, addr, length, true);
            Console.WriteLine("Zeroed EEPROM");
        }
        else
        {
            Console.WriteLine("unknown command: {0}", command);
        }

        // Close the device
        AardvarkApi.aa_close(handle);
    }
示例#25
0
    /*=====================================================================
     | FUNCTIONS
     | ====================================================================*/
    static void blastBytes(int handle, string filename)
    {
        FileStream file;
        int        trans_num = 0;

        byte[] dataIn  = new byte[BUFFER_SIZE];
        byte[] dataOut = new byte[BUFFER_SIZE];

        // Open the file
        try {
            file = new FileStream(filename, FileMode.Open, FileAccess.Read);
        }
        catch (Exception) {
            Console.WriteLine("Unable to open file '{0}'", filename);
            return;
        }

        while (file.Length != file.Position)
        {
            int numWrite, count;
            int i;

            // Read from the file
            numWrite = file.Read(dataOut, 0, BUFFER_SIZE);
            if (numWrite == 0)
            {
                break;
            }

            if (numWrite < BUFFER_SIZE)
            {
                byte[] temp = new byte[numWrite];
                for (i = 0; i < numWrite; i++)
                {
                    temp[i] = dataOut[i];
                }
                dataOut = temp;
            }

            // Write the data to the bus
            count = AardvarkApi.aa_spi_write(handle, BUFFER_SIZE, dataOut,
                                             BUFFER_SIZE, dataIn);
            if (count < 0)
            {
                Console.WriteLine("error: {0}",
                                  AardvarkApi.aa_status_string(count));
                break;
            }
            else if (count != numWrite)
            {
                Console.WriteLine("error: only a partial number of bytes " +
                                  "written");
                Console.WriteLine("  ({0}) instead of full ({1})",
                                  count, numWrite);
                break;
            }

            Console.WriteLine("*** Transaction #{0:d2}", trans_num);

            // Dump the data to the screen
            Console.Write("Data written to device:");
            for (i = 0; i < count; ++i)
            {
                if ((i & 0x0f) == 0)
                {
                    Console.Write("\n{0:x4}:  ", i);
                }
                Console.Write("{0:x2} ", dataOut[i] & 0xff);
                if (((i + 1) & 0x07) == 0)
                {
                    Console.Write(" ");
                }
            }
            Console.WriteLine();
            Console.WriteLine();

            // Dump the data to the screen
            Console.Write("Data read from device:");
            for (i = 0; i < count; ++i)
            {
                if ((i & 0x0f) == 0)
                {
                    Console.Write("\n{0:x4}:  ", i);
                }
                Console.Write("{0:x2} ", dataIn[i] & 0xff);
                if (((i + 1) & 0x07) == 0)
                {
                    Console.Write(" ");
                }
            }
            Console.WriteLine();
            Console.WriteLine();

            ++trans_num;

            // Sleep a tad to make sure slave has time to process this request
            AardvarkApi.aa_sleep_ms(10);
        }

        file.Close();
    }
示例#26
0
    /*=====================================================================
     | MAIN PROGRAM
     | ====================================================================*/
    public static void Main(string[] args)
    {
        int handle;
        int port = 0;
        int mode = 0;

        string filename;

        int bitrate;

        if (args.Length != 3)
        {
            Console.WriteLine("usage: aaspi_file PORT MODE filename");
            Console.WriteLine("  mode 0 : pol = 0, phase = 0");
            Console.WriteLine("  mode 1 : pol = 0, phase = 1");
            Console.WriteLine("  mode 2 : pol = 1, phase = 0");
            Console.WriteLine("  mode 3 : pol = 1, phase = 1");
            Console.WriteLine();
            Console.WriteLine("  'filename' should contain data to be sent");
            Console.WriteLine("  to the downstream spi device");
            return;
        }


        // Parse the port argument
        try {
            port = Convert.ToInt32(args[0]);
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid port");
            return;
        }

        // Parse the mode argument
        try {
            mode = Convert.ToInt32(args[1]) & 0x3;
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid mode");
            return;
        }

        filename = args[2];

        // Open the device
        handle = AardvarkApi.aa_open(port);
        if (handle <= 0)
        {
            Console.WriteLine("Unable to open Aardvark device on port {0}",
                              port);
            Console.WriteLine("error: {0}",
                              AardvarkApi.aa_status_string(handle));
            return;
        }

        // Ensure that the SPI subsystem is enabled
        AardvarkApi.aa_configure(handle, AardvarkConfig.AA_CONFIG_SPI_I2C);

        // Enable 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.
        AardvarkApi.aa_target_power(handle, AardvarkApi.AA_TARGET_POWER_BOTH);

        // Setup the clock phase
        AardvarkApi.aa_spi_configure(handle, (AardvarkSpiPolarity)(mode >> 1),
                                     (AardvarkSpiPhase)(mode & 1),
                                     AardvarkSpiBitorder.AA_SPI_BITORDER_MSB);

        // Setup the bitrate
        bitrate = AardvarkApi.aa_spi_bitrate(handle, SPI_BITRATE);
        Console.WriteLine("Bitrate set to {0} kHz", bitrate);

        blastBytes(handle, filename);

        // Close the device
        AardvarkApi.aa_close(handle);

        return;
    }
示例#27
0
    /*=====================================================================
     | MAIN PROGRAM
     | ====================================================================*/
    public static void Main(string[] args)
    {
        int   handle;
        int   port    = 0;
        int   bitrate = 100;
        int   bus_timeout;
        byte  device;
        byte  addr;
        short length;

        if (args.Length != 6)
        {
            Console.WriteLine("usage: aai2c_eeprom PORT BITRATE read  SLAVE_ADDR OFFSET LENGTH");
            Console.WriteLine("usage: aai2c_eeprom PORT BITRATE write SLAVE_ADDR OFFSET LENGTH");
            Console.WriteLine("usage: aai2c_eeprom PORT BITRATE zero  SLAVE_ADDR OFFSET LENGTH");
            return;
        }

        string command = args[2];

        // Parse the port argument
        try {
            port = Convert.ToInt32(args[0]);
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid port number");
            return;
        }

        // Parse the bitrate argument
        try {
            bitrate = Convert.ToInt32(args[1]);
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid bitrate");
            return;
        }

        // Parse the slave address argument
        try {
            if (args[3].StartsWith("0x"))
            {
                device = Convert.ToByte(args[3].Substring(2), 16);
            }
            else
            {
                device = Convert.ToByte(args[3]);
            }
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid device number");
            return;
        }

        // Parse the memory offset argument
        try {
            if (args[4].StartsWith("0x"))
            {
                addr = Convert.ToByte(args[4].Substring(2), 16);
            }
            else
            {
                addr = Convert.ToByte(args[4]);
            }
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid memory addr");
            return;
        }

        // Parse the length
        try {
            length = Convert.ToInt16(args[5]);
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid length");
            return;
        }

        // Open the device
        handle = AardvarkApi.aa_open(port);
        if (handle <= 0)
        {
            Console.WriteLine("Unable to open Aardvark device on port {0}",
                              port);
            Console.WriteLine("error: {0}",
                              AardvarkApi.aa_status_string(handle));
            return;
        }

        // Ensure that the I2C subsystem is enabled
        AardvarkApi.aa_configure(handle, AardvarkConfig.AA_CONFIG_SPI_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.
        AardvarkApi.aa_i2c_pullup(handle, AardvarkApi.AA_I2C_PULLUP_BOTH);

        // Power the EEPROM 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.
        AardvarkApi.aa_target_power(handle,
                                    AardvarkApi.AA_TARGET_POWER_BOTH);

        // Set the bitrate
        bitrate = AardvarkApi.aa_i2c_bitrate(handle, bitrate);
        Console.WriteLine("Bitrate set to {0} kHz", bitrate);

        // Set the bus lock timeout
        bus_timeout = AardvarkApi.aa_i2c_bus_timeout(handle, BUS_TIMEOUT);
        Console.WriteLine("Bus lock timeout set to {0} ms", bus_timeout);

        // Perform the operation
        if (command == "write")
        {
            _writeMemory(handle, device, addr, length, false);
            Console.WriteLine("Wrote to EEPROM");
        }
        else if (command == "read")
        {
            _readMemory(handle, device, addr, length);
        }
        else if (command == "zero")
        {
            _writeMemory(handle, device, addr, length, true);
            Console.WriteLine("Zeroed EEPROM");
        }
        else
        {
            Console.WriteLine("unknown command: {0}", command);
        }

        // Close the device and exit
        AardvarkApi.aa_close(handle);
    }
示例#28
0
    /*=====================================================================
     | MAIN PROGRAM
     | ====================================================================*/
    public static void Main(string[] args)
    {
        int port;
        int handle;
        int result;

        AardvarkApi.AardvarkExt aaExt = new AardvarkApi.AardvarkExt();

        if (args.Length != 1)
        {
            Console.WriteLine("usage: aamonitor PORT");
            return;
        }


        // Parse the port argument
        try {
            port = Convert.ToInt32(args[0]);
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid port");
            return;
        }

        // Open the device
        handle = AardvarkApi.aa_open_ext(port, ref aaExt);

        if (handle <= 0)
        {
            Console.WriteLine("Unable to open Aardvark device on port {0}",
                              port);
            Console.WriteLine("error: {0}",
                              AardvarkApi.aa_status_string(handle));
            return;
        }

        Console.WriteLine("Opened Aardvark; features = 0x{0:x2}",
                          aaExt.features);

        // 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.
        AardvarkApi.aa_i2c_pullup(handle, AardvarkApi.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.
        AardvarkApi.aa_target_power(handle, AardvarkApi.AA_TARGET_POWER_NONE);

        // Enable the monitor
        result = AardvarkApi.aa_i2c_monitor_enable(handle);
        if (result < 0)
        {
            Console.WriteLine("error: {0}",
                              AardvarkApi.aa_status_string(result));
            return;
        }
        Console.WriteLine("Enabled I2C monitor.");

        // Dump the data to the console
        dump(handle);

        // Disable the monitor and close the device
        AardvarkApi.aa_i2c_monitor_disable(handle);
        AardvarkApi.aa_close(handle);
    }
示例#29
0
    /*=====================================================================
     | FUNCTIONS
     | ====================================================================*/
    static void dump(int handle)
    {
        int    result;
        ushort last_data1 = 0;
        ushort last_data0 = 0;

        ushort[] data = new ushort[BUFFER_SIZE];

        // Wait for data on the bus
        Console.WriteLine("Waiting {0} ms for first transaction...",
                          INITIAL_TIMEOUT);
        result = AardvarkApi.aa_async_poll(handle, INITIAL_TIMEOUT);
        if (result == AardvarkApi.AA_ASYNC_NO_DATA)
        {
            Console.WriteLine("  no data pending.");
            return;
        }

        Console.WriteLine("  data received.");

        // Loop until aa_async_poll times out
        for (;;)
        {
            // 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.
            int num_bytes = AardvarkApi.aa_i2c_monitor_read(
                handle, (ushort)BUFFER_SIZE, data);
            int i;

            if (num_bytes < 0)
            {
                Console.WriteLine("error: {0}",
                                  AardvarkApi.aa_status_string(num_bytes));
                return;
            }

            for (i = 0; i < num_bytes; ++i)
            {
                // Check for start condition
                if (data[i] == AardvarkApi.AA_I2C_MONITOR_CMD_START)
                {
                    DateTime now = DateTime.Now;
                    Console.Write("\n{0:ddd MMM dd HH:mm:ss yyyy} - [S] ",
                                  now);
                }

                // Check for stop condition
                else if (data[i] == AardvarkApi.AA_I2C_MONITOR_CMD_STOP)
                {
                    Console.WriteLine("[P]");
                }

                else
                {
                    int nack = (data[i] & AardvarkApi.AA_I2C_MONITOR_NACK);
                    // 7-bit addresses
                    if (last_data0 ==
                        (int)AardvarkApi.AA_I2C_MONITOR_CMD_START &&
                        ((data[i] & 0xf8) != 0xf0 || nack != 0))
                    {
                        Console.Write("<{0:x2}:{1}>{2} ",
                                      (data[i] & 0xff) >> 1,
                                      (data[i] & 0x01) != 0 ? 'r' : 'w',
                                      nack != 0 ? "*" : "");
                    }

                    // 10-bit addresses
                    // See Philips specification for more details.
                    else if (last_data1 ==
                             (int)AardvarkApi.AA_I2C_MONITOR_CMD_START &&
                             (last_data0 & 0xf8) == 0xf0)
                    {
                        Console.Write(
                            "<{0:x3}:{1}>{2} ",
                            ((last_data0 << 7) & 0x300) | (data[i] & 0xff),
                            (last_data0 & 0x01) != 0 ? 'r' : 'w',
                            nack != 0 ? "*" : "");
                    }

                    // Normal data
                    else if (last_data0 !=
                             (int)AardvarkApi.AA_I2C_MONITOR_CMD_START)
                    {
                        Console.Write("{0:x2}{1} ", data[i] & 0xff,
                                      nack != 0 ? "*" : "");
                    }
                }

                last_data1 = last_data0;
                last_data0 = data[i];
            }

            Console.WriteLine("\nWaiting {0} ms for subsequent transaction...",
                              INTERVAL_TIMEOUT);
            result = AardvarkApi.aa_async_poll(handle, INTERVAL_TIMEOUT);
            if (result == AardvarkApi.AA_ASYNC_NO_DATA)
            {
                Console.WriteLine("  no more data pending.");
                break;
            }
            Console.WriteLine(" data received.");
            Console.WriteLine();
        }
    }
示例#30
0
    /*=====================================================================
     | MAIN PROGRAM
     | ====================================================================*/
    public static void Main(string[] args)
    {
        int  port = -1;
        byte val;
        int  handle;

        AardvarkApi.AardvarkExt aaExt = new AardvarkApi.AardvarkExt();

        if (args.Length != 1)
        {
            Console.WriteLine("usage: aagpio PORT");
            return;
        }

        try {
            port = Convert.ToInt32(args[0]);
        }
        catch (Exception) {
            Console.WriteLine("Error: invalid port number");
        }

        // Open the device
        handle = AardvarkApi.aa_open_ext(port, ref aaExt);

        if (handle <= 0)
        {
            Console.WriteLine("Unable to open Aardvark device on port {0}",
                              port);
            Console.WriteLine("error: {0}",
                              AardvarkApi.aa_status_string(handle));
            return;
        }

        Console.WriteLine("Opened Aardvark adapter");

        // Configure the Aardvark adapter so all pins
        // are now controlled by the GPIO subsystem
        AardvarkApi.aa_configure(handle, AardvarkConfig.AA_CONFIG_GPIO_ONLY);

        // Turn off the external I2C line pullups
        AardvarkApi.aa_i2c_pullup(handle, AardvarkApi.AA_I2C_PULLUP_NONE);

        // Make sure the charge has dissipated on those lines
        AardvarkApi.aa_gpio_set(handle, 0x00);
        AardvarkApi.aa_gpio_direction(handle, 0xff);

        // By default all GPIO pins are inputs.  Writing 1 to the
        // bit position corresponding to the appropriate line will
        // configure that line as an output
        AardvarkApi.aa_gpio_direction(handle,
                                      (byte)(AardvarkGpioBits.AA_GPIO_SS | AardvarkGpioBits.AA_GPIO_SCL));

        // By default all GPIO outputs are logic low.  Writing a 1
        // to the appropriate bit position will force that line
        // high provided it is configured as an output.  If it is
        // not configured as an output the line state will be
        // cached such that if the direction later changed, the
        // latest output value for the line will be enforced.
        AardvarkApi.aa_gpio_set(handle, (byte)AardvarkGpioBits.AA_GPIO_SCL);
        Console.WriteLine("Setting SCL to logic low");

        // The get method will return the line states of all inputs.
        // If a line is not configured as an input the value of
        // that particular bit position in the mask will be 0.
        val = (byte)AardvarkApi.aa_gpio_get(handle);

        // Check the state of SCK
        if ((val & (byte)AardvarkGpioBits.AA_GPIO_SCK) != 0)
        {
            Console.WriteLine("Read the SCK line as logic high");
        }
        else
        {
            Console.WriteLine("Read the SCK line as logic low");
        }

        // Optionally we can set passive pullups on certain lines.
        // This can prevent input lines from floating.  The pullup
        // configuration is only valid for lines configured as inputs.
        // If the line is not currently input the requested pullup
        // state will take effect only if the line is later changed
        // to be an input line.
        AardvarkApi.aa_gpio_pullup(handle,
                                   (byte)(AardvarkGpioBits.AA_GPIO_MISO | AardvarkGpioBits.AA_GPIO_MOSI));

        // Now reading the MISO line should give a logic high,
        // provided there is nothing attached to the Aardvark
        // adapter that is driving the pin low.
        val = (byte)AardvarkApi.aa_gpio_get(handle);
        if ((val & (byte)AardvarkGpioBits.AA_GPIO_MISO) != 0)
        {
            Console.WriteLine(
                "Read the MISO line as logic high (passive pullup)");
        }
        else
        {
            Console.WriteLine(
                "Read the MISO line as logic low (is pin driven low?)");
        }


        // Now do a 1000 gets from the GPIO to test performance
        for (int i = 0; i < 1000; ++i)
        {
            AardvarkApi.aa_gpio_get(handle);
        }

        int oldval, newval;

        // Demonstrate use of aa_gpio_change
        AardvarkApi.aa_gpio_direction(handle, 0x00);
        oldval = AardvarkApi.aa_gpio_get(handle);

        Console.WriteLine("Calling aa_gpio_change for 2 seconds...");
        newval = AardvarkApi.aa_gpio_change(handle, 2000);

        if (newval != oldval)
        {
            Console.WriteLine("  GPIO inputs changed.\n");
        }
        else
        {
            Console.WriteLine("  GPIO inputs did not change.\n");
        }

        // Turn on the I2C line pullups since we are done
        AardvarkApi.aa_i2c_pullup(handle, AardvarkApi.AA_I2C_PULLUP_BOTH);

        // Configure the Aardvark adapter back to SPI/I2C mode.
        AardvarkApi.aa_configure(handle, AardvarkConfig.AA_CONFIG_SPI_I2C);

        // Close the device
        AardvarkApi.aa_close(handle);
    }