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
use {Result, Error};
use sys::{cpuinfo, memory};
use std::{fmt, result};
use self::Hardware::{
RaspberryPi
};
#[derive(Clone, Copy, Debug)]
pub struct Board {
pub hardware: Hardware,
pub cpu: CPU,
pub memory: u32,
pub overvolted: bool
}
#[derive(Clone, Copy, Debug)]
pub enum Hardware {
RaspberryPi(RaspberryModel, RaspberryRevision, RaspberryMaker),
Unknown
}
#[derive(Clone, Copy, Debug)]
pub enum CPU {
BCM2708,
BCM2709,
Unknown
}
#[derive(Clone, Copy, Debug)]
pub enum RaspberryModel { A, B, BP, AP, CM, B2, UN }
impl<'a> From<&'a RaspberryModel> for &'static str {
fn from(model: &'a RaspberryModel) -> Self {
match *model {
RaspberryModel::A => "Model A",
RaspberryModel::B => "Model B",
RaspberryModel::BP => "Model B+",
RaspberryModel::AP => "Model A+",
RaspberryModel::CM => "Compute Module",
RaspberryModel::B2 => "Model 2B",
RaspberryModel::UN => "Unknown"
}
}
}
impl fmt::Display for RaspberryModel {
fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
let s: &'static str = self.into();
try!(write!(f, "{}", s));
Ok(())
}
}
#[derive(Clone, Copy, Debug)]
pub enum RaspberryRevision { V1, V11, V12, V2, UN }
impl RaspberryRevision {
const PIN_TO_GPIO: [usize; 32] = [
17, 18, 27, 22, 23, 24, 25, 4,
2, 3, 8, 7, 10, 9, 11, 14, 15,
28, 29, 30, 31,
5, 6, 13, 19, 26, 12, 16, 20, 21, 0, 1
];
}
impl<'a> From<&'a RaspberryRevision> for &'static str {
fn from(rev: &'a RaspberryRevision) -> Self {
match *rev {
RaspberryRevision::V1 => "1",
RaspberryRevision::V11 => "1.1",
RaspberryRevision::V12 => "1.2",
RaspberryRevision::V2 => "2",
RaspberryRevision::UN => "Unknown"
}
}
}
impl fmt::Display for RaspberryRevision {
fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
let s: &'static str = self.into();
try!(write!(f, "{}", s));
Ok(())
}
}
#[derive(Clone, Copy, Debug)]
pub enum RaspberryMaker { Egoman, Sony, Qisda, MBest, Unknown }
impl<'a> From<&'a RaspberryMaker> for &'static str {
fn from(maker: &'a RaspberryMaker) -> Self {
match *maker {
RaspberryMaker::Egoman => "Egoman",
RaspberryMaker::Sony => "Sony",
RaspberryMaker::Qisda => "Qisda",
RaspberryMaker::MBest => "MBest",
RaspberryMaker::Unknown => "Unknown"
}
}
}
impl fmt::Display for RaspberryMaker {
fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
let s: &'static str = self.into();
try!(write!(f, "{}", s));
Ok(())
}
}
impl Board {
pub fn pin_to_gpio(&self, pin: usize) -> Result<usize> {
match self.hardware {
Hardware::RaspberryPi(model, _, _) => {
let max_pins = match model {
RaspberryModel::BP |
RaspberryModel::AP |
RaspberryModel::B2 |
RaspberryModel::CM => 32,
_ => 21
};
if pin >= max_pins {
return Err(Error::UnconnectedPin);
};
Ok(RaspberryRevision::PIN_TO_GPIO[pin])
},
Hardware::Unknown => Err(Error::UnsupportedHardware)
}
}
}
pub fn board() -> Result<Board> {
let cpuinfo = try!(cpuinfo());
let memory = try!(memory());
let rev = cpuinfo.0.get("Revision");
match cpuinfo.0.get("Hardware") {
Some(hardware) => match hardware.as_str() {
"BCM2708" => {
match rev {
Some(ref rev) => {
let size = rev.len();
let overvolted = size > 4;
let revision: &str = &rev[size-4..size];
let hardware = match revision.as_ref() {
"0002" => RaspberryPi(RaspberryModel::B, RaspberryRevision::V1, RaspberryMaker::Egoman),
"0003" => RaspberryPi(RaspberryModel::B, RaspberryRevision::V11, RaspberryMaker::Egoman),
"0004" | "000e" => RaspberryPi(RaspberryModel::B, RaspberryRevision::V2, RaspberryMaker::Sony),
"0005" | "0009" => RaspberryPi(RaspberryModel::B, RaspberryRevision::V2, RaspberryMaker::Qisda),
"0006" | "0007" | "000d" | "000f" => RaspberryPi(RaspberryModel::B, RaspberryRevision::V2, RaspberryMaker::Egoman),
"0008" => RaspberryPi(RaspberryModel::A, RaspberryRevision::V2, RaspberryMaker::Sony),
"0010" => RaspberryPi(RaspberryModel::BP, RaspberryRevision::V12, RaspberryMaker::Sony),
"0011" | "0014" => RaspberryPi(RaspberryModel::CM, RaspberryRevision::V12, RaspberryMaker::Sony),
"0012" => RaspberryPi(RaspberryModel::AP, RaspberryRevision::V12, RaspberryMaker::Sony),
"0013" => RaspberryPi(RaspberryModel::BP, RaspberryRevision::V12, RaspberryMaker::MBest),
_ => RaspberryPi(RaspberryModel::UN, RaspberryRevision::UN, RaspberryMaker::Unknown)
};
Ok(Board { hardware: hardware, memory: memory.total, cpu: CPU::BCM2708, overvolted: overvolted })
},
None => Ok(Board { hardware: Hardware::Unknown, memory: memory.total, cpu: CPU::BCM2708, overvolted: false })
}
},
"BCM2709" => Ok(Board { hardware: RaspberryPi(RaspberryModel::B2, RaspberryRevision::V11, RaspberryMaker::Sony), memory: memory.total, cpu: CPU::BCM2709, overvolted: false }),
_ => Ok(Board { hardware: Hardware::Unknown, memory: memory.total, cpu: CPU::Unknown, overvolted: false }),
},
None => Ok(Board { hardware: Hardware::Unknown, memory: memory.total, cpu: CPU::Unknown, overvolted: false })
}
}