This entry's latest version is outdated and must be revised. Please see the documentation for the latest API.

I2CDevice extension methods by MarkusFriedel

Jan. 22, 2013   |   Snippet   |   Licensed as Apache 2.0   |   1473 views

i2c

This class contains several extension methods for I2CDevice in order to read and write register based data.

For every extension method "<MethodName>" there's also an extension method "Try<MethodName", whereas the "Try..." method returns a boolean value indicating, whether the transactions have been successfull, while the methods not starting with Try... throw an Exception.

Those methods allow you to
read / write registers,
read / write consecutive registers,
read / write single bits in registers,
read / write multiple bits in registers

It also includes and uses the extension methods Execute and Broadcast published by "Bec a Fuel", which provide a simple way to address multiple I2C slaves.

Comments or questions?   Discuss on the forum.



Author Version Date
MarkusFriedel 1 01/22 '13 at 07:45am
1 — Source
  1. using System;
  2. using Microsoft.SPOT;
  3. using Microsoft.SPOT.Hardware;
  4.  
  5. // Extension Methods Execute and Broadcast have been published by "Bec a Fuel"
  6.  
  7. namespace I2CExtensions
  8. {
  9. public static class I2CExtension
  10. {
  11. public static int Execute(this I2CDevice device, I2CDevice.Configuration config, I2CDevice.I2CTransaction[] transactions, int timeout)
  12. {
  13. var ConfigSav = device.Config;
  14. device.Config = config;
  15. var RetVal = device.Execute(transactions, timeout);
  16. device.Config = ConfigSav;
  17. return RetVal;
  18. }
  19.  
  20. public static void Broadcast(this I2CDevice device, I2CDevice.Configuration[] config, I2CDevice.I2CTransaction[] transactions, int timeout)
  21. {
  22. var ConfigSav = device.Config;
  23. foreach (I2CDevice.Configuration cnf in config)
  24. {
  25. device.Config = cnf;
  26. device.Execute(transactions, timeout);
  27. }
  28. device.Config = ConfigSav;
  29. }
  30.  
  31. public static bool TrySetRegister(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, params byte[] value)
  32. {
  33. byte[] data = new byte[value.Length + 1];
  34.  
  35. data[0] = register;
  36. for (int i = 0; i < value.Length; i++)
  37. {
  38. data[i + 1] = value[i];
  39. }
  40.  
  41. int result = device.Execute(config, new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(data) }, timeout);
  42. return (result == data.Length);
  43. }
  44.  
  45. public static void SetRegister(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, params byte[] value)
  46. {
  47. if (!TrySetRegister(device, config, timeout, register, value))
  48. throw new Exception();
  49. }
  50.  
  51. public static bool TryGetRegister(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, out byte result)
  52. {
  53. byte[] output = new byte[1];
  54. result = 0;
  55.  
  56. int bytesTransfered = device.Execute(config, new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { register }) }, timeout);
  57. bytesTransfered += device.Execute(config, new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(output) }, timeout);
  58.  
  59. if (bytesTransfered != 2)
  60. return false;
  61.  
  62. result = output[0];
  63. return true;
  64. }
  65.  
  66. public static byte GetRegister(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register)
  67. {
  68. byte result;
  69. if (TryGetRegister(device, config, timeout, register, out result))
  70. return result;
  71. else
  72. throw new Exception();
  73. }
  74.  
  75. public static bool TryGetRegisters(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte[] result)
  76. {
  77. int bytesTransfered = device.Execute(config, new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { register }) }, timeout);
  78. bytesTransfered += device.Execute(config, new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(result) }, timeout);
  79.  
  80. if (bytesTransfered != result.Length + 1)
  81. return false;
  82.  
  83. return true;
  84. }
  85.  
  86. public static byte[] GetRegisters(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, int size)
  87. {
  88. byte[] result = new byte[size];
  89.  
  90. int bytesTransfered = device.Execute(config, new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { register }) }, timeout);
  91. bytesTransfered += device.Execute(config, new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(result) }, timeout);
  92.  
  93. if (bytesTransfered != result.Length + 1)
  94. {
  95. throw new Exception();
  96. }
  97. return result;
  98. }
  99.  
  100. public static bool TrySetBit(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte bitNum, bool value)
  101. {
  102. byte oldValue;
  103. if (!device.TryGetRegister(config, timeout, register, out oldValue))
  104. return false;
  105.  
  106. byte newValue = (value == true ? (byte)(oldValue | (1 << bitNum)) : (byte)(oldValue & ~(1 << bitNum)));
  107.  
  108. return device.TrySetRegister(config, timeout, register, newValue);
  109. }
  110.  
  111. public static void SetBit(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte bitNum, bool value)
  112. {
  113. if (!device.TrySetBit(config, timeout, register, bitNum, value))
  114. throw new Exception();
  115. }
  116.  
  117. public static bool TrySetBits(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte startBit, byte length, byte value)
  118. {
  119. byte oldValue;
  120. if (!device.TryGetRegister(config, timeout, register, out oldValue))
  121. return false;
  122. byte mask = (byte)(((1 << length) - 1) << (startBit - length + 1));
  123. value <<= (startBit - length + 1);
  124. value &= mask;
  125. oldValue &= (byte)~mask;
  126. oldValue |= value;
  127. return device.TrySetRegister(config, timeout, register, oldValue);
  128. }
  129.  
  130. public static void SetBits(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte bitstart, byte length, byte value)
  131. {
  132. if (!device.TrySetBits(config, timeout, register, bitstart, length, value))
  133. throw new Exception();
  134. }
  135.  
  136. public static bool TryGetBit(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte bitNum, out bool result)
  137. {
  138.  
  139. byte value;
  140. if (!device.TryGetRegister(config, timeout, register, out value))
  141. {
  142. result = false;
  143. return false;
  144. }
  145.  
  146. result = (value & (1 << bitNum)) > 0;
  147.  
  148. return true;
  149. }
  150.  
  151. public static bool GetBit(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte bitNum)
  152. {
  153. bool result;
  154. if (device.TryGetBit(config, timeout,register, bitNum, out result))
  155. return result;
  156. else
  157. throw new Exception();
  158. }
  159.  
  160. public static bool TryGetBits(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte bitStart, byte length, out byte data)
  161. {
  162. byte value;
  163. if (!device.TryGetRegister(config, timeout, register, out value))
  164. {
  165. data = 0;
  166. return false;
  167. }
  168. byte mask = (byte)(((1 << length) - 1) << (bitStart - length + 1));
  169. value &= mask;
  170. value >>= (bitStart - length + 1);
  171.  
  172. data = value;
  173. return true;
  174. }
  175.  
  176. public static byte GetBits(this I2CDevice device, I2CDevice.Configuration config, int timeout,byte register, byte bitStart, byte length)
  177. {
  178. byte result;
  179. if (device.TryGetBits(config, timeout,register, bitStart, length, out result))
  180. return result;
  181. else
  182. throw new Exception();
  183. }
  184. }
  185. }
  186.