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
use std::sync::{Arc, Mutex};
use map::{SystemMemory, MemoryMap};
use {Result, Error, Logic, DigitalLogic, DigitalWrite, DigitalRead, CPU, RegisterOperations, delay_ms};
use super::{PeripheralsBase, BCM2708, BCM2709, GPIORegister, GPIOFunctionSelect, PullUpDnControl};
pub struct GPIOBase(Mutex<MemoryMap>);
pub struct GPIO {
gpio_base: Arc<GPIOBase>
}
impl GPIO {
pub fn new(cpu: CPU) -> Result<GPIO> {
let ptr = match cpu {
CPU::BCM2708 => BCM2708::PERI_BASE + BCM2708::GPIO_BASE,
CPU::BCM2709 => BCM2709::PERI_BASE + BCM2709::GPIO_BASE,
CPU::Unknown => return Err(Error::UnsupportedHardware),
};
let sys_mem = try!(SystemMemory::new());
let gpio_base = try!(sys_mem.mmap(ptr));
Ok(GPIO {
gpio_base: Arc::new(GPIOBase(Mutex::new(gpio_base)))
})
}
pub unsafe fn pin(&self, pin: usize) -> PinOptions {
PinOptions {
gpio_base: self.gpio_base.clone(),
pin: pin,
pull_ctrl: Some(PullUpDnControl::PullOff),
default_value: 0
}
}
}
pub struct PinOptions {
gpio_base: Arc<GPIOBase>,
pin: usize,
pull_ctrl: Option<PullUpDnControl>,
default_value: usize
}
impl PinOptions {
pub fn pin(&mut self, pin: usize) -> &mut PinOptions {
self.pin = pin; self
}
pub fn pull_up(&mut self) -> &mut PinOptions {
self.pull_ctrl = Some(PullUpDnControl::PullUp); self
}
pub fn pull_down(&mut self) -> &mut PinOptions {
self.pull_ctrl = Some(PullUpDnControl::PullDown); self
}
pub fn pull_off(&mut self) -> &mut PinOptions {
self.pull_ctrl = Some(PullUpDnControl::PullOff); self
}
pub fn set(&mut self, value: usize) -> &mut PinOptions {
self.default_value = value; self
}
pub fn input(&self) -> PinInput {
{
let gpio_base = match self.gpio_base.0.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
let func_reg = gpio_base.register(GPIORegister::GPIOFunctionSelect(self.pin/10));
let shift = (self.pin % 10) * 3;
func_reg.bitand(!(0b111 << shift));
}
let pin = PinInput { gpio_base: self.gpio_base.clone(), pin: self.pin };
match self.pull_ctrl {
Some(ctrl) => pin.pull_mode(ctrl),
None => ()
}
pin
}
pub fn output(&self) -> PinOutput {
{
let gpio_base = match self.gpio_base.0.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
let func_reg = gpio_base.register(GPIORegister::GPIOFunctionSelect(self.pin/10));
let func_shift = (self.pin % 10) * 3;
func_reg.bitand(!(0b111 << func_shift));
let output_reg = match self.default_value {
0 => gpio_base.register(GPIORegister::GPIOPinOutputClear(self.pin/32)),
_ => gpio_base.register(GPIORegister::GPIOPinOutputSet(self.pin/32))
};
let output_shift = self.pin % 32;
output_reg.write(1 << output_shift);
let bits = GPIOFunctionSelect::GPIOFunctionOutput.bits();
func_reg.bitor(bits << func_shift);
}
PinOutput { gpio_base: self.gpio_base.clone(), pin: self.pin }
}
}
pub struct PinInput {
gpio_base: Arc<GPIOBase>,
pin: usize
}
pub struct PinOutput {
gpio_base: Arc<GPIOBase>,
pin: usize
}
impl PinInput {
pub fn read(&self) -> Logic {
let gpio_base = match self.gpio_base.0.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
let level_reg = gpio_base.register(GPIORegister::GPIOPinLevel(self.pin/32));
let shift = self.pin % 32;
match level_reg.read() & (1 << shift) {
0 => Logic::Low,
_ => Logic::High
}
}
pub fn pull_up(&self) {
self.pull_mode(PullUpDnControl::PullUp);
}
pub fn pull_down(&self) {
self.pull_mode(PullUpDnControl::PullDown);
}
pub fn pull_off(&self) {
self.pull_mode(PullUpDnControl::PullOff);
}
fn pull_mode(&self, mode: PullUpDnControl) {
let gpio_base = match self.gpio_base.0.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
let enable_reg = gpio_base.register(GPIORegister::GPIOPinPullUpDownEnable);
let clock_reg = gpio_base.register(GPIORegister::GPIOPinPullUpDownEnableClock(self.pin/32));
let shift = self.pin % 32;
enable_reg.write(mode.bcm270x_pud()); delay_ms(5);
clock_reg.write(1 << shift); delay_ms(5);
enable_reg.write(0); delay_ms(5);
clock_reg.write(0); delay_ms(5);
}
}
impl DigitalRead for PinInput {
fn digital_read(&mut self) -> Result<Logic> {
Ok(self.read())
}
}
impl PinOutput {
pub fn write<L: DigitalLogic>(&self, value: L) {
let gpio_base = match self.gpio_base.0.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
let output_reg = match value.logic_level() {
Logic::Low => gpio_base.register(GPIORegister::GPIOPinOutputClear(self.pin/32)),
Logic::High => gpio_base.register(GPIORegister::GPIOPinOutputSet(self.pin/32))
};
let shift = self.pin % 32;
let bits = GPIOFunctionSelect::GPIOFunctionOutput.bits();
output_reg.write(bits << shift);
}
}
impl DigitalWrite for PinOutput {
fn digital_write<L: DigitalLogic>(&mut self, level: L) -> Result<()> {
self.write(level);
Ok(())
}
}
impl Drop for PinOutput {
fn drop(&mut self) {
let gpio_base = match self.gpio_base.0.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
let func_reg = gpio_base.register(GPIORegister::GPIOFunctionSelect(self.pin/10));
let shift = (self.pin % 10) * 3;
func_reg.bitand(!(0b111 << shift));
}
}