forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pcx857x.cs
271 lines (230 loc) · 8.93 KB
/
Pcx857x.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Device.Gpio;
using System.Device.I2c;
using System.Runtime.CompilerServices;
namespace Iot.Device.Pcx857x
{
/// <summary>
/// Base class for PCx857x GPIO expanders
/// </summary>
public abstract class Pcx857x : GpioDriver
{
/// <summary>
/// I2C device used for communication with the device
/// </summary>
protected I2cDevice Device { get; }
private readonly GpioController _controller;
private readonly int _interrupt;
private bool _shouldDispose;
// Pin mode bits- 0 for input, 1 for output to match PinMode
private ushort _pinModes;
// Pin value bits, 1 for high
private ushort _pinValues;
/// <summary>
/// Remote I/O expander for I2C-bus with interrupt.
/// </summary>
/// <param name="device">The I2C device.</param>
/// <param name="interrupt">The interrupt pin number, if present.</param>
/// <param name="gpioController">
/// The GPIO controller for the <paramref name="interrupt"/>.
/// If not specified, the default controller will be used.
/// </param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
public Pcx857x(I2cDevice device, int interrupt = -1, GpioController gpioController = null, bool shouldDispose = true)
{
Device = device ?? throw new ArgumentNullException(nameof(device));
_interrupt = interrupt;
_shouldDispose = shouldDispose || gpioController is null;
if (_interrupt != -1)
{
_controller = gpioController ?? new GpioController();
_controller.OpenPin(_interrupt, PinMode.Input);
}
// These controllers do not have commands, setting the pins to high designates
// them as able to receive input. As we don't want to set high on pins intended
// for output we'll set all of the pins to low for our initial state.
if (PinCount == 8)
{
WriteByte(0x00);
}
else
{
InternalWriteUInt16(0x0000);
}
_pinModes = 0xFFFF;
}
/// <summary>
/// Reads byte from the device
/// </summary>
/// <returns>Byte read from the device</returns>
public byte ReadByte() => Device.ReadByte();
/// <summary>
/// Writes byte to the device
/// </summary>
/// <param name="value">Byte to be written to the device</param>
public void WriteByte(byte value) => Device.WriteByte(value);
/// <summary>
/// Reads 16-bit unsigned integer from the device
/// </summary>
/// <returns>16-bit unsigned integer read from the device</returns>
protected ushort InternalReadUInt16()
{
SpanByte buffer = new byte[2];
Device.Read(buffer);
return (ushort)(buffer[0] | buffer[1] << 8);
}
/// <summary>
/// Writes 16-bit unsigned integer to the device
/// </summary>
/// <param name="value">16-vit unsigned integer to be written to the device</param>
protected void InternalWriteUInt16(ushort value)
{
SpanByte buffer = new byte[2];
buffer[0] = (byte)value;
buffer[1] = (byte)(value >> 8);
Device.Write(buffer);
}
/// <inheritdoc/>
protected override void ClosePin(int pinNumber)
{
// No-op
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (_shouldDispose)
{
_controller?.Dispose();
}
Device.Dispose();
base.Dispose(disposing);
}
/// <inheritdoc/>
protected override void OpenPin(int pinNumber)
{
// No-op
}
/// <inheritdoc/>
protected override PinValue Read(int pinNumber)
{
var values = new PinValuePair[]
{
new PinValuePair(pinNumber, default)
};
Read(values);
return values[0].PinValue;
}
/// <summary>
/// Reads multiple pins from the device
/// </summary>
/// <param name="pinValues">Pins and values to be read</param>
public void Read(PinValuePair[] pinValues)
{
(uint pins, _) = new PinVector32(pinValues);
if (pins >> PinCount > 0)
{
ThrowInvalidPin(nameof(pinValues));
}
if ((pins & _pinModes) != 0)
{
// One of the specified pins was set to output (1)
throw new InvalidOperationException("Cannot read from output pins.");
}
ushort data = PinCount == 8 ? ReadByte() : InternalReadUInt16();
for (int i = 0; i < pinValues.Length; i++)
{
int pin = pinValues[i].PinNumber;
pinValues[i] = new PinValuePair(pin, (data >> pin) & 1);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
// This is a helper to allow the JIT to inline calling methods.
// (Methods with throws cannot be inlined.)
private void ThrowInvalidPin(string argumentName) => throw new ArgumentOutOfRangeException(argumentName, $"Pin numbers must be in the range of 0 to {PinCount - 1}.");
private void ValidatePinNumber(int pinNumber)
{
if (pinNumber < 0 || pinNumber >= PinCount)
{
ThrowInvalidPin(nameof(pinNumber));
}
}
/// <inheritdoc/>
protected override void SetPinMode(int pinNumber, PinMode mode)
{
ValidatePinNumber(pinNumber);
ushort SetBit(ushort data, int bitNumber) => (ushort)(data | (1 << bitNumber));
ushort ClearBit(ushort data, int bitNumber) => (ushort)(data & ~(1 << bitNumber));
if (mode == PinMode.Input)
{
_pinModes = ClearBit(_pinModes, pinNumber);
}
else if (mode == PinMode.Output)
{
_pinModes = SetBit(_pinModes, pinNumber);
}
else
{
throw new ArgumentOutOfRangeException(nameof(mode), "Only Input and Output modes are supported.");
}
WritePins(_pinValues);
}
private void WritePins(ushort value)
{
// We need to set all input pins to high
_pinValues = (ushort)(value | ~_pinModes);
if (PinCount == 8)
{
WriteByte((byte)_pinValues);
}
else
{
InternalWriteUInt16(_pinValues);
}
}
/// <inheritdoc/>
protected override void Write(int pinNumber, PinValue value)
{
PinValuePair[] values = new PinValuePair[]
{
new PinValuePair(pinNumber, value)
};
Write(values);
}
/// <summary>
/// Writes a value to a set of pins.
/// </summary>
public void Write(PinValuePair[] pinValues)
{
(uint pins, uint values) = new PinVector32(pinValues);
if (pins >> PinCount > 0)
{
ThrowInvalidPin(nameof(pinValues));
}
if ((pins & ~_pinModes) != 0)
{
// One of the specified pins was set to input (0)
throw new InvalidOperationException("Cannot write to input pins.");
}
ushort SetBits(ushort current, ushort bits, ushort mask)
{
current &= (ushort)~mask;
current |= bits;
return current;
}
WritePins(SetBits(_pinValues, (ushort)values, (ushort)pins));
}
/// <inheritdoc/>
protected override PinMode GetPinMode(int pinNumber) => ((_pinModes & (1 << pinNumber)) == 0) ? PinMode.Input : PinMode.Output;
/// <inheritdoc/>
protected override bool IsPinModeSupported(int pinNumber, PinMode mode) => (mode == PinMode.Output || mode == PinMode.Input);
/// <inheritdoc/>
protected override int ConvertPinNumberToLogicalNumberingScheme(int pinNumber) => pinNumber;
// For now eventing APIs are not supported.
/// <inheritdoc/>
protected override void AddCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventTypes, PinChangeEventHandler callback) => throw new NotImplementedException();
/// <inheritdoc/>
protected override void RemoveCallbackForPinValueChangedEvent(int pinNumber, PinChangeEventHandler callback) => throw new NotImplementedException();
}
}