@@ -20,11 +20,8 @@ Some pins that could be configured here may be missing from actual MCU depending
2020on the package.
2121*/
2222
23- use core:: option:: Option ;
24-
2523use super :: sim;
2624
27-
2825/// A pin.
2926#[ allow( missing_doc) ]
3027pub struct Pin {
@@ -79,21 +76,20 @@ pub enum SlewRate {
7976}
8077
8178impl Pin {
82- /// Create and setup a Pin.
79+ /// Create and setup a Pin in open-drain mode .
8380 pub fn new ( port : Port , pin_index : u8 , function : Function ,
84- gpiodir : Option < :: hal :: pin :: GPIODirection > ) -> Pin {
81+ pull : PullConf , open_drain : bool ) -> Pin {
8582 let pin = Pin {
8683 port : port,
8784 pin : pin_index,
8885 } ;
89- pin. setup_regs ( function, gpiodir , PullNone ,
90- DriveStrengthHigh , SlewSlow , false , false ) ;
86+ pin. setup_regs ( function, pull , DriveStrengthHigh , SlewSlow ,
87+ false , open_drain ) ;
9188
9289 pin
9390 }
9491
9592 fn setup_regs ( & self , function : Function ,
96- gpiodir : Option < :: hal:: pin:: GPIODirection > ,
9793 pull : PullConf , drive_strength : DriveStrength ,
9894 slew_rate : SlewRate , filter : bool , open_drain : bool ) {
9995 // enable port clock
@@ -121,20 +117,6 @@ impl Pin {
121117 . set_ode ( open_drain)
122118 . set_dse ( dse)
123119 . 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- }
138120 }
139121
140122 fn pcr ( & self ) -> & ' static reg:: PORT_pcr {
@@ -149,21 +131,51 @@ impl Pin {
149131 }
150132}
151133
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 {
153165 /// Sets output GPIO value to high.
154166 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 ) ;
156168 }
157169
158170 /// Sets output GPIO value to low.
159171 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 ) ;
161173 }
162174
163175 /// Returns input GPIO level.
164176 fn level ( & self ) -> :: hal:: pin:: GPIOLevel {
165177 let reg = self . gpioreg ( ) ;
166- match reg. pdir . pdi ( self . pin as uint ) {
178+ match reg. pdir . pdi ( self . pin . pin as uint ) {
167179 false => :: hal:: pin:: Low ,
168180 _ => :: hal:: pin:: High ,
169181 }
@@ -176,7 +188,7 @@ impl ::hal::pin::GPIO for Pin {
176188 :: hal:: pin:: In => reg:: INPUT ,
177189 :: hal:: pin:: Out => reg:: OUTPUT ,
178190 } ;
179- reg. pddr . set_pdd ( self . pin as uint , val) ;
191+ reg. pddr . set_pdd ( self . pin . pin as uint , val) ;
180192 }
181193}
182194
0 commit comments