I have used the i2cmaster.h when i was working with arduino. Now i try to convert it to C# but i dont understand it.
Can some pleas help me convert this code to c#.
Having just figured out I2C myself, I can tell you that you don't have the same (low) level of control over I2C in C# (NETMF). The I2CTransaction wraps the complexities of the protocol and handles the start/stop/acks for you.
Here's some basic test code that I used to read/write a 24LC32A serial EEPROM:
public static void Main() { byte[] buffer = new byte[1]; int bytesWritten = 0; // Create the I2C device (device address: 0x54, clock rate: 50Khz) I2CDevice.Configuration i2cConfig = new I2CDevice.Configuration(0x54, 50); I2CDevice eeprom = new I2CDevice(i2cConfig); // Write the letter 'A' (0x41) to address 0x0000 on the eeprom I2CDevice.I2CTransaction[] writeTx = new I2CDevice.I2CTransaction[] { CreateWriteTransaction(new byte[] { 0x00, 0x00, 0x41 }, 0, 0) }; bytesWritten = eeprom.Execute(writeTx, 1000); // Read the byte at address 0x0000 on the eeprom I2CDevice.I2CTransaction[] readTx = new I2CDevice.I2CTransaction[] { CreateReadTransaction(buffer, 0x0000, 2) }; do { bytesWritten = eeprom.Execute(readTx, 1000); } while (bytesWritten == 0); Debug.Print(buffer[0].ToString()); Thread.Sleep(Timeout.Infinite); } #region see: http://forums.netduino.com/index.php?/topic/944-i2c-internaladdress-repeated-start-bit-support/ static I2CDevice.I2CWriteTransaction CreateWriteTransaction(byte[] buffer, uint internalAddress, byte internalAddressSize) { I2CDevice.I2CWriteTransaction writeTransaction = I2CDevice.CreateWriteTransaction(buffer); Type writeTransactionType = typeof(I2CDevice.I2CWriteTransaction); FieldInfo fieldInfo = writeTransactionType.GetField("Custom_InternalAddress", BindingFlags.NonPublic | BindingFlags.Instance); fieldInfo.SetValue(writeTransaction, internalAddress); fieldInfo = writeTransactionType.GetField("Custom_InternalAddressSize", BindingFlags.NonPublic | BindingFlags.Instance); fieldInfo.SetValue(writeTransaction, internalAddressSize); return writeTransaction; } static I2CDevice.I2CReadTransaction CreateReadTransaction(byte[] buffer, uint internalAddress, byte internalAddressSize) { I2CDevice.I2CReadTransaction readTransaction = I2CDevice.CreateReadTransaction(buffer); Type readTransactionType = typeof(I2CDevice.I2CReadTransaction); FieldInfo fieldInfo = readTransactionType.GetField("Custom_InternalAddress", BindingFlags.NonPublic | BindingFlags.Instance); fieldInfo.SetValue(readTransaction, internalAddress); fieldInfo = readTransactionType.GetField("Custom_InternalAddressSize", BindingFlags.NonPublic | BindingFlags.Instance); fieldInfo.SetValue(readTransaction, internalAddressSize); return readTransaction; } #endregion
Make sure you have the 4.1.1 ALPHA 7 firmware.
Hope that helps.