@@ -20,11 +20,8 @@ Some pins that could be configured here may be missing from actual MCU depending
20
20
on the package.
21
21
*/
22
22
23
- use core:: option:: Option ;
24
-
25
23
use super :: sim;
26
24
27
-
28
25
/// A pin.
29
26
#[ allow( missing_doc) ]
30
27
pub struct Pin {
@@ -79,21 +76,20 @@ pub enum SlewRate {
79
76
}
80
77
81
78
impl Pin {
82
- /// Create and setup a Pin.
79
+ /// Create and setup a Pin in open-drain mode .
83
80
pub fn new ( port : Port , pin_index : u8 , function : Function ,
84
- gpiodir : Option < :: hal :: pin :: GPIODirection > ) -> Pin {
81
+ pull : PullConf , open_drain : bool ) -> Pin {
85
82
let pin = Pin {
86
83
port : port,
87
84
pin : pin_index,
88
85
} ;
89
- pin. setup_regs ( function, gpiodir , PullNone ,
90
- DriveStrengthHigh , SlewSlow , false , false ) ;
86
+ pin. setup_regs ( function, pull , DriveStrengthHigh , SlewSlow ,
87
+ false , open_drain ) ;
91
88
92
89
pin
93
90
}
94
91
95
92
fn setup_regs ( & self , function : Function ,
96
- gpiodir : Option < :: hal:: pin:: GPIODirection > ,
97
93
pull : PullConf , drive_strength : DriveStrength ,
98
94
slew_rate : SlewRate , filter : bool , open_drain : bool ) {
99
95
// enable port clock
@@ -121,20 +117,6 @@ impl Pin {
121
117
. set_ode ( open_drain)
122
118
. set_dse ( dse)
123
119
. set_mux ( function as u32 ) ;
124
-
125
- if function == GPIO {
126
- ( self as & :: hal:: pin:: GPIO ) . set_direction ( gpiodir. unwrap ( ) ) ;
127
- }
128
- }
129
-
130
- fn gpioreg ( & self ) -> & ' static reg:: GPIO {
131
- match self . port {
132
- PortA => & reg:: GPIOA ,
133
- PortB => & reg:: GPIOB ,
134
- PortC => & reg:: GPIOC ,
135
- PortD => & reg:: GPIOD ,
136
- PortE => & reg:: GPIOE ,
137
- }
138
120
}
139
121
140
122
fn pcr ( & self ) -> & ' static reg:: PORT_pcr {
@@ -149,21 +131,51 @@ impl Pin {
149
131
}
150
132
}
151
133
152
- impl :: hal:: pin:: GPIO for Pin {
134
+ /// A pin configured as a GPIO
135
+ pub struct GpioPin {
136
+ pin : Pin
137
+ }
138
+
139
+ impl GpioPin {
140
+ /// Configure a `Pin` as a GPIO pin.
141
+ pub fn from_pin ( pin : Pin , gpiodir : :: hal:: pin:: GPIODirection ) -> GpioPin {
142
+ let pin = GpioPin { pin : pin} ;
143
+ ( & pin as & :: hal:: pin:: GPIO ) . set_direction ( gpiodir) ;
144
+ pin
145
+ }
146
+
147
+ /// Create and setup a GPIO Pin.
148
+ pub fn new ( port : Port , pin_index : u8 ,
149
+ gpiodir : :: hal:: pin:: GPIODirection ) -> GpioPin {
150
+ GpioPin :: from_pin ( Pin :: new ( port, pin_index, GPIO , PullNone , false ) , gpiodir)
151
+ }
152
+
153
+ fn gpioreg ( & self ) -> & ' static reg:: GPIO {
154
+ match self . pin . port {
155
+ PortA => & reg:: GPIOA ,
156
+ PortB => & reg:: GPIOB ,
157
+ PortC => & reg:: GPIOC ,
158
+ PortD => & reg:: GPIOD ,
159
+ PortE => & reg:: GPIOE ,
160
+ }
161
+ }
162
+ }
163
+
164
+ impl :: hal:: pin:: GPIO for GpioPin {
153
165
/// Sets output GPIO value to high.
154
166
fn set_high ( & self ) {
155
- self . gpioreg ( ) . psor . set_ptso ( self . pin as uint , true ) ;
167
+ self . gpioreg ( ) . psor . set_ptso ( self . pin . pin as uint , true ) ;
156
168
}
157
169
158
170
/// Sets output GPIO value to low.
159
171
fn set_low ( & self ) {
160
- self . gpioreg ( ) . pcor . set_ptco ( self . pin as uint , true ) ;
172
+ self . gpioreg ( ) . pcor . set_ptco ( self . pin . pin as uint , true ) ;
161
173
}
162
174
163
175
/// Returns input GPIO level.
164
176
fn level ( & self ) -> :: hal:: pin:: GPIOLevel {
165
177
let reg = self . gpioreg ( ) ;
166
- match reg. pdir . pdi ( self . pin as uint ) {
178
+ match reg. pdir . pdi ( self . pin . pin as uint ) {
167
179
false => :: hal:: pin:: Low ,
168
180
_ => :: hal:: pin:: High ,
169
181
}
@@ -176,7 +188,7 @@ impl ::hal::pin::GPIO for Pin {
176
188
:: hal:: pin:: In => reg:: INPUT ,
177
189
:: hal:: pin:: Out => reg:: OUTPUT ,
178
190
} ;
179
- reg. pddr . set_pdd ( self . pin as uint , val) ;
191
+ reg. pddr . set_pdd ( self . pin . pin as uint , val) ;
180
192
}
181
193
}
182
194
0 commit comments