public void writeChar(char val) { I2CDevice.I2CTransaction[] write = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { (byte)val }) }; m_display.Execute(write, 1000); }
/// /// 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]); } }
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); }
/// <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 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 !!! }
private static void Write(byte[] bytesToWrite) { var actions = new I2CDevice.I2CTransaction[1]; actions[0] = I2CDevice.CreateWriteTransaction(bytesToWrite); Hardware.I2CBus.Execute(_i2CConfiguration, actions, _i2CTimeOut); }
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()); }
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."); } }
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); }
/// <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)); }
/// <summary> /// Reads the raw counts from the ADC /// </summary> /// <param name="number">Number of bytes to read (four if 18-bit conversion else three) </param> /// <returns>Result of 12, 14, 16 or 18-bit conversion</returns> double dataReadRaw(byte number) { Int32 data = 0; byte[] inbuffer = new byte[number]; I2CDevice.I2CReadTransaction readTransaction = I2CDevice.CreateReadTransaction(inbuffer); I2CDevice.I2CTransaction[] xAction = new I2CDevice.I2CTransaction[] { readTransaction }; _i2cBus = new I2CDevice(_config); int transferred = _i2cBus.Execute(xAction, _transactionTimeOut); _i2cBus.Dispose(); if (transferred < inbuffer.Length) { throw new System.IO.IOException("Error i2cBus " + _sla); } else if ((inbuffer[number - 1] & 0x80) == 0) // End of conversion { if (_resolution == SampleRate.EighteenBits) { data = (((Int32)inbuffer[0]) << 16) + (((Int32)inbuffer[1]) << 8) + (Int32)inbuffer[2]; } else { data = (((Int32)inbuffer[0]) << 8) + (Int32)inbuffer[1]; } _endOfConversion = true; return(data &= _dataMask); } else { _endOfConversion = false; return(0); // return something } }
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); }
public void clear() { I2CDevice.I2CTransaction[] write = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0x82 }) }; m_display.Execute(write, 1000); }
public void showAddress() { I2CDevice.I2CTransaction[] write = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0x90 }) }; m_display.Execute(write, 1000); }
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 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); }
/// <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()); } }
private static int writePause = 5; // Mandatory after each Write transaction !!! public static ArrayList Scan(this I2CDevice device, ushort startAddress, ushort endAddress, int clockRate, int timeout) { ArrayList addresses = new ArrayList(); for (ushort address = startAddress; address <= endAddress; address++) { var ConfigSav = device.Config; device.Config = new I2CDevice.Configuration(address, clockRate); byte x = 0; var xActions = new I2CDevice.I2CTransaction[1]; xActions[0] = I2CDevice.CreateReadTransaction(new byte[1] { x }); int result = device.Execute(xActions, timeout); if (result != 0) { addresses.Add(address); } device.Config = ConfigSav; //Thread.Sleep(writePause); // Mandatory after each Write transaction !!! } return(addresses); }
/// <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 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> /// 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 int Read(I2CDevice.Configuration config, byte[] readBuffer, int transactionTimeout) { lock (_slaveDevice) { // 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) }; // 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."); } return(transferred); } }
/// <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 static void Main() { // // Create a new I2C device for the TMP102 on address 0x48 with the clock // running at 50 KHz. // I2CDevice tmp102 = new I2CDevice(new I2CDevice.Configuration(0x48, 50)); // // Create a transaction to read two bytes of data from the TMP102 sensor. // byte[] buffer = new byte[2]; I2CDevice.I2CTransaction[] reading = new I2CDevice.I2CTransaction[1]; reading[0] = I2CDevice.CreateReadTransaction(buffer); while (true) { // // Read the temperature. // int bytesRead = tmp102.Execute(reading, 100); // // Convert the reading into Centigrade and Fahrenheit. // int sensorReading = ((buffer[0] << 4) | (buffer[1]) >> 4); double centigrade = sensorReading * 0.0625; double fahrenheit = centigrade * 1.8 + 32; // // Display the readings in the debug window and pause before repeating. // Debug.Print(centigrade.ToString() + " C / " + fahrenheit.ToString() + " F"); Thread.Sleep(1000); } }
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 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"); } } }
/// <summary> /// Read byte from uALFAT /// </summary> /// <param name="b">Read byte</param> /// <param name="timeout">Timeout</param> /// <returns>True on succes, False on error</returns> public bool ReadByte(out byte b, long timeout) { // Store start time long start = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; // Buffer used for reading byte[] buf = new byte[1]; // Start with 0xff; b = 0xff; do { // Create read transaction I2CDevice.I2CTransaction[] ReadByte = new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(buf) }; // Execute and check if byte is read succesfull int bytesread = _i2cconnection.Execute(ReadByte, 100); if (bytesread != 1) { return(false); } // Get read byte b = buf[0]; // Check if timed otu if ((start + timeout) < (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond)) { return(false); } } while (b == 0xff); // Check for half-data token if (b == 0xfe) { // Create read transaction I2CDevice.I2CTransaction[] ReadByte = new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(buf) }; // Execute and check if byte is read succesfull int bytesread = _i2cconnection.Execute(ReadByte, 100); if (bytesread != 1) { return(false); } // Get read byte b = buf[0]; // Check for second half data token, if not send 0xff if (b != 0xfe) { b = 0xff; } return(true); } // Succes return(true); }
/// <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]; }
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); }
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)); }
public void setDots(bool dot0, bool dot1, bool dot2, bool dot3) { m_dots = 0; if (dot0) { m_dots |= 1 << 1; } if (dot1) { m_dots |= 1 << 2; } if (dot2) { m_dots |= 1 << 3; } if (dot3) { m_dots |= 1 << 4; } I2CDevice.I2CTransaction[] write = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0x85, m_dots }) }; m_display.Execute(write, 1000); }