Esempio n. 1
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);
     }
 }
Esempio n. 2
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);
        }
Esempio n. 3
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();
        }
    }
Esempio n. 4
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);
    }
Esempio n. 5
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);
    }
Esempio n. 6
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;
    }
Esempio n. 7
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);
    }
Esempio n. 8
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);
    }
Esempio n. 9
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);
    }
Esempio n. 10
0
    /*=====================================================================
     | MAIN PROGRAM
     | ====================================================================*/
    public static void Main(string[] args)
    {
        int handle;

        int  port      = 0;
        byte addr      = 0;
        int  timeoutMs = 0;

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

        if (args.Length != 3)
        {
            Console.WriteLine("usage: aai2c_slave PORT SLAVE_ADDR TIMEOUT_MS");
            Console.WriteLine("  SLAVE_ADDR is the slave address for this device");
            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 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;
        }

        // 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 I2C 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);

        // Set the slave response; this won't be used unless the master
        // reads bytes from the slave.
        for (i = 0; i < SLAVE_RESP_SIZE; ++i)
        {
            slaveResp[i] = (byte)('A' + i);
        }

        AardvarkApi.aa_i2c_slave_set_response(handle, SLAVE_RESP_SIZE,
                                              slaveResp);

        // Enable the slave
        AardvarkApi.aa_i2c_slave_enable(handle, addr, 0, 0);

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

        // Disable the slave and close the device
        AardvarkApi.aa_i2c_slave_disable(handle);
        AardvarkApi.aa_close(handle);
    }