forked from raspberry-sharp/raspberry-sharp-system
/
Board.cs
296 lines (250 loc) · 7.98 KB
/
Board.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
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
#region References
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
#endregion
namespace Raspberry
{
/// <summary>
/// Represents the Raspberry Pi mainboard.
/// </summary>
/// <remarks>Version and revisions are based on <see cref="http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/"/>.</remarks>
public class Board
{
#region Fields
private static readonly Lazy<Board> board = new Lazy<Board>(LoadBoard);
private readonly Dictionary<string, string> settings;
private readonly Lazy<Model> model;
private readonly Lazy<ConnectorPinout> connectorPinout;
#endregion
#region Instance Management
private Board(Dictionary<string, string> settings)
{
model = new Lazy<Model>(LoadModel);
connectorPinout = new Lazy<ConnectorPinout>(LoadConnectorPinout);
this.settings = settings;
}
#endregion
#region Properties
/// <summary>
/// Gets the current mainboard configuration.
/// </summary>
public static Board Current
{
get { return board.Value; }
}
/// <summary>
/// Gets a value indicating whether this instance is a Raspberry Pi.
/// </summary>
/// <value>
/// <c>true</c> if this instance is a Raspberry Pi; otherwise, <c>false</c>.
/// </value>
public bool IsRaspberryPi
{
get
{
return Processor != Processor.Unknown;
}
}
/// <summary>
/// Gets the processor name.
/// </summary>
/// <value>
/// The name of the processor.
/// </value>
public string ProcessorName
{
get
{
string hardware;
return settings.TryGetValue("Hardware", out hardware) ? hardware : null;
}
}
/// <summary>
/// Gets the processor.
/// </summary>
/// <value>
/// The processor.
/// </value>
public Processor Processor
{
get
{
Processor processor;
if (Enum.TryParse(ProcessorName, true, out processor))
{
// Check to see if we're dealing with a Pi 4 Model B
// The Pi 4 Model B currently lies to us and tells us that it's a BCM2835
if (processor == Processor.Bcm2835 && Model == Model.Pi4)
{
processor = Processor.Bcm2711;
}
return processor;
}
return Processor.Unknown;
}
}
/// <summary>
/// Gets the board firmware version.
/// </summary>
public int Firmware
{
get
{
string revision;
int firmware;
if (settings.TryGetValue("Revision", out revision)
&& !string.IsNullOrEmpty(revision)
&& int.TryParse(revision, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out firmware))
return firmware;
return 0;
}
}
/// <summary>
/// Gets the serial number.
/// </summary>
public string SerialNumber
{
get {
string serial;
if (settings.TryGetValue("Serial", out serial)
&& !string.IsNullOrEmpty(serial))
return serial;
return null;
}
}
/// <summary>
/// Gets a value indicating whether Raspberry Pi board is overclocked.
/// </summary>
/// <value>
/// <c>true</c> if Raspberry Pi is overclocked; otherwise, <c>false</c>.
/// </value>
public bool IsOverclocked
{
get
{
var firmware = Firmware;
return (firmware & 0xFFFF0000) != 0;
}
}
/// <summary>
/// Gets the model.
/// </summary>
/// <value>
/// The model.
/// </value>
public Model Model
{
get { return model.Value; }
}
/// <summary>
/// Gets the connector revision.
/// </summary>
/// <value>
/// The connector revision.
/// </value>
/// <remarks>See <see cref="http://raspi.tv/2014/rpi-gpio-quick-reference-updated-for-raspberry-pi-b"/> for more information.</remarks>
public ConnectorPinout ConnectorPinout
{
get { return connectorPinout.Value; }
}
#endregion
#region Private Helpers
private static Board LoadBoard()
{
try
{
const string filePath = "/proc/cpuinfo";
var cpuInfo = File.ReadAllLines(filePath);
var settings = new Dictionary<string, string>();
var suffix = string.Empty;
foreach(var l in cpuInfo)
{
var separator = l.IndexOf(':');
if (!string.IsNullOrWhiteSpace(l) && separator > 0)
{
var key = l.Substring(0, separator).Trim();
var val = l.Substring(separator + 1).Trim();
if (string.Equals(key, "processor", StringComparison.InvariantCultureIgnoreCase))
suffix = "." + val;
settings.Add(key + suffix, val);
}
else
suffix = "";
}
return new Board(settings);
}
catch
{
return new Board(new Dictionary<string, string>());
}
}
private Model LoadModel()
{
var firmware = Firmware;
switch (firmware & 0xFFFF)
{
case 0x2:
case 0x3:
return Model.BRev1;
case 0x4:
case 0x5:
case 0x6:
case 0xd:
case 0xe:
case 0xf:
return Model.BRev2;
case 0x7:
case 0x8:
case 0x9:
return Model.A;
case 0x10:
return Model.BPlus;
case 0x11:
return Model.ComputeModule;
case 0x12:
return Model.APlus;
case 0x1040:
case 0x1041:
return Model.B2;
case 0x0092:
case 0x0093:
return Model.Zero;
case 0x2082:
return Model.B3;
case 0x20A0:
return Model.ComputeModule3;
case 0x03111:
case 0x03112:
case 0x03114:
return Model.Pi4;
default:
return Model.Unknown;
}
}
private ConnectorPinout LoadConnectorPinout()
{
switch (Model)
{
case Model.BRev1:
return ConnectorPinout.Rev1;
case Model.BRev2:
case Model.A:
return ConnectorPinout.Rev2;
case Model.BPlus:
case Model.ComputeModule:
case Model.APlus:
case Model.B2:
case Model.Zero:
case Model.B3:
case Model.ComputeModule3:
case Model.Pi4:
return ConnectorPinout.Plus;
default:
return ConnectorPinout.Unknown;
}
}
#endregion
}
}