-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathGpioConnectionDriver.cs
More file actions
468 lines (391 loc) · 17.4 KB
/
GpioConnectionDriver.cs
File metadata and controls
468 lines (391 loc) · 17.4 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#region References
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using Raspberry.IO.Interop;
using Raspberry.Timers;
#endregion
namespace Raspberry.IO.GeneralPurpose
{
/// <summary>
/// Represents the default connection driver that uses memory for accesses and files for edge detection.
/// </summary>
public class GpioConnectionDriver : IGpioConnectionDriver
{
#region Fields
private const string gpioPath = "/sys/class/gpio";
private readonly IntPtr gpioAddress;
private readonly Dictionary<ProcessorPin, PinResistor> pinResistors = new Dictionary<ProcessorPin, PinResistor>();
private readonly Dictionary<ProcessorPin, PinPoll> pinPolls = new Dictionary<ProcessorPin, PinPoll>();
/// <summary>
/// The default timeout (5 seconds).
/// </summary>
public static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(5);
/// <summary>
/// The minimum timeout (1 milliseconds)
/// </summary>
public static readonly TimeSpan MinimumTimeout = TimeSpan.FromMilliseconds(1);
private static readonly TimeSpan resistorSetDelay = TimeSpanUtility.FromMicroseconds(5);
#endregion
#region Instance Management
/// <summary>
/// Initializes a new instance of the <see cref="GpioConnectionDriver"/> class.
/// </summary>
public GpioConnectionDriver() {
using (var memoryFile = UnixFile.Open("/dev/mem", UnixFileMode.ReadWrite | UnixFileMode.Synchronized)) {
gpioAddress = MemoryMap.Create(
IntPtr.Zero,
Interop.BCM2835_BLOCK_SIZE,
MemoryProtection.ReadWrite,
MemoryFlags.Shared,
memoryFile.Descriptor,
GetProcessorBaseAddress(Board.Current.Processor));
}
}
/// <summary>
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="GpioConnectionDriver"/> is reclaimed by garbage collection.
/// </summary>
~GpioConnectionDriver()
{
MemoryMap.Close(gpioAddress, Interop.BCM2835_BLOCK_SIZE);
}
#endregion
#region Methods
/// <summary>
/// Gets driver capabilities.
/// </summary>
/// <returns>The capabilites.</returns>
GpioConnectionDriverCapabilities IGpioConnectionDriver.GetCapabilities()
{
return GetCapabilities();
}
/// <summary>
/// Gets driver capabilities.
/// </summary>
/// <returns>The capabilites.</returns>
public static GpioConnectionDriverCapabilities GetCapabilities()
{
return GpioConnectionDriverCapabilities.CanSetPinResistor | GpioConnectionDriverCapabilities.CanSetPinDetectedEdges;
}
/// <summary>
/// Allocates the specified pin.
/// </summary>
/// <param name="pin">The pin.</param>
/// <param name="direction">The direction.</param>
public void Allocate(ProcessorPin pin, PinDirection direction)
{
var gpioId = string.Format("gpio{0}", (int)pin);
if (Directory.Exists(Path.Combine(gpioPath, gpioId)))
{
// Reinitialize pin virtual file
using (var streamWriter = new StreamWriter(Path.Combine(gpioPath, "unexport"), false))
streamWriter.Write((int) pin);
}
// Export pin for file mode
using (var streamWriter = new StreamWriter(Path.Combine(gpioPath, "export"), false))
streamWriter.Write((int)pin);
// Set the direction on the pin and update the exported list
SetPinMode(pin, direction == PinDirection.Input ? Interop.BCM2835_GPIO_FSEL_INPT : Interop.BCM2835_GPIO_FSEL_OUTP);
// Set direction in pin virtual file
var filePath = Path.Combine(gpioId, "direction");
using (var streamWriter = new StreamWriter(Path.Combine(gpioPath, filePath), false))
streamWriter.Write(direction == PinDirection.Input ? "in" : "out");
if (direction == PinDirection.Input)
{
PinResistor pinResistor;
if (!pinResistors.TryGetValue(pin, out pinResistor) || pinResistor != PinResistor.None)
SetPinResistor(pin, PinResistor.None);
SetPinDetectedEdges(pin, PinDetectedEdges.Both);
InitializePoll(pin);
}
}
/// <summary>
/// Sets the pin resistor.
/// </summary>
/// <param name="pin">The pin.</param>
/// <param name="resistor">The resistor.</param>
public void SetPinResistor(ProcessorPin pin, PinResistor resistor)
{
// Set the pullup/down resistor for a pin
//
// The GPIO Pull-up/down Clock Registers control the actuation of internal pull-downs on
// the respective GPIO pins. These registers must be used in conjunction with the GPPUD
// register to effect GPIO Pull-up/down changes. The following sequence of events is
// required:
// 1. Write to GPPUD to set the required control signal (i.e. Pull-up or Pull-Down or neither
// to remove the current Pull-up/down)
// 2. Wait 150 cycles ? this provides the required set-up time for the control signal
// 3. Write to GPPUDCLK0/1 to clock the control signal into the GPIO pads you wish to
// modify ? NOTE only the pads which receive a clock will be modified, all others will
// retain their previous state.
// 4. Wait 150 cycles ? this provides the required hold time for the control signal
// 5. Write to GPPUD to remove the control signal
// 6. Write to GPPUDCLK0/1 to remove the clock
//
// RPi has P1-03 and P1-05 with 1k8 pullup resistor
uint pud;
switch (resistor)
{
case PinResistor.None:
pud = Interop.BCM2835_GPIO_PUD_OFF;
break;
case PinResistor.PullDown:
pud = Interop.BCM2835_GPIO_PUD_DOWN;
break;
case PinResistor.PullUp:
pud = Interop.BCM2835_GPIO_PUD_UP;
break;
default:
throw new ArgumentOutOfRangeException("resistor", resistor, string.Format(CultureInfo.InvariantCulture, "{0} is not a valid value for pin resistor", resistor));
}
WriteResistor(pud);
HighResolutionTimer.Sleep(resistorSetDelay);
SetPinResistorClock(pin, true);
HighResolutionTimer.Sleep(resistorSetDelay);
WriteResistor(Interop.BCM2835_GPIO_PUD_OFF);
SetPinResistorClock(pin, false);
pinResistors[pin] = PinResistor.None;
}
/// <summary>
/// Sets the detected edges on an input pin.
/// </summary>
/// <param name="pin">The pin.</param>
/// <param name="edges">The edges.</param>
/// <remarks>By default, both edges may be detected on input pins.</remarks>
public void SetPinDetectedEdges(ProcessorPin pin, PinDetectedEdges edges)
{
var edgePath = Path.Combine(gpioPath, string.Format("gpio{0}/edge", (int)pin));
using (var streamWriter = new StreamWriter(edgePath, false))
streamWriter.Write(ToString(edges));
}
/// <summary>
/// Waits for the specified pin to be in the specified state.
/// </summary>
/// <param name="pin">The pin.</param>
/// <param name="waitForUp">if set to <c>true</c> waits for the pin to be up. Default value is <c>true</c>.</param>
/// <param name="timeout">The timeout. Default value is <see cref="TimeSpan.Zero" />.</param>
/// <remarks>
/// If <c>timeout</c> is set to <see cref="TimeSpan.Zero" />, a 5 seconds timeout is used.
/// </remarks>
public void Wait(ProcessorPin pin, bool waitForUp = true, TimeSpan timeout = new TimeSpan())
{
var pinPoll = pinPolls[pin];
if (Read(pin) == waitForUp)
return;
var actualTimeout = GetActualTimeout(timeout);
while (true)
{
// TODO: timeout after the remaining amount of time.
var waitResult = Interop.epoll_wait(pinPoll.PollDescriptor, pinPoll.OutEventPtr, 1, (int)actualTimeout.TotalMilliseconds);
if (waitResult > 0)
{
if (Read(pin) == waitForUp)
break;
}
else if (waitResult == 0)
throw new TimeoutException(string.Format(CultureInfo.InvariantCulture, "Operation timed out after waiting {0}ms for the pin {1} to be {2}", actualTimeout, pin, (waitForUp ? "up" : "down")));
else
throw new IOException("Call to epoll_wait API failed");
}
}
/// <summary>
/// Releases the specified pin.
/// </summary>
/// <param name="pin">The pin.</param>
public void Release(ProcessorPin pin)
{
UninitializePoll(pin);
using (var streamWriter = new StreamWriter(Path.Combine(gpioPath, "unexport"), false))
streamWriter.Write((int)pin);
SetPinMode(pin, Interop.BCM2835_GPIO_FSEL_INPT);
}
/// <summary>
/// Modified the status of a pin.
/// </summary>
/// <param name="pin">The pin.</param>
/// <param name="value">The pin status.</param>
public void Write(ProcessorPin pin, bool value)
{
int shift;
var offset = Math.DivRem((int)pin, 32, out shift);
var pinGroupAddress = gpioAddress + (int)((value ? Interop.BCM2835_GPSET0 : Interop.BCM2835_GPCLR0) + offset);
SafeWriteUInt32(pinGroupAddress, (uint)1 << shift);
}
/// <summary>
/// Reads the status of the specified pin.
/// </summary>
/// <param name="pin">The pin.</param>
/// <returns>
/// The pin status.
/// </returns>
public bool Read(ProcessorPin pin)
{
int shift;
var offset = Math.DivRem((int)pin, 32, out shift);
var pinGroupAddress = gpioAddress + (int)(Interop.BCM2835_GPLEV0 + offset);
var value = SafeReadUInt32(pinGroupAddress);
return (value & (1 << shift)) != 0;
}
/// <summary>
/// Reads the status of the specified pins.
/// </summary>
/// <param name="pins">The pins.</param>
/// <returns>
/// The pins status.
/// </returns>
public ProcessorPins Read(ProcessorPins pins)
{
var pinGroupAddress = gpioAddress + (int)(Interop.BCM2835_GPLEV0 + (uint)0 * 4);
var value = SafeReadUInt32(pinGroupAddress);
return (ProcessorPins)((uint)pins & value);
}
#endregion
#region Private Methods
private static uint GetProcessorBaseAddress(Processor processor)
{
switch (processor)
{
case Processor.Bcm2835:
case Processor.Bcm2708:
return Interop.BCM2835_GPIO_BASE;
case Processor.Bcm2709:
return Interop.BCM2836_GPIO_BASE;
default:
throw new ArgumentOutOfRangeException("processor");
}
}
private static TimeSpan GetActualTimeout(TimeSpan timeout)
{
if (timeout > TimeSpan.Zero)
return timeout;
if (timeout < TimeSpan.Zero)
return MinimumTimeout;
return DefaultTimeout;
}
private void InitializePoll(ProcessorPin pin)
{
lock (pinPolls)
{
PinPoll poll;
if (pinPolls.TryGetValue(pin, out poll))
return;
var pinPoll = new PinPoll();
pinPoll.PollDescriptor = Interop.epoll_create(1);
if (pinPoll.PollDescriptor < 0)
throw new IOException("Call to epoll_create(1) API failed with the following return value: " + pinPoll.PollDescriptor);
var valuePath = Path.Combine(gpioPath, string.Format("gpio{0}/value", (int)pin));
pinPoll.FileDescriptor = UnixFile.OpenFileDescriptor(valuePath, UnixFileMode.ReadOnly | UnixFileMode.NonBlocking);
var ev = new Interop.epoll_event
{
events = (Interop.EPOLLIN | Interop.EPOLLET | Interop.EPOLLPRI),
data = new Interop.epoll_data { fd = pinPoll.FileDescriptor }
};
pinPoll.InEventPtr = Marshal.AllocHGlobal(64);
Marshal.StructureToPtr(ev, pinPoll.InEventPtr, false);
var controlResult = Interop.epoll_ctl(pinPoll.PollDescriptor, Interop.EPOLL_CTL_ADD, pinPoll.FileDescriptor, pinPoll.InEventPtr);
if (controlResult != 0)
throw new IOException("Call to epoll_ctl(EPOLL_CTL_ADD) API failed with the following return value: " + controlResult);
pinPoll.OutEventPtr = Marshal.AllocHGlobal(64);
pinPolls[pin] = pinPoll;
}
}
private void UninitializePoll(ProcessorPin pin)
{
PinPoll poll;
if (pinPolls.TryGetValue(pin, out poll))
{
pinPolls.Remove(pin);
var controlResult = poll.InEventPtr != IntPtr.Zero ? Interop.epoll_ctl(poll.PollDescriptor, Interop.EPOLL_CTL_DEL, poll.FileDescriptor, poll.InEventPtr) : 0;
Marshal.FreeHGlobal(poll.InEventPtr);
Marshal.FreeHGlobal(poll.OutEventPtr);
UnixFile.CloseFileDescriptor(poll.PollDescriptor);
UnixFile.CloseFileDescriptor(poll.FileDescriptor);
if (controlResult != 0)
throw new IOException("Call to epoll_ctl(EPOLL_CTL_DEL) API failed with the following return value: " + controlResult);
}
}
private static string ToString(PinDetectedEdges edges)
{
switch (edges)
{
case PinDetectedEdges.Both:
return "both";
case PinDetectedEdges.Rising:
return "rising";
case PinDetectedEdges.Falling:
return "falling";
case PinDetectedEdges.None:
return "none";
default:
throw new ArgumentOutOfRangeException("edges", edges, string.Format(CultureInfo.InvariantCulture, "{0} is not a valid value for edge detection", edges));
}
}
private void SetPinResistorClock(ProcessorPin pin, bool on)
{
int shift;
var offset = Math.DivRem((int)pin, 32, out shift);
var clockAddress = gpioAddress + (int)(Interop.BCM2835_GPPUDCLK0 + offset);
SafeWriteUInt32(clockAddress, (uint)(on ? 1 : 0) << shift);
}
private void WriteResistor(uint resistor)
{
var resistorPin = gpioAddress + (int)Interop.BCM2835_GPPUD;
SafeWriteUInt32(resistorPin, resistor);
}
private void SetPinMode(ProcessorPin pin, uint mode)
{
// Function selects are 10 pins per 32 bit word, 3 bits per pin
var pinModeAddress = gpioAddress + (int)(Interop.BCM2835_GPFSEL0 + 4 * ((int)pin / 10));
var shift = 3 * ((int)pin % 10);
var mask = Interop.BCM2835_GPIO_FSEL_MASK << shift;
var value = mode << shift;
WriteUInt32Mask(pinModeAddress, value, mask);
}
private static void WriteUInt32Mask(IntPtr address, uint value, uint mask)
{
var v = SafeReadUInt32(address);
v = (v & ~mask) | (value & mask);
SafeWriteUInt32(address, v);
}
private static uint SafeReadUInt32(IntPtr address)
{
// Make sure we dont return the _last_ read which might get lost
// if subsequent code changes to a different peripheral
var ret = ReadUInt32(address);
ReadUInt32(address);
return ret;
}
private static uint ReadUInt32(IntPtr address)
{
unchecked
{
return (uint)Marshal.ReadInt32(address);
}
}
private static void SafeWriteUInt32(IntPtr address, uint value)
{
// Make sure we don't rely on the first write, which may get
// lost if the previous access was to a different peripheral.
WriteUInt32(address, value);
WriteUInt32(address, value);
}
private static void WriteUInt32(IntPtr address, uint value)
{
unchecked
{
Marshal.WriteInt32(address, (int)value);
}
}
private struct PinPoll
{
public int FileDescriptor;
public int PollDescriptor;
public IntPtr InEventPtr;
public IntPtr OutEventPtr;
}
#endregion
}
}