public SiliconLabsSI7005(byte deviceId = DeviceIdDefault, int clockRateKHz = ClockRateKHzDefault, int transactionTimeoutmSec = TransactionTimeoutmSecDefault) { this.deviceId = deviceId; this.clockRateKHz = clockRateKHz; this.transactionTimeoutmSec = transactionTimeoutmSec; using (OutputPort i2cPort = new OutputPort(Pins.GPIO_PIN_SDA, true)) { i2cPort.Write(false); Thread.Sleep(250); } using (I2CDevice device = new I2CDevice(new I2CDevice.Configuration(deviceId, clockRateKHz))) { byte[] writeBuffer = { RegisterIdDeviceId }; byte[] readBuffer = new byte[1]; // The first request always fails I2CDevice.I2CTransaction[] action = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer), I2CDevice.CreateReadTransaction(readBuffer) }; if( device.Execute(action, transactionTimeoutmSec) == 0 ) { // throw new ApplicationException("Unable to send get device id command"); } } }
public static void Main() { //our 10 bit address const ushort address10Bit = 0x1001; //binary 1000000001 = 129 const byte addressMask = 0x78; //reserved address mask 011110XX for 10 bit addressing //first MSB part of address ushort address1 = addressMask | (address10Bit >> 8); //is 7A and contains the two MSB of the 10 bit address I2CDevice.Configuration config = new I2CDevice.Configuration(address1, 100); I2CDevice device = new I2CDevice(config); //second LSB part of address byte address2 = (byte)(address10Bit & 0xFF); //the other 8 bits (LSB) byte[] address2OutBuffer = new byte[] { address2 }; I2CDevice.I2CWriteTransaction addressWriteTransaction = device.CreateWriteTransaction(address2OutBuffer); //prepare buffer to write data byte[] outBuffer = new byte[] { 0xAA }; I2CDevice.I2CWriteTransaction writeTransaction = device.CreateWriteTransaction(outBuffer); //prepare buffer to read data byte[] inBuffer = new byte[4]; I2CDevice.I2CReadTransaction readTransaction = device.CreateReadTransaction(inBuffer); //execute transactions I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { addressWriteTransaction, writeTransaction, readTransaction }; device.Execute(transactions, 100); }
public double ReadValue() { double temp; byte[] inBuffer = new byte[2]; I2CDevice.I2CReadTransaction readTransaction = I2CDevice.CreateReadTransaction(inBuffer); //execute both transactions I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { readTransaction }; int transferred = device.Execute(transactions, 100 //timeout in ms ); // The value is now converted temp = (float)(inBuffer[0] << 1) / 2; if ((inBuffer[1] >> 7) != 0) temp += (float)0.5; if ((inBuffer[0] >> 7) != 0) temp = -temp; Thread.Sleep(1000); return temp; }
/// <summary> /// Turns on the Oscillator. Turns on the Display. Turns off Blinking. Sets Brightness to full. /// </summary> public void Init() { Config = new I2CDevice.Configuration(HT16K33_ADRESS, HT16K33_CLKRATE); Matrix = new I2CDevice(Config); byte[] write = new byte[1]; write[0] = HT16K33_OSC_ON; // IC Oscillator ON byte[] write2 = new byte[1]; write2[0] = HT16K33_DISPLAY_ON; // Display ON I2CDevice.I2CTransaction[] i2cTx = new I2CDevice.I2CTransaction[1]; i2cTx[0] = I2CDevice.CreateWriteTransaction(write); I2CDevice.I2CTransaction[] i2cTx2 = new I2CDevice.I2CTransaction[1]; i2cTx2[0] = I2CDevice.CreateWriteTransaction(write2); Matrix.Execute(i2cTx, Timeout); Matrix.Execute(i2cTx2, Timeout); // initialize DisplayBuffer for (int i = 0; i < 8; i++) { DisplayBuffer[i] = 0x00; } }
public void Read(I2CDevice.Configuration config, byte[] buffer, int transactionTimeout) { _slaveDevice.Config = config; I2CDevice.I2CTransaction[] i2CTransactions = new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(buffer) }; lock (_slaveDevice) _slaveDevice.Execute(i2CTransactions, transactionTimeout); }
public static void Sketch01() { //create I2C object //note that the netmf i2cdevice configuration requires a 7-bit address! It set the 8th R/W bit automatically. I2CDevice.Configuration con = new I2CDevice.Configuration(0x68, 100); I2CDevice MyI2C = new I2CDevice(con); // Create transactions // We need 2 in this example, we are reading from the device // First transaction is writing the "read command" // Second transaction is reading the data I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2]; // create write buffer (we need one byte) byte[] RegisterNum = new byte[1] { 0x14 }; xActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum); // create read buffer to read the register byte[] RegisterValue = new byte[1]; xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue); // Now we access the I2C bus using a timeout of one second // if the execute command returns zero, the transaction failed (this // is a good check to make sure that you are communicating with the device correctly // and don’t have a wiring issue or other problem with the I2C device) if (MyI2C.Execute(xActions, 1000) == 0) { Debug.Print("Failed to perform I2C transaction"); } else { Debug.Print("Register value: " + RegisterValue[0].ToString()); } }
private void ds1624Thread() { I2CDevice.I2CTransaction[] cmdStartConversion = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] {0xEE}) }; I2CDevice.I2CTransaction[] cmdStartRead = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0xAA }) }; byte[] temperatureData = new byte[2]; I2CDevice.I2CTransaction[] cmdRead = new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(temperatureData) }; while (runThread == true) { if (i2cDevice.Execute(cmdStartConversion, 100) == 0) { throw new Exception("i2c"); } Thread.Sleep(1000); if (i2cDevice.Execute(cmdStartRead, 100) == 0) { throw new Exception("i2c"); } if (i2cDevice.Execute(cmdRead, 100) < 2) { throw new Exception("i2c"); } else { this.lastTemperature = this.ConvertTemperature(temperatureData); this.TemperatureEvent(this.LastTemperature); } } }
public TSL2561(byte I2CAddress, int ClockinKHz) { I2CConfig = new I2CDevice.Configuration(I2CAddress, ClockinKHz); I2C = new I2CDevice(I2CConfig); // read the ID register. var Actions = new I2CDevice.I2CTransaction[2]; byte[] rx = new byte[1]; Actions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x0a }); Actions[1] = I2CDevice.CreateReadTransaction(rx); if (I2C.Execute(Actions, 1000) == 0) { Debug.Print("Read ID Register failed"); // exit or something } else { Debug.Print("ID value: " + rx[0].ToString()); } // 4 msb must be 0001 for a TSL2561 if ((rx[0] & 0xf0) != 0xa0) { // exit or something } setGain(0x10); Thread.Sleep(5); // Mandatory after each Write transaction !!! }
public bool GetVersion(out byte majorVersion, out byte minorVersion) { var command = new byte[1]; command[0] = 0x00; var writeAction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(command), }; i2c.Execute(writeAction, 1000); var response = new byte[3]; var readAction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(response), }; i2c.Execute(readAction, 1000); if (response[0] != 0x80) { majorVersion = 0; minorVersion = 0; return false; } majorVersion = response[2]; minorVersion = response[1]; return true; }
/// <summary> /// /// </summary> /// <param name="channel"></param> /// <returns></returns> public int GetChannel(Channels channel) { //Odd high //Even low byte[] msb = new byte[] { (byte)((int)channel << 1) }; byte[] lsb = new byte[] { (byte)(((int)channel << 1) + 1) }; byte[] msbOut = new byte[1]; byte[] lsbOut = new byte[1]; I2CDevice.I2CTransaction[] transaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(msb), I2CDevice.CreateReadTransaction(msbOut), I2CDevice.CreateWriteTransaction(lsb), I2CDevice.CreateReadTransaction(lsbOut), }; int output = ((((int) msbOut[0]) << 8) | (lsbOut[0])); return output; }
public void clear() { I2CDevice.I2CTransaction[] write = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0x82 }) }; m_display.Execute(write, 1000); }
void Write (byte[] writeBuffer) { Connect (); // create a write transaction containing the bytes to be written to the device var writeTransaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer) }; // write the data to the device int written = this.i2cDevice.Execute (writeTransaction, TransactionTimeout); while (written < writeBuffer.Length) { byte[] newBuffer = new byte[writeBuffer.Length - written]; Array.Copy (writeBuffer, written, newBuffer, 0, newBuffer.Length); writeTransaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(newBuffer) }; written += this.i2cDevice.Execute (writeTransaction, TransactionTimeout); } // make sure the data was sent if (written != writeBuffer.Length) { throw new Exception ("Could not write to device."); } }
/// <summary> /// Write to a I2C slave device. /// </summary> /// <param name="device">I2C slave device.</param> /// <param name="writeBuffer">Bytes to be written to the slave.</param> /// <param name="transactionTimeout">Time in mS the system will allow for a transaction.</param> public void Write(I2CSlave device, byte[] writeBuffer, int transactionTimeout) { Device.Config = new I2CDevice.Configuration(device.Address, ClockRate); I2CDevice.I2CTransaction[] writeTransaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer) }; lock(Device) if(Device.Execute(writeTransaction, transactionTimeout) != writeBuffer.Length) throw new IOException("Could not write to slave."); }
/// <summary> /// Read from a I2C slave device. /// </summary> /// <param name="device">I2C slave device.</param> /// <param name="readBuffer">Bytes to be read from the slave.</param> /// <param name="transactionTimeout">The amount of time the system will wait before resuming execution of the transaction.</param> public void Read(I2CSlave device, byte[] readBuffer, int transactionTimeout) { Device.Config = new I2CDevice.Configuration(device.Address, ClockRate); I2CDevice.I2CTransaction[] readTransaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(readBuffer) }; lock(Device) if(Device.Execute(readTransaction, transactionTimeout) != readBuffer.Length) throw new IOException("Could not read from slave."); }
// 0x81 public void changeAddress(uint new_addr) { if (new_addr > 255) return; I2CDevice.I2CTransaction[] write = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0x80, (byte)new_addr }) }; m_display.Execute(write, 1000); }
public byte[] ReadRegister(I2CDevice.Configuration config, byte register, int timeout) { var registerValue = new byte[] { 0, 0 }; var xActions = new I2CDevice.I2CTransaction[2]; xActions[0] = I2CDevice.CreateWriteTransaction(new[] { register }); xActions[1] = I2CDevice.CreateReadTransaction(registerValue); Transact(config, xActions, timeout); return registerValue; }
public static byte GetData(I2CDevice FPGA, byte Port) { int I2CTimeout = 1000; byte[] buffer = new byte[1]; var transaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] {Port}), I2CDevice.CreateReadTransaction(buffer) }; FPGA.Execute(transaction, I2CTimeout); return buffer[0]; }
public void WriteRegister(I2CDevice.Configuration config, byte register, byte[] buffer, int transactionTimeout) { _slaveDevice.Config = config; I2CDevice.I2CTransaction[] i2CTransactions = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] {register}) , I2CDevice.CreateWriteTransaction(buffer) }; lock (_slaveDevice) _slaveDevice.Execute(i2CTransactions, transactionTimeout); }
public static void SetRedLED(I2CDevice FPGA, bool state) { int I2CTimeout = 1000; byte[] buffer = new byte[2]; buffer[0] = 0x10; buffer[1] = state ? (byte)0x01 : (byte)0x00; var transaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(buffer) }; FPGA.Execute(transaction, I2CTimeout); }
void readValues(I2CDevice adxl345, short[] result) { byte[] values = new byte[6]; I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2]; byte[] RegisterNum = new byte[1] { valuesRegister }; xActions[0] = adxl345.CreateWriteTransaction(RegisterNum); xActions[1] = adxl345.CreateReadTransaction(values); if (adxl345.Execute(xActions, 1000) == 0) throw new Exception("Unable to read accelerometer"); result[0] = (short)(values[0] + (values[1] << 8)); result[1] = (short)(values[2] + (values[3] << 8)); result[2] = (short)(values[4] + (values[5] << 8)); }
public byte[] Read(byte[] RegisterNum, int length) { if (length == -1) length = RegisterNum.Length; I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2]; xActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum); byte[] RegisterValue = new byte[length]; xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue); var transfered = this.Execute(xActions, 1000); return RegisterValue; }
/// <summary> /// Send out one byte to shift register /// </summary> /// <param name="data">Byte to send</param> public void SendByte(byte data) { if (!_bigEndian) data = ReverseBits(data); lock (_i2cbus) { _i2cbus.Config = _config; I2CDevice.I2CTransaction[] xact = new I2CDevice.I2CTransaction[] { _i2cbus.CreateWriteTransaction(new byte[] { data }) }; _i2cbus.Execute(xact, 3000); } }
public static void Main() { OutputPort Led = new OutputPort(Pins.ONBOARD_LED, false); byte[] Addr=new byte[2]; Addr[0]=0x00; Addr[1]=0x01; byte[] TxBuff = new byte[4]; TxBuff[0] = (byte)'1'; TxBuff[1] = (byte)'2'; TxBuff[2] = (byte)'3'; TxBuff[3] = (byte)'4'; byte[] RxBuff= new byte[4]; I2CDevice.Configuration I2C_Configuration = new I2CDevice.Configuration(0x50, 400); I2CDevice I2C1 = new I2CDevice(I2C_Configuration); I2CDevice.I2CTransaction[] WriteTran = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(Addr), I2CDevice.CreateWriteTransaction(TxBuff) }; I2CDevice.I2CTransaction[] ReadTran = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(Addr), I2CDevice.CreateReadTransaction(RxBuff) }; while(true) { Led.Write(true); I2C1.Execute(WriteTran, 1000); Debug.Print("Write Success!"); Thread.Sleep(200); I2C1.Execute(ReadTran, 1000); Debug.Print("Read Success!"); string ReadOut = new string(System.Text.Encoding.UTF8.GetChars(RxBuff)); Debug.Print("EEPROM CONTENT:"+ReadOut); Led.Write(false); Thread.Sleep(200); } }
/// <summary> /// Generic read operation from I2C slave device. /// </summary> /// <param name="config">I2C slave device configuration.</param> /// <param name="readBuffer">The array of bytes that will contain the data read from the device.</param> /// <param name="transactionTimeout">The amount of time the system will wait before resuming execution of the transaction.</param> public void Read(I2CDevice.Configuration config, byte[] readBuffer, int transactionTimeout) { // Set i2c device configuration. _slaveDevice.Config = config; // create an i2c read transaction to be sent to the device. I2CDevice.I2CTransaction[] readXAction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(readBuffer) }; lock (_slaveDevice) { // the i2c data is received here from the device. int transferred = _slaveDevice.Execute(readXAction, transactionTimeout); // make sure the data was received. if (transferred != readBuffer.Length) throw new Exception("Could not read from device."); } }
/// <summary> /// Generic write operation to I2C slave device. /// </summary> /// <param name="config">I2C slave device configuration.</param> /// <param name="writeBuffer">The array of bytes that will be sent to the device.</param> /// <param name="transactionTimeout">The amount of time the system will wait before resuming execution of the transaction.</param> public void Write(I2CDevice.Configuration config, byte[] writeBuffer, int transactionTimeout) { // Set i2c device configuration. _slaveDevice.Config = config; // create an i2c write transaction to be sent to the device. I2CDevice.I2CTransaction[] writeXAction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer) }; lock (_slaveDevice) { // the i2c data is sent here to the device. int transferred = _slaveDevice.Execute(writeXAction, transactionTimeout); // make sure the data was sent. if (transferred != writeBuffer.Length) throw new Exception("Could not write to device."); } }
/// <summary> /// Sendet den Inhalt des Byte Array zur Hardware /// </summary> /// <param name="writeBuffer">Byte Array</param> public int Write(byte[] writeBuffer) { // Byte Array übergeben für das ertellen einer Transaction I2CDevice.I2CTransaction[] writeTransaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer) }; // Sende die Daten an die Hardware. Timeout bei 1 Sekunde int written = this._Device.Execute(writeTransaction, 1000); // Prüfe ob alle daten gesendet wurden, ansonsten Exception ausführen if (written != writeBuffer.Length) { Debug.Print("Nelze zapisovat do modulu SL030."); } return written; }
/// <summary> /// Gets the date / time in 24 hour format. /// </summary> /// <returns>A DateTime object</returns> public DateTime Get() { byte[] clockData = new byte [7]; // Read time registers (7 bytes from DS1307_RTC_START_ADDRESS) var transaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] {DS1307_RTC_START_ADDRESS}), I2CDevice.CreateReadTransaction(clockData) }; if (Clock.Execute(transaction, TimeOutMs) == 0) { throw new Exception("I2C transaction failed"); } return new DateTime( BcdToDec(clockData[6]) + 2000, // year BcdToDec(clockData[5]), // month BcdToDec(clockData[4]), // day BcdToDec(clockData[2] & 0x3f), // hours over 24 hours BcdToDec(clockData[1]), // minutes BcdToDec(clockData[0] & 0x7f) // seconds ); }
/// <summary> /// Set a target servo to a degree /// </summary> /// <param name="port"></param> /// <param name="degrees"></param> public void SetServoDegree(FpgaPwmPorts port, double degrees) { // Calculate how much time to hold the pin high int time = (int)(degrees * 1000 / 180 + 1000); // Generate MSB/LSB byte[] bufferLsb = new byte[2] { (byte)port, (byte)(time & 0xff) }; byte[] bufferMsb = new byte[2] { (byte)(port + 1), (byte)((time >> 8) & 0xff) }; // Create the transaction I2CDevice.I2CTransaction[] transaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(bufferMsb), I2CDevice.CreateWriteTransaction(bufferLsb) }; // Execute! fpga.Execute(transaction, 1000); }
/// <summary> /// Function for reading from a specific configuration /// </summary> /// <param name="configuration">Configuration to read from</param> /// <param name="buffer">Buffer to be read to</param> /// <param name="transactionTimeout">Timeout on read execution</param> /// <returns>Bytes read</returns> public int Read(I2CDevice.Configuration configuration, byte[] buffer, int transactionTimeout) { //Create readtransaction to i2c I2CDevice.I2CTransaction[] readTransaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(buffer) }; //Locking object, so no other can occupy the lines lock (sync) { //Set configuration device.Config = configuration; //Execute read int transferred = device.Execute(readTransaction, transactionTimeout); //If the transferred isnt equal to data length, error on read if (transferred != buffer.Length) Debug.Print("Error reading from device."); //Return bytes read return transferred; } }
/// <summary> /// Ruft mit den Adressen im Buffer die Werte ab /// </summary> /// <param name="readBuffer">Byte Array mit Adressen für den Abruf entsprechender Daten</param> public int Read(byte[] readBuffer) { // Erstelle ein Transaction zum Lesen mit übergabe des Byte Array I2CDevice.I2CTransaction[] readTransaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(readBuffer) }; // Lese die Daten von der Hardware. Timeout von einer Sekunde int read = this._Device.Execute(readTransaction, 1000); // Prüfe, ob die Daten gesendt wurden if (read != readBuffer.Length) { //throw new Exception("Es konnte nicht vom Modul gelesen werden."); Debug.Print("Nelze cist z modulu SL030."); } return read; }