A while back I wrote a post about some problems I was having with a Silicon Labs Si7005 device and now I have had some time to package up the code.
My code strobes the I2C SDA line and then initiates a request that will always fail, from there on everything works as expected.
public SiliconLabsSI7005(byte deviceId = DeviceIdDefault, int clockRateKHz = ClockRateKHzDefault, int transactionTimeoutmSec = TransactionTimeoutmSecDefault) { this.deviceId = deviceId; this.clockRateKHz = clockRateKHz; this.transactionTimeoutmSec = transactionTimeoutmSec; using (OutputPort i2cPort = new OutputPort(Pins.GPIO_PIN_SDA, true)) { i2cPort.Write(false); Thread.Sleep(250); } using (I2CDevice device = new I2CDevice(new I2CDevice.Configuration(deviceId, clockRateKHz))) { byte[] writeBuffer = { RegisterIdDeviceId }; byte[] readBuffer = new byte[1]; // The first request always fails I2CDevice.I2CTransaction[] action = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer), I2CDevice.CreateReadTransaction(readBuffer) }; if( device.Execute(action, transactionTimeoutmSec) == 0 ) { // throw new ApplicationException("Unable to send get device id command"); } } }
This is how the driver should be used in an application
public static void Main() { SiliconLabsSI7005 sensor = new SiliconLabsSI7005(); while (true) { double temperature = sensor.Temperature(); double humidity = sensor.Humidity(); Debug.Print("T:" + temperature.ToString("F1") + " H:" + humidity.ToString("F1")); Thread.Sleep(5000); } }
I have added code to catch failures and there is a sample application in the project. For a project I’m working on I will modify the code to use one of the I2C sharing libraries so I can have a number of devices on the bus
Pingback: Netduino 3 Wifi pollution Sensor Part 1 | devMobile's blog
Pingback: Mikrobus.Net Quail and Weather Click | devMobile's blog
Pingback: Mikrobus.Net Quail and Weather Click | devMobile's blog
Nice job!
Thanks for sharing.