Skip to content

Commit 4b59e9c

Browse files
committed
Reworked support for hardware
Reworked concepts of processors, connector layouts and board revisions
1 parent 9ba5485 commit 4b59e9c

File tree

6 files changed

+255
-76
lines changed

6 files changed

+255
-76
lines changed

Raspberry.System/Board.cs

Lines changed: 109 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,29 @@ namespace Raspberry
1212
/// <summary>
1313
/// Represents the Raspberry Pi mainboard.
1414
/// </summary>
15-
/// <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>
15+
/// <remarks>
16+
/// 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/"/>.
17+
/// <see cref="http://www.raspberrypi-spy.co.uk/2012/09/checking-your-raspberry-pi-board-version/"/> for information.
18+
/// </remarks>
1619
public class Board
1720
{
1821
#region Fields
1922

2023
private static readonly Lazy<Board> board = new Lazy<Board>(LoadBoard);
21-
22-
private readonly Dictionary<string, string> settings;
23-
private readonly HashSet<string> raspberryPiProcessors = new HashSet<string>(new[]{ "BCM2708", "BCM2709" }, StringComparer.InvariantCultureIgnoreCase);
2424

25+
private readonly Dictionary<string, string> settings;
26+
private readonly Lazy<Model> model;
27+
private readonly Lazy<ConnectorPinout> connectorPinout;
28+
2529
#endregion
2630

2731
#region Instance Management
2832

2933
private Board(Dictionary<string, string> settings)
3034
{
35+
model = new Lazy<Model>(LoadModel);
36+
connectorPinout = new Lazy<ConnectorPinout>(LoadConnectorPinout);
37+
3138
this.settings = settings;
3239
}
3340

@@ -51,13 +58,19 @@ public static Board Current
5158
/// </value>
5259
public bool IsRaspberryPi
5360
{
54-
get { return raspberryPiProcessors.Contains(Processor); }
61+
get
62+
{
63+
return Processor != Processor.Unknown;
64+
}
5565
}
5666

5767
/// <summary>
58-
/// Gets the processor.
68+
/// Gets the processor name.
5969
/// </summary>
60-
public string Processor
70+
/// <value>
71+
/// The name of the processor.
72+
/// </value>
73+
public string ProcessorName
6174
{
6275
get
6376
{
@@ -66,6 +79,21 @@ public string Processor
6679
}
6780
}
6881

82+
/// <summary>
83+
/// Gets the processor.
84+
/// </summary>
85+
/// <value>
86+
/// The processor.
87+
/// </value>
88+
public Processor Processor
89+
{
90+
get
91+
{
92+
Processor processor;
93+
return Enum.TryParse(ProcessorName, true, out processor) ? processor : Processor.Unknown;
94+
}
95+
}
96+
6997
/// <summary>
7098
/// Gets the board firmware version.
7199
/// </summary>
@@ -117,79 +145,24 @@ public bool IsOverclocked
117145
/// <summary>
118146
/// Gets the model.
119147
/// </summary>
120-
/// <returns>The model name (<c>A</c> or <c>B</c>) if known; otherwise, <c>(char)0</c>.</returns>
121-
public char Model
148+
/// <value>
149+
/// The model.
150+
/// </value>
151+
public Model Model
122152
{
123-
get
124-
{
125-
var firmware = Firmware;
126-
switch(firmware & 0xFFFF)
127-
{
128-
case 0x7:
129-
case 0x8:
130-
case 0x9:
131-
return 'A';
132-
133-
case 0x2:
134-
case 0x3:
135-
case 0x4:
136-
case 0x5:
137-
case 0x6:
138-
case 0xd:
139-
case 0xe:
140-
case 0xf:
141-
case 0x10:
142-
return 'B';
143-
144-
case 0x1040:
145-
case 0x1041:
146-
return '2';
147-
148-
default:
149-
return (char)0;
150-
}
151-
}
153+
get { return model.Value; }
152154
}
153155

154156
/// <summary>
155-
/// Gets the board revision.
157+
/// Gets the connector revision.
156158
/// </summary>
157-
/// <returns>The board revision for the given <see cref="Model"/> if known; otherwise, <c>0</c>.</returns>
158-
public int Revision
159+
/// <value>
160+
/// The connector revision.
161+
/// </value>
162+
/// <remarks>See <see cref="http://raspi.tv/2014/rpi-gpio-quick-reference-updated-for-raspberry-pi-b"/> for more information.</remarks>
163+
public ConnectorPinout ConnectorPinout
159164
{
160-
get
161-
{
162-
var firmware = Firmware;
163-
switch (firmware & 0xFFFF)
164-
{
165-
case 0x7:
166-
case 0x8:
167-
case 0x9:
168-
return 1; // Model A, rev1
169-
170-
case 0x2:
171-
case 0x3:
172-
return 1; // Model B, rev1
173-
174-
case 0x4:
175-
case 0x5:
176-
case 0x6:
177-
case 0xd:
178-
case 0xe:
179-
case 0xf:
180-
return 2; // Model B, rev2
181-
182-
case 0x10:
183-
return 3; // Model B+, rev3
184-
185-
case 0x1040:
186-
case 0x1041:
187-
return 4;
188-
189-
default:
190-
return 0; // Unknown
191-
}
192-
}
165+
get { return connectorPinout.Value; }
193166
}
194167

195168
#endregion
@@ -231,6 +204,68 @@ private static Board LoadBoard()
231204
}
232205
}
233206

