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