-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathSpiDeviceManager.cs
166 lines (141 loc) · 4.85 KB
/
SpiDeviceManager.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
// 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.Collections.Generic;
using System.Device;
using System.Device.Spi;
using System.Diagnostics;
using System.Linq;
namespace Iot.Device.Board
{
internal class SpiDeviceManager : SpiDevice, IDeviceManager
{
private readonly Board _board;
private readonly int[] _pins;
private SpiDevice _device;
public SpiDeviceManager(Board board, SpiConnectionSettings connectionSettings, int[]? pins, Func<SpiConnectionSettings, int[], SpiDevice> createOperation)
{
if (pins == null || pins.Length < 3 || pins.Length > 4)
{
throw new ArgumentException("Must provide a valid set of 3 or 4 pins", nameof(pins));
}
_board = board;
ConnectionSettings = connectionSettings;
try
{
_board.ReservePin(pins[0], PinUsage.Spi, this);
_board.ReservePin(pins[1], PinUsage.Spi, this);
_board.ReservePin(pins[2], PinUsage.Spi, this);
if (pins.Length == 4)
{
// The fourth pin, if provided, is the CS line. This may be handled manually, but if done in hardware, SPI
// numbering must be used here I think. So the value would be 0 or 1 for CE0 and CE1 in ALT 0 mode, which are
// the logical pins 7 and 8.
_board.ReservePin(pins[3], PinUsage.Spi, this);
}
_device = createOperation(connectionSettings, pins);
}
catch (Exception x)
{
// TODO: Replace with logging
Debug.WriteLine($"Exception: {x}");
_board.ReleasePin(pins[0], PinUsage.Spi, this);
_board.ReleasePin(pins[1], PinUsage.Spi, this);
_board.ReleasePin(pins[2], PinUsage.Spi, this);
if (pins.Length == 4)
{
_board.ReleasePin(pins[3], PinUsage.Spi, this);
}
throw;
}
_pins = pins;
}
public override SpiConnectionSettings ConnectionSettings
{
get;
}
internal SpiDevice RawDevice
{
get
{
return _device;
}
}
public bool IsDisposed
{
get
{
return _device == null;
}
}
public IReadOnlyCollection<int> GetActiveManagedPins()
{
return _pins.ToList();
}
public override byte ReadByte()
{
if (_device == null)
{
throw new ObjectDisposedException("SPI Device");
}
return _device.ReadByte();
}
public override void Read(Span<byte> buffer)
{
if (_device == null)
{
throw new ObjectDisposedException("SPI Device");
}
_device.Read(buffer);
}
public override void WriteByte(byte value)
{
if (_device == null)
{
throw new ObjectDisposedException("SPI Device");
}
_device.WriteByte(value);
}
public override void Write(ReadOnlySpan<byte> buffer)
{
if (_device == null)
{
throw new ObjectDisposedException("SPI Device");
}
_device.Write(buffer);
}
public override void TransferFullDuplex(ReadOnlySpan<byte> writeBuffer, Span<byte> readBuffer)
{
if (_device == null)
{
throw new ObjectDisposedException("SPI Device");
}
_device.TransferFullDuplex(writeBuffer, readBuffer);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (_device != null)
{
_device.Dispose();
foreach (int pin in _pins)
{
_board.ReleasePin(pin, PinUsage.Spi, this);
}
}
// Do not release pins a second time
_device = null!;
}
base.Dispose(disposing);
}
/// <summary>
/// Query the component information (the tree of active drivers) for diagnostic purposes.
/// </summary>
/// <returns>A <see cref="ComponentInformation"/> instance</returns>
public ComponentInformation QueryComponentInformation()
{
return new ComponentInformation(this, $"SPI Bus Manager, Bus number {_device.ConnectionSettings.BusId}");
}
}
}