Skip to content

Commit 5951c2c

Browse files
authored
Adding Mcp3xxx (#48)
1 parent 0121dc0 commit 5951c2c

31 files changed

+1095
-0
lines changed

devices/Mcp3xxx/Mcp3001.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Device.Spi;
5+
6+
namespace Iot.Device.Adc
7+
{
8+
/// <summary>
9+
/// MCP3001 Analog to Digital Converter (ADC)
10+
/// </summary>
11+
public class Mcp3001 : Mcp3Base
12+
{
13+
/// <summary>
14+
/// Constructs Mcp3008 instance
15+
/// </summary>
16+
/// <param name="spiDevice">Device used for SPI communication</param>
17+
public Mcp3001(SpiDevice spiDevice)
18+
: base(spiDevice)
19+
{
20+
}
21+
22+
/// <summary>
23+
/// Reads a 10-bit (0..1023) value from the device
24+
/// </summary>
25+
/// <returns>10-bit value corresponding to relative voltage level on specified device channel</returns>
26+
// Read the data from the device. As the 10 bits of data start at bit 3 then read 13 bits and shift right by 3.
27+
public int Read() => ReadInternal(adcRequest: 0, adcResolutionBits: 10 + 3, delayBits: 0) >> 3;
28+
}
29+
}

devices/Mcp3xxx/Mcp3002.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Device.Spi;
5+
6+
namespace Iot.Device.Adc
7+
{
8+
/// <summary>
9+
/// MCP3002 Analog to Digital Converter (ADC)
10+
/// </summary>
11+
public class Mcp3002 : Mcp3xxx
12+
{
13+
/// <summary>
14+
/// Constructs Mcp3002 instance
15+
/// </summary>
16+
/// <param name="spiDevice">Device used for SPI communication</param>
17+
public Mcp3002(SpiDevice spiDevice)
18+
: base(spiDevice, channelCount: 2, adcResolutionBits: 10)
19+
{
20+
}
21+
}
22+
}

devices/Mcp3xxx/Mcp3004.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Device.Spi;
5+
6+
namespace Iot.Device.Adc
7+
{
8+
/// <summary>
9+
/// MCP3004 Analog to Digital Converter (ADC)
10+
/// </summary>
11+
public class Mcp3004 : Mcp3xxx
12+
{
13+
/// <summary>
14+
/// Constructs Mcp3004 instance
15+
/// </summary>
16+
/// <param name="spiDevice">Device used for SPI communication</param>
17+
public Mcp3004(SpiDevice spiDevice)
18+
: base(spiDevice, channelCount: 4, adcResolutionBits: 10)
19+
{
20+
}
21+
}
22+
}

devices/Mcp3xxx/Mcp3008.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Device.Spi;
5+
6+
namespace Iot.Device.Adc
7+
{
8+
/// <summary>
9+
/// MCP3008 Analog to Digital Converter (ADC)
10+
/// </summary>
11+
public class Mcp3008 : Mcp3xxx
12+
{
13+
/// <summary>
14+
/// Constructs Mcp3008 instance
15+
/// </summary>
16+
/// <param name="spiDevice">Device used for SPI communication</param>
17+
public Mcp3008(SpiDevice spiDevice)
18+
: base(spiDevice, channelCount: 8, adcResolutionBits: 10)
19+
{
20+
}
21+
}
22+
}

devices/Mcp3xxx/Mcp3201.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Device.Spi;
5+
6+
namespace Iot.Device.Adc
7+
{
8+
/// <summary>
9+
/// MCP32001 Analog to Digital Converter (ADC)
10+
/// </summary>
11+
public class Mcp3201 : Mcp3Base
12+
{
13+
/// <summary>
14+
/// Constructs Mcp3201 instance
15+
/// </summary>
16+
/// <param name="spiDevice">Device used for SPI communication</param>
17+
public Mcp3201(SpiDevice spiDevice)
18+
: base(spiDevice)
19+
{
20+
}
21+
22+
/// <summary>
23+
/// Reads a 12-bit (0..4096) value from the device
24+
/// </summary>
25+
/// <returns>12-bit value corresponding to relative voltage level on specified device channel</returns>
26+
// Read the data from the device. As the 12 bits of data start at bit 1 then read 13 bits and shift right by 1.
27+
public int Read() => ReadInternal(adcRequest: 0, adcResolutionBits: 12 + 1, delayBits: 0) >> 1;
28+
}
29+
}

devices/Mcp3xxx/Mcp3202.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Device.Spi;
5+
6+
namespace Iot.Device.Adc
7+
{
8+
/// <summary>
9+
/// MCP3202 Analog to Digital Converter (ADC)
10+
/// </summary>
11+
public class Mcp3202 : Mcp3xxx
12+
{
13+
/// <summary>
14+
/// Constructs Mcp3202 instance
15+
/// </summary>
16+
/// <param name="spiDevice">Device used for SPI communication</param>
17+
public Mcp3202(SpiDevice spiDevice)
18+
: base(spiDevice, channelCount: 2, adcResolutionBits: 12)
19+
{
20+
}
21+
}
22+
}

devices/Mcp3xxx/Mcp3204.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Device.Spi;
5+
6+
namespace Iot.Device.Adc
7+
{
8+
/// <summary>
9+
/// MCP3204 Analog to Digital Converter (ADC)
10+
/// </summary>
11+
public class Mcp3204 : Mcp3xxx
12+
{
13+
/// <summary>
14+
/// Constructs Mcp3204 instance
15+
/// </summary>
16+
/// <param name="spiDevice">Device used for SPI communication</param>
17+
public Mcp3204(SpiDevice spiDevice)
18+
: base(spiDevice, channelCount: 4, adcResolutionBits: 12)
19+
{
20+
}
21+
}
22+
}

devices/Mcp3xxx/Mcp3208.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Device.Spi;
6+
7+
namespace Iot.Device.Adc
8+
{
9+
/// <summary>
10+
/// MCP3208 Analog to Digital Converter (ADC)
11+
/// </summary>
12+
public class Mcp3208 : Mcp3xxx
13+
{
14+
/// <summary>
15+
/// Constructs Mcp3208 instance
16+
/// </summary>
17+
/// <param name="spiDevice">Device used for SPI communication</param>
18+
public Mcp3208(SpiDevice spiDevice)
19+
: base(spiDevice, channelCount: 8, adcResolutionBits: 12)
20+
{
21+
}
22+
}
23+
}

devices/Mcp3xxx/Mcp3301.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Device.Spi;
5+
6+
namespace Iot.Device.Adc
7+
{
8+
/// <summary>
9+
/// MCP3301 Analog to Digital Converter (ADC)
10+
/// </summary>
11+
public class Mcp3301 : Mcp3Base
12+
{
13+
/// <summary>
14+
/// Constructs Mcp3301 instance
15+
/// </summary>
16+
/// <param name="spiDevice">Device used for SPI communication</param>
17+
public Mcp3301(SpiDevice spiDevice)
18+
: base(spiDevice)
19+
{
20+
}
21+
22+
/// <summary>
23+
/// Reads a 13 bit signed value from the device using differential inputs
24+
/// </summary>
25+
/// <returns>A 13 bit signed value corresponding to relative voltage level on specified device channels</returns>
26+
public int ReadDifferential()
27+
{
28+
int signedResult = ReadInternal(adcRequest: 0, adcResolutionBits: 13, delayBits: 0);
29+
30+
// convert 13 bit signed to 32 bit signed
31+
return Mcp33xx.SignExtend(signedValue: signedResult, signingBit: 12);
32+
}
33+
}
34+
}

devices/Mcp3xxx/Mcp3302.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Device.Spi;
6+
7+
namespace Iot.Device.Adc
8+
{
9+
/// <summary>
10+
/// MCP3302 Analog to Digital Converter (ADC)
11+
/// </summary>
12+
public class Mcp3302 : Mcp33xx
13+
{
14+
/// <summary>
15+
/// Constructs Mcp3302 instance
16+
/// </summary>
17+
/// <param name="spiDevice">Device used for SPI communication</param>
18+
public Mcp3302(SpiDevice spiDevice)
19+
: base(spiDevice, channelCount: 4, adcResolutionBits: 13)
20+
{
21+
}
22+
}
23+
}

devices/Mcp3xxx/Mcp3304.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Device.Spi;
6+
7+
namespace Iot.Device.Adc
8+
{
9+
/// <summary>
10+
/// MCP3304 Analog to Digital Converter (ADC)
11+
/// </summary>
12+
public class Mcp3304 : Mcp33xx
13+
{
14+
/// <summary>
15+
/// Constructs Mcp3304 instance
16+
/// </summary>
17+
/// <param name="spiDevice">Device used for SPI communication</param>
18+
public Mcp3304(SpiDevice spiDevice)
19+
: base(spiDevice, channelCount: 8, adcResolutionBits: 13)
20+
{
21+
}
22+
}
23+
}

devices/Mcp3xxx/Mcp33xx.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Device.Spi;
6+
7+
namespace Iot.Device.Adc
8+
{
9+
/// <summary>
10+
/// MCP33XX family of Analog to Digital Converters
11+
/// </summary>
12+
public abstract class Mcp33xx : Mcp3xxx
13+
{
14+
/// <summary>
15+
/// Constructs Mcp33xx instance
16+
/// </summary>
17+
/// <param name="spiDevice">Device used for SPI communication</param>
18+
/// <param name="channelCount">Value representing the number of single ended input channels on the device.</param>
19+
/// <param name="adcResolutionBits">The number of bits of resolution for the ADC.</param>
20+
public Mcp33xx(SpiDevice spiDevice, byte channelCount, byte adcResolutionBits)
21+
: base(spiDevice, channelCount, adcResolutionBits)
22+
{
23+
}
24+
25+
/// <summary>
26+
/// Convert a signed value with a sign bit at a particular location to an int.
27+
/// </summary>
28+
/// <param name="signedValue">Signed value with a sign bit at a particular location</param>
29+
/// <param name="signingBit">Bit number that contains the sign bit</param>
30+
/// <returns>A value corresponding to the signed value sign extended into an int</returns>
31+
// if the sign bit is set then extend the signing bit to create a signed integer
32+
public static int SignExtend(int signedValue, int signingBit) => (signedValue >> signingBit) == 0 ? signedValue : signedValue - (2 << signingBit);
33+
34+
/// <summary>
35+
/// Reads a value from the device using pseudo-differential inputs
36+
/// </summary>
37+
/// <param name="valueChannel">Channel which represents the signal (valid values: 0 to channelcount - 1).</param>
38+
/// <param name="referenceChannel">Channel which represents the signal ground (valid values: 0 to channelcount - 1).</param>
39+
/// <returns>A value corresponding to relative voltage level on specified device channels</returns>
40+
public override int ReadPseudoDifferential(int valueChannel, int referenceChannel) => throw new NotSupportedException($"Mcp33xx device does not support {nameof(ReadPseudoDifferential)}.");
41+
42+
/// <summary>
43+
/// Reads a 13 bit signed value from the device using differential inputs
44+
/// </summary>
45+
/// <remarks>
46+
/// The value that is read respresents the difference between the voltage on the value channel and the voltage on the reference channel (valueChannel Reading - referenceChannel Reading).
47+
/// If the valueChannel and the referenceChannel are part of the same channel pairing then the ADC converter will internally subtract the two values. If not then the subtraction is
48+
/// performed in software which may mean that errors are introduced with rapidly changing signals.
49+
/// </remarks>
50+
/// <param name="valueChannel">Channel which represents the signal driving the value in a positive direction (valid values: 0 to channelcount - 1).</param>
51+
/// <param name="referenceChannel">Channel which represents the signal driving the value in a negative direction (valid values: 0 to channelcount - 1).</param>
52+
/// <returns>A 13 bit signed value corresponding to relative voltage level on specified device channels</returns>
53+
public override int ReadDifferential(int valueChannel, int referenceChannel)
54+
{
55+
int retval;
56+
57+
CheckChannelRange(valueChannel, ChannelCount);
58+
CheckChannelRange(referenceChannel, ChannelCount);
59+
60+
if (valueChannel == referenceChannel)
61+
{
62+
throw new ArgumentException(nameof(valueChannel), $"ADC differential channels must be different. {nameof(valueChannel)}: {valueChannel}; {nameof(referenceChannel)}: {referenceChannel}.");
63+
}
64+
65+
// check if it is possible to use hardware differential because both input channels are in the same differential channel pairing
66+
if (valueChannel / 2 == referenceChannel / 2)
67+
{
68+
// read a value from the ADC where the channel is the channel pairing
69+
retval = ReadInternal(channel: valueChannel / 2, valueChannel > referenceChannel ? InputType.InvertedDifferential : InputType.Differential, adcResolutionBits: 13);
70+
71+
// convert 13 bit signed to 32 bit signed
72+
retval = SignExtend(signedValue: retval, signingBit: 12);
73+
}
74+
else // otherwise just subtract two readings
75+
{
76+
retval = ReadInternal(valueChannel, InputType.SingleEnded, adcResolutionBits: 12) -
77+
ReadInternal(referenceChannel, InputType.SingleEnded, adcResolutionBits: 12);
78+
}
79+
80+
return retval;
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)