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 void clear() { I2CDevice.I2CTransaction[] write = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0x82 }) }; m_display.Execute(write, 1000); }
public void RegWrite(byte reg, byte val) { wdata[0] = reg; wdata[1] = val; trRegWrite = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(wdata) }; int result = i2c.Execute(trRegWrite, timeout); }
private static bool WriteByte(byte register, byte value) { var xActions = new I2CDevice.I2CTransaction[1]; xActions[0] = I2CDevice.CreateWriteTransaction(new[] { register, value }); return(Hardware.I2CBus.Execute(_i2CConfiguration, xActions, _i2CTimeout) != 0); }
/// /// Reads an arbitrary RTC or RAM register /// /// Register address between 0x00 and 0x3f /// The value of the byte read at the address public byte this[byte address] { get { if (address > DS1307_RAM_END_ADDRESS) { throw new ArgumentOutOfRangeException("Invalid register address"); } var value = new byte[1]; // Read the RAM register @ the address var transaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { address }), I2CDevice.CreateReadTransaction(value) }; if (clock.Execute(transaction, DS1307_I2C_TRANSACTION_TIMEOUT_MS) == 0) { throw new Exception("I2C transaction failed"); } return(value[0]); } }
public SiliconLabsSI7005() { using (I2CDevice device = new I2CDevice(new I2CDevice.Configuration(DeviceId, ClockRateKHz))) { byte[] writeBuffer = { RegisterIdDeviceId }; byte[] readBuffer = new byte[1]; // check that sensor connected I2CDevice.I2CTransaction[] action = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer), I2CDevice.CreateReadTransaction(readBuffer) }; if (device.Execute(action, TransactionTimeoutMilliseconds) == 0) { // The first read always fails } if (device.Execute(action, TransactionTimeoutMilliseconds) == 0) { throw new Exception("Unable to read DeviceId"); } if (readBuffer[0] != RegisterDeviceId) { throw new Exception("DeviceId invalid"); } } }
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 void Read() { I2CDevice MyI2C = new I2CDevice(i2con); //create transactions (we need 2 in this example) I2CDevice.I2CTransaction[] readActions = new I2CDevice.I2CTransaction[2]; // create write buffer (we need one byte) byte[] RegisterNum = new byte[1] { 2 }; readActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum); // create read buffer to read the register byte[] RegisterValue = new byte[1]; readActions[1] = I2CDevice.CreateReadTransaction(RegisterValue); // Now we access the I2C bus and timeout in one second if no responce MyI2C.Execute(readActions, 1000); if (currentValue != RegisterValue[0]) { currentValue = RegisterValue[0]; if (OnChange != null) { OnChange(dialId, currentValue); } } MyI2C.Dispose(); //Debug.Print("Register value: " + RegisterValue[0].ToString()); }
private void send(byte[] data) { this._device.Execute( new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(data) }, _timeout ); }
static ArrayList FindDevices() { var deviceAddresses = new ArrayList(); for (ushort address = 0x48; address < 0x48 + 0x07; address++) { var device = new I2CDevice(new I2CDevice.Configuration(address, ClockRateKHz)); byte[] writeBuffer = { 0x00 }; byte[] readBuffer = new byte[2]; I2CDevice.I2CTransaction[] action = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer) , I2CDevice.CreateReadTransaction(readBuffer) }; var transactionResult = device.Execute(action, transactionTimeout); if (transactionResult == 3) { deviceAddresses.Add(device.Config.Address); Debug.Print("Found device @" + device.Config.Address); } else { Debug.Print("No device found @" + device.Config.Address); } device.Dispose(); } return(deviceAddresses); }
public byte[] WriteRead(byte[] write, ushort length) { _device.Config = _configuration; var read = new byte[length]; I2CDevice.I2CTransaction[] transaction = { I2CDevice.CreateWriteTransaction(write), I2CDevice.CreateReadTransaction(read) }; var bytesTransferred = 0; var retryCount = 0; while (_device.Execute(transaction, _transactionTimeout) != (write.Length + read.Length)) { if (retryCount > 3) { throw new Exception("WriteRead: Retry count exceeded."); } retryCount++; } //while (bytesTransferred != (write.Length + read.Length)) //{ // if (retryCount > 3) // { // throw new Exception("WriteRead: Retry count exceeded."); // } // retryCount++; // bytesTransferred = _device.Execute(transaction, _transactionTimeout); //} return(read); }
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> /// Reading temperature in desired format /// </summary> /// <param name="format"></param> /// <returns></returns> public override double ReadTemperature(TemperatureFormat format) { I2CDevice.I2CTransaction[] i2cTx = new I2CDevice.I2CTransaction[2]; byte[] register = new byte[1]; byte[] data = new byte[2]; register[0] = TEMPERATURE_REG_ADDR; i2cTx[0] = I2CDevice.CreateWriteTransaction(register); i2cTx[1] = I2CDevice.CreateReadTransaction(data); int result = i2cDevice.Execute(i2cTx, TIMEOUT); //converting 12-bit temperature to double int rawTemperature = (data[0] << 4) | (data[1] >> 4); double temperature = 0; if (register.Length + data.Length == result) { if ((rawTemperature & 0x0800) > 0) { rawTemperature = (rawTemperature ^ 0xffff) + 1; temperature = -1; } temperature *= rawTemperature * 0.0625f; } return(temperature + ((format == TemperatureFormat.KELVIN) ? 273.15 : 0.0)); }
public void SetupDevice() { relayPort = new OutputPort((Cpu.Pin) 21, false); relayPort.Write(true); var i2cConfig = new I2CDevice.Configuration(0x51, 400); i2cSensor = new I2CDevice(i2cConfig); var xwsaction = I2CDevice.CreateWriteTransaction(new byte[] { 0x01 }); var xrsaction = I2CDevice.CreateReadTransaction(new byte[8]); i2cSensor.Execute(new I2CDevice.I2CTransaction[] { xwsaction, xrsaction }, 100); var read = xrsaction.Buffer; var temperature = BitConverter.ToDouble(read, 0); var accelOrder = I2CDevice.CreateWriteTransaction(new byte[] { 0x02 }); var accelRead = I2CDevice.CreateReadTransaction(new byte[24]); i2cSensor.Execute(new I2CDevice.I2CTransaction[] { accelOrder, accelRead }, 100); var accelX = BitConverter.ToDouble(accelRead.Buffer, 0); var accelY = BitConverter.ToDouble(accelRead.Buffer, 8); var accelZ = BitConverter.ToDouble(accelRead.Buffer, 16); }
private void Write(Byte register, Byte data) { var actions = new I2CDevice.I2CTransaction[1]; actions[0] = I2CDevice.CreateWriteTransaction(new [] { register, data }); Hardware.I2CBus.Execute(_config, actions, 1000); }
/// <summary> /// Reads an arbitrary RTC or RAM register /// </summary> /// <param name="address">Register address between 0x00 and 0x3f</param> /// <param name="length">The number of bytes to read</param> /// <returns>The value of the bytes read at the address</returns> public byte[] ReadRegister(byte address, int length = 1) { if (length < 1) { throw new ArgumentOutOfRangeException("length", "Must read at least 1 byte"); } if (address + length - 1 > DS1307_RAM_END_ADDRESS) { throw new ArgumentOutOfRangeException("Invalid register address"); } var buffer = new byte[length]; lock (clock) { clock.Config = config; // Read the RAM register @ the address var transaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { address }), I2CDevice.CreateReadTransaction(buffer) }; if (clock.Execute(transaction, DS1307_I2C_TRANSACTION_TIMEOUT_MS) == 0) { throw new Exception("I2C transaction failed"); } } return(buffer); }
public void WriteRegister(byte address, byte[] data) { if (address > DS1307_RAM_END_ADDRESS) { throw new ArgumentOutOfRangeException("Invalid register address"); } if (address + data.Length > DS1307_RAM_END_ADDRESS) { throw new ArgumentException("Buffer overrun"); } byte[] txData = new byte[data.Length + 1]; txData[0] = address; data.CopyTo(txData, 1); lock (clock) { clock.Config = config; var transaction = new I2CDevice.I2CWriteTransaction[] { I2CDevice.CreateWriteTransaction(txData) }; if (clock.Execute(transaction, DS1307_I2C_TRANSACTION_TIMEOUT_MS) == 0) { throw new Exception("I2C write transaction failed"); } } }
private void ConfigDevice() { if (device == null) { device = new I2CDevice(new I2CDevice.Configuration((ushort)(AddressBase + baseAddressOffset), EmxI2cClock)); xConfigAction = new I2CDevice.I2CTransaction[1]; xConfigAction[0] = I2CDevice.CreateWriteTransaction(configReg); xReadAction = new I2CDevice.I2CTransaction[1]; xReadAction[0] = I2CDevice.CreateReadTransaction(dataReg); } double[] lsbValues = new double[4] { 0.001, 0.00025, 0.0000625, 0.000015625 }; lsbVolts = lsbValues[(ushort)resolution]; countsMask = (1 << (12 + (int)resolution * 2)) - 1; maxValue = 1 << (11 + (int)resolution * 2); gainDivisor = Math.Pow(2.0, (double)gain); configReg[0] = (byte)((ushort)gain + ((ushort)resolution << 2) + ((ushort)conversionMode << 4) + ((ushort)channel << 5)); WriteConfigReg(); configDirty = false; }
/// <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 int Write(I2CDevice.Configuration config, byte[] writeBuffer, int transactionTimeout) { lock (_slaveDevice) { // 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) }; // the i2c data is sent here to the device. int transferred = 0; do { transferred = _slaveDevice.Execute(writeXAction, transactionTimeout); if (transferred == 0) { throw new Exception("Could not write to device."); } }while (transferred == 0); // make sure the data was sent. if (transferred != writeBuffer.Length) { throw new Exception("Could not write to device."); } return(transferred); } }
public void SetChannel(int channel) { byte[] TxBuff = new byte[1]; int requestchannel = channel - 1; if (channel > 4) { requestchannel = requestchannel - 4; } TxBuff[0] = ChannelArray[requestchannel]; if (channel <= 4) { MyI2C.Config = conADCA; } else { MyI2C.Config = conADCB; } I2CDevice.I2CTransaction[] WriteTran = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(TxBuff) }; MyI2C.Execute(WriteTran, 1000); Thread.Sleep(5); }
/// <summary> /// 指定アドレスにデータを設定する /// </summary> /// <param name="reg">データ設定対象のアドレス</param> /// <param name="val">設定するデータ</param> protected void RegWrite(byte reg, byte val) { _wdata[0] = reg; _wdata[1] = val; _trRegWrite = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(_wdata) }; _i2C.Execute(_trRegWrite, _timeout); }
/// <summary> /// Read array of bytes at specific register from the I2C slave device. /// </summary> /// <param name="config">I2C slave device configuration.</param> /// <param name="register">The register to read bytes from.</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 ReadRegister(I2CDevice.Configuration config, byte register, byte[] readBuffer) { byte[] writeBuffer = { register }; // create an i2c write transaction to be sent to the device. I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer), I2CDevice.CreateReadTransaction(readBuffer) }; // the i2c data is sent here to the device. int transferred; lock (this.m_i2c) { this.m_i2c.Config = config; transferred = this.m_i2c.Execute(transactions, TransactionTimeout); } // make sure the data was sent. if (transferred != writeBuffer.Length + readBuffer.Length) { throw new IOException("Read Failed: " + transferred + "!=" + (writeBuffer.Length + readBuffer.Length)); } }
public BlinkMCommand(String name, char command, int argCount, int retValCount, int postExecuteDelayMillis = 0) { _postExecuteDelayMillis = postExecuteDelayMillis; _name = name; _command = command; _argCount = argCount; _retValCount = retValCount; _args = new byte[_argCount + 1]; _args[0] = (byte)_command; _retVals = new byte[_retValCount]; _transactions = new I2CDevice.I2CTransaction[_retValCount == 0 ? 1 : 2]; _transactions[0] = I2CDevice.CreateWriteTransaction(_args); if (_retValCount > 0) { _transactions[1] = I2CDevice.CreateReadTransaction(_retVals); } _expectedBytesTransferred = _args.Length + _retVals.Length; _flushTransaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(_flushBuffer) }; }
private static void SetRegister(Byte register, Byte value) { var actions = new I2CDevice.I2CTransaction[1]; actions[0] = I2CDevice.CreateWriteTransaction(new[] { register, value }); Hardware.I2CBus.Execute(_i2CConfig, actions, _i2CTimeout); }
/// <summary> /// Reads all the settings. /// </summary> /// <remarks>Reads all the settings from the FT5306</remarks> public void ReadSettings(byte StartAddr) { I2CDevice.I2CTransaction[] _readActions2 = new I2CDevice.I2CTransaction[12]; I2CDevice.I2CReadTransaction readAction2; I2CDevice.I2CWriteTransaction writeAction2; byte[] _registerValues2 = new byte[10]; byte dummy; writeAction2 = I2CDevice.CreateWriteTransaction(new byte[1] { StartAddr }); readAction2 = I2CDevice.CreateReadTransaction(_registerValues2); _readActions2 = new I2CDevice.I2CTransaction[] { writeAction2, readAction2 }; lock (_sharedBus) { _sharedBus.Config = _busConfiguration; if (_sharedBus.Execute(_readActions2, 500) == 0) { Debug.Print("Failed to perform I2C transaction"); } } dummy = _registerValues2[0]; }
/// <summary> /// ''' Read one byte at the address /// ''' </summary> private static string ReadByte(int Address) { try { I2cBus.Device.Config = Configuration; var Data = new byte[1]; var xActions = new I2CDevice.I2CTransaction[1]; xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { HighByte(Address), LowByte(Address) }); Thread.Sleep(5); I2cBus.Device.Execute(xActions, 1000); xActions[0] = I2CDevice.CreateReadTransaction(Data); Thread.Sleep(5); I2cBus.Device.Execute(xActions, 1000); // Convert the byte to string return(Strings.ChrW(Data[0]).ToString()); } catch (Exception ex) { return("Error: ReadBtye " + ex.ToString()); } }
public int WriteRead(byte[] writeBuffer, ref byte[] readBuffer) { I2CDevice.I2CTransaction[] xaction = new I2CDevice.I2CTransaction[2]; xaction[0] = I2CDevice.CreateWriteTransaction(writeBuffer); xaction[1] = I2CDevice.CreateReadTransaction(readBuffer); return(_device.Execute(xaction, _timeout)); }
private static void Write(byte[] bytesToWrite) { var actions = new I2CDevice.I2CTransaction[1]; actions[0] = I2CDevice.CreateWriteTransaction(bytesToWrite); Hardware.I2CBus.Execute(_i2CConfiguration, actions, _i2CTimeOut); }
public double AcquisitionTemperature() { byte[] temp = new byte[2]; //Besoin de 2 octets double realtemp = 0.0; //15 chiffres de précision(virgule flottante) MyI2C.Config = Temperature; xActions = new I2CDevice.I2CTransaction[3]; //Création de 3 transactions byte[] commande1 = new byte[1] { 238 }; //Commande EE(Début de conversion) byte[] commande2 = new byte[1] { 170 }; //Commande AA(Lecture de la température) //Transactions xActions[0] = I2CDevice.CreateWriteTransaction(commande1); //Représente une transaction I2c qui écrit dans le dispositif adressé xActions[1] = I2CDevice.CreateWriteTransaction(commande2); xActions[2] = I2CDevice.CreateReadTransaction(temp); //Représente une transaction I2c qui lit le dispositif adressé MyI2C.Execute(xActions, 1000); //Accès au bus I2c avec un délai de 1sec realtemp = temp[0]; if (temp[1] != 0) //(opérateur d'inégalité) { realtemp = realtemp + 0.5; } return(realtemp); }
public void showAddress() { I2CDevice.I2CTransaction[] write = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0x90 }) }; m_display.Execute(write, 1000); }