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 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"); } } }
public void WriteByte(ushort address, byte data) { byte[] writeBuffer = { HighAddress(address), LowAddress(address), data }; I2CDevice.I2CWriteTransaction write = I2CDevice.CreateWriteTransaction(writeBuffer); I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { write }; m_transfer_count = I2C_device.Execute(transactions, m_timeout); m_transfer_type = transfer_type.write; }
private static void WriteToCompass(byte[] command) { I2CDevice.I2CWriteTransaction writeTransaction = I2CDevice.CreateWriteTransaction(command); I2CDevice.I2CTransaction[] transaction = new I2CDevice.I2CTransaction[] { writeTransaction }; compass.Execute(transaction, 1000); compass.Execute(transaction, 1000); }
private I2CDevice.I2CWriteTransaction CreateInternalAddressWriteTransaction(byte[] buffer, uint internalAddress, byte internalAddressSize) { I2CDevice.I2CWriteTransaction writeTransaction = I2CDevice.CreateWriteTransaction(buffer); ModifyToRepeatedStartTransaction(internalAddress, internalAddressSize, writeTransaction, typeof(I2CDevice.I2CWriteTransaction)); return(writeTransaction); }
public void WritePage(ushort Address, EEPROM_I2C_Page EEPROM_Page) { Address = PageAddress(Address); byte[] dummyAddress = { HighAddress(Address), LowAddress(Address) }; I2CDevice.I2CWriteTransaction writeCounter = I2CDevice.CreateWriteTransaction(dummyAddress); I2CDevice.I2CWriteTransaction pageWrite = I2CDevice.CreateWriteTransaction(EEPROM_Page.Page()); I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { writeCounter, pageWrite }; m_transfer_count = I2C_device.Execute(transactions, m_timeout) - 2; m_transfer_type = transfer_type.write; }
protected byte Read(byte address, byte[] buffer) { I2CDevice.I2CWriteTransaction write = I2CDevice.CreateWriteTransaction(new Byte[] { address }); I2CDevice.I2CReadTransaction read = I2CDevice.CreateReadTransaction(buffer); I2CDevice.I2CTransaction[] readTransaction = new I2CDevice.I2CTransaction[] { write, read }; var result = I2CBus.Execute(Config, readTransaction, 1000); return(buffer[0]); }
public void InitializeI2C() { myI2CDeviceConfiguration = new I2CDevice.Configuration(0x0, 0x0); myI2CDevice = new I2CDevice(myI2CDeviceConfiguration); #if false myI2CDeviceRead = new I2CDevice.I2CReadTransaction( ); myI2CDeviceTrans = new I2CDevice.I2CTransaction(); myI2CDeviceWrite = new I2CDevice.I2CWriteTransaction(); #endif }
public void SetXOFF(short xoff) { Engine.getInstance().Info("Fin x" + xoff); byte[] XOFF = FromShortC2(xoff); I2CDevice.I2CWriteTransaction XOFFM = I2CDevice.CreateWriteTransaction(new byte[] { 0x09, XOFF[1] }); I2CDevice.I2CWriteTransaction XOFFL = I2CDevice.CreateWriteTransaction(new byte[] { 0x0A, XOFF[0] }); int i = magnetometer.Execute(new I2CDevice.I2CTransaction[] { XOFFM, XOFFL }, 1000); Debug.Print("X: " + XOFF.ToString()); }
public void SetYOFF(short yoff) { Engine.getInstance().Info("Fin y" + yoff); byte[] YOFF = FromShortC2(yoff); I2CDevice.I2CWriteTransaction YOFFM = I2CDevice.CreateWriteTransaction(new byte[] { 0x0B, YOFF[1] }); I2CDevice.I2CWriteTransaction YOFFL = I2CDevice.CreateWriteTransaction(new byte[] { 0x0C, YOFF[0] }); Engine.getInstance().Info("Y1:" + YOFF[1] + "Y0:" + YOFF[0]); int i = magnetometer.Execute(new I2CDevice.I2CTransaction[] { YOFFM, YOFFL }, 1000); Debug.Print("Y:" + YOFF.ToString()); }
public byte RandomByteRead(ushort address) { byte[] dummyAddress = { HighAddress(address), LowAddress(address) }; byte[] readBuffer = { 0x0 }; // // non-data write relocates internal EEPROM counter for random read // I2CDevice.I2CReadTransaction read = I2CDevice.CreateReadTransaction(readBuffer); I2CDevice.I2CWriteTransaction writeCounter = I2CDevice.CreateWriteTransaction(dummyAddress); I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { writeCounter, read }; m_transfer_count = I2C_device.Execute(transactions, m_timeout) - 2; m_transfer_type = transfer_type.read; return(readBuffer[0]); }
public void ReadSequential(ushort Address, uint Length, ref byte[] ReadBuffer) { if ( ((Address + Length) > EEPROM_Size) || (ReadBuffer.Length < Length)) { return; } byte[] dummyAddress = { HighAddress(Address), LowAddress(Address) }; I2CDevice.I2CReadTransaction read = I2CDevice.CreateReadTransaction(ReadBuffer); I2CDevice.I2CWriteTransaction writeCounter = I2CDevice.CreateWriteTransaction(dummyAddress); I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { writeCounter, read }; m_transfer_count = I2C_device.Execute(transactions, m_timeout) - 2; m_transfer_type = transfer_type.read; }
void writeConfRegister(byte value) { byte[] outbuffer = { value }; I2CDevice.I2CWriteTransaction writeTransaction = I2CDevice.CreateWriteTransaction(outbuffer); I2CDevice.I2CTransaction[] xAction = new I2CDevice.I2CTransaction[] { writeTransaction }; _i2cBus = new I2CDevice(_config); int transferred = _i2cBus.Execute(xAction, _transactionTimeOut); _i2cBus.Dispose(); if (transferred < outbuffer.Length) { throw new System.IO.IOException("Error i2cBus " + _sla); } }
/// <summary> /// Writes an arbitrary RTC or RAM register /// </summary> /// <param name="address">Register address between 0x00 and 0x3f</param> /// <param name="val">The value of the byte to write at that address</param> public void WriteRegister(byte address, byte val) { if (address > DS1307_RAM_END_ADDRESS) { throw new ArgumentOutOfRangeException("Invalid register address"); } var transaction = new I2CDevice.I2CWriteTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { address, (byte)val }) }; if (clock.Execute(transaction, DS1307_I2C_TRANSACTION_TIMEOUT_MS) == 0) { throw new Exception("I2C write transaction failed"); } }
protected void Write(params byte[] buffer) { var transactions = new I2CDevice.I2CWriteTransaction[] { I2CDevice.CreateWriteTransaction(buffer) }; var resultLength = Device.Execute(transactions, Timeout); while (resultLength < buffer.Length) { var extendedBuffer = new byte[buffer.Length - resultLength]; Array.Copy(buffer, resultLength, extendedBuffer, 0, extendedBuffer.Length); transactions = new I2CDevice.I2CWriteTransaction[] { I2CDevice.CreateWriteTransaction(extendedBuffer) }; resultLength += Device.Execute(transactions, Timeout); } if (resultLength != buffer.Length) { throw new Exception("Could not write to device."); } }
private void WriteRam(byte address, params byte[] data) { if (data == null) { throw new ArgumentNullException("data"); } byte[] buffer = Utility.CombineArrays(new[] { address }, data); I2CDevice.I2CWriteTransaction transaction = I2CDevice.CreateWriteTransaction(buffer); var transactions = new I2CDevice.I2CTransaction[] { transaction }; int actual = _device.Execute(transactions, Timeout); int expected = buffer.Length; if (actual != expected) { throw new IOException("Unexpected I2C transaction result"); } }
/// <summary> /// Sets the time on the clock using the datetime object. Milliseconds are not used. /// </summary> /// <param name="dt">A DateTime object used to set the clock</param> public void Set(DateTime dt) { var transaction = new I2CDevice.I2CWriteTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { DS1307_RTC_START_ADDRESS, DecToBcd(dt.Second), DecToBcd(dt.Minute), DecToBcd(dt.Hour), DecToBcd((int)dt.DayOfWeek), DecToBcd(dt.Day), DecToBcd(dt.Month), DecToBcd(dt.Year - 2000) }) }; if (clock.Execute(transaction, DS1307_I2C_TRANSACTION_TIMEOUT_MS) == 0) { throw new Exception("I2C write transaction failed"); } }
/// <summary> /// Writes to the clock's user RAM registers as a block /// </summary> /// <param name="buffer">A byte buffer of size DS1307_RAM_SIZE</param> public void SetRAM(byte[] buffer) { if (buffer.Length != DS1307_RAM_SIZE) { throw new ArgumentOutOfRangeException("Invalid buffer length"); } // Allocate a new buffer large enough to include the RAM start address byte and the payload var TrxBuffer = new byte[sizeof(byte) /*Address byte*/ + DS1307_RAM_SIZE]; // Set the RAM start address TrxBuffer[0] = DS1307_RAM_START_ADDRESS; // Copy the user buffer after the address buffer.CopyTo(TrxBuffer, 1); // Write to the clock's RAM var transaction = new I2CDevice.I2CWriteTransaction[] { I2CDevice.CreateWriteTransaction(TrxBuffer) }; if (Clock.Execute(transaction, TimeOutMs) == 0) { throw new Exception("I2C write transaction failed"); } }
public void read(Object state) { monitor = new object(); byte[] buffer = new byte[bufferSize]; I2CDevice.I2CWriteTransaction transactionWrite = I2CDevice.CreateWriteTransaction(new byte[] { 0x01 }); I2CDevice.I2CReadTransaction transactionRead = I2CDevice.CreateReadTransaction(buffer); int i = magnetometer.Execute(new I2CDevice.I2CTransaction[] { transactionWrite, transactionRead }, 1000); if (i == 8 && buffer[6] == 0xC4) { //X magX = ToShortC2(new byte[] { buffer[1], buffer[0] }); //Y magY = ToShortC2(new byte[] { buffer[3], buffer[2] }); //Z magZ = ToShortC2(new byte[] { buffer[5], buffer[4] }); MAGHeadingRad = exMath.Atan2(-1 * magY, magX); /* * if ((magX == 0) && (magY < 0)) * MAGHeadingRad = exMath.PI / 2.0; * if ((magX == 0) && (magY > 0)) * MAGHeadingRad = 3.0 * exMath.PI / 2.0; * if (magX < 0) * MAGHeadingRad = exMath.PI - exMath.Atan2(magX, magY); * if ((magX > 0) && (magY < 0)) * MAGHeadingRad = -exMath.Atan2(magX, magY); * if ((magX > 0) && (magY > 0)) * MAGHeadingRad = 2.0 * exMath.PI - exMath.Atan2(magX, magY); */ MAGHeadingDeg = (float)exMath.ToDeg((float)MAGHeadingRad); Debug.Print("X: " + magX.ToString() + " Y: " + magY.ToString() + " Z: " + magZ.ToString() + " " + MAGHeadingDeg.ToString()); //Debug.Print(buffer[0] + " " + buffer[1] + " " + buffer[2] + " " + buffer[3]); //Debug.Print("Y: " + magY.ToString()); //Engine.getInstance().Debug("X: " + magX.ToString() + " Y: " + magY.ToString() + " " + MAGHeadingDeg.ToString()); } }
public static void Main() { I2CDevice.Configuration config = new I2CDevice.Configuration(58, //address 100 //clockrate in KHz ); I2CDevice device = new I2CDevice(config); //prepare buffer to write byte AA byte[] outBuffer = new byte[] { 0xAA }; I2CDevice.I2CWriteTransaction writeTransaction = device.CreateWriteTransaction(outBuffer); //prepare buffer to read four bytes byte[] inBuffer = new byte[4]; I2CDevice.I2CReadTransaction readTransaction = device.CreateReadTransaction(inBuffer); //execute both transactions I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { writeTransaction, readTransaction }; int transferred = device.Execute(transactions, 100 //timeout in ms ); //transferred bytes should be 1 + 4 = 5 }
public Magnetometer() { monitor = new object(); magnetometer = new I2CDevice(new I2CDevice.Configuration(address, freq)); //I2CDevice.I2CWriteTransaction activeAutoReset_RAWMode = I2CDevice.CreateWriteTransaction(new byte[] { 0x12, 0xC0 }); I2CDevice.I2CWriteTransaction activeMode = I2CDevice.CreateWriteTransaction(new byte[] { 0x10, 0x01 }); I2CDevice.I2CWriteTransaction correct = I2CDevice.CreateWriteTransaction(new byte[] { 0x11, 0x00 }); //I2CDevice.I2CWriteTransaction activeMode_FastMode = I2CDevice.CreateWriteTransaction(new byte[] { 0x10, 0x05 }); int i = magnetometer.Execute(new I2CDevice.I2CTransaction[] { activeMode, correct }, 1000); //2285 //-3062 byte[] XOFF = FromShortC2(2630); I2CDevice.I2CWriteTransaction XOFFM = I2CDevice.CreateWriteTransaction(new byte[] { 0x09, XOFF[1] }); I2CDevice.I2CWriteTransaction XOFFL = I2CDevice.CreateWriteTransaction(new byte[] { 0x0A, XOFF[0] }); byte[] YOFF = FromShortC2(-3192); I2CDevice.I2CWriteTransaction YOFFM = I2CDevice.CreateWriteTransaction(new byte[] { 0x0B, YOFF[1] }); I2CDevice.I2CWriteTransaction YOFFL = I2CDevice.CreateWriteTransaction(new byte[] { 0x0C, YOFF[0] }); int i2 = magnetometer.Execute(new I2CDevice.I2CTransaction[] { XOFFM, XOFFL, YOFFM, YOFFL }, 1000); update = new Timer(new TimerCallback(read), new object(), 1000, 500); }
/// <summary> /// Writes an array of bytes to the I2C device. /// </summary> /// <param name="writeBuffer">The array of bytes that will be sent to the I2C device.</param> /// <param name="timeout">The amount of time, in milliseconds, that the system will wait before resuming execution of the transaction.</param> /// <returns>The number of bytes of data transferred in the transaction.</returns> public int Write(byte[] writeBuffer, int timeout) { var transactions = new I2CDevice.I2CWriteTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer) }; return(Interface.Execute(transactions, timeout)); }
public void Write(byte[] frame) { st = I2CDevice.CreateWriteTransaction(frame); matrix8x8.Execute(new I2CDevice.I2CTransaction[] { st }, timeOut); }