Skip to content
This repository was archived by the owner on Apr 10, 2020. It is now read-only.

HTU21DF driver #1

Merged
merged 1 commit into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ TestResults
**.suo
**.sdf
*.sln.docstates
.vs/*

# resharper
/_[Rr]e[Ss]harper.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Globalization;

namespace Raspberry.IO.Components.Sensors.Temperature.Dht
namespace Raspberry.IO.Components
{
public class InvalidChecksumException : Exception
{
Expand Down
3 changes: 2 additions & 1 deletion Raspberry.IO.Components/Raspberry.IO.Components.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,15 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sensors\Distance\HcSr04\HcSr04Connection.cs" />
<Compile Include="Sensors\Distance\HcSr04\Units.cs" />
<Compile Include="Sensors\Humidity\Htu21df\Htu21dfConnection.cs" />
<Compile Include="Sensors\Pressure\Bmp085\Bmp085Data.cs" />
<Compile Include="Sensors\Pressure\Bmp085\Bmp085I2cConnectionExtensionMethods.cs" />
<Compile Include="Sensors\Pressure\Bmp085\Bmp085Precision.cs" />
<Compile Include="Sensors\Temperature\Dht\Dht11Connection.cs" />
<Compile Include="Sensors\Temperature\Dht\Dht22Connection.cs" />
<Compile Include="Sensors\Temperature\Dht\DhtConnection.cs" />
<Compile Include="Sensors\Temperature\Dht\DhtData.cs" />
<Compile Include="Sensors\Temperature\Dht\InvalidChecksumException.cs" />
<Compile Include="InvalidChecksumException.cs" />
<Compile Include="Sensors\Temperature\Ds18b20\Ds18b20Connection.cs" />
<Compile Include="Sensors\VariableResistiveDividerConnection.cs" />
<Compile Include="Sensors\Temperature\Tmp36\Tmp36Connection.cs" />
Expand Down
183 changes: 183 additions & 0 deletions Raspberry.IO.Components/Sensors/Humidity/Htu21df/Htu21dfConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
using System;
using System.Threading;
using Raspberry.IO.Components.Sensors.Temperature.Dht;
using Raspberry.IO.InterIntegratedCircuit;

namespace Raspberry.IO.Components.Sensors.Humidity.Htu21df
{
/// <summary>
/// Connection to the HTU21D-F Adafruit humidity and temperature breakout board.
/// </summary>
/// <remarks>See <see cref="https://www.adafruit.com/datasheets/1899_HTU21D.pdf"/> for more information.</remarks>///
public class Htu21dfConnection
{
#region Helpers

public enum I2cDefs : byte
{
HTU21DF_I2CADDR = 0x40,
HTU21DF_READTEMP = 0xE3,
HTU21DF_READHUM = 0xE5,
HTU21DF_READTEMP_NOHOLDMASTER = 0xF3,
HTU21DF_READHUM_NOHOLDMASTER = 0xF5,
HTU21DF_WRITEREG = 0xE6,
HTU21DF_READREG = 0xE7,
HTU21DF_RESET = 0xFE,
}

public enum I2cReadMode
{
/// <summary>
/// Default. In this mode, the SCK line is blocked during the measurement process. When measurement is complete
/// the sensor indicates by releasing SCK and the read can continue.
/// </summary>
HoldMaster,

/// <summary>
/// The sensor does not hold SCK so other I2C comms can take place on the bus. The MCU (R-Pi) has to then
/// poll for the data. NOT CURRENTLY SUPPORTED.
/// </summary>
NoHoldMaster,
}

#endregion

#region Fields

private readonly I2cDeviceConnection connection;

#endregion

#region Instance Management

/// <summary>
/// Initializes a new instance of the <see cref="Htu21dfConnection"/> class.
/// </summary>
/// <param name="connection">The connection.</param>
public Htu21dfConnection( I2cDeviceConnection connection )
{
this.connection = connection;
ReadMode = I2cReadMode.HoldMaster;
}

#endregion

#region Methods

public I2cReadMode ReadMode { get; set; }

/// <summary>
/// Init's the sensor and checks its status is OK.
/// </summary>
public void Begin()
{
Reset();

connection.WriteByte( (byte) I2cDefs.HTU21DF_READREG );
var status = connection.ReadByte();
if ( status != 0x02 )
{
throw new Exception( $"Status following reset should be 0x02. Have {status}" );
}
}

/// <summary>
/// Resets the sensor by power cycling. Takes about 15ms.
/// </summary>
private void Reset()
{
connection.WriteByte((byte)I2cDefs.HTU21DF_RESET);
Thread.Sleep(15);
}

/// <summary>
/// Reads the temperature value from the sensor.
/// </summary>
/// <returns>Temperature in degrees Centigrade.</returns>
public double ReadTemperature()
{
if ( ReadMode == I2cReadMode.NoHoldMaster )
{
throw new NotSupportedException( "No-Hold-Master read mode not supported." );
}

connection.WriteByte( (byte) I2cDefs.HTU21DF_READTEMP );

// Add delay between request and actual read
Thread.Sleep( 50 );

// Read 3 bytes; 2 bytes temp data and one byte checksum.
var readBytes = connection.Read( 3 );
CheckCrc( readBytes );

// Get data value from bytes 0 and 1.
var tVal = ( readBytes[0] << 8 ) + readBytes[1];

// Compute temp using formula from datasheet.
return ( ( tVal * 175.72 ) / 65536 ) - 46.85;
}

/// <summary>
/// Reads the humidity value from the sensor.
/// </summary>
/// <returns>The relative humidity value as a percentage.</returns>
public double ReadHumidity()
{
if (ReadMode == I2cReadMode.NoHoldMaster)
{
throw new NotSupportedException("No-Hold-Master read mode not supported.");
}

connection.WriteByte((byte)I2cDefs.HTU21DF_READHUM);

// Add delay between request and actual read
Thread.Sleep( 50 );

// Read 3 bytes; 2 bytes temp data and one byte checksum.
var readBytes = connection.Read( 3 );
CheckCrc( readBytes );

// Get data value from bytes 0 and 1.
var hVal = ( readBytes[0] << 8 ) + readBytes[1];

// Compute temp using formula from datasheet.
return ( ( (double)hVal * 125 ) / 65536 ) - 6;
}

/// <summary>
/// Calculate the CRC checksum. The result should be zero.
/// Thanks to the IoT-Playground - https://github.com/iot-playground.
/// </summary>
public static void CheckCrc(byte[] readData)
{
// Test cases from datasheet:
// sensor value = 0xDC, checkvalue is 0x79. readData = 0x00DC79.
// message = 0x683A, checkvalue is 0x7C. readData = 0x683A7C
// message = 0x4E85, checkvalue is 0x6B. readData = 0x45856B

// Create a 32-bit value with bytes of {0, val-msb, val-lsb, checksum}
var remainder = (uint) ( ( readData[0] << 16 ) + ( readData[1] << 8 ) + readData[2] );

// This is the x^8 + x^5 + x^4 + 1 polynomial (100110001), but shifted up to start at the left-hand
// side of the 24-bit value.
uint divisor = 0x988000;

for ( var i = 0; i < 16; i++ )
{
if ( (remainder & (1<<(23-i))) != 0 )
{
remainder ^= divisor;
}

divisor >>= 1;
}

if ( remainder != 0 )
{
throw new InvalidChecksumException( 0x00, remainder );
}
}

#endregion
}
}
4 changes: 2 additions & 2 deletions Raspberry.IO.InterIntegratedCircuit/I2cDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,10 @@ private static uint GetProcessorBscAddress(Processor processor)
switch (processor)
{
case Processor.Bcm2708:
case Processor.BCM2835: // <- added this one JJ FIX per RB3
return Interop.BCM2835_BSC1_BASE;

case Processor.Bcm2709:
case Processor.BCM2835: // <- added this one JJ FIX per RB3
return Interop.BCM2836_BSC1_BASE;

default:
Expand All @@ -507,10 +507,10 @@ private static uint GetProcessorGpioAddress(Processor processor)
switch (processor)
{
case Processor.Bcm2708:
case Processor.BCM2835: // <- added this one JJ FIX per RB3
return Interop.BCM2835_GPIO_BASE;

case Processor.Bcm2709:
case Processor.BCM2835: // <- added this one JJ FIX per RB3
return Interop.BCM2836_GPIO_BASE;

default:
Expand Down
20 changes: 16 additions & 4 deletions RaspberrySharp.IO.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2006
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raspberry.IO.GeneralPurpose", "Raspberry.IO.GeneralPurpose\Raspberry.IO.GeneralPurpose.csproj", "{281C71ED-C36D-408E-8BAA-75C381DC17E7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{93E87796-2109-444A-8852-7174C93E6F45}"
Expand Down Expand Up @@ -66,6 +68,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.Gpio.Ds1307", "Tests\T
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.I2C.LTC2943", "Tests\Test.I2C.LTC2943\Test.I2C.LTC2943.csproj", "{6A8D4696-3291-492C-B301-AB0AFECC9440}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.Gpio.Htu21df", "Tests\Test.Gpio.Htu21df\Test.Gpio.Htu21df.csproj", "{04E66E09-8128-4122-BBF3-17220BC75ADA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -176,13 +180,16 @@ Global
{6A8D4696-3291-492C-B301-AB0AFECC9440}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6A8D4696-3291-492C-B301-AB0AFECC9440}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6A8D4696-3291-492C-B301-AB0AFECC9440}.Release|Any CPU.Build.0 = Release|Any CPU
{04E66E09-8128-4122-BBF3-17220BC75ADA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{04E66E09-8128-4122-BBF3-17220BC75ADA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{04E66E09-8128-4122-BBF3-17220BC75ADA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{04E66E09-8128-4122-BBF3-17220BC75ADA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{15ECD485-B3FD-4B6F-8491-7489DFFC89BC} = {93E87796-2109-444A-8852-7174C93E6F45}
{C39D0CC3-C0F2-4B58-8779-2CCB5B52534C} = {93E87796-2109-444A-8852-7174C93E6F45}
{B28253A7-BB93-40F7-B41C-B4AE369174ED} = {15ECD485-B3FD-4B6F-8491-7489DFFC89BC}
{54075457-7C1D-4C8F-BE7D-CFCA34F11228} = {15ECD485-B3FD-4B6F-8491-7489DFFC89BC}
{6B5D38D3-7642-4DF9-A9AA-3AF7D00890BC} = {15ECD485-B3FD-4B6F-8491-7489DFFC89BC}
Expand All @@ -197,12 +204,17 @@ Global
{D4B87926-598E-4EAB-BA75-CD9FB253D450} = {15ECD485-B3FD-4B6F-8491-7489DFFC89BC}
{CAD5F2DD-AB2D-4145-9850-8101343E2AF5} = {15ECD485-B3FD-4B6F-8491-7489DFFC89BC}
{E401FE2A-7F73-41E7-9347-B51FEDAE71B9} = {15ECD485-B3FD-4B6F-8491-7489DFFC89BC}
{E9412139-F9EA-4E39-AB7C-CE775ED06C82} = {15ECD485-B3FD-4B6F-8491-7489DFFC89BC}
{6A8D4696-3291-492C-B301-AB0AFECC9440} = {15ECD485-B3FD-4B6F-8491-7489DFFC89BC}
{C39D0CC3-C0F2-4B58-8779-2CCB5B52534C} = {93E87796-2109-444A-8852-7174C93E6F45}
{17A2A965-36FA-4CDB-B2D6-AC69C9E9857F} = {C39D0CC3-C0F2-4B58-8779-2CCB5B52534C}
{0BB6C3CE-422E-49F2-9165-DEC06AFE1B1A} = {C39D0CC3-C0F2-4B58-8779-2CCB5B52534C}
{99EB3D1A-F0B7-454E-BB50-9B2F5349BC5B} = {C39D0CC3-C0F2-4B58-8779-2CCB5B52534C}
{CAF876A0-0FCB-44F7-96F1-53704CB6015F} = {C39D0CC3-C0F2-4B58-8779-2CCB5B52534C}
{E9412139-F9EA-4E39-AB7C-CE775ED06C82} = {15ECD485-B3FD-4B6F-8491-7489DFFC89BC}
{6A8D4696-3291-492C-B301-AB0AFECC9440} = {15ECD485-B3FD-4B6F-8491-7489DFFC89BC}
{04E66E09-8128-4122-BBF3-17220BC75ADA} = {15ECD485-B3FD-4B6F-8491-7489DFFC89BC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7B21C3A9-4153-4372-8A05-012F51A2E4E9}
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
Policies = $0
Expand Down
6 changes: 6 additions & 0 deletions Tests/Test.Gpio.Htu21df/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
38 changes: 38 additions & 0 deletions Tests/Test.Gpio.Htu21df/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Threading;
using Raspberry.IO.Components.Sensors.Humidity.Htu21df;
using Raspberry.IO.GeneralPurpose;
using Raspberry.IO.InterIntegratedCircuit;


namespace Test.Gpio.Htu21df
{
class Program
{
static void Main()
{
const ConnectorPin sdaPin = ConnectorPin.P1Pin03;
const ConnectorPin sclPin = ConnectorPin.P1Pin05;

Console.WriteLine( "HTU21DF Sample: Read humidity and temperature" );
Console.WriteLine();
Console.WriteLine( "\tSDA: {0}", sdaPin );
Console.WriteLine( "\tSCL: {0}", sclPin );
Console.WriteLine();

using ( var driver = new I2cDriver( sdaPin.ToProcessor(), sclPin.ToProcessor() ) )
{
var deviceConnection = new Htu21dfConnection( driver.Connect( 0x40 ) );
deviceConnection.Begin();

while ( !Console.KeyAvailable )
{
var temp = deviceConnection.ReadTemperature();
var humidity = deviceConnection.ReadHumidity();
Console.WriteLine( $"Temp is: {temp:F1}C. RH is {humidity:F1}%" );
Thread.Sleep( 2000 );
}
}
}
}
}
36 changes: 36 additions & 0 deletions Tests/Test.Gpio.Htu21df/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Test.Gpio.Htu21df")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Test.Gpio.Htu21df")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("04e66e09-8128-4122-bbf3-17220bc75ada")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading