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); } }
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); }
/*===================================================================== | 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(); } }
/*===================================================================== | 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); }
/*===================================================================== | 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); }