207+
private Model LoadModel()
208+
{
209+
var firmware = Firmware;
210+
switch (firmware & 0xFFFF)
211+
{
212+
case 0x2:
213+
case 0x3:
214+
return Model.BRev1;
215+
216+
case 0x4:
217+
case 0x5:
218+
case 0x6:
219+
case 0xd:
220+
case 0xe:
221+
case 0xf:
222+
return Model.BRev2;
223+
224+
case 0x7:
225+
case 0x8:
226+
case 0x9:
227+
return Model.A;
228+
229+
case 0x10:
230+
return Model.BPlus;
231+
232+
case 0x11:
233+
return Model.ComputeModule;
234+
235+
case 0x12:
236+
return Model.APlus;
237+
238+
case 0x1040:
239+
case 0x1041:
240+
return Model.B2;
241+
242+
default:
243+
return Model.Unknown;
244+
}
245+
}
246+
247+
private ConnectorPinout LoadConnectorPinout()
248+
{
249+
switch (Model)
250+
{
251+
case Model.BRev1:
252+
return ConnectorPinout.Rev1;
253+
254+
case Model.BRev2:
255+
case Model.A:
256+
return ConnectorPinout.Rev2;
257+
258+
case Model.BPlus:
259+
case Model.ComputeModule:
260+
case Model.APlus:
261+
case Model.B2:
262+
return ConnectorPinout.Plus;
263+
264+
default:
265+
return ConnectorPinout.Unknown;
266+
}
267+
}
268+
234269
#endregion
235270
}
236271
}

Raspberry.System/ConnectorPinout.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace Raspberry
2+
{
3+
/// <summary>
4+
/// The Raspberry Pi connector pinout revision.
5+
/// </summary>
6+
public enum ConnectorPinout
7+
{
8+
/// <summary>
9+
/// Connector pinout is unknown.
10+
/// </summary>
11+
Unknown,
12+
13+
/// <summary>
14+
/// The first revision, as of Model B rev1.
15+
/// </summary>
16+
Rev1,
17+
18+
/// <summary>
19+
/// The second revision, as of Model B rev2.
20+
/// </summary>
21+
Rev2,
22+
23+
/// <summary>
24+
/// The third revision, as of Model B+.
25+
/// </summary>
26+
Plus,
27+
}
28+
}

Raspberry.System/Model.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.ComponentModel;
3+
4+
namespace Raspberry
5+
{
6+
/// <summary>
7+
/// The Rasperry Pi model.
8+
/// </summary>
9+
public enum Model
10+
{
11+
/// <summary>
12+
/// Unknown model.
13+
/// </summary>
14+
Unknown,
15+
16+
/// <summary>
17+
/// Model A.
18+
/// </summary>
19+
A,
20+
21+
/// <summary>
22+
/// Model A+.
23+
/// </summary>
24+
APlus,
25+
26+
/// <summary>
27+
/// Model B rev1.
28+
/// </summary>
29+
BRev1,
30+
31+
/// <summary>
32+
/// Model B rev2.
33+
/// </summary>
34+
BRev2,
35+
36+
/// <summary>
37+
/// Model B+.
38+
/// </summary>
39+
BPlus,
40+
41+
/// <summary>
42+
/// Compute module.
43+
/// </summary>
44+
ComputeModule,
45+
46+
/// <summary>
47+
/// Pi 2 Model B.
48+
/// </summary>
49+
B2
50+
}
51+
52+
/// <summary>
53+
/// Provides extension methods for <see cref="Model"/>.
54+
/// </summary>
55+
public static class ModelExtensionMethods
56+
{
57+
/// <summary>
58+
/// Gets the model display name.
59+
/// </summary>
60+
/// <param name="model">The model.</param>
61+
/// <returns>The display name, if known; otherwise, <c>null</c>.</returns>
62+
public static string GetDisplayName(this Model model)
63+
{
64+
switch (model)
65+
{
66+
case Model.Unknown:
67+
return null;
68+
case Model.A:
69+
return "Raspberry Pi Model A";
70+
case Model.APlus:
71+
return "Raspberry Pi Model A+";
72+
case Model.BRev1:
73+
return "Raspberry Pi Model B rev1";
74+
case Model.BRev2:
75+
return "Raspberry Pi Model B rev2";
76+
case Model.BPlus:
77+
return "Raspberry Pi Model B+";
78+
case Model.ComputeModule:
79+
return "Raspberry Pi Compute Module";
80+
case Model.B2:
81+
return "Raspberry Pi 2 Model B";
82+
83+
default:
84+
throw new ArgumentOutOfRangeException("model");
85+
}
86+
}
87+
}
88+
}

Raspberry.System/Processor.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Raspberry
2+
{
3+
/// <summary>
4+
/// The Raspberry Pi processor.
5+
/// </summary>
6+
public enum Processor
7+
{
8+
/// <summary>
9+
/// Processor is unknown.
10+
/// </summary>
11+
Unknown,
12+
13+
/// <summary>
14+
/// Processor is a BCM2708.
15+
/// </summary>
16+
Bcm2708,
17+
18+
/// <summary>
19+
/// Processor is a BCM2709.
20+
/// </summary>
21+
Bcm2709
22+
}
23+
}

0 commit comments

Comments
 (0